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;
112 * Map of peer identities to 'struct Neighbour'.
114 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
119 static struct GNUNET_TRANSPORT_Handle *transport;
123 * Find the entry for the given neighbour.
125 * @param peer identity of the neighbour
126 * @return NULL if we are not connected, otherwise the
129 static struct Neighbour *
130 find_neighbour (const struct GNUNET_PeerIdentity *peer)
132 if (NULL == neighbours)
134 return GNUNET_CONTAINER_multihashmap_get (neighbours, &peer->hashPubKey);
139 * Free the given entry for the neighbour.
141 * @param n neighbour to free
144 free_neighbour (struct Neighbour *n)
146 struct NeighbourMessageEntry *m;
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Destroying neighbour entry for peer `%4s'\n",
150 GNUNET_i2s (&n->peer));
151 while (NULL != (m = n->message_head))
153 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
158 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
161 GNUNET_STATISTICS_update (GSC_stats,
163 ("# sessions terminated by transport disconnect"),
165 if (NULL != n->kxinfo)
167 GSC_KX_stop (n->kxinfo);
170 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
172 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
173 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
175 GNUNET_assert (GNUNET_OK ==
176 GNUNET_CONTAINER_multihashmap_remove (neighbours,
177 &n->peer.hashPubKey, n));
178 GNUNET_STATISTICS_set (GSC_stats,
179 gettext_noop ("# neighbour entries allocated"),
180 GNUNET_CONTAINER_multihashmap_size (neighbours),
187 * Check if we have encrypted messages for the specified neighbour
188 * pending, and if so, check with the transport about sending them
191 * @param n neighbour to check.
194 process_queue (struct Neighbour *n);
198 * Function called when the transport service is ready to receive a
199 * message for the respective peer
201 * @param cls neighbour to use message from
202 * @param size number of bytes we can transmit
203 * @param buf where to copy the message
204 * @return number of bytes transmitted
207 transmit_ready (void *cls, size_t size, void *buf)
209 struct Neighbour *n = cls;
210 struct NeighbourMessageEntry *m;
221 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Transmission of message of type %u and size %u failed\n",
227 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
228 (unsigned int) m->size);
234 GNUNET_assert (size >= m->size);
235 memcpy (cbuf, &m[1], m->size);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
240 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
241 (unsigned int) ret, GNUNET_i2s (&n->peer));
244 GNUNET_STATISTICS_update (GSC_stats,
246 ("# encrypted bytes given to transport"), ret,
253 * Check if we have messages for the specified neighbour pending, and
254 * if so, check with the transport about sending them out.
256 * @param n neighbour to check.
259 process_queue (struct Neighbour *n)
261 struct NeighbourMessageEntry *m;
264 return; /* request already pending */
268 /* notify sessions that the queue is empty and more messages
269 * could thus be queued now */
270 GSC_SESSIONS_solicit (&n->peer);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Asking transport for transmission of %u bytes to `%4s' in next %s\n",
275 (unsigned int) m->size, GNUNET_i2s (&n->peer),
276 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline), GNUNET_NO));
278 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size, 0,
279 GNUNET_TIME_absolute_get_remaining
280 (m->deadline), &transmit_ready,
284 /* message request too large or duplicate request */
286 /* discard encrypted message */
287 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
295 * Function called by transport to notify us that
296 * a peer connected to us (on the network level).
299 * @param peer the peer that connected
300 * @param atsi performance data
301 * @param atsi_count number of entries in ats (excluding 0-termination)
304 handle_transport_notify_connect (void *cls,
305 const struct GNUNET_PeerIdentity *peer,
306 const struct GNUNET_ATS_Information *atsi,
311 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
316 n = find_neighbour (peer);
319 /* duplicate connect notification!? */
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received connection from `%4s'.\n",
325 n = GNUNET_malloc (sizeof (struct Neighbour));
327 GNUNET_assert (GNUNET_OK ==
328 GNUNET_CONTAINER_multihashmap_put (neighbours,
329 &n->peer.hashPubKey, n,
330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
331 GNUNET_STATISTICS_set (GSC_stats,
332 gettext_noop ("# neighbour entries allocated"),
333 GNUNET_CONTAINER_multihashmap_size (neighbours),
335 n->kxinfo = GSC_KX_start (peer);
340 * Function called by transport telling us that a peer
344 * @param peer the peer that disconnected
347 handle_transport_notify_disconnect (void *cls,
348 const struct GNUNET_PeerIdentity *peer)
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Peer `%4s' disconnected from us; received notification from transport.\n",
355 n = find_neighbour (peer);
366 * Function called by the transport for each received message.
369 * @param peer (claimed) identity of the other peer
370 * @param message the message
371 * @param atsi performance data
372 * @param atsi_count number of entries in ats (excluding 0-termination)
375 handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
376 const struct GNUNET_MessageHeader *message,
377 const struct GNUNET_ATS_Information *atsi,
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Received message of type %u from `%4s', demultiplexing.\n",
385 (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
386 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
391 n = find_neighbour (peer);
394 /* received message from peer that is not connected!? */
398 type = ntohs (message->type);
401 case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
402 GSC_KX_handle_ephemeral_key (n->kxinfo, message);
404 case GNUNET_MESSAGE_TYPE_CORE_PING:
405 GSC_KX_handle_ping (n->kxinfo, message);
407 case GNUNET_MESSAGE_TYPE_CORE_PONG:
408 GSC_KX_handle_pong (n->kxinfo, message);
410 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
411 GSC_KX_handle_encrypted_message (n->kxinfo, message, atsi, atsi_count);
413 case GNUNET_MESSAGE_TYPE_DUMMY:
414 /* Dummy messages for testing / benchmarking, just discard */
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
419 ("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
420 (unsigned int) type, (unsigned int) ntohs (message->size),
428 * Transmit the given message to the given target.
430 * @param target peer that should receive the message (must be connected)
431 * @param msg message to transmit
432 * @param timeout by when should the transmission be done?
435 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
436 const struct GNUNET_MessageHeader *msg,
437 struct GNUNET_TIME_Relative timeout)
439 struct NeighbourMessageEntry *me;
443 n = find_neighbour (target);
449 msize = ntohs (msg->size);
450 me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
451 me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
453 memcpy (&me[1], msg, msize);
454 GNUNET_CONTAINER_DLL_insert_tail (n->message_head, n->message_tail, me);
460 * Initialize neighbours subsystem.
463 GSC_NEIGHBOURS_init ()
465 neighbours = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
467 GNUNET_TRANSPORT_connect (GSC_cfg, &GSC_my_identity, NULL,
468 &handle_transport_receive,
469 &handle_transport_notify_connect,
470 &handle_transport_notify_disconnect);
471 if (NULL == transport)
473 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
475 return GNUNET_SYSERR;
482 * Wrapper around 'free_neighbour'.
485 * @param key peer identity
486 * @param value the 'struct Neighbour' to free
487 * @return GNUNET_OK (continue to iterate)
490 free_neighbour_helper (void *cls, const struct GNUNET_HashCode * key, void *value)
492 struct Neighbour *n = value;
494 /* transport should have 'disconnected' all neighbours... */
502 * Shutdown neighbours subsystem.
505 GSC_NEIGHBOURS_done ()
507 if (NULL != transport)
509 GNUNET_TRANSPORT_disconnect (transport);
512 if (NULL != neighbours)
514 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &free_neighbour_helper,
516 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
521 /* end of gnunet-service-core_neighbours.c */