curly wars / auto-indentation
[oweals/gnunet.git] / src / core / gnunet-service-core_sessions.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file core/gnunet-service-core_sessions.c
23  * @brief code for managing of 'encrypted' sessions (key exchange done)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
34
35 /**
36  * How often do we transmit our typemap?
37  */
38 #define TYPEMAP_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
39
40
41 /**
42  * Message ready for encryption.  This struct is followed by the
43  * actual content of the message.
44  */
45 struct SessionMessageEntry
46 {
47
48   /**
49    * We keep messages in a doubly linked list.
50    */
51   struct SessionMessageEntry *next;
52
53   /**
54    * We keep messages in a doubly linked list.
55    */
56   struct SessionMessageEntry *prev;
57
58   /**
59    * Deadline for transmission, 1s after we received it (if we
60    * are not corking), otherwise "now".  Note that this message
61    * does NOT expire past its deadline.
62    */
63   struct GNUNET_TIME_Absolute deadline;
64
65   /**
66    * How long is the message? (number of bytes following the "struct
67    * MessageEntry", but not including the size of "struct
68    * MessageEntry" itself!)
69    */
70   size_t size;
71
72 };
73
74
75 /**
76  * Data kept per session.
77  */
78 struct Session
79 {
80   /**
81    * Identity of the other peer.
82    */
83   struct GNUNET_PeerIdentity peer;
84
85   /**
86    * Head of list of requests from clients for transmission to
87    * this peer.
88    */
89   struct GSC_ClientActiveRequest *active_client_request_head;
90
91   /**
92    * Tail of list of requests from clients for transmission to
93    * this peer.
94    */
95   struct GSC_ClientActiveRequest *active_client_request_tail;
96
97   /**
98    * Head of list of messages ready for encryption.
99    */
100   struct SessionMessageEntry *sme_head;
101
102   /**
103    * Tail of list of messages ready for encryption.
104    */
105   struct SessionMessageEntry *sme_tail;
106
107   /**
108    * Information about the key exchange with the other peer.
109    */
110   struct GSC_KeyExchangeInfo *kxinfo;
111
112   /**
113    * Current type map for this peer.
114    */
115   struct GSC_TypeMap *tmap;
116
117   /**
118    * At what time did we initially establish this session?
119    * (currently unused, should be integrated with ATS in the
120    * future...).
121    */
122   struct GNUNET_TIME_Absolute time_established;
123
124   /**
125    * Task to transmit corked messages with a delay.
126    */
127   GNUNET_SCHEDULER_TaskIdentifier cork_task;
128
129   /**
130    * Task to transmit our type map.
131    */
132   GNUNET_SCHEDULER_TaskIdentifier typemap_task;
133
134   /**
135    * Is the neighbour queue empty and thus ready for us
136    * to transmit an encrypted message?
137    */
138   int ready_to_transmit;
139
140 };
141
142
143 /**
144  * Map of peer identities to 'struct Session'.
145  */
146 static struct GNUNET_CONTAINER_MultiHashMap *sessions;
147
148
149 /**
150  * Find the session for the given peer.
151  *
152  * @param peer identity of the peer
153  * @return NULL if we are not connected, otherwise the
154  *         session handle
155  */
156 static struct Session *
157 find_session (const struct GNUNET_PeerIdentity *peer)
158 {
159   return GNUNET_CONTAINER_multihashmap_get (sessions, &peer->hashPubKey);
160 }
161
162
163 /**
164  * End the session with the given peer (we are no longer
165  * connected).
166  *
167  * @param pid identity of peer to kill session with
168  */
169 void
170 GSC_SESSIONS_end (const struct GNUNET_PeerIdentity *pid)
171 {
172   struct Session *session;
173   struct GSC_ClientActiveRequest *car;
174
175   session = find_session (pid);
176   if (NULL == session)
177     return;
178 #if DEBUG_CORE
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying session for peer `%4s'\n",
180               GNUNET_i2s (&session->peer));
181 #endif
182   if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
183   {
184     GNUNET_SCHEDULER_cancel (session->cork_task);
185     session->cork_task = GNUNET_SCHEDULER_NO_TASK;
186   }
187   while (NULL != (car = session->active_client_request_head))
188   {
189     GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
190                                  session->active_client_request_tail, car);
191     GSC_CLIENTS_reject_request (car);
192   }
193   GNUNET_SCHEDULER_cancel (session->typemap_task);
194   GSC_CLIENTS_notify_clients_about_neighbour (&session->peer, NULL,
195                                               0 /* FIXME: ATSI */ ,
196                                               session->tmap, NULL);
197   GNUNET_assert (GNUNET_YES ==
198                  GNUNET_CONTAINER_multihashmap_remove (sessions,
199                                                        &session->
200                                                        peer.hashPubKey,
201                                                        session));
202   GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# entries in session map"),
203                          GNUNET_CONTAINER_multihashmap_size (sessions),
204                          GNUNET_NO);
205   GSC_TYPEMAP_destroy (session->tmap);
206   session->tmap = NULL;
207   GNUNET_free (session);
208 }
209
210
211 /**
212  * Transmit our current typemap message to the other peer.
213  * (Done periodically in case an update got lost).
214  *
215  * @param cls the 'struct Session*'
216  * @param tc unused
217  */
218 static void
219 transmit_typemap_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
220 {
221   struct Session *session = cls;
222   struct GNUNET_MessageHeader *hdr;
223   struct GNUNET_TIME_Relative delay;
224
225   delay = TYPEMAP_FREQUENCY;
226   /* randomize a bit to avoid spont. sync */
227   delay.rel_value +=
228       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
229   session->typemap_task =
230       GNUNET_SCHEDULER_add_delayed (delay, &transmit_typemap_task, session);
231   GNUNET_STATISTICS_update (GSC_stats,
232                             gettext_noop ("# type map refreshes sent"), 1,
233                             GNUNET_NO);
234   hdr = GSC_TYPEMAP_compute_type_map_message ();
235   GSC_KX_encrypt_and_transmit (session->kxinfo, hdr, ntohs (hdr->size));
236   GNUNET_free (hdr);
237 }
238
239
240 /**
241  * Create a session, a key exchange was just completed.
242  *
243  * @param peer peer that is now connected
244  * @param kx key exchange that completed
245  */
246 void
247 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
248                      struct GSC_KeyExchangeInfo *kx)
249 {
250   struct Session *session;
251
252 #if DEBUG_CORE
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating session for peer `%4s'\n",
254               GNUNET_i2s (peer));
255 #endif
256   session = GNUNET_malloc (sizeof (struct Session));
257   session->tmap = GSC_TYPEMAP_create ();
258   session->peer = *peer;
259   session->kxinfo = kx;
260   session->time_established = GNUNET_TIME_absolute_get ();
261   session->typemap_task =
262       GNUNET_SCHEDULER_add_now (&transmit_typemap_task, session);
263   GNUNET_assert (GNUNET_OK ==
264                  GNUNET_CONTAINER_multihashmap_put (sessions, &peer->hashPubKey,
265                                                     session,
266                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
267   GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# entries in session map"),
268                          GNUNET_CONTAINER_multihashmap_size (sessions),
269                          GNUNET_NO);
270   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0 /* FIXME: ATSI */ ,
271                                               NULL, session->tmap);
272 }
273
274
275 /**
276  * Notify the given client about the session (client is new).
277  *
278  * @param cls the 'struct GSC_Client'
279  * @param key peer identity
280  * @param value the 'struct Session'
281  * @return GNUNET_OK (continue to iterate)
282  */
283 static int
284 notify_client_about_session (void *cls, const GNUNET_HashCode * key,
285                              void *value)
286 {
287   struct GSC_Client *client = cls;
288   struct Session *session = value;
289
290   GSC_CLIENTS_notify_client_about_neighbour (client, &session->peer, NULL, 0,   /* FIXME: ATS!? */
291                                              NULL,      /* old TMAP: none */
292                                              session->tmap);
293   return GNUNET_OK;
294 }
295
296
297 /**
298  * We have a new client, notify it about all current sessions.
299  *
300  * @param client the new client
301  */
302 void
303 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
304 {
305   /* notify new client about existing sessions */
306   GNUNET_CONTAINER_multihashmap_iterate (sessions, &notify_client_about_session,
307                                          client);
308 }
309
310
311 /**
312  * Try to perform a transmission on the given session.  Will solicit
313  * additional messages if the 'sme' queue is not full enough.
314  *
315  * @param session session to transmit messages from
316  */
317 static void
318 try_transmission (struct Session *session);
319
320
321 /**
322  * Queue a request from a client for transmission to a particular peer.
323  *
324  * @param car request to queue; this handle is then shared between
325  *         the caller (CLIENTS subsystem) and SESSIONS and must not
326  *         be released by either until either 'GNUNET_SESSIONS_dequeue',
327  *         'GNUNET_SESSIONS_transmit' or 'GNUNET_CLIENTS_failed'
328  *         have been invoked on it
329  */
330 void
331 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
332 {
333   struct Session *session;
334
335   session = find_session (&car->target);
336   if (session == NULL)
337   {
338     /* neighbour must have disconnected since request was issued,
339      * ignore (client will realize it once it processes the
340      * disconnect notification) */
341 #if DEBUG_CORE
342     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343                 "Dropped client request for transmission (am disconnected)\n");
344 #endif
345     GNUNET_STATISTICS_update (GSC_stats,
346                               gettext_noop
347                               ("# send requests dropped (disconnected)"), 1,
348                               GNUNET_NO);
349     GSC_CLIENTS_reject_request (car);
350     return;
351   }
352   if (car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
353   {
354     GNUNET_break (0);
355     GSC_CLIENTS_reject_request (car);
356     return;
357   }
358 #if DEBUG_CORE
359   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360               "Received client transmission request. queueing\n");
361 #endif
362   GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
363                                session->active_client_request_tail, car);
364   try_transmission (session);
365 }
366
367
368 /**
369  * Dequeue a request from a client from transmission to a particular peer.
370  *
371  * @param car request to dequeue; this handle will then be 'owned' by
372  *        the caller (CLIENTS sysbsystem)
373  */
374 void
375 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
376 {
377   struct Session *s;
378
379   if (0 ==
380       memcmp (&car->target, &GSC_my_identity,
381               sizeof (struct GNUNET_PeerIdentity)))
382     return;
383   s = find_session (&car->target);
384   GNUNET_assert (NULL != s);
385   GNUNET_CONTAINER_DLL_remove (s->active_client_request_head,
386                                s->active_client_request_tail, car);
387 }
388
389
390 /**
391  * Discard all expired active transmission requests from clients.
392  *
393  * @param session session to clean up
394  */
395 static void
396 discard_expired_requests (struct Session *session)
397 {
398   struct GSC_ClientActiveRequest *pos;
399   struct GSC_ClientActiveRequest *nxt;
400   struct GNUNET_TIME_Absolute now;
401
402   now = GNUNET_TIME_absolute_get ();
403   pos = NULL;
404   nxt = session->active_client_request_head;
405   while (NULL != nxt)
406   {
407     pos = nxt;
408     nxt = pos->next;
409     if ((pos->deadline.abs_value < now.abs_value) &&
410         (GNUNET_YES != pos->was_solicited))
411     {
412       GNUNET_STATISTICS_update (GSC_stats,
413                                 gettext_noop
414                                 ("# messages discarded (expired prior to transmission)"),
415                                 1, GNUNET_NO);
416       GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
417                                    session->active_client_request_tail, pos);
418       GSC_CLIENTS_reject_request (pos);
419     }
420   }
421 }
422
423
424 /**
425  * Solicit messages for transmission.
426  *
427  * @param session session to solict messages for
428  */
429 static void
430 solicit_messages (struct Session *session)
431 {
432   struct GSC_ClientActiveRequest *car;
433   size_t so_size;
434
435   discard_expired_requests (session);
436   so_size = 0;
437   for (car = session->active_client_request_head; NULL != car; car = car->next)
438   {
439     if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
440       break;
441     so_size += car->msize;
442     if (car->was_solicited == GNUNET_YES)
443       continue;
444     car->was_solicited = GNUNET_YES;
445     GSC_CLIENTS_solicit_request (car);
446   }
447 }
448
449
450 /**
451  * Some messages were delayed (corked), but the timeout has now expired.
452  * Send them now.
453  *
454  * @param cls 'struct Session' with the messages to transmit now
455  * @param tc scheduler context (unused)
456  */
457 static void
458 pop_cork_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
459 {
460   struct Session *session = cls;
461
462   session->cork_task = GNUNET_SCHEDULER_NO_TASK;
463   try_transmission (session);
464 }
465
466
467 /**
468  * Try to perform a transmission on the given session. Will solicit
469  * additional messages if the 'sme' queue is not full enough.
470  *
471  * @param session session to transmit messages from
472  */
473 static void
474 try_transmission (struct Session *session)
475 {
476   struct SessionMessageEntry *pos;
477   size_t msize;
478   struct GNUNET_TIME_Absolute now;
479   struct GNUNET_TIME_Absolute min_deadline;
480
481   if (GNUNET_YES != session->ready_to_transmit)
482     return;
483   msize = 0;
484   min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
485   /* check 'ready' messages */
486   pos = session->sme_head;
487   while ((NULL != pos) &&
488          (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
489   {
490     GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
491     msize += pos->size;
492     min_deadline = GNUNET_TIME_absolute_min (min_deadline, pos->deadline);
493     pos = pos->next;
494   }
495   now = GNUNET_TIME_absolute_get ();
496   if ((msize == 0) ||
497       ((msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
498        (min_deadline.abs_value > now.abs_value)))
499   {
500     /* not enough ready yet, try to solicit more */
501     solicit_messages (session);
502     if (msize > 0)
503     {
504       /* if there is data to send, just not yet, make sure we do transmit
505        * it once the deadline is reached */
506       if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
507         GNUNET_SCHEDULER_cancel (session->cork_task);
508       session->cork_task =
509           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
510                                         (min_deadline), &pop_cork_task,
511                                         session);
512     }
513     return;
514   }
515   /* create plaintext buffer of all messages, encrypt and transmit */
516   {
517     static unsigned long long total_bytes;
518     static unsigned int total_msgs;
519     char pbuf[msize];           /* plaintext */
520     size_t used;
521
522     used = 0;
523     while ((NULL != (pos = session->sme_head)) && (used + pos->size <= msize))
524     {
525       memcpy (&pbuf[used], &pos[1], pos->size);
526       used += pos->size;
527       GNUNET_CONTAINER_DLL_remove (session->sme_head, session->sme_tail, pos);
528       GNUNET_free (pos);
529     }
530     /* compute average payload size */
531     total_bytes += used;
532     total_msgs++;
533     if (0 == total_msgs)
534     {
535       /* 2^32 messages, wrap around... */
536       total_msgs = 1;
537       total_bytes = used;
538     }
539     GNUNET_STATISTICS_set (GSC_stats, "# avg payload per encrypted message",
540                            total_bytes / total_msgs, GNUNET_NO);
541     /* now actually transmit... */
542     session->ready_to_transmit = GNUNET_NO;
543     GSC_KX_encrypt_and_transmit (session->kxinfo, pbuf, used);
544   }
545 }
546
547
548 /**
549  * Send a message to the neighbour now.
550  *
551  * @param cls the message
552  * @param key neighbour's identity
553  * @param value 'struct Neighbour' of the target
554  * @return always GNUNET_OK
555  */
556 static int
557 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
558 {
559   const struct GNUNET_MessageHeader *hdr = cls;
560   struct Session *session = value;
561   struct SessionMessageEntry *m;
562   uint16_t size;
563
564   size = ntohs (hdr->size);
565   m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
566   memcpy (&m[1], hdr, size);
567   m->size = size;
568   GNUNET_CONTAINER_DLL_insert (session->sme_head, session->sme_tail, m);
569   try_transmission (session);
570   return GNUNET_OK;
571 }
572
573
574 /**
575  * Broadcast a message to all neighbours.
576  *
577  * @param msg message to transmit
578  */
579 void
580 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
581 {
582   if (NULL == sessions)
583     return;
584   GNUNET_CONTAINER_multihashmap_iterate (sessions, &do_send_message,
585                                          (void *) msg);
586 }
587
588
589 /**
590  * Traffic is being solicited for the given peer.  This means that the
591  * message queue on the transport-level (NEIGHBOURS subsystem) is now
592  * empty and it is now OK to transmit another (non-control) message.
593  *
594  * @param pid identity of peer ready to receive data
595  */
596 void
597 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
598 {
599   struct Session *session;
600
601   session = find_session (pid);
602   if (NULL == session)
603     return;
604   session->ready_to_transmit = GNUNET_YES;
605   try_transmission (session);
606 }
607
608
609 /**
610  * Transmit a message to a particular peer.
611  *
612  * @param car original request that was queued and then solicited;
613  *            this handle will now be 'owned' by the SESSIONS subsystem
614  * @param msg message to transmit
615  * @param cork is corking allowed?
616  */
617 void
618 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
619                        const struct GNUNET_MessageHeader *msg, int cork)
620 {
621   struct Session *session;
622   struct SessionMessageEntry *sme;
623   size_t msize;
624
625   session = find_session (&car->target);
626   if (NULL == session)
627     return;
628   msize = ntohs (msg->size);
629   sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
630   memcpy (&sme[1], msg, msize);
631   sme->size = msize;
632   if (GNUNET_YES == cork)
633     sme->deadline =
634         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
635   GNUNET_CONTAINER_DLL_insert_tail (session->sme_head, session->sme_tail, sme);
636   try_transmission (session);
637 }
638
639
640 /**
641  * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
642  *
643  * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
644  * @param key identity of the connected peer
645  * @param value the 'struct Neighbour' for the peer
646  * @return GNUNET_OK (continue to iterate)
647  */
648 #include "core.h"
649 static int
650 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
651 {
652   struct GNUNET_SERVER_TransmitContext *tc = cls;
653   struct Session *session = value;
654   struct ConnectNotifyMessage cnm;
655
656   /* FIXME: code duplication with clients... */
657   cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
658   cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
659   // FIXME: full ats...
660   cnm.ats_count = htonl (0);
661   cnm.peer = session->peer;
662   GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
663   return GNUNET_OK;
664 }
665
666
667 /**
668  * Handle CORE_ITERATE_PEERS request. For this request type, the client
669  * does not have to have transmitted an INIT request.  All current peers
670  * are returned, regardless of which message types they accept.
671  *
672  * @param cls unused
673  * @param client client sending the iteration request
674  * @param message iteration request message
675  */
676 void
677 GSC_SESSIONS_handle_client_iterate_peers (void *cls,
678                                           struct GNUNET_SERVER_Client *client,
679                                           const struct GNUNET_MessageHeader
680                                           *message)
681 {
682   struct GNUNET_MessageHeader done_msg;
683   struct GNUNET_SERVER_TransmitContext *tc;
684
685   tc = GNUNET_SERVER_transmit_context_create (client);
686   GNUNET_CONTAINER_multihashmap_iterate (sessions, &queue_connect_message, tc);
687   done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
688   done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
689   GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
690   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
691 }
692
693
694 /**
695  * Handle CORE_PEER_CONNECTED request.   Notify client about connection
696  * to the given neighbour.  For this request type, the client does not
697  * have to have transmitted an INIT request.  All current peers are
698  * returned, regardless of which message types they accept.
699  *
700  * @param cls unused
701  * @param client client sending the iteration request
702  * @param message iteration request message
703  */
704 void
705 GSC_SESSIONS_handle_client_have_peer (void *cls,
706                                       struct GNUNET_SERVER_Client *client,
707                                       const struct GNUNET_MessageHeader
708                                       *message)
709 {
710   struct GNUNET_MessageHeader done_msg;
711   struct GNUNET_SERVER_TransmitContext *tc;
712   const struct GNUNET_PeerIdentity *peer;
713
714   peer = (const struct GNUNET_PeerIdentity *) &message[1];      // YUCK!
715   tc = GNUNET_SERVER_transmit_context_create (client);
716   GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
717                                               &queue_connect_message, tc);
718   done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
719   done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
720   GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
721   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
722 }
723
724
725 /**
726  * We've received a typemap message from a peer, update ours.
727  * Notifies clients about the session.
728  *
729  * @param peer peer this is about
730  * @param msg typemap update message
731  */
732 void
733 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
734                           const struct GNUNET_MessageHeader *msg)
735 {
736   struct Session *session;
737   struct GSC_TypeMap *nmap;
738
739   nmap = GSC_TYPEMAP_get_from_message (msg);
740   if (NULL == nmap)
741     return;                     /* malformed */
742   session = find_session (peer);
743   if (NULL == session)
744   {
745     GNUNET_break (0);
746     return;
747   }
748   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0,    /* FIXME: ATS */
749                                               session->tmap, nmap);
750   GSC_TYPEMAP_destroy (session->tmap);
751   session->tmap = nmap;
752 }
753
754
755 /**
756  * The given peer send a message of the specified type.  Make sure the
757  * respective bit is set in its type-map and that clients are notified
758  * about the session.
759  *
760  * @param peer peer this is about
761  * @param type type of the message
762  */
763 void
764 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
765                              uint16_t type)
766 {
767   struct Session *session;
768   struct GSC_TypeMap *nmap;
769
770   if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
771     return;
772   session = find_session (peer);
773   GNUNET_assert (NULL != session);
774   if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap, &type, 1))
775     return;                     /* already in it */
776   nmap = GSC_TYPEMAP_extend (session->tmap, &type, 1);
777   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0,    /* FIXME: ATS */
778                                               session->tmap, nmap);
779   GSC_TYPEMAP_destroy (session->tmap);
780   session->tmap = nmap;
781 }
782
783
784 /**
785  * Initialize sessions subsystem.
786  */
787 void
788 GSC_SESSIONS_init ()
789 {
790   sessions = GNUNET_CONTAINER_multihashmap_create (128);
791 }
792
793
794 /**
795  * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
796  *
797  * @param cls NULL
798  * @param key identity of the connected peer
799  * @param value the 'struct Session' for the peer
800  * @return GNUNET_OK (continue to iterate)
801  */
802 static int
803 free_session_helper (void *cls, const GNUNET_HashCode * key, void *value)
804 {
805   struct Session *session = value;
806
807   GSC_SESSIONS_end (&session->peer);
808   return GNUNET_OK;
809 }
810
811
812 /**
813  * Shutdown sessions subsystem.
814  */
815 void
816 GSC_SESSIONS_done ()
817 {
818   GNUNET_CONTAINER_multihashmap_iterate (sessions, &free_session_helper, NULL);
819   GNUNET_CONTAINER_multihashmap_destroy (sessions);
820   sessions = NULL;
821 }
822
823 /* end of gnunet-service-core_sessions.c */