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_constants.h"
35 #include "transport.h"
39 * Size of the neighbour hash map.
41 #define NEIGHBOUR_TABLE_SIZE 256
44 * How often must a peer violate bandwidth quotas before we start
45 * to simply drop its messages?
47 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
50 * How often do we send KEEPALIVE messages to each of our neighbours?
51 * (idle timeout is 5 minutes or 300 seconds, so with 90s interval we
52 * send 3 keepalives in each interval, so 3 messages would need to be
53 * lost in a row for a disconnect).
55 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
59 * Entry in neighbours.
61 struct NeighbourMapEntry;
64 * Message a peer sends to another to indicate its
65 * preference for communicating via a particular
66 * session (and the desire to establish a real
69 struct SessionConnectMessage
72 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
74 struct GNUNET_MessageHeader header;
79 uint32_t reserved GNUNET_PACKED;
82 * Absolute time at the sender. Only the most recent connect
83 * message implies which session is preferred by the sender.
85 struct GNUNET_TIME_AbsoluteNBO timestamp;
90 struct SessionDisconnectMessage
93 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
95 struct GNUNET_MessageHeader header;
100 uint32_t reserved GNUNET_PACKED;
103 * Purpose of the signature. Extends over the timestamp.
104 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
106 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
109 * Absolute time at the sender. Only the most recent connect
110 * message implies which session is preferred by the sender.
112 struct GNUNET_TIME_AbsoluteNBO timestamp;
115 * Signature of the peer that sends us the disconnect. Only
116 * valid if the timestamp is AFTER the timestamp from the
117 * corresponding 'CONNECT' message.
119 struct GNUNET_CRYPTO_RsaSignature signature;
124 * For each neighbour we keep a list of messages
125 * that we still want to transmit to the neighbour.
131 * This is a doubly linked list.
133 struct MessageQueue *next;
136 * This is a doubly linked list.
138 struct MessageQueue *prev;
141 * Once this message is actively being transmitted, which
142 * neighbour is it associated with?
144 struct NeighbourMapEntry *n;
147 * Function to call once we're done.
149 GST_NeighbourSendContinuation cont;
157 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
158 * stuck together in memory. Allocated at the end of this struct.
160 const char *message_buf;
163 * Size of the message buf
165 size_t message_buf_size;
168 * At what time should we fail?
170 struct GNUNET_TIME_Absolute timeout;
176 * Entry in neighbours.
178 struct NeighbourMapEntry
182 * Head of list of messages we would like to send to this peer;
183 * must contain at most one message per client.
185 struct MessageQueue *messages_head;
188 * Tail of list of messages we would like to send to this peer; must
189 * contain at most one message per client.
191 struct MessageQueue *messages_tail;
194 * Performance data for the peer.
196 //struct GNUNET_TRANSPORT_ATS_Information *ats;
199 * Are we currently trying to send a message? If so, which one?
201 struct MessageQueue *is_active;
204 * Active session for communicating with the peer.
206 struct Session *session;
209 * Name of the plugin we currently use.
214 * Address used for communicating with the peer, NULL for inbound connections.
219 * Number of bytes in 'addr'.
224 * Identity of this neighbour.
226 struct GNUNET_PeerIdentity id;
229 * ID of task scheduled to run when this peer is about to
230 * time out (will free resources associated with the peer).
232 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
235 * ID of task scheduled to send keepalives.
237 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
240 * ID of task scheduled to run when we should try transmitting
241 * the head of the message queue.
243 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
246 * Tracker for inbound bandwidth.
248 struct GNUNET_BANDWIDTH_Tracker in_tracker;
251 * How often has the other peer (recently) violated the inbound
252 * traffic limit? Incremented by 10 per violation, decremented by 1
253 * per non-violation (for each time interval).
255 unsigned int quota_violation_count;
258 * Number of values in 'ats' array.
260 //unsigned int ats_count;
263 * Are we already in the process of disconnecting this neighbour?
268 * Do we currently consider this neighbour connected? (as far as
269 * the connect/disconnect callbacks are concerned)?
277 * All known neighbours and their HELLOs.
279 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
282 * Closure for connect_notify_cb and disconnect_notify_cb
284 static void *callback_cls;
287 * Function to call when we connected to a neighbour.
289 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
292 * Function to call when we disconnected from a neighbour.
294 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
297 * counter for connected neighbours
299 static int neighbours_connected;
302 * Lookup a neighbour entry in the neighbours hash map.
304 * @param pid identity of the peer to look up
305 * @return the entry, NULL if there is no existing record
307 static struct NeighbourMapEntry *
308 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
310 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
315 * Task invoked to start a transmission to another peer.
317 * @param cls the 'struct NeighbourMapEntry'
318 * @param tc scheduler context
321 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
325 * We're done with our transmission attempt, continue processing.
327 * @param cls the 'struct MessageQueue' of the message
328 * @param receiver intended receiver
329 * @param success whether it worked or not
332 transmit_send_continuation (void *cls,
333 const struct GNUNET_PeerIdentity *receiver,
336 struct MessageQueue *mq;
337 struct NeighbourMapEntry *n;
343 GNUNET_assert (n->is_active == mq);
345 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
346 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
348 if (NULL != mq->cont)
349 mq->cont (mq->cont_cls, success);
355 * Check the ready list for the given neighbour and if a plugin is
356 * ready for transmission (and if we have a message), do so!
358 * @param n target peer for which to transmit
361 try_transmission_to_peer (struct NeighbourMapEntry *n)
363 struct MessageQueue *mq;
364 struct GNUNET_TIME_Relative timeout;
366 struct GNUNET_TRANSPORT_PluginFunctions *papi;
368 if (n->is_active != NULL)
369 return; /* transmission already pending */
370 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
371 return; /* currently waiting for bandwidth */
372 while (NULL != (mq = n->messages_head))
374 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
375 if (timeout.rel_value > 0)
377 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
379 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
382 return; /* no more messages */
384 papi = GST_plugins_find (n->plugin_name);
390 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
394 if (((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0)))
396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No address peer for peer `%s'\n",
397 GNUNET_i2s (&n->id));
398 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
399 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
404 papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
405 0 /* priority -- remove from plugin API? */ ,
406 timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
407 &transmit_send_continuation, mq);
410 /* failure, but 'send' would not call continuation in this case,
411 * so we need to do it here! */
412 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
413 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
419 * Task invoked to start a transmission to another peer.
421 * @param cls the 'struct NeighbourMapEntry'
422 * @param tc scheduler context
425 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
427 struct NeighbourMapEntry *n = cls;
429 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
430 try_transmission_to_peer (n);
435 * Initialize the neighbours subsystem.
437 * @param cls closure for callbacks
438 * @param connect_cb function to call if we connect to a peer
439 * @param disconnect_cb function to call if we disconnect from a peer
442 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
443 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
446 connect_notify_cb = connect_cb;
447 disconnect_notify_cb = disconnect_cb;
448 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
453 * Disconnect from the given neighbour, clean up the record.
455 * @param n neighbour to disconnect from
458 disconnect_neighbour (struct NeighbourMapEntry *n)
460 struct MessageQueue *mq;
462 if (GNUNET_YES == n->in_disconnect)
464 n->in_disconnect = GNUNET_YES;
465 while (NULL != (mq = n->messages_head))
467 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
468 if (NULL != mq->cont)
469 mq->cont (mq->cont_cls, GNUNET_SYSERR);
472 if (NULL != n->is_active)
474 n->is_active->n = NULL;
477 if (GNUNET_YES == n->is_connected)
479 n->is_connected = GNUNET_NO;
480 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
481 GNUNET_SCHEDULER_cancel (n->keepalive_task);
482 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
483 GNUNET_assert (neighbours_connected > 0);
484 neighbours_connected--;
485 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
487 disconnect_notify_cb (callback_cls, &n->id);
489 GNUNET_assert (GNUNET_YES ==
490 GNUNET_CONTAINER_multihashmap_remove (neighbours,
491 &n->id.hashPubKey, n));
492 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
494 GNUNET_SCHEDULER_cancel (n->timeout_task);
495 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
497 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
499 GNUNET_SCHEDULER_cancel (n->transmission_task);
500 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
502 if (NULL != n->plugin_name)
504 GNUNET_free (n->plugin_name);
505 n->plugin_name = NULL;
509 GNUNET_free (n->addr);
519 * Peer has been idle for too long. Disconnect.
521 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
522 * @param tc scheduler context
525 neighbour_timeout_task (void *cls,
526 const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct NeighbourMapEntry *n = cls;
530 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
531 if (GNUNET_YES == n->is_connected)
532 GNUNET_STATISTICS_update (GST_stats,
533 gettext_noop ("# peers disconnected due to timeout"), 1,
535 disconnect_neighbour (n);
540 * Send another keepalive message.
542 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
543 * @param tc scheduler context
546 neighbour_keepalive_task (void *cls,
547 const struct GNUNET_SCHEDULER_TaskContext *tc)
549 struct NeighbourMapEntry *n = cls;
550 struct GNUNET_MessageHeader m;
551 struct GNUNET_TRANSPORT_PluginFunctions *papi;
553 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
554 GNUNET_assert (GNUNET_YES == n->is_connected);
555 GNUNET_STATISTICS_update (GST_stats,
556 gettext_noop ("# keepalives sent"), 1,
558 m.size = htons (sizeof (struct GNUNET_MessageHeader));
559 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
560 papi = GST_plugins_find (n->plugin_name);
562 papi->send (papi->cls,
563 &n->id, (const void *) &m,
565 UINT32_MAX /* priority */ ,
566 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
567 GNUNET_YES, NULL, NULL);
568 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
569 &neighbour_keepalive_task,
575 * Disconnect from the given neighbour.
578 * @param key hash of neighbour's public key (not used)
579 * @param value the 'struct NeighbourMapEntry' of the neighbour
582 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
584 struct NeighbourMapEntry *n = value;
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
588 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
590 if (GNUNET_YES == n->is_connected)
591 GNUNET_STATISTICS_update (GST_stats,
592 gettext_noop ("# peers disconnected due to global disconnect"), 1,
594 disconnect_neighbour (n);
600 * Cleanup the neighbours subsystem.
603 GST_neighbours_stop ()
605 GNUNET_assert (neighbours != NULL);
607 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
609 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
610 GNUNET_assert (neighbours_connected == 0);
613 connect_notify_cb = NULL;
614 disconnect_notify_cb = NULL;
619 * For an existing neighbour record, set the active connection to
622 * @param peer identity of the peer to switch the address for
623 * @param plugin_name name of transport that delivered the PONG
624 * @param address address of the other peer, NULL if other peer
626 * @param address_len number of bytes in address
627 * @param session session to use (or NULL)
628 * @param ats performance data
629 * @param ats_count number of entries in ats (excluding 0-termination)
632 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
633 const char *plugin_name, const void *address,
634 size_t address_len, struct Session *session,
635 const struct GNUNET_TRANSPORT_ATS_Information
636 *ats, uint32_t ats_count)
638 struct NeighbourMapEntry *n;
639 struct SessionConnectMessage connect_msg;
642 GNUNET_assert (neighbours != NULL);
644 n = lookup_neighbour (peer);
647 /* FIXME: ATS not fully implemented, once ATS only generates
648 these events for 'connected' addresses, things should be better... */
652 was_connected = n->is_connected;
653 n->is_connected = GNUNET_YES;
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
658 GNUNET_i2s (peer), plugin_name,
659 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
665 GNUNET_free_non_null (n->addr);
666 n->addr = GNUNET_malloc (address_len);
667 memcpy (n->addr, address, address_len);
668 n->addrlen = address_len;
669 n->session = session;
670 GNUNET_free_non_null (n->plugin_name);
671 n->plugin_name = GNUNET_strdup (plugin_name);
672 GNUNET_SCHEDULER_cancel (n->timeout_task);
674 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
675 &neighbour_timeout_task, n);
676 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
677 connect_msg.header.type =
678 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
679 connect_msg.reserved = htonl (0);
680 connect_msg.timestamp =
681 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
682 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
683 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
685 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task,
687 if (GNUNET_YES == was_connected)
689 /* First tell clients about connected neighbours...*/
690 neighbours_connected++;
691 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
693 connect_notify_cb (callback_cls, peer, ats, ats_count);
697 * Try to create a connection to the given target (eventually).
699 * @param target peer to try to connect to
702 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
704 struct NeighbourMapEntry *n;
706 GNUNET_assert (neighbours != NULL);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
710 GNUNET_i2s (target));
714 memcmp (target, &GST_my_identity,
715 sizeof (struct GNUNET_PeerIdentity)));
716 n = lookup_neighbour (target);
717 if ((NULL != n) && (GNUNET_YES == n->is_connected))
718 return; /* already connected */
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Unknown peer `%s', creating new neighbour\n",
724 GNUNET_i2s (target));
726 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
728 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
729 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
730 MAX_BANDWIDTH_CARRY_S);
732 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
733 &neighbour_timeout_task, n);
734 GNUNET_assert (GNUNET_OK ==
735 GNUNET_CONTAINER_multihashmap_put (neighbours,
736 &n->id.hashPubKey, n,
737 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Asking ATS for suggested address to connect to peer `%s'\n",
742 GNUNET_i2s (target));
744 GNUNET_ATS_suggest_address (GST_ats, target);
749 * Test if we're connected to the given peer.
751 * @param target peer to test
752 * @return GNUNET_YES if we are connected, GNUNET_NO if not
755 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
757 struct NeighbourMapEntry *n;
759 GNUNET_assert (neighbours != NULL);
761 n = lookup_neighbour (target);
762 if ((NULL == n) || (n->is_connected != GNUNET_YES))
763 return GNUNET_NO; /* not connected */
769 * A session was terminated. Take note.
771 * @param peer identity of the peer where the session died
772 * @param session session that is gone
775 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
776 struct Session *session)
778 struct NeighbourMapEntry *n;
780 GNUNET_assert (neighbours != NULL);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Session %X to peer `%s' ended \n",
785 session, GNUNET_i2s (peer));
788 n = lookup_neighbour (peer);
791 if (session != n->session)
792 return; /* doesn't affect us */
795 GNUNET_free (n->addr);
800 if (GNUNET_YES != n->is_connected)
801 return; /* not connected anymore anyway, shouldn't matter */
803 //n->is_connected = GNUNET_NO;
805 /* fast disconnect unless ATS suggests a new address */
806 GNUNET_SCHEDULER_cancel (n->timeout_task);
808 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
809 &neighbour_timeout_task, n);
810 /* try QUICKLY to re-establish a connection, reduce timeout! */
811 GNUNET_ATS_suggest_address (GST_ats, peer);
816 * Transmit a message to the given target using the active connection.
818 * @param target destination
819 * @param msg message to send
820 * @param msg_size number of bytes in msg
821 * @param timeout when to fail with timeout
822 * @param cont function to call when done
823 * @param cont_cls closure for 'cont'
826 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
827 size_t msg_size, struct GNUNET_TIME_Relative timeout,
828 GST_NeighbourSendContinuation cont, void *cont_cls)
830 struct NeighbourMapEntry *n;
831 struct MessageQueue *mq;
833 GNUNET_assert (neighbours != NULL);
835 n = lookup_neighbour (target);
836 if ((n == NULL) || (GNUNET_YES != n->is_connected))
838 GNUNET_STATISTICS_update (GST_stats,
840 ("# messages not sent (no such peer or not connected)"),
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Could not send message to peer `%s': unknown neighbor",
846 GNUNET_i2s (target));
847 else if (GNUNET_YES != n->is_connected)
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Could not send message to peer `%s': not connected\n",
850 GNUNET_i2s (target));
853 cont (cont_cls, GNUNET_SYSERR);
857 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
859 GNUNET_STATISTICS_update (GST_stats,
861 ("# messages not sent (no such peer or not connected)"),
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "Could not send message to peer `%s': no address available\n",
866 GNUNET_i2s (target));
870 cont (cont_cls, GNUNET_SYSERR);
875 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
876 GNUNET_STATISTICS_update (GST_stats,
878 ("# bytes in message queue for other peers"),
879 msg_size, GNUNET_NO);
880 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
882 mq->cont_cls = cont_cls;
883 /* FIXME: this memcpy can be up to 7% of our total runtime! */
884 memcpy (&mq[1], msg, msg_size);
885 mq->message_buf = (const char *) &mq[1];
886 mq->message_buf_size = msg_size;
887 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
888 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
889 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
890 (NULL == n->is_active))
891 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
896 * We have received a message from the given sender. How long should
897 * we delay before receiving more? (Also used to keep the peer marked
900 * @param sender sender of the message
901 * @param size size of the message
902 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
903 * GNUNET_NO if the neighbour is not connected or violates the quota
904 * @return how long to wait before reading more from this sender
906 struct GNUNET_TIME_Relative
907 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
908 *sender, ssize_t size, int *do_forward)
910 struct NeighbourMapEntry *n;
911 struct GNUNET_TIME_Relative ret;
913 GNUNET_assert (neighbours != NULL);
915 n = lookup_neighbour (sender);
918 *do_forward = GNUNET_NO;
919 return GNUNET_TIME_UNIT_ZERO;
921 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
923 n->quota_violation_count++;
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
927 n->in_tracker.available_bytes_per_s__,
928 n->quota_violation_count);
930 /* Discount 32k per violation */
931 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
935 if (n->quota_violation_count > 0)
937 /* try to add 32k back */
938 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
939 n->quota_violation_count--;
942 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
944 GNUNET_STATISTICS_update (GST_stats,
946 ("# bandwidth quota violations by other peers"),
948 *do_forward = GNUNET_NO;
949 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
951 *do_forward = GNUNET_YES;
952 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
953 if (ret.rel_value > 0)
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
958 (unsigned long long) n->in_tracker.
959 consumption_since_last_update__,
960 (unsigned int) n->in_tracker.available_bytes_per_s__,
961 (unsigned long long) ret.rel_value);
963 GNUNET_STATISTICS_update (GST_stats,
964 gettext_noop ("# ms throttling suggested"),
965 (int64_t) ret.rel_value, GNUNET_NO);
972 * Keep the connection to the given neighbour alive longer,
973 * we received a KEEPALIVE (or equivalent).
975 * @param neighbour neighbour to keep alive
978 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
980 struct NeighbourMapEntry *n;
982 GNUNET_assert (neighbours != NULL);
984 n = lookup_neighbour (neighbour);
987 GNUNET_STATISTICS_update (GST_stats,
989 ("# KEEPALIVE messages discarded (not connected)"),
993 GNUNET_SCHEDULER_cancel (n->timeout_task);
995 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
996 &neighbour_timeout_task, n);
1001 * Change the incoming quota for the given peer.
1003 * @param neighbour identity of peer to change qutoa for
1004 * @param quota new quota
1007 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1008 struct GNUNET_BANDWIDTH_Value32NBO quota)
1010 struct NeighbourMapEntry *n;
1012 GNUNET_assert (neighbours != NULL);
1014 n = lookup_neighbour (neighbour);
1017 GNUNET_STATISTICS_update (GST_stats,
1019 ("# SET QUOTA messages ignored (no such peer)"),
1023 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1024 if (0 != ntohl (quota.value__))
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1028 GNUNET_i2s (&n->id), "SET_QUOTA");
1030 if (GNUNET_YES == n->is_connected)
1031 GNUNET_STATISTICS_update (GST_stats,
1032 gettext_noop ("# disconnects due to quota of 0"), 1,
1034 disconnect_neighbour (n);
1039 * Closure for the neighbours_iterate function.
1041 struct IteratorContext
1044 * Function to call on each connected neighbour.
1046 GST_NeighbourIterator cb;
1056 * Call the callback from the closure for each connected neighbour.
1058 * @param cls the 'struct IteratorContext'
1059 * @param key the hash of the public key of the neighbour
1060 * @param value the 'struct NeighbourMapEntry'
1061 * @return GNUNET_OK (continue to iterate)
1064 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1066 struct IteratorContext *ic = cls;
1067 struct NeighbourMapEntry *n = value;
1069 if (GNUNET_YES != n->is_connected)
1072 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1078 * Iterate over all connected neighbours.
1080 * @param cb function to call
1081 * @param cb_cls closure for cb
1084 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1086 struct IteratorContext ic;
1088 GNUNET_assert (neighbours != NULL);
1092 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1097 * If we have an active connection to the given target, it must be shutdown.
1099 * @param target peer to disconnect from
1102 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1104 struct NeighbourMapEntry *n;
1105 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1106 struct SessionDisconnectMessage disconnect_msg;
1108 GNUNET_assert (neighbours != NULL);
1110 n = lookup_neighbour (target);
1112 return; /* not active */
1113 if (GNUNET_YES == n->is_connected)
1115 /* we're actually connected, send DISCONNECT message */
1116 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1117 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1118 disconnect_msg.reserved = htonl (0);
1119 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1120 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1121 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1122 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1123 GNUNET_assert (GNUNET_OK ==
1124 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1125 &disconnect_msg.purpose,
1126 &disconnect_msg.signature));
1127 papi = GST_plugins_find (n->plugin_name);
1129 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1130 sizeof (disconnect_msg),
1131 UINT32_MAX /* priority */ ,
1132 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1133 GNUNET_YES, NULL, NULL);
1134 GNUNET_STATISTICS_update (GST_stats,
1135 gettext_noop ("# peers disconnected due to external request"), 1,
1138 disconnect_neighbour (n);
1142 /* end of file gnunet-service-transport_neighbours.c */