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;
56 * For each neighbour we keep a list of messages
57 * that we still want to transmit to the neighbour.
63 * This is a doubly linked list.
65 struct MessageQueue *next;
68 * This is a doubly linked list.
70 struct MessageQueue *prev;
73 * Once this message is actively being transmitted, which
74 * neighbour is it associated with?
76 struct NeighbourMapEntry *n;
79 * Function to call once we're done.
81 GST_NeighbourSendContinuation cont;
89 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
90 * stuck together in memory. Allocated at the end of this struct.
92 const char *message_buf;
95 * Size of the message buf
97 size_t message_buf_size;
100 * At what time should we fail?
102 struct GNUNET_TIME_Absolute timeout;
108 * Entry in neighbours.
110 struct NeighbourMapEntry
114 * Head of list of messages we would like to send to this peer;
115 * must contain at most one message per client.
117 struct MessageQueue *messages_head;
120 * Tail of list of messages we would like to send to this peer; must
121 * contain at most one message per client.
123 struct MessageQueue *messages_tail;
126 * Context for address suggestion.
127 * NULL after we are connected.
129 struct GNUNET_ATS_SuggestionContext *asc;
132 * Performance data for the peer.
134 struct GNUNET_TRANSPORT_ATS_Information *ats;
137 * Are we currently trying to send a message? If so, which one?
139 struct MessageQueue *is_active;
142 * Active session for communicating with the peer.
144 struct Session *session;
147 * Name of the plugin we currently use.
152 * Address used for communicating with the peer, NULL for inbound connections.
157 * Number of bytes in 'addr'.
162 * Identity of this neighbour.
164 struct GNUNET_PeerIdentity id;
167 * ID of task scheduled to run when this peer is about to
168 * time out (will free resources associated with the peer).
170 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
173 * ID of task scheduled to run when we should try transmitting
174 * the head of the message queue.
176 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
179 * Tracker for inbound bandwidth.
181 struct GNUNET_BANDWIDTH_Tracker in_tracker;
184 * How often has the other peer (recently) violated the inbound
185 * traffic limit? Incremented by 10 per violation, decremented by 1
186 * per non-violation (for each time interval).
188 unsigned int quota_violation_count;
191 * Number of values in 'ats' array.
193 unsigned int ats_count;
196 * Are we already in the process of disconnecting this neighbour?
201 * Do we currently consider this neighbour connected? (as far as
202 * the connect/disconnect callbacks are concerned)?
210 * All known neighbours and their HELLOs.
212 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
215 * Closure for connect_notify_cb and disconnect_notify_cb
217 static void *callback_cls;
220 * Function to call when we connected to a neighbour.
222 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
225 * Function to call when we disconnected from a neighbour.
227 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
230 * counter for connected neighbours
232 static int neighbours_connected;
235 * Lookup a neighbour entry in the neighbours hash map.
237 * @param pid identity of the peer to look up
238 * @return the entry, NULL if there is no existing record
240 static struct NeighbourMapEntry *
241 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
243 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
248 * Task invoked to start a transmission to another peer.
250 * @param cls the 'struct NeighbourMapEntry'
251 * @param tc scheduler context
254 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
258 * We're done with our transmission attempt, continue processing.
260 * @param cls the 'struct MessageQueue' of the message
261 * @param receiver intended receiver
262 * @param success whether it worked or not
265 transmit_send_continuation (void *cls,
266 const struct GNUNET_PeerIdentity *receiver,
269 struct MessageQueue *mq;
270 struct NeighbourMapEntry *n;
276 GNUNET_assert (n->is_active == mq);
278 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
279 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
281 if (NULL != mq->cont)
282 mq->cont (mq->cont_cls, success);
288 * Check the ready list for the given neighbour and if a plugin is
289 * ready for transmission (and if we have a message), do so!
291 * @param neighbour target peer for which to transmit
294 try_transmission_to_peer (struct NeighbourMapEntry *n)
296 struct MessageQueue *mq;
297 struct GNUNET_TIME_Relative timeout;
299 struct GNUNET_TRANSPORT_PluginFunctions *papi;
301 if (n->is_active != NULL)
302 return; /* transmission already pending */
303 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
304 return; /* currently waiting for bandwidth */
305 mq = n->messages_head;
306 while (NULL != (mq = n->messages_head))
308 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
309 if (timeout.rel_value > 0)
311 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
314 return; /* no more messages */
316 papi = GST_plugins_find (n->plugin_name);
322 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
327 papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
328 0 /* priority -- remove from plugin API? */ ,
329 timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
330 &transmit_send_continuation, mq);
333 /* failure, but 'send' would not call continuation in this case,
334 * so we need to do it here! */
335 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
336 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
342 * Task invoked to start a transmission to another peer.
344 * @param cls the 'struct NeighbourMapEntry'
345 * @param tc scheduler context
348 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
350 struct NeighbourMapEntry *n = cls;
352 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
353 try_transmission_to_peer (n);
358 * Initialize the neighbours subsystem.
360 * @param cls closure for callbacks
361 * @param connect_cb function to call if we connect to a peer
362 * @param disconnect_cb function to call if we disconnect from a peer
365 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
366 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
369 connect_notify_cb = connect_cb;
370 disconnect_notify_cb = disconnect_cb;
371 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
376 * Disconnect from the given neighbour, clean up the record.
378 * @param n neighbour to disconnect from
381 disconnect_neighbour (struct NeighbourMapEntry *n)
383 struct MessageQueue *mq;
385 if (GNUNET_YES == n->in_disconnect)
387 n->in_disconnect = GNUNET_YES;
388 while (NULL != (mq = n->messages_head))
390 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
391 mq->cont (mq->cont_cls, GNUNET_SYSERR);
394 if (NULL != n->is_active)
396 n->is_active->n = NULL;
399 if (GNUNET_YES == n->is_connected)
401 n->is_connected = GNUNET_NO;
403 GNUNET_assert (neighbours_connected > 0);
404 neighbours_connected--;
406 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
408 disconnect_notify_cb (callback_cls, &n->id);
410 GNUNET_assert (GNUNET_YES ==
411 GNUNET_CONTAINER_multihashmap_remove (neighbours,
412 &n->id.hashPubKey, n));
413 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
415 GNUNET_SCHEDULER_cancel (n->timeout_task);
416 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
418 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
420 GNUNET_SCHEDULER_cancel (n->timeout_task);
421 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
425 GNUNET_ATS_suggest_address_cancel (n->asc);
428 GNUNET_array_grow (n->ats, n->ats_count, 0);
429 if (NULL != n->plugin_name)
431 GNUNET_free (n->plugin_name);
432 n->plugin_name = NULL;
436 GNUNET_free (n->addr);
446 * Peer has been idle for too long. Disconnect.
448 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
449 * @param tc scheduler context
452 neighbour_timeout_task (void *cls,
453 const struct GNUNET_SCHEDULER_TaskContext *tc)
455 struct NeighbourMapEntry *n = cls;
457 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
458 disconnect_neighbour (n);
463 * Disconnect from the given neighbour.
466 * @param key hash of neighbour's public key (not used)
467 * @param value the 'struct NeighbourMapEntry' of the neighbour
470 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
472 struct NeighbourMapEntry *n = value;
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
476 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
478 disconnect_neighbour (n);
484 * Cleanup the neighbours subsystem.
487 GST_neighbours_stop ()
489 GNUNET_assert (neighbours != NULL);
491 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
493 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
494 GNUNET_assert (neighbours_connected == 0);
497 connect_notify_cb = NULL;
498 disconnect_notify_cb = NULL;
503 * For an existing neighbour record, set the active connection to
506 * @param peer identity of the peer to switch the address for
507 * @param plugin_name name of transport that delivered the PONG
508 * @param address address of the other peer, NULL if other peer
510 * @param address_len number of bytes in address
511 * @param session session to use (or NULL)
512 * @param ats performance data
513 * @param ats_count number of entries in ats (excluding 0-termination)
516 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
517 const char *plugin_name, const void *address,
518 size_t address_len, struct Session *session,
519 const struct GNUNET_TRANSPORT_ATS_Information
520 *ats, uint32_t ats_count)
522 struct NeighbourMapEntry *n;
523 struct GNUNET_MessageHeader connect_msg;
525 GNUNET_assert (neighbours != NULL);
527 n = lookup_neighbour (peer);
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
537 GNUNET_i2s (peer), plugin_name,
538 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
544 GNUNET_free_non_null (n->addr);
545 n->addr = GNUNET_malloc (address_len);
546 memcpy (n->addr, address, address_len);
547 n->addrlen = address_len;
548 n->session = session;
549 GNUNET_array_grow (n->ats, n->ats_count, ats_count);
551 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
552 GNUNET_free_non_null (n->plugin_name);
553 n->plugin_name = GNUNET_strdup (plugin_name);
554 GNUNET_SCHEDULER_cancel (n->timeout_task);
556 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
557 &neighbour_timeout_task, n);
558 connect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
559 connect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
560 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
561 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
566 * Try to connect to the target peer using the given address
568 * @param cls the 'struct NeighbourMapEntry' of the target
569 * @param target identity of the target peer
570 * @param plugin_name name of the plugin
571 * @param plugin_address binary address
572 * @param plugin_address_len length of address
573 * @param bandwidth available bandwidth
574 * @param ats performance data for the address (as far as known)
575 * @param ats_count number of performance records in 'ats'
578 try_connect_using_address (void *cls, const struct GNUNET_PeerIdentity *target,
579 const char *plugin_name, const void *plugin_address,
580 size_t plugin_address_len, struct Session *session,
581 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
582 const struct GNUNET_TRANSPORT_ATS_Information *ats,
585 struct NeighbourMapEntry *n = cls;
589 was_connected = n->is_connected;
590 n->is_connected = GNUNET_YES;
591 GST_neighbours_switch_to_address (target, plugin_name, plugin_address,
592 plugin_address_len, session, ats,
594 if (GNUNET_YES == was_connected)
597 neighbours_connected++;
598 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
600 connect_notify_cb (callback_cls, target, n->ats, n->ats_count);
605 * Try to create a connection to the given target (eventually).
607 * @param target peer to try to connect to
610 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
612 struct NeighbourMapEntry *n;
614 GNUNET_assert (neighbours != NULL);
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
618 GNUNET_i2s (target));
622 memcmp (target, &GST_my_identity,
623 sizeof (struct GNUNET_PeerIdentity)));
624 n = lookup_neighbour (target);
625 if ((NULL != n) && (GNUNET_YES == n->is_connected))
626 return; /* already connected */
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Unknown peer `%s', creating new neighbour\n",
632 GNUNET_i2s (target));
634 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
636 GNUNET_array_grow (n->ats, n->ats_count, 1);
637 n->ats[0].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);;
638 n->ats[0].value = htonl (0);
639 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
640 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
641 MAX_BANDWIDTH_CARRY_S);
643 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
644 &neighbour_timeout_task, n);
645 GNUNET_assert (GNUNET_OK ==
646 GNUNET_CONTAINER_multihashmap_put (neighbours,
647 &n->id.hashPubKey, n,
648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
651 return; /* already trying */
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Asking ATS for suggested address to connect to peer `%s'\n",
655 GNUNET_i2s (target));
658 GNUNET_ATS_suggest_address (GST_ats, target, &try_connect_using_address,
664 * Test if we're connected to the given peer.
666 * @param target peer to test
667 * @return GNUNET_YES if we are connected, GNUNET_NO if not
670 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
672 struct NeighbourMapEntry *n;
674 GNUNET_assert (neighbours != NULL);
676 n = lookup_neighbour (target);
677 if ((NULL == n) || (n->is_connected != GNUNET_YES))
678 return GNUNET_NO; /* not connected */
684 * A session was terminated. Take note.
686 * @param peer identity of the peer where the session died
687 * @param session session that is gone
690 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
691 struct Session *session)
693 struct NeighbourMapEntry *n;
695 GNUNET_assert (neighbours != NULL);
697 n = lookup_neighbour (peer);
700 if (session != n->session)
701 return; /* doesn't affect us */
703 if (GNUNET_YES != n->is_connected)
704 return; /* not connected anymore anyway, shouldn't matter */
706 GNUNET_SCHEDULER_cancel (n->timeout_task);
708 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
709 &neighbour_timeout_task, n);
710 /* try QUICKLY to re-establish a connection, reduce timeout! */
713 /* how can this be!? */
718 GNUNET_ATS_suggest_address (GST_ats, peer, &try_connect_using_address, n);
723 * Transmit a message to the given target using the active connection.
725 * @param target destination
726 * @param msg message to send
727 * @param msg_size number of bytes in msg
728 * @param timeout when to fail with timeout
729 * @param cont function to call when done
730 * @param cont_cls closure for 'cont'
733 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
734 size_t msg_size, struct GNUNET_TIME_Relative timeout,
735 GST_NeighbourSendContinuation cont, void *cont_cls)
737 struct NeighbourMapEntry *n;
738 struct MessageQueue *mq;
740 GNUNET_assert (neighbours != NULL);
742 n = lookup_neighbour (target);
743 if ((n == NULL) || (GNUNET_YES != n->is_connected))
745 GNUNET_STATISTICS_update (GST_stats,
747 ("# messages not sent (no such peer or not connected)"),
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Could not send message to peer `%s': unknown neighbor",
753 GNUNET_i2s (target));
754 if (GNUNET_YES != n->is_connected)
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Could not send message to peer `%s': not connected\n",
757 GNUNET_i2s (target));
760 cont (cont_cls, GNUNET_SYSERR);
764 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
765 GNUNET_STATISTICS_update (GST_stats,
767 ("# bytes in message queue for other peers"),
768 msg_size, GNUNET_NO);
769 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
771 mq->cont_cls = cont_cls;
772 /* FIXME: this memcpy can be up to 7% of our total runtime! */
773 memcpy (&mq[1], msg, msg_size);
774 mq->message_buf = (const char *) &mq[1];
775 mq->message_buf_size = msg_size;
776 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
777 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
778 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
779 (NULL == n->is_active))
780 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
785 * We have received a message from the given sender. How long should
786 * we delay before receiving more? (Also used to keep the peer marked
789 * @param sender sender of the message
790 * @param size size of the message
791 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
792 * GNUNET_NO if the neighbour is not connected or violates the quota
793 * @return how long to wait before reading more from this sender
795 struct GNUNET_TIME_Relative
796 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
797 *sender, ssize_t size, int *do_forward)
799 struct NeighbourMapEntry *n;
800 struct GNUNET_TIME_Relative ret;
802 GNUNET_assert (neighbours != NULL);
804 n = lookup_neighbour (sender);
807 *do_forward = GNUNET_NO;
808 return GNUNET_TIME_UNIT_ZERO;
810 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
812 n->quota_violation_count++;
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
816 n->in_tracker.available_bytes_per_s__,
817 n->quota_violation_count);
819 /* Discount 32k per violation */
820 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
824 if (n->quota_violation_count > 0)
826 /* try to add 32k back */
827 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
828 n->quota_violation_count--;
831 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
833 GNUNET_STATISTICS_update (GST_stats,
835 ("# bandwidth quota violations by other peers"),
837 *do_forward = GNUNET_NO;
838 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
840 *do_forward = GNUNET_YES;
841 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
842 if (ret.rel_value > 0)
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
847 (unsigned long long) n->in_tracker.
848 consumption_since_last_update__,
849 (unsigned int) n->in_tracker.available_bytes_per_s__,
850 (unsigned long long) ret.rel_value);
852 GNUNET_STATISTICS_update (GST_stats,
853 gettext_noop ("# ms throttling suggested"),
854 (int64_t) ret.rel_value, GNUNET_NO);
861 * Keep the connection to the given neighbour alive longer,
862 * we received a KEEPALIVE (or equivalent).
864 * @param neighbour neighbour to keep alive
867 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
869 struct NeighbourMapEntry *n;
871 GNUNET_assert (neighbours != NULL);
873 n = lookup_neighbour (neighbour);
876 GNUNET_STATISTICS_update (GST_stats,
878 ("# KEEPALIVE messages discarded (not connected)"),
882 GNUNET_SCHEDULER_cancel (n->timeout_task);
884 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
885 &neighbour_timeout_task, n);
890 * Change the incoming quota for the given peer.
892 * @param neighbour identity of peer to change qutoa for
893 * @param quota new quota
896 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
897 struct GNUNET_BANDWIDTH_Value32NBO quota)
899 struct NeighbourMapEntry *n;
901 GNUNET_assert (neighbours != NULL);
903 n = lookup_neighbour (neighbour);
906 GNUNET_STATISTICS_update (GST_stats,
908 ("# SET QUOTA messages ignored (no such peer)"),
912 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
913 if (0 != ntohl (quota.value__))
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
917 GNUNET_i2s (&n->id), "SET_QUOTA");
919 GNUNET_STATISTICS_update (GST_stats,
920 gettext_noop ("# disconnects due to quota of 0"), 1,
922 disconnect_neighbour (n);
927 * Closure for the neighbours_iterate function.
929 struct IteratorContext
932 * Function to call on each connected neighbour.
934 GST_NeighbourIterator cb;
944 * Call the callback from the closure for each connected neighbour.
946 * @param cls the 'struct IteratorContext'
947 * @param key the hash of the public key of the neighbour
948 * @param value the 'struct NeighbourMapEntry'
949 * @return GNUNET_OK (continue to iterate)
952 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
954 struct IteratorContext *ic = cls;
955 struct NeighbourMapEntry *n = value;
957 if (GNUNET_YES != n->is_connected)
960 GNUNET_assert (n->ats_count > 0);
961 ic->cb (ic->cb_cls, &n->id, n->ats, n->ats_count);
967 * Iterate over all connected neighbours.
969 * @param cb function to call
970 * @param cb_cls closure for cb
973 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
975 struct IteratorContext ic;
977 GNUNET_assert (neighbours != NULL);
981 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
986 * If we have an active connection to the given target, it must be shutdown.
988 * @param target peer to disconnect from
991 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
993 struct NeighbourMapEntry *n;
994 struct GNUNET_TRANSPORT_PluginFunctions *papi;
995 struct GNUNET_MessageHeader disconnect_msg;
997 GNUNET_assert (neighbours != NULL);
999 n = lookup_neighbour (target);
1001 return; /* not active */
1002 if (GNUNET_YES == n->is_connected)
1004 /* we're actually connected, send DISCONNECT message */
1005 disconnect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
1006 disconnect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1007 papi = GST_plugins_find (n->plugin_name);
1009 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1010 sizeof (struct GNUNET_MessageHeader),
1011 UINT32_MAX /* priority */ ,
1012 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1013 GNUNET_YES, NULL, NULL);
1015 disconnect_neighbour (n);
1019 /* end of file gnunet-service-transport_neighbours.c */