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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * How long is the message? (number of bytes following the "struct
61 * MessageEntry", but not including the size of "struct
62 * MessageEntry" itself!)
70 * Data kept per transport-connected peer.
76 * Head of the batched message queue (already ordered, transmit
77 * starting with the head).
79 struct NeighbourMessageEntry *message_head;
82 * Tail of the batched message queue (already ordered, append new
85 struct NeighbourMessageEntry *message_tail;
88 * Handle for pending requests for transmission to this peer
89 * with the transport service. NULL if no request is pending.
91 struct GNUNET_TRANSPORT_TransmitHandle *th;
94 * Information about the key exchange with the other peer.
96 struct GSC_KeyExchangeInfo *kxinfo;
99 * Identity of the other peer.
101 struct GNUNET_PeerIdentity peer;
104 * ID of task used for re-trying plaintext scheduling.
106 struct GNUNET_SCHEDULER_Task *retry_plaintext_task;
109 * How many messages are in the queue for this neighbour?
111 unsigned int queue_size;
114 * #GNUNET_YES if this peer currently has excess bandwidth.
116 int has_excess_bandwidth;
122 * Map of peer identities to 'struct Neighbour'.
124 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
129 static struct GNUNET_TRANSPORT_Handle *transport;
133 * Find the entry for the given neighbour.
135 * @param peer identity of the neighbour
136 * @return NULL if we are not connected, otherwise the
139 static struct Neighbour *
140 find_neighbour (const struct GNUNET_PeerIdentity *peer)
142 if (NULL == neighbours)
144 return GNUNET_CONTAINER_multipeermap_get (neighbours, peer);
149 * Free the given entry for the neighbour.
151 * @param n neighbour to free
154 free_neighbour (struct Neighbour *n)
156 struct NeighbourMessageEntry *m;
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Destroying neighbour entry for peer `%4s'\n",
160 GNUNET_i2s (&n->peer));
161 while (NULL != (m = n->message_head))
163 GNUNET_CONTAINER_DLL_remove (n->message_head,
169 GNUNET_assert (0 == n->queue_size);
172 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
175 GNUNET_STATISTICS_update (GSC_stats,
177 ("# sessions terminated by transport disconnect"),
179 if (NULL != n->kxinfo)
181 GSC_KX_stop (n->kxinfo);
184 if (NULL != n->retry_plaintext_task)
186 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
187 n->retry_plaintext_task = NULL;
189 GNUNET_assert (GNUNET_OK ==
190 GNUNET_CONTAINER_multipeermap_remove (neighbours,
192 GNUNET_STATISTICS_set (GSC_stats,
193 gettext_noop ("# neighbour entries allocated"),
194 GNUNET_CONTAINER_multipeermap_size (neighbours),
201 * Check if we have encrypted messages for the specified neighbour
202 * pending, and if so, check with the transport about sending them
205 * @param n neighbour to check.
208 process_queue (struct Neighbour *n);
212 * Function called when the transport service is ready to receive a
213 * message for the respective peer
215 * @param cls neighbour to use message from
216 * @param size number of bytes we can transmit
217 * @param buf where to copy the message
218 * @return number of bytes transmitted
221 transmit_ready (void *cls, size_t size, void *buf)
223 struct Neighbour *n = cls;
224 struct NeighbourMessageEntry *m;
235 GNUNET_CONTAINER_DLL_remove (n->message_head,
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Transmission of message of type %u and size %u failed\n",
244 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
245 (unsigned int) m->size);
251 GNUNET_assert (size >= m->size);
252 memcpy (cbuf, &m[1], m->size);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
257 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
258 (unsigned int) ret, GNUNET_i2s (&n->peer));
260 n->has_excess_bandwidth = GNUNET_NO;
262 GNUNET_STATISTICS_update (GSC_stats,
264 ("# encrypted bytes given to transport"), ret,
271 * Check if we have messages for the specified neighbour pending, and
272 * if so, check with the transport about sending them out.
274 * @param n neighbour to check.
277 process_queue (struct Neighbour *n)
279 struct NeighbourMessageEntry *m;
282 return; /* request already pending */
286 /* notify sessions that the queue is empty and more messages
287 * could thus be queued now */
288 GSC_SESSIONS_solicit (&n->peer);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Asking transport for transmission of %u bytes to `%4s' in next %s\n",
293 (unsigned int) m->size,
294 GNUNET_i2s (&n->peer),
295 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline),
298 = GNUNET_TRANSPORT_notify_transmit_ready (transport,
301 GNUNET_TIME_absolute_get_remaining (m->deadline),
306 /* message request too large or duplicate request */
308 /* discard encrypted message */
309 GNUNET_CONTAINER_DLL_remove (n->message_head,
319 * Function called by transport to notify us that
320 * a peer connected to us (on the network level).
323 * @param peer the peer that connected
326 handle_transport_notify_connect (void *cls,
327 const struct GNUNET_PeerIdentity *peer)
331 if (0 == memcmp (peer,
333 sizeof (struct GNUNET_PeerIdentity)))
338 n = find_neighbour (peer);
341 /* duplicate connect notification!? */
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Received connection from `%4s'.\n",
348 n = GNUNET_new (struct Neighbour);
350 GNUNET_assert (GNUNET_OK ==
351 GNUNET_CONTAINER_multipeermap_put (neighbours,
353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
354 GNUNET_STATISTICS_set (GSC_stats,
355 gettext_noop ("# neighbour entries allocated"),
356 GNUNET_CONTAINER_multipeermap_size (neighbours),
358 n->kxinfo = GSC_KX_start (peer);
363 * Function called by transport telling us that a peer
367 * @param peer the peer that disconnected
370 handle_transport_notify_disconnect (void *cls,
371 const struct GNUNET_PeerIdentity *peer)
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Peer `%4s' disconnected from us; received notification from transport.\n",
378 n = find_neighbour (peer);
389 * Function called by the transport for each received message.
392 * @param peer (claimed) identity of the other peer
393 * @param message the message
396 handle_transport_receive (void *cls,
397 const struct GNUNET_PeerIdentity *peer,
398 const struct GNUNET_MessageHeader *message)
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Received message of type %u from `%4s', demultiplexing.\n",
405 (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
406 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
411 n = find_neighbour (peer);
414 /* received message from peer that is not connected!? */
418 type = ntohs (message->type);
421 case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
422 GSC_KX_handle_ephemeral_key (n->kxinfo, message);
424 case GNUNET_MESSAGE_TYPE_CORE_PING:
425 GSC_KX_handle_ping (n->kxinfo, message);
427 case GNUNET_MESSAGE_TYPE_CORE_PONG:
428 GSC_KX_handle_pong (n->kxinfo, message);
430 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
431 GSC_KX_handle_encrypted_message (n->kxinfo, message);
433 case GNUNET_MESSAGE_TYPE_DUMMY:
434 /* Dummy messages for testing / benchmarking, just discard */
437 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
438 _("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
440 (unsigned int) ntohs (message->size),
448 * Transmit the given message to the given target.
450 * @param target peer that should receive the message (must be connected)
451 * @param msg message to transmit
452 * @param timeout by when should the transmission be done?
455 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
456 const struct GNUNET_MessageHeader *msg,
457 struct GNUNET_TIME_Relative timeout)
459 struct NeighbourMessageEntry *me;
463 n = find_neighbour (target);
469 msize = ntohs (msg->size);
470 me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
471 me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
476 GNUNET_CONTAINER_DLL_insert_tail (n->message_head,
485 * One of our neighbours has excess bandwidth,
489 * @param pid identity of the peer with excess bandwidth
492 handle_transport_notify_excess_bw (void *cls,
493 const struct GNUNET_PeerIdentity *pid)
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Peer %s has excess bandwidth available\n",
500 n = find_neighbour (pid);
506 n->has_excess_bandwidth = GNUNET_YES;
507 GSC_SESSIONS_solicit (pid);
512 * Check how many messages are queued for the given neighbour.
514 * @param target neighbour to check
515 * @return number of items in the message queue
518 GSC_NEIGHBOURS_get_queue_size (const struct GNUNET_PeerIdentity *target)
522 n = find_neighbour (target);
528 return n->queue_size;
533 * Check if the given neighbour has excess bandwidth available.
535 * @param target neighbour to check
536 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
539 GSC_NEIGHBOURS_check_excess_bandwidth (const struct GNUNET_PeerIdentity *target)
543 n = find_neighbour (target);
547 return GNUNET_SYSERR;
549 return n->has_excess_bandwidth;
554 * Initialize neighbours subsystem.
557 GSC_NEIGHBOURS_init ()
559 neighbours = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
561 GNUNET_TRANSPORT_connect2 (GSC_cfg, &GSC_my_identity, NULL,
562 &handle_transport_receive,
563 &handle_transport_notify_connect,
564 &handle_transport_notify_disconnect,
565 &handle_transport_notify_excess_bw);
566 if (NULL == transport)
568 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
570 return GNUNET_SYSERR;
577 * Wrapper around 'free_neighbour'.
580 * @param key peer identity
581 * @param value the `struct Neighbour` to free
582 * @return #GNUNET_OK (continue to iterate)
585 free_neighbour_helper (void *cls,
586 const struct GNUNET_PeerIdentity * key,
589 struct Neighbour *n = value;
591 /* transport should have 'disconnected' all neighbours... */
599 * Shutdown neighbours subsystem.
602 GSC_NEIGHBOURS_done ()
604 if (NULL != transport)
606 GNUNET_TRANSPORT_disconnect (transport);
609 if (NULL != neighbours)
611 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
612 &free_neighbour_helper,
614 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
619 /* end of gnunet-service-core_neighbours.c */