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
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_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 the neighbour queue empty and thus ready for us
151 * to transmit an encrypted message?
153 int ready_to_transmit;
156 * Is this the first time we're sending the typemap? If so,
157 * we want to send it a bit faster the second time. 0 if
158 * we are sending for the first time, 1 if not.
164 GNUNET_NETWORK_STRUCT_BEGIN
167 * Message sent to confirm that a typemap was received.
169 struct TypeMapConfirmationMessage
173 * Header with type #GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP.
175 struct GNUNET_MessageHeader header;
178 * Reserved, always zero.
180 uint32_t reserved GNUNET_PACKED;
183 * Hash of the (decompressed) type map that was received.
185 struct GNUNET_HashCode tm_hash;
189 GNUNET_NETWORK_STRUCT_END
193 * Map of peer identities to `struct Session`.
195 static struct GNUNET_CONTAINER_MultiPeerMap *sessions;
199 * Find the session for the given peer.
201 * @param peer identity of the peer
202 * @return NULL if we are not connected, otherwise the
205 static struct Session *
206 find_session (const struct GNUNET_PeerIdentity *peer)
208 if (NULL == sessions)
210 return GNUNET_CONTAINER_multipeermap_get (sessions,
216 * End the session with the given peer (we are no longer
219 * @param pid identity of peer to kill session with
222 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
224 struct Session *session;
225 struct GSC_ClientActiveRequest *car;
226 struct SessionMessageEntry *sme;
228 session = find_session (pid);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Destroying session for peer `%s'\n",
233 GNUNET_i2s (session->peer));
234 if (NULL != session->cork_task)
236 GNUNET_SCHEDULER_cancel (session->cork_task);
237 session->cork_task = NULL;
239 while (NULL != (car = session->active_client_request_head))
241 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
242 session->active_client_request_tail, car);
243 GSC_CLIENTS_reject_request (car,
246 while (NULL != (sme = session->sme_head))
248 GNUNET_CONTAINER_DLL_remove (session->sme_head,
253 if (NULL != session->typemap_task)
255 GNUNET_SCHEDULER_cancel (session->typemap_task);
256 session->typemap_task = NULL;
258 GSC_CLIENTS_notify_clients_about_neighbour (session->peer,
261 GNUNET_assert (GNUNET_YES ==
262 GNUNET_CONTAINER_multipeermap_remove (sessions,
265 GNUNET_STATISTICS_set (GSC_stats,
266 gettext_noop ("# peers connected"),
267 GNUNET_CONTAINER_multipeermap_size (sessions),
269 GSC_TYPEMAP_destroy (session->tmap);
270 session->tmap = NULL;
271 GNUNET_free (session);
276 * Transmit our current typemap message to the other peer.
277 * (Done periodically until the typemap is confirmed).
279 * @param cls the `struct Session *`
282 transmit_typemap_task (void *cls)
284 struct Session *session = cls;
285 struct GNUNET_MessageHeader *hdr;
286 struct GNUNET_TIME_Relative delay;
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Sending TYPEMAP to %s\n",
290 GNUNET_i2s (session->peer));
291 session->typemap_delay = GNUNET_TIME_STD_BACKOFF (session->typemap_delay);
292 delay = session->typemap_delay;
293 /* randomize a bit to avoid spont. sync */
294 delay.rel_value_us +=
295 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
297 session->typemap_task =
298 GNUNET_SCHEDULER_add_delayed (delay,
299 &transmit_typemap_task,
301 GNUNET_STATISTICS_update (GSC_stats,
302 gettext_noop ("# type map refreshes sent"),
305 hdr = GSC_TYPEMAP_compute_type_map_message ();
306 GSC_KX_encrypt_and_transmit (session->kx,
314 * Restart the typemap task for the given session.
316 * @param session session to restart typemap transmission for
319 start_typemap_task (struct Session *session)
321 if (NULL != session->typemap_task)
322 GNUNET_SCHEDULER_cancel (session->typemap_task);
323 session->typemap_delay = GNUNET_TIME_UNIT_SECONDS;
324 session->typemap_task =
325 GNUNET_SCHEDULER_add_delayed (session->typemap_delay,
326 &transmit_typemap_task,
332 * Create a session, a key exchange was just completed.
334 * @param peer peer that is now connected
335 * @param kx key exchange that completed
338 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
339 struct GSC_KeyExchangeInfo *kx)
341 struct Session *session;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Creating session for peer `%s'\n",
346 session = GNUNET_new (struct Session);
347 session->tmap = GSC_TYPEMAP_create ();
348 session->peer = peer;
350 GNUNET_assert (GNUNET_OK ==
351 GNUNET_CONTAINER_multipeermap_put (sessions,
354 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
355 GNUNET_STATISTICS_set (GSC_stats,
356 gettext_noop ("# peers connected"),
357 GNUNET_CONTAINER_multipeermap_size (sessions),
359 GSC_CLIENTS_notify_clients_about_neighbour (peer,
362 start_typemap_task (session);
367 * The other peer has indicated that he 'lost' the session
368 * (KX down), reinitialize the session on our end, in particular
369 * this means to restart the typemap transmission.
371 * @param peer peer that is now connected
374 GSC_SESSIONS_reinit (const struct GNUNET_PeerIdentity *peer)
376 struct Session *session;
378 session = find_session (peer);
381 /* KX/session is new for both sides; thus no need to restart what
385 start_typemap_task (session);
390 * The other peer has confirmed receiving our type map,
391 * check if it is current and if so, stop retransmitting it.
393 * @param peer peer that confirmed the type map
394 * @param msg confirmation message we received
397 GSC_SESSIONS_confirm_typemap (const struct GNUNET_PeerIdentity *peer,
398 const struct GNUNET_MessageHeader *msg)
400 const struct TypeMapConfirmationMessage *cmsg;
401 struct Session *session;
403 session = find_session (peer);
409 if (ntohs (msg->size) != sizeof (struct TypeMapConfirmationMessage))
414 cmsg = (const struct TypeMapConfirmationMessage *) msg;
416 GSC_TYPEMAP_check_hash (&cmsg->tm_hash))
418 /* our typemap has changed in the meantime, do not
419 accept confirmation */
420 GNUNET_STATISTICS_update (GSC_stats,
422 ("# outdated typemap confirmations received"),
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Got outdated typemap confirmated from peer `%s'\n",
426 GNUNET_i2s (session->peer));
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Got typemap confirmation from peer `%s'\n",
431 GNUNET_i2s (session->peer));
432 if (NULL != session->typemap_task)
434 GNUNET_SCHEDULER_cancel (session->typemap_task);
435 session->typemap_task = NULL;
437 GNUNET_STATISTICS_update (GSC_stats,
439 ("# valid typemap confirmations received"),
445 * Notify the given client about the session (client is new).
447 * @param cls the `struct GSC_Client`
448 * @param key peer identity
449 * @param value the `struct Session`
450 * @return #GNUNET_OK (continue to iterate)
453 notify_client_about_session (void *cls,
454 const struct GNUNET_PeerIdentity *key,
457 struct GSC_Client *client = cls;
458 struct Session *session = value;
460 GSC_CLIENTS_notify_client_about_neighbour (client,
462 NULL, /* old TMAP: none */
469 * We have a new client, notify it about all current sessions.
471 * @param client the new client
474 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
476 /* notify new client about existing sessions */
477 GNUNET_CONTAINER_multipeermap_iterate (sessions,
478 ¬ify_client_about_session,
484 * Try to perform a transmission on the given session. Will solicit
485 * additional messages if the 'sme' queue is not full enough.
487 * @param session session to transmit messages from
490 try_transmission (struct Session *session);
494 * Queue a request from a client for transmission to a particular peer.
496 * @param car request to queue; this handle is then shared between
497 * the caller (CLIENTS subsystem) and SESSIONS and must not
498 * be released by either until either #GSC_SESSIONS_dequeue(),
499 * #GSC_SESSIONS_transmit() or #GSC_CLIENTS_failed()
500 * have been invoked on it
503 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
505 struct Session *session;
507 session = find_session (&car->target);
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Dropped client request for transmission (am disconnected)\n");
512 GNUNET_break (0); /* should have been rejected earlier */
513 GSC_CLIENTS_reject_request (car,
517 if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
520 GSC_CLIENTS_reject_request (car,
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Received client transmission request. queueing\n");
526 GNUNET_CONTAINER_DLL_insert_tail (session->active_client_request_head,
527 session->active_client_request_tail,
529 try_transmission (session);
534 * Dequeue a request from a client from transmission to a particular peer.
536 * @param car request to dequeue; this handle will then be 'owned' by
537 * the caller (CLIENTS sysbsystem)
540 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
542 struct Session *session;
545 memcmp (&car->target,
547 sizeof (struct GNUNET_PeerIdentity)))
549 session = find_session (&car->target);
550 GNUNET_assert (NULL != session);
551 GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
552 session->active_client_request_tail,
554 /* dequeueing of 'high' priority messages may unblock
555 transmission for lower-priority messages, so we also
556 need to try in this case. */
557 try_transmission (session);
562 * Solicit messages for transmission, starting with those of the highest
565 * @param session session to solict messages for
566 * @param msize how many bytes do we have already
569 solicit_messages (struct Session *session,
572 struct GSC_ClientActiveRequest *car;
573 struct GSC_ClientActiveRequest *nxt;
575 enum GNUNET_CORE_Priority pmax;
578 pmax = GNUNET_CORE_PRIO_BACKGROUND;
579 for (car = session->active_client_request_head; NULL != car; car = car->next)
581 if (GNUNET_YES == car->was_solicited)
583 pmax = GNUNET_MAX (pmax, car->priority);
585 nxt = session->active_client_request_head;
586 while (NULL != (car = nxt))
589 if (car->priority < pmax)
591 if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
593 so_size += car->msize;
594 if (GNUNET_YES == car->was_solicited)
596 car->was_solicited = GNUNET_YES;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Soliciting message with priority %u\n",
600 GSC_CLIENTS_solicit_request (car);
601 /* The above call may *dequeue* requests and thereby
602 clobber 'nxt'. Hence we need to restart from the
604 nxt = session->active_client_request_head;
611 * Some messages were delayed (corked), but the timeout has now expired.
614 * @param cls `struct Session` with the messages to transmit now
617 pop_cork_task (void *cls)
619 struct Session *session = cls;
621 session->cork_task = NULL;
622 try_transmission (session);
627 * Try to perform a transmission on the given session. Will solicit
628 * additional messages if the 'sme' queue is not full enough or has
629 * only low-priority messages.
631 * @param session session to transmit messages from
634 try_transmission (struct Session *session)
636 struct SessionMessageEntry *pos;
638 struct GNUNET_TIME_Absolute now;
639 struct GNUNET_TIME_Absolute min_deadline;
640 enum GNUNET_CORE_Priority maxp;
641 enum GNUNET_CORE_Priority maxpc;
642 struct GSC_ClientActiveRequest *car;
645 if (GNUNET_YES != session->ready_to_transmit)
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "Not yet ready to transmit, not evaluating queue\n");
652 min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
653 /* if the peer has excess bandwidth, background traffic is allowed,
655 if (MAX_ENCRYPTED_MESSAGE_QUEUE_SIZE <=
656 GSC_NEIGHBOURS_get_queue_length (session->kx))
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Transmission queue already very long, waiting...\n");
660 return; /* queue already too long */
662 excess = GSC_NEIGHBOURS_check_excess_bandwidth (session->kx);
663 if (GNUNET_YES == excess)
664 maxp = GNUNET_CORE_PRIO_BACKGROUND;
666 maxp = GNUNET_CORE_PRIO_BEST_EFFORT;
667 /* determine highest priority of 'ready' messages we already solicited from clients */
668 pos = session->sme_head;
669 while ((NULL != pos) &&
670 (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
672 GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
674 maxp = GNUNET_MAX (maxp, pos->priority);
675 min_deadline = GNUNET_TIME_absolute_min (min_deadline,
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Calculating transmission set with %u priority (%s) and %s earliest deadline\n",
682 (GNUNET_YES == excess) ? "excess bandwidth" : "limited bandwidth",
683 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
686 if (maxp < GNUNET_CORE_PRIO_CRITICAL_CONTROL)
688 /* if highest already solicited priority from clients is not critical,
689 check if there are higher-priority messages to be solicited from clients */
690 if (GNUNET_YES == excess)
691 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
693 maxpc = GNUNET_CORE_PRIO_BEST_EFFORT;
694 for (car = session->active_client_request_head; NULL != car; car = car->next)
696 if (GNUNET_YES == car->was_solicited)
698 maxpc = GNUNET_MAX (maxpc,
703 /* we have messages waiting for solicitation that have a higher
704 priority than those that we already accepted; solicit the
705 high-priority messages first */
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "Soliciting messages based on priority (%u > %u)\n",
710 solicit_messages (session, 0);
716 /* never solicit more, we have critical messages to process */
718 maxpc = GNUNET_CORE_PRIO_BACKGROUND;
720 now = GNUNET_TIME_absolute_get ();
721 if ( ( (GNUNET_YES == excess) ||
722 (maxpc >= GNUNET_CORE_PRIO_BEST_EFFORT) ) &&
724 ( (msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
725 (min_deadline.abs_value_us > now.abs_value_us))) )
727 /* not enough ready yet (tiny message & cork possible), or no messages at all,
728 and either excess bandwidth or best-effort or higher message waiting at
729 client; in this case, we try to solicit more */
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Soliciting messages (excess %d, maxpc %d, message size %u, deadline %s)\n",
734 (unsigned int) msize,
735 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
737 solicit_messages (session,
741 /* if there is data to send, just not yet, make sure we do transmit
742 * it once the deadline is reached */
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "Corking until %s\n",
745 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_deadline),
747 if (NULL != session->cork_task)
748 GNUNET_SCHEDULER_cancel (session->cork_task);
750 = GNUNET_SCHEDULER_add_at (min_deadline,
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Queue empty, waiting for solicitations\n");
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Building combined plaintext buffer to transmit message!\n");
763 /* create plaintext buffer of all messages (that fit), encrypt and
766 static unsigned long long total_bytes;
767 static unsigned int total_msgs;
768 char pbuf[msize]; /* plaintext */
772 while ( (NULL != (pos = session->sme_head)) &&
773 (used + pos->size <= msize) )
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Adding message of type %d (%d/%d) to payload for %s\n",
777 ntohs (((const struct GNUNET_MessageHeader *)&pos[1])->type),
779 pos->is_typemap_confirm,
780 GNUNET_i2s (session->peer));
781 GNUNET_memcpy (&pbuf[used],
785 GNUNET_CONTAINER_DLL_remove (session->sme_head,
790 /* compute average payload size */
795 /* 2^32 messages, wrap around... */
799 GNUNET_STATISTICS_set (GSC_stats,
800 "# avg payload per encrypted message",
801 total_bytes / total_msgs,
803 /* now actually transmit... */
804 session->ready_to_transmit = GNUNET_NO;
805 GSC_KX_encrypt_and_transmit (session->kx,
813 * Send an updated typemap message to the neighbour now,
814 * and restart typemap transmissions.
816 * @param cls the message
817 * @param key neighbour's identity
818 * @param value `struct Neighbour` of the target
819 * @return always #GNUNET_OK
822 do_restart_typemap_message (void *cls,
823 const struct GNUNET_PeerIdentity *key,
826 const struct GNUNET_MessageHeader *hdr = cls;
827 struct Session *session = value;
828 struct SessionMessageEntry *sme;
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Restarting sending TYPEMAP to %s\n",
833 GNUNET_i2s (session->peer));
834 size = ntohs (hdr->size);
835 for (sme = session->sme_head; NULL != sme; sme = sme->next)
837 if (GNUNET_YES == sme->is_typemap)
839 GNUNET_CONTAINER_DLL_remove (session->sme_head,
846 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
847 sme->is_typemap = GNUNET_YES;
848 GNUNET_memcpy (&sme[1],
852 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
853 GNUNET_CONTAINER_DLL_insert (session->sme_head,
856 try_transmission (session);
857 start_typemap_task (session);
863 * Broadcast an updated typemap message to all neighbours.
864 * Restarts the retransmissions until the typemaps are confirmed.
866 * @param msg message to transmit
869 GSC_SESSIONS_broadcast_typemap (const struct GNUNET_MessageHeader *msg)
871 if (NULL == sessions)
873 GNUNET_CONTAINER_multipeermap_iterate (sessions,
874 &do_restart_typemap_message,
880 * Traffic is being solicited for the given peer. This means that the
881 * message queue on the transport-level (NEIGHBOURS subsystem) is now
882 * empty and it is now OK to transmit another (non-control) message.
884 * @param pid identity of peer ready to receive data
887 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
889 struct Session *session;
891 session = find_session (pid);
894 session->ready_to_transmit = GNUNET_YES;
895 try_transmission (session);
900 * Transmit a message to a particular peer.
902 * @param car original request that was queued and then solicited;
903 * this handle will now be 'owned' by the SESSIONS subsystem
904 * @param msg message to transmit
905 * @param cork is corking allowed?
906 * @param priority how important is this message
909 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
910 const struct GNUNET_MessageHeader *msg,
912 enum GNUNET_CORE_Priority priority)
914 struct Session *session;
915 struct SessionMessageEntry *sme;
916 struct SessionMessageEntry *pos;
919 session = find_session (&car->target);
922 msize = ntohs (msg->size);
923 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
924 GNUNET_memcpy (&sme[1],
928 sme->priority = priority;
929 if (GNUNET_YES == cork)
932 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Mesage corked, delaying transmission\n");
936 pos = session->sme_head;
937 while ( (NULL != pos) &&
938 (pos->priority >= sme->priority) )
941 GNUNET_CONTAINER_DLL_insert_tail (session->sme_head,
945 GNUNET_CONTAINER_DLL_insert_after (session->sme_head,
949 try_transmission (session);
954 * We have received a typemap message from a peer, update ours.
955 * Notifies clients about the session.
957 * @param peer peer this is about
958 * @param msg typemap update message
961 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
962 const struct GNUNET_MessageHeader *msg)
964 struct Session *session;
965 struct GSC_TypeMap *nmap;
966 struct SessionMessageEntry *sme;
967 struct TypeMapConfirmationMessage *tmc;
969 nmap = GSC_TYPEMAP_get_from_message (msg);
973 return; /* malformed */
975 session = find_session (peer);
978 GSC_TYPEMAP_destroy (nmap);
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Received TYPEMAP from %s\n",
984 GNUNET_i2s (session->peer));
985 for (sme = session->sme_head; NULL != sme; sme = sme->next)
987 if (GNUNET_YES == sme->is_typemap_confirm)
989 GNUNET_CONTAINER_DLL_remove (session->sme_head,
996 sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) +
997 sizeof (struct TypeMapConfirmationMessage));
998 sme->deadline = GNUNET_TIME_absolute_get ();
999 sme->size = sizeof (struct TypeMapConfirmationMessage);
1000 sme->priority = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
1001 sme->is_typemap_confirm = GNUNET_YES;
1002 tmc = (struct TypeMapConfirmationMessage *) &sme[1];
1003 tmc->header.size = htons (sizeof (struct TypeMapConfirmationMessage));
1004 tmc->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP);
1005 tmc->reserved = htonl (0);
1006 GSC_TYPEMAP_hash (nmap,
1008 GNUNET_CONTAINER_DLL_insert (session->sme_head,
1011 try_transmission (session);
1012 GSC_CLIENTS_notify_clients_about_neighbour (peer,
1015 GSC_TYPEMAP_destroy (session->tmap);
1016 session->tmap = nmap;
1021 * The given peer send a message of the specified type. Make sure the
1022 * respective bit is set in its type-map and that clients are notified
1023 * about the session.
1025 * @param peer peer this is about
1026 * @param type type of the message
1029 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
1032 struct Session *session;
1033 struct GSC_TypeMap *nmap;
1035 if (0 == memcmp (peer,
1037 sizeof (struct GNUNET_PeerIdentity)))
1039 session = find_session (peer);
1040 GNUNET_assert (NULL != session);
1041 if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap,
1043 return; /* already in it */
1044 nmap = GSC_TYPEMAP_extend (session->tmap,
1047 GSC_CLIENTS_notify_clients_about_neighbour (peer,
1050 GSC_TYPEMAP_destroy (session->tmap);
1051 session->tmap = nmap;
1056 * Initialize sessions subsystem.
1059 GSC_SESSIONS_init ()
1061 sessions = GNUNET_CONTAINER_multipeermap_create (128,
1067 * Helper function for #GSC_SESSIONS_done() to free all
1071 * @param key identity of the connected peer
1072 * @param value the `struct Session` for the peer
1073 * @return #GNUNET_OK (continue to iterate)
1076 free_session_helper (void *cls,
1077 const struct GNUNET_PeerIdentity *key,
1080 /* struct Session *session = value; */
1082 GSC_SESSIONS_end (key);
1088 * Shutdown sessions subsystem.
1091 GSC_SESSIONS_done ()
1093 if (NULL != sessions)
1095 GNUNET_CONTAINER_multipeermap_iterate (sessions,
1096 &free_session_helper,
1098 GNUNET_CONTAINER_multipeermap_destroy (sessions);
1103 /* end of gnunet-service-core_sessions.c */