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-service-transport_neighbours.h"
28 #include "gnunet-service-transport.h"
29 #include "gnunet_constants.h"
33 * Size of the neighbour hash map.
35 #define NEIGHBOUR_TABLE_SIZE 256
39 // - have a way to access the currently 'connected' session
40 // (for sending and to notice disconnect of it!)
41 // - have a way to access/update bandwidth/quota information per peer
42 // (for CostReport/TrafficReport callbacks)
45 struct NeighbourMapEntry;
48 * For each neighbour we keep a list of messages
49 * that we still want to transmit to the neighbour.
55 * This is a doubly linked list.
57 struct MessageQueue *next;
60 * This is a doubly linked list.
62 struct MessageQueue *prev;
65 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
66 * stuck together in memory. Allocated at the end of this struct.
68 const char *message_buf;
71 * Size of the message buf
73 size_t message_buf_size;
76 * Client responsible for queueing the message; used to check that a
77 * client has no two messages pending for the same target and to
78 * notify the client of a successful transmission; NULL if this is
79 * an internal message.
81 struct TransportClient *client;
84 * At what time should we fail?
86 struct GNUNET_TIME_Absolute timeout;
89 * Internal message of the transport system that should not be
90 * included in the usual SEND-SEND_OK transmission confirmation
91 * traffic management scheme. Typically, "internal_msg" will
92 * be set whenever "client" is NULL (but it is not strictly
98 * How important is the message?
100 unsigned int priority;
107 * Entry in neighbours.
109 struct NeighbourMapEntry
113 * Head of list of messages we would like to send to this peer;
114 * must contain at most one message per client.
116 struct MessageQueue *messages_head;
119 * Tail of list of messages we would like to send to this peer; must
120 * contain at most one message per client.
122 struct MessageQueue *messages_tail;
125 * Context for peerinfo iteration.
126 * NULL after we are done processing peerinfo's information.
128 struct GNUNET_PEERINFO_IteratorContext *piter;
131 * Performance data for the peer.
133 struct GNUNET_TRANSPORT_ATS_Information *ats;
136 * Public key for this peer. Valid only if the respective flag is set below.
138 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
141 * Identity of this neighbour.
143 struct GNUNET_PeerIdentity id;
146 * ID of task scheduled to run when this peer is about to
147 * time out (will free resources associated with the peer).
149 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
152 * ID of task scheduled to run when we should retry transmitting
153 * the head of the message queue. Actually triggered when the
154 * transmission is timing out (we trigger instantly when we have
155 * a chance of success).
157 GNUNET_SCHEDULER_TaskIdentifier retry_task;
160 * How long until we should consider this peer dead (if we don't
161 * receive another message in the meantime)?
163 struct GNUNET_TIME_Absolute peer_timeout;
166 * Tracker for inbound bandwidth.
168 struct GNUNET_BANDWIDTH_Tracker in_tracker;
171 * The latency we have seen for this particular address for
172 * this particular peer. This latency may have been calculated
173 * over multiple transports. This value reflects how long it took
174 * us to receive a response when SENDING via this particular
175 * transport/neighbour/address combination!
177 * FIXME: we need to periodically send PINGs to update this
178 * latency (at least more often than the current "huge" (11h?)
181 struct GNUNET_TIME_Relative latency;
184 * How often has the other peer (recently) violated the inbound
185 * traffic limit? Incremented by 10 per violation, decremented by 1
186 * per non-violation (for each time interval).
188 unsigned int quota_violation_count;
191 * DV distance to this peer (1 if no DV is used).
196 * Have we seen an PONG from this neighbour in the past (and
197 * not had a disconnect since)?
202 * Do we have a valid public key for this neighbour?
204 int public_key_valid;
207 * Are we already in the process of disconnecting this neighbour?
215 * All known neighbours and their HELLOs.
217 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
220 * Closure for connect_notify_cb and disconnect_notify_cb
222 static void *callback_cls;
225 * Function to call when we connected to a neighbour.
227 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
230 * Function to call when we disconnected from a neighbour.
232 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
237 * Check the ready list for the given neighbour and if a plugin is
238 * ready for transmission (and if we have a message), do so!
240 * @param neighbour target peer for which to transmit
243 try_transmission_to_peer (struct NeighbourMapEntry *n)
245 struct ReadyList *rl;
246 struct MessageQueue *mq;
247 struct GNUNET_TIME_Relative timeout;
251 if (n->messages_head == NULL)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Transmission queue for `%4s' is empty\n",
256 GNUNET_i2s (&n->id));
258 return; /* nothing to do */
261 mq = n->messages_head;
262 force_address = GNUNET_YES;
263 if (mq->specific_address == NULL)
266 mq->specific_address = get_preferred_ats_address(n);
267 GNUNET_STATISTICS_update (stats,
268 gettext_noop ("# transport selected peer address freely"),
271 force_address = GNUNET_NO;
273 if (mq->specific_address == NULL)
275 GNUNET_STATISTICS_update (stats,
276 gettext_noop ("# transport failed to selected peer address"),
279 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
280 if (timeout.rel_value == 0)
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "No destination address available to transmit message of size %u to peer `%4s'\n",
285 mq->message_buf_size,
286 GNUNET_i2s (&mq->neighbour_id));
288 GNUNET_STATISTICS_update (stats,
289 gettext_noop ("# bytes in message queue for other peers"),
290 - (int64_t) mq->message_buf_size,
292 GNUNET_STATISTICS_update (stats,
293 gettext_noop ("# bytes discarded (no destination address available)"),
294 mq->message_buf_size,
296 if (mq->client != NULL)
297 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
298 GNUNET_CONTAINER_DLL_remove (n->messages_head,
302 return; /* nobody ready */
304 GNUNET_STATISTICS_update (stats,
305 gettext_noop ("# message delivery deferred (no address)"),
308 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
309 GNUNET_SCHEDULER_cancel (n->retry_task);
310 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
311 &retry_transmission_task,
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
316 mq->message_buf_size,
317 GNUNET_i2s (&mq->neighbour_id),
320 /* FIXME: might want to trigger peerinfo lookup here
321 (unless that's already pending...) */
324 GNUNET_CONTAINER_DLL_remove (n->messages_head,
327 if (mq->specific_address->connected == GNUNET_NO)
328 mq->specific_address->connect_attempts++;
329 rl = mq->specific_address->ready_list;
330 mq->plugin = rl->plugin;
331 if (!mq->internal_msg)
332 mq->specific_address->in_transmit = GNUNET_YES;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
336 mq->message_buf_size,
338 (mq->specific_address->addr != NULL)
339 ? a2s (mq->plugin->short_name,
340 mq->specific_address->addr,
341 mq->specific_address->addrlen)
343 rl->plugin->short_name);
345 GNUNET_STATISTICS_update (stats,
346 gettext_noop ("# bytes in message queue for other peers"),
347 - (int64_t) mq->message_buf_size,
349 GNUNET_STATISTICS_update (stats,
350 gettext_noop ("# bytes pending with plugins"),
351 mq->message_buf_size,
354 GNUNET_CONTAINER_DLL_insert (n->cont_head,
358 ret = rl->plugin->api->send (rl->plugin->api->cls,
361 mq->message_buf_size,
363 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
364 mq->specific_address->session,
365 mq->specific_address->addr,
366 mq->specific_address->addrlen,
368 &transmit_send_continuation, mq);
371 /* failure, but 'send' would not call continuation in this case,
372 so we need to do it here! */
373 transmit_send_continuation (mq,
381 * Send the specified message to the specified peer.
383 * @param client source of the transmission request (can be NULL)
384 * @param peer_address ForeignAddressList where we should send this message
385 * @param priority how important is the message
386 * @param timeout how long do we have to transmit?
387 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
388 * @param message_buf_size total size of all messages in message_buf
389 * @param is_internal is this an internal message; these are pre-pended and
390 * also do not count for plugins being "ready" to transmit
391 * @param neighbour handle to the neighbour for transmission
394 transmit_to_peer (struct TransportClient *client,
395 struct ForeignAddressList *peer_address,
396 unsigned int priority,
397 struct GNUNET_TIME_Relative timeout,
398 const char *message_buf,
399 size_t message_buf_size,
400 int is_internal, struct NeighbourMapEntry *neighbour)
402 struct MessageQueue *mq;
407 /* check for duplicate submission */
408 mq = neighbour->messages_head;
411 if (mq->client == client)
413 /* client transmitted to same peer twice
414 before getting SEND_OK! */
422 GNUNET_STATISTICS_update (stats,
423 gettext_noop ("# bytes in message queue for other peers"),
426 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
427 mq->specific_address = peer_address;
429 /* FIXME: this memcpy can be up to 7% of our total runtime! */
430 memcpy (&mq[1], message_buf, message_buf_size);
431 mq->message_buf = (const char*) &mq[1];
432 mq->message_buf_size = message_buf_size;
433 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
434 mq->internal_msg = is_internal;
435 mq->priority = priority;
436 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
438 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
439 neighbour->messages_tail,
442 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
443 neighbour->messages_tail,
444 neighbour->messages_tail,
446 try_transmission_to_peer (neighbour);
451 * Create a fresh entry in our neighbour list for the given peer.
452 * Will try to transmit our current HELLO to the new neighbour.
453 * Do not call this function directly, use 'setup_peer_check_blacklist.
455 * @param peer the peer for which we create the entry
456 * @param do_hello should we schedule transmitting a HELLO
457 * @return the new neighbour list entry
459 static struct NeighbourMapEntry *
460 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
463 struct NeighbourMapEntry *n;
464 struct TransportPlugin *tp;
465 struct ReadyList *rl;
467 GNUNET_assert (0 != memcmp (peer,
469 sizeof (struct GNUNET_PeerIdentity)));
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Setting up state for neighbour `%4s'\n",
475 GNUNET_STATISTICS_update (stats,
476 gettext_noop ("# active neighbours"),
479 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
482 GNUNET_TIME_relative_to_absolute
483 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
484 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
485 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
486 MAX_BANDWIDTH_CARRY_S);
490 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
492 rl = GNUNET_malloc (sizeof (struct ReadyList));
494 rl->next = n->plugins;
497 rl->addresses = NULL;
501 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
503 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
504 &neighbour_timeout_task, n);
505 GNUNET_CONTAINER_multihashmap_put (neighbours,
508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
511 GNUNET_STATISTICS_update (stats,
512 gettext_noop ("# peerinfo new neighbor iterate requests"),
515 GNUNET_STATISTICS_update (stats,
516 gettext_noop ("# outstanding peerinfo iterate requests"),
519 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
520 GNUNET_TIME_UNIT_FOREVER_REL,
521 &add_hello_for_peer, n);
523 GNUNET_STATISTICS_update (stats,
524 gettext_noop ("# HELLO's sent to new neighbors"),
527 if (NULL != our_hello)
528 transmit_to_peer (NULL, NULL, 0,
529 HELLO_ADDRESS_EXPIRATION,
530 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
539 * Initialize the neighbours subsystem.
541 * @param cls closure for callbacks
542 * @param connect_cb function to call if we connect to a peer
543 * @param disconnect_cb function to call if we disconnect from a peer
546 GST_neighbours_start (void *cls,
547 GNUNET_TRANSPORT_NotifyConnect connect_cb,
548 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
551 connect_notify_cb = connect_cb;
552 disconnect_notify_cb = disconnect_cb;
553 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
558 * Disconnect from the given neighbour.
561 * @param key hash of neighbour's public key (not used)
562 * @param value the 'struct NeighbourMapEntry' of the neighbour
565 disconnect_all_neighbours (void *cls,
566 const GNUNET_HashCode *key,
569 struct NeighbourMapEntry *n = value;
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Disconnecting peer `%4s', %s\n",
578 // disconnect_neighbour (n);
585 * Cleanup the neighbours subsystem.
588 GST_neighbours_stop ()
590 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
591 &disconnect_all_neighbours,
593 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
596 connect_notify_cb = NULL;
597 disconnect_notify_cb = NULL;
602 * Try to create a connection to the given target (eventually).
604 * @param target peer to try to connect to
607 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
613 * Test if we're connected to the given peer.
615 * @param target peer to test
616 * @return GNUNET_YES if we are connected, GNUNET_NO if not
619 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
626 * Transmit a message to the given target using the active connection.
628 * @param target destination
629 * @param msg message to send
630 * @param cont function to call when done
631 * @param cont_cls closure for 'cont'
634 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
635 const struct GNUNET_MessageHeader *msg,
636 GST_NeighbourSendContinuation cont,
643 * Change the incoming quota for the given peer.
645 * @param neighbour identity of peer to change qutoa for
646 * @param quota new quota
649 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
650 struct GNUNET_BANDWIDTH_Value32NBO quota)
654 n = find_neighbour (neighbour);
657 GNUNET_STATISTICS_update (stats,
658 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
663 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
665 if (0 != ntohl (qsm->quota.value__))
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Disconnecting peer `%4s' due to `%s'\n",
673 GNUNET_STATISTICS_update (stats,
674 gettext_noop ("# disconnects due to quota of 0"),
677 GST_neighbours_force_disconnect (neighbour);
684 * If we have an active connection to the given target, it must be shutdown.
686 * @param target peer to disconnect from
689 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
695 * Iterate over all connected neighbours.
697 * @param cb function to call
698 * @param cb_cls closure for cb
701 GST_neighbours_iterate (GST_NeighbourIterator cb,
708 * We have received a PONG. Update lifeness of the neighbour.
710 * @param sender peer sending the PONG
711 * @param hdr the PONG message (presumably)
712 * @param plugin_name name of transport that delivered the PONG
713 * @param sender_address address of the other peer, NULL if other peer
715 * @param sender_address_len number of bytes in sender_address
716 * @param ats performance data
717 * @param ats_count number of entries in ats (excluding 0-termination)
718 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not
721 GST_neighbours_handle_pong (const struct GNUNET_PeerIdentity *sender,
722 const struct GNUNET_MessageHeader *hdr,
723 const char *plugin_name,
724 const void *sender_address,
725 size_t sender_address_len,
726 const struct GNUNET_TRANSPORT_ATS_Information *ats,
729 return GNUNET_SYSERR;
734 * We have received a CONNECT. Set the peer to connected.
736 * @param sender peer sending the PONG
737 * @param hdr the PONG message (presumably)
738 * @param plugin_name name of transport that delivered the PONG
739 * @param sender_address address of the other peer, NULL if other peer
741 * @param sender_address_len number of bytes in sender_address
742 * @param ats performance data
743 * @param ats_count number of entries in ats (excluding 0-termination)
744 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not
747 GST_neighbours_handle_connect (const struct GNUNET_PeerIdentity *sender,
748 const struct GNUNET_MessageHeader *hdr,
749 const char *plugin_name,
750 const void *sender_address,
751 size_t sender_address_len,
752 const struct GNUNET_TRANSPORT_ATS_Information *ats,
755 return GNUNET_SYSERR;
760 * We have received a DISCONNECT. Set the peer to disconnected.
762 * @param sender peer sending the PONG
763 * @param hdr the PONG message (presumably)
764 * @param plugin_name name of transport that delivered the PONG
765 * @param sender_address address of the other peer, NULL if other peer
767 * @param sender_address_len number of bytes in sender_address
768 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not
771 GST_neighbours_handle_disconnect (const struct GNUNET_PeerIdentity *sender,
772 const struct GNUNET_MessageHeader *hdr,
773 const char *plugin_name,
774 const void *sender_address,
775 size_t sender_address_len)
777 return GNUNET_SYSERR;
781 /* end of file gnunet-service-transport_neighbours.c */