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