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.
17 * @file core/gnunet-service-core_sessions.c
18 * @brief code for managing of 'encrypted' sessions (key exchange done)
19 * @author Christian Grothoff
22 #include "gnunet-service-core.h"
23 #include "gnunet-service-core_kx.h"
24 #include "gnunet-service-core_typemap.h"
25 #include "gnunet-service-core_sessions.h"
26 #include "gnunet_constants.h"
31 * How many encrypted messages do we queue at most?
32 * Needed to bound memory consumption.
34 #define MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE 4
38 * Message ready for encryption. This struct is followed by the
39 * actual content of the message.
41 struct SessionMessageEntry
45 * We keep messages in a doubly linked list.
47 struct SessionMessageEntry *next;
50 * We keep messages in a doubly linked list.
52 struct SessionMessageEntry *prev;
55 * How important is this message.
57 enum GNUNET_CORE_Priority priority;
60 * Flag set to #GNUNET_YES if this is a typemap message.
65 * Flag set to #GNUNET_YES if this is a typemap confirmation message.
67 int is_typemap_confirm;
70 * Deadline for transmission, 1s after we received it (if we
71 * are not corking), otherwise "now". Note that this message
72 * does NOT expire past its deadline.
74 struct GNUNET_TIME_Absolute deadline;
77 * How long is the message? (number of bytes following the `struct
78 * MessageEntry`, but not including the size of `struct
79 * MessageEntry` itself!)
87 * Data kept per session.
92 * Identity of the other peer.
94 const struct GNUNET_PeerIdentity *peer;
97 * Key exchange state for this peer.
99 struct GSC_KeyExchangeInfo *kx;
102 * Head of list of requests from clients for transmission to
105 struct GSC_ClientActiveRequest *active_client_request_head;
108 * Tail of list of requests from clients for transmission to
111 struct GSC_ClientActiveRequest *active_client_request_tail;
114 * Head of list of messages ready for encryption.
116 struct SessionMessageEntry *sme_head;
119 * Tail of list of messages ready for encryption.
121 struct SessionMessageEntry *sme_tail;
124 * Current type map for this peer.
126 struct GSC_TypeMap *tmap;
129 * Task to transmit corked messages with a delay.
131 struct GNUNET_SCHEDULER_Task *cork_task;
134 * Task to transmit our type map.
136 struct GNUNET_SCHEDULER_Task *typemap_task;
139 * Retransmission delay we currently use for the typemap
140 * transmissions (if not confirmed).
142 struct GNUNET_TIME_Relative typemap_delay;
145 * Is this the first time we're sending the typemap? If so,
146 * we want to send it a bit faster the second time. 0 if
147 * we are sending for the first time, 1 if not.
153 GNUNET_NETWORK_STRUCT_BEGIN
156 * Message sent to confirm that a typemap was received.
158 struct TypeMapConfirmationMessage
162 * Header with type #GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP.
164 struct GNUNET_MessageHeader header;
167 * Reserved, always zero.
169 uint32_t reserved GNUNET_PACKED;
172 * Hash of the (decompressed) type map that was received.
174 struct GNUNET_HashCode tm_hash;
178 GNUNET_NETWORK_STRUCT_END
182 * Map of peer identities to `struct Session`.
184 static struct GNUNET_CONTAINER_MultiPeerMap *sessions;
188 * Find the session for the given peer.
190 * @param peer identity of the peer
191 * @return NULL if we are not connected, otherwise the
194 static struct Session *
195 find_session (const struct GNUNET_PeerIdentity *peer)
197 if (NULL == sessions)
199 return GNUNET_CONTAINER_multipeermap_get (sessions,
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 `%s'\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,
250 GNUNET_assert (GNUNET_YES ==
251 GNUNET_CONTAINER_multipeermap_remove (sessions,
254 GNUNET_STATISTICS_set (GSC_stats,
255 gettext_noop ("# peers connected"),
256 GNUNET_CONTAINER_multipeermap_size (sessions),
258 GSC_TYPEMAP_destroy (session->tmap);
259 session->tmap = NULL;
260 GNUNET_free (session);
265 * Transmit our current typemap message to the other peer.
266 * (Done periodically until the typemap is confirmed).
268 * @param cls the `struct Session *`
271 transmit_typemap_task (void *cls)
273 struct Session *session = cls;
274 struct GNUNET_MessageHeader *hdr;
275 struct GNUNET_TIME_Relative delay;
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Sending TYPEMAP to %s\n",
279 GNUNET_i2s (session->peer));
280 session->typemap_delay = GNUNET_TIME_STD_BACKOFF (session->typemap_delay);
281 delay = session->typemap_delay;
282 /* randomize a bit to avoid spont. sync */
283 delay.rel_value_us +=
284 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
286 session->typemap_task =
287 GNUNET_SCHEDULER_add_delayed (delay,
288 &transmit_typemap_task,
290 GNUNET_STATISTICS_update (GSC_stats,
291 gettext_noop ("# type map refreshes sent"),
294 hdr = GSC_TYPEMAP_compute_type_map_message ();
295 GSC_KX_encrypt_and_transmit (session->kx,
303 * Restart the typemap task for the given session.
305 * @param session session to restart typemap transmission for
308 start_typemap_task (struct Session *session)
310 if (NULL != session->typemap_task)
311 GNUNET_SCHEDULER_cancel (session->typemap_task);
312 session->typemap_delay = GNUNET_TIME_UNIT_SECONDS;
313 session->typemap_task =
314 GNUNET_SCHEDULER_add_delayed (session->typemap_delay,
315 &transmit_typemap_task,
321 * Create a session, a key exchange was just completed.
323 * @param peer peer that is now connected
324 * @param kx key exchange that completed
327 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
328 struct GSC_KeyExchangeInfo *kx)
330 struct Session *session;
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Creating session for peer `%s'\n",
335 session = GNUNET_new (struct Session);
336 session->tmap = GSC_TYPEMAP_create ();
337 session->peer = peer;
339 GNUNET_assert (GNUNET_OK ==
340 GNUNET_CONTAINER_multipeermap_put (sessions,
343 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
344 GNUNET_STATISTICS_set (GSC_stats,
345 gettext_noop ("# peers connected"),
346 GNUNET_CONTAINER_multipeermap_size (sessions),
348 GSC_CLIENTS_notify_clients_about_neighbour (peer,
351 start_typemap_task (session);
356 * The other peer has indicated that he 'lost' the session
357 * (KX down), reinitialize the session on our end, in particular
358 * this means to restart the typemap transmission.
360 * @param peer peer that is now connected
363 GSC_SESSIONS_reinit (const struct GNUNET_PeerIdentity *peer)
365 struct Session *session;
367 session = find_session (peer);
370 /* KX/session is new for both sides; thus no need to restart what
374 start_typemap_task (session);
379 * The other peer has confirmed receiving our type map,
380 * check if it is current and if so, stop retransmitting it.
382 * @param peer peer that confirmed the type map
383 * @param msg confirmation message we received
386 GSC_SESSIONS_confirm_typemap (const struct GNUNET_PeerIdentity *peer,
387 const struct GNUNET_MessageHeader *msg)
389 const struct TypeMapConfirmationMessage *cmsg;
390 struct Session *session;
392 session = find_session (peer);
398 if (ntohs (msg->size) != sizeof (struct TypeMapConfirmationMessage))
403 cmsg = (const struct TypeMapConfirmationMessage *) msg;
405 GSC_TYPEMAP_check_hash (&cmsg->tm_hash))
407 /* our typemap has changed in the meantime, do not
408 accept confirmation */
409 GNUNET_STATISTICS_update (GSC_stats,
411 ("# outdated typemap confirmations received"),
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Got outdated typemap confirmated from peer `%s'\n",
415 GNUNET_i2s (session->peer));
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Got typemap confirmation from peer `%s'\n",
420 GNUNET_i2s (session->peer));
421 if (NULL != session->typemap_task)
423 GNUNET_SCHEDULER_cancel (session->typemap_task);
424 session->typemap_task = NULL;
426 GNUNET_STATISTICS_update (GSC_stats,
428 ("# valid typemap confirmations received"),
434 * Notify the given client about the session (client is new).
436 * @param cls the `struct GSC_Client`
437 * @param key peer identity
438 * @param value the `struct Session`
439 * @return #GNUNET_OK (continue to iterate)
442 notify_client_about_session (void *cls,
443 const struct GNUNET_PeerIdentity *key,
446 struct GSC_Client *client = cls;
447 struct Session *session = value;
449 GSC_CLIENTS_notify_client_about_neighbour (client,
451 NULL, /* old TMAP: none */
458 * We have a new client, notify it about all current sessions.
460 * @param client the new client
463 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
465 /* notify new client about existing sessions */
466 GNUNET_CONTAINER_multipeermap_iterate (sessions,
467 ¬ify_client_about_session,
473 * Try to perform a transmission on the given session. Will solicit
474 * additional messages if the 'sme' queue is not full enough.
476 * @param session session to transmit messages from
479 try_transmission (struct Session *session);
483 * Queue a request from a client for transmission to a particular peer.
485 * @param car request to queue; this handle is then shared between
486 * the caller (CLIENTS subsystem) and SESSIONS and must not
487 * be released by either until either #GSC_SESSIONS_dequeue(),
488 * #GSC_SESSIONS_transmit() or #GSC_CLIENTS_failed()
489 * have been invoked on it
492 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
494 struct Session *session;
496 session = find_session (&car->target);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 "Dropped client request for transmission (am disconnected)\n");
501 GNUNET_break (0); /* should have been rejected earlier */
502 GSC_CLIENTS_reject_request (car,
506 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
509 GSC_CLIENTS_reject_request (car,
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Received client transmission request. queueing\n");
515 GNUNET_CONTAINER_DLL_insert_tail (session->active_client_request_head,
516 session->active_client_request_tail,
518 try_transmission (session);
523 * Dequeue a request from a client from transmission to a particular peer.
525 * @param car request to dequeue; this handle will then be 'owned' by
526 * the caller (CLIENTS sysbsystem)
529 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
531 struct Session *session;
534 memcmp (&car->target,
536 sizeof (struct GNUNET_PeerIdentity)))
538 session = find_session (&car->target);
539 GNUNET_assert (NULL != session);
540 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
541 session->active_client_request_tail,
543 /* dequeueing of 'high' priority messages may unblock
544 transmission for lower-priority messages, so we also
545 need to try in this case. */
546 try_transmission (session);
551 * Solicit messages for transmission, starting with those of the highest
554 * @param session session to solict messages for
555 * @param msize how many bytes do we have already
558 solicit_messages (struct Session *session,
561 struct GSC_ClientActiveRequest *car;
562 struct GSC_ClientActiveRequest *nxt;
564 enum GNUNET_CORE_Priority pmax;
567 pmax = GNUNET_CORE_PRIO_BACKGROUND;
568 for (car = session->active_client_request_head; NULL != car; car = car->next)
570 if (GNUNET_YES == car->was_solicited)
572 pmax = GNUNET_MAX (pmax, car->priority);
574 nxt = session->active_client_request_head;
575 while (NULL != (car = nxt))
578 if (car->priority < pmax)
580 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
582 so_size += car->msize;
583 if (GNUNET_YES == car->was_solicited)
585 car->was_solicited = GNUNET_YES;
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Soliciting message with priority %u\n",
589 GSC_CLIENTS_solicit_request (car);
590 /* The above call may *dequeue* requests and thereby
591 clobber 'nxt'. Hence we need to restart from the
593 nxt = session->active_client_request_head;
600 * Some messages were delayed (corked), but the timeout has now expired.
603 * @param cls `struct Session` with the messages to transmit now
606 pop_cork_task (void *cls)
608 struct Session *session = cls;
610 session->cork_task = NULL;
611 try_transmission (session);
616 * Try to perform a transmission on the given session. Will solicit
617 * additional messages if the 'sme' queue is not full enough or has
618 * only low-priority messages.
620 * @param session session to transmit messages from
623 try_transmission (struct Session *session)
625 struct SessionMessageEntry *pos;
627 struct GNUNET_TIME_Absolute now;
628 struct GNUNET_TIME_Absolute min_deadline;
629 enum GNUNET_CORE_Priority maxp;
630 enum GNUNET_CORE_Priority maxpc;
631 struct GSC_ClientActiveRequest *car;
635 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
636 /* if the peer has excess bandwidth, background traffic is allowed,
638 if (MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE <=
639 GSC_NEIGHBOURS_get_queue_length (session->kx))
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Transmission queue already very long, waiting...\n");
643 return; /* queue already too long */
645 excess = GSC_NEIGHBOURS_check_excess_bandwidth (session->kx);
646 if (GNUNET_YES == excess)
647 maxp = GNUNET_CORE_PRIO_BACKGROUND;
649 maxp = GNUNET_CORE_PRIO_BEST_EFFORT;
650 /* determine highest priority of 'ready' messages we already solicited from clients */
651 pos = session->sme_head;
652 while ((NULL != pos) &&
653 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
655 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
657 maxp = GNUNET_MAX (maxp, pos->priority);
658 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Calculating transmission set with %u priority (%s) and %s earliest deadline\n",
665 (GNUNET_YES == excess) ? "excess bandwidth" : "limited bandwidth",
666 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
669 if (maxp < GNUNET_CORE_PRIO_CRITICAL_CONTROL)
671 /* if highest already solicited priority from clients is not critical,
672 check if there are higher-priority messages to be solicited from clients */
673 if (GNUNET_YES == excess)
674 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
676 maxpc = GNUNET_CORE_PRIO_BEST_EFFORT;
677 for (car = session->active_client_request_head; NULL != car; car = car->next)
679 if (GNUNET_YES == car->was_solicited)
681 maxpc = GNUNET_MAX (maxpc,
686 /* we have messages waiting for solicitation that have a higher
687 priority than those that we already accepted; solicit the
688 high-priority messages first */
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Soliciting messages based on priority (%u > %u)\n",
693 solicit_messages (session, 0);
699 /* never solicit more, we have critical messages to process */
701 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
703 now = GNUNET_TIME_absolute_get ();
704 if ( ( (GNUNET_YES == excess) ||
705 (maxpc >= GNUNET_CORE_PRIO_BEST_EFFORT) ) &&
707 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
708 (min_deadline.abs_value_us > now.abs_value_us))) )
710 /* not enough ready yet (tiny message & cork possible), or no messages at all,
711 and either excess bandwidth or best-effort or higher message waiting at
712 client; in this case, we try to solicit more */
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Soliciting messages (excess %d, maxpc %d, message size %u, deadline %s)\n",
717 (unsigned int) msize,
718 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
720 solicit_messages (session,
724 /* if there is data to send, just not yet, make sure we do transmit
725 * it once the deadline is reached */
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Corking until %s\n",
728 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
730 if (NULL != session->cork_task)
731 GNUNET_SCHEDULER_cancel (session->cork_task);
733 = GNUNET_SCHEDULER_add_at (min_deadline,
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Queue empty, waiting for solicitations\n");
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Building combined plaintext buffer to transmit message!\n");
746 /* create plaintext buffer of all messages (that fit), encrypt and
749 static unsigned long long total_bytes;
750 static unsigned int total_msgs;
751 char pbuf[msize]; /* plaintext */
755 while ( (NULL != (pos = session->sme_head)) &&
756 (used + pos->size <= msize) )
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
759 "Adding message of type %d (%d/%d) to payload for %s\n",
760 ntohs (((const struct GNUNET_MessageHeader *)&pos[1])->type),
762 pos->is_typemap_confirm,
763 GNUNET_i2s (session->peer));
764 GNUNET_memcpy (&pbuf[used],
768 GNUNET_CONTAINER_DLL_remove (session->sme_head,
773 /* compute average payload size */
778 /* 2^32 messages, wrap around... */
782 GNUNET_STATISTICS_set (GSC_stats,
783 "# avg payload per encrypted message",
784 total_bytes / total_msgs,
786 /* now actually transmit... */
787 GSC_KX_encrypt_and_transmit (session->kx,
795 * Send an updated typemap message to the neighbour now,
796 * and restart typemap transmissions.
798 * @param cls the message
799 * @param key neighbour's identity
800 * @param value `struct Neighbour` of the target
801 * @return always #GNUNET_OK
804 do_restart_typemap_message (void *cls,
805 const struct GNUNET_PeerIdentity *key,
808 const struct GNUNET_MessageHeader *hdr = cls;
809 struct Session *session = value;
810 struct SessionMessageEntry *sme;
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Restarting sending TYPEMAP to %s\n",
815 GNUNET_i2s (session->peer));
816 size = ntohs (hdr->size);
817 for (sme = session->sme_head; NULL != sme; sme = sme->next)
819 if (GNUNET_YES == sme->is_typemap)
821 GNUNET_CONTAINER_DLL_remove (session->sme_head,
828 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
829 sme->is_typemap = GNUNET_YES;
830 GNUNET_memcpy (&sme[1],
834 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
835 GNUNET_CONTAINER_DLL_insert (session->sme_head,
838 try_transmission (session);
839 start_typemap_task (session);
845 * Broadcast an updated typemap message to all neighbours.
846 * Restarts the retransmissions until the typemaps are confirmed.
848 * @param msg message to transmit
851 GSC_SESSIONS_broadcast_typemap (const struct GNUNET_MessageHeader *msg)
853 if (NULL == sessions)
855 GNUNET_CONTAINER_multipeermap_iterate (sessions,
856 &do_restart_typemap_message,
862 * Traffic is being solicited for the given peer. This means that the
863 * message queue on the transport-level (NEIGHBOURS subsystem) is now
864 * empty and it is now OK to transmit another (non-control) message.
866 * @param pid identity of peer ready to receive data
869 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
871 struct Session *session;
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Transport solicits for %s\n",
876 session = find_session (pid);
879 try_transmission (session);
884 * Transmit a message to a particular peer.
886 * @param car original request that was queued and then solicited;
887 * this handle will now be 'owned' by the SESSIONS subsystem
888 * @param msg message to transmit
889 * @param cork is corking allowed?
890 * @param priority how important is this message
893 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
894 const struct GNUNET_MessageHeader *msg,
896 enum GNUNET_CORE_Priority priority)
898 struct Session *session;
899 struct SessionMessageEntry *sme;
900 struct SessionMessageEntry *pos;
903 session = find_session (&car->target);
906 msize = ntohs (msg->size);
907 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
908 GNUNET_memcpy (&sme[1],
912 sme->priority = priority;
913 if (GNUNET_YES == cork)
916 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Mesage corked, delaying transmission\n");
920 pos = session->sme_head;
921 while ( (NULL != pos) &&
922 (pos->priority >= sme->priority) )
925 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
929 GNUNET_CONTAINER_DLL_insert_after (session->sme_head,
933 try_transmission (session);
938 * We have received a typemap message from a peer, update ours.
939 * Notifies clients about the session.
941 * @param peer peer this is about
942 * @param msg typemap update message
945 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
946 const struct GNUNET_MessageHeader *msg)
948 struct Session *session;
949 struct GSC_TypeMap *nmap;
950 struct SessionMessageEntry *sme;
951 struct TypeMapConfirmationMessage *tmc;
953 nmap = GSC_TYPEMAP_get_from_message (msg);
957 return; /* malformed */
959 session = find_session (peer);
962 GSC_TYPEMAP_destroy (nmap);
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Received TYPEMAP from %s\n",
968 GNUNET_i2s (session->peer));
969 for (sme = session->sme_head; NULL != sme; sme = sme->next)
971 if (GNUNET_YES == sme->is_typemap_confirm)
973 GNUNET_CONTAINER_DLL_remove (session->sme_head,
980 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) +
981 sizeof (struct TypeMapConfirmationMessage));
982 sme->deadline = GNUNET_TIME_absolute_get ();
983 sme->size = sizeof (struct TypeMapConfirmationMessage);
984 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
985 sme->is_typemap_confirm = GNUNET_YES;
986 tmc = (struct TypeMapConfirmationMessage *) &sme[1];
987 tmc->header.size = htons (sizeof (struct TypeMapConfirmationMessage));
988 tmc->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP);
989 tmc->reserved = htonl (0);
990 GSC_TYPEMAP_hash (nmap,
992 GNUNET_CONTAINER_DLL_insert (session->sme_head,
995 try_transmission (session);
996 GSC_CLIENTS_notify_clients_about_neighbour (peer,
999 GSC_TYPEMAP_destroy (session->tmap);
1000 session->tmap = nmap;
1005 * The given peer send a message of the specified type. Make sure the
1006 * respective bit is set in its type-map and that clients are notified
1007 * about the session.
1009 * @param peer peer this is about
1010 * @param type type of the message
1013 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
1016 struct Session *session;
1017 struct GSC_TypeMap *nmap;
1019 if (0 == memcmp (peer,
1021 sizeof (struct GNUNET_PeerIdentity)))
1023 session = find_session (peer);
1024 GNUNET_assert (NULL != session);
1025 if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap,
1027 return; /* already in it */
1028 nmap = GSC_TYPEMAP_extend (session->tmap,
1031 GSC_CLIENTS_notify_clients_about_neighbour (peer,
1034 GSC_TYPEMAP_destroy (session->tmap);
1035 session->tmap = nmap;
1040 * Initialize sessions subsystem.
1043 GSC_SESSIONS_init ()
1045 sessions = GNUNET_CONTAINER_multipeermap_create (128,
1051 * Helper function for #GSC_SESSIONS_done() to free all
1055 * @param key identity of the connected peer
1056 * @param value the `struct Session` for the peer
1057 * @return #GNUNET_OK (continue to iterate)
1060 free_session_helper (void *cls,
1061 const struct GNUNET_PeerIdentity *key,
1064 /* struct Session *session = value; */
1066 GSC_SESSIONS_end (key);
1072 * Shutdown sessions subsystem.
1075 GSC_SESSIONS_done ()
1077 if (NULL != sessions)
1079 GNUNET_CONTAINER_multipeermap_iterate (sessions,
1080 &free_session_helper,
1082 GNUNET_CONTAINER_multipeermap_destroy (sessions);
1087 /* end of gnunet-service-core_sessions.c */