2 This file is part of GNUnet.
3 Copyright (C) 2009-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
38 * How many encrypted messages do we queue at most?
39 * Needed to bound memory consumption.
41 #define MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE 4
45 * Message ready for encryption. This struct is followed by the
46 * actual content of the message.
48 struct SessionMessageEntry
52 * We keep messages in a doubly linked list.
54 struct SessionMessageEntry *next;
57 * We keep messages in a doubly linked list.
59 struct SessionMessageEntry *prev;
62 * Deadline for transmission, 1s after we received it (if we
63 * are not corking), otherwise "now". Note that this message
64 * does NOT expire past its deadline.
66 struct GNUNET_TIME_Absolute deadline;
69 * How long is the message? (number of bytes following the `struct
70 * MessageEntry`, but not including the size of `struct
71 * MessageEntry` itself!)
76 * How important is this message.
78 enum GNUNET_CORE_Priority priority;
84 * Data kept per session.
89 * Identity of the other peer.
91 struct GNUNET_PeerIdentity peer;
94 * Head of list of requests from clients for transmission to
97 struct GSC_ClientActiveRequest *active_client_request_head;
100 * Tail of list of requests from clients for transmission to
103 struct GSC_ClientActiveRequest *active_client_request_tail;
106 * Head of list of messages ready for encryption.
108 struct SessionMessageEntry *sme_head;
111 * Tail of list of messages ready for encryption.
113 struct SessionMessageEntry *sme_tail;
116 * Information about the key exchange with the other peer.
118 struct GSC_KeyExchangeInfo *kxinfo;
121 * Current type map for this peer.
123 struct GSC_TypeMap *tmap;
126 * Task to transmit corked messages with a delay.
128 struct GNUNET_SCHEDULER_Task *cork_task;
131 * Task to transmit our type map.
133 struct GNUNET_SCHEDULER_Task *typemap_task;
136 * Retransmission delay we currently use for the typemap
137 * transmissions (if not confirmed).
139 struct GNUNET_TIME_Relative typemap_delay;
142 * Is the neighbour queue empty and thus ready for us
143 * to transmit an encrypted message?
145 int ready_to_transmit;
148 * Is this the first time we're sending the typemap? If so,
149 * we want to send it a bit faster the second time. 0 if
150 * we are sending for the first time, 1 if not.
156 GNUNET_NETWORK_STRUCT_BEGIN
159 * Message sent to confirm that a typemap was received.
161 struct TypeMapConfirmationMessage
165 * Header with type #GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP.
167 struct GNUNET_MessageHeader header;
170 * Reserved, always zero.
172 uint32_t reserved GNUNET_PACKED;
175 * Hash of the (decompressed) type map that was received.
177 struct GNUNET_HashCode tm_hash;
181 GNUNET_NETWORK_STRUCT_END
185 * Map of peer identities to `struct Session`.
187 static struct GNUNET_CONTAINER_MultiPeerMap *sessions;
191 * Find the session for the given peer.
193 * @param peer identity of the peer
194 * @return NULL if we are not connected, otherwise the
197 static struct Session *
198 find_session (const struct GNUNET_PeerIdentity *peer)
200 return GNUNET_CONTAINER_multipeermap_get (sessions, peer);
205 * End the session with the given peer (we are no longer
208 * @param pid identity of peer to kill session with
211 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
213 struct Session *session;
214 struct GSC_ClientActiveRequest *car;
215 struct SessionMessageEntry *sme;
217 session = find_session (pid);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Destroying session for peer `%4s'\n",
222 GNUNET_i2s (&session->peer));
223 if (NULL != session->cork_task)
225 GNUNET_SCHEDULER_cancel (session->cork_task);
226 session->cork_task = NULL;
228 while (NULL != (car = session->active_client_request_head))
230 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
231 session->active_client_request_tail, car);
232 GSC_CLIENTS_reject_request (car,
235 while (NULL != (sme = session->sme_head))
237 GNUNET_CONTAINER_DLL_remove (session->sme_head,
242 if (NULL != session->typemap_task)
244 GNUNET_SCHEDULER_cancel (session->typemap_task);
245 session->typemap_task = NULL;
247 GSC_CLIENTS_notify_clients_about_neighbour (&session->peer,
248 session->tmap, NULL);
249 GNUNET_assert (GNUNET_YES ==
250 GNUNET_CONTAINER_multipeermap_remove (sessions,
253 GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# peers connected"),
254 GNUNET_CONTAINER_multipeermap_size (sessions),
256 GSC_TYPEMAP_destroy (session->tmap);
257 session->tmap = NULL;
258 GNUNET_free (session);
263 * Transmit our current typemap message to the other peer.
264 * (Done periodically until the typemap is confirmed).
266 * @param cls the `struct Session *`
270 transmit_typemap_task (void *cls,
271 const struct GNUNET_SCHEDULER_TaskContext *tc)
273 struct Session *session = cls;
274 struct GNUNET_MessageHeader *hdr;
275 struct GNUNET_TIME_Relative delay;
277 session->typemap_delay = GNUNET_TIME_STD_BACKOFF (session->typemap_delay);
278 delay = session->typemap_delay;
279 /* randomize a bit to avoid spont. sync */
280 delay.rel_value_us +=
281 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000 * 1000);
282 session->typemap_task =
283 GNUNET_SCHEDULER_add_delayed (delay,
284 &transmit_typemap_task, session);
285 GNUNET_STATISTICS_update (GSC_stats,
286 gettext_noop ("# type map refreshes sent"),
289 hdr = GSC_TYPEMAP_compute_type_map_message ();
290 GSC_KX_encrypt_and_transmit (session->kxinfo,
298 * Restart the typemap task for the given session.
300 * @param session session to restart typemap transmission for
303 start_typemap_task (struct Session *session)
305 if (NULL != session->typemap_task)
306 GNUNET_SCHEDULER_cancel (session->typemap_task);
307 session->typemap_delay = GNUNET_TIME_UNIT_SECONDS;
308 session->typemap_task =
309 GNUNET_SCHEDULER_add_delayed (session->typemap_delay,
310 &transmit_typemap_task,
316 * Create a session, a key exchange was just completed.
318 * @param peer peer that is now connected
319 * @param kx key exchange that completed
322 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
323 struct GSC_KeyExchangeInfo *kx)
325 struct Session *session;
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Creating session for peer `%4s'\n",
330 session = GNUNET_new (struct Session);
331 session->tmap = GSC_TYPEMAP_create ();
332 session->peer = *peer;
333 session->kxinfo = kx;
334 GNUNET_assert (GNUNET_OK ==
335 GNUNET_CONTAINER_multipeermap_put (sessions,
338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
339 GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# peers connected"),
340 GNUNET_CONTAINER_multipeermap_size (sessions),
342 GSC_CLIENTS_notify_clients_about_neighbour (peer,
345 start_typemap_task (session);
350 * The other peer has indicated that he 'lost' the session
351 * (KX down), reinitialize the session on our end, in particular
352 * this means to restart the typemap transmission.
354 * @param peer peer that is now connected
357 GSC_SESSIONS_reinit (const struct GNUNET_PeerIdentity *peer)
359 struct Session *session;
361 session = find_session (peer);
364 /* KX/session is new for both sides; thus no need to restart what
368 start_typemap_task (session);
373 * The other peer has confirmed receiving our type map,
374 * check if it is current and if so, stop retransmitting it.
376 * @param peer peer that confirmed the type map
377 * @param msg confirmation message we received
380 GSC_SESSIONS_confirm_typemap (const struct GNUNET_PeerIdentity *peer,
381 const struct GNUNET_MessageHeader *msg)
383 const struct TypeMapConfirmationMessage *cmsg;
384 struct Session *session;
386 session = find_session (peer);
392 if (ntohs (msg->size) != sizeof (struct TypeMapConfirmationMessage))
397 cmsg = (const struct TypeMapConfirmationMessage *) msg;
399 GSC_TYPEMAP_check_hash (&cmsg->tm_hash))
401 /* our typemap has changed in the meantime, do not
402 accept confirmation */
403 GNUNET_STATISTICS_update (GSC_stats,
405 ("# outdated typemap confirmations received"),
409 if (NULL != session->typemap_task)
411 GNUNET_SCHEDULER_cancel (session->typemap_task);
412 session->typemap_task = NULL;
414 GNUNET_STATISTICS_update (GSC_stats,
416 ("# valid typemap confirmations received"),
422 * Notify the given client about the session (client is new).
424 * @param cls the `struct GSC_Client`
425 * @param key peer identity
426 * @param value the `struct Session`
427 * @return #GNUNET_OK (continue to iterate)
430 notify_client_about_session (void *cls,
431 const struct GNUNET_PeerIdentity *key,
434 struct GSC_Client *client = cls;
435 struct Session *session = value;
437 GSC_CLIENTS_notify_client_about_neighbour (client,
439 NULL, /* old TMAP: none */
446 * We have a new client, notify it about all current sessions.
448 * @param client the new client
451 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
453 /* notify new client about existing sessions */
454 GNUNET_CONTAINER_multipeermap_iterate (sessions,
455 ¬ify_client_about_session,
461 * Try to perform a transmission on the given session. Will solicit
462 * additional messages if the 'sme' queue is not full enough.
464 * @param session session to transmit messages from
467 try_transmission (struct Session *session);
471 * Queue a request from a client for transmission to a particular peer.
473 * @param car request to queue; this handle is then shared between
474 * the caller (CLIENTS subsystem) and SESSIONS and must not
475 * be released by either until either #GSC_SESSIONS_dequeue(),
476 * #GSC_SESSIONS_transmit() or #GSC_CLIENTS_failed()
477 * have been invoked on it
480 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
482 struct Session *session;
484 session = find_session (&car->target);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Dropped client request for transmission (am disconnected)\n");
489 GNUNET_break (0); /* should have been rejected earlier */
490 GSC_CLIENTS_reject_request (car,
494 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
497 GSC_CLIENTS_reject_request (car,
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Received client transmission request. queueing\n");
503 GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
504 session->active_client_request_tail,
506 try_transmission (session);
511 * Dequeue a request from a client from transmission to a particular peer.
513 * @param car request to dequeue; this handle will then be 'owned' by
514 * the caller (CLIENTS sysbsystem)
517 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
519 struct Session *session;
522 memcmp (&car->target,
524 sizeof (struct GNUNET_PeerIdentity)))
526 session = find_session (&car->target);
527 GNUNET_assert (NULL != session);
528 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
529 session->active_client_request_tail,
531 /* dequeueing of 'high' priority messages may unblock
532 transmission for lower-priority messages, so we also
533 need to try in this case. */
534 try_transmission (session);
539 * Solicit messages for transmission, starting with those of the highest
542 * @param session session to solict messages for
543 * @param msize how many bytes do we have already
546 solicit_messages (struct Session *session,
549 struct GSC_ClientActiveRequest *car;
550 struct GSC_ClientActiveRequest *nxt;
552 enum GNUNET_CORE_Priority pmax;
555 pmax = GNUNET_CORE_PRIO_BACKGROUND;
556 for (car = session->active_client_request_head; NULL != car; car = car->next)
558 if (GNUNET_YES == car->was_solicited)
560 pmax = GNUNET_MAX (pmax, car->priority);
562 nxt = session->active_client_request_head;
563 while (NULL != (car = nxt))
566 if (car->priority < pmax)
568 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
570 so_size += car->msize;
571 if (GNUNET_YES == car->was_solicited)
573 car->was_solicited = GNUNET_YES;
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Soliciting message with priority %u\n",
577 GSC_CLIENTS_solicit_request (car);
578 /* The above call may *dequeue* requests and thereby
579 clobber 'nxt'. Hence we need to restart from the
581 nxt = session->active_client_request_head;
588 * Some messages were delayed (corked), but the timeout has now expired.
591 * @param cls `struct Session` with the messages to transmit now
592 * @param tc scheduler context (unused)
595 pop_cork_task (void *cls,
596 const struct GNUNET_SCHEDULER_TaskContext *tc)
598 struct Session *session = cls;
600 session->cork_task = NULL;
601 try_transmission (session);
606 * Try to perform a transmission on the given session. Will solicit
607 * additional messages if the 'sme' queue is not full enough or has
608 * only low-priority messages.
610 * @param session session to transmit messages from
613 try_transmission (struct Session *session)
615 struct SessionMessageEntry *pos;
617 struct GNUNET_TIME_Absolute now;
618 struct GNUNET_TIME_Absolute min_deadline;
619 enum GNUNET_CORE_Priority maxp;
620 enum GNUNET_CORE_Priority maxpc;
621 struct GSC_ClientActiveRequest *car;
624 if (GNUNET_YES != session->ready_to_transmit)
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Already ready to transmit, not evaluating queue\n");
631 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
632 /* if the peer has excess bandwidth, background traffic is allowed,
634 if (MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE <=
635 GSC_NEIGHBOURS_get_queue_size (&session->peer))
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Transmission queue already very long, waiting...\n");
639 return; /* queue already too long */
641 excess = GSC_NEIGHBOURS_check_excess_bandwidth (&session->peer);
642 if (GNUNET_YES == excess)
643 maxp = GNUNET_CORE_PRIO_BACKGROUND;
645 maxp = GNUNET_CORE_PRIO_BEST_EFFORT;
646 /* determine highest priority of 'ready' messages we already solicited from clients */
647 pos = session->sme_head;
648 while ((NULL != pos) &&
649 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
651 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
653 maxp = GNUNET_MAX (maxp, pos->priority);
654 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Calculating transmission set with %u priority (%s) and %s earliest deadline\n",
661 (GNUNET_YES == excess) ? "excess bandwidth" : "limited bandwidth",
662 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
665 if (maxp < GNUNET_CORE_PRIO_CRITICAL_CONTROL)
667 /* if highest already solicited priority from clients is not critical,
668 check if there are higher-priority messages to be solicited from clients */
669 if (GNUNET_YES == excess)
670 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
672 maxpc = GNUNET_CORE_PRIO_BEST_EFFORT;
673 for (car = session->active_client_request_head; NULL != car; car = car->next)
675 if (GNUNET_YES == car->was_solicited)
677 maxpc = GNUNET_MAX (maxpc,
682 /* we have messages waiting for solicitation that have a higher
683 priority than those that we already accepted; solicit the
684 high-priority messages first */
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Soliciting messages based on priority (%u > %u)\n",
689 solicit_messages (session, 0);
695 /* never solicit more, we have critical messages to process */
697 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
699 now = GNUNET_TIME_absolute_get ();
700 if ( ( (GNUNET_YES == excess) ||
701 (maxpc >= GNUNET_CORE_PRIO_BEST_EFFORT) ) &&
703 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
704 (min_deadline.abs_value_us > now.abs_value_us))) )
706 /* not enough ready yet (tiny message & cork possible), or no messages at all,
707 and either excess bandwidth or best-effort or higher message waiting at
708 client; in this case, we try to solicit more */
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Soliciting messages (excess %d, maxpc %d, message size %u, deadline %s)\n",
714 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
716 solicit_messages (session,
720 /* if there is data to send, just not yet, make sure we do transmit
721 * it once the deadline is reached */
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Corking until %s\n",
724 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
726 if (NULL != session->cork_task)
727 GNUNET_SCHEDULER_cancel (session->cork_task);
729 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Queue empty, waiting for solicitations\n");
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Building combined plaintext buffer to transmit message!\n");
742 /* create plaintext buffer of all messages (that fit), encrypt and
745 static unsigned long long total_bytes;
746 static unsigned int total_msgs;
747 char pbuf[msize]; /* plaintext */
751 while ( (NULL != (pos = session->sme_head)) &&
752 (used + pos->size <= msize) )
754 memcpy (&pbuf[used], &pos[1], pos->size);
756 GNUNET_CONTAINER_DLL_remove (session->sme_head,
761 /* compute average payload size */
766 /* 2^32 messages, wrap around... */
770 GNUNET_STATISTICS_set (GSC_stats,
771 "# avg payload per encrypted message",
772 total_bytes / total_msgs,
774 /* now actually transmit... */
775 session->ready_to_transmit = GNUNET_NO;
776 GSC_KX_encrypt_and_transmit (session->kxinfo,
784 * Send an updated typemap message to the neighbour now,
785 * and restart typemap transmissions.
787 * @param cls the message
788 * @param key neighbour's identity
789 * @param value `struct Neighbour` of the target
790 * @return always #GNUNET_OK
793 do_restart_typemap_message (void *cls,
794 const struct GNUNET_PeerIdentity *key,
797 const struct GNUNET_MessageHeader *hdr = cls;
798 struct Session *session = value;
799 struct SessionMessageEntry *sme;
802 size = ntohs (hdr->size);
803 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
804 memcpy (&sme[1], hdr, size);
806 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
807 GNUNET_CONTAINER_DLL_insert (session->sme_head,
810 try_transmission (session);
811 start_typemap_task (session);
817 * Broadcast an updated typemap message to all neighbours.
818 * Restarts the retransmissions until the typemaps are confirmed.
820 * @param msg message to transmit
823 GSC_SESSIONS_broadcast_typemap (const struct GNUNET_MessageHeader *msg)
825 if (NULL == sessions)
827 GNUNET_CONTAINER_multipeermap_iterate (sessions,
828 &do_restart_typemap_message,
834 * Traffic is being solicited for the given peer. This means that the
835 * message queue on the transport-level (NEIGHBOURS subsystem) is now
836 * empty and it is now OK to transmit another (non-control) message.
838 * @param pid identity of peer ready to receive data
841 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
843 struct Session *session;
845 session = find_session (pid);
848 session->ready_to_transmit = GNUNET_YES;
849 try_transmission (session);
854 * Transmit a message to a particular peer.
856 * @param car original request that was queued and then solicited;
857 * this handle will now be 'owned' by the SESSIONS subsystem
858 * @param msg message to transmit
859 * @param cork is corking allowed?
860 * @param priority how important is this message
863 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
864 const struct GNUNET_MessageHeader *msg,
866 enum GNUNET_CORE_Priority priority)
868 struct Session *session;
869 struct SessionMessageEntry *sme;
870 struct SessionMessageEntry *pos;
873 session = find_session (&car->target);
876 msize = ntohs (msg->size);
877 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
878 memcpy (&sme[1], msg, msize);
880 sme->priority = priority;
881 if (GNUNET_YES == cork)
883 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
884 pos = session->sme_head;
885 while ( (NULL != pos) &&
886 (pos->priority >= sme->priority) )
889 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
893 GNUNET_CONTAINER_DLL_insert_after (session->sme_head,
897 try_transmission (session);
902 * We have received a typemap message from a peer, update ours.
903 * Notifies clients about the session.
905 * @param peer peer this is about
906 * @param msg typemap update message
909 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
910 const struct GNUNET_MessageHeader *msg)
912 struct Session *session;
913 struct GSC_TypeMap *nmap;
914 struct SessionMessageEntry *sme;
915 struct TypeMapConfirmationMessage *tmc;
917 nmap = GSC_TYPEMAP_get_from_message (msg);
919 return; /* malformed */
920 session = find_session (peer);
926 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) +
927 sizeof (struct TypeMapConfirmationMessage));
928 sme->deadline = GNUNET_TIME_absolute_get ();
929 sme->size = sizeof (struct TypeMapConfirmationMessage);
930 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
931 tmc = (struct TypeMapConfirmationMessage *) &sme[1];
932 tmc->header.size = htons (sizeof (struct TypeMapConfirmationMessage));
933 tmc->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP);
934 tmc->reserved = htonl (0);
935 GSC_TYPEMAP_hash (nmap,
937 GNUNET_CONTAINER_DLL_insert (session->sme_head,
940 try_transmission (session);
941 GSC_CLIENTS_notify_clients_about_neighbour (peer,
944 GSC_TYPEMAP_destroy (session->tmap);
945 session->tmap = nmap;
950 * The given peer send a message of the specified type. Make sure the
951 * respective bit is set in its type-map and that clients are notified
954 * @param peer peer this is about
955 * @param type type of the message
958 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
961 struct Session *session;
962 struct GSC_TypeMap *nmap;
964 if (0 == memcmp (peer,
966 sizeof (struct GNUNET_PeerIdentity)))
968 session = find_session (peer);
969 GNUNET_assert (NULL != session);
970 if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap, &type, 1))
971 return; /* already in it */
972 nmap = GSC_TYPEMAP_extend (session->tmap, &type, 1);
973 GSC_CLIENTS_notify_clients_about_neighbour (peer,
974 session->tmap, nmap);
975 GSC_TYPEMAP_destroy (session->tmap);
976 session->tmap = nmap;
981 * Initialize sessions subsystem.
986 sessions = GNUNET_CONTAINER_multipeermap_create (128,
992 * Helper function for #GSC_SESSIONS_done() to free all
996 * @param key identity of the connected peer
997 * @param value the `struct Session` for the peer
998 * @return #GNUNET_OK (continue to iterate)
1001 free_session_helper (void *cls,
1002 const struct GNUNET_PeerIdentity *key,
1005 struct Session *session = value;
1007 GSC_SESSIONS_end (&session->peer);
1013 * Shutdown sessions subsystem.
1016 GSC_SESSIONS_done ()
1018 if (NULL != sessions)
1020 GNUNET_CONTAINER_multipeermap_iterate (sessions,
1021 &free_session_helper, NULL);
1022 GNUNET_CONTAINER_multipeermap_destroy (sessions);
1027 /* end of gnunet-service-core_sessions.c */