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