2 This file is part of GNUnet.
3 Copyright (C) 2009-2014 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"
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);
234 while (NULL != (sme = session->sme_head))
236 GNUNET_CONTAINER_DLL_remove (session->sme_head,
241 if (NULL != session->typemap_task)
243 GNUNET_SCHEDULER_cancel (session->typemap_task);
244 session->typemap_task = NULL;
246 GSC_CLIENTS_notify_clients_about_neighbour (&session->peer,
247 session->tmap, NULL);
248 GNUNET_assert (GNUNET_YES ==
249 GNUNET_CONTAINER_multipeermap_remove (sessions,
252 GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# peers connected"),
253 GNUNET_CONTAINER_multipeermap_size (sessions),
255 GSC_TYPEMAP_destroy (session->tmap);
256 session->tmap = NULL;
257 GNUNET_free (session);
262 * Transmit our current typemap message to the other peer.
263 * (Done periodically until the typemap is confirmed).
265 * @param cls the `struct Session *`
269 transmit_typemap_task (void *cls,
270 const struct GNUNET_SCHEDULER_TaskContext *tc)
272 struct Session *session = cls;
273 struct GNUNET_MessageHeader *hdr;
274 struct GNUNET_TIME_Relative delay;
276 session->typemap_delay = GNUNET_TIME_STD_BACKOFF (session->typemap_delay);
277 delay = session->typemap_delay;
278 /* randomize a bit to avoid spont. sync */
279 delay.rel_value_us +=
280 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000 * 1000);
281 session->typemap_task =
282 GNUNET_SCHEDULER_add_delayed (delay,
283 &transmit_typemap_task, session);
284 GNUNET_STATISTICS_update (GSC_stats,
285 gettext_noop ("# type map refreshes sent"),
288 hdr = GSC_TYPEMAP_compute_type_map_message ();
289 GSC_KX_encrypt_and_transmit (session->kxinfo,
297 * Restart the typemap task for the given session.
299 * @param session session to restart typemap transmission for
302 start_typemap_task (struct Session *session)
304 if (NULL != session->typemap_task)
305 GNUNET_SCHEDULER_cancel (session->typemap_task);
306 session->typemap_delay = GNUNET_TIME_UNIT_SECONDS;
307 session->typemap_task =
308 GNUNET_SCHEDULER_add_delayed (session->typemap_delay,
309 &transmit_typemap_task,
315 * Create a session, a key exchange was just completed.
317 * @param peer peer that is now connected
318 * @param kx key exchange that completed
321 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
322 struct GSC_KeyExchangeInfo *kx)
324 struct Session *session;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Creating session for peer `%4s'\n",
329 session = GNUNET_new (struct Session);
330 session->tmap = GSC_TYPEMAP_create ();
331 session->peer = *peer;
332 session->kxinfo = kx;
333 GNUNET_assert (GNUNET_OK ==
334 GNUNET_CONTAINER_multipeermap_put (sessions,
337 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
338 GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# peers connected"),
339 GNUNET_CONTAINER_multipeermap_size (sessions),
341 GSC_CLIENTS_notify_clients_about_neighbour (peer,
344 start_typemap_task (session);
349 * The other peer has indicated that he 'lost' the session
350 * (KX down), reinitialize the session on our end, in particular
351 * this means to restart the typemap transmission.
353 * @param peer peer that is now connected
356 GSC_SESSIONS_reinit (const struct GNUNET_PeerIdentity *peer)
358 struct Session *session;
360 session = find_session (peer);
363 /* KX/session is new for both sides; thus no need to restart what
367 start_typemap_task (session);
372 * The other peer has confirmed receiving our type map,
373 * check if it is current and if so, stop retransmitting it.
375 * @param peer peer that confirmed the type map
376 * @param msg confirmation message we received
379 GSC_SESSIONS_confirm_typemap (const struct GNUNET_PeerIdentity *peer,
380 const struct GNUNET_MessageHeader *msg)
382 const struct TypeMapConfirmationMessage *cmsg;
383 struct Session *session;
385 session = find_session (peer);
391 if (ntohs (msg->size) != sizeof (struct TypeMapConfirmationMessage))
396 cmsg = (const struct TypeMapConfirmationMessage *) msg;
398 GSC_TYPEMAP_check_hash (&cmsg->tm_hash))
400 /* our typemap has changed in the meantime, do not
401 accept confirmation */
402 GNUNET_STATISTICS_update (GSC_stats,
404 ("# outdated typemap confirmations received"),
408 if (NULL != session->typemap_task)
410 GNUNET_SCHEDULER_cancel (session->typemap_task);
411 session->typemap_task = NULL;
413 GNUNET_STATISTICS_update (GSC_stats,
415 ("# valid typemap confirmations received"),
421 * Notify the given client about the session (client is new).
423 * @param cls the `struct GSC_Client`
424 * @param key peer identity
425 * @param value the `struct Session`
426 * @return #GNUNET_OK (continue to iterate)
429 notify_client_about_session (void *cls,
430 const struct GNUNET_PeerIdentity *key,
433 struct GSC_Client *client = cls;
434 struct Session *session = value;
436 GSC_CLIENTS_notify_client_about_neighbour (client,
438 NULL, /* old TMAP: none */
445 * We have a new client, notify it about all current sessions.
447 * @param client the new client
450 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
452 /* notify new client about existing sessions */
453 GNUNET_CONTAINER_multipeermap_iterate (sessions,
454 ¬ify_client_about_session,
460 * Try to perform a transmission on the given session. Will solicit
461 * additional messages if the 'sme' queue is not full enough.
463 * @param session session to transmit messages from
466 try_transmission (struct Session *session);
470 * Queue a request from a client for transmission to a particular peer.
472 * @param car request to queue; this handle is then shared between
473 * the caller (CLIENTS subsystem) and SESSIONS and must not
474 * be released by either until either #GSC_SESSIONS_dequeue(),
475 * #GSC_SESSIONS_transmit() or #GSC_CLIENTS_failed()
476 * have been invoked on it
479 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
481 struct Session *session;
483 session = find_session (&car->target);
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Dropped client request for transmission (am disconnected)\n");
488 GNUNET_break (0); /* should have been rejected earlier */
489 GSC_CLIENTS_reject_request (car);
492 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
495 GSC_CLIENTS_reject_request (car);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Received client transmission request. queueing\n");
500 GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
501 session->active_client_request_tail, car);
502 try_transmission (session);
507 * Dequeue a request from a client from transmission to a particular peer.
509 * @param car request to dequeue; this handle will then be 'owned' by
510 * the caller (CLIENTS sysbsystem)
513 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
515 struct Session *session;
518 memcmp (&car->target, &GSC_my_identity,
519 sizeof (struct GNUNET_PeerIdentity)))
521 session = find_session (&car->target);
522 GNUNET_assert (NULL != session);
523 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
524 session->active_client_request_tail,
530 * Discard all expired active transmission requests from clients.
532 * @param session session to clean up
535 discard_expired_requests (struct Session *session)
537 struct GSC_ClientActiveRequest *pos;
538 struct GSC_ClientActiveRequest *nxt;
539 struct GNUNET_TIME_Absolute now;
541 now = GNUNET_TIME_absolute_get ();
543 nxt = session->active_client_request_head;
548 if ( (pos->deadline.abs_value_us < now.abs_value_us) &&
549 (GNUNET_YES != pos->was_solicited) )
551 GNUNET_STATISTICS_update (GSC_stats,
553 ("# messages discarded (expired prior to transmission)"),
555 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
556 session->active_client_request_tail,
558 GSC_CLIENTS_reject_request (pos);
565 * Solicit messages for transmission, starting with those of the highest
568 * @param session session to solict messages for
569 * @param msize how many bytes do we have already
572 solicit_messages (struct Session *session,
575 struct GSC_ClientActiveRequest *car;
576 struct GSC_ClientActiveRequest *nxt;
578 enum GNUNET_CORE_Priority pmax;
580 discard_expired_requests (session);
582 pmax = GNUNET_CORE_PRIO_BACKGROUND;
583 for (car = session->active_client_request_head; NULL != car; car = car->next)
585 if (GNUNET_YES == car->was_solicited)
587 pmax = GNUNET_MAX (pmax, car->priority);
589 nxt = session->active_client_request_head;
590 while (NULL != (car = nxt))
593 if (car->priority < pmax)
595 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
597 so_size += car->msize;
598 if (GNUNET_YES == car->was_solicited)
600 car->was_solicited = GNUNET_YES;
601 GSC_CLIENTS_solicit_request (car);
607 * Some messages were delayed (corked), but the timeout has now expired.
610 * @param cls `struct Session` with the messages to transmit now
611 * @param tc scheduler context (unused)
614 pop_cork_task (void *cls,
615 const struct GNUNET_SCHEDULER_TaskContext *tc)
617 struct Session *session = cls;
619 session->cork_task = NULL;
620 try_transmission (session);
625 * Try to perform a transmission on the given session. Will solicit
626 * additional messages if the 'sme' queue is not full enough or has
627 * only low-priority messages.
629 * @param session session to transmit messages from
632 try_transmission (struct Session *session)
634 struct SessionMessageEntry *pos;
636 struct GNUNET_TIME_Absolute now;
637 struct GNUNET_TIME_Absolute min_deadline;
638 enum GNUNET_CORE_Priority maxp;
639 enum GNUNET_CORE_Priority maxpc;
640 struct GSC_ClientActiveRequest *car;
643 if (GNUNET_YES != session->ready_to_transmit)
646 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
647 /* if the peer has excess bandwidth, background traffic is allowed,
649 if (MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE >=
650 GSC_NEIGHBOURS_check_excess_bandwidth (&session->peer))
651 return; /* queue already too long */
652 excess = GSC_NEIGHBOURS_check_excess_bandwidth (&session->peer);
653 if (GNUNET_YES == excess)
654 maxp = GNUNET_CORE_PRIO_BACKGROUND;
656 maxp = GNUNET_CORE_PRIO_BEST_EFFORT;
657 /* determine highest priority of 'ready' messages we already solicited from clients */
658 pos = session->sme_head;
659 while ((NULL != pos) &&
660 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
662 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
664 maxp = GNUNET_MAX (maxp, pos->priority);
665 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Calculating transmission set with %u priority (%s) and %s earliest deadline\n",
672 (GNUNET_YES == excess) ? "excess bandwidth" : "limited bandwidth",
673 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
676 if (maxp < GNUNET_CORE_PRIO_CRITICAL_CONTROL)
678 /* if highest already solicited priority from clients is not critical,
679 check if there are higher-priority messages to be solicited from clients */
680 if (GNUNET_YES == excess)
681 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
683 maxpc = GNUNET_CORE_PRIO_BEST_EFFORT;
684 for (car = session->active_client_request_head; NULL != car; car = car->next)
686 if (GNUNET_YES == car->was_solicited)
688 maxpc = GNUNET_MAX (maxpc, car->priority);
692 /* we have messages waiting for solicitation that have a higher
693 priority than those that we already accepted; solicit the
694 high-priority messages first */
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Soliciting messages based on priority (%u > %u)\n",
699 solicit_messages (session, 0);
705 /* never solicit more, we have critical messages to process */
707 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
709 now = GNUNET_TIME_absolute_get ();
710 if ( ( (GNUNET_YES == excess) ||
711 (maxpc >= GNUNET_CORE_PRIO_BEST_EFFORT) ) &&
713 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
714 (min_deadline.abs_value_us > now.abs_value_us))) )
716 /* not enough ready yet (tiny message & cork possible), or no messages at all,
717 and either excess bandwidth or best-effort or higher message waiting at
718 client; in this case, we try to solicit more */
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Soliciting messages (excess %d, maxpc %d, message size %u, deadline %s)\n",
724 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
726 solicit_messages (session,
730 /* if there is data to send, just not yet, make sure we do transmit
731 * it once the deadline is reached */
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Corking until %s\n",
734 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
736 if (NULL != session->cork_task)
737 GNUNET_SCHEDULER_cancel (session->cork_task);
739 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (min_deadline),
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Building combined plaintext buffer to transmit message!\n");
747 /* create plaintext buffer of all messages (that fit), encrypt and
750 static unsigned long long total_bytes;
751 static unsigned int total_msgs;
752 char pbuf[msize]; /* plaintext */
756 while ( (NULL != (pos = session->sme_head)) &&
757 (used + pos->size <= msize) )
759 memcpy (&pbuf[used], &pos[1], pos->size);
761 GNUNET_CONTAINER_DLL_remove (session->sme_head,
766 /* compute average payload size */
771 /* 2^32 messages, wrap around... */
775 GNUNET_STATISTICS_set (GSC_stats,
776 "# avg payload per encrypted message",
777 total_bytes / total_msgs,
779 /* now actually transmit... */
780 session->ready_to_transmit = GNUNET_NO;
781 GSC_KX_encrypt_and_transmit (session->kxinfo,
789 * Send an updated typemap message to the neighbour now,
790 * and restart typemap transmissions.
792 * @param cls the message
793 * @param key neighbour's identity
794 * @param value `struct Neighbour` of the target
795 * @return always #GNUNET_OK
798 do_restart_typemap_message (void *cls,
799 const struct GNUNET_PeerIdentity *key,
802 const struct GNUNET_MessageHeader *hdr = cls;
803 struct Session *session = value;
804 struct SessionMessageEntry *sme;
807 size = ntohs (hdr->size);
808 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
809 memcpy (&sme[1], hdr, size);
811 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
812 GNUNET_CONTAINER_DLL_insert (session->sme_head,
815 try_transmission (session);
816 start_typemap_task (session);
822 * Broadcast an updated typemap message to all neighbours.
823 * Restarts the retransmissions until the typemaps are confirmed.
825 * @param msg message to transmit
828 GSC_SESSIONS_broadcast_typemap (const struct GNUNET_MessageHeader *msg)
830 if (NULL == sessions)
832 GNUNET_CONTAINER_multipeermap_iterate (sessions,
833 &do_restart_typemap_message,
839 * Traffic is being solicited for the given peer. This means that the
840 * message queue on the transport-level (NEIGHBOURS subsystem) is now
841 * empty and it is now OK to transmit another (non-control) message.
843 * @param pid identity of peer ready to receive data
846 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
848 struct Session *session;
850 session = find_session (pid);
853 session->ready_to_transmit = GNUNET_YES;
854 try_transmission (session);
859 * Transmit a message to a particular peer.
861 * @param car original request that was queued and then solicited;
862 * this handle will now be 'owned' by the SESSIONS subsystem
863 * @param msg message to transmit
864 * @param cork is corking allowed?
865 * @param priority how important is this message
868 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
869 const struct GNUNET_MessageHeader *msg,
871 enum GNUNET_CORE_Priority priority)
873 struct Session *session;
874 struct SessionMessageEntry *sme;
875 struct SessionMessageEntry *pos;
878 session = find_session (&car->target);
881 msize = ntohs (msg->size);
882 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
883 memcpy (&sme[1], msg, msize);
885 sme->priority = priority;
886 if (GNUNET_YES == cork)
888 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
889 pos = session->sme_head;
890 while ( (NULL != pos) &&
891 (pos->priority >= sme->priority) )
894 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
898 GNUNET_CONTAINER_DLL_insert_after (session->sme_head,
902 try_transmission (session);
907 * We have received a typemap message from a peer, update ours.
908 * Notifies clients about the session.
910 * @param peer peer this is about
911 * @param msg typemap update message
914 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
915 const struct GNUNET_MessageHeader *msg)
917 struct Session *session;
918 struct GSC_TypeMap *nmap;
919 struct SessionMessageEntry *sme;
920 struct TypeMapConfirmationMessage *tmc;
922 nmap = GSC_TYPEMAP_get_from_message (msg);
924 return; /* malformed */
925 session = find_session (peer);
931 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) +
932 sizeof (struct TypeMapConfirmationMessage));
933 sme->deadline = GNUNET_TIME_absolute_get ();
934 sme->size = sizeof (struct TypeMapConfirmationMessage);
935 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
936 tmc = (struct TypeMapConfirmationMessage *) &sme[1];
937 tmc->header.size = htons (sizeof (struct TypeMapConfirmationMessage));
938 tmc->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP);
939 tmc->reserved = htonl (0);
940 GSC_TYPEMAP_hash (nmap,
942 GNUNET_CONTAINER_DLL_insert (session->sme_head,
945 try_transmission (session);
946 GSC_CLIENTS_notify_clients_about_neighbour (peer,
949 GSC_TYPEMAP_destroy (session->tmap);
950 session->tmap = nmap;
955 * The given peer send a message of the specified type. Make sure the
956 * respective bit is set in its type-map and that clients are notified
959 * @param peer peer this is about
960 * @param type type of the message
963 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
966 struct Session *session;
967 struct GSC_TypeMap *nmap;
969 if (0 == memcmp (peer,
971 sizeof (struct GNUNET_PeerIdentity)))
973 session = find_session (peer);
974 GNUNET_assert (NULL != session);
975 if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap, &type, 1))
976 return; /* already in it */
977 nmap = GSC_TYPEMAP_extend (session->tmap, &type, 1);
978 GSC_CLIENTS_notify_clients_about_neighbour (peer,
979 session->tmap, nmap);
980 GSC_TYPEMAP_destroy (session->tmap);
981 session->tmap = nmap;
986 * Initialize sessions subsystem.
991 sessions = GNUNET_CONTAINER_multipeermap_create (128,
997 * Helper function for #GSC_SESSIONS_done() to free all
1001 * @param key identity of the connected peer
1002 * @param value the `struct Session` for the peer
1003 * @return #GNUNET_OK (continue to iterate)
1006 free_session_helper (void *cls,
1007 const struct GNUNET_PeerIdentity *key,
1010 struct Session *session = value;
1012 GSC_SESSIONS_end (&session->peer);
1018 * Shutdown sessions subsystem.
1021 GSC_SESSIONS_done ()
1023 if (NULL != sessions)
1025 GNUNET_CONTAINER_multipeermap_iterate (sessions,
1026 &free_session_helper, NULL);
1027 GNUNET_CONTAINER_multipeermap_destroy (sessions);
1032 /* end of gnunet-service-core_sessions.c */