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 GSC_TYPEMAP_destroy (session->tmap);
204 session->tmap = NULL;
205 GNUNET_free (session);
210 * Transmit our current typemap message to the other peer.
211 * (Done periodically in case an update got lost).
213 * @param cls the 'struct Session*'
217 transmit_typemap_task (void *cls,
218 const struct GNUNET_SCHEDULER_TaskContext *tc)
220 struct Session *session = cls;
221 struct GNUNET_MessageHeader *hdr;
222 struct GNUNET_TIME_Relative delay;
224 delay = TYPEMAP_FREQUENCY;
225 /* randomize a bit to avoid spont. sync */
226 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
228 session->typemap_task = GNUNET_SCHEDULER_add_delayed (delay,
229 &transmit_typemap_task,
231 GNUNET_STATISTICS_update (GSC_stats,
232 gettext_noop ("# type map refreshes sent"),
235 hdr = GSC_TYPEMAP_compute_type_map_message ();
236 GSC_KX_encrypt_and_transmit (session->kxinfo,
244 * Create a session, a key exchange was just completed.
246 * @param peer peer that is now connected
247 * @param kx key exchange that completed
250 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
251 struct GSC_KeyExchangeInfo *kx)
253 struct Session *session;
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257 "Creating session for peer `%4s'\n", GNUNET_i2s (peer));
259 session = GNUNET_malloc (sizeof (struct Session));
260 session->tmap = GSC_TYPEMAP_create ();
261 session->peer = *peer;
262 session->kxinfo = kx;
263 session->time_established = GNUNET_TIME_absolute_get ();
264 session->typemap_task = GNUNET_SCHEDULER_add_now (&transmit_typemap_task,
266 GNUNET_assert (GNUNET_OK ==
267 GNUNET_CONTAINER_multihashmap_put (sessions,
268 &peer->hashPubKey, session,
269 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
270 GNUNET_STATISTICS_set (GSC_stats,
271 gettext_noop ("# entries in session map"),
272 GNUNET_CONTAINER_multihashmap_size (sessions),
274 GSC_CLIENTS_notify_clients_about_neighbour (peer,
275 NULL, 0 /* FIXME: ATSI */,
282 * Notify the given client about the session (client is new).
284 * @param cls the 'struct GSC_Client'
285 * @param key peer identity
286 * @param value the 'struct Session'
287 * @return GNUNET_OK (continue to iterate)
290 notify_client_about_session (void *cls,
291 const GNUNET_HashCode *key,
294 struct GSC_Client *client = cls;
295 struct Session *session = value;
297 GSC_CLIENTS_notify_client_about_neighbour (client,
299 NULL, 0, /* FIXME: ATS!? */
300 NULL, /* old TMAP: none */
307 * We have a new client, notify it about all current sessions.
309 * @param client the new client
312 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
314 /* notify new client about existing sessions */
315 GNUNET_CONTAINER_multihashmap_iterate (sessions,
316 ¬ify_client_about_session, client);
321 * Try to perform a transmission on the given session. Will solicit
322 * additional messages if the 'sme' queue is not full enough.
324 * @param session session to transmit messages from
327 try_transmission (struct Session *session);
331 * Queue a request from a client for transmission to a particular peer.
333 * @param car request to queue; this handle is then shared between
334 * the caller (CLIENTS subsystem) and SESSIONS and must not
335 * be released by either until either 'GNUNET_SESSIONS_dequeue',
336 * 'GNUNET_SESSIONS_transmit' or 'GNUNET_CLIENTS_failed'
337 * have been invoked on it
340 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
342 struct Session *session;
344 session = find_session (&car->target);
347 /* neighbour must have disconnected since request was issued,
348 * ignore (client will realize it once it processes the
349 * disconnect notification) */
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Dropped client request for transmission (am disconnected)\n");
354 GNUNET_STATISTICS_update (GSC_stats,
356 ("# send requests dropped (disconnected)"), 1,
358 GSC_CLIENTS_reject_request (car);
361 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
364 GSC_CLIENTS_reject_request (car);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Received client transmission request. queueing\n");
371 GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
372 session->active_client_request_tail, car);
373 try_transmission (session);
378 * Dequeue a request from a client from transmission to a particular peer.
380 * @param car request to dequeue; this handle will then be 'owned' by
381 * the caller (CLIENTS sysbsystem)
384 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
388 if (0 == memcmp (&car->target,
390 sizeof (struct GNUNET_PeerIdentity)))
392 s = find_session (&car->target);
393 GNUNET_assert (NULL != s);
394 GNUNET_CONTAINER_DLL_remove (s->active_client_request_head,
395 s->active_client_request_tail, car);
400 * Discard all expired active transmission requests from clients.
402 * @param session session to clean up
405 discard_expired_requests (struct Session *session)
407 struct GSC_ClientActiveRequest *pos;
408 struct GSC_ClientActiveRequest *nxt;
409 struct GNUNET_TIME_Absolute now;
411 now = GNUNET_TIME_absolute_get ();
413 nxt = session->active_client_request_head;
418 if ( (pos->deadline.abs_value < now.abs_value) &&
419 (GNUNET_YES != pos->was_solicited) )
421 GNUNET_STATISTICS_update (GSC_stats,
423 ("# messages discarded (expired prior to transmission)"),
425 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
426 session->active_client_request_tail,
428 GSC_CLIENTS_reject_request (pos);
435 * Solicit messages for transmission.
437 * @param session session to solict messages for
440 solicit_messages (struct Session *session)
442 struct GSC_ClientActiveRequest *car;
445 discard_expired_requests (session);
447 for (car = session->active_client_request_head; NULL != car; car = car->next)
449 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
451 so_size += car->msize;
452 if (car->was_solicited == GNUNET_YES)
454 car->was_solicited = GNUNET_YES;
455 GSC_CLIENTS_solicit_request (car);
461 * Some messages were delayed (corked), but the timeout has now expired.
464 * @param cls 'struct Session' with the messages to transmit now
465 * @param tc scheduler context (unused)
468 pop_cork_task (void *cls,
469 const struct GNUNET_SCHEDULER_TaskContext *tc)
471 struct Session *session = cls;
473 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
474 try_transmission (session);
479 * Try to perform a transmission on the given session. Will solicit
480 * additional messages if the 'sme' queue is not full enough.
482 * @param session session to transmit messages from
485 try_transmission (struct Session *session)
487 struct SessionMessageEntry *pos;
489 struct GNUNET_TIME_Absolute now;
490 struct GNUNET_TIME_Absolute min_deadline;
492 if (GNUNET_YES != session->ready_to_transmit)
495 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
496 /* check 'ready' messages */
497 pos = session->sme_head;
498 while ( (NULL != pos) &&
499 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) )
501 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
503 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
507 now = GNUNET_TIME_absolute_get ();
509 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
510 (min_deadline.abs_value > now.abs_value) ) )
512 /* not enough ready yet, try to solicit more */
513 solicit_messages (session);
516 /* if there is data to send, just not yet, make sure we do transmit
517 it once the deadline is reached */
518 if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
519 GNUNET_SCHEDULER_cancel (session->cork_task);
520 session->cork_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
526 /* create plaintext buffer of all messages, encrypt and transmit */
528 static unsigned long long total_bytes;
529 static unsigned int total_msgs;
530 char pbuf[msize]; /* plaintext */
534 while ( (NULL != (pos = session->sme_head)) &&
535 (used + pos->size <= msize) )
537 memcpy (&pbuf[used], &pos[1], pos->size);
539 GNUNET_CONTAINER_DLL_remove (session->sme_head,
544 /* compute average payload size */
549 /* 2^32 messages, wrap around... */
553 GNUNET_STATISTICS_set (GSC_stats,
554 "# avg payload per encrypted message",
555 total_bytes / total_msgs,
557 /* now actually transmit... */
558 session->ready_to_transmit = GNUNET_NO;
559 GSC_KX_encrypt_and_transmit (session->kxinfo,
567 * Send a message to the neighbour now.
569 * @param cls the message
570 * @param key neighbour's identity
571 * @param value 'struct Neighbour' of the target
572 * @return always GNUNET_OK
575 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
577 const struct GNUNET_MessageHeader *hdr = cls;
578 struct Session *session = value;
579 struct SessionMessageEntry *m;
582 size = ntohs (hdr->size);
583 m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
584 memcpy (&m[1], hdr, size);
586 GNUNET_CONTAINER_DLL_insert (session->sme_head,
589 try_transmission (session);
595 * Broadcast a message to all neighbours.
597 * @param msg message to transmit
600 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
602 if (NULL == sessions)
604 GNUNET_CONTAINER_multihashmap_iterate (sessions,
605 &do_send_message, (void*) msg);
610 * Traffic is being solicited for the given peer. This means that the
611 * message queue on the transport-level (NEIGHBOURS subsystem) is now
612 * empty and it is now OK to transmit another (non-control) message.
614 * @param pid identity of peer ready to receive data
617 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
619 struct Session *session;
621 session = find_session (pid);
624 session->ready_to_transmit = GNUNET_YES;
625 try_transmission (session);
630 * Transmit a message to a particular peer.
632 * @param car original request that was queued and then solicited;
633 * this handle will now be 'owned' by the SESSIONS subsystem
634 * @param msg message to transmit
635 * @param cork is corking allowed?
638 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
639 const struct GNUNET_MessageHeader *msg,
642 struct Session *session;
643 struct SessionMessageEntry *sme;
646 session = find_session (&car->target);
649 msize = ntohs (msg->size);
650 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
651 memcpy (&sme[1], msg, msize);
653 if (GNUNET_YES == cork)
654 sme->deadline = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
655 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
658 try_transmission (session);
663 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
665 * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
666 * @param key identity of the connected peer
667 * @param value the 'struct Neighbour' for the peer
668 * @return GNUNET_OK (continue to iterate)
672 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
674 struct GNUNET_SERVER_TransmitContext *tc = cls;
675 struct Session *session = value;
676 struct ConnectNotifyMessage cnm;
678 /* FIXME: code duplication with clients... */
679 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
680 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
681 // FIXME: full ats...
682 cnm.ats_count = htonl (0);
683 cnm.peer = session->peer;
684 GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
690 * Handle CORE_ITERATE_PEERS request. For this request type, the client
691 * does not have to have transmitted an INIT request. All current peers
692 * are returned, regardless of which message types they accept.
695 * @param client client sending the iteration request
696 * @param message iteration request message
699 GSC_SESSIONS_handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client *client,
700 const struct GNUNET_MessageHeader *message)
702 struct GNUNET_MessageHeader done_msg;
703 struct GNUNET_SERVER_TransmitContext *tc;
705 tc = GNUNET_SERVER_transmit_context_create (client);
706 GNUNET_CONTAINER_multihashmap_iterate (sessions,
707 &queue_connect_message,
709 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
710 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
711 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
712 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
717 * Handle CORE_PEER_CONNECTED request. Notify client about connection
718 * to the given neighbour. For this request type, the client does not
719 * have to have transmitted an INIT request. All current peers are
720 * returned, regardless of which message types they accept.
723 * @param client client sending the iteration request
724 * @param message iteration request message
727 GSC_SESSIONS_handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *client,
728 const struct GNUNET_MessageHeader *message)
730 struct GNUNET_MessageHeader done_msg;
731 struct GNUNET_SERVER_TransmitContext *tc;
732 const struct GNUNET_PeerIdentity *peer;
734 peer = (const struct GNUNET_PeerIdentity *) &message[1]; // YUCK!
735 tc = GNUNET_SERVER_transmit_context_create (client);
736 GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
737 &queue_connect_message, tc);
738 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
739 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
740 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
741 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
746 * We've received a typemap message from a peer, update ours.
747 * Notifies clients about the session.
749 * @param peer peer this is about
750 * @param msg typemap update message
753 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
754 const struct GNUNET_MessageHeader *msg)
756 struct Session *session;
757 struct GSC_TypeMap *nmap;
759 nmap = GSC_TYPEMAP_get_from_message (msg);
761 return; /* malformed */
762 session = find_session (peer);
768 GSC_CLIENTS_notify_clients_about_neighbour (peer,
769 NULL, 0, /* FIXME: ATS */
772 GSC_TYPEMAP_destroy (session->tmap);
773 session->tmap = nmap;
778 * The given peer send a message of the specified type. Make sure the
779 * respective bit is set in its type-map and that clients are notified
782 * @param peer peer this is about
783 * @param type type of the message
786 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
789 struct Session *session;
790 struct GSC_TypeMap *nmap;
792 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
794 session = find_session (peer);
795 GNUNET_assert (NULL != session);
797 GSC_TYPEMAP_test_match (session->tmap,
799 return; /* already in it */
800 nmap = GSC_TYPEMAP_extend (session->tmap,
802 GSC_CLIENTS_notify_clients_about_neighbour (peer,
803 NULL, 0, /* FIXME: ATS */
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 */