2 This file is part of GNUnet.
3 Copyright (C) 2009, 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., 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;
244 GNUNET_CONTAINER_DLL_remove (n->message_head,
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Transmission of message of type %u and size %u failed\n",
253 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
254 (unsigned int) m->size);
259 delay = GNUNET_TIME_absolute_get_duration (m->submission_time);
261 GNUNET_assert (size >= m->size);
266 if (delay.rel_value_us > GNUNET_TIME_UNIT_SECONDS.rel_value_us)
267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268 "Copied message of type %u and size %u into transport buffer for `%s' with delay of %s\n",
270 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
272 GNUNET_i2s (&n->peer),
273 GNUNET_STRINGS_relative_time_to_string (delay,
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Copied message of type %u and size %u into transport buffer for `%s' with delay of %s\n",
279 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
281 GNUNET_i2s (&n->peer),
282 GNUNET_STRINGS_relative_time_to_string (delay,
285 n->has_excess_bandwidth = GNUNET_NO;
287 GNUNET_STATISTICS_update (GSC_stats,
289 ("# encrypted bytes given to transport"), ret,
296 * Check if we have messages for the specified neighbour pending, and
297 * if so, check with the transport about sending them out.
299 * @param n neighbour to check.
302 process_queue (struct Neighbour *n)
304 struct NeighbourMessageEntry *m;
307 return; /* request already pending */
311 /* notify sessions that the queue is empty and more messages
312 * could thus be queued now */
313 GSC_SESSIONS_solicit (&n->peer);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Asking transport for transmission of %u bytes to `%s' in next %s\n",
318 (unsigned int) m->size,
319 GNUNET_i2s (&n->peer),
320 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline),
322 m->submission_time = GNUNET_TIME_absolute_get ();
324 = GNUNET_TRANSPORT_notify_transmit_ready (transport,
327 GNUNET_TIME_absolute_get_remaining (m->deadline),
332 /* message request too large or duplicate request */
334 /* discard encrypted message */
335 GNUNET_CONTAINER_DLL_remove (n->message_head,
345 * Function called by transport to notify us that
346 * a peer connected to us (on the network level).
349 * @param peer the peer that connected
352 handle_transport_notify_connect (void *cls,
353 const struct GNUNET_PeerIdentity *peer)
357 if (0 == memcmp (peer,
359 sizeof (struct GNUNET_PeerIdentity)))
364 n = find_neighbour (peer);
367 /* duplicate connect notification!? */
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Received connection from `%s'.\n",
374 n = GNUNET_new (struct Neighbour);
376 GNUNET_assert (GNUNET_OK ==
377 GNUNET_CONTAINER_multipeermap_put (neighbours,
380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
381 GNUNET_STATISTICS_set (GSC_stats,
382 gettext_noop ("# neighbour entries allocated"),
383 GNUNET_CONTAINER_multipeermap_size (neighbours),
385 n->kxinfo = GSC_KX_start (peer);
390 * Function called by transport telling us that a peer
394 * @param peer the peer that disconnected
397 handle_transport_notify_disconnect (void *cls,
398 const struct GNUNET_PeerIdentity *peer)
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Peer `%s' disconnected from us; received notification from transport.\n",
405 n = find_neighbour (peer);
416 * Function called by the transport for each received message.
419 * @param peer (claimed) identity of the other peer
420 * @param message the message
423 handle_transport_receive (void *cls,
424 const struct GNUNET_PeerIdentity *peer,
425 const struct GNUNET_MessageHeader *message)
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Received message of type %u from `%s', demultiplexing.\n",
432 (unsigned int) ntohs (message->type),
434 if (0 == memcmp (peer,
436 sizeof (struct GNUNET_PeerIdentity)))
441 n = find_neighbour (peer);
444 /* received message from peer that is not connected!? */
448 type = ntohs (message->type);
451 case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
452 GSC_KX_handle_ephemeral_key (n->kxinfo, message);
454 case GNUNET_MESSAGE_TYPE_CORE_PING:
455 GSC_KX_handle_ping (n->kxinfo, message);
457 case GNUNET_MESSAGE_TYPE_CORE_PONG:
458 GSC_KX_handle_pong (n->kxinfo, message);
460 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
461 GSC_KX_handle_encrypted_message (n->kxinfo, message);
463 case GNUNET_MESSAGE_TYPE_DUMMY:
464 /* Dummy messages for testing / benchmarking, just discard */
467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468 _("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
470 (unsigned int) ntohs (message->size),
478 * Transmit the given message to the given target.
480 * @param target peer that should receive the message (must be connected)
481 * @param msg message to transmit
482 * @param timeout by when should the transmission be done?
485 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
486 const struct GNUNET_MessageHeader *msg,
487 struct GNUNET_TIME_Relative timeout)
489 struct NeighbourMessageEntry *me;
493 n = find_neighbour (target);
499 msize = ntohs (msg->size);
500 me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
501 me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
506 GNUNET_CONTAINER_DLL_insert_tail (n->message_head,
515 * One of our neighbours has excess bandwidth,
519 * @param pid identity of the peer with excess bandwidth
522 handle_transport_notify_excess_bw (void *cls,
523 const struct GNUNET_PeerIdentity *pid)
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Peer %s has excess bandwidth available\n",
530 n = find_neighbour (pid);
536 n->has_excess_bandwidth = GNUNET_YES;
537 GSC_SESSIONS_solicit (pid);
542 * Check how many messages are queued for the given neighbour.
544 * @param target neighbour to check
545 * @return number of items in the message queue
548 GSC_NEIGHBOURS_get_queue_size (const struct GNUNET_PeerIdentity *target)
552 n = find_neighbour (target);
558 return n->queue_size;
563 * Check if the given neighbour has excess bandwidth available.
565 * @param target neighbour to check
566 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
569 GSC_NEIGHBOURS_check_excess_bandwidth (const struct GNUNET_PeerIdentity *target)
573 n = find_neighbour (target);
577 return GNUNET_SYSERR;
579 return n->has_excess_bandwidth;
584 * Initialize neighbours subsystem.
587 GSC_NEIGHBOURS_init ()
589 neighbours = GNUNET_CONTAINER_multipeermap_create (128,
592 GNUNET_TRANSPORT_connect2 (GSC_cfg,
595 &handle_transport_receive,
596 &handle_transport_notify_connect,
597 &handle_transport_notify_disconnect,
598 &handle_transport_notify_excess_bw);
599 if (NULL == transport)
601 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
603 return GNUNET_SYSERR;
610 * Wrapper around #free_neighbour().
613 * @param key peer identity
614 * @param value the `struct Neighbour` to free
615 * @return #GNUNET_OK (continue to iterate)
618 free_neighbour_helper (void *cls,
619 const struct GNUNET_PeerIdentity * key,
622 struct Neighbour *n = value;
624 /* transport should have 'disconnected' all neighbours... */
632 * Shutdown neighbours subsystem.
635 GSC_NEIGHBOURS_done ()
637 if (NULL != transport)
639 GNUNET_TRANSPORT_disconnect (transport);
642 if (NULL != neighbours)
644 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
645 &free_neighbour_helper,
647 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
652 /* end of gnunet-service-core_neighbours.c */