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 * Message ready for encryption. This struct is followed by the
37 * actual content of the message.
39 struct SessionMessageEntry
43 * We keep messages in a doubly linked list.
45 struct SessionMessageEntry *next;
48 * We keep messages in a doubly linked list.
50 struct SessionMessageEntry *prev;
53 * Deadline for transmission, 1s after we received it (if we
54 * are not corking), otherwise "now". Note that this message
55 * does NOT expire past its deadline.
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 session.
75 * Identity of the other peer.
77 struct GNUNET_PeerIdentity peer;
80 * Head of list of requests from clients for transmission to
83 struct GSC_ClientActiveRequest *active_client_request_head;
86 * Tail of list of requests from clients for transmission to
89 struct GSC_ClientActiveRequest *active_client_request_tail;
92 * Head of list of messages ready for encryption.
94 struct SessionMessageEntry *sme_head;
97 * Tail of list of messages ready for encryption.
99 struct SessionMessageEntry *sme_tail;
102 * Information about the key exchange with the other peer.
104 struct GSC_KeyExchangeInfo *kxinfo;
107 * Current type map for this peer.
109 struct GSC_TypeMap *tmap;
112 * At what time did we initially establish this session?
113 * (currently unused, should be integrated with ATS in the
116 struct GNUNET_TIME_Absolute time_established;
119 * Task to transmit corked messages with a delay.
121 GNUNET_SCHEDULER_TaskIdentifier cork_task;
124 * Is the neighbour queue empty and thus ready for us
125 * to transmit an encrypted message?
127 int ready_to_transmit;
133 * Map of peer identities to 'struct Session'.
135 static struct GNUNET_CONTAINER_MultiHashMap *sessions;
139 * Find the session for the given peer.
141 * @param peer identity of the peer
142 * @return NULL if we are not connected, otherwise the
145 static struct Session *
146 find_session (const struct GNUNET_PeerIdentity *peer)
148 return GNUNET_CONTAINER_multihashmap_get (sessions, &peer->hashPubKey);
153 * End the session with the given peer (we are no longer
156 * @param pid identity of peer to kill session with
159 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
161 struct Session *session;
162 struct GSC_ClientActiveRequest *car;
164 session = find_session (pid);
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Destroying session for peer `%4s'\n",
170 GNUNET_i2s (&session->peer));
172 if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
174 GNUNET_SCHEDULER_cancel (session->cork_task);
175 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
177 while (NULL != (car = session->active_client_request_head))
179 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
180 session->active_client_request_tail,
182 GSC_CLIENTS_reject_request (car);
184 GNUNET_assert (GNUNET_YES ==
185 GNUNET_CONTAINER_multihashmap_remove (sessions,
186 &session->peer.hashPubKey, session));
187 GNUNET_STATISTICS_set (GSC_stats,
188 gettext_noop ("# established sessions"),
189 GNUNET_CONTAINER_multihashmap_size (sessions),
191 if (NULL != session->tmap)
193 GSC_TYPEMAP_destroy (session->tmap);
194 session->tmap = NULL;
196 GNUNET_free (session);
201 * Create a session, a key exchange was just completed.
203 * @param peer peer that is now connected
204 * @param kx key exchange that completed
207 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
208 struct GSC_KeyExchangeInfo *kx)
210 struct GNUNET_MessageHeader *hdr;
211 struct Session *session;
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Creating session for peer `%4s'\n", GNUNET_i2s (peer));
217 session = GNUNET_malloc (sizeof (struct Session));
218 session->peer = *peer;
219 session->kxinfo = kx;
220 session->time_established = GNUNET_TIME_absolute_get ();
221 GNUNET_assert (GNUNET_OK ==
222 GNUNET_CONTAINER_multihashmap_put (sessions,
223 &peer->hashPubKey, session,
224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
225 GNUNET_STATISTICS_update (GSC_stats,
226 gettext_noop ("# established sessions"),
227 GNUNET_CONTAINER_multihashmap_size (sessions),
229 /* FIXME: we should probably do this periodically (in case
230 type map message is lost...) */
231 hdr = GSC_TYPEMAP_compute_type_map_message ();
232 GSC_KX_encrypt_and_transmit (kx,
240 * Notify the given client about the session (client is new).
242 * @param cls the 'struct GSC_Client'
243 * @param key peer identity
244 * @param value the 'struct Session'
245 * @return GNUNET_OK (continue to iterate)
248 notify_client_about_session (void *cls,
249 const GNUNET_HashCode *key,
252 struct GSC_Client *client = cls;
253 struct Session *session = value;
255 GSC_CLIENTS_notify_client_about_neighbour (client,
257 NULL, 0, /* FIXME: ATS!? */
258 NULL, /* old TMAP: none */
265 * We have a new client, notify it about all current sessions.
267 * @param client the new client
270 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
272 /* notify new client about existing sessions */
273 GNUNET_CONTAINER_multihashmap_iterate (sessions,
274 ¬ify_client_about_session, client);
279 * Try to perform a transmission on the given session. Will solicit
280 * additional messages if the 'sme' queue is not full enough.
282 * @param session session to transmit messages from
285 try_transmission (struct Session *session);
289 * Queue a request from a client for transmission to a particular peer.
291 * @param car request to queue; this handle is then shared between
292 * the caller (CLIENTS subsystem) and SESSIONS and must not
293 * be released by either until either 'GNUNET_SESSIONS_dequeue',
294 * 'GNUNET_SESSIONS_transmit' or 'GNUNET_CLIENTS_failed'
295 * have been invoked on it
298 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
300 struct Session *session;
302 session = find_session (&car->target);
305 /* neighbour must have disconnected since request was issued,
306 * ignore (client will realize it once it processes the
307 * disconnect notification) */
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Dropped client request for transmission (am disconnected)\n");
312 GNUNET_STATISTICS_update (GSC_stats,
314 ("# send requests dropped (disconnected)"), 1,
316 GSC_CLIENTS_reject_request (car);
319 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
322 GSC_CLIENTS_reject_request (car);
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Received client transmission request. queueing\n");
329 GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
330 session->active_client_request_tail, car);
331 try_transmission (session);
336 * Dequeue a request from a client from transmission to a particular peer.
338 * @param car request to dequeue; this handle will then be 'owned' by
339 * the caller (CLIENTS sysbsystem)
342 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
346 if (0 == memcmp (&car->target,
348 sizeof (struct GNUNET_PeerIdentity)))
350 s = find_session (&car->target);
351 GNUNET_assert (NULL != s);
352 GNUNET_CONTAINER_DLL_remove (s->active_client_request_head,
353 s->active_client_request_tail, car);
358 * Discard all expired active transmission requests from clients.
360 * @param session session to clean up
363 discard_expired_requests (struct Session *session)
365 struct GSC_ClientActiveRequest *pos;
366 struct GSC_ClientActiveRequest *nxt;
367 struct GNUNET_TIME_Absolute now;
369 now = GNUNET_TIME_absolute_get ();
371 nxt = session->active_client_request_head;
376 if ( (pos->deadline.abs_value < now.abs_value) &&
377 (GNUNET_YES != pos->was_solicited) )
379 GNUNET_STATISTICS_update (GSC_stats,
381 ("# messages discarded (expired prior to transmission)"),
383 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
384 session->active_client_request_tail,
386 GSC_CLIENTS_reject_request (pos);
393 * Solicit messages for transmission.
395 * @param session session to solict messages for
398 solicit_messages (struct Session *session)
400 struct GSC_ClientActiveRequest *car;
403 discard_expired_requests (session);
405 for (car = session->active_client_request_head; NULL != car; car = car->next)
407 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
409 so_size += car->msize;
410 if (car->was_solicited == GNUNET_YES)
412 car->was_solicited = GNUNET_YES;
413 GSC_CLIENTS_solicit_request (car);
419 * Some messages were delayed (corked), but the timeout has now expired.
422 * @param cls 'struct Session' with the messages to transmit now
423 * @param tc scheduler context (unused)
426 pop_cork_task (void *cls,
427 const struct GNUNET_SCHEDULER_TaskContext *tc)
429 struct Session *session = cls;
431 session->cork_task = GNUNET_SCHEDULER_NO_TASK;
432 try_transmission (session);
437 * Try to perform a transmission on the given session. Will solicit
438 * additional messages if the 'sme' queue is not full enough.
440 * @param session session to transmit messages from
443 try_transmission (struct Session *session)
445 struct SessionMessageEntry *pos;
447 struct GNUNET_TIME_Absolute now;
448 struct GNUNET_TIME_Absolute min_deadline;
450 if (GNUNET_YES != session->ready_to_transmit)
453 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
454 /* check 'ready' messages */
455 pos = session->sme_head;
456 while ( (NULL != pos) &&
457 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) )
459 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
461 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
465 now = GNUNET_TIME_absolute_get ();
467 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
468 (min_deadline.abs_value > now.abs_value) ) )
470 /* not enough ready yet, try to solicit more */
471 solicit_messages (session);
474 /* if there is data to send, just not yet, make sure we do transmit
475 it once the deadline is reached */
476 if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
477 GNUNET_SCHEDULER_cancel (session->cork_task);
478 session->cork_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
484 /* create plaintext buffer of all messages, encrypt and transmit */
486 static unsigned long long total_bytes;
487 static unsigned int total_msgs;
488 char pbuf[msize]; /* plaintext */
492 while ( (NULL != (pos = session->sme_head)) &&
493 (used + pos->size <= msize) )
495 memcpy (&pbuf[used], &pos[1], pos->size);
497 GNUNET_CONTAINER_DLL_remove (session->sme_head,
502 /* compute average payload size */
507 /* 2^32 messages, wrap around... */
511 GNUNET_STATISTICS_set (GSC_stats,
512 "# avg payload per encrypted message",
513 total_bytes / total_msgs,
515 /* now actually transmit... */
516 session->ready_to_transmit = GNUNET_NO;
517 GSC_KX_encrypt_and_transmit (session->kxinfo,
525 * Send a message to the neighbour now.
527 * @param cls the message
528 * @param key neighbour's identity
529 * @param value 'struct Neighbour' of the target
530 * @return always GNUNET_OK
533 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
535 const struct GNUNET_MessageHeader *hdr = cls;
536 struct Session *session = value;
537 struct SessionMessageEntry *m;
540 size = ntohs (hdr->size);
541 m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
542 memcpy (&m[1], hdr, size);
544 GNUNET_CONTAINER_DLL_insert (session->sme_head,
547 try_transmission (session);
553 * Broadcast a message to all neighbours.
555 * @param msg message to transmit
558 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
560 if (NULL == sessions)
562 GNUNET_CONTAINER_multihashmap_iterate (sessions,
563 &do_send_message, (void*) msg);
568 * Traffic is being solicited for the given peer. This means that the
569 * message queue on the transport-level (NEIGHBOURS subsystem) is now
570 * empty and it is now OK to transmit another (non-control) message.
572 * @param pid identity of peer ready to receive data
575 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
577 struct Session *session;
579 session = find_session (pid);
582 session->ready_to_transmit = GNUNET_YES;
583 try_transmission (session);
588 * Transmit a message to a particular peer.
590 * @param car original request that was queued and then solicited;
591 * this handle will now be 'owned' by the SESSIONS subsystem
592 * @param msg message to transmit
593 * @param cork is corking allowed?
596 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
597 const struct GNUNET_MessageHeader *msg,
600 struct Session *session;
601 struct SessionMessageEntry *sme;
604 session = find_session (&car->target);
605 msize = ntohs (msg->size);
606 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
607 memcpy (&sme[1], msg, msize);
609 if (GNUNET_YES == cork)
610 sme->deadline = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
611 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
614 try_transmission (session);
619 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
621 * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
622 * @param key identity of the connected peer
623 * @param value the 'struct Neighbour' for the peer
624 * @return GNUNET_OK (continue to iterate)
628 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
630 struct GNUNET_SERVER_TransmitContext *tc = cls;
631 struct Session *session = value;
632 struct ConnectNotifyMessage cnm;
633 struct GNUNET_TRANSPORT_ATS_Information *a;
635 /* FIXME: code duplication with clients... */
636 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
637 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
638 cnm.ats_count = htonl (0);
639 cnm.peer = session->peer;
641 // FIXME: full ats...
642 a[0].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
643 a[0].value = htonl (0);
644 GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
650 * Handle CORE_ITERATE_PEERS request. For this request type, the client
651 * does not have to have transmitted an INIT request. All current peers
652 * are returned, regardless of which message types they accept.
655 * @param client client sending the iteration request
656 * @param message iteration request message
659 GSC_SESSIONS_handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client *client,
660 const struct GNUNET_MessageHeader *message)
662 struct GNUNET_MessageHeader done_msg;
663 struct GNUNET_SERVER_TransmitContext *tc;
665 tc = GNUNET_SERVER_transmit_context_create (client);
666 GNUNET_CONTAINER_multihashmap_iterate (sessions,
667 &queue_connect_message,
669 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
670 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
671 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
672 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
677 * Handle CORE_PEER_CONNECTED request. Notify client about connection
678 * to the given neighbour. For this request type, the client does not
679 * have to have transmitted an INIT request. All current peers are
680 * returned, regardless of which message types they accept.
683 * @param client client sending the iteration request
684 * @param message iteration request message
687 GSC_SESSIONS_handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *client,
688 const struct GNUNET_MessageHeader *message)
690 struct GNUNET_MessageHeader done_msg;
691 struct GNUNET_SERVER_TransmitContext *tc;
692 const struct GNUNET_PeerIdentity *peer;
694 peer = (const struct GNUNET_PeerIdentity *) &message[1]; // YUCK!
695 tc = GNUNET_SERVER_transmit_context_create (client);
696 GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
697 &queue_connect_message, tc);
698 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
699 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
700 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
701 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
706 * We've received a typemap message from a peer, update ours.
707 * Notifies clients about the session.
709 * @param peer peer this is about
710 * @param msg typemap update message
713 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
714 const struct GNUNET_MessageHeader *msg)
716 struct Session *session;
717 struct GSC_TypeMap *nmap;
719 nmap = GSC_TYPEMAP_get_from_message (msg);
721 return; /* malformed */
722 session = find_session (peer);
728 GSC_CLIENTS_notify_clients_about_neighbour (peer,
729 NULL, 0, /* FIXME: ATS */
732 if (NULL != session->tmap)
733 GSC_TYPEMAP_destroy (session->tmap);
734 session->tmap = nmap;
739 * The given peer send a message of the specified type. Make sure the
740 * respective bit is set in its type-map and that clients are notified
743 * @param peer peer this is about
744 * @param type type of the message
747 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
750 struct Session *session;
751 struct GSC_TypeMap *nmap;
753 if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
755 session = find_session (peer);
756 GNUNET_assert (NULL != session);
758 GSC_TYPEMAP_test_match (session->tmap,
760 return; /* already in it */
761 nmap = GSC_TYPEMAP_extend (session->tmap,
763 GSC_CLIENTS_notify_clients_about_neighbour (peer,
764 NULL, 0, /* FIXME: ATS */
767 if (NULL != session->tmap)
768 GSC_TYPEMAP_destroy (session->tmap);
769 session->tmap = nmap;
774 * Initialize sessions subsystem.
779 sessions = GNUNET_CONTAINER_multihashmap_create (128);
784 * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
787 * @param key identity of the connected peer
788 * @param value the 'struct Session' for the peer
789 * @return GNUNET_OK (continue to iterate)
792 free_session_helper (void *cls, const GNUNET_HashCode * key, void *value)
794 struct Session *session = value;
796 GSC_SESSIONS_end (&session->peer);
802 * Shutdown sessions subsystem.
807 GNUNET_CONTAINER_multihashmap_iterate (sessions,
808 &free_session_helper,
810 GNUNET_CONTAINER_multihashmap_destroy (sessions);
812 GNUNET_STATISTICS_set (GSC_stats,
813 gettext_noop ("# established sessions"),
817 /* end of gnunet-service-core_sessions.c */