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_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 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
253 struct GNUNET_TIME_Absolute connect_ts;
256 * How often has the other peer (recently) violated the inbound
257 * traffic limit? Incremented by 10 per violation, decremented by 1
258 * per non-violation (for each time interval).
260 unsigned int quota_violation_count;
263 * Number of values in 'ats' array.
265 //unsigned int ats_count;
268 * Are we already in the process of disconnecting this neighbour?
273 * Do we currently consider this neighbour connected? (as far as
274 * the connect/disconnect callbacks are concerned)?
282 * All known neighbours and their HELLOs.
284 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
287 * Closure for connect_notify_cb and disconnect_notify_cb
289 static void *callback_cls;
292 * Function to call when we connected to a neighbour.
294 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
297 * Function to call when we disconnected from a neighbour.
299 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
302 * counter for connected neighbours
304 static int neighbours_connected;
307 * Lookup a neighbour entry in the neighbours hash map.
309 * @param pid identity of the peer to look up
310 * @return the entry, NULL if there is no existing record
312 static struct NeighbourMapEntry *
313 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
315 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
320 * Task invoked to start a transmission to another peer.
322 * @param cls the 'struct NeighbourMapEntry'
323 * @param tc scheduler context
326 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
330 * We're done with our transmission attempt, continue processing.
332 * @param cls the 'struct MessageQueue' of the message
333 * @param receiver intended receiver
334 * @param success whether it worked or not
337 transmit_send_continuation (void *cls,
338 const struct GNUNET_PeerIdentity *receiver,
341 struct MessageQueue *mq;
342 struct NeighbourMapEntry *n;
348 GNUNET_assert (n->is_active == mq);
350 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
351 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
353 if (NULL != mq->cont)
354 mq->cont (mq->cont_cls, success);
360 * Check the ready list for the given neighbour and if a plugin is
361 * ready for transmission (and if we have a message), do so!
363 * @param n target peer for which to transmit
366 try_transmission_to_peer (struct NeighbourMapEntry *n)
368 struct MessageQueue *mq;
369 struct GNUNET_TIME_Relative timeout;
371 struct GNUNET_TRANSPORT_PluginFunctions *papi;
373 if (n->is_active != NULL)
374 return; /* transmission already pending */
375 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
376 return; /* currently waiting for bandwidth */
377 while (NULL != (mq = n->messages_head))
379 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
380 if (timeout.rel_value > 0)
382 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
384 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
387 return; /* no more messages */
389 papi = GST_plugins_find (n->plugin_name);
395 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
399 if (((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0)))
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No address peer for peer `%s'\n",
402 GNUNET_i2s (&n->id));
403 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
404 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
409 papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
410 0 /* priority -- remove from plugin API? */ ,
411 timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
412 &transmit_send_continuation, mq);
415 /* failure, but 'send' would not call continuation in this case,
416 * so we need to do it here! */
417 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
418 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
424 * Task invoked to start a transmission to another peer.
426 * @param cls the 'struct NeighbourMapEntry'
427 * @param tc scheduler context
430 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
432 struct NeighbourMapEntry *n = cls;
434 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
435 try_transmission_to_peer (n);
440 * Initialize the neighbours subsystem.
442 * @param cls closure for callbacks
443 * @param connect_cb function to call if we connect to a peer
444 * @param disconnect_cb function to call if we disconnect from a peer
447 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
448 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
451 connect_notify_cb = connect_cb;
452 disconnect_notify_cb = disconnect_cb;
453 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
458 * Disconnect from the given neighbour, clean up the record.
460 * @param n neighbour to disconnect from
463 disconnect_neighbour (struct NeighbourMapEntry *n)
465 struct MessageQueue *mq;
467 if (GNUNET_YES == n->in_disconnect)
469 n->in_disconnect = GNUNET_YES;
470 while (NULL != (mq = n->messages_head))
472 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
473 if (NULL != mq->cont)
474 mq->cont (mq->cont_cls, GNUNET_SYSERR);
477 if (NULL != n->is_active)
479 n->is_active->n = NULL;
482 if (GNUNET_YES == n->is_connected)
484 n->is_connected = GNUNET_NO;
485 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
486 GNUNET_SCHEDULER_cancel (n->keepalive_task);
487 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
488 GNUNET_assert (neighbours_connected > 0);
489 neighbours_connected--;
490 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
492 disconnect_notify_cb (callback_cls, &n->id);
494 GNUNET_assert (GNUNET_YES ==
495 GNUNET_CONTAINER_multihashmap_remove (neighbours,
496 &n->id.hashPubKey, n));
497 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
499 GNUNET_SCHEDULER_cancel (n->timeout_task);
500 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
502 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
504 GNUNET_SCHEDULER_cancel (n->transmission_task);
505 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
507 if (NULL != n->plugin_name)
509 GNUNET_free (n->plugin_name);
510 n->plugin_name = NULL;
514 GNUNET_free (n->addr);
524 * Peer has been idle for too long. Disconnect.
526 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
527 * @param tc scheduler context
530 neighbour_timeout_task (void *cls,
531 const struct GNUNET_SCHEDULER_TaskContext *tc)
533 struct NeighbourMapEntry *n = cls;
535 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
536 if (GNUNET_YES == n->is_connected)
537 GNUNET_STATISTICS_update (GST_stats,
538 gettext_noop ("# peers disconnected due to timeout"), 1,
540 disconnect_neighbour (n);
545 * Send another keepalive message.
547 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
548 * @param tc scheduler context
551 neighbour_keepalive_task (void *cls,
552 const struct GNUNET_SCHEDULER_TaskContext *tc)
554 struct NeighbourMapEntry *n = cls;
555 struct GNUNET_MessageHeader m;
556 struct GNUNET_TRANSPORT_PluginFunctions *papi;
558 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
559 &neighbour_keepalive_task,
561 GNUNET_assert (GNUNET_YES == n->is_connected);
562 GNUNET_STATISTICS_update (GST_stats,
563 gettext_noop ("# keepalives sent"), 1,
565 m.size = htons (sizeof (struct GNUNET_MessageHeader));
566 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
567 papi = GST_plugins_find (n->plugin_name);
569 papi->send (papi->cls,
570 &n->id, (const void *) &m,
572 UINT32_MAX /* priority */ ,
573 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
574 GNUNET_YES, NULL, NULL);
579 * Disconnect from the given neighbour.
582 * @param key hash of neighbour's public key (not used)
583 * @param value the 'struct NeighbourMapEntry' of the neighbour
586 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
588 struct NeighbourMapEntry *n = value;
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
592 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
594 if (GNUNET_YES == n->is_connected)
595 GNUNET_STATISTICS_update (GST_stats,
596 gettext_noop ("# peers disconnected due to global disconnect"), 1,
598 disconnect_neighbour (n);
604 * Cleanup the neighbours subsystem.
607 GST_neighbours_stop ()
609 GNUNET_assert (neighbours != NULL);
611 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
613 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
614 GNUNET_assert (neighbours_connected == 0);
617 connect_notify_cb = NULL;
618 disconnect_notify_cb = NULL;
623 * For an existing neighbour record, set the active connection to
626 * @param peer identity of the peer to switch the address for
627 * @param plugin_name name of transport that delivered the PONG
628 * @param address address of the other peer, NULL if other peer
630 * @param address_len number of bytes in address
631 * @param session session to use (or NULL)
632 * @param ats performance data
633 * @param ats_count number of entries in ats (excluding 0-termination)
636 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
637 const char *plugin_name, const void *address,
638 size_t address_len, struct Session *session,
639 const struct GNUNET_ATS_Information
640 *ats, uint32_t ats_count)
642 struct NeighbourMapEntry *n;
643 struct SessionConnectMessage connect_msg;
646 GNUNET_assert (neighbours != NULL);
647 n = lookup_neighbour (peer);
650 GNUNET_ATS_address_destroyed (GST_ats,
652 plugin_name, address,
653 address_len, session);
656 was_connected = n->is_connected;
657 n->is_connected = GNUNET_YES;
658 if (GNUNET_YES != was_connected)
659 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
660 &neighbour_keepalive_task,
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
666 GNUNET_i2s (peer), plugin_name,
667 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
672 GNUNET_free_non_null (n->addr);
673 n->addr = GNUNET_malloc (address_len);
674 memcpy (n->addr, address, address_len);
675 n->addrlen = address_len;
676 n->session = session;
677 GNUNET_free_non_null (n->plugin_name);
678 n->plugin_name = GNUNET_strdup (plugin_name);
679 GNUNET_SCHEDULER_cancel (n->timeout_task);
681 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
682 &neighbour_timeout_task, n);
683 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
684 connect_msg.header.type =
685 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
686 connect_msg.reserved = htonl (0);
687 connect_msg.timestamp =
688 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
689 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
690 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
691 if (GNUNET_YES == was_connected)
693 /* First tell clients about connected neighbours...*/
694 neighbours_connected++;
695 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
697 connect_notify_cb (callback_cls, peer, ats, ats_count);
702 * Create an entry in the neighbour map for the given peer
704 * @param peer peer to create an entry for
705 * @return new neighbour map entry
707 static struct NeighbourMapEntry *
708 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
710 struct NeighbourMapEntry *n;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Unknown peer `%s', creating new neighbour\n",
717 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
719 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
720 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
721 MAX_BANDWIDTH_CARRY_S);
723 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
724 &neighbour_timeout_task, n);
725 GNUNET_assert (GNUNET_OK ==
726 GNUNET_CONTAINER_multihashmap_put (neighbours,
727 &n->id.hashPubKey, n,
728 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
734 * Try to create a connection to the given target (eventually).
736 * @param target peer to try to connect to
739 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
741 struct NeighbourMapEntry *n;
743 GNUNET_assert (neighbours != NULL);
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
746 GNUNET_i2s (target));
749 memcmp (target, &GST_my_identity,
750 sizeof (struct GNUNET_PeerIdentity)));
751 n = lookup_neighbour (target);
752 if ((NULL != n) && (GNUNET_YES == n->is_connected))
753 return; /* already connected */
755 n = setup_neighbour (target);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Asking ATS for suggested address to connect to peer `%s'\n",
759 GNUNET_i2s (&n->id));
761 GNUNET_ATS_suggest_address (GST_ats, &n->id);
766 * Test if we're connected to the given peer.
768 * @param target peer to test
769 * @return GNUNET_YES if we are connected, GNUNET_NO if not
772 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
774 struct NeighbourMapEntry *n;
776 GNUNET_assert (neighbours != NULL);
778 n = lookup_neighbour (target);
779 if ((NULL == n) || (n->is_connected != GNUNET_YES))
780 return GNUNET_NO; /* not connected */
786 * A session was terminated. Take note.
788 * @param peer identity of the peer where the session died
789 * @param session session that is gone
792 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
793 struct Session *session)
795 struct NeighbourMapEntry *n;
797 GNUNET_assert (neighbours != NULL);
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Session %X to peer `%s' ended \n",
802 session, GNUNET_i2s (peer));
804 GNUNET_ATS_address_destroyed (GST_ats,
808 n = lookup_neighbour (peer);
811 if (session != n->session)
812 return; /* doesn't affect us */
815 GNUNET_free (n->addr);
820 if (GNUNET_YES != n->is_connected)
821 return; /* not connected anymore anyway, shouldn't matter */
822 /* fast disconnect unless ATS suggests a new address */
823 GNUNET_SCHEDULER_cancel (n->timeout_task);
825 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
826 &neighbour_timeout_task, n);
827 /* try QUICKLY to re-establish a connection, reduce timeout! */
828 GNUNET_ATS_suggest_address (GST_ats, peer);
833 * Transmit a message to the given target using the active connection.
835 * @param target destination
836 * @param msg message to send
837 * @param msg_size number of bytes in msg
838 * @param timeout when to fail with timeout
839 * @param cont function to call when done
840 * @param cont_cls closure for 'cont'
843 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
844 size_t msg_size, struct GNUNET_TIME_Relative timeout,
845 GST_NeighbourSendContinuation cont, void *cont_cls)
847 struct NeighbourMapEntry *n;
848 struct MessageQueue *mq;
850 GNUNET_assert (neighbours != NULL);
852 n = lookup_neighbour (target);
853 if ((n == NULL) || (GNUNET_YES != n->is_connected))
855 GNUNET_STATISTICS_update (GST_stats,
857 ("# messages not sent (no such peer or not connected)"),
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Could not send message to peer `%s': unknown neighbor",
863 GNUNET_i2s (target));
864 else if (GNUNET_YES != n->is_connected)
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Could not send message to peer `%s': not connected\n",
867 GNUNET_i2s (target));
870 cont (cont_cls, GNUNET_SYSERR);
874 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
876 GNUNET_STATISTICS_update (GST_stats,
878 ("# messages not sent (no such peer or not connected)"),
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Could not send message to peer `%s': no address available\n",
883 GNUNET_i2s (target));
887 cont (cont_cls, GNUNET_SYSERR);
890 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
891 GNUNET_STATISTICS_update (GST_stats,
893 ("# bytes in message queue for other peers"),
894 msg_size, GNUNET_NO);
895 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
897 mq->cont_cls = cont_cls;
898 /* FIXME: this memcpy can be up to 7% of our total runtime! */
899 memcpy (&mq[1], msg, msg_size);
900 mq->message_buf = (const char *) &mq[1];
901 mq->message_buf_size = msg_size;
902 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
903 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
904 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
905 (NULL == n->is_active))
906 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
911 * We have received a message from the given sender. How long should
912 * we delay before receiving more? (Also used to keep the peer marked
915 * @param sender sender of the message
916 * @param size size of the message
917 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
918 * GNUNET_NO if the neighbour is not connected or violates the quota,
919 * GNUNET_SYSERR if the connection is not fully up yet
920 * @return how long to wait before reading more from this sender
922 struct GNUNET_TIME_Relative
923 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
924 *sender, ssize_t size, int *do_forward)
926 struct NeighbourMapEntry *n;
927 struct GNUNET_TIME_Relative ret;
929 GNUNET_assert (neighbours != NULL);
931 n = lookup_neighbour (sender);
934 *do_forward = GNUNET_NO;
935 return GNUNET_TIME_UNIT_ZERO;
937 if (GNUNET_YES != n->is_connected)
939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
940 _("Plugin gave us %d bytes of data but somehow the session is not marked as UP yet!\n"),
942 *do_forward = GNUNET_SYSERR;
943 return GNUNET_TIME_UNIT_ZERO;
945 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
947 n->quota_violation_count++;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
951 n->in_tracker.available_bytes_per_s__,
952 n->quota_violation_count);
954 /* Discount 32k per violation */
955 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
959 if (n->quota_violation_count > 0)
961 /* try to add 32k back */
962 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
963 n->quota_violation_count--;
966 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
968 GNUNET_STATISTICS_update (GST_stats,
970 ("# bandwidth quota violations by other peers"),
972 *do_forward = GNUNET_NO;
973 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
975 *do_forward = GNUNET_YES;
976 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
977 if (ret.rel_value > 0)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
982 (unsigned long long) n->in_tracker.
983 consumption_since_last_update__,
984 (unsigned int) n->in_tracker.available_bytes_per_s__,
985 (unsigned long long) ret.rel_value);
987 GNUNET_STATISTICS_update (GST_stats,
988 gettext_noop ("# ms throttling suggested"),
989 (int64_t) ret.rel_value, GNUNET_NO);
996 * Keep the connection to the given neighbour alive longer,
997 * we received a KEEPALIVE (or equivalent).
999 * @param neighbour neighbour to keep alive
1002 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1004 struct NeighbourMapEntry *n;
1006 GNUNET_assert (neighbours != NULL);
1008 n = lookup_neighbour (neighbour);
1011 GNUNET_STATISTICS_update (GST_stats,
1013 ("# KEEPALIVE messages discarded (not connected)"),
1017 GNUNET_SCHEDULER_cancel (n->timeout_task);
1019 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1020 &neighbour_timeout_task, n);
1025 * Change the incoming quota for the given peer.
1027 * @param neighbour identity of peer to change qutoa for
1028 * @param quota new quota
1031 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1032 struct GNUNET_BANDWIDTH_Value32NBO quota)
1034 struct NeighbourMapEntry *n;
1036 GNUNET_assert (neighbours != NULL);
1038 n = lookup_neighbour (neighbour);
1041 GNUNET_STATISTICS_update (GST_stats,
1043 ("# SET QUOTA messages ignored (no such peer)"),
1047 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1048 if (0 != ntohl (quota.value__))
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1052 GNUNET_i2s (&n->id), "SET_QUOTA");
1054 if (GNUNET_YES == n->is_connected)
1055 GNUNET_STATISTICS_update (GST_stats,
1056 gettext_noop ("# disconnects due to quota of 0"), 1,
1058 disconnect_neighbour (n);
1063 * Closure for the neighbours_iterate function.
1065 struct IteratorContext
1068 * Function to call on each connected neighbour.
1070 GST_NeighbourIterator cb;
1080 * Call the callback from the closure for each connected neighbour.
1082 * @param cls the 'struct IteratorContext'
1083 * @param key the hash of the public key of the neighbour
1084 * @param value the 'struct NeighbourMapEntry'
1085 * @return GNUNET_OK (continue to iterate)
1088 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1090 struct IteratorContext *ic = cls;
1091 struct NeighbourMapEntry *n = value;
1093 if (GNUNET_YES != n->is_connected)
1096 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1102 * Iterate over all connected neighbours.
1104 * @param cb function to call
1105 * @param cb_cls closure for cb
1108 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1110 struct IteratorContext ic;
1112 GNUNET_assert (neighbours != NULL);
1116 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1121 * If we have an active connection to the given target, it must be shutdown.
1123 * @param target peer to disconnect from
1126 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1128 struct NeighbourMapEntry *n;
1129 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1130 struct SessionDisconnectMessage disconnect_msg;
1132 GNUNET_assert (neighbours != NULL);
1134 n = lookup_neighbour (target);
1136 return; /* not active */
1137 if (GNUNET_YES == n->is_connected)
1139 /* we're actually connected, send DISCONNECT message */
1140 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1141 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1142 disconnect_msg.reserved = htonl (0);
1143 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1144 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1145 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1146 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1147 GNUNET_assert (GNUNET_OK ==
1148 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1149 &disconnect_msg.purpose,
1150 &disconnect_msg.signature));
1151 papi = GST_plugins_find (n->plugin_name);
1153 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1154 sizeof (disconnect_msg),
1155 UINT32_MAX /* priority */ ,
1156 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1157 GNUNET_YES, NULL, NULL);
1158 GNUNET_STATISTICS_update (GST_stats,
1159 gettext_noop ("# peers disconnected due to external request"), 1,
1162 disconnect_neighbour (n);
1167 * We received a 'SESSION_CONNECT' message from the other peer.
1168 * Consider switching to it.
1170 * @param message possibly a 'struct SessionConnectMessage' (check format)
1171 * @param peer identity of the peer to switch the address for
1172 * @param plugin_name name of transport that delivered the PONG
1173 * @param address address of the other peer, NULL if other peer
1175 * @param address_len number of bytes in address
1176 * @param session session to use (or NULL)
1177 * @param ats performance data
1178 * @param ats_count number of entries in ats (excluding 0-termination)
1181 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
1182 const struct GNUNET_PeerIdentity *peer,
1183 const char *plugin_name,
1184 const char *sender_address, uint16_t sender_address_len,
1185 struct Session *session,
1186 const struct GNUNET_ATS_Information *ats,
1189 const struct SessionConnectMessage *scm;
1190 struct GNUNET_TIME_Absolute ts;
1191 struct NeighbourMapEntry *n;
1193 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1195 GNUNET_break_op (0);
1198 scm = (const struct SessionConnectMessage *) message;
1199 GNUNET_break_op (ntohl (scm->reserved) == 0);
1200 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
1201 n = lookup_neighbour (peer);
1203 n = setup_neighbour (peer);
1204 if (ts.abs_value > n->connect_ts.abs_value)
1206 GNUNET_ATS_address_update (GST_ats,
1208 plugin_name, sender_address, sender_address_len,
1209 session, ats, ats_count);
1215 /* end of file gnunet-service-transport_neighbours.c */