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_sessions.c
23 * @brief code for managing of 'encrypted' sessions (key exchange done)
24 * @author Christian Grothoff
27 #include "gnunet-service-core.h"
28 #include "gnunet-service-core_neighbours.h"
29 #include "gnunet-service-core_kx.h"
30 #include "gnunet-service-core_typemap.h"
31 #include "gnunet-service-core_sessions.h"
32 #include "gnunet-service-core_clients.h"
33 #include "gnunet_constants.h"
37 * Message ready for encryption. This struct is followed by the
38 * actual content of the message.
40 struct SessionMessageEntry
44 * We keep messages in a doubly linked list.
46 struct SessionMessageEntry *next;
49 * We keep messages in a doubly linked list.
51 struct SessionMessageEntry *prev;
54 * Deadline for transmission, 1s after we received it (if we
55 * are not corking), otherwise "now". Note that this message
56 * does NOT expire past its deadline.
58 struct GNUNET_TIME_Absolute deadline;
61 * How long is the message? (number of bytes following the "struct
62 * MessageEntry", but not including the size of "struct
63 * MessageEntry" itself!)
71 * Data kept per session.
76 * Identity of the other peer.
78 struct GNUNET_PeerIdentity peer;
81 * Head of list of requests from clients for transmission to
84 struct GSC_ClientActiveRequest *active_client_request_head;
87 * Tail of list of requests from clients for transmission to
90 struct GSC_ClientActiveRequest *active_client_request_tail;
93 * Head of list of messages ready for encryption.
95 struct SessionMessageEntry *sme_head;
98 * Tail of list of messages ready for encryption.
100 struct SessionMessageEntry *sme_tail;
103 * Information about the key exchange with the other peer.
105 struct GSC_KeyExchangeInfo *kxinfo;
108 * Current type map for this peer.
110 struct GSC_TypeMap *tmap;
113 * At what time did we initially establish this session?
114 * (currently unused, should be integrated with ATS in the
117 struct GNUNET_TIME_Absolute time_established;
120 * Task to transmit corked messages with a delay.
122 GNUNET_SCHEDULER_TaskIdentifier cork_task;
125 * Is the neighbour queue empty and thus ready for us
126 * to transmit an encrypted message?
128 int ready_to_transmit;
134 * Map of peer identities to 'struct Session'.
136 static struct GNUNET_CONTAINER_MultiHashMap *sessions;
140 * Find the session for the given peer.
142 * @param peer identity of the peer
143 * @return NULL if we are not connected, otherwise the
146 static struct Session *
147 find_session (const struct GNUNET_PeerIdentity *peer)
149 return GNUNET_CONTAINER_multihashmap_get (sessions, &peer->hashPubKey);
154 * End the session with the given peer (we are no longer
157 * @param pid identity of peer to kill session with
160 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
162 struct Session *session;
163 struct GSC_ClientActiveRequest *car;
165 session = find_session (pid);
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Destroying session for peer `%4s'\n",
171 GNUNET_i2s (&session->peer));
173 if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
175 GNUNET_SCHEDULER_cancel (session->cork_task);
176 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
178 GNUNET_assert (GNUNET_YES ==
179 GNUNET_CONTAINER_multihashmap_remove (sessions,
180 &session->peer.hashPubKey, session));
181 while (NULL != (car = session->active_client_request_head))
183 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
184 session->active_client_request_tail,
186 GSC_CLIENTS_reject_request (car);
188 GNUNET_STATISTICS_set (GSC_stats,
189 gettext_noop ("# established sessions"),
190 GNUNET_CONTAINER_multihashmap_size (sessions),
192 GNUNET_free (session);
197 * Create a session, a key exchange was just completed.
199 * @param peer peer that is now connected
200 * @param kx key exchange that completed
203 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
204 struct GSC_KeyExchangeInfo *kx)
206 struct GNUNET_MessageHeader *hdr;
207 struct Session *session;
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Creating session for peer `%4s'\n", GNUNET_i2s (pid));
213 session = GNUNET_malloc (sizeof (struct Session));
214 session->peer = *peer;
215 session->kxinfo = kx;
216 session->time_established = GNUNET_TIME_absolute_get ();
217 GNUNET_assert (GNUNET_OK ==
218 GNUNET_CONTAINER_multihashmap_put (sessions,
219 &peer->hashPubKey, session,
220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
221 GNUNET_STATISTICS_update (GSC_stats,
222 gettext_noop ("# established sessions"),
223 GNUNET_CONTAINER_multihashmap_size (sessions),
225 /* FIXME: we should probably do this periodically (in case
226 type map message is lost...) */
227 hdr = GSC_TYPEMAP_compute_type_map_message ();
228 GSC_KX_encrypt_and_transmit (kx,
236 * Notify the given client about the session (client is new).
238 * @param cls the 'struct GSC_Client'
239 * @param key peer identity
240 * @param value the 'struct Session'
241 * @return GNUNET_OK (continue to iterate)
244 notify_client_about_session (void *cls,
245 const GNUNET_HashCode *key,
248 struct GSC_Client *client = cls;
249 struct Session *session = value;
251 GDS_CLIENTS_notify_client_about_neighbour (client,
253 NULL, 0, /* FIXME: ATS!? */
254 NULL, /* old TMAP: none */
261 * We have a new client, notify it about all current sessions.
263 * @param client the new client
266 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
268 /* notify new client about existing sessions */
269 GNUNET_CONTAINER_multihashmap_iterate (sessions,
270 ¬ify_client_about_session, client);
275 * Try to perform a transmission on the given session. Will solicit
276 * additional messages if the 'sme' queue is not full enough.
278 * @param session session to transmit messages from
281 try_transmission (struct Session *session);
285 * Queue a request from a client for transmission to a particular peer.
287 * @param car request to queue; this handle is then shared between
288 * the caller (CLIENTS subsystem) and SESSIONS and must not
289 * be released by either until either 'GNUNET_SESSIONS_dequeue',
290 * 'GNUNET_SESSIONS_transmit' or 'GNUNET_CLIENTS_failed'
291 * have been invoked on it
294 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
296 struct Session *session;
298 session = find_session (&car->target);
301 /* neighbour must have disconnected since request was issued,
302 * ignore (client will realize it once it processes the
303 * disconnect notification) */
304 #if DEBUG_CORE_CLIENT
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Dropped client request for transmission (am disconnected)\n");
308 GNUNET_STATISTICS_update (GSC_stats,
310 ("# send requests dropped (disconnected)"), 1,
312 GSC_CLIENTS_reject_request (car);
315 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
318 GSC_CLIENTS_reject_request (car);
321 #if DEBUG_CORE_CLIENT
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Received client transmission request. queueing\n");
325 GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
326 session->active_client_request_tail, car);
327 try_transmission (session);
332 * Dequeue a request from a client from transmission to a particular peer.
334 * @param car request to dequeue; this handle will then be 'owned' by
335 * the caller (CLIENTS sysbsystem)
338 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
342 s = find_session (&car->target);
343 GNUNET_CONTAINER_DLL_remove (s->active_client_request_head,
344 s->active_client_request_tail, car);
349 * Discard all expired active transmission requests from clients.
351 * @param session session to clean up
354 discard_expired_requests (struct Session *session)
356 struct GSC_ClientActiveRequest *pos;
357 struct GSC_ClientActiveRequest *nxt;
358 struct GNUNET_TIME_Absolute now;
360 now = GNUNET_TIME_absolute_get ();
362 nxt = session->active_client_request_head;
367 if ( (pos->deadline.abs_value < now.abs_value) &&
368 (GNUNET_YES != pos->was_solicited) )
370 GNUNET_STATISTICS_update (GSC_stats,
372 ("# messages discarded (expired prior to transmission)"),
374 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
375 session->active_client_request_tail,
377 GSC_CLIENTS_reject_request (pos);
384 * Solicit messages for transmission.
386 * @param session session to solict messages for
389 solicit_messages (struct Session *session)
391 struct GSC_ClientActiveRequest *car;
394 discard_expired_requests (session);
396 for (car = session->active_client_request_head; NULL != car; car = car->next)
398 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
400 so_size += car->msize;
401 if (car->was_solicited == GNUNET_YES)
403 car->was_solicited = GNUNET_YES;
404 GSC_CLIENTS_solicit_request (car);
410 * Some messages were delayed (corked), but the timeout has now expired.
413 * @param cls 'struct Session' with the messages to transmit now
414 * @param tc scheduler context (unused)
417 pop_cork_task (void *cls,
418 const struct GNUNET_SCHEDULER_TaskContext *tc)
420 struct Session *session = session;
422 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
423 try_transmission (session);
428 * Try to perform a transmission on the given session. Will solicit
429 * additional messages if the 'sme' queue is not full enough.
431 * @param session session to transmit messages from
434 try_transmission (struct Session *session)
436 struct SessionMessageEntry *pos;
438 struct GNUNET_TIME_Absolute now;
439 struct GNUNET_TIME_Absolute min_deadline;
441 if (GNUNET_YES != session->ready_to_transmit)
444 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
445 /* check 'ready' messages */
446 pos = session->sme_head;
447 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
448 while ( (NULL != pos) &&
449 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) )
452 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
456 now = GNUNET_TIME_absolute_get ();
458 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
459 (min_deadline.abs_value > now.abs_value) ) )
461 /* not enough ready yet, try to solicit more */
462 solicit_messages (session);
465 /* if there is data to send, just not yet, make sure we do transmit
466 it once the deadline is reached */
467 if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
468 GNUNET_SCHEDULER_cancel (session->cork_task);
469 session->cork_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
475 /* create plaintext buffer of all messages, encrypt and transmit */
477 static unsigned long long total_bytes;
478 static unsigned int total_msgs;
479 char pbuf[msize]; /* plaintext */
483 pos = session->sme_head;
484 while ( (NULL != pos) &&
485 (used + pos->size <= msize) )
487 memcpy (&pbuf[used], &pos[1], pos->size);
490 /* compute average payload size */
495 /* 2^32 messages, wrap around... */
499 GNUNET_STATISTICS_set (GSC_stats,
500 "# avg payload per encrypted message",
501 total_bytes / total_msgs,
503 /* now actually transmit... */
504 session->ready_to_transmit = GNUNET_NO;
505 GSC_KX_encrypt_and_transmit (session->kxinfo,
513 * Send a message to the neighbour now.
515 * @param cls the message
516 * @param key neighbour's identity
517 * @param value 'struct Neighbour' of the target
518 * @return always GNUNET_OK
521 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
523 const struct GNUNET_MessageHeader *hdr = cls;
524 struct Session *session = value;
525 struct SessionMessageEntry *m;
528 size = ntohs (hdr->size);
529 m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
530 memcpy (&m[1], hdr, size);
532 GNUNET_CONTAINER_DLL_insert (session->sme_head,
535 try_transmission (session);
541 * Broadcast a message to all neighbours.
543 * @param msg message to transmit
546 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
548 if (NULL == sessions)
550 GNUNET_CONTAINER_multihashmap_iterate (sessions,
551 &do_send_message, (void*) msg);
556 * Traffic is being solicited for the given peer. This means that the
557 * message queue on the transport-level (NEIGHBOURS subsystem) is now
558 * empty and it is now OK to transmit another (non-control) message.
560 * @param pid identity of peer ready to receive data
563 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
565 struct Session *session;
567 session = find_session (pid);
568 session->ready_to_transmit = GNUNET_YES;
569 try_transmission (session);
574 * Transmit a message to a particular peer.
576 * @param car original request that was queued and then solicited;
577 * this handle will now be 'owned' by the SESSIONS subsystem
578 * @param msg message to transmit
579 * @param cork is corking allowed?
582 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
583 const struct GNUNET_MessageHeader *msg,
586 struct Session *session;
587 struct SessionMessageEntry *sme;
590 session = find_session (&car->target);
591 msize = ntohs (msg->size);
592 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
593 memcpy (&sme[1], msg, msize);
595 if (GNUNET_YES == cork)
596 sme->deadline = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
597 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
600 try_transmission (session);
605 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
607 * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
608 * @param key identity of the connected peer
609 * @param value the 'struct Neighbour' for the peer
610 * @return GNUNET_OK (continue to iterate)
614 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
616 struct GNUNET_SERVER_TransmitContext *tc = cls;
617 struct Session *session = value;
618 struct ConnectNotifyMessage cnm;
619 struct GNUNET_TRANSPORT_ATS_Information *a;
621 /* FIXME: code duplication with clients... */
622 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
623 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
624 cnm.ats_count = htonl (0);
625 cnm.peer = session->peer;
627 // FIXME: full ats...
628 a[0].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
629 a[0].value = htonl (0);
630 GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
636 * Handle CORE_ITERATE_PEERS request. For this request type, the client
637 * does not have to have transmitted an INIT request. All current peers
638 * are returned, regardless of which message types they accept.
641 * @param client client sending the iteration request
642 * @param message iteration request message
645 GSC_SESSIONS_handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client *client,
646 const struct GNUNET_MessageHeader *message)
648 struct GNUNET_MessageHeader done_msg;
649 struct GNUNET_SERVER_TransmitContext *tc;
651 tc = GNUNET_SERVER_transmit_context_create (client);
652 GNUNET_CONTAINER_multihashmap_iterate (sessions,
653 &queue_connect_message,
655 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
656 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
657 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
658 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
663 * Handle CORE_PEER_CONNECTED request. Notify client about connection
664 * to the given neighbour. For this request type, the client does not
665 * have to have transmitted an INIT request. All current peers are
666 * returned, regardless of which message types they accept.
669 * @param client client sending the iteration request
670 * @param message iteration request message
673 GSC_SESSIONS_handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *client,
674 const struct GNUNET_MessageHeader *message)
676 struct GNUNET_MessageHeader done_msg;
677 struct GNUNET_SERVER_TransmitContext *tc;
678 const struct GNUNET_PeerIdentity *peer;
680 peer = (const struct GNUNET_PeerIdentity *) &message[1]; // YUCK!
681 tc = GNUNET_SERVER_transmit_context_create (client);
682 GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
683 &queue_connect_message, tc);
684 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
685 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
686 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
687 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
692 * Initialize sessions subsystem.
697 sessions = GNUNET_CONTAINER_multihashmap_create (128);
702 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
705 * @param key identity of the connected peer
706 * @param value the 'struct Session' for the peer
707 * @return GNUNET_OK (continue to iterate)
710 free_session_helper (void *cls, const GNUNET_HashCode * key, void *value)
712 struct Session *session = value;
714 GSC_SESSIONS_end (&session->peer);
720 * Shutdown sessions subsystem.
725 GNUNET_CONTAINER_multihashmap_iterate (sessions,
726 &free_session_helper,
728 GNUNET_CONTAINER_multihashmap_destroy (sessions);
730 GNUNET_STATISTICS_set (GSC_stats,
731 gettext_noop ("# established sessions"),
735 /* end of gnunet-service-core_sessions.c */