lon
[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.my_identity = GSC_my_identity;
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   car->was_solicited = GNUNET_NO;
344   if (0 ==
345       memcmp (&req->peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
346     GSC_CLIENTS_solicit_request (car);
347   else
348     GSC_SESSIONS_queue_request (car);
349   GNUNET_SERVER_receive_done (client, GNUNET_OK);
350 }
351
352
353 /**
354  * Handle CORE_SEND request.
355  *
356  * @param cls unused
357  * @param client the client issuing the request
358  * @param message the "struct SendMessage"
359  */
360 static void
361 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
362                     const struct GNUNET_MessageHeader *message)
363 {
364   const struct SendMessage *sm;
365   struct GSC_Client *c;
366   struct GSC_ClientActiveRequest *car;
367   uint16_t msize;
368
369   msize = ntohs (message->size);
370   if (msize <
371       sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
372   {
373     GNUNET_break (0);
374     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
375     return;
376   }
377   sm = (const struct SendMessage *) message;
378   msize -= sizeof (struct SendMessage);
379   GNUNET_break (0 == ntohl (sm->reserved));
380   c = find_client (client);
381   if (c == NULL)
382   {
383     /* client did not send INIT first! */
384     GNUNET_break (0);
385     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
386     return;
387   }
388   car = GNUNET_CONTAINER_multihashmap_get (c->requests, &sm->peer.hashPubKey);
389   if (NULL == car)
390   {
391     /* client did not request transmission first! */
392     GNUNET_break (0);
393     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
394     return;
395   }
396   GNUNET_assert (GNUNET_YES ==
397                  GNUNET_CONTAINER_multihashmap_remove (c->requests, 
398                                                        &sm->peer.hashPubKey,
399                                                        car));
400   car->cork = ntohl (sm->cork);
401   GNUNET_SERVER_mst_receive (client_mst,
402                              car, 
403                              (const char*) &sm[1], msize,
404                              GNUNET_YES,
405                              GNUNET_NO);
406   if (0 !=
407       memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))  
408     GSC_SESSIONS_dequeue_request (car);
409   GNUNET_free (car);  
410   GNUNET_SERVER_receive_done (client, GNUNET_OK);
411 }
412
413
414 /**
415  * Functions with this signature are called whenever a complete
416  * message is received by the tokenizer.  Used by the 'client_mst' for
417  * dispatching messages from clients to either the SESSION subsystem
418  * or other CLIENT (for loopback).
419  *
420  * @param cls closure
421  * @param client reservation request ('struct GSC_ClientActiveRequest')
422  * @param message the actual message
423  */
424 static void
425 client_tokenizer_callback (void *cls, void *client,
426                            const struct GNUNET_MessageHeader *message)
427 {
428   struct GSC_ClientActiveRequest *car = client;
429
430   if (0 ==
431       memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))  
432   {
433     GSC_CLIENTS_deliver_message (&GSC_my_identity, 
434                                  NULL, 0,
435                                  message,
436                                  ntohs (message->size),
437                                  GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);  
438     GSC_CLIENTS_deliver_message (&GSC_my_identity, 
439                                  NULL, 0,
440                                  message,
441                                  sizeof (struct GNUNET_MessageHeader),
442                                  GNUNET_CORE_OPTION_SEND_HDR_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);  
443   }
444   else
445     GSC_SESSIONS_transmit (car, message, car->cork);
446 }
447
448
449 /**
450  * Free client request records.
451  *
452  * @param cls NULL
453  * @param key identity of peer for which this is an active request
454  * @param value the 'struct GSC_ClientActiveRequest' to free
455  * @return GNUNET_YES (continue iteration)
456  */
457 static int
458 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
459                                void *value)
460 {
461   struct GSC_ClientActiveRequest *car = value;
462
463   GNUNET_assert (GNUNET_YES ==
464                  GNUNET_CONTAINER_multihashmap_remove (car->client_handle->requests,
465                                                        &car->target.hashPubKey,
466                                                        car));
467   GSC_SESSIONS_dequeue_request (car);
468   GNUNET_free (car);
469   return GNUNET_YES;
470 }
471
472
473 /**
474  * A client disconnected, clean up.
475  *
476  * @param cls closure
477  * @param client identification of the client
478  */
479 static void
480 handle_client_disconnect (void *cls,
481                           struct GNUNET_SERVER_Client *client)
482 {
483   struct GSC_Client *c;
484
485   if (client == NULL)
486     return;
487 #if DEBUG_CORE_CLIENT
488   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489               "Client %p has disconnected from core service.\n", client);
490 #endif
491   c = find_client (client);
492   if (c == NULL)
493     return; /* client never sent INIT */
494   GNUNET_CONTAINER_DLL_remove (client_head,
495                                client_tail,
496                                c);
497   if (c->requests != NULL)
498   {
499     GNUNET_CONTAINER_multihashmap_iterate (c->requests,
500                                            &destroy_active_client_request,
501                                            NULL);
502     GNUNET_CONTAINER_multihashmap_destroy (c->requests);
503   }
504   GSC_TYPEMAP_remove (c->types, c->tcnt);
505   GNUNET_free (c);
506 }
507
508
509 /**
510  * Tell a client that we are ready to receive the message.
511  *
512  * @param car request that is now ready; the responsibility
513  *        for the handle remains shared between CLIENTS
514  *        and SESSIONS after this call.
515  */
516 void
517 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
518 {
519   struct GSC_Client *c;
520   struct SendMessageReady smr;
521
522   c = car->client_handle;
523   smr.header.size = htons (sizeof (struct SendMessageReady));
524   smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
525   smr.size = htons (car->msize);
526   smr.smr_id = car->smr_id;
527   smr.peer = car->target;
528   send_to_client (c, &smr.header, GNUNET_NO);
529 }
530
531
532 /**
533  * Tell a client that we will never be ready to receive the
534  * given message in time (disconnect or timeout).
535  *
536  * @param car request that now permanently failed; the
537  *        responsibility for the handle is now returned
538  *        to CLIENTS (SESSIONS is done with it).
539  */
540 void
541 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
542 {
543   GNUNET_assert (GNUNET_YES ==
544                  destroy_active_client_request (NULL, &car->target.hashPubKey, car));  
545 }
546
547
548 /**
549  * Notify a particular client about a change to existing connection to
550  * one of our neighbours (check if the client is interested).  Called
551  * from 'GSC_SESSIONS_notify_client_about_sessions'.
552  *
553  * @param client client to notify
554  * @param neighbour identity of the neighbour that changed status
555  * @param atsi performance information about neighbour
556  * @param atsi_count number of entries in 'ats' array
557  * @param tmap_old previous type map for the neighbour, NULL for disconnect
558  * @param tmap_new updated type map for the neighbour, NULL for disconnect
559  */
560 void
561 GDS_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
562                                            const struct GNUNET_PeerIdentity *neighbour,
563                                            const struct GNUNET_TRANSPORT_ATS_Information *atsi,
564                                            unsigned int atsi_count,
565                                            const struct GSC_TypeMap *tmap_old,
566                                            const struct GSC_TypeMap *tmap_new)
567 {
568   struct ConnectNotifyMessage *cnm;
569   size_t size;
570   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
571   struct GNUNET_TRANSPORT_ATS_Information *a;
572   struct DisconnectNotifyMessage dcm;
573   int old_match;
574   int new_match;
575
576   old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
577   new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
578   if (old_match == new_match)
579     return; /* no change */
580   if (old_match == GNUNET_NO)
581   {
582     /* send connect */  
583     size =
584       sizeof (struct ConnectNotifyMessage) +
585       (atsi_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
586     if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
587       {
588         GNUNET_break (0);
589         /* recovery strategy: throw away performance data */
590         atsi_count = 0;
591         size = sizeof (struct ConnectNotifyMessage);
592       }
593     cnm = (struct ConnectNotifyMessage *) buf;
594     cnm->header.size = htons (size);
595     cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
596     cnm->ats_count = htonl (atsi_count);
597     a = &cnm->ats;
598     memcpy (a, atsi,
599             sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
600     a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
601     a[atsi_count].value = htonl (0);
602 #if DEBUG_CORE_CLIENT
603     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
604                 "Sending `%s' message to client.\n",
605                 "NOTIFY_CONNECT");
606 #endif
607     cnm->peer = *neighbour;
608     send_to_client (client, &cnm->header, GNUNET_NO);
609   }
610   else
611   {
612     /* send disconnect */
613     dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
614     dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
615     dcm.reserved = htonl (0);
616     dcm.peer = *neighbour;
617     send_to_client (client, &dcm.header, GNUNET_NO);
618   }
619 }
620
621
622 /**
623  * Notify all clients about a change to existing session.
624  * Called from SESSIONS whenever there is a change in sessions
625  * or types processed by the respective peer.
626  *
627  * @param neighbour identity of the neighbour that changed status
628  * @param atsi performance information about neighbour
629  * @param atsi_count number of entries in 'ats' array
630  * @param tmap_old previous type map for the neighbour, NULL for disconnect
631  * @param tmap_new updated type map for the neighbour, NULL for disconnect
632  */
633 void
634 GDS_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
635                                             const struct GNUNET_TRANSPORT_ATS_Information *atsi,
636                                             unsigned int atsi_count,
637                                             const struct GSC_TypeMap *tmap_old,
638                                             const struct GSC_TypeMap *tmap_new)
639 {
640   struct GSC_Client *c;
641
642   for (c = client_head; c != NULL; c = c->next)
643     GDS_CLIENTS_notify_client_about_neighbour (c, neighbour, atsi,
644                                                atsi_count, 
645                                                tmap_old, tmap_new);
646 }
647
648
649 /**
650  * Deliver P2P message to interested clients.  Caller must have checked
651  * that the sending peer actually lists the given message type as one 
652  * of its types.
653  *
654  * @param sender peer who sent us the message 
655  * @param atsi performance information about neighbour
656  * @param atsi_count number of entries in 'ats' array
657  * @param msg the message
658  * @param msize number of bytes to transmit
659  * @param options options for checking which clients should
660  *        receive the message
661  */
662 void
663 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
664                              const struct GNUNET_TRANSPORT_ATS_Information *atsi,
665                              unsigned int atsi_count,
666                              const struct GNUNET_MessageHeader *msg,
667                              uint16_t msize,
668                              int options)
669 {
670   size_t size = msize + sizeof (struct NotifyTrafficMessage) +
671       atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
672   char buf[size];
673   struct NotifyTrafficMessage *ntm;
674   struct GNUNET_TRANSPORT_ATS_Information *a;
675
676   if (0 == options)
677   {
678     GNUNET_snprintf (buf, sizeof (buf),
679                      gettext_noop ("# bytes of messages of type %u received"),
680                      (unsigned int) ntohs (msg->type));
681     GNUNET_STATISTICS_update (GSC_stats, buf, msize, GNUNET_NO);
682   }
683   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
684   {
685     GNUNET_break (0);
686     /* recovery strategy: throw performance data away... */
687     atsi_count = 0;
688     size = msize + sizeof (struct NotifyTrafficMessage);
689   }
690 #if DEBUG_CORE
691   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692               "Core service passes message from `%4s' of type %u to client.\n",
693               GNUNET_i2s (sender),
694               (unsigned int) ntohs (msg->type));
695 #endif
696   ntm = (struct NotifyTrafficMessage *) buf;
697   ntm->header.size = htons (size);
698   ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
699   ntm->ats_count = htonl (atsi_count);
700   ntm->peer = *sender;
701   a = &ntm->ats;
702   memcpy (a, atsi,
703           sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
704   a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
705   a[atsi_count].value = htonl (0);
706   memcpy (&a[atsi_count + 1], msg, msize);
707   send_to_all_clients (&ntm->header, GNUNET_YES, 
708                        options, ntohs (msg->type));
709 }
710
711
712 /**
713  * Initialize clients subsystem.
714  *
715  * @param server handle to server clients connect to
716  */
717 void
718 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
719 {
720   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
721     {&handle_client_init, NULL,
722      GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
723     {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
724      GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
725      sizeof (struct GNUNET_MessageHeader)},
726     {&GSC_SESSIONS_handle_client_have_peer, NULL,
727      GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
728      sizeof (struct GNUNET_MessageHeader) +
729      sizeof (struct GNUNET_PeerIdentity)},
730     {&handle_client_send_request, NULL,
731      GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
732      sizeof (struct SendMessageRequest)},
733     {&handle_client_send, NULL,
734      GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
735     {NULL, NULL, 0, 0}
736   };
737
738   /* setup notification */
739   client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
740   notifier =
741       GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
742   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
743   GNUNET_SERVER_add_handlers (server, handlers);
744 }
745
746
747 /**
748  * Shutdown clients subsystem.
749  */
750 void
751 GSC_CLIENTS_done ()
752 {
753   struct GSC_Client *c;
754
755   while (NULL != (c = client_head))  
756     handle_client_disconnect (NULL, c->client_handle);
757   GNUNET_SERVER_notification_context_destroy (notifier);
758   notifier = NULL;
759   GNUNET_SERVER_mst_destroy (client_mst);
760   client_mst = NULL;
761 }
762
763 /* end of gnunet-service-core_clients.c */