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 return GNUNET_CONTAINER_multihashmap_get (neighbours, &peer->hashPubKey);
137 * Free the given entry for the neighbour.
139 * @param n neighbour to free
142 free_neighbour (struct Neighbour *n)
144 struct NeighbourMessageEntry *m;
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Destroying neighbour entry for peer `%4s'\n",
149 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 GSC_SESSIONS_end (&n->peer);
162 if (NULL != n->kxinfo)
164 GSC_KX_stop (n->kxinfo);
167 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
169 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
170 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
172 GNUNET_assert (GNUNET_OK ==
173 GNUNET_CONTAINER_multihashmap_remove (neighbours,
174 &n->peer.hashPubKey, n));
175 GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# neighbour entries allocated"),
176 GNUNET_CONTAINER_multihashmap_size (neighbours),
183 * Check if we have encrypted messages for the specified neighbour
184 * pending, and if so, check with the transport about sending them
187 * @param n neighbour to check.
190 process_queue (struct Neighbour *n);
194 * Function called when the transport service is ready to receive a
195 * message for the respective peer
197 * @param cls neighbour to use message from
198 * @param size number of bytes we can transmit
199 * @param buf where to copy the message
200 * @return number of bytes transmitted
203 transmit_ready (void *cls, size_t size, void *buf)
205 struct Neighbour *n = cls;
206 struct NeighbourMessageEntry *m;
217 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Transmission of message of type %u and size %u failed\n",
224 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
225 (unsigned int) m->size);
233 GNUNET_assert (size >= m->size);
234 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));
245 GNUNET_STATISTICS_update (GSC_stats,
247 ("# encrypted bytes given to transport"), ret,
254 * Check if we have messages for the specified neighbour pending, and
255 * if so, check with the transport about sending them out.
257 * @param n neighbour to check.
260 process_queue (struct Neighbour *n)
262 struct NeighbourMessageEntry *m;
265 return; /* request already pending */
269 /* notify sessions that the queue is empty and more messages
270 could thus be queued now */
271 GSC_SESSIONS_solicit (&n->peer);
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
277 (unsigned int) m->size, GNUNET_i2s (&n->peer),
279 GNUNET_TIME_absolute_get_remaining (m->deadline).rel_value);
282 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size,
284 GNUNET_TIME_absolute_get_remaining
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
306 * @param ats performance data
307 * @param ats_count number of entries in ats (excluding 0-termination)
310 handle_transport_notify_connect (void *cls,
311 const struct GNUNET_PeerIdentity *peer,
312 const struct GNUNET_ATS_Information
313 *ats, uint32_t ats_count)
317 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
322 n = find_neighbour (peer);
325 /* duplicate connect notification!? */
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received connection from `%4s'.\n",
333 n = GNUNET_malloc (sizeof (struct Neighbour));
335 GNUNET_assert (GNUNET_OK ==
336 GNUNET_CONTAINER_multihashmap_put (neighbours,
337 &n->peer.hashPubKey, n,
338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
339 GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# neighbour entries allocated"),
340 GNUNET_CONTAINER_multihashmap_size (neighbours),
342 n->kxinfo = GSC_KX_start (peer);
347 * Function called by transport telling us that a peer
351 * @param peer the peer that disconnected
354 handle_transport_notify_disconnect (void *cls,
355 const struct GNUNET_PeerIdentity *peer)
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Peer `%4s' disconnected from us; received notification from transport.\n",
364 n = find_neighbour (peer);
375 * Function called by the transport for each received message.
378 * @param peer (claimed) identity of the other peer
379 * @param message the message
380 * @param ats performance data
381 * @param ats_count number of entries in ats (excluding 0-termination)
384 handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
385 const struct GNUNET_MessageHeader *message,
386 const struct GNUNET_ATS_Information *ats,
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Received message of type %u from `%4s', demultiplexing.\n",
395 (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
397 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
402 n = find_neighbour (peer);
405 /* received message from peer that is not connected!? */
409 type = ntohs (message->type);
412 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
413 GSC_KX_handle_set_key (n->kxinfo, message);
415 case GNUNET_MESSAGE_TYPE_CORE_PING:
416 GSC_KX_handle_ping (n->kxinfo, message);
418 case GNUNET_MESSAGE_TYPE_CORE_PONG:
419 GSC_KX_handle_pong (n->kxinfo, message);
421 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
422 GSC_KX_handle_encrypted_message (n->kxinfo,
427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
428 _("Unsupported message of type %u received.\n"),
429 (unsigned int) type);
436 * Transmit the given message to the given target.
438 * @param target peer that should receive the message (must be connected)
439 * @param msg message to transmit
440 * @param timeout by when should the transmission be done?
443 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
444 const struct GNUNET_MessageHeader *msg,
445 struct GNUNET_TIME_Relative timeout)
447 struct NeighbourMessageEntry *me;
451 n = find_neighbour (target);
457 msize = ntohs (msg->size);
458 me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
459 me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
461 memcpy (&me[1], msg, msize);
462 GNUNET_CONTAINER_DLL_insert_tail (n->message_head,
470 * Initialize neighbours subsystem.
473 GSC_NEIGHBOURS_init ()
475 neighbours = GNUNET_CONTAINER_multihashmap_create (128);
477 GNUNET_TRANSPORT_connect (GSC_cfg,
478 &GSC_my_identity, NULL,
479 &handle_transport_receive,
480 &handle_transport_notify_connect,
481 &handle_transport_notify_disconnect);
482 if (NULL == transport)
484 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
486 return GNUNET_SYSERR;
493 * Wrapper around 'free_neighbour'.
496 * @param key peer identity
497 * @param value the 'struct Neighbour' to free
498 * @return GNUNET_OK (continue to iterate)
501 free_neighbour_helper (void *cls, const GNUNET_HashCode * key, void *value)
503 struct Neighbour *n = value;
505 /* transport should have 'disconnected' all neighbours... */
513 * Shutdown neighbours subsystem.
516 GSC_NEIGHBOURS_done ()
518 if (NULL == transport)
520 GNUNET_TRANSPORT_disconnect (transport);
522 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &free_neighbour_helper,
524 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
528 /* end of gnunet-service-core_neighbours.c */