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_transport_service.h"
29 #include "gnunet_service_core.h"
30 #include "gnunet_service_core-neighbours.h"
31 #include "gnunet_service_core-kx.h"
35 * Message ready for transmission via transport service. This struct
36 * is followed by the actual content of the message.
42 * We keep messages in a doubly linked list.
44 struct MessageEntry *next;
47 * We keep messages in a doubly linked list.
49 struct MessageEntry *prev;
52 * By when are we supposed to transmit this message?
54 struct GNUNET_TIME_Absolute deadline;
57 * How long is the message? (number of bytes following the "struct
58 * MessageEntry", but not including the size of "struct
59 * MessageEntry" itself!)
67 * Data kept per transport-connected peer.
73 * Head of the batched message queue (already ordered, transmit
74 * starting with the head).
76 struct MessageEntry *message_head;
79 * Tail of the batched message queue (already ordered, append new
82 struct MessageEntry *message_tail;
85 * Handle for pending requests for transmission to this peer
86 * with the transport service. NULL if no request is pending.
88 struct GNUNET_TRANSPORT_TransmitHandle *th;
91 * Information about the key exchange with the other peer.
93 struct GSC_KeyExchangeInfo *kxinfo;
96 * Identity of the other peer.
98 struct GNUNET_PeerIdentity peer;
101 * ID of task used for re-trying plaintext scheduling.
103 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
106 * Tracking bandwidth for sending to this peer.
108 struct GNUNET_BANDWIDTH_Tracker available_send_window;
111 * Tracking bandwidth for sending to this peer.
113 struct GNUNET_BANDWIDTH_Tracker available_recv_window;
120 * Map of peer identities to 'struct Neighbour'.
122 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
127 static struct GNUNET_TRANSPORT_Handle *transport;
132 * Find the entry for the given neighbour.
134 * @param peer identity of the neighbour
135 * @return NULL if we are not connected, otherwise the
138 static struct Neighbour *
139 find_neighbour (const struct GNUNET_PeerIdentity *peer)
141 return GNUNET_CONTAINER_multihashmap_get (neighbours, &peer->hashPubKey);
146 * Free the given entry for the neighbour.
148 * @param n neighbour to free
151 free_neighbour (struct Neighbour *n)
153 struct MessageEntry *m;
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Destroying neighbour entry for peer `%4s'\n",
158 GNUNET_i2s (&n->peer));
160 while (NULL != (m = n->message_head))
162 GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
167 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
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_multihashmap_remove (neighbours,
182 &n->peer.hashPubKey, n));
183 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"),
184 GNUNET_CONTAINER_multihashmap_size (neighbours),
191 * Check if we have encrypted messages for the specified neighbour
192 * pending, and if so, check with the transport about sending them
195 * @param n neighbour to check.
198 process_queue (struct Neighbour *n);
202 * Function called when the transport service is ready to receive a
203 * message for the respective peer
205 * @param cls neighbour to use message from
206 * @param size number of bytes we can transmit
207 * @param buf where to copy the message
208 * @return number of bytes transmitted
211 transmit_ready (void *cls, size_t size, void *buf)
213 struct Neighbour *n = cls;
214 struct MessageEntry *m;
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Encrypted message queue empty, no messages added to buffer for `%4s'\n",
225 GNUNET_i2s (&n->peer));
229 GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, m);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Transmission of message of type %u and size %u failed\n",
236 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
237 (unsigned int) m->size);
245 GNUNET_assert (size >= m->size);
246 memcpy (cbuf, &m[1], m->size);
248 GNUNET_BANDWIDTH_tracker_consume (&n->available_send_window, m->size);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
253 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
254 (unsigned int) ret, GNUNET_i2s (&n->peer));
258 GNUNET_STATISTICS_update (GSC_stats,
260 ("# encrypted bytes given to transport"), ret,
267 * Check if we have messages for the specified neighbour pending, and
268 * if so, check with the transport about sending them out.
270 * @param n neighbour to check.
273 process_queue (struct Neighbour *n)
275 struct MessageEntry *m;
278 return; /* request already pending */
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
285 (unsigned int) m->size, GNUNET_i2s (&n->peer),
287 GNUNET_TIME_absolute_get_remaining (m->deadline).rel_value);
290 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size,
292 GNUNET_TIME_absolute_get_remaining
298 /* message request too large or duplicate request */
300 /* discard encrypted message */
301 GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, m);
309 * Function called by transport to notify us that
310 * a peer connected to us (on the network level).
313 * @param peer the peer that connected
314 * @param ats performance data
315 * @param ats_count number of entries in ats (excluding 0-termination)
318 handle_transport_notify_connect (void *cls,
319 const struct GNUNET_PeerIdentity *peer,
320 const struct GNUNET_TRANSPORT_ATS_Information
321 *ats, uint32_t ats_count)
325 if (0 == memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
330 n = find_neighbour (peer);
333 /* duplicate connect notification!? */
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received connection from `%4s'.\n",
341 n = GNUNET_malloc (sizeof (struct Neighbour));
343 GNUNET_BANDWIDTH_tracker_init (&n->available_send_window,
344 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
346 GNUNET_BANDWIDTH_tracker_init (&n->available_recv_window,
347 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
349 GNUNET_assert (GNUNET_OK ==
350 GNUNET_CONTAINER_multihashmap_put (neighbours,
351 &n->peer.hashPubKey, n,
352 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
353 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"),
354 GNUNET_CONTAINER_multihashmap_size (neighbours),
356 GNUNET_TRANSPORT_set_quota (transport, peer,
357 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
358 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT);
359 n->kx = GSC_KX_start (pid);
364 * Function called by transport telling us that a peer
368 * @param peer the peer that disconnected
371 handle_transport_notify_disconnect (void *cls,
372 const struct GNUNET_PeerIdentity *peer)
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Peer `%4s' disconnected from us; received notification from transport.\n",
381 n = find_neighbour (peer);
392 * Function called by the transport for each received message.
395 * @param peer (claimed) identity of the other peer
396 * @param message the message
397 * @param ats performance data
398 * @param ats_count number of entries in ats (excluding 0-termination)
401 handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
402 const struct GNUNET_MessageHeader *message,
403 const struct GNUNET_TRANSPORT_ATS_Information *ats,
407 struct GNUNET_TIME_Absolute now;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Received message of type %u from `%4s', demultiplexing.\n",
416 (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
418 if (0 == memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
423 n = find_neighbour (peer);
426 /* received message from peer that is not connected!? */
433 up = (n->status == PEER_STATE_KEY_CONFIRMED);
434 type = ntohs (message->type);
435 size = ntohs (message->size);
438 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
439 if (size != sizeof (struct SetKeyMessage))
444 GNUNET_STATISTICS_update (stats, gettext_noop ("# session keys received"),
446 handle_set_key (n, (const struct SetKeyMessage *) message, ats, ats_count);
448 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
450 sizeof (struct EncryptedMessage) + sizeof (struct GNUNET_MessageHeader))
455 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
456 (n->status != PEER_STATE_KEY_CONFIRMED))
458 GNUNET_STATISTICS_update (stats,
460 ("# failed to decrypt message (no session key)"),
465 handle_encrypted_message (n, (const struct EncryptedMessage *) message, ats,
468 case GNUNET_MESSAGE_TYPE_CORE_PING:
469 if (size != sizeof (struct PingMessage))
474 GNUNET_STATISTICS_update (stats, gettext_noop ("# PING messages received"),
476 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
477 (n->status != PEER_STATE_KEY_CONFIRMED))
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
482 "PING", GNUNET_i2s (&n->peer));
484 GNUNET_free_non_null (n->pending_ping);
485 n->pending_ping = GNUNET_malloc (sizeof (struct PingMessage));
486 memcpy (n->pending_ping, message, sizeof (struct PingMessage));
489 handle_ping (n, (const struct PingMessage *) message, ats, ats_count);
491 case GNUNET_MESSAGE_TYPE_CORE_PONG:
492 if (size != sizeof (struct PongMessage))
497 GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages received"),
499 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
500 (n->status != PEER_STATE_KEY_CONFIRMED))
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
505 "PONG", GNUNET_i2s (&n->peer));
507 GNUNET_free_non_null (n->pending_pong);
508 n->pending_pong = GNUNET_malloc (sizeof (struct PongMessage));
509 memcpy (n->pending_pong, message, sizeof (struct PongMessage));
512 handle_pong (n, (const struct PongMessage *) message, ats, ats_count);
515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
516 _("Unsupported message of type %u received.\n"),
517 (unsigned int) type);
520 if (n->status == PEER_STATE_KEY_CONFIRMED)
522 now = GNUNET_TIME_absolute_get ();
523 n->last_activity = now;
524 changed = GNUNET_YES;
527 GNUNET_STATISTICS_update (stats, gettext_noop ("# established sessions"),
529 n->time_established = now;
531 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
532 GNUNET_SCHEDULER_cancel (n->keep_alive_task);
534 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
535 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
536 2), &send_keep_alive, n);
539 handle_peer_status_change (n);
544 * Transmit the given message to the given target.
546 * @param target peer that should receive the message (must be connected)
547 * @param msg message to transmit
548 * @param timeout by when should the transmission be done?
551 GDS_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
552 const struct GNUNET_MessageHeader *msg,
553 struct GNUNET_TIME_Relative timeout)
560 * Initialize neighbours subsystem.
563 GSC_NEIGHBOURS_init ()
565 neighbours = GNUNET_CONTAINER_multihashmap_create (128);
567 GNUNET_TRANSPORT_connect (GSC_cfg,
568 &GSC_my_identity, NULL,
569 &handle_transport_receive,
570 &handle_transport_notify_connect,
571 &handle_transport_notify_disconnect);
572 if (NULL == transport)
574 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
576 return GNUNET_SYSERR;
583 * Wrapper around 'free_neighbour'.
586 * @param key peer identity
587 * @param value the 'struct Neighbour' to free
588 * @return GNUNET_OK (continue to iterate)
591 free_neighbour_helper (void *cls, const GNUNET_HashCode * key, void *value)
593 struct Neighbour *n = value;
601 * Shutdown neighbours subsystem.
604 GSC_NEIGHBOURS_done ()
606 if (NULL == transport)
608 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &free_neighbour_helper,
610 GNUNET_TRANSPORT_disconnect (transport);
612 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
616 /* end of gnunet-service-core_neighbours.c */