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 * Context for address suggestion.
195 * NULL after we are connected.
197 struct GNUNET_ATS_SuggestionContext *asc;
200 * Performance data for the peer.
202 struct GNUNET_TRANSPORT_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 * How often has the other peer (recently) violated the inbound
258 * traffic limit? Incremented by 10 per violation, decremented by 1
259 * per non-violation (for each time interval).
261 unsigned int quota_violation_count;
264 * Number of values in 'ats' array.
266 unsigned int ats_count;
269 * Are we already in the process of disconnecting this neighbour?
274 * Do we currently consider this neighbour connected? (as far as
275 * the connect/disconnect callbacks are concerned)?
283 * All known neighbours and their HELLOs.
285 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
288 * Closure for connect_notify_cb and disconnect_notify_cb
290 static void *callback_cls;
293 * Function to call when we connected to a neighbour.
295 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
298 * Function to call when we disconnected from a neighbour.
300 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
303 * counter for connected neighbours
305 static int neighbours_connected;
308 * Lookup a neighbour entry in the neighbours hash map.
310 * @param pid identity of the peer to look up
311 * @return the entry, NULL if there is no existing record
313 static struct NeighbourMapEntry *
314 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
316 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
321 * Task invoked to start a transmission to another peer.
323 * @param cls the 'struct NeighbourMapEntry'
324 * @param tc scheduler context
327 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
331 * We're done with our transmission attempt, continue processing.
333 * @param cls the 'struct MessageQueue' of the message
334 * @param receiver intended receiver
335 * @param success whether it worked or not
338 transmit_send_continuation (void *cls,
339 const struct GNUNET_PeerIdentity *receiver,
342 struct MessageQueue *mq;
343 struct NeighbourMapEntry *n;
349 GNUNET_assert (n->is_active == mq);
351 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
352 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
354 if (NULL != mq->cont)
355 mq->cont (mq->cont_cls, success);
361 * Check the ready list for the given neighbour and if a plugin is
362 * ready for transmission (and if we have a message), do so!
364 * @param n target peer for which to transmit
367 try_transmission_to_peer (struct NeighbourMapEntry *n)
369 struct MessageQueue *mq;
370 struct GNUNET_TIME_Relative timeout;
372 struct GNUNET_TRANSPORT_PluginFunctions *papi;
374 if (n->is_active != NULL)
375 return; /* transmission already pending */
376 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
377 return; /* currently waiting for bandwidth */
378 while (NULL != (mq = n->messages_head))
380 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
381 if (timeout.rel_value > 0)
383 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
385 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
388 return; /* no more messages */
390 papi = GST_plugins_find (n->plugin_name);
396 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
400 if (((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0)))
402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No address peer for peer `%s'\n",
403 GNUNET_i2s (&n->id));
404 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
405 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
410 papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
411 0 /* priority -- remove from plugin API? */ ,
412 timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
413 &transmit_send_continuation, mq);
416 /* failure, but 'send' would not call continuation in this case,
417 * so we need to do it here! */
418 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
419 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
425 * Task invoked to start a transmission to another peer.
427 * @param cls the 'struct NeighbourMapEntry'
428 * @param tc scheduler context
431 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
433 struct NeighbourMapEntry *n = cls;
435 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
436 try_transmission_to_peer (n);
441 * Initialize the neighbours subsystem.
443 * @param cls closure for callbacks
444 * @param connect_cb function to call if we connect to a peer
445 * @param disconnect_cb function to call if we disconnect from a peer
448 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
449 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
452 connect_notify_cb = connect_cb;
453 disconnect_notify_cb = disconnect_cb;
454 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
459 * Disconnect from the given neighbour, clean up the record.
461 * @param n neighbour to disconnect from
464 disconnect_neighbour (struct NeighbourMapEntry *n)
466 struct MessageQueue *mq;
468 if (GNUNET_YES == n->in_disconnect)
470 n->in_disconnect = GNUNET_YES;
471 while (NULL != (mq = n->messages_head))
473 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
474 if (NULL != mq->cont)
475 mq->cont (mq->cont_cls, GNUNET_SYSERR);
478 if (NULL != n->is_active)
480 n->is_active->n = NULL;
483 if (GNUNET_YES == n->is_connected)
485 n->is_connected = GNUNET_NO;
486 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
487 GNUNET_SCHEDULER_cancel (n->keepalive_task);
488 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
489 GNUNET_assert (neighbours_connected > 0);
490 neighbours_connected--;
491 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
493 disconnect_notify_cb (callback_cls, &n->id);
495 GNUNET_assert (GNUNET_YES ==
496 GNUNET_CONTAINER_multihashmap_remove (neighbours,
497 &n->id.hashPubKey, n));
498 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
500 GNUNET_SCHEDULER_cancel (n->timeout_task);
501 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
503 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
505 GNUNET_SCHEDULER_cancel (n->transmission_task);
506 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
510 GNUNET_ATS_suggest_address_cancel (n->asc);
513 GNUNET_array_grow (n->ats, n->ats_count, 0);
514 if (NULL != n->plugin_name)
516 GNUNET_free (n->plugin_name);
517 n->plugin_name = NULL;
521 GNUNET_free (n->addr);
531 * Peer has been idle for too long. Disconnect.
533 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
534 * @param tc scheduler context
537 neighbour_timeout_task (void *cls,
538 const struct GNUNET_SCHEDULER_TaskContext *tc)
540 struct NeighbourMapEntry *n = cls;
542 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
543 if (GNUNET_YES == n->is_connected)
544 GNUNET_STATISTICS_update (GST_stats,
545 gettext_noop ("# peers disconnected due to timeout"), 1,
547 disconnect_neighbour (n);
552 * Send another keepalive message.
554 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
555 * @param tc scheduler context
558 neighbour_keepalive_task (void *cls,
559 const struct GNUNET_SCHEDULER_TaskContext *tc)
561 struct NeighbourMapEntry *n = cls;
562 struct GNUNET_MessageHeader m;
563 struct GNUNET_TRANSPORT_PluginFunctions *papi;
565 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
566 GNUNET_assert (GNUNET_YES == n->is_connected);
567 GNUNET_STATISTICS_update (GST_stats,
568 gettext_noop ("# keepalives sent"), 1,
570 m.size = htons (sizeof (struct GNUNET_MessageHeader));
571 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
572 papi = GST_plugins_find (n->plugin_name);
574 papi->send (papi->cls,
575 &n->id, (const void *) &m,
577 UINT32_MAX /* priority */ ,
578 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
579 GNUNET_YES, NULL, NULL);
580 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
581 &neighbour_keepalive_task,
587 * Disconnect from the given neighbour.
590 * @param key hash of neighbour's public key (not used)
591 * @param value the 'struct NeighbourMapEntry' of the neighbour
594 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
596 struct NeighbourMapEntry *n = value;
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
600 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
602 if (GNUNET_YES == n->is_connected)
603 GNUNET_STATISTICS_update (GST_stats,
604 gettext_noop ("# peers disconnected due to global disconnect"), 1,
606 disconnect_neighbour (n);
612 * Cleanup the neighbours subsystem.
615 GST_neighbours_stop ()
617 GNUNET_assert (neighbours != NULL);
619 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
621 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
622 GNUNET_assert (neighbours_connected == 0);
625 connect_notify_cb = NULL;
626 disconnect_notify_cb = NULL;
631 * For an existing neighbour record, set the active connection to
634 * @param peer identity of the peer to switch the address for
635 * @param plugin_name name of transport that delivered the PONG
636 * @param address address of the other peer, NULL if other peer
638 * @param address_len number of bytes in address
639 * @param session session to use (or NULL)
640 * @param ats performance data
641 * @param ats_count number of entries in ats (excluding 0-termination)
644 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
645 const char *plugin_name, const void *address,
646 size_t address_len, struct Session *session,
647 const struct GNUNET_TRANSPORT_ATS_Information
648 *ats, uint32_t ats_count)
650 struct NeighbourMapEntry *n;
651 struct SessionConnectMessage connect_msg;
653 GNUNET_assert (neighbours != NULL);
655 n = lookup_neighbour (peer);
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
665 GNUNET_i2s (peer), plugin_name,
666 (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_array_grow (n->ats, n->ats_count, ats_count);
679 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
680 GNUNET_free_non_null (n->plugin_name);
681 n->plugin_name = GNUNET_strdup (plugin_name);
682 GNUNET_SCHEDULER_cancel (n->timeout_task);
684 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
685 &neighbour_timeout_task, n);
686 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
687 connect_msg.header.type =
688 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
689 connect_msg.reserved = htonl (0);
690 connect_msg.timestamp =
691 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
692 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
693 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
698 * Try to connect to the target peer using the given address
700 * @param cls the 'struct NeighbourMapEntry' of the target
701 * @param target identity of the target peer
702 * @param plugin_name name of the plugin
703 * @param plugin_address binary address
704 * @param plugin_address_len length of address
705 * @param session session to use
706 * @param bandwidth_out available outbound bandwidth
707 * @param bandwidth_in available inbound bandwidth
708 * @param ats performance data for the address (as far as known)
709 * @param ats_count number of performance records in 'ats'
712 try_connect_using_address (void *cls, const struct GNUNET_PeerIdentity *target,
713 const char *plugin_name, const void *plugin_address,
714 size_t plugin_address_len, struct Session *session,
715 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
716 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
717 const struct GNUNET_TRANSPORT_ATS_Information *ats,
720 struct NeighbourMapEntry *n = cls;
724 was_connected = n->is_connected;
725 n->is_connected = GNUNET_YES;
727 GST_neighbours_switch_to_address (target, plugin_name, plugin_address,
728 plugin_address_len, session, ats,
730 if (GNUNET_YES == was_connected)
732 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task,
735 /* ATS told us inbound quota for this peer */
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting inbound quota of %u for peer `%s' to \n",
738 ntohl (bandwidth_in), GNUNET_i2s (target));
740 GST_neighbours_set_incoming_quota (&n->id, bandwidth_in);
742 /* First tell clients about connected neighbours...*/
743 neighbours_connected++;
744 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
746 connect_notify_cb (callback_cls, target, n->ats, n->ats_count);
748 /* ... then send outbound quota for this peer to all clients */
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
751 ntohl (bandwidth_out), GNUNET_i2s (target));
754 struct QuotaSetMessage msg;
755 msg.header.size = htons (sizeof (struct QuotaSetMessage));
756 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
757 msg.quota = bandwidth_out;
758 msg.peer = (*target);
759 GST_clients_broadcast ((struct GNUNET_MessageHeader *) &msg, GNUNET_NO);
765 * Try to create a connection to the given target (eventually).
767 * @param target peer to try to connect to
770 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
772 struct NeighbourMapEntry *n;
774 GNUNET_assert (neighbours != NULL);
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
778 GNUNET_i2s (target));
782 memcmp (target, &GST_my_identity,
783 sizeof (struct GNUNET_PeerIdentity)));
784 n = lookup_neighbour (target);
785 if ((NULL != n) && (GNUNET_YES == n->is_connected))
786 return; /* already connected */
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Unknown peer `%s', creating new neighbour\n",
792 GNUNET_i2s (target));
794 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
796 GNUNET_array_grow (n->ats, n->ats_count, 1);
797 n->ats[0].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);;
798 n->ats[0].value = htonl (0);
799 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
800 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
801 MAX_BANDWIDTH_CARRY_S);
803 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
804 &neighbour_timeout_task, n);
805 GNUNET_assert (GNUNET_OK ==
806 GNUNET_CONTAINER_multihashmap_put (neighbours,
807 &n->id.hashPubKey, n,
808 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
811 return; /* already trying */
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Asking ATS for suggested address to connect to peer `%s'\n",
815 GNUNET_i2s (target));
818 GNUNET_ATS_suggest_address (GST_ats, target, &try_connect_using_address,
824 * Test if we're connected to the given peer.
826 * @param target peer to test
827 * @return GNUNET_YES if we are connected, GNUNET_NO if not
830 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
832 struct NeighbourMapEntry *n;
834 GNUNET_assert (neighbours != NULL);
836 n = lookup_neighbour (target);
837 if ((NULL == n) || (n->is_connected != GNUNET_YES))
838 return GNUNET_NO; /* not connected */
844 * A session was terminated. Take note.
846 * @param peer identity of the peer where the session died
847 * @param session session that is gone
850 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
851 struct Session *session)
853 struct NeighbourMapEntry *n;
855 GNUNET_assert (neighbours != NULL);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Session %X to peer `%s' ended \n",
860 session, GNUNET_i2s (peer));
863 n = lookup_neighbour (peer);
866 if (session != n->session)
867 return; /* doesn't affect us */
870 GNUNET_free (n->addr);
875 if (GNUNET_YES != n->is_connected)
876 return; /* not connected anymore anyway, shouldn't matter */
878 //n->is_connected = GNUNET_NO;
880 /* fast disconnect unless ATS suggests a new address */
881 GNUNET_SCHEDULER_cancel (n->timeout_task);
883 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
884 &neighbour_timeout_task, n);
885 /* try QUICKLY to re-establish a connection, reduce timeout! */
888 /* how can this be!? */
893 GNUNET_ATS_suggest_address (GST_ats, peer, &try_connect_using_address, n);
898 * Transmit a message to the given target using the active connection.
900 * @param target destination
901 * @param msg message to send
902 * @param msg_size number of bytes in msg
903 * @param timeout when to fail with timeout
904 * @param cont function to call when done
905 * @param cont_cls closure for 'cont'
908 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
909 size_t msg_size, struct GNUNET_TIME_Relative timeout,
910 GST_NeighbourSendContinuation cont, void *cont_cls)
912 struct NeighbourMapEntry *n;
913 struct MessageQueue *mq;
915 GNUNET_assert (neighbours != NULL);
917 n = lookup_neighbour (target);
918 if ((n == NULL) || (GNUNET_YES != n->is_connected))
920 GNUNET_STATISTICS_update (GST_stats,
922 ("# messages not sent (no such peer or not connected)"),
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Could not send message to peer `%s': unknown neighbor",
928 GNUNET_i2s (target));
929 else if (GNUNET_YES != n->is_connected)
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "Could not send message to peer `%s': not connected\n",
932 GNUNET_i2s (target));
935 cont (cont_cls, GNUNET_SYSERR);
939 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
941 GNUNET_STATISTICS_update (GST_stats,
943 ("# messages not sent (no such peer or not connected)"),
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Could not send message to peer `%s': no address available\n",
948 GNUNET_i2s (target));
952 cont (cont_cls, GNUNET_SYSERR);
957 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
958 GNUNET_STATISTICS_update (GST_stats,
960 ("# bytes in message queue for other peers"),
961 msg_size, GNUNET_NO);
962 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
964 mq->cont_cls = cont_cls;
965 /* FIXME: this memcpy can be up to 7% of our total runtime! */
966 memcpy (&mq[1], msg, msg_size);
967 mq->message_buf = (const char *) &mq[1];
968 mq->message_buf_size = msg_size;
969 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
970 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
971 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
972 (NULL == n->is_active))
973 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
978 * We have received a message from the given sender. How long should
979 * we delay before receiving more? (Also used to keep the peer marked
982 * @param sender sender of the message
983 * @param size size of the message
984 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
985 * GNUNET_NO if the neighbour is not connected or violates the quota
986 * @return how long to wait before reading more from this sender
988 struct GNUNET_TIME_Relative
989 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
990 *sender, ssize_t size, int *do_forward)
992 struct NeighbourMapEntry *n;
993 struct GNUNET_TIME_Relative ret;
995 GNUNET_assert (neighbours != NULL);
997 n = lookup_neighbour (sender);
1000 *do_forward = GNUNET_NO;
1001 return GNUNET_TIME_UNIT_ZERO;
1003 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1005 n->quota_violation_count++;
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1009 n->in_tracker.available_bytes_per_s__,
1010 n->quota_violation_count);
1012 /* Discount 32k per violation */
1013 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1017 if (n->quota_violation_count > 0)
1019 /* try to add 32k back */
1020 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1021 n->quota_violation_count--;
1024 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1026 GNUNET_STATISTICS_update (GST_stats,
1028 ("# bandwidth quota violations by other peers"),
1030 *do_forward = GNUNET_NO;
1031 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1033 *do_forward = GNUNET_YES;
1034 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
1035 if (ret.rel_value > 0)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1040 (unsigned long long) n->in_tracker.
1041 consumption_since_last_update__,
1042 (unsigned int) n->in_tracker.available_bytes_per_s__,
1043 (unsigned long long) ret.rel_value);
1045 GNUNET_STATISTICS_update (GST_stats,
1046 gettext_noop ("# ms throttling suggested"),
1047 (int64_t) ret.rel_value, GNUNET_NO);
1054 * Keep the connection to the given neighbour alive longer,
1055 * we received a KEEPALIVE (or equivalent).
1057 * @param neighbour neighbour to keep alive
1060 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1062 struct NeighbourMapEntry *n;
1064 GNUNET_assert (neighbours != NULL);
1066 n = lookup_neighbour (neighbour);
1069 GNUNET_STATISTICS_update (GST_stats,
1071 ("# KEEPALIVE messages discarded (not connected)"),
1075 GNUNET_SCHEDULER_cancel (n->timeout_task);
1077 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1078 &neighbour_timeout_task, n);
1083 * Change the incoming quota for the given peer.
1085 * @param neighbour identity of peer to change qutoa for
1086 * @param quota new quota
1089 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1090 struct GNUNET_BANDWIDTH_Value32NBO quota)
1092 struct NeighbourMapEntry *n;
1094 GNUNET_assert (neighbours != NULL);
1096 n = lookup_neighbour (neighbour);
1099 GNUNET_STATISTICS_update (GST_stats,
1101 ("# SET QUOTA messages ignored (no such peer)"),
1105 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1106 if (0 != ntohl (quota.value__))
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1110 GNUNET_i2s (&n->id), "SET_QUOTA");
1112 if (GNUNET_YES == n->is_connected)
1113 GNUNET_STATISTICS_update (GST_stats,
1114 gettext_noop ("# disconnects due to quota of 0"), 1,
1116 disconnect_neighbour (n);
1121 * Closure for the neighbours_iterate function.
1123 struct IteratorContext
1126 * Function to call on each connected neighbour.
1128 GST_NeighbourIterator cb;
1138 * Call the callback from the closure for each connected neighbour.
1140 * @param cls the 'struct IteratorContext'
1141 * @param key the hash of the public key of the neighbour
1142 * @param value the 'struct NeighbourMapEntry'
1143 * @return GNUNET_OK (continue to iterate)
1146 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1148 struct IteratorContext *ic = cls;
1149 struct NeighbourMapEntry *n = value;
1151 if (GNUNET_YES != n->is_connected)
1154 GNUNET_assert (n->ats_count > 0);
1155 ic->cb (ic->cb_cls, &n->id, n->ats, n->ats_count, n->plugin_name, n->addr, n->addrlen);
1161 * Iterate over all connected neighbours.
1163 * @param cb function to call
1164 * @param cb_cls closure for cb
1167 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1169 struct IteratorContext ic;
1171 GNUNET_assert (neighbours != NULL);
1175 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1180 * If we have an active connection to the given target, it must be shutdown.
1182 * @param target peer to disconnect from
1185 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1187 struct NeighbourMapEntry *n;
1188 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1189 struct SessionDisconnectMessage disconnect_msg;
1191 GNUNET_assert (neighbours != NULL);
1193 n = lookup_neighbour (target);
1195 return; /* not active */
1196 if (GNUNET_YES == n->is_connected)
1198 /* we're actually connected, send DISCONNECT message */
1199 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1200 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1201 disconnect_msg.reserved = htonl (0);
1202 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1203 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1204 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1205 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1206 GNUNET_assert (GNUNET_OK ==
1207 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1208 &disconnect_msg.purpose,
1209 &disconnect_msg.signature));
1210 papi = GST_plugins_find (n->plugin_name);
1212 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1213 sizeof (disconnect_msg),
1214 UINT32_MAX /* priority */ ,
1215 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1216 GNUNET_YES, NULL, NULL);
1217 GNUNET_STATISTICS_update (GST_stats,
1218 gettext_noop ("# peers disconnected due to external request"), 1,
1221 disconnect_neighbour (n);
1225 /* end of file gnunet-service-transport_neighbours.c */