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