2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file core/gnunet-service-core_neighbours.c
23 * @brief code for managing low-level 'plaintext' connections with transport (key exchange may or may not be done yet)
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet-service-core.h"
31 #include "gnunet-service-core_neighbours.h"
32 #include "gnunet-service-core_kx.h"
33 #include "gnunet-service-core_sessions.h"
34 #include "gnunet_constants.h"
38 * Message ready for transmission via transport service. This struct
39 * is followed by the actual content of the message.
41 struct NeighbourMessageEntry
45 * We keep messages in a doubly linked list.
47 struct NeighbourMessageEntry *next;
50 * We keep messages in a doubly linked list.
52 struct NeighbourMessageEntry *prev;
55 * By when are we supposed to transmit this message?
57 struct GNUNET_TIME_Absolute deadline;
60 * What time did we submit the request?
62 struct GNUNET_TIME_Absolute submission_time;
65 * How long is the message? (number of bytes following the `struct
66 * MessageEntry`, but not including the size of `struct
67 * MessageEntry` itself!)
75 * Data kept per transport-connected peer.
81 * Head of the batched message queue (already ordered, transmit
82 * starting with the head).
84 struct NeighbourMessageEntry *message_head;
87 * Tail of the batched message queue (already ordered, append new
90 struct NeighbourMessageEntry *message_tail;
93 * Handle for pending requests for transmission to this peer
94 * with the transport service. NULL if no request is pending.
96 struct GNUNET_TRANSPORT_TransmitHandle *th;
99 * Information about the key exchange with the other peer.
101 struct GSC_KeyExchangeInfo *kxinfo;
104 * Identity of the other peer.
106 struct GNUNET_PeerIdentity peer;
109 * ID of task used for re-trying plaintext scheduling.
111 struct GNUNET_SCHEDULER_Task *retry_plaintext_task;
114 * How many messages are in the queue for this neighbour?
116 unsigned int queue_size;
119 * #GNUNET_YES if this peer currently has excess bandwidth.
121 int has_excess_bandwidth;
127 * Map of peer identities to `struct Neighbour`.
129 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
134 static struct GNUNET_TRANSPORT_Handle *transport;
138 * Find the entry for the given neighbour.
140 * @param peer identity of the neighbour
141 * @return NULL if we are not connected, otherwise the
144 static struct Neighbour *
145 find_neighbour (const struct GNUNET_PeerIdentity *peer)
147 if (NULL == neighbours)
149 return GNUNET_CONTAINER_multipeermap_get (neighbours,
155 * Free the given entry for the neighbour.
157 * @param n neighbour to free
160 free_neighbour (struct Neighbour *n)
162 struct NeighbourMessageEntry *m;
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Destroying neighbour entry for peer `%s'\n",
166 GNUNET_i2s (&n->peer));
167 while (NULL != (m = n->message_head))
169 GNUNET_CONTAINER_DLL_remove (n->message_head,
175 GNUNET_assert (0 == n->queue_size);
178 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
181 GNUNET_STATISTICS_update (GSC_stats,
183 ("# sessions terminated by transport disconnect"),
185 if (NULL != n->kxinfo)
187 GSC_KX_stop (n->kxinfo);
190 if (NULL != n->retry_plaintext_task)
192 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
193 n->retry_plaintext_task = NULL;
195 GNUNET_assert (GNUNET_OK ==
196 GNUNET_CONTAINER_multipeermap_remove (neighbours,
198 GNUNET_STATISTICS_set (GSC_stats,
199 gettext_noop ("# neighbour entries allocated"),
200 GNUNET_CONTAINER_multipeermap_size (neighbours),
207 * Check if we have encrypted messages for the specified neighbour
208 * pending, and if so, check with the transport about sending them
211 * @param n neighbour to check.
214 process_queue (struct Neighbour *n);
218 * Function called when the transport service is ready to receive a
219 * message for the respective peer
221 * @param cls neighbour to use message from
222 * @param size number of bytes we can transmit
223 * @param buf where to copy the message
224 * @return number of bytes transmitted
227 transmit_ready (void *cls,
231 struct Neighbour *n = cls;
232 struct NeighbourMessageEntry *m;
235 struct GNUNET_TIME_Relative delay;
236 struct GNUNET_TIME_Relative overdue;
245 GNUNET_CONTAINER_DLL_remove (n->message_head,
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Transmission of message of type %u and size %u failed\n",
254 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
255 (unsigned int) m->size);
260 delay = GNUNET_TIME_absolute_get_duration (m->submission_time);
261 overdue = GNUNET_TIME_absolute_get_duration (m->deadline);
263 GNUNET_assert (size >= m->size);
268 if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
270 "Copied overdue message of type %u and size %u into transport buffer for `%s' with delay of %s\n",
272 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
274 GNUNET_i2s (&n->peer),
275 GNUNET_STRINGS_relative_time_to_string (delay,
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Copied message of type %u and size %u into transport buffer for `%s' with delay of %s\n",
281 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
283 GNUNET_i2s (&n->peer),
284 GNUNET_STRINGS_relative_time_to_string (delay,
287 n->has_excess_bandwidth = GNUNET_NO;
289 GNUNET_STATISTICS_update (GSC_stats,
291 ("# encrypted bytes given to transport"), ret,
298 * Check if we have messages for the specified neighbour pending, and
299 * if so, check with the transport about sending them out.
301 * @param n neighbour to check.
304 process_queue (struct Neighbour *n)
306 struct NeighbourMessageEntry *m;
309 return; /* request already pending */
313 /* notify sessions that the queue is empty and more messages
314 * could thus be queued now */
315 GSC_SESSIONS_solicit (&n->peer);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Asking transport for transmission of %u bytes to `%s' in next %s\n",
320 (unsigned int) m->size,
321 GNUNET_i2s (&n->peer),
322 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline),
324 m->submission_time = GNUNET_TIME_absolute_get ();
326 = GNUNET_TRANSPORT_notify_transmit_ready (transport,
329 GNUNET_TIME_absolute_get_remaining (m->deadline),
334 /* message request too large or duplicate request */
336 /* discard encrypted message */
337 GNUNET_CONTAINER_DLL_remove (n->message_head,
347 * Function called by transport to notify us that
348 * a peer connected to us (on the network level).
351 * @param peer the peer that connected
354 handle_transport_notify_connect (void *cls,
355 const struct GNUNET_PeerIdentity *peer)
359 if (0 == memcmp (peer,
361 sizeof (struct GNUNET_PeerIdentity)))
366 n = find_neighbour (peer);
369 /* duplicate connect notification!? */
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 "Peer %s exists already\n",
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Received connection from `%s'.\n",
379 n = GNUNET_new (struct Neighbour);
381 GNUNET_assert (GNUNET_OK ==
382 GNUNET_CONTAINER_multipeermap_put (neighbours,
385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
386 GNUNET_STATISTICS_set (GSC_stats,
387 gettext_noop ("# neighbour entries allocated"),
388 GNUNET_CONTAINER_multipeermap_size (neighbours),
390 n->kxinfo = GSC_KX_start (peer);
395 * Function called by transport telling us that a peer
399 * @param peer the peer that disconnected
402 handle_transport_notify_disconnect (void *cls,
403 const struct GNUNET_PeerIdentity *peer)
407 if (0 == memcmp (peer,
409 sizeof (struct GNUNET_PeerIdentity)))
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Peer `%s' disconnected from us; received notification from transport.\n",
417 n = find_neighbour (peer);
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 "Peer %s not found\n",
431 * Function called by the transport for each received message.
434 * @param peer (claimed) identity of the other peer
435 * @param message the message
438 handle_transport_receive (void *cls,
439 const struct GNUNET_PeerIdentity *peer,
440 const struct GNUNET_MessageHeader *message)
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446 "Received message of type %u from `%s', demultiplexing.\n",
447 (unsigned int) ntohs (message->type),
449 if (0 == memcmp (peer,
451 sizeof (struct GNUNET_PeerIdentity)))
456 n = find_neighbour (peer);
459 /* received message from peer that is not connected!? */
461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
462 "Peer %s not found\n",
466 type = ntohs (message->type);
469 case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
470 GSC_KX_handle_ephemeral_key (n->kxinfo, message);
472 case GNUNET_MESSAGE_TYPE_CORE_PING:
473 GSC_KX_handle_ping (n->kxinfo, message);
475 case GNUNET_MESSAGE_TYPE_CORE_PONG:
476 GSC_KX_handle_pong (n->kxinfo, message);
478 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
479 GSC_KX_handle_encrypted_message (n->kxinfo, message);
481 case GNUNET_MESSAGE_TYPE_DUMMY:
482 /* Dummy messages for testing / benchmarking, just discard */
485 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
486 _("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
488 (unsigned int) ntohs (message->size),
496 * Transmit the given message to the given target.
498 * @param target peer that should receive the message (must be connected)
499 * @param msg message to transmit
500 * @param timeout by when should the transmission be done?
503 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
504 const struct GNUNET_MessageHeader *msg,
505 struct GNUNET_TIME_Relative timeout)
507 struct NeighbourMessageEntry *me;
511 n = find_neighbour (target);
515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
516 "Peer %s not found\n",
517 GNUNET_i2s (target));
520 msize = ntohs (msg->size);
521 me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
522 me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
527 GNUNET_CONTAINER_DLL_insert_tail (n->message_head,
536 * One of our neighbours has excess bandwidth, remember this.
539 * @param pid identity of the peer with excess bandwidth
542 handle_transport_notify_excess_bw (void *cls,
543 const struct GNUNET_PeerIdentity *pid)
547 n = find_neighbour (pid);
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
552 "Peer %s not found\n",
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Peer %s has excess bandwidth available\n",
559 n->has_excess_bandwidth = GNUNET_YES;
560 GSC_SESSIONS_solicit (pid);
565 * Check how many messages are queued for the given neighbour.
567 * @param target neighbour to check
568 * @return number of items in the message queue
571 GSC_NEIGHBOURS_get_queue_size (const struct GNUNET_PeerIdentity *target)
575 n = find_neighbour (target);
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 "Peer %s not found\n",
581 GNUNET_i2s (target));
584 return n->queue_size;
589 * Check if the given neighbour has excess bandwidth available.
591 * @param target neighbour to check
592 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
595 GSC_NEIGHBOURS_check_excess_bandwidth (const struct GNUNET_PeerIdentity *target)
599 n = find_neighbour (target);
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604 "Peer %s not found\n",
605 GNUNET_i2s (target));
606 return GNUNET_SYSERR;
608 return n->has_excess_bandwidth;
613 * Initialize neighbours subsystem.
616 GSC_NEIGHBOURS_init ()
618 neighbours = GNUNET_CONTAINER_multipeermap_create (128,
621 GNUNET_TRANSPORT_connect2 (GSC_cfg,
624 &handle_transport_receive,
625 &handle_transport_notify_connect,
626 &handle_transport_notify_disconnect,
627 &handle_transport_notify_excess_bw);
628 if (NULL == transport)
630 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
632 return GNUNET_SYSERR;
639 * Wrapper around #free_neighbour().
642 * @param key peer identity
643 * @param value the `struct Neighbour` to free
644 * @return #GNUNET_OK (continue to iterate)
647 free_neighbour_helper (void *cls,
648 const struct GNUNET_PeerIdentity * key,
651 struct Neighbour *n = value;
653 /* transport should have 'disconnected' all neighbours... */
661 * Shutdown neighbours subsystem.
664 GSC_NEIGHBOURS_done ()
666 if (NULL != transport)
668 GNUNET_TRANSPORT_disconnect (transport);
671 if (NULL != neighbours)
673 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
674 &free_neighbour_helper,
676 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
681 /* end of gnunet-service-core_neighbours.c */