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"
36 * How often do we transmit our typemap?
38 #define TYPEMAP_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
42 * Message ready for encryption. This struct is followed by the
43 * actual content of the message.
45 struct SessionMessageEntry
49 * We keep messages in a doubly linked list.
51 struct SessionMessageEntry *next;
54 * We keep messages in a doubly linked list.
56 struct SessionMessageEntry *prev;
59 * Deadline for transmission, 1s after we received it (if we
60 * are not corking), otherwise "now". Note that this message
61 * does NOT expire past its deadline.
63 struct GNUNET_TIME_Absolute deadline;
66 * How long is the message? (number of bytes following the "struct
67 * MessageEntry", but not including the size of "struct
68 * MessageEntry" itself!)
76 * Data kept per session.
81 * Identity of the other peer.
83 struct GNUNET_PeerIdentity peer;
86 * Head of list of requests from clients for transmission to
89 struct GSC_ClientActiveRequest *active_client_request_head;
92 * Tail of list of requests from clients for transmission to
95 struct GSC_ClientActiveRequest *active_client_request_tail;
98 * Head of list of messages ready for encryption.
100 struct SessionMessageEntry *sme_head;
103 * Tail of list of messages ready for encryption.
105 struct SessionMessageEntry *sme_tail;
108 * Information about the key exchange with the other peer.
110 struct GSC_KeyExchangeInfo *kxinfo;
113 * Current type map for this peer.
115 struct GSC_TypeMap *tmap;
118 * At what time did we initially establish this session?
119 * (currently unused, should be integrated with ATS in the
122 struct GNUNET_TIME_Absolute time_established;
125 * Task to transmit corked messages with a delay.
127 GNUNET_SCHEDULER_TaskIdentifier cork_task;
130 * Task to transmit our type map.
132 GNUNET_SCHEDULER_TaskIdentifier typemap_task;
135 * Is the neighbour queue empty and thus ready for us
136 * to transmit an encrypted message?
138 int ready_to_transmit;
144 * Map of peer identities to 'struct Session'.
146 static struct GNUNET_CONTAINER_MultiHashMap *sessions;
150 * Find the session for the given peer.
152 * @param peer identity of the peer
153 * @return NULL if we are not connected, otherwise the
156 static struct Session *
157 find_session (const struct GNUNET_PeerIdentity *peer)
159 return GNUNET_CONTAINER_multihashmap_get (sessions, &peer->hashPubKey);
164 * End the session with the given peer (we are no longer
167 * @param pid identity of peer to kill session with
170 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
172 struct Session *session;
173 struct GSC_ClientActiveRequest *car;
175 session = find_session (pid);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Destroying session for peer `%4s'\n",
181 GNUNET_i2s (&session->peer));
183 if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
185 GNUNET_SCHEDULER_cancel (session->cork_task);
186 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
188 while (NULL != (car = session->active_client_request_head))
190 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
191 session->active_client_request_tail,
193 GSC_CLIENTS_reject_request (car);
195 GNUNET_SCHEDULER_cancel (session->typemap_task);
196 GNUNET_assert (GNUNET_YES ==
197 GNUNET_CONTAINER_multihashmap_remove (sessions,
198 &session->peer.hashPubKey, session));
199 GNUNET_STATISTICS_set (GSC_stats,
200 gettext_noop ("# entries in session map"),
201 GNUNET_CONTAINER_multihashmap_size (sessions),
203 if (NULL != session->tmap)
205 GSC_TYPEMAP_destroy (session->tmap);
206 session->tmap = NULL;
208 GNUNET_free (session);
213 * Transmit our current typemap message to the other peer.
214 * (Done periodically in case an update got lost).
216 * @param cls the 'struct Session*'
220 transmit_typemap_task (void *cls,
221 const struct GNUNET_SCHEDULER_TaskContext *tc)
223 struct Session *session = cls;
224 struct GNUNET_MessageHeader *hdr;
225 struct GNUNET_TIME_Relative delay;
227 delay = TYPEMAP_FREQUENCY;
228 /* randomize a bit to avoid spont. sync */
229 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
231 session->typemap_task = GNUNET_SCHEDULER_add_delayed (delay,
232 &transmit_typemap_task,
234 GNUNET_STATISTICS_update (GSC_stats,
235 gettext_noop ("# type map refreshes sent"),
238 hdr = GSC_TYPEMAP_compute_type_map_message ();
239 GSC_KX_encrypt_and_transmit (session->kxinfo,
247 * Create a session, a key exchange was just completed.
249 * @param peer peer that is now connected
250 * @param kx key exchange that completed
253 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
254 struct GSC_KeyExchangeInfo *kx)
256 struct Session *session;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Creating session for peer `%4s'\n", GNUNET_i2s (peer));
262 session = GNUNET_malloc (sizeof (struct Session));
263 session->peer = *peer;
264 session->kxinfo = kx;
265 session->time_established = GNUNET_TIME_absolute_get ();
266 session->typemap_task = GNUNET_SCHEDULER_add_now (&transmit_typemap_task,
268 GNUNET_assert (GNUNET_OK ==
269 GNUNET_CONTAINER_multihashmap_put (sessions,
270 &peer->hashPubKey, session,
271 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
272 GNUNET_STATISTICS_set (GSC_stats,
273 gettext_noop ("# entries in session map"),
274 GNUNET_CONTAINER_multihashmap_size (sessions),
280 * Notify the given client about the session (client is new).
282 * @param cls the 'struct GSC_Client'
283 * @param key peer identity
284 * @param value the 'struct Session'
285 * @return GNUNET_OK (continue to iterate)
288 notify_client_about_session (void *cls,
289 const GNUNET_HashCode *key,
292 struct GSC_Client *client = cls;
293 struct Session *session = value;
295 GSC_CLIENTS_notify_client_about_neighbour (client,
297 NULL, 0, /* FIXME: ATS!? */
298 NULL, /* old TMAP: none */
305 * We have a new client, notify it about all current sessions.
307 * @param client the new client
310 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
312 /* notify new client about existing sessions */
313 GNUNET_CONTAINER_multihashmap_iterate (sessions,
314 ¬ify_client_about_session, client);
319 * Try to perform a transmission on the given session. Will solicit
320 * additional messages if the 'sme' queue is not full enough.
322 * @param session session to transmit messages from
325 try_transmission (struct Session *session);
329 * Queue a request from a client for transmission to a particular peer.
331 * @param car request to queue; this handle is then shared between
332 * the caller (CLIENTS subsystem) and SESSIONS and must not
333 * be released by either until either 'GNUNET_SESSIONS_dequeue',
334 * 'GNUNET_SESSIONS_transmit' or 'GNUNET_CLIENTS_failed'
335 * have been invoked on it
338 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
340 struct Session *session;
342 session = find_session (&car->target);
345 /* neighbour must have disconnected since request was issued,
346 * ignore (client will realize it once it processes the
347 * disconnect notification) */
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Dropped client request for transmission (am disconnected)\n");
352 GNUNET_STATISTICS_update (GSC_stats,
354 ("# send requests dropped (disconnected)"), 1,
356 GSC_CLIENTS_reject_request (car);
359 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
362 GSC_CLIENTS_reject_request (car);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Received client transmission request. queueing\n");
369 GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
370 session->active_client_request_tail, car);
371 try_transmission (session);
376 * Dequeue a request from a client from transmission to a particular peer.
378 * @param car request to dequeue; this handle will then be 'owned' by
379 * the caller (CLIENTS sysbsystem)
382 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
386 if (0 == memcmp (&car->target,
388 sizeof (struct GNUNET_PeerIdentity)))
390 s = find_session (&car->target);
391 GNUNET_assert (NULL != s);
392 GNUNET_CONTAINER_DLL_remove (s->active_client_request_head,
393 s->active_client_request_tail, car);
398 * Discard all expired active transmission requests from clients.
400 * @param session session to clean up
403 discard_expired_requests (struct Session *session)
405 struct GSC_ClientActiveRequest *pos;
406 struct GSC_ClientActiveRequest *nxt;
407 struct GNUNET_TIME_Absolute now;
409 now = GNUNET_TIME_absolute_get ();
411 nxt = session->active_client_request_head;
416 if ( (pos->deadline.abs_value < now.abs_value) &&
417 (GNUNET_YES != pos->was_solicited) )
419 GNUNET_STATISTICS_update (GSC_stats,
421 ("# messages discarded (expired prior to transmission)"),
423 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
424 session->active_client_request_tail,
426 GSC_CLIENTS_reject_request (pos);
433 * Solicit messages for transmission.
435 * @param session session to solict messages for
438 solicit_messages (struct Session *session)
440 struct GSC_ClientActiveRequest *car;
443 discard_expired_requests (session);
445 for (car = session->active_client_request_head; NULL != car; car = car->next)
447 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
449 so_size += car->msize;
450 if (car->was_solicited == GNUNET_YES)
452 car->was_solicited = GNUNET_YES;
453 GSC_CLIENTS_solicit_request (car);
459 * Some messages were delayed (corked), but the timeout has now expired.
462 * @param cls 'struct Session' with the messages to transmit now
463 * @param tc scheduler context (unused)
466 pop_cork_task (void *cls,
467 const struct GNUNET_SCHEDULER_TaskContext *tc)
469 struct Session *session = cls;
471 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
472 try_transmission (session);
477 * Try to perform a transmission on the given session. Will solicit
478 * additional messages if the 'sme' queue is not full enough.
480 * @param session session to transmit messages from
483 try_transmission (struct Session *session)
485 struct SessionMessageEntry *pos;
487 struct GNUNET_TIME_Absolute now;
488 struct GNUNET_TIME_Absolute min_deadline;
490 if (GNUNET_YES != session->ready_to_transmit)
493 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
494 /* check 'ready' messages */
495 pos = session->sme_head;
496 while ( (NULL != pos) &&
497 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) )
499 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
501 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
505 now = GNUNET_TIME_absolute_get ();
507 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
508 (min_deadline.abs_value > now.abs_value) ) )
510 /* not enough ready yet, try to solicit more */
511 solicit_messages (session);
514 /* if there is data to send, just not yet, make sure we do transmit
515 it once the deadline is reached */
516 if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
517 GNUNET_SCHEDULER_cancel (session->cork_task);
518 session->cork_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
524 /* create plaintext buffer of all messages, encrypt and transmit */
526 static unsigned long long total_bytes;
527 static unsigned int total_msgs;
528 char pbuf[msize]; /* plaintext */
532 while ( (NULL != (pos = session->sme_head)) &&
533 (used + pos->size <= msize) )
535 memcpy (&pbuf[used], &pos[1], pos->size);
537 GNUNET_CONTAINER_DLL_remove (session->sme_head,
542 /* compute average payload size */
547 /* 2^32 messages, wrap around... */
551 GNUNET_STATISTICS_set (GSC_stats,
552 "# avg payload per encrypted message",
553 total_bytes / total_msgs,
555 /* now actually transmit... */
556 session->ready_to_transmit = GNUNET_NO;
557 GSC_KX_encrypt_and_transmit (session->kxinfo,
565 * Send a message to the neighbour now.
567 * @param cls the message
568 * @param key neighbour's identity
569 * @param value 'struct Neighbour' of the target
570 * @return always GNUNET_OK
573 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
575 const struct GNUNET_MessageHeader *hdr = cls;
576 struct Session *session = value;
577 struct SessionMessageEntry *m;
580 size = ntohs (hdr->size);
581 m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
582 memcpy (&m[1], hdr, size);
584 GNUNET_CONTAINER_DLL_insert (session->sme_head,
587 try_transmission (session);
593 * Broadcast a message to all neighbours.
595 * @param msg message to transmit
598 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
600 if (NULL == sessions)
602 GNUNET_CONTAINER_multihashmap_iterate (sessions,
603 &do_send_message, (void*) msg);
608 * Traffic is being solicited for the given peer. This means that the
609 * message queue on the transport-level (NEIGHBOURS subsystem) is now
610 * empty and it is now OK to transmit another (non-control) message.
612 * @param pid identity of peer ready to receive data
615 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
617 struct Session *session;
619 session = find_session (pid);
622 session->ready_to_transmit = GNUNET_YES;
623 try_transmission (session);
628 * Transmit a message to a particular peer.
630 * @param car original request that was queued and then solicited;
631 * this handle will now be 'owned' by the SESSIONS subsystem
632 * @param msg message to transmit
633 * @param cork is corking allowed?
636 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
637 const struct GNUNET_MessageHeader *msg,
640 struct Session *session;
641 struct SessionMessageEntry *sme;
644 session = find_session (&car->target);
647 msize = ntohs (msg->size);
648 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
649 memcpy (&sme[1], msg, msize);
651 if (GNUNET_YES == cork)
652 sme->deadline = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
653 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
656 try_transmission (session);
661 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
663 * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
664 * @param key identity of the connected peer
665 * @param value the 'struct Neighbour' for the peer
666 * @return GNUNET_OK (continue to iterate)
670 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
672 struct GNUNET_SERVER_TransmitContext *tc = cls;
673 struct Session *session = value;
674 struct ConnectNotifyMessage cnm;
676 /* FIXME: code duplication with clients... */
677 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
678 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
679 // FIXME: full ats...
680 cnm.ats_count = htonl (0);
681 cnm.peer = session->peer;
682 GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
688 * Handle CORE_ITERATE_PEERS request. For this request type, the client
689 * does not have to have transmitted an INIT request. All current peers
690 * are returned, regardless of which message types they accept.
693 * @param client client sending the iteration request
694 * @param message iteration request message
697 GSC_SESSIONS_handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client *client,
698 const struct GNUNET_MessageHeader *message)
700 struct GNUNET_MessageHeader done_msg;
701 struct GNUNET_SERVER_TransmitContext *tc;
703 tc = GNUNET_SERVER_transmit_context_create (client);
704 GNUNET_CONTAINER_multihashmap_iterate (sessions,
705 &queue_connect_message,
707 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
708 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
709 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
710 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
715 * Handle CORE_PEER_CONNECTED request. Notify client about connection
716 * to the given neighbour. For this request type, the client does not
717 * have to have transmitted an INIT request. All current peers are
718 * returned, regardless of which message types they accept.
721 * @param client client sending the iteration request
722 * @param message iteration request message
725 GSC_SESSIONS_handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *client,
726 const struct GNUNET_MessageHeader *message)
728 struct GNUNET_MessageHeader done_msg;
729 struct GNUNET_SERVER_TransmitContext *tc;
730 const struct GNUNET_PeerIdentity *peer;
732 peer = (const struct GNUNET_PeerIdentity *) &message[1]; // YUCK!
733 tc = GNUNET_SERVER_transmit_context_create (client);
734 GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
735 &queue_connect_message, tc);
736 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
737 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
738 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
739 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
744 * We've received a typemap message from a peer, update ours.
745 * Notifies clients about the session.
747 * @param peer peer this is about
748 * @param msg typemap update message
751 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
752 const struct GNUNET_MessageHeader *msg)
754 struct Session *session;
755 struct GSC_TypeMap *nmap;
757 nmap = GSC_TYPEMAP_get_from_message (msg);
759 return; /* malformed */
760 session = find_session (peer);
766 GSC_CLIENTS_notify_clients_about_neighbour (peer,
767 NULL, 0, /* FIXME: ATS */
770 if (NULL != session->tmap)
771 GSC_TYPEMAP_destroy (session->tmap);
772 session->tmap = nmap;
777 * The given peer send a message of the specified type. Make sure the
778 * respective bit is set in its type-map and that clients are notified
781 * @param peer peer this is about
782 * @param type type of the message
785 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
788 struct Session *session;
789 struct GSC_TypeMap *nmap;
791 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
793 session = find_session (peer);
794 GNUNET_assert (NULL != session);
796 GSC_TYPEMAP_test_match (session->tmap,
798 return; /* already in it */
799 nmap = GSC_TYPEMAP_extend (session->tmap,
801 GSC_CLIENTS_notify_clients_about_neighbour (peer,
802 NULL, 0, /* FIXME: ATS */
805 if (NULL != session->tmap)
806 GSC_TYPEMAP_destroy (session->tmap);
807 session->tmap = nmap;
812 * Initialize sessions subsystem.
817 sessions = GNUNET_CONTAINER_multihashmap_create (128);
822 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
825 * @param key identity of the connected peer
826 * @param value the 'struct Session' for the peer
827 * @return GNUNET_OK (continue to iterate)
830 free_session_helper (void *cls, const GNUNET_HashCode * key, void *value)
832 struct Session *session = value;
834 GSC_SESSIONS_end (&session->peer);
840 * Shutdown sessions subsystem.
845 GNUNET_CONTAINER_multihashmap_iterate (sessions,
846 &free_session_helper,
848 GNUNET_CONTAINER_multihashmap_destroy (sessions);
852 /* end of gnunet-service-core_sessions.c */