-code cleanup
[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   struct SessionMessageEntry *sme;
175
176   session = find_session (pid);
177   if (NULL == session)
178     return;
179 #if DEBUG_CORE
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying session for peer `%4s'\n",
181               GNUNET_i2s (&session->peer));
182 #endif
183   if (GNUNET_SCHEDULER_NO_TASK != session->cork_task)
184   {
185     GNUNET_SCHEDULER_cancel (session->cork_task);
186     session->cork_task = GNUNET_SCHEDULER_NO_TASK;
187   }
188   while (NULL != (car = session->active_client_request_head))
189   {
190     GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
191                                  session->active_client_request_tail, car);
192     GSC_CLIENTS_reject_request (car);
193   }
194   while (NULL != (sme = session->sme_head))
195   {
196     GNUNET_CONTAINER_DLL_remove (session->sme_head, session->sme_tail, sme);
197     GNUNET_free (sme);
198   }
199   GNUNET_SCHEDULER_cancel (session->typemap_task);
200   GSC_CLIENTS_notify_clients_about_neighbour (&session->peer, NULL,
201                                               0 /* FIXME: ATSI */ ,
202                                               session->tmap, NULL);
203   GNUNET_assert (GNUNET_YES ==
204                  GNUNET_CONTAINER_multihashmap_remove (sessions,
205                                                        &session->
206                                                        peer.hashPubKey,
207                                                        session));
208   GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# entries in session map"),
209                          GNUNET_CONTAINER_multihashmap_size (sessions),
210                          GNUNET_NO);
211   GSC_TYPEMAP_destroy (session->tmap);
212   session->tmap = NULL;
213   GNUNET_free (session);
214 }
215
216
217 /**
218  * Transmit our current typemap message to the other peer.
219  * (Done periodically in case an update got lost).
220  *
221  * @param cls the 'struct Session*'
222  * @param tc unused
223  */
224 static void
225 transmit_typemap_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 {
227   struct Session *session = cls;
228   struct GNUNET_MessageHeader *hdr;
229   struct GNUNET_TIME_Relative delay;
230
231   delay = TYPEMAP_FREQUENCY;
232   /* randomize a bit to avoid spont. sync */
233   delay.rel_value +=
234       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
235   session->typemap_task =
236       GNUNET_SCHEDULER_add_delayed (delay, &transmit_typemap_task, session);
237   GNUNET_STATISTICS_update (GSC_stats,
238                             gettext_noop ("# type map refreshes sent"), 1,
239                             GNUNET_NO);
240   hdr = GSC_TYPEMAP_compute_type_map_message ();
241   GSC_KX_encrypt_and_transmit (session->kxinfo, hdr, ntohs (hdr->size));
242   GNUNET_free (hdr);
243 }
244
245
246 /**
247  * Create a session, a key exchange was just completed.
248  *
249  * @param peer peer that is now connected
250  * @param kx key exchange that completed
251  */
252 void
253 GSC_SESSIONS_create (const struct GNUNET_PeerIdentity *peer,
254                      struct GSC_KeyExchangeInfo *kx)
255 {
256   struct Session *session;
257
258 #if DEBUG_CORE
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating session for peer `%4s'\n",
260               GNUNET_i2s (peer));
261 #endif
262   session = GNUNET_malloc (sizeof (struct Session));
263   session->tmap = GSC_TYPEMAP_create ();
264   session->peer = *peer;
265   session->kxinfo = kx;
266   session->time_established = GNUNET_TIME_absolute_get ();
267   session->typemap_task =
268       GNUNET_SCHEDULER_add_now (&transmit_typemap_task, session);
269   GNUNET_assert (GNUNET_OK ==
270                  GNUNET_CONTAINER_multihashmap_put (sessions, &peer->hashPubKey,
271                                                     session,
272                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
273   GNUNET_STATISTICS_set (GSC_stats, gettext_noop ("# entries in session map"),
274                          GNUNET_CONTAINER_multihashmap_size (sessions),
275                          GNUNET_NO);
276   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0 /* FIXME: ATSI */ ,
277                                               NULL, session->tmap);
278 }
279
280
281 /**
282  * Notify the given client about the session (client is new).
283  *
284  * @param cls the 'struct GSC_Client'
285  * @param key peer identity
286  * @param value the 'struct Session'
287  * @return GNUNET_OK (continue to iterate)
288  */
289 static int
290 notify_client_about_session (void *cls, const GNUNET_HashCode * key,
291                              void *value)
292 {
293   struct GSC_Client *client = cls;
294   struct Session *session = value;
295
296   GSC_CLIENTS_notify_client_about_neighbour (client, &session->peer, NULL, 0,   /* FIXME: ATS!? */
297                                              NULL,      /* old TMAP: none */
298                                              session->tmap);
299   return GNUNET_OK;
300 }
301
302
303 /**
304  * We have a new client, notify it about all current sessions.
305  *
306  * @param client the new client
307  */
308 void
309 GSC_SESSIONS_notify_client_about_sessions (struct GSC_Client *client)
310 {
311   /* notify new client about existing sessions */
312   GNUNET_CONTAINER_multihashmap_iterate (sessions, &notify_client_about_session,
313                                          client);
314 }
315
316
317 /**
318  * Try to perform a transmission on the given session.  Will solicit
319  * additional messages if the 'sme' queue is not full enough.
320  *
321  * @param session session to transmit messages from
322  */
323 static void
324 try_transmission (struct Session *session);
325
326
327 /**
328  * Queue a request from a client for transmission to a particular peer.
329  *
330  * @param car request to queue; this handle is then shared between
331  *         the caller (CLIENTS subsystem) and SESSIONS and must not
332  *         be released by either until either 'GNUNET_SESSIONS_dequeue',
333  *         'GNUNET_SESSIONS_transmit' or 'GNUNET_CLIENTS_failed'
334  *         have been invoked on it
335  */
336 void
337 GSC_SESSIONS_queue_request (struct GSC_ClientActiveRequest *car)
338 {
339   struct Session *session;
340
341   session = find_session (&car->target);
342   if (session == NULL)
343   {
344 #if DEBUG_CORE
345     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346                 "Dropped client request for transmission (am disconnected)\n");
347 #endif
348     GNUNET_break (0);           /* should have been rejected earlier */
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   struct GSC_ClientActiveRequest *nxt;
434   size_t so_size;
435
436   discard_expired_requests (session);
437   so_size = 0;
438   nxt = session->active_client_request_head;
439   while (NULL != (car = nxt))
440   {
441     nxt = car->next;
442     if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
443       break;
444     so_size += car->msize;
445     if (car->was_solicited == GNUNET_YES)
446       continue;
447     car->was_solicited = GNUNET_YES;
448     GSC_CLIENTS_solicit_request (car);
449   }
450 }
451
452
453 /**
454  * Some messages were delayed (corked), but the timeout has now expired.
455  * Send them now.
456  *
457  * @param cls 'struct Session' with the messages to transmit now
458  * @param tc scheduler context (unused)
459  */
460 static void
461 pop_cork_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
462 {
463   struct Session *session = cls;
464
465   session->cork_task = GNUNET_SCHEDULER_NO_TASK;
466   try_transmission (session);
467 }
468
469
470 /**
471  * Try to perform a transmission on the given session. Will solicit
472  * additional messages if the 'sme' queue is not full enough.
473  *
474  * @param session session to transmit messages from
475  */
476 static void
477 try_transmission (struct Session *session)
478 {
479   struct SessionMessageEntry *pos;
480   size_t msize;
481   struct GNUNET_TIME_Absolute now;
482   struct GNUNET_TIME_Absolute min_deadline;
483
484   if (GNUNET_YES != session->ready_to_transmit)
485     return;
486   msize = 0;
487   min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
488   /* check 'ready' messages */
489   pos = session->sme_head;
490   while ((NULL != pos) &&
491          (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
492   {
493     GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
494     msize += pos->size;
495     min_deadline = GNUNET_TIME_absolute_min (min_deadline, pos->deadline);
496     pos = pos->next;
497   }
498   now = GNUNET_TIME_absolute_get ();
499   if ((msize == 0) ||
500       ((msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
501        (min_deadline.abs_value > now.abs_value)))
502   {
503     /* not enough ready yet, try to solicit more */
504     solicit_messages (session);
505     if (msize > 0)
506     {
507       /* if there is data to send, just not yet, make sure we do transmit
508        * it once the deadline is reached */
509       if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
510         GNUNET_SCHEDULER_cancel (session->cork_task);
511       session->cork_task =
512           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
513                                         (min_deadline), &pop_cork_task,
514                                         session);
515     }
516     return;
517   }
518   /* create plaintext buffer of all messages, encrypt and transmit */
519   {
520     static unsigned long long total_bytes;
521     static unsigned int total_msgs;
522     char pbuf[msize];           /* plaintext */
523     size_t used;
524
525     used = 0;
526     while ((NULL != (pos = session->sme_head)) && (used + pos->size <= msize))
527     {
528       memcpy (&pbuf[used], &pos[1], pos->size);
529       used += pos->size;
530       GNUNET_CONTAINER_DLL_remove (session->sme_head, session->sme_tail, pos);
531       GNUNET_free (pos);
532     }
533     /* compute average payload size */
534     total_bytes += used;
535     total_msgs++;
536     if (0 == total_msgs)
537     {
538       /* 2^32 messages, wrap around... */
539       total_msgs = 1;
540       total_bytes = used;
541     }
542     GNUNET_STATISTICS_set (GSC_stats, "# avg payload per encrypted message",
543                            total_bytes / total_msgs, GNUNET_NO);
544     /* now actually transmit... */
545     session->ready_to_transmit = GNUNET_NO;
546     GSC_KX_encrypt_and_transmit (session->kxinfo, pbuf, used);
547   }
548 }
549
550
551 /**
552  * Send a message to the neighbour now.
553  *
554  * @param cls the message
555  * @param key neighbour's identity
556  * @param value 'struct Neighbour' of the target
557  * @return always GNUNET_OK
558  */
559 static int
560 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
561 {
562   const struct GNUNET_MessageHeader *hdr = cls;
563   struct Session *session = value;
564   struct SessionMessageEntry *m;
565   uint16_t size;
566
567   size = ntohs (hdr->size);
568   m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
569   memcpy (&m[1], hdr, size);
570   m->size = size;
571   GNUNET_CONTAINER_DLL_insert (session->sme_head, session->sme_tail, m);
572   try_transmission (session);
573   return GNUNET_OK;
574 }
575
576
577 /**
578  * Broadcast a message to all neighbours.
579  *
580  * @param msg message to transmit
581  */
582 void
583 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
584 {
585   if (NULL == sessions)
586     return;
587   GNUNET_CONTAINER_multihashmap_iterate (sessions, &do_send_message,
588                                          (void *) msg);
589 }
590
591
592 /**
593  * Traffic is being solicited for the given peer.  This means that the
594  * message queue on the transport-level (NEIGHBOURS subsystem) is now
595  * empty and it is now OK to transmit another (non-control) message.
596  *
597  * @param pid identity of peer ready to receive data
598  */
599 void
600 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
601 {
602   struct Session *session;
603
604   session = find_session (pid);
605   if (NULL == session)
606     return;
607   session->ready_to_transmit = GNUNET_YES;
608   try_transmission (session);
609 }
610
611
612 /**
613  * Transmit a message to a particular peer.
614  *
615  * @param car original request that was queued and then solicited;
616  *            this handle will now be 'owned' by the SESSIONS subsystem
617  * @param msg message to transmit
618  * @param cork is corking allowed?
619  */
620 void
621 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
622                        const struct GNUNET_MessageHeader *msg, int cork)
623 {
624   struct Session *session;
625   struct SessionMessageEntry *sme;
626   size_t msize;
627
628   session = find_session (&car->target);
629   if (NULL == session)
630     return;
631   msize = ntohs (msg->size);
632   sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
633   memcpy (&sme[1], msg, msize);
634   sme->size = msize;
635   if (GNUNET_YES == cork)
636     sme->deadline =
637         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
638   GNUNET_CONTAINER_DLL_insert_tail (session->sme_head, session->sme_tail, sme);
639   try_transmission (session);
640 }
641
642
643 /**
644  * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
645  *
646  * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
647  * @param key identity of the connected peer
648  * @param value the 'struct Neighbour' for the peer
649  * @return GNUNET_OK (continue to iterate)
650  */
651 #include "core.h"
652 static int
653 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
654 {
655   struct GNUNET_SERVER_TransmitContext *tc = cls;
656   struct Session *session = value;
657   struct ConnectNotifyMessage cnm;
658
659   /* FIXME: code duplication with clients... */
660   cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
661   cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
662   // FIXME: full ats...
663   cnm.ats_count = htonl (0);
664   cnm.peer = session->peer;
665   GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
666   return GNUNET_OK;
667 }
668
669
670 /**
671  * Handle CORE_ITERATE_PEERS request. For this request type, the client
672  * does not have to have transmitted an INIT request.  All current peers
673  * are returned, regardless of which message types they accept.
674  *
675  * @param cls unused
676  * @param client client sending the iteration request
677  * @param message iteration request message
678  */
679 void
680 GSC_SESSIONS_handle_client_iterate_peers (void *cls,
681                                           struct GNUNET_SERVER_Client *client,
682                                           const struct GNUNET_MessageHeader
683                                           *message)
684 {
685   struct GNUNET_MessageHeader done_msg;
686   struct GNUNET_SERVER_TransmitContext *tc;
687
688   tc = GNUNET_SERVER_transmit_context_create (client);
689   GNUNET_CONTAINER_multihashmap_iterate (sessions, &queue_connect_message, tc);
690   done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
691   done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
692   GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
693   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
694 }
695
696
697 /**
698  * Handle CORE_PEER_CONNECTED request.   Notify client about connection
699  * to the given neighbour.  For this request type, the client does not
700  * have to have transmitted an INIT request.  All current peers are
701  * returned, regardless of which message types they accept.
702  *
703  * @param cls unused
704  * @param client client sending the iteration request
705  * @param message iteration request message
706  */
707 void
708 GSC_SESSIONS_handle_client_have_peer (void *cls,
709                                       struct GNUNET_SERVER_Client *client,
710                                       const struct GNUNET_MessageHeader
711                                       *message)
712 {
713   struct GNUNET_MessageHeader done_msg;
714   struct GNUNET_SERVER_TransmitContext *tc;
715   const struct GNUNET_PeerIdentity *peer;
716
717   peer = (const struct GNUNET_PeerIdentity *) &message[1];      // YUCK!
718   tc = GNUNET_SERVER_transmit_context_create (client);
719   GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
720                                               &queue_connect_message, tc);
721   done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
722   done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
723   GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
724   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
725 }
726
727
728 /**
729  * We've received a typemap message from a peer, update ours.
730  * Notifies clients about the session.
731  *
732  * @param peer peer this is about
733  * @param msg typemap update message
734  */
735 void
736 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
737                           const struct GNUNET_MessageHeader *msg)
738 {
739   struct Session *session;
740   struct GSC_TypeMap *nmap;
741
742   nmap = GSC_TYPEMAP_get_from_message (msg);
743   if (NULL == nmap)
744     return;                     /* malformed */
745   session = find_session (peer);
746   if (NULL == session)
747   {
748     GNUNET_break (0);
749     return;
750   }
751   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0,    /* FIXME: ATS */
752                                               session->tmap, nmap);
753   GSC_TYPEMAP_destroy (session->tmap);
754   session->tmap = nmap;
755 }
756
757
758 /**
759  * The given peer send a message of the specified type.  Make sure the
760  * respective bit is set in its type-map and that clients are notified
761  * about the session.
762  *
763  * @param peer peer this is about
764  * @param type type of the message
765  */
766 void
767 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
768                              uint16_t type)
769 {
770   struct Session *session;
771   struct GSC_TypeMap *nmap;
772
773   if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
774     return;
775   session = find_session (peer);
776   GNUNET_assert (NULL != session);
777   if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap, &type, 1))
778     return;                     /* already in it */
779   nmap = GSC_TYPEMAP_extend (session->tmap, &type, 1);
780   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0,    /* FIXME: ATS */
781                                               session->tmap, nmap);
782   GSC_TYPEMAP_destroy (session->tmap);
783   session->tmap = nmap;
784 }
785
786
787 /**
788  * Initialize sessions subsystem.
789  */
790 void
791 GSC_SESSIONS_init ()
792 {
793   sessions = GNUNET_CONTAINER_multihashmap_create (128);
794 }
795
796
797 /**
798  * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
799  *
800  * @param cls NULL
801  * @param key identity of the connected peer
802  * @param value the 'struct Session' for the peer
803  * @return GNUNET_OK (continue to iterate)
804  */
805 static int
806 free_session_helper (void *cls, const GNUNET_HashCode * key, void *value)
807 {
808   struct Session *session = value;
809
810   GSC_SESSIONS_end (&session->peer);
811   return GNUNET_OK;
812 }
813
814
815 /**
816  * Shutdown sessions subsystem.
817  */
818 void
819 GSC_SESSIONS_done ()
820 {
821   GNUNET_CONTAINER_multihashmap_iterate (sessions, &free_session_helper, NULL);
822   GNUNET_CONTAINER_multihashmap_destroy (sessions);
823   sessions = NULL;
824 }
825
826 /* end of gnunet-service-core_sessions.c */