fixes
[oweals/gnunet.git] / src / core / gnunet-service-core_clients.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_clients.c
23  * @brief code for managing interactions with clients of core service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet-service-core.h"
31 #include "gnunet-service-core_clients.h"
32 #include "gnunet-service-core_sessions.h"
33 #include "gnunet-service-core_typemap.h"
34 #include "core.h"
35
36 /**
37  * How many messages do we queue up at most for optional
38  * notifications to a client?  (this can cause notifications
39  * about outgoing messages to be dropped).
40  */
41 #define MAX_NOTIFY_QUEUE 1024
42
43
44 /**
45  * Data structure for each client connected to the core service.
46  */
47 struct GSC_Client
48 {
49   /**
50    * Clients are kept in a linked list.
51    */
52   struct GSC_Client *next;
53
54   /**
55    * Clients are kept in a linked list.
56    */
57   struct GSC_Client *prev;
58
59   /**
60    * Handle for the client with the server API.
61    */
62   struct GNUNET_SERVER_Client *client_handle;
63
64   /**
65    * Array of the types of messages this peer cares
66    * about (with "tcnt" entries).  Allocated as part
67    * of this client struct, do not free!
68    */
69   const uint16_t *types;
70
71   /**
72    * Map of peer identities to active transmission requests of this
73    * client to the peer (of type 'struct GSC_ClientActiveRequest').
74    */
75   struct GNUNET_CONTAINER_MultiHashMap *requests;
76
77   /**
78    * Options for messages this client cares about,
79    * see GNUNET_CORE_OPTION_ values.
80    */
81   uint32_t options;
82
83   /**
84    * Number of types of incoming messages this client
85    * specifically cares about.  Size of the "types" array.
86    */
87   unsigned int tcnt;
88
89 };
90
91
92 /**
93  * Head of linked list of our clients.
94  */
95 static struct GSC_Client *client_head;
96
97 /**
98  * Tail of linked list of our clients.
99  */
100 static struct GSC_Client *client_tail;
101
102 /**
103  * Context for notifications we need to send to our clients.
104  */
105 static struct GNUNET_SERVER_NotificationContext *notifier;
106
107 /**
108  * Tokenizer for messages received from clients.
109  */
110 static struct GNUNET_SERVER_MessageStreamTokenizer *client_mst;
111
112
113 /**
114  * Lookup our client struct given the server's client handle.
115  *
116  * @param client server client handle to look up
117  * @return our client handle for the client
118  */
119 static struct GSC_Client *
120 find_client (struct GNUNET_SERVER_Client *client)
121 {
122   struct GSC_Client *c;
123
124   c = client_head;
125   while ((c != NULL) && (c->client_handle != client))
126     c = c->next;
127   return c;
128 }
129
130
131 /**
132  * Send a message to one of our clients.
133  *
134  * @param client target for the message
135  * @param msg message to transmit
136  * @param can_drop could this message be dropped if the
137  *        client's queue is getting too large?
138  */
139 static void
140 send_to_client (struct GSC_Client *client, 
141                 const struct GNUNET_MessageHeader *msg,
142                 int can_drop)
143 {
144 #if DEBUG_CORE_CLIENT
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146               "Preparing to send %u bytes of message of type %u to client.\n",
147               (unsigned int) ntohs (msg->size),
148               (unsigned int) ntohs (msg->type));
149 #endif
150   GNUNET_SERVER_notification_context_unicast (notifier, client->client_handle,
151                                               msg, can_drop);
152 }
153
154
155 /**
156  * Send a message to one of our clients.
157  *
158  * @param client target for the message
159  * @param msg message to transmit
160  * @param can_drop could this message be dropped if the
161  *        client's queue is getting too large?
162  */
163 void
164 GSC_CLIENTS_send_to_client (struct GNUNET_SERVER_Client *client,
165                             const struct GNUNET_MessageHeader *msg,
166                             int can_drop)
167 {
168   struct GSC_Client *c;
169
170   c = find_client (client);
171   if (NULL == c)
172   {
173     GNUNET_break (0);
174     return;
175   }
176   send_to_client (c, msg, can_drop);
177 }
178
179
180 /**
181  * Test if the client is interested in messages of the given type.
182  *
183  * @param type message type
184  * @param c client to test
185  * @return GNUNET_YES if 'c' is interested, GNUNET_NO if not.
186  */
187 static int
188 type_match (uint16_t type,
189             struct GSC_Client *c)
190 {
191   unsigned int i;
192
193   for (i=0;i<c->tcnt;i++)
194     if (type == c->types[i])
195       return GNUNET_YES;
196   return GNUNET_NO;
197 }
198
199
200 /**
201  * Send a message to all of our current clients that have the right
202  * options set.
203  *
204  * @param msg message to multicast
205  * @param can_drop can this message be discarded if the queue is too long
206  * @param options mask to use
207  * @param type type of the embedded message, 0 for none
208  */
209 static void
210 send_to_all_clients (const struct GNUNET_MessageHeader *msg, 
211                      int can_drop,
212                      int options,
213                      uint16_t type)
214 {
215   struct GSC_Client *c;
216
217   for (c = client_head; c != NULL; c = c->next)
218   {
219     if (! ( (0 != (c->options & options)) ||
220             ( (0 != (options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) &&
221               (GNUNET_YES == type_match (type, c)) ) ) )
222       continue; /* skip */
223 #if DEBUG_CORE_CLIENT > 1
224     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                 "Sending message of type %u to client.\n",
226                 (unsigned int) ntohs (msg->type));
227 #endif
228     send_to_client (c, msg, can_drop);
229   }
230 }
231
232
233 /**
234  * Handle CORE_INIT request.
235  *
236  * @param cls unused
237  * @param client new client that sent INIT
238  * @param message the 'struct InitMessage' (presumably)
239  */
240 static void
241 handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
242                     const struct GNUNET_MessageHeader *message)
243 {
244   const struct InitMessage *im;
245   struct InitReplyMessage irm;
246   struct GSC_Client *c;
247   uint16_t msize;
248   const uint16_t *types;
249   uint16_t *wtypes;
250   unsigned int i;
251
252   /* check that we don't have an entry already */
253   c = find_client (client);
254   if (NULL != c)
255   {
256     GNUNET_break (0);
257     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
258     return;
259   }
260   msize = ntohs (message->size);
261   if (msize < sizeof (struct InitMessage))
262   {
263     GNUNET_break (0);
264     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
265     return;
266   }
267   GNUNET_SERVER_notification_context_add (notifier, client);
268   im = (const struct InitMessage *) message;
269   types = (const uint16_t *) &im[1];
270   msize -= sizeof (struct InitMessage);
271   c = GNUNET_malloc (sizeof (struct GSC_Client) + msize);
272   c->client_handle = client;
273   c->tcnt = msize / sizeof (uint16_t);
274   c->options = ntohl (im->options);
275   c->types = (const uint16_t *) &c[1];
276   wtypes = (uint16_t *) & c[1];
277   for (i = 0; i < c->tcnt; i++)
278     wtypes[i] = ntohs (types[i]);
279   GSC_TYPEMAP_add (wtypes, c->tcnt);
280   GNUNET_CONTAINER_DLL_insert (client_head,
281                                client_tail,
282                                c);
283 #if DEBUG_CORE_CLIENT
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285               "Client connecting to core service is interested in %u message types\n", 
286               (unsigned int) c->tcnt);
287 #endif
288   /* send init reply message */
289   irm.header.size = htons (sizeof (struct InitReplyMessage));
290   irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
291   irm.reserved = htonl (0);
292   irm.publicKey = GSC_my_public_key;
293   send_to_client (c, &irm.header, GNUNET_NO);
294   if (0 != (c->options & GNUNET_CORE_OPTION_SEND_CONNECT))
295     GSC_SESSIONS_notify_client_about_sessions (c);
296   GNUNET_SERVER_receive_done (client, GNUNET_OK);
297 }
298
299
300 /**
301  * Handle CORE_SEND_REQUEST message.
302  *
303  * @param cls unused
304  * @param client new client that sent CORE_SEND_REQUEST
305  * @param message the 'struct InitMessage' (presumably)
306  */
307 static void
308 handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
309                             const struct GNUNET_MessageHeader *message)
310 {
311   const struct SendMessageRequest *req;
312   struct GSC_Client *c;
313   struct GSC_ClientActiveRequest *car;
314
315   req = (const struct SendMessageRequest *) message;
316   c = find_client (client);
317   if (c == NULL)
318   {
319     /* client did not send INIT first! */
320     GNUNET_break (0);
321     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
322     return;
323   }
324   if (c->requests == NULL)
325     c->requests = GNUNET_CONTAINER_multihashmap_create (16);
326   car = GNUNET_CONTAINER_multihashmap_get (c->requests, &req->peer.hashPubKey);
327   if (car == NULL)
328   {
329     /* create new entry */
330     car = GNUNET_malloc (sizeof (struct GSC_ClientActiveRequest));
331     GNUNET_assert (GNUNET_OK ==
332                    GNUNET_CONTAINER_multihashmap_put (c->requests,
333                                                       &req->peer.hashPubKey,
334                                                       car,
335                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
336     car->client_handle = c;
337   }
338   car->target = req->peer;
339   car->deadline = GNUNET_TIME_absolute_ntoh (req->deadline);
340   car->priority = ntohl (req->priority);
341   car->msize = ntohs (req->size);
342   car->smr_id = req->smr_id;
343   if (0 ==
344       memcmp (&req->peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
345     GSC_CLIENTS_solicit_request (car);
346   else
347     GSC_SESSIONS_queue_request (car);
348   GNUNET_SERVER_receive_done (client, GNUNET_OK);
349 }
350
351
352 /**
353  * Handle CORE_SEND request.
354  *
355  * @param cls unused
356  * @param client the client issuing the request
357  * @param message the "struct SendMessage"
358  */
359 static void
360 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
361                     const struct GNUNET_MessageHeader *message)
362 {
363   const struct SendMessage *sm;
364   struct GSC_Client *c;
365   struct GSC_ClientActiveRequest *car;
366   uint16_t msize;
367
368   msize = ntohs (message->size);
369   if (msize <
370       sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
371   {
372     GNUNET_break (0);
373     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
374     return;
375   }
376   sm = (const struct SendMessage *) message;
377   msize -= sizeof (struct SendMessage);
378   GNUNET_break (0 == ntohl (sm->reserved));
379   c = find_client (client);
380   if (c == NULL)
381   {
382     /* client did not send INIT first! */
383     GNUNET_break (0);
384     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
385     return;
386   }
387   car = GNUNET_CONTAINER_multihashmap_get (c->requests, &sm->peer.hashPubKey);
388   if (NULL == car)
389   {
390     /* client did not request transmission first! */
391     GNUNET_break (0);
392     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393     return;
394   }
395   GNUNET_assert (GNUNET_YES ==
396                  GNUNET_CONTAINER_multihashmap_remove (c->requests, 
397                                                        &sm->peer.hashPubKey,
398                                                        car));
399   GNUNET_SERVER_mst_receive (client_mst,
400                              car, 
401                              (const char*) &sm[1], msize,
402                              GNUNET_YES,
403                              GNUNET_NO);
404   if (0 !=
405       memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))  
406     GSC_SESSIONS_dequeue_request (car);
407   GNUNET_free (car);  
408   GNUNET_SERVER_receive_done (client, GNUNET_OK);
409 }
410
411
412 /**
413  * Functions with this signature are called whenever a complete
414  * message is received by the tokenizer.  Used by the 'client_mst' for
415  * dispatching messages from clients to either the SESSION subsystem
416  * or other CLIENT (for loopback).
417  *
418  * @param cls closure
419  * @param client reservation request ('struct GSC_ClientActiveRequest')
420  * @param message the actual message
421  */
422 static void
423 client_tokenizer_callback (void *cls, void *client,
424                            const struct GNUNET_MessageHeader *message)
425 {
426   struct GSC_ClientActiveRequest *car = client;
427
428   if (0 ==
429       memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))  
430   {
431     GSC_CLIENTS_deliver_message (&GSC_my_identity, 
432                                  NULL, 0,
433                                  message,
434                                  ntohs (message->size),
435                                  GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);  
436     GSC_CLIENTS_deliver_message (&GSC_my_identity, 
437                                  NULL, 0,
438                                  message,
439                                  sizeof (struct GNUNET_MessageHeader),
440                                  GNUNET_CORE_OPTION_SEND_HDR_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);  
441   }
442   else
443     GSC_SESSIONS_transmit (car, message);
444 }
445
446
447 /**
448  * Free client request records.
449  *
450  * @param cls NULL
451  * @param key identity of peer for which this is an active request
452  * @param value the 'struct GSC_ClientActiveRequest' to free
453  * @return GNUNET_YES (continue iteration)
454  */
455 static int
456 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
457                                void *value)
458 {
459   struct GSC_ClientActiveRequest *car = value;
460
461   GNUNET_assert (GNUNET_YES ==
462                  GNUNET_CONTAINER_multihashmap_remove (car->client_handle->requests,
463                                                        &car->target.hashPubKey,
464                                                        car));
465   GSC_SESSIONS_dequeue_request (car);
466   GNUNET_free (car);
467   return GNUNET_YES;
468 }
469
470
471 /**
472  * A client disconnected, clean up.
473  *
474  * @param cls closure
475  * @param client identification of the client
476  */
477 static void
478 handle_client_disconnect (void *cls,
479                           struct GNUNET_SERVER_Client *client)
480 {
481   struct GSC_Client *c;
482
483   if (client == NULL)
484     return;
485 #if DEBUG_CORE_CLIENT
486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487               "Client %p has disconnected from core service.\n", client);
488 #endif
489   c = find_client (client);
490   if (c == NULL)
491     return; /* client never sent INIT */
492   GNUNET_CONTAINER_DLL_remove (client_head,
493                                client_tail,
494                                c);
495   if (c->requests != NULL)
496   {
497     GNUNET_CONTAINER_multihashmap_iterate (c->requests,
498                                            &destroy_active_client_request,
499                                            NULL);
500     GNUNET_CONTAINER_multihashmap_destroy (c->requests);
501   }
502   GSC_TYPEMAP_remove (c->types, c->tcnt);
503   GNUNET_free (c);
504 }
505
506
507 /**
508  * Tell a client that we are ready to receive the message.
509  *
510  * @param car request that is now ready; the responsibility
511  *        for the handle remains shared between CLIENTS
512  *        and SESSIONS after this call.
513  */
514 void
515 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
516 {
517   struct GSC_Client *c;
518   struct SendMessageReady smr;
519
520   c = car->client_handle;
521   smr.header.size = htons (sizeof (struct SendMessageReady));
522   smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
523   smr.size = htons (car->msize);
524   smr.smr_id = car->smr_id;
525   smr.peer = car->target;
526   send_to_client (c, &smr.header, GNUNET_NO);
527 }
528
529
530 /**
531  * Tell a client that we will never be ready to receive the
532  * given message in time (disconnect or timeout).
533  *
534  * @param car request that now permanently failed; the
535  *        responsibility for the handle is now returned
536  *        to CLIENTS (SESSIONS is done with it).
537  */
538 void
539 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
540 {
541   GNUNET_assert (GNUNET_YES ==
542                  destroy_active_client_request (NULL, &car->target.hashPubKey, car));  
543 }
544
545
546 /**
547  * Notify a particular client about a change to existing connection to
548  * one of our neighbours (check if the client is interested).  Called
549  * from 'GSC_SESSIONS_notify_client_about_sessions'.
550  *
551  * @param client client to notify
552  * @param neighbour identity of the neighbour that changed status
553  * @param atsi performance information about neighbour
554  * @param atsi_count number of entries in 'ats' array
555  * @param tmap_old previous type map for the neighbour, NULL for disconnect
556  * @param tmap_new updated type map for the neighbour, NULL for disconnect
557  */
558 void
559 GDS_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
560                                            const struct GNUNET_PeerIdentity *neighbour,
561                                            const struct GNUNET_TRANSPORT_ATS_Information *atsi,
562                                            unsigned int atsi_count,
563                                            const struct GSC_TypeMap *tmap_old,
564                                            const struct GSC_TypeMap *tmap_new)
565 {
566   struct ConnectNotifyMessage *cnm;
567   size_t size;
568   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
569   struct GNUNET_TRANSPORT_ATS_Information *a;
570   struct DisconnectNotifyMessage dcm;
571   int old_match;
572   int new_match;
573
574   old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
575   new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
576   if (old_match == new_match)
577     return; /* no change */
578   if (old_match == GNUNET_NO)
579   {
580     /* send connect */  
581     size =
582       sizeof (struct ConnectNotifyMessage) +
583       (atsi_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
584     if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
585       {
586         GNUNET_break (0);
587         /* recovery strategy: throw away performance data */
588         atsi_count = 0;
589         size = sizeof (struct ConnectNotifyMessage);
590       }
591     cnm = (struct ConnectNotifyMessage *) buf;
592     cnm->header.size = htons (size);
593     cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
594     cnm->ats_count = htonl (atsi_count);
595     a = &cnm->ats;
596     memcpy (a, atsi,
597             sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
598     a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
599     a[atsi_count].value = htonl (0);
600 #if DEBUG_CORE_CLIENT
601     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
602                 "Sending `%s' message to client.\n",
603                 "NOTIFY_CONNECT");
604 #endif
605     cnm->peer = *neighbour;
606     send_to_client (client, &cnm->header, GNUNET_NO);
607   }
608   else
609   {
610     /* send disconnect */
611     dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
612     dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
613     dcm.reserved = htonl (0);
614     dcm.peer = *neighbour;
615     send_to_client (client, &dcm.header, GNUNET_NO);
616   }
617 }
618
619
620 /**
621  * Notify all clients about a change to existing session.
622  * Called from SESSIONS whenever there is a change in sessions
623  * or types processed by the respective peer.
624  *
625  * @param neighbour identity of the neighbour that changed status
626  * @param atsi performance information about neighbour
627  * @param atsi_count number of entries in 'ats' array
628  * @param tmap_old previous type map for the neighbour, NULL for disconnect
629  * @param tmap_new updated type map for the neighbour, NULL for disconnect
630  */
631 void
632 GDS_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
633                                             const struct GNUNET_TRANSPORT_ATS_Information *atsi,
634                                             unsigned int atsi_count,
635                                             const struct GSC_TypeMap *tmap_old,
636                                             const struct GSC_TypeMap *tmap_new)
637 {
638   struct GSC_Client *c;
639
640   for (c = client_head; c != NULL; c = c->next)
641     GDS_CLIENTS_notify_client_about_neighbour (c, neighbour, atsi,
642                                                atsi_count, 
643                                                tmap_old, tmap_new);
644 }
645
646
647 /**
648  * Deliver P2P message to interested clients.  Caller must have checked
649  * that the sending peer actually lists the given message type as one 
650  * of its types.
651  *
652  * @param sender peer who sent us the message 
653  * @param atsi performance information about neighbour
654  * @param atsi_count number of entries in 'ats' array
655  * @param msg the message
656  * @param msize number of bytes to transmit
657  * @param options options for checking which clients should
658  *        receive the message
659  */
660 void
661 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
662                              const struct GNUNET_TRANSPORT_ATS_Information *atsi,
663                              unsigned int atsi_count,
664                              const struct GNUNET_MessageHeader *msg,
665                              uint16_t msize,
666                              int options)
667 {
668   size_t size = msize + sizeof (struct NotifyTrafficMessage) +
669       atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
670   char buf[size];
671   struct NotifyTrafficMessage *ntm;
672   struct GNUNET_TRANSPORT_ATS_Information *a;
673
674   if (0 == options)
675   {
676     GNUNET_snprintf (buf, sizeof (buf),
677                      gettext_noop ("# bytes of messages of type %u received"),
678                      (unsigned int) ntohs (msg->type));
679     GNUNET_STATISTICS_update (GSC_stats, buf, msize, GNUNET_NO);
680   }
681   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
682   {
683     GNUNET_break (0);
684     /* recovery strategy: throw performance data away... */
685     atsi_count = 0;
686     size = msize + sizeof (struct NotifyTrafficMessage);
687   }
688 #if DEBUG_CORE
689   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690               "Core service passes message from `%4s' of type %u to client.\n",
691               GNUNET_i2s (sender),
692               (unsigned int) ntohs (msg->type));
693 #endif
694   ntm = (struct NotifyTrafficMessage *) buf;
695   ntm->header.size = htons (size);
696   ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
697   ntm->ats_count = htonl (atsi_count);
698   ntm->peer = *sender;
699   a = &ntm->ats;
700   memcpy (a, atsi,
701           sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
702   a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
703   a[atsi_count].value = htonl (0);
704   memcpy (&a[atsi_count + 1], msg, msize);
705   send_to_all_clients (&ntm->header, GNUNET_YES, 
706                        options, ntohs (msg->type));
707 }
708
709
710 /**
711  * Initialize clients subsystem.
712  *
713  * @param server handle to server clients connect to
714  */
715 void
716 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
717 {
718   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
719     {&handle_client_init, NULL,
720      GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
721     {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
722      GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
723      sizeof (struct GNUNET_MessageHeader)},
724     {&GSC_SESSIONS_handle_client_have_peer, NULL,
725      GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
726      sizeof (struct GNUNET_MessageHeader) +
727      sizeof (struct GNUNET_PeerIdentity)},
728     {&GSC_SESSIONS_handle_client_request_info, NULL,
729      GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO,
730      sizeof (struct RequestInfoMessage)},
731     {&handle_client_send_request, NULL,
732      GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
733      sizeof (struct SendMessageRequest)},
734     {&handle_client_send, NULL,
735      GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
736     {NULL, NULL, 0, 0}
737   };
738
739   /* setup notification */
740   client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
741   notifier =
742       GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
743   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
744   GNUNET_SERVER_add_handlers (server, handlers);
745 }
746
747
748 /**
749  * Shutdown clients subsystem.
750  */
751 void
752 GSC_CLIENTS_done ()
753 {
754   struct GSC_Client *c;
755
756   while (NULL != (c = client_head))  
757     handle_client_disconnect (NULL, c->client_handle);
758   GNUNET_SERVER_notification_context_destroy (notifier);
759   notifier = NULL;
760   GNUNET_SERVER_mst_destroy (client_mst);
761   client_mst = NULL;
762 }
763
764 /* end of gnunet-service-core_clients.c */