2 This file is part of GNUnet.
3 (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 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
109 * #GNUNET_YES if this peer currently has excess bandwidth.
111 int has_excess_bandwidth;
117 * Map of peer identities to 'struct Neighbour'.
119 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
124 static struct GNUNET_TRANSPORT_Handle *transport;
128 * Find the entry for the given neighbour.
130 * @param peer identity of the neighbour
131 * @return NULL if we are not connected, otherwise the
134 static struct Neighbour *
135 find_neighbour (const struct GNUNET_PeerIdentity *peer)
137 if (NULL == neighbours)
139 return GNUNET_CONTAINER_multipeermap_get (neighbours, peer);
144 * Free the given entry for the neighbour.
146 * @param n neighbour to free
149 free_neighbour (struct Neighbour *n)
151 struct NeighbourMessageEntry *m;
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Destroying neighbour entry for peer `%4s'\n",
155 GNUNET_i2s (&n->peer));
156 while (NULL != (m = n->message_head))
158 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
163 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
166 GNUNET_STATISTICS_update (GSC_stats,
168 ("# sessions terminated by transport disconnect"),
170 if (NULL != n->kxinfo)
172 GSC_KX_stop (n->kxinfo);
175 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
177 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
178 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
180 GNUNET_assert (GNUNET_OK ==
181 GNUNET_CONTAINER_multipeermap_remove (neighbours,
183 GNUNET_STATISTICS_set (GSC_stats,
184 gettext_noop ("# neighbour entries allocated"),
185 GNUNET_CONTAINER_multipeermap_size (neighbours),
192 * Check if we have encrypted messages for the specified neighbour
193 * pending, and if so, check with the transport about sending them
196 * @param n neighbour to check.
199 process_queue (struct Neighbour *n);
203 * Function called when the transport service is ready to receive a
204 * message for the respective peer
206 * @param cls neighbour to use message from
207 * @param size number of bytes we can transmit
208 * @param buf where to copy the message
209 * @return number of bytes transmitted
212 transmit_ready (void *cls, size_t size, void *buf)
214 struct Neighbour *n = cls;
215 struct NeighbourMessageEntry *m;
226 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Transmission of message of type %u and size %u failed\n",
232 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
233 (unsigned int) m->size);
239 GNUNET_assert (size >= m->size);
240 memcpy (cbuf, &m[1], m->size);
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
245 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
246 (unsigned int) ret, GNUNET_i2s (&n->peer));
248 n->has_excess_bandwidth = GNUNET_NO;
250 GNUNET_STATISTICS_update (GSC_stats,
252 ("# encrypted bytes given to transport"), ret,
259 * Check if we have messages for the specified neighbour pending, and
260 * if so, check with the transport about sending them out.
262 * @param n neighbour to check.
265 process_queue (struct Neighbour *n)
267 struct NeighbourMessageEntry *m;
270 return; /* request already pending */
274 /* notify sessions that the queue is empty and more messages
275 * could thus be queued now */
276 GSC_SESSIONS_solicit (&n->peer);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Asking transport for transmission of %u bytes to `%4s' in next %s\n",
281 (unsigned int) m->size, GNUNET_i2s (&n->peer),
282 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline), GNUNET_NO));
284 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size,
285 GNUNET_TIME_absolute_get_remaining
286 (m->deadline), &transmit_ready,
290 /* message request too large or duplicate request */
292 /* discard encrypted message */
293 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
301 * Function called by transport to notify us that
302 * a peer connected to us (on the network level).
305 * @param peer the peer that connected
308 handle_transport_notify_connect (void *cls,
309 const struct GNUNET_PeerIdentity *peer)
313 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
318 n = find_neighbour (peer);
321 /* duplicate connect notification!? */
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Received connection from `%4s'.\n",
328 n = GNUNET_new (struct Neighbour);
330 GNUNET_assert (GNUNET_OK ==
331 GNUNET_CONTAINER_multipeermap_put (neighbours,
333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
334 GNUNET_STATISTICS_set (GSC_stats,
335 gettext_noop ("# neighbour entries allocated"),
336 GNUNET_CONTAINER_multipeermap_size (neighbours),
338 n->kxinfo = GSC_KX_start (peer);
343 * Function called by transport telling us that a peer
347 * @param peer the peer that disconnected
350 handle_transport_notify_disconnect (void *cls,
351 const struct GNUNET_PeerIdentity *peer)
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Peer `%4s' disconnected from us; received notification from transport.\n",
358 n = find_neighbour (peer);
369 * Function called by the transport for each received message.
372 * @param peer (claimed) identity of the other peer
373 * @param message the message
376 handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_MessageHeader *message)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Received message of type %u from `%4s', demultiplexing.\n",
384 (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
385 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
390 n = find_neighbour (peer);
393 /* received message from peer that is not connected!? */
397 type = ntohs (message->type);
400 case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
401 GSC_KX_handle_ephemeral_key (n->kxinfo, message);
403 case GNUNET_MESSAGE_TYPE_CORE_PING:
404 GSC_KX_handle_ping (n->kxinfo, message);
406 case GNUNET_MESSAGE_TYPE_CORE_PONG:
407 GSC_KX_handle_pong (n->kxinfo, message);
409 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
410 GSC_KX_handle_encrypted_message (n->kxinfo, message);
412 case GNUNET_MESSAGE_TYPE_DUMMY:
413 /* Dummy messages for testing / benchmarking, just discard */
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
418 ("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
419 (unsigned int) type, (unsigned int) ntohs (message->size),
427 * Transmit the given message to the given target.
429 * @param target peer that should receive the message (must be connected)
430 * @param msg message to transmit
431 * @param timeout by when should the transmission be done?
434 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
435 const struct GNUNET_MessageHeader *msg,
436 struct GNUNET_TIME_Relative timeout)
438 struct NeighbourMessageEntry *me;
442 n = find_neighbour (target);
448 msize = ntohs (msg->size);
449 me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
450 me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
452 memcpy (&me[1], msg, msize);
453 GNUNET_CONTAINER_DLL_insert_tail (n->message_head, n->message_tail, me);
459 * One of our neighbours has excess bandwidth,
463 * @param pid identity of the peer with excess bandwidth
466 handle_transport_notify_excess_bw (void *cls,
467 const struct GNUNET_PeerIdentity *pid)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Peer %s has excess bandwidth available\n",
474 n = find_neighbour (pid);
480 n->has_excess_bandwidth = GNUNET_YES;
481 GSC_SESSIONS_solicit (pid);
486 * Check if the given neighbour has excess bandwidth available.
488 * @param target neighbour to check
489 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
492 GSC_NEIGHBOURS_check_excess_bandwidth (const struct GNUNET_PeerIdentity *target)
496 n = find_neighbour (target);
500 return GNUNET_SYSERR;
502 return n->has_excess_bandwidth;
507 * Initialize neighbours subsystem.
510 GSC_NEIGHBOURS_init ()
512 neighbours = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
514 GNUNET_TRANSPORT_connect2 (GSC_cfg, &GSC_my_identity, NULL,
515 &handle_transport_receive,
516 &handle_transport_notify_connect,
517 &handle_transport_notify_disconnect,
518 &handle_transport_notify_excess_bw);
519 if (NULL == transport)
521 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
523 return GNUNET_SYSERR;
530 * Wrapper around 'free_neighbour'.
533 * @param key peer identity
534 * @param value the `struct Neighbour` to free
535 * @return #GNUNET_OK (continue to iterate)
538 free_neighbour_helper (void *cls,
539 const struct GNUNET_PeerIdentity * key,
542 struct Neighbour *n = value;
544 /* transport should have 'disconnected' all neighbours... */
552 * Shutdown neighbours subsystem.
555 GSC_NEIGHBOURS_done ()
557 if (NULL != transport)
559 GNUNET_TRANSPORT_disconnect (transport);
562 if (NULL != neighbours)
564 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &free_neighbour_helper,
566 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
571 /* end of gnunet-service-core_neighbours.c */