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);
651 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
653 "Telling ATS to destroy session %p of peer %s\n",
656 GNUNET_ATS_address_destroyed (GST_ats,
658 plugin_name, address,
659 address_len, session);
662 was_connected = n->is_connected;
663 n->is_connected = GNUNET_YES;
664 if (GNUNET_YES != was_connected)
665 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
666 &neighbour_keepalive_task,
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
672 GNUNET_i2s (peer), plugin_name,
673 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
678 GNUNET_free_non_null (n->addr);
679 n->addr = GNUNET_malloc (address_len);
680 memcpy (n->addr, address, address_len);
681 n->addrlen = address_len;
682 n->session = session;
683 GNUNET_free_non_null (n->plugin_name);
684 n->plugin_name = GNUNET_strdup (plugin_name);
685 GNUNET_SCHEDULER_cancel (n->timeout_task);
687 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
688 &neighbour_timeout_task, n);
689 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
690 connect_msg.header.type =
691 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
692 connect_msg.reserved = htonl (0);
693 connect_msg.timestamp =
694 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
695 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
696 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
697 if (GNUNET_YES == was_connected)
699 /* First tell clients about connected neighbours...*/
700 neighbours_connected++;
701 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
703 connect_notify_cb (callback_cls, peer, ats, ats_count);
708 * Create an entry in the neighbour map for the given peer
710 * @param peer peer to create an entry for
711 * @return new neighbour map entry
713 static struct NeighbourMapEntry *
714 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
716 struct NeighbourMapEntry *n;
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Unknown peer `%s', creating new neighbour\n",
723 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
725 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
726 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
727 MAX_BANDWIDTH_CARRY_S);
729 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
730 &neighbour_timeout_task, n);
731 GNUNET_assert (GNUNET_OK ==
732 GNUNET_CONTAINER_multihashmap_put (neighbours,
733 &n->id.hashPubKey, n,
734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
740 * Try to create a connection to the given target (eventually).
742 * @param target peer to try to connect to
745 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
747 struct NeighbourMapEntry *n;
749 GNUNET_assert (neighbours != NULL);
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
752 GNUNET_i2s (target));
755 memcmp (target, &GST_my_identity,
756 sizeof (struct GNUNET_PeerIdentity)));
757 n = lookup_neighbour (target);
758 if ((NULL != n) && (GNUNET_YES == n->is_connected))
759 return; /* already connected */
761 n = setup_neighbour (target);
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "Asking ATS for suggested address to connect to peer `%s'\n",
765 GNUNET_i2s (&n->id));
767 GNUNET_ATS_suggest_address (GST_ats, &n->id);
772 * Test if we're connected to the given peer.
774 * @param target peer to test
775 * @return GNUNET_YES if we are connected, GNUNET_NO if not
778 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
780 struct NeighbourMapEntry *n;
782 GNUNET_assert (neighbours != NULL);
784 n = lookup_neighbour (target);
785 if ((NULL == n) || (n->is_connected != GNUNET_YES))
786 return GNUNET_NO; /* not connected */
792 * A session was terminated. Take note.
794 * @param peer identity of the peer where the session died
795 * @param session session that is gone
798 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
799 struct Session *session)
801 struct NeighbourMapEntry *n;
803 GNUNET_assert (neighbours != NULL);
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Session %X to peer `%s' ended \n",
808 session, GNUNET_i2s (peer));
811 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
813 "Telling ATS to destroy session %p of peer %s\n",
816 GNUNET_ATS_address_destroyed (GST_ats,
820 n = lookup_neighbour (peer);
823 if (session != n->session)
824 return; /* doesn't affect us */
827 GNUNET_free (n->addr);
832 if (GNUNET_YES != n->is_connected)
833 return; /* not connected anymore anyway, shouldn't matter */
834 /* fast disconnect unless ATS suggests a new address */
835 GNUNET_SCHEDULER_cancel (n->timeout_task);
837 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
838 &neighbour_timeout_task, n);
839 /* try QUICKLY to re-establish a connection, reduce timeout! */
840 GNUNET_ATS_suggest_address (GST_ats, peer);
845 * Transmit a message to the given target using the active connection.
847 * @param target destination
848 * @param msg message to send
849 * @param msg_size number of bytes in msg
850 * @param timeout when to fail with timeout
851 * @param cont function to call when done
852 * @param cont_cls closure for 'cont'
855 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
856 size_t msg_size, struct GNUNET_TIME_Relative timeout,
857 GST_NeighbourSendContinuation cont, void *cont_cls)
859 struct NeighbourMapEntry *n;
860 struct MessageQueue *mq;
862 GNUNET_assert (neighbours != NULL);
864 n = lookup_neighbour (target);
865 if ((n == NULL) || (GNUNET_YES != n->is_connected))
867 GNUNET_STATISTICS_update (GST_stats,
869 ("# messages not sent (no such peer or not connected)"),
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Could not send message to peer `%s': unknown neighbor",
875 GNUNET_i2s (target));
876 else if (GNUNET_YES != n->is_connected)
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Could not send message to peer `%s': not connected\n",
879 GNUNET_i2s (target));
882 cont (cont_cls, GNUNET_SYSERR);
886 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
888 GNUNET_STATISTICS_update (GST_stats,
890 ("# messages not sent (no such peer or not connected)"),
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Could not send message to peer `%s': no address available\n",
895 GNUNET_i2s (target));
899 cont (cont_cls, GNUNET_SYSERR);
902 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
903 GNUNET_STATISTICS_update (GST_stats,
905 ("# bytes in message queue for other peers"),
906 msg_size, GNUNET_NO);
907 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
909 mq->cont_cls = cont_cls;
910 /* FIXME: this memcpy can be up to 7% of our total runtime! */
911 memcpy (&mq[1], msg, msg_size);
912 mq->message_buf = (const char *) &mq[1];
913 mq->message_buf_size = msg_size;
914 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
915 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
916 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
917 (NULL == n->is_active))
918 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
923 * We have received a message from the given sender. How long should
924 * we delay before receiving more? (Also used to keep the peer marked
927 * @param sender sender of the message
928 * @param size size of the message
929 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
930 * GNUNET_NO if the neighbour is not connected or violates the quota,
931 * GNUNET_SYSERR if the connection is not fully up yet
932 * @return how long to wait before reading more from this sender
934 struct GNUNET_TIME_Relative
935 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
936 *sender, ssize_t size, int *do_forward)
938 struct NeighbourMapEntry *n;
939 struct GNUNET_TIME_Relative ret;
941 GNUNET_assert (neighbours != NULL);
943 n = lookup_neighbour (sender);
946 *do_forward = GNUNET_NO;
947 return GNUNET_TIME_UNIT_ZERO;
949 if (GNUNET_YES != n->is_connected)
951 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
952 _("Plugin gave us %d bytes of data but somehow the session is not marked as UP yet!\n"),
954 *do_forward = GNUNET_SYSERR;
955 return GNUNET_TIME_UNIT_ZERO;
957 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
959 n->quota_violation_count++;
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
963 n->in_tracker.available_bytes_per_s__,
964 n->quota_violation_count);
966 /* Discount 32k per violation */
967 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
971 if (n->quota_violation_count > 0)
973 /* try to add 32k back */
974 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
975 n->quota_violation_count--;
978 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
980 GNUNET_STATISTICS_update (GST_stats,
982 ("# bandwidth quota violations by other peers"),
984 *do_forward = GNUNET_NO;
985 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
987 *do_forward = GNUNET_YES;
988 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
989 if (ret.rel_value > 0)
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
994 (unsigned long long) n->in_tracker.
995 consumption_since_last_update__,
996 (unsigned int) n->in_tracker.available_bytes_per_s__,
997 (unsigned long long) ret.rel_value);
999 GNUNET_STATISTICS_update (GST_stats,
1000 gettext_noop ("# ms throttling suggested"),
1001 (int64_t) ret.rel_value, GNUNET_NO);
1008 * Keep the connection to the given neighbour alive longer,
1009 * we received a KEEPALIVE (or equivalent).
1011 * @param neighbour neighbour to keep alive
1014 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1016 struct NeighbourMapEntry *n;
1018 GNUNET_assert (neighbours != NULL);
1020 n = lookup_neighbour (neighbour);
1023 GNUNET_STATISTICS_update (GST_stats,
1025 ("# KEEPALIVE messages discarded (not connected)"),
1029 GNUNET_SCHEDULER_cancel (n->timeout_task);
1031 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1032 &neighbour_timeout_task, n);
1037 * Change the incoming quota for the given peer.
1039 * @param neighbour identity of peer to change qutoa for
1040 * @param quota new quota
1043 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1044 struct GNUNET_BANDWIDTH_Value32NBO quota)
1046 struct NeighbourMapEntry *n;
1048 GNUNET_assert (neighbours != NULL);
1050 n = lookup_neighbour (neighbour);
1053 GNUNET_STATISTICS_update (GST_stats,
1055 ("# SET QUOTA messages ignored (no such peer)"),
1059 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1060 if (0 != ntohl (quota.value__))
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1064 GNUNET_i2s (&n->id), "SET_QUOTA");
1066 if (GNUNET_YES == n->is_connected)
1067 GNUNET_STATISTICS_update (GST_stats,
1068 gettext_noop ("# disconnects due to quota of 0"), 1,
1070 disconnect_neighbour (n);
1075 * Closure for the neighbours_iterate function.
1077 struct IteratorContext
1080 * Function to call on each connected neighbour.
1082 GST_NeighbourIterator cb;
1092 * Call the callback from the closure for each connected neighbour.
1094 * @param cls the 'struct IteratorContext'
1095 * @param key the hash of the public key of the neighbour
1096 * @param value the 'struct NeighbourMapEntry'
1097 * @return GNUNET_OK (continue to iterate)
1100 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1102 struct IteratorContext *ic = cls;
1103 struct NeighbourMapEntry *n = value;
1105 if (GNUNET_YES != n->is_connected)
1108 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1114 * Iterate over all connected neighbours.
1116 * @param cb function to call
1117 * @param cb_cls closure for cb
1120 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1122 struct IteratorContext ic;
1124 GNUNET_assert (neighbours != NULL);
1128 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1133 * If we have an active connection to the given target, it must be shutdown.
1135 * @param target peer to disconnect from
1138 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1140 struct NeighbourMapEntry *n;
1141 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1142 struct SessionDisconnectMessage disconnect_msg;
1144 GNUNET_assert (neighbours != NULL);
1146 n = lookup_neighbour (target);
1148 return; /* not active */
1149 if (GNUNET_YES == n->is_connected)
1151 /* we're actually connected, send DISCONNECT message */
1152 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1153 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1154 disconnect_msg.reserved = htonl (0);
1155 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1156 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1157 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1158 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1159 GNUNET_assert (GNUNET_OK ==
1160 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1161 &disconnect_msg.purpose,
1162 &disconnect_msg.signature));
1163 papi = GST_plugins_find (n->plugin_name);
1165 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1166 sizeof (disconnect_msg),
1167 UINT32_MAX /* priority */ ,
1168 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1169 GNUNET_YES, NULL, NULL);
1170 GNUNET_STATISTICS_update (GST_stats,
1171 gettext_noop ("# peers disconnected due to external request"), 1,
1174 disconnect_neighbour (n);
1179 * We received a 'SESSION_CONNECT' message from the other peer.
1180 * Consider switching to it.
1182 * @param message possibly a 'struct SessionConnectMessage' (check format)
1183 * @param peer identity of the peer to switch the address for
1184 * @param plugin_name name of transport that delivered the PONG
1185 * @param address address of the other peer, NULL if other peer
1187 * @param address_len number of bytes in address
1188 * @param session session to use (or NULL)
1189 * @param ats performance data
1190 * @param ats_count number of entries in ats (excluding 0-termination)
1193 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
1194 const struct GNUNET_PeerIdentity *peer,
1195 const char *plugin_name,
1196 const char *sender_address, uint16_t sender_address_len,
1197 struct Session *session,
1198 const struct GNUNET_ATS_Information *ats,
1201 const struct SessionConnectMessage *scm;
1202 struct GNUNET_TIME_Absolute ts;
1203 struct NeighbourMapEntry *n;
1205 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1207 GNUNET_break_op (0);
1210 scm = (const struct SessionConnectMessage *) message;
1211 GNUNET_break_op (ntohl (scm->reserved) == 0);
1212 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
1213 n = lookup_neighbour (peer);
1215 n = setup_neighbour (peer);
1216 if (ts.abs_value > n->connect_ts.abs_value)
1218 if (NULL != session)
1219 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1221 "Giving ATS session %p of plugin %s for peer %s\n",
1225 GNUNET_ATS_address_update (GST_ats,
1227 plugin_name, sender_address, sender_address_len,
1228 session, ats, ats_count);
1234 /* end of file gnunet-service-transport_neighbours.c */