2 This file is part of GNUnet.
3 Copyright (C) 2009-2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_kx.h"
29 #include "gnunet-service-core_typemap.h"
30 #include "gnunet-service-core_sessions.h"
31 #include "gnunet_constants.h"
36 * How many encrypted messages do we queue at most?
37 * Needed to bound memory consumption.
39 #define MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE 4
43 * Message ready for encryption. This struct is followed by the
44 * actual content of the message.
46 struct SessionMessageEntry
50 * We keep messages in a doubly linked list.
52 struct SessionMessageEntry *next;
55 * We keep messages in a doubly linked list.
57 struct SessionMessageEntry *prev;
60 * How important is this message.
62 enum GNUNET_CORE_Priority priority;
65 * Flag set to #GNUNET_YES if this is a typemap message.
70 * Flag set to #GNUNET_YES if this is a typemap confirmation message.
72 int is_typemap_confirm;
75 * Deadline for transmission, 1s after we received it (if we
76 * are not corking), otherwise "now". Note that this message
77 * does NOT expire past its deadline.
79 struct GNUNET_TIME_Absolute deadline;
82 * How long is the message? (number of bytes following the `struct
83 * MessageEntry`, but not including the size of `struct
84 * MessageEntry` itself!)
92 * Data kept per session.
97 * Identity of the other peer.
99 const struct GNUNET_PeerIdentity *peer;
102 * Key exchange state for this peer.
104 struct GSC_KeyExchangeInfo *kx;
107 * Head of list of requests from clients for transmission to
110 struct GSC_ClientActiveRequest *active_client_request_head;
113 * Tail of list of requests from clients for transmission to
116 struct GSC_ClientActiveRequest *active_client_request_tail;
119 * Head of list of messages ready for encryption.
121 struct SessionMessageEntry *sme_head;
124 * Tail of list of messages ready for encryption.
126 struct SessionMessageEntry *sme_tail;
129 * Current type map for this peer.
131 struct GSC_TypeMap *tmap;
134 * Task to transmit corked messages with a delay.
136 struct GNUNET_SCHEDULER_Task *cork_task;
139 * Task to transmit our type map.
141 struct GNUNET_SCHEDULER_Task *typemap_task;
144 * Retransmission delay we currently use for the typemap
145 * transmissions (if not confirmed).
147 struct GNUNET_TIME_Relative typemap_delay;
150 * Is this the first time we're sending the typemap? If so,
151 * we want to send it a bit faster the second time. 0 if
152 * we are sending for the first time, 1 if not.
158 GNUNET_NETWORK_STRUCT_BEGIN
161 * Message sent to confirm that a typemap was received.
163 struct TypeMapConfirmationMessage
167 * Header with type #GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP.
169 struct GNUNET_MessageHeader header;
172 * Reserved, always zero.
174 uint32_t reserved GNUNET_PACKED;
177 * Hash of the (decompressed) type map that was received.
179 struct GNUNET_HashCode tm_hash;
183 GNUNET_NETWORK_STRUCT_END
187 * Map of peer identities to `struct Session`.
189 static struct GNUNET_CONTAINER_MultiPeerMap *sessions;
193 * Find the session for the given peer.
195 * @param peer identity of the peer
196 * @return NULL if we are not connected, otherwise the
199 static struct Session *
200 find_session (const struct GNUNET_PeerIdentity *peer)
202 if (NULL == sessions)
204 return GNUNET_CONTAINER_multipeermap_get (sessions,
210 * End the session with the given peer (we are no longer
213 * @param pid identity of peer to kill session with
216 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
218 struct Session *session;
219 struct GSC_ClientActiveRequest *car;
220 struct SessionMessageEntry *sme;
222 session = find_session (pid);
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Destroying session for peer `%s'\n",
227 GNUNET_i2s (session->peer));
228 if (NULL != session->cork_task)
230 GNUNET_SCHEDULER_cancel (session->cork_task);
231 session->cork_task = NULL;
233 while (NULL != (car = session->active_client_request_head))
235 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
236 session->active_client_request_tail, car);
237 GSC_CLIENTS_reject_request (car,
240 while (NULL != (sme = session->sme_head))
242 GNUNET_CONTAINER_DLL_remove (session->sme_head,
247 if (NULL != session->typemap_task)
249 GNUNET_SCHEDULER_cancel (session->typemap_task);
250 session->typemap_task = NULL;
252 GSC_CLIENTS_notify_clients_about_neighbour (session->peer,
255 GNUNET_assert (GNUNET_YES ==
256 GNUNET_CONTAINER_multipeermap_remove (sessions,
259 GNUNET_STATISTICS_set (GSC_stats,
260 gettext_noop ("# peers connected"),
261 GNUNET_CONTAINER_multipeermap_size (sessions),
263 GSC_TYPEMAP_destroy (session->tmap);
264 session->tmap = NULL;
265 GNUNET_free (session);
270 * Transmit our current typemap message to the other peer.
271 * (Done periodically until the typemap is confirmed).
273 * @param cls the `struct Session *`
276 transmit_typemap_task (void *cls)
278 struct Session *session = cls;
279 struct GNUNET_MessageHeader *hdr;
280 struct GNUNET_TIME_Relative delay;
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Sending TYPEMAP to %s\n",
284 GNUNET_i2s (session->peer));
285 session->typemap_delay = GNUNET_TIME_STD_BACKOFF (session->typemap_delay);
286 delay = session->typemap_delay;
287 /* randomize a bit to avoid spont. sync */
288 delay.rel_value_us +=
289 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
291 session->typemap_task =
292 GNUNET_SCHEDULER_add_delayed (delay,
293 &transmit_typemap_task,
295 GNUNET_STATISTICS_update (GSC_stats,
296 gettext_noop ("# type map refreshes sent"),
299 hdr = GSC_TYPEMAP_compute_type_map_message ();
300 GSC_KX_encrypt_and_transmit (session->kx,
308 * Restart the typemap task for the given session.
310 * @param session session to restart typemap transmission for
313 start_typemap_task (struct Session *session)
315 if (NULL != session->typemap_task)
316 GNUNET_SCHEDULER_cancel (session->typemap_task);
317 session->typemap_delay = GNUNET_TIME_UNIT_SECONDS;
318 session->typemap_task =
319 GNUNET_SCHEDULER_add_delayed (session->typemap_delay,
320 &transmit_typemap_task,
326 * Create a session, a key exchange was just completed.
328 * @param peer peer that is now connected
329 * @param kx key exchange that completed
332 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
333 struct GSC_KeyExchangeInfo *kx)
335 struct Session *session;
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Creating session for peer `%s'\n",
340 session = GNUNET_new (struct Session);
341 session->tmap = GSC_TYPEMAP_create ();
342 session->peer = peer;
344 GNUNET_assert (GNUNET_OK ==
345 GNUNET_CONTAINER_multipeermap_put (sessions,
348 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
349 GNUNET_STATISTICS_set (GSC_stats,
350 gettext_noop ("# peers connected"),
351 GNUNET_CONTAINER_multipeermap_size (sessions),
353 GSC_CLIENTS_notify_clients_about_neighbour (peer,
356 start_typemap_task (session);
361 * The other peer has indicated that it 'lost' the session
362 * (KX down), reinitialize the session on our end, in particular
363 * this means to restart the typemap transmission.
365 * @param peer peer that is now connected
368 GSC_SESSIONS_reinit (const struct GNUNET_PeerIdentity *peer)
370 struct Session *session;
372 session = find_session (peer);
375 /* KX/session is new for both sides; thus no need to restart what
379 start_typemap_task (session);
384 * The other peer has confirmed receiving our type map,
385 * check if it is current and if so, stop retransmitting it.
387 * @param peer peer that confirmed the type map
388 * @param msg confirmation message we received
391 GSC_SESSIONS_confirm_typemap (const struct GNUNET_PeerIdentity *peer,
392 const struct GNUNET_MessageHeader *msg)
394 const struct TypeMapConfirmationMessage *cmsg;
395 struct Session *session;
397 session = find_session (peer);
403 if (ntohs (msg->size) != sizeof (struct TypeMapConfirmationMessage))
408 cmsg = (const struct TypeMapConfirmationMessage *) msg;
410 GSC_TYPEMAP_check_hash (&cmsg->tm_hash))
412 /* our typemap has changed in the meantime, do not
413 accept confirmation */
414 GNUNET_STATISTICS_update (GSC_stats,
416 ("# outdated typemap confirmations received"),
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Got outdated typemap confirmated from peer `%s'\n",
420 GNUNET_i2s (session->peer));
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Got typemap confirmation from peer `%s'\n",
425 GNUNET_i2s (session->peer));
426 if (NULL != session->typemap_task)
428 GNUNET_SCHEDULER_cancel (session->typemap_task);
429 session->typemap_task = NULL;
431 GNUNET_STATISTICS_update (GSC_stats,
433 ("# valid typemap confirmations received"),
439 * Notify the given client about the session (client is new).
441 * @param cls the `struct GSC_Client`
442 * @param key peer identity
443 * @param value the `struct Session`
444 * @return #GNUNET_OK (continue to iterate)
447 notify_client_about_session (void *cls,
448 const struct GNUNET_PeerIdentity *key,
451 struct GSC_Client *client = cls;
452 struct Session *session = value;
454 GSC_CLIENTS_notify_client_about_neighbour (client,
456 NULL, /* old TMAP: none */
463 * We have a new client, notify it about all current sessions.
465 * @param client the new client
468 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
470 /* notify new client about existing sessions */
471 GNUNET_CONTAINER_multipeermap_iterate (sessions,
472 ¬ify_client_about_session,
478 * Try to perform a transmission on the given session. Will solicit
479 * additional messages if the 'sme' queue is not full enough.
481 * @param session session to transmit messages from
484 try_transmission (struct Session *session);
488 * Queue a request from a client for transmission to a particular peer.
490 * @param car request to queue; this handle is then shared between
491 * the caller (CLIENTS subsystem) and SESSIONS and must not
492 * be released by either until either #GSC_SESSIONS_dequeue(),
493 * #GSC_SESSIONS_transmit() or #GSC_CLIENTS_failed()
494 * have been invoked on it
497 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
499 struct Session *session;
501 session = find_session (&car->target);
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Dropped client request for transmission (am disconnected)\n");
506 GNUNET_break (0); /* should have been rejected earlier */
507 GSC_CLIENTS_reject_request (car,
511 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
514 GSC_CLIENTS_reject_request (car,
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Received client transmission request. queueing\n");
520 GNUNET_CONTAINER_DLL_insert_tail (session->active_client_request_head,
521 session->active_client_request_tail,
523 try_transmission (session);
528 * Dequeue a request from a client from transmission to a particular peer.
530 * @param car request to dequeue; this handle will then be 'owned' by
531 * the caller (CLIENTS sysbsystem)
534 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
536 struct Session *session;
539 memcmp (&car->target,
541 sizeof (struct GNUNET_PeerIdentity)))
543 session = find_session (&car->target);
544 GNUNET_assert (NULL != session);
545 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
546 session->active_client_request_tail,
548 /* dequeueing of 'high' priority messages may unblock
549 transmission for lower-priority messages, so we also
550 need to try in this case. */
551 try_transmission (session);
556 * Solicit messages for transmission, starting with those of the highest
559 * @param session session to solict messages for
560 * @param msize how many bytes do we have already
563 solicit_messages (struct Session *session,
566 struct GSC_ClientActiveRequest *car;
567 struct GSC_ClientActiveRequest *nxt;
569 enum GNUNET_CORE_Priority pmax;
572 pmax = GNUNET_CORE_PRIO_BACKGROUND;
573 for (car = session->active_client_request_head; NULL != car; car = car->next)
575 if (GNUNET_YES == car->was_solicited)
577 pmax = GNUNET_MAX (pmax, car->priority);
579 nxt = session->active_client_request_head;
580 while (NULL != (car = nxt))
583 if (car->priority < pmax)
585 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
587 so_size += car->msize;
588 if (GNUNET_YES == car->was_solicited)
590 car->was_solicited = GNUNET_YES;
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Soliciting message with priority %u\n",
594 GSC_CLIENTS_solicit_request (car);
595 /* The above call may *dequeue* requests and thereby
596 clobber 'nxt'. Hence we need to restart from the
598 nxt = session->active_client_request_head;
605 * Some messages were delayed (corked), but the timeout has now expired.
608 * @param cls `struct Session` with the messages to transmit now
611 pop_cork_task (void *cls)
613 struct Session *session = cls;
615 session->cork_task = NULL;
616 try_transmission (session);
621 * Try to perform a transmission on the given session. Will solicit
622 * additional messages if the 'sme' queue is not full enough or has
623 * only low-priority messages.
625 * @param session session to transmit messages from
628 try_transmission (struct Session *session)
630 struct SessionMessageEntry *pos;
632 struct GNUNET_TIME_Absolute now;
633 struct GNUNET_TIME_Absolute min_deadline;
634 enum GNUNET_CORE_Priority maxp;
635 enum GNUNET_CORE_Priority maxpc;
636 struct GSC_ClientActiveRequest *car;
640 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
641 /* if the peer has excess bandwidth, background traffic is allowed,
643 if (MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE <=
644 GSC_NEIGHBOURS_get_queue_length (session->kx))
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Transmission queue already very long, waiting...\n");
648 return; /* queue already too long */
650 excess = GSC_NEIGHBOURS_check_excess_bandwidth (session->kx);
651 if (GNUNET_YES == excess)
652 maxp = GNUNET_CORE_PRIO_BACKGROUND;
654 maxp = GNUNET_CORE_PRIO_BEST_EFFORT;
655 /* determine highest priority of 'ready' messages we already solicited from clients */
656 pos = session->sme_head;
657 while ((NULL != pos) &&
658 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
660 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
662 maxp = GNUNET_MAX (maxp, pos->priority);
663 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Calculating transmission set with %u priority (%s) and %s earliest deadline\n",
670 (GNUNET_YES == excess) ? "excess bandwidth" : "limited bandwidth",
671 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
674 if (maxp < GNUNET_CORE_PRIO_CRITICAL_CONTROL)
676 /* if highest already solicited priority from clients is not critical,
677 check if there are higher-priority messages to be solicited from clients */
678 if (GNUNET_YES == excess)
679 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
681 maxpc = GNUNET_CORE_PRIO_BEST_EFFORT;
682 for (car = session->active_client_request_head; NULL != car; car = car->next)
684 if (GNUNET_YES == car->was_solicited)
686 maxpc = GNUNET_MAX (maxpc,
691 /* we have messages waiting for solicitation that have a higher
692 priority than those that we already accepted; solicit the
693 high-priority messages first */
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Soliciting messages based on priority (%u > %u)\n",
698 solicit_messages (session, 0);
704 /* never solicit more, we have critical messages to process */
706 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
708 now = GNUNET_TIME_absolute_get ();
709 if ( ( (GNUNET_YES == excess) ||
710 (maxpc >= GNUNET_CORE_PRIO_BEST_EFFORT) ) &&
712 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
713 (min_deadline.abs_value_us > now.abs_value_us))) )
715 /* not enough ready yet (tiny message & cork possible), or no messages at all,
716 and either excess bandwidth or best-effort or higher message waiting at
717 client; in this case, we try to solicit more */
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Soliciting messages (excess %d, maxpc %d, message size %u, deadline %s)\n",
722 (unsigned int) msize,
723 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
725 solicit_messages (session,
729 /* if there is data to send, just not yet, make sure we do transmit
730 * it once the deadline is reached */
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Corking until %s\n",
733 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
735 if (NULL != session->cork_task)
736 GNUNET_SCHEDULER_cancel (session->cork_task);
738 = GNUNET_SCHEDULER_add_at (min_deadline,
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Queue empty, waiting for solicitations\n");
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Building combined plaintext buffer to transmit message!\n");
751 /* create plaintext buffer of all messages (that fit), encrypt and
754 static unsigned long long total_bytes;
755 static unsigned int total_msgs;
756 char pbuf[msize]; /* plaintext */
760 while ( (NULL != (pos = session->sme_head)) &&
761 (used + pos->size <= msize) )
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "Adding message of type %d (%d/%d) to payload for %s\n",
765 ntohs (((const struct GNUNET_MessageHeader *)&pos[1])->type),
767 pos->is_typemap_confirm,
768 GNUNET_i2s (session->peer));
769 GNUNET_memcpy (&pbuf[used],
773 GNUNET_CONTAINER_DLL_remove (session->sme_head,
778 /* compute average payload size */
783 /* 2^32 messages, wrap around... */
787 GNUNET_STATISTICS_set (GSC_stats,
788 "# avg payload per encrypted message",
789 total_bytes / total_msgs,
791 /* now actually transmit... */
792 GSC_KX_encrypt_and_transmit (session->kx,
800 * Send an updated typemap message to the neighbour now,
801 * and restart typemap transmissions.
803 * @param cls the message
804 * @param key neighbour's identity
805 * @param value `struct Neighbour` of the target
806 * @return always #GNUNET_OK
809 do_restart_typemap_message (void *cls,
810 const struct GNUNET_PeerIdentity *key,
813 const struct GNUNET_MessageHeader *hdr = cls;
814 struct Session *session = value;
815 struct SessionMessageEntry *sme;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Restarting sending TYPEMAP to %s\n",
820 GNUNET_i2s (session->peer));
821 size = ntohs (hdr->size);
822 for (sme = session->sme_head; NULL != sme; sme = sme->next)
824 if (GNUNET_YES == sme->is_typemap)
826 GNUNET_CONTAINER_DLL_remove (session->sme_head,
833 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
834 sme->is_typemap = GNUNET_YES;
835 GNUNET_memcpy (&sme[1],
839 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
840 GNUNET_CONTAINER_DLL_insert (session->sme_head,
843 try_transmission (session);
844 start_typemap_task (session);
850 * Broadcast an updated typemap message to all neighbours.
851 * Restarts the retransmissions until the typemaps are confirmed.
853 * @param msg message to transmit
856 GSC_SESSIONS_broadcast_typemap (const struct GNUNET_MessageHeader *msg)
858 if (NULL == sessions)
860 GNUNET_CONTAINER_multipeermap_iterate (sessions,
861 &do_restart_typemap_message,
867 * Traffic is being solicited for the given peer. This means that the
868 * message queue on the transport-level (NEIGHBOURS subsystem) is now
869 * empty and it is now OK to transmit another (non-control) message.
871 * @param pid identity of peer ready to receive data
874 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
876 struct Session *session;
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Transport solicits for %s\n",
881 session = find_session (pid);
884 try_transmission (session);
889 * Transmit a message to a particular peer.
891 * @param car original request that was queued and then solicited;
892 * this handle will now be 'owned' by the SESSIONS subsystem
893 * @param msg message to transmit
894 * @param cork is corking allowed?
895 * @param priority how important is this message
898 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
899 const struct GNUNET_MessageHeader *msg,
901 enum GNUNET_CORE_Priority priority)
903 struct Session *session;
904 struct SessionMessageEntry *sme;
905 struct SessionMessageEntry *pos;
908 session = find_session (&car->target);
911 msize = ntohs (msg->size);
912 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
913 GNUNET_memcpy (&sme[1],
917 sme->priority = priority;
918 if (GNUNET_YES == cork)
921 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Mesage corked, delaying transmission\n");
925 pos = session->sme_head;
926 while ( (NULL != pos) &&
927 (pos->priority >= sme->priority) )
930 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
934 GNUNET_CONTAINER_DLL_insert_after (session->sme_head,
938 try_transmission (session);
943 * We have received a typemap message from a peer, update ours.
944 * Notifies clients about the session.
946 * @param peer peer this is about
947 * @param msg typemap update message
950 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
951 const struct GNUNET_MessageHeader *msg)
953 struct Session *session;
954 struct GSC_TypeMap *nmap;
955 struct SessionMessageEntry *sme;
956 struct TypeMapConfirmationMessage *tmc;
958 nmap = GSC_TYPEMAP_get_from_message (msg);
962 return; /* malformed */
964 session = find_session (peer);
967 GSC_TYPEMAP_destroy (nmap);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Received TYPEMAP from %s\n",
973 GNUNET_i2s (session->peer));
974 for (sme = session->sme_head; NULL != sme; sme = sme->next)
976 if (GNUNET_YES == sme->is_typemap_confirm)
978 GNUNET_CONTAINER_DLL_remove (session->sme_head,
985 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) +
986 sizeof (struct TypeMapConfirmationMessage));
987 sme->deadline = GNUNET_TIME_absolute_get ();
988 sme->size = sizeof (struct TypeMapConfirmationMessage);
989 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
990 sme->is_typemap_confirm = GNUNET_YES;
991 tmc = (struct TypeMapConfirmationMessage *) &sme[1];
992 tmc->header.size = htons (sizeof (struct TypeMapConfirmationMessage));
993 tmc->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP);
994 tmc->reserved = htonl (0);
995 GSC_TYPEMAP_hash (nmap,
997 GNUNET_CONTAINER_DLL_insert (session->sme_head,
1000 try_transmission (session);
1001 GSC_CLIENTS_notify_clients_about_neighbour (peer,
1004 GSC_TYPEMAP_destroy (session->tmap);
1005 session->tmap = nmap;
1010 * The given peer send a message of the specified type. Make sure the
1011 * respective bit is set in its type-map and that clients are notified
1012 * about the session.
1014 * @param peer peer this is about
1015 * @param type type of the message
1018 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
1021 struct Session *session;
1022 struct GSC_TypeMap *nmap;
1024 if (0 == memcmp (peer,
1026 sizeof (struct GNUNET_PeerIdentity)))
1028 session = find_session (peer);
1029 GNUNET_assert (NULL != session);
1030 if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap,
1032 return; /* already in it */
1033 nmap = GSC_TYPEMAP_extend (session->tmap,
1036 GSC_CLIENTS_notify_clients_about_neighbour (peer,
1039 GSC_TYPEMAP_destroy (session->tmap);
1040 session->tmap = nmap;
1045 * Initialize sessions subsystem.
1048 GSC_SESSIONS_init ()
1050 sessions = GNUNET_CONTAINER_multipeermap_create (128,
1056 * Helper function for #GSC_SESSIONS_done() to free all
1060 * @param key identity of the connected peer
1061 * @param value the `struct Session` for the peer
1062 * @return #GNUNET_OK (continue to iterate)
1065 free_session_helper (void *cls,
1066 const struct GNUNET_PeerIdentity *key,
1069 /* struct Session *session = value; */
1071 GSC_SESSIONS_end (key);
1077 * Shutdown sessions subsystem.
1080 GSC_SESSIONS_done ()
1082 if (NULL != sessions)
1084 GNUNET_CONTAINER_multipeermap_iterate (sessions,
1085 &free_session_helper,
1087 GNUNET_CONTAINER_multipeermap_destroy (sessions);
1092 /* end of gnunet-service-core_sessions.c */