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