ab825e9b6028f84bb655dbad273a7aa37a644da9
[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 #if DEBUG_CORE
339     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340                 "Dropped client request for transmission (am disconnected)\n");
341 #endif
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 #if DEBUG_CORE
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354               "Received client transmission request. queueing\n");
355 #endif
356   GNUNET_CONTAINER_DLL_insert (session->active_client_request_head,
357                                session->active_client_request_tail, car);
358   try_transmission (session);
359 }
360
361
362 /**
363  * Dequeue a request from a client from transmission to a particular peer.
364  *
365  * @param car request to dequeue; this handle will then be 'owned' by
366  *        the caller (CLIENTS sysbsystem)
367  */
368 void
369 GSC_SESSIONS_dequeue_request (struct GSC_ClientActiveRequest *car)
370 {
371   struct Session *s;
372
373   if (0 ==
374       memcmp (&car->target, &GSC_my_identity,
375               sizeof (struct GNUNET_PeerIdentity)))
376     return;
377   s = find_session (&car->target);
378   GNUNET_assert (NULL != s);
379   GNUNET_CONTAINER_DLL_remove (s->active_client_request_head,
380                                s->active_client_request_tail, car);
381 }
382
383
384 /**
385  * Discard all expired active transmission requests from clients.
386  *
387  * @param session session to clean up
388  */
389 static void
390 discard_expired_requests (struct Session *session)
391 {
392   struct GSC_ClientActiveRequest *pos;
393   struct GSC_ClientActiveRequest *nxt;
394   struct GNUNET_TIME_Absolute now;
395
396   now = GNUNET_TIME_absolute_get ();
397   pos = NULL;
398   nxt = session->active_client_request_head;
399   while (NULL != nxt)
400   {
401     pos = nxt;
402     nxt = pos->next;
403     if ((pos->deadline.abs_value < now.abs_value) &&
404         (GNUNET_YES != pos->was_solicited))
405     {
406       GNUNET_STATISTICS_update (GSC_stats,
407                                 gettext_noop
408                                 ("# messages discarded (expired prior to transmission)"),
409                                 1, GNUNET_NO);
410       GNUNET_CONTAINER_DLL_remove (session->active_client_request_head,
411                                    session->active_client_request_tail, pos);
412       GSC_CLIENTS_reject_request (pos);
413     }
414   }
415 }
416
417
418 /**
419  * Solicit messages for transmission.
420  *
421  * @param session session to solict messages for
422  */
423 static void
424 solicit_messages (struct Session *session)
425 {
426   struct GSC_ClientActiveRequest *car;
427   size_t so_size;
428
429   discard_expired_requests (session);
430   so_size = 0;
431   for (car = session->active_client_request_head; NULL != car; car = car->next)
432   {
433     if (so_size + car->msize > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
434       break;
435     so_size += car->msize;
436     if (car->was_solicited == GNUNET_YES)
437       continue;
438     car->was_solicited = GNUNET_YES;
439     GSC_CLIENTS_solicit_request (car);
440   }
441 }
442
443
444 /**
445  * Some messages were delayed (corked), but the timeout has now expired.
446  * Send them now.
447  *
448  * @param cls 'struct Session' with the messages to transmit now
449  * @param tc scheduler context (unused)
450  */
451 static void
452 pop_cork_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
453 {
454   struct Session *session = cls;
455
456   session->cork_task = GNUNET_SCHEDULER_NO_TASK;
457   try_transmission (session);
458 }
459
460
461 /**
462  * Try to perform a transmission on the given session. Will solicit
463  * additional messages if the 'sme' queue is not full enough.
464  *
465  * @param session session to transmit messages from
466  */
467 static void
468 try_transmission (struct Session *session)
469 {
470   struct SessionMessageEntry *pos;
471   size_t msize;
472   struct GNUNET_TIME_Absolute now;
473   struct GNUNET_TIME_Absolute min_deadline;
474
475   if (GNUNET_YES != session->ready_to_transmit)
476     return;
477   msize = 0;
478   min_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
479   /* check 'ready' messages */
480   pos = session->sme_head;
481   while ((NULL != pos) &&
482          (msize + pos->size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE))
483   {
484     GNUNET_assert (pos->size < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
485     msize += pos->size;
486     min_deadline = GNUNET_TIME_absolute_min (min_deadline, pos->deadline);
487     pos = pos->next;
488   }
489   now = GNUNET_TIME_absolute_get ();
490   if ((msize == 0) ||
491       ((msize < GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / 2) &&
492        (min_deadline.abs_value > now.abs_value)))
493   {
494     /* not enough ready yet, try to solicit more */
495     solicit_messages (session);
496     if (msize > 0)
497     {
498       /* if there is data to send, just not yet, make sure we do transmit
499        * it once the deadline is reached */
500       if (session->cork_task != GNUNET_SCHEDULER_NO_TASK)
501         GNUNET_SCHEDULER_cancel (session->cork_task);
502       session->cork_task =
503           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
504                                         (min_deadline), &pop_cork_task,
505                                         session);
506     }
507     return;
508   }
509   /* create plaintext buffer of all messages, encrypt and transmit */
510   {
511     static unsigned long long total_bytes;
512     static unsigned int total_msgs;
513     char pbuf[msize];           /* plaintext */
514     size_t used;
515
516     used = 0;
517     while ((NULL != (pos = session->sme_head)) && (used + pos->size <= msize))
518     {
519       memcpy (&pbuf[used], &pos[1], pos->size);
520       used += pos->size;
521       GNUNET_CONTAINER_DLL_remove (session->sme_head, session->sme_tail, pos);
522       GNUNET_free (pos);
523     }
524     /* compute average payload size */
525     total_bytes += used;
526     total_msgs++;
527     if (0 == total_msgs)
528     {
529       /* 2^32 messages, wrap around... */
530       total_msgs = 1;
531       total_bytes = used;
532     }
533     GNUNET_STATISTICS_set (GSC_stats, "# avg payload per encrypted message",
534                            total_bytes / total_msgs, GNUNET_NO);
535     /* now actually transmit... */
536     session->ready_to_transmit = GNUNET_NO;
537     GSC_KX_encrypt_and_transmit (session->kxinfo, pbuf, used);
538   }
539 }
540
541
542 /**
543  * Send a message to the neighbour now.
544  *
545  * @param cls the message
546  * @param key neighbour's identity
547  * @param value 'struct Neighbour' of the target
548  * @return always GNUNET_OK
549  */
550 static int
551 do_send_message (void *cls, const GNUNET_HashCode * key, void *value)
552 {
553   const struct GNUNET_MessageHeader *hdr = cls;
554   struct Session *session = value;
555   struct SessionMessageEntry *m;
556   uint16_t size;
557
558   size = ntohs (hdr->size);
559   m = GNUNET_malloc (sizeof (struct SessionMessageEntry) + size);
560   memcpy (&m[1], hdr, size);
561   m->size = size;
562   GNUNET_CONTAINER_DLL_insert (session->sme_head, session->sme_tail, m);
563   try_transmission (session);
564   return GNUNET_OK;
565 }
566
567
568 /**
569  * Broadcast a message to all neighbours.
570  *
571  * @param msg message to transmit
572  */
573 void
574 GSC_SESSIONS_broadcast (const struct GNUNET_MessageHeader *msg)
575 {
576   if (NULL == sessions)
577     return;
578   GNUNET_CONTAINER_multihashmap_iterate (sessions, &do_send_message,
579                                          (void *) msg);
580 }
581
582
583 /**
584  * Traffic is being solicited for the given peer.  This means that the
585  * message queue on the transport-level (NEIGHBOURS subsystem) is now
586  * empty and it is now OK to transmit another (non-control) message.
587  *
588  * @param pid identity of peer ready to receive data
589  */
590 void
591 GSC_SESSIONS_solicit (const struct GNUNET_PeerIdentity *pid)
592 {
593   struct Session *session;
594
595   session = find_session (pid);
596   if (NULL == session)
597     return;
598   session->ready_to_transmit = GNUNET_YES;
599   try_transmission (session);
600 }
601
602
603 /**
604  * Transmit a message to a particular peer.
605  *
606  * @param car original request that was queued and then solicited;
607  *            this handle will now be 'owned' by the SESSIONS subsystem
608  * @param msg message to transmit
609  * @param cork is corking allowed?
610  */
611 void
612 GSC_SESSIONS_transmit (struct GSC_ClientActiveRequest *car,
613                        const struct GNUNET_MessageHeader *msg, int cork)
614 {
615   struct Session *session;
616   struct SessionMessageEntry *sme;
617   size_t msize;
618
619   session = find_session (&car->target);
620   if (NULL == session)
621     return;
622   msize = ntohs (msg->size);
623   sme = GNUNET_malloc (sizeof (struct SessionMessageEntry) + msize);
624   memcpy (&sme[1], msg, msize);
625   sme->size = msize;
626   if (GNUNET_YES == cork)
627     sme->deadline =
628         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY);
629   GNUNET_CONTAINER_DLL_insert_tail (session->sme_head, session->sme_tail, sme);
630   try_transmission (session);
631 }
632
633
634 /**
635  * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
636  *
637  * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
638  * @param key identity of the connected peer
639  * @param value the 'struct Neighbour' for the peer
640  * @return GNUNET_OK (continue to iterate)
641  */
642 #include "core.h"
643 static int
644 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
645 {
646   struct GNUNET_SERVER_TransmitContext *tc = cls;
647   struct Session *session = value;
648   struct ConnectNotifyMessage cnm;
649
650   /* FIXME: code duplication with clients... */
651   cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
652   cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
653   // FIXME: full ats...
654   cnm.ats_count = htonl (0);
655   cnm.peer = session->peer;
656   GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
657   return GNUNET_OK;
658 }
659
660
661 /**
662  * Handle CORE_ITERATE_PEERS request. For this request type, the client
663  * does not have to have transmitted an INIT request.  All current peers
664  * are returned, regardless of which message types they accept.
665  *
666  * @param cls unused
667  * @param client client sending the iteration request
668  * @param message iteration request message
669  */
670 void
671 GSC_SESSIONS_handle_client_iterate_peers (void *cls,
672                                           struct GNUNET_SERVER_Client *client,
673                                           const struct GNUNET_MessageHeader
674                                           *message)
675 {
676   struct GNUNET_MessageHeader done_msg;
677   struct GNUNET_SERVER_TransmitContext *tc;
678
679   tc = GNUNET_SERVER_transmit_context_create (client);
680   GNUNET_CONTAINER_multihashmap_iterate (sessions, &queue_connect_message, tc);
681   done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
682   done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
683   GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
684   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
685 }
686
687
688 /**
689  * Handle CORE_PEER_CONNECTED request.   Notify client about connection
690  * to the given neighbour.  For this request type, the client does not
691  * have to have transmitted an INIT request.  All current peers are
692  * returned, regardless of which message types they accept.
693  *
694  * @param cls unused
695  * @param client client sending the iteration request
696  * @param message iteration request message
697  */
698 void
699 GSC_SESSIONS_handle_client_have_peer (void *cls,
700                                       struct GNUNET_SERVER_Client *client,
701                                       const struct GNUNET_MessageHeader
702                                       *message)
703 {
704   struct GNUNET_MessageHeader done_msg;
705   struct GNUNET_SERVER_TransmitContext *tc;
706   const struct GNUNET_PeerIdentity *peer;
707
708   peer = (const struct GNUNET_PeerIdentity *) &message[1];      // YUCK!
709   tc = GNUNET_SERVER_transmit_context_create (client);
710   GNUNET_CONTAINER_multihashmap_get_multiple (sessions, &peer->hashPubKey,
711                                               &queue_connect_message, tc);
712   done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
713   done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
714   GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
715   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
716 }
717
718
719 /**
720  * We've received a typemap message from a peer, update ours.
721  * Notifies clients about the session.
722  *
723  * @param peer peer this is about
724  * @param msg typemap update message
725  */
726 void
727 GSC_SESSIONS_set_typemap (const struct GNUNET_PeerIdentity *peer,
728                           const struct GNUNET_MessageHeader *msg)
729 {
730   struct Session *session;
731   struct GSC_TypeMap *nmap;
732
733   nmap = GSC_TYPEMAP_get_from_message (msg);
734   if (NULL == nmap)
735     return;                     /* malformed */
736   session = find_session (peer);
737   if (NULL == session)
738   {
739     GNUNET_break (0);
740     return;
741   }
742   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0,    /* FIXME: ATS */
743                                               session->tmap, nmap);
744   GSC_TYPEMAP_destroy (session->tmap);
745   session->tmap = nmap;
746 }
747
748
749 /**
750  * The given peer send a message of the specified type.  Make sure the
751  * respective bit is set in its type-map and that clients are notified
752  * about the session.
753  *
754  * @param peer peer this is about
755  * @param type type of the message
756  */
757 void
758 GSC_SESSIONS_add_to_typemap (const struct GNUNET_PeerIdentity *peer,
759                              uint16_t type)
760 {
761   struct Session *session;
762   struct GSC_TypeMap *nmap;
763
764   if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
765     return;
766   session = find_session (peer);
767   GNUNET_assert (NULL != session);
768   if (GNUNET_YES == GSC_TYPEMAP_test_match (session->tmap, &type, 1))
769     return;                     /* already in it */
770   nmap = GSC_TYPEMAP_extend (session->tmap, &type, 1);
771   GSC_CLIENTS_notify_clients_about_neighbour (peer, NULL, 0,    /* FIXME: ATS */
772                                               session->tmap, nmap);
773   GSC_TYPEMAP_destroy (session->tmap);
774   session->tmap = nmap;
775 }
776
777
778 /**
779  * Initialize sessions subsystem.
780  */
781 void
782 GSC_SESSIONS_init ()
783 {
784   sessions = GNUNET_CONTAINER_multihashmap_create (128);
785 }
786
787
788 /**
789  * Helper function for GSC_SESSIONS_handle_client_iterate_peers.
790  *
791  * @param cls NULL
792  * @param key identity of the connected peer
793  * @param value the 'struct Session' for the peer
794  * @return GNUNET_OK (continue to iterate)
795  */
796 static int
797 free_session_helper (void *cls, const GNUNET_HashCode * key, void *value)
798 {
799   struct Session *session = value;
800
801   GSC_SESSIONS_end (&session->peer);
802   return GNUNET_OK;
803 }
804
805
806 /**
807  * Shutdown sessions subsystem.
808  */
809 void
810 GSC_SESSIONS_done ()
811 {
812   GNUNET_CONTAINER_multihashmap_iterate (sessions, &free_session_helper, NULL);
813   GNUNET_CONTAINER_multihashmap_destroy (sessions);
814   sessions = NULL;
815 }
816
817 /* end of gnunet-service-core_sessions.c */