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 * Public key of the sender.
117 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
120 * Signature of the peer that sends us the disconnect. Only
121 * valid if the timestamp is AFTER the timestamp from the
122 * corresponding 'CONNECT' message.
124 struct GNUNET_CRYPTO_RsaSignature signature;
130 * For each neighbour we keep a list of messages
131 * that we still want to transmit to the neighbour.
137 * This is a doubly linked list.
139 struct MessageQueue *next;
142 * This is a doubly linked list.
144 struct MessageQueue *prev;
147 * Once this message is actively being transmitted, which
148 * neighbour is it associated with?
150 struct NeighbourMapEntry *n;
153 * Function to call once we're done.
155 GST_NeighbourSendContinuation cont;
163 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
164 * stuck together in memory. Allocated at the end of this struct.
166 const char *message_buf;
169 * Size of the message buf
171 size_t message_buf_size;
174 * At what time should we fail?
176 struct GNUNET_TIME_Absolute timeout;
182 * Entry in neighbours.
184 struct NeighbourMapEntry
188 * Head of list of messages we would like to send to this peer;
189 * must contain at most one message per client.
191 struct MessageQueue *messages_head;
194 * Tail of list of messages we would like to send to this peer; must
195 * contain at most one message per client.
197 struct MessageQueue *messages_tail;
200 * Performance data for the peer.
202 //struct GNUNET_ATS_Information *ats;
205 * Are we currently trying to send a message? If so, which one?
207 struct MessageQueue *is_active;
210 * Active session for communicating with the peer.
212 struct Session *session;
215 * Name of the plugin we currently use.
220 * Address used for communicating with the peer, NULL for inbound connections.
225 * Number of bytes in 'addr'.
230 * Identity of this neighbour.
232 struct GNUNET_PeerIdentity id;
235 * ID of task scheduled to run when this peer is about to
236 * time out (will free resources associated with the peer).
238 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
241 * ID of task scheduled to send keepalives.
243 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
246 * ID of task scheduled to run when we should try transmitting
247 * the head of the message queue.
249 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
252 * Tracker for inbound bandwidth.
254 struct GNUNET_BANDWIDTH_Tracker in_tracker;
257 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
259 struct GNUNET_TIME_Absolute connect_ts;
262 * How often has the other peer (recently) violated the inbound
263 * traffic limit? Incremented by 10 per violation, decremented by 1
264 * per non-violation (for each time interval).
266 unsigned int quota_violation_count;
269 * Number of values in 'ats' array.
271 //unsigned int ats_count;
274 * Are we already in the process of disconnecting this neighbour?
279 * Do we currently consider this neighbour connected? (as far as
280 * the connect/disconnect callbacks are concerned)?
288 * All known neighbours and their HELLOs.
290 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
293 * Closure for connect_notify_cb and disconnect_notify_cb
295 static void *callback_cls;
298 * Function to call when we connected to a neighbour.
300 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
303 * Function to call when we disconnected from a neighbour.
305 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
308 * counter for connected neighbours
310 static int neighbours_connected;
313 * Lookup a neighbour entry in the neighbours hash map.
315 * @param pid identity of the peer to look up
316 * @return the entry, NULL if there is no existing record
318 static struct NeighbourMapEntry *
319 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
321 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
326 * Task invoked to start a transmission to another peer.
328 * @param cls the 'struct NeighbourMapEntry'
329 * @param tc scheduler context
332 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
336 * We're done with our transmission attempt, continue processing.
338 * @param cls the 'struct MessageQueue' of the message
339 * @param receiver intended receiver
340 * @param success whether it worked or not
343 transmit_send_continuation (void *cls,
344 const struct GNUNET_PeerIdentity *receiver,
347 struct MessageQueue *mq;
348 struct NeighbourMapEntry *n;
354 GNUNET_assert (n->is_active == mq);
356 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
357 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
359 if (NULL != mq->cont)
360 mq->cont (mq->cont_cls, success);
366 * Check the ready list for the given neighbour and if a plugin is
367 * ready for transmission (and if we have a message), do so!
369 * @param n target peer for which to transmit
372 try_transmission_to_peer (struct NeighbourMapEntry *n)
374 struct MessageQueue *mq;
375 struct GNUNET_TIME_Relative timeout;
377 struct GNUNET_TRANSPORT_PluginFunctions *papi;
379 if (n->is_active != NULL)
380 return; /* transmission already pending */
381 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
382 return; /* currently waiting for bandwidth */
383 while (NULL != (mq = n->messages_head))
385 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
386 if (timeout.rel_value > 0)
388 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
390 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
393 return; /* no more messages */
395 papi = GST_plugins_find (n->plugin_name);
401 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
405 if (((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0)))
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
408 GNUNET_i2s (&n->id));
409 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
410 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
415 papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
416 0 /* priority -- remove from plugin API? */ ,
417 timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
418 &transmit_send_continuation, mq);
421 /* failure, but 'send' would not call continuation in this case,
422 * so we need to do it here! */
423 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
424 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
430 * Task invoked to start a transmission to another peer.
432 * @param cls the 'struct NeighbourMapEntry'
433 * @param tc scheduler context
436 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
438 struct NeighbourMapEntry *n = cls;
440 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
441 try_transmission_to_peer (n);
446 * Initialize the neighbours subsystem.
448 * @param cls closure for callbacks
449 * @param connect_cb function to call if we connect to a peer
450 * @param disconnect_cb function to call if we disconnect from a peer
453 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
454 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
457 connect_notify_cb = connect_cb;
458 disconnect_notify_cb = disconnect_cb;
459 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
464 * Disconnect from the given neighbour, clean up the record.
466 * @param n neighbour to disconnect from
469 disconnect_neighbour (struct NeighbourMapEntry *n)
471 struct MessageQueue *mq;
473 if (GNUNET_YES == n->in_disconnect)
475 n->in_disconnect = GNUNET_YES;
476 while (NULL != (mq = n->messages_head))
478 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
479 if (NULL != mq->cont)
480 mq->cont (mq->cont_cls, GNUNET_SYSERR);
483 if (NULL != n->is_active)
485 n->is_active->n = NULL;
488 if (GNUNET_YES == n->is_connected)
490 n->is_connected = GNUNET_NO;
491 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
492 GNUNET_SCHEDULER_cancel (n->keepalive_task);
493 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
494 GNUNET_assert (neighbours_connected > 0);
495 neighbours_connected--;
496 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
498 disconnect_notify_cb (callback_cls, &n->id);
500 GNUNET_assert (GNUNET_YES ==
501 GNUNET_CONTAINER_multihashmap_remove (neighbours,
502 &n->id.hashPubKey, n));
503 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
505 GNUNET_SCHEDULER_cancel (n->timeout_task);
506 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
508 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
510 GNUNET_SCHEDULER_cancel (n->transmission_task);
511 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
513 if (NULL != n->plugin_name)
515 GNUNET_free (n->plugin_name);
516 n->plugin_name = NULL;
520 GNUNET_free (n->addr);
530 * Peer has been idle for too long. Disconnect.
532 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
533 * @param tc scheduler context
536 neighbour_timeout_task (void *cls,
537 const struct GNUNET_SCHEDULER_TaskContext *tc)
539 struct NeighbourMapEntry *n = cls;
541 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
542 if (GNUNET_YES == n->is_connected)
543 GNUNET_STATISTICS_update (GST_stats,
544 gettext_noop ("# peers disconnected due to timeout"), 1,
546 disconnect_neighbour (n);
551 * Send another keepalive message.
553 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
554 * @param tc scheduler context
557 neighbour_keepalive_task (void *cls,
558 const struct GNUNET_SCHEDULER_TaskContext *tc)
560 struct NeighbourMapEntry *n = cls;
561 struct GNUNET_MessageHeader m;
562 struct GNUNET_TRANSPORT_PluginFunctions *papi;
564 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
565 &neighbour_keepalive_task,
567 GNUNET_assert (GNUNET_YES == n->is_connected);
568 GNUNET_STATISTICS_update (GST_stats,
569 gettext_noop ("# keepalives sent"), 1,
571 m.size = htons (sizeof (struct GNUNET_MessageHeader));
572 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
573 papi = GST_plugins_find (n->plugin_name);
575 papi->send (papi->cls,
576 &n->id, (const void *) &m,
578 UINT32_MAX /* priority */ ,
579 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
580 GNUNET_YES, NULL, NULL);
585 * Disconnect from the given neighbour.
588 * @param key hash of neighbour's public key (not used)
589 * @param value the 'struct NeighbourMapEntry' of the neighbour
592 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
594 struct NeighbourMapEntry *n = value;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
598 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
600 if (GNUNET_YES == n->is_connected)
601 GNUNET_STATISTICS_update (GST_stats,
602 gettext_noop ("# peers disconnected due to global disconnect"), 1,
604 disconnect_neighbour (n);
610 * Cleanup the neighbours subsystem.
613 GST_neighbours_stop ()
615 GNUNET_assert (neighbours != NULL);
617 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
619 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
620 GNUNET_assert (neighbours_connected == 0);
623 connect_notify_cb = NULL;
624 disconnect_notify_cb = NULL;
629 * We tried to send a SESSION_CONNECT message to another peer. If this
630 * succeeded, we should mark the peer up. If it failed, we should tell
631 * ATS to not use this address anymore (until it is re-validated).
633 * @param cls the 'struct NeighbourMapEntry'
634 * @param success GNUNET_OK on success
637 send_connect_continuation (void *cls,
640 struct NeighbourMapEntry *n = cls;
642 if (GNUNET_YES == n->in_disconnect)
643 return; /* neighbour is going away */
644 if (GNUNET_YES != success)
646 GNUNET_ATS_address_destroyed (GST_ats,
658 * For an existing neighbour record, set the active connection to
661 * @param peer identity of the peer to switch the address for
662 * @param plugin_name name of transport that delivered the PONG
663 * @param address address of the other peer, NULL if other peer
665 * @param address_len number of bytes in address
666 * @param session session to use (or NULL)
667 * @param ats performance data
668 * @param ats_count number of entries in ats (excluding 0-termination)
671 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
672 const char *plugin_name, const void *address,
673 size_t address_len, struct Session *session,
674 const struct GNUNET_ATS_Information
675 *ats, uint32_t ats_count)
677 struct NeighbourMapEntry *n;
678 struct SessionConnectMessage connect_msg;
681 GNUNET_assert (neighbours != NULL);
682 n = lookup_neighbour (peer);
686 GNUNET_ATS_address_destroyed (GST_ats,
688 plugin_name, address,
692 was_connected = n->is_connected;
693 n->is_connected = GNUNET_YES;
694 if (GNUNET_YES != was_connected)
695 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
696 &neighbour_keepalive_task,
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
702 GNUNET_i2s (peer), plugin_name,
703 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
708 GNUNET_free_non_null (n->addr);
709 n->addr = GNUNET_malloc (address_len);
710 memcpy (n->addr, address, address_len);
711 n->addrlen = address_len;
712 n->session = session;
713 GNUNET_free_non_null (n->plugin_name);
714 n->plugin_name = GNUNET_strdup (plugin_name);
715 GNUNET_SCHEDULER_cancel (n->timeout_task);
717 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
718 &neighbour_timeout_task, n);
719 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
720 connect_msg.header.type =
721 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
722 connect_msg.reserved = htonl (0);
723 connect_msg.timestamp =
724 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
725 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
726 GNUNET_TIME_UNIT_FOREVER_REL,
727 &send_connect_continuation,
729 if (GNUNET_YES == was_connected)
731 /* First tell clients about connected neighbours...*/
732 neighbours_connected++;
733 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
735 connect_notify_cb (callback_cls, peer, ats, ats_count);
740 * Create an entry in the neighbour map for the given peer
742 * @param peer peer to create an entry for
743 * @return new neighbour map entry
745 static struct NeighbourMapEntry *
746 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
748 struct NeighbourMapEntry *n;
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Unknown peer `%s', creating new neighbour\n",
755 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
757 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
758 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
759 MAX_BANDWIDTH_CARRY_S);
761 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
762 &neighbour_timeout_task, n);
763 GNUNET_assert (GNUNET_OK ==
764 GNUNET_CONTAINER_multihashmap_put (neighbours,
765 &n->id.hashPubKey, n,
766 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
772 * Try to create a connection to the given target (eventually).
774 * @param target peer to try to connect to
777 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
779 struct NeighbourMapEntry *n;
781 GNUNET_assert (neighbours != NULL);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
784 GNUNET_i2s (target));
787 memcmp (target, &GST_my_identity,
788 sizeof (struct GNUNET_PeerIdentity)));
789 n = lookup_neighbour (target);
790 if ((NULL != n) && (GNUNET_YES == n->is_connected))
791 return; /* already connected */
793 n = setup_neighbour (target);
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Asking ATS for suggested address to connect to peer `%s'\n",
797 GNUNET_i2s (&n->id));
799 GNUNET_ATS_suggest_address (GST_ats, &n->id);
804 * Test if we're connected to the given peer.
806 * @param target peer to test
807 * @return GNUNET_YES if we are connected, GNUNET_NO if not
810 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
812 struct NeighbourMapEntry *n;
814 GNUNET_assert (neighbours != NULL);
816 n = lookup_neighbour (target);
817 if ((NULL == n) || (n->is_connected != GNUNET_YES))
818 return GNUNET_NO; /* not connected */
824 * A session was terminated. Take note.
826 * @param peer identity of the peer where the session died
827 * @param session session that is gone
830 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
831 struct Session *session)
833 struct NeighbourMapEntry *n;
835 GNUNET_assert (neighbours != NULL);
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "Session %X to peer `%s' ended \n",
840 session, GNUNET_i2s (peer));
842 n = lookup_neighbour (peer);
845 if (session != n->session)
846 return; /* doesn't affect us */
849 GNUNET_free (n->addr);
854 if (GNUNET_YES != n->is_connected)
855 return; /* not connected anymore anyway, shouldn't matter */
856 /* fast disconnect unless ATS suggests a new address */
857 GNUNET_SCHEDULER_cancel (n->timeout_task);
859 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
860 &neighbour_timeout_task, n);
861 /* try QUICKLY to re-establish a connection, reduce timeout! */
862 GNUNET_ATS_suggest_address (GST_ats, peer);
867 * Transmit a message to the given target using the active connection.
869 * @param target destination
870 * @param msg message to send
871 * @param msg_size number of bytes in msg
872 * @param timeout when to fail with timeout
873 * @param cont function to call when done
874 * @param cont_cls closure for 'cont'
877 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
878 size_t msg_size, struct GNUNET_TIME_Relative timeout,
879 GST_NeighbourSendContinuation cont, void *cont_cls)
881 struct NeighbourMapEntry *n;
882 struct MessageQueue *mq;
884 GNUNET_assert (neighbours != NULL);
886 n = lookup_neighbour (target);
887 if ((n == NULL) || (GNUNET_YES != n->is_connected))
889 GNUNET_STATISTICS_update (GST_stats,
891 ("# messages not sent (no such peer or not connected)"),
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Could not send message to peer `%s': unknown neighbor",
897 GNUNET_i2s (target));
898 else if (GNUNET_YES != n->is_connected)
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Could not send message to peer `%s': not connected\n",
901 GNUNET_i2s (target));
904 cont (cont_cls, GNUNET_SYSERR);
908 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
910 GNUNET_STATISTICS_update (GST_stats,
912 ("# messages not sent (no such peer or not connected)"),
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Could not send message to peer `%s': no address available\n",
917 GNUNET_i2s (target));
921 cont (cont_cls, GNUNET_SYSERR);
924 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
925 GNUNET_STATISTICS_update (GST_stats,
927 ("# bytes in message queue for other peers"),
928 msg_size, GNUNET_NO);
929 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
931 mq->cont_cls = cont_cls;
932 /* FIXME: this memcpy can be up to 7% of our total runtime! */
933 memcpy (&mq[1], msg, msg_size);
934 mq->message_buf = (const char *) &mq[1];
935 mq->message_buf_size = msg_size;
936 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
937 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
938 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
939 (NULL == n->is_active))
940 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
945 * We have received a message from the given sender. How long should
946 * we delay before receiving more? (Also used to keep the peer marked
949 * @param sender sender of the message
950 * @param size size of the message
951 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
952 * GNUNET_NO if the neighbour is not connected or violates the quota,
953 * GNUNET_SYSERR if the connection is not fully up yet
954 * @return how long to wait before reading more from this sender
956 struct GNUNET_TIME_Relative
957 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
958 *sender, ssize_t size, int *do_forward)
960 struct NeighbourMapEntry *n;
961 struct GNUNET_TIME_Relative ret;
963 GNUNET_assert (neighbours != NULL);
965 n = lookup_neighbour (sender);
968 *do_forward = GNUNET_NO;
969 return GNUNET_TIME_UNIT_ZERO;
971 if (GNUNET_YES != n->is_connected)
973 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
974 _("Plugin gave us %d bytes of data but somehow the session is not marked as UP yet!\n"),
976 *do_forward = GNUNET_SYSERR;
977 return GNUNET_TIME_UNIT_ZERO;
979 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
981 n->quota_violation_count++;
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
985 n->in_tracker.available_bytes_per_s__,
986 n->quota_violation_count);
988 /* Discount 32k per violation */
989 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
993 if (n->quota_violation_count > 0)
995 /* try to add 32k back */
996 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
997 n->quota_violation_count--;
1000 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1002 GNUNET_STATISTICS_update (GST_stats,
1004 ("# bandwidth quota violations by other peers"),
1006 *do_forward = GNUNET_NO;
1007 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1009 *do_forward = GNUNET_YES;
1010 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
1011 if (ret.rel_value > 0)
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1016 (unsigned long long) n->in_tracker.
1017 consumption_since_last_update__,
1018 (unsigned int) n->in_tracker.available_bytes_per_s__,
1019 (unsigned long long) ret.rel_value);
1021 GNUNET_STATISTICS_update (GST_stats,
1022 gettext_noop ("# ms throttling suggested"),
1023 (int64_t) ret.rel_value, GNUNET_NO);
1030 * Keep the connection to the given neighbour alive longer,
1031 * we received a KEEPALIVE (or equivalent).
1033 * @param neighbour neighbour to keep alive
1036 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1038 struct NeighbourMapEntry *n;
1040 GNUNET_assert (neighbours != NULL);
1042 n = lookup_neighbour (neighbour);
1045 GNUNET_STATISTICS_update (GST_stats,
1047 ("# KEEPALIVE messages discarded (not connected)"),
1051 GNUNET_SCHEDULER_cancel (n->timeout_task);
1053 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1054 &neighbour_timeout_task, n);
1059 * Change the incoming quota for the given peer.
1061 * @param neighbour identity of peer to change qutoa for
1062 * @param quota new quota
1065 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1066 struct GNUNET_BANDWIDTH_Value32NBO quota)
1068 struct NeighbourMapEntry *n;
1070 GNUNET_assert (neighbours != NULL);
1072 n = lookup_neighbour (neighbour);
1075 GNUNET_STATISTICS_update (GST_stats,
1077 ("# SET QUOTA messages ignored (no such peer)"),
1081 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1082 if (0 != ntohl (quota.value__))
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1086 GNUNET_i2s (&n->id), "SET_QUOTA");
1088 if (GNUNET_YES == n->is_connected)
1089 GNUNET_STATISTICS_update (GST_stats,
1090 gettext_noop ("# disconnects due to quota of 0"), 1,
1092 disconnect_neighbour (n);
1097 * Closure for the neighbours_iterate function.
1099 struct IteratorContext
1102 * Function to call on each connected neighbour.
1104 GST_NeighbourIterator cb;
1114 * Call the callback from the closure for each connected neighbour.
1116 * @param cls the 'struct IteratorContext'
1117 * @param key the hash of the public key of the neighbour
1118 * @param value the 'struct NeighbourMapEntry'
1119 * @return GNUNET_OK (continue to iterate)
1122 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1124 struct IteratorContext *ic = cls;
1125 struct NeighbourMapEntry *n = value;
1127 if (GNUNET_YES != n->is_connected)
1130 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1136 * Iterate over all connected neighbours.
1138 * @param cb function to call
1139 * @param cb_cls closure for cb
1142 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1144 struct IteratorContext ic;
1146 GNUNET_assert (neighbours != NULL);
1150 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1155 * If we have an active connection to the given target, it must be shutdown.
1157 * @param target peer to disconnect from
1160 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1162 struct NeighbourMapEntry *n;
1163 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1164 struct SessionDisconnectMessage disconnect_msg;
1166 GNUNET_assert (neighbours != NULL);
1168 n = lookup_neighbour (target);
1170 return; /* not active */
1171 if (GNUNET_YES == n->is_connected)
1173 /* we're actually connected, send DISCONNECT message */
1174 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1175 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1176 disconnect_msg.reserved = htonl (0);
1177 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1178 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1179 sizeof (struct GNUNET_TIME_AbsoluteNBO) );
1180 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1181 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1182 disconnect_msg.public_key = GST_my_public_key;
1183 GNUNET_assert (GNUNET_OK ==
1184 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1185 &disconnect_msg.purpose,
1186 &disconnect_msg.signature));
1187 papi = GST_plugins_find (n->plugin_name);
1189 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1190 sizeof (disconnect_msg),
1191 UINT32_MAX /* priority */ ,
1192 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1193 GNUNET_YES, NULL, NULL);
1194 GNUNET_STATISTICS_update (GST_stats,
1195 gettext_noop ("# peers disconnected due to external request"), 1,
1197 n = lookup_neighbour (target);
1199 return; /* gone already */
1201 disconnect_neighbour (n);
1206 * We received a disconnect message from the given peer,
1207 * validate and process.
1209 * @param peer sender of the message
1210 * @param msg the disconnect message
1213 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
1214 const struct GNUNET_MessageHeader *msg)
1216 struct NeighbourMapEntry *n;
1217 const struct SessionDisconnectMessage *sdm;
1220 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1222 // GNUNET_break_op (0);
1223 GNUNET_STATISTICS_update (GST_stats,
1224 gettext_noop ("# disconnect messages ignored (old format)"), 1,
1228 sdm = (const struct SessionDisconnectMessage* ) msg;
1229 n = lookup_neighbour (peer);
1231 return; /* gone already */
1232 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1233 n->connect_ts.abs_value)
1235 GNUNET_STATISTICS_update (GST_stats,
1236 gettext_noop ("# disconnect messages ignored (timestamp)"), 1,
1240 GNUNET_CRYPTO_hash (&sdm->public_key,
1241 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1243 if (0 != memcmp (peer,
1245 sizeof (struct GNUNET_PeerIdentity)))
1247 GNUNET_break_op (0);
1250 if (ntohl (sdm->purpose.size) !=
1251 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1252 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1253 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1255 GNUNET_break_op (0);
1259 GNUNET_CRYPTO_rsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
1264 GNUNET_break_op (0);
1267 GST_neighbours_force_disconnect (peer);
1272 * We received a 'SESSION_CONNECT' message from the other peer.
1273 * Consider switching to it.
1275 * @param message possibly a 'struct SessionConnectMessage' (check format)
1276 * @param peer identity of the peer to switch the address for
1277 * @param plugin_name name of transport that delivered the PONG
1278 * @param address address of the other peer, NULL if other peer
1280 * @param address_len number of bytes in address
1281 * @param session session to use (or NULL)
1282 * @param ats performance data
1283 * @param ats_count number of entries in ats (excluding 0-termination)
1286 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
1287 const struct GNUNET_PeerIdentity *peer,
1288 const char *plugin_name,
1289 const char *sender_address, uint16_t sender_address_len,
1290 struct Session *session,
1291 const struct GNUNET_ATS_Information *ats,
1294 const struct SessionConnectMessage *scm;
1295 struct GNUNET_TIME_Absolute ts;
1296 struct NeighbourMapEntry *n;
1298 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1300 GNUNET_break_op (0);
1303 scm = (const struct SessionConnectMessage *) message;
1304 GNUNET_break_op (ntohl (scm->reserved) == 0);
1305 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
1306 n = lookup_neighbour (peer);
1308 n = setup_neighbour (peer);
1309 if (ts.abs_value > n->connect_ts.abs_value)
1311 if (NULL != session)
1312 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
1314 "Giving ATS session %p of plugin %s for peer %s\n",
1318 GNUNET_ATS_address_update (GST_ats,
1320 plugin_name, sender_address, sender_address_len,
1321 session, ats, ats_count);
1327 /* end of file gnunet-service-transport_neighbours.c */