47bbd98db18432828a47742269fb92f7c0cf76a0
[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
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   if (c->tcnt == 0)
194     return GNUNET_YES; /* peer without handlers matches ALL */
195   for (i=0;i<c->tcnt;i++)
196     if (type == c->types[i])
197       return GNUNET_YES;
198   return GNUNET_NO;
199 }
200
201
202 /**
203  * Send a message to all of our current clients that have the right
204  * options set.
205  *
206  * @param msg message to multicast
207  * @param can_drop can this message be discarded if the queue is too long
208  * @param options mask to use
209  * @param type type of the embedded message, 0 for none
210  */
211 static void
212 send_to_all_clients (const struct GNUNET_MessageHeader *msg, 
213                      int can_drop,
214                      int options,
215                      uint16_t type)
216 {
217   struct GSC_Client *c;
218
219   for (c = client_head; c != NULL; c = c->next)
220   {
221     if ( (0 == (options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) &&
222          (GNUNET_YES == type_match (type, c)) )
223       continue; /* not the full message, but we'd like the full one! */
224     if ( (0 == (c->options & options)) &&
225          (GNUNET_YES != type_match (type, c)) )
226       continue; /* neither options nor type match permit the message */
227 #if DEBUG_CORE
228     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229                 "Sending message to client interested in messages of type %u.\n",
230                 (unsigned int) type);
231 #endif
232     send_to_client (c, msg, can_drop);
233   }
234 }
235
236
237 /**
238  * Handle CORE_INIT request.
239  *
240  * @param cls unused
241  * @param client new client that sent INIT
242  * @param message the 'struct InitMessage' (presumably)
243  */
244 static void
245 handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
246                     const struct GNUNET_MessageHeader *message)
247 {
248   const struct InitMessage *im;
249   struct InitReplyMessage irm;
250   struct GSC_Client *c;
251   uint16_t msize;
252   const uint16_t *types;
253   uint16_t *wtypes;
254   unsigned int i;
255
256   /* check that we don't have an entry already */
257   c = find_client (client);
258   if (NULL != c)
259   {
260     GNUNET_break (0);
261     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
262     return;
263   }
264   msize = ntohs (message->size);
265   if (msize < sizeof (struct InitMessage))
266   {
267     GNUNET_break (0);
268     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
269     return;
270   }
271   GNUNET_SERVER_notification_context_add (notifier, client);
272   im = (const struct InitMessage *) message;
273   types = (const uint16_t *) &im[1];
274   msize -= sizeof (struct InitMessage);
275   c = GNUNET_malloc (sizeof (struct GSC_Client) + msize);
276   c->client_handle = client;
277   c->tcnt = msize / sizeof (uint16_t);
278   c->options = ntohl (im->options);
279   c->types = (const uint16_t *) &c[1];
280   wtypes = (uint16_t *) & c[1];
281   for (i = 0; i < c->tcnt; i++)
282     wtypes[i] = ntohs (types[i]);
283   GSC_TYPEMAP_add (wtypes, c->tcnt);
284   GNUNET_CONTAINER_DLL_insert (client_head,
285                                client_tail,
286                                c);
287 #if DEBUG_CORE
288   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289               "Client connecting to core service is interested in %u message types\n", 
290               (unsigned int) c->tcnt);
291 #endif
292   /* send init reply message */
293   irm.header.size = htons (sizeof (struct InitReplyMessage));
294   irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
295   irm.reserved = htonl (0);
296   irm.my_identity = GSC_my_identity;
297   send_to_client (c, &irm.header, GNUNET_NO);
298   GSC_SESSIONS_notify_client_about_sessions (c);
299   GNUNET_SERVER_receive_done (client, GNUNET_OK);
300 }
301
302
303 /**
304  * Handle CORE_SEND_REQUEST message.
305  *
306  * @param cls unused
307  * @param client new client that sent CORE_SEND_REQUEST
308  * @param message the 'struct InitMessage' (presumably)
309  */
310 static void
311 handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
312                             const struct GNUNET_MessageHeader *message)
313 {
314   const struct SendMessageRequest *req;
315   struct GSC_Client *c;
316   struct GSC_ClientActiveRequest *car;
317
318   req = (const struct SendMessageRequest *) message;
319   c = find_client (client);
320   if (c == NULL)
321   {
322     /* client did not send INIT first! */
323     GNUNET_break (0);
324     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
325     return;
326   }
327   if (c->requests == NULL)
328     c->requests = GNUNET_CONTAINER_multihashmap_create (16);
329 #if DEBUG_CORE
330   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331               "Client asked for transmission to `%s'\n",
332               GNUNET_i2s (&req->peer));
333 #endif
334   car = GNUNET_CONTAINER_multihashmap_get (c->requests, &req->peer.hashPubKey);
335   if (car == NULL)
336   {
337     /* create new entry */
338     car = GNUNET_malloc (sizeof (struct GSC_ClientActiveRequest));
339     GNUNET_assert (GNUNET_OK ==
340                    GNUNET_CONTAINER_multihashmap_put (c->requests,
341                                                       &req->peer.hashPubKey,
342                                                       car,
343                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
344     car->client_handle = c;
345   }
346   else
347   {
348     GSC_SESSIONS_dequeue_request (car);
349   }
350   car->target = req->peer;
351   car->deadline = GNUNET_TIME_absolute_ntoh (req->deadline);
352   car->priority = ntohl (req->priority);
353   car->msize = ntohs (req->size);
354   car->smr_id = req->smr_id;
355   car->was_solicited = GNUNET_NO;
356   if (0 ==
357       memcmp (&req->peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
358     GSC_CLIENTS_solicit_request (car);
359   else
360     GSC_SESSIONS_queue_request (car);
361   GNUNET_SERVER_receive_done (client, GNUNET_OK);
362 }
363
364
365 /**
366  * Closure for the 'client_tokenizer_callback'.
367  */
368 struct TokenizerContext
369 {
370
371   /**
372    * Active request handle for the message.
373    */ 
374   struct GSC_ClientActiveRequest *car;
375
376   /**
377    * Is corking allowed (set only once we have the real message).
378    */
379   int cork;
380
381 };
382
383
384 /**
385  * Handle CORE_SEND request.
386  *
387  * @param cls unused
388  * @param client the client issuing the request
389  * @param message the "struct SendMessage"
390  */
391 static void
392 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
393                     const struct GNUNET_MessageHeader *message)
394 {
395   const struct SendMessage *sm;
396   struct GSC_Client *c;
397   struct TokenizerContext tc;
398   uint16_t msize;
399
400   msize = ntohs (message->size);
401   if (msize <
402       sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
403   {
404     GNUNET_break (0);
405     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
406     return;
407   }
408   sm = (const struct SendMessage *) message;
409   msize -= sizeof (struct SendMessage);
410   GNUNET_break (0 == ntohl (sm->reserved));
411   c = find_client (client);
412   if (c == NULL)
413   {
414     /* client did not send INIT first! */
415     GNUNET_break (0);
416     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
417     return;
418   }
419   tc.car = GNUNET_CONTAINER_multihashmap_get (c->requests, &sm->peer.hashPubKey);
420   if (NULL == tc.car)
421   {
422     /* client did not request transmission first! */
423     GNUNET_break (0);
424     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
425     return;
426   }
427   GNUNET_assert (GNUNET_YES ==
428                  GNUNET_CONTAINER_multihashmap_remove (c->requests, 
429                                                        &sm->peer.hashPubKey,
430                                                        tc.car));
431   tc.cork = ntohl (sm->cork);
432 #if DEBUG_CORE
433   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434               "Client asked for transmission of %u bytes to `%s' %s\n",
435               msize,
436               GNUNET_i2s (&sm->peer),
437               tc.cork ? "now" : "");
438 #endif
439   GNUNET_SERVER_mst_receive (client_mst,
440                              &tc, 
441                              (const char*) &sm[1], msize,
442                              GNUNET_YES,
443                              GNUNET_NO);
444   if (0 !=
445       memcmp (&tc.car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))  
446     GSC_SESSIONS_dequeue_request (tc.car);
447   GNUNET_free (tc.car);  
448   GNUNET_SERVER_receive_done (client, GNUNET_OK);
449 }
450
451
452 /**
453  * Functions with this signature are called whenever a complete
454  * message is received by the tokenizer.  Used by the 'client_mst' for
455  * dispatching messages from clients to either the SESSION subsystem
456  * or other CLIENT (for loopback).
457  *
458  * @param cls closure
459  * @param client reservation request ('struct GSC_ClientActiveRequest')
460  * @param message the actual message
461  */
462 static void
463 client_tokenizer_callback (void *cls, void *client,
464                            const struct GNUNET_MessageHeader *message)
465 {
466   struct TokenizerContext *tc = client;
467   struct GSC_ClientActiveRequest *car = tc->car;
468
469   if (0 ==
470       memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))  
471   {
472 #if DEBUG_CORE
473     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474                 "Delivering message of type %u to myself\n",
475                 ntohs (message->type));
476 #endif
477     GSC_CLIENTS_deliver_message (&GSC_my_identity, 
478                                  NULL, 0,
479                                  message,
480                                  ntohs (message->size),
481                                  GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);  
482     GSC_CLIENTS_deliver_message (&GSC_my_identity, 
483                                  NULL, 0,
484                                  message,
485                                  sizeof (struct GNUNET_MessageHeader),
486                                  GNUNET_CORE_OPTION_SEND_HDR_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);  
487   }
488   else
489   {
490 #if DEBUG_CORE
491     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492                 "Delivering message of type %u to %s\n",
493                 ntohs (message->type),
494                 GNUNET_i2s (&car->target));
495 #endif
496     GSC_SESSIONS_transmit (car, message, tc->cork);
497   }
498 }
499
500
501 /**
502  * Free client request records.
503  *
504  * @param cls NULL
505  * @param key identity of peer for which this is an active request
506  * @param value the 'struct GSC_ClientActiveRequest' to free
507  * @return GNUNET_YES (continue iteration)
508  */
509 static int
510 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
511                                void *value)
512 {
513   struct GSC_ClientActiveRequest *car = value;
514
515   GNUNET_assert (GNUNET_YES ==
516                  GNUNET_CONTAINER_multihashmap_remove (car->client_handle->requests,
517                                                        &car->target.hashPubKey,
518                                                        car));
519   GSC_SESSIONS_dequeue_request (car);
520   GNUNET_free (car);
521   return GNUNET_YES;
522 }
523
524
525 /**
526  * A client disconnected, clean up.
527  *
528  * @param cls closure
529  * @param client identification of the client
530  */
531 static void
532 handle_client_disconnect (void *cls,
533                           struct GNUNET_SERVER_Client *client)
534 {
535   struct GSC_Client *c;
536
537   if (client == NULL)
538     return;
539 #if DEBUG_CORE
540   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541               "Client %p has disconnected from core service.\n", client);
542 #endif
543   c = find_client (client);
544   if (c == NULL)
545     return; /* client never sent INIT */
546   GNUNET_CONTAINER_DLL_remove (client_head,
547                                client_tail,
548                                c);
549   if (c->requests != NULL)
550   {
551     GNUNET_CONTAINER_multihashmap_iterate (c->requests,
552                                            &destroy_active_client_request,
553                                            NULL);
554     GNUNET_CONTAINER_multihashmap_destroy (c->requests);
555   }
556   GSC_TYPEMAP_remove (c->types, c->tcnt);
557   GNUNET_free (c);
558 }
559
560
561 /**
562  * Tell a client that we are ready to receive the message.
563  *
564  * @param car request that is now ready; the responsibility
565  *        for the handle remains shared between CLIENTS
566  *        and SESSIONS after this call.
567  */
568 void
569 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
570 {
571   struct GSC_Client *c;
572   struct SendMessageReady smr;
573
574   c = car->client_handle;
575   smr.header.size = htons (sizeof (struct SendMessageReady));
576   smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
577   smr.size = htons (car->msize);
578   smr.smr_id = car->smr_id;
579   smr.peer = car->target;
580   send_to_client (c, &smr.header, GNUNET_NO);
581 }
582
583
584 /**
585  * Tell a client that we will never be ready to receive the
586  * given message in time (disconnect or timeout).
587  *
588  * @param car request that now permanently failed; the
589  *        responsibility for the handle is now returned
590  *        to CLIENTS (SESSIONS is done with it).
591  */
592 void
593 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
594 {
595   GNUNET_assert (GNUNET_YES ==
596                  GNUNET_CONTAINER_multihashmap_remove (car->client_handle->requests,
597                                                        &car->target.hashPubKey,
598                                                        car));
599   GNUNET_free (car);
600 }
601
602
603 /**
604  * Notify a particular client about a change to existing connection to
605  * one of our neighbours (check if the client is interested).  Called
606  * from 'GSC_SESSIONS_notify_client_about_sessions'.
607  *
608  * @param client client to notify
609  * @param neighbour identity of the neighbour that changed status
610  * @param atsi performance information about neighbour
611  * @param atsi_count number of entries in 'ats' array
612  * @param tmap_old previous type map for the neighbour, NULL for disconnect
613  * @param tmap_new updated type map for the neighbour, NULL for disconnect
614  * @param is_new GNUNET_YES if this is a completely new neighbour
615  */
616 void
617 GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
618                                            const struct GNUNET_PeerIdentity *neighbour,
619                                            const struct GNUNET_ATS_Information *atsi,
620                                            unsigned int atsi_count,
621                                            const struct GSC_TypeMap *tmap_old,
622                                            const struct GSC_TypeMap *tmap_new)
623 {
624   struct ConnectNotifyMessage *cnm;
625   size_t size;
626   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
627   struct GNUNET_ATS_Information *a;
628   struct DisconnectNotifyMessage dcm;
629   int old_match;
630   int new_match;
631
632   old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
633   new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
634   if (client->tcnt == 0)
635   {
636     /* empty list matches ALL (if not NULL) */
637     if (tmap_new != NULL)
638       new_match = GNUNET_YES; 
639     if (tmap_old != NULL)
640       old_match = GNUNET_YES;
641   }
642   if (old_match == new_match)
643     return; /* no change */
644   if (old_match == GNUNET_NO)
645   {
646     /* send connect */  
647     size =
648       sizeof (struct ConnectNotifyMessage) +
649       (atsi_count) * sizeof (struct GNUNET_ATS_Information);
650     if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
651       {
652         GNUNET_break (0);
653         /* recovery strategy: throw away performance data */
654         atsi_count = 0;
655         size = sizeof (struct ConnectNotifyMessage);
656       }
657     cnm = (struct ConnectNotifyMessage *) buf;
658     cnm->header.size = htons (size);
659     cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
660     cnm->ats_count = htonl (atsi_count);
661     a = (struct GNUNET_ATS_Information* ) &cnm[1];
662     memcpy (a, atsi,
663             sizeof (struct GNUNET_ATS_Information) * atsi_count);
664 #if DEBUG_CORE
665     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
666                 "Sending `%s' message to client.\n",
667                 "NOTIFY_CONNECT");
668 #endif
669     cnm->peer = *neighbour;
670     send_to_client (client, &cnm->header, GNUNET_NO);
671   }
672   else
673   {
674     /* send disconnect */
675     dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
676     dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
677     dcm.reserved = htonl (0);
678     dcm.peer = *neighbour;
679     send_to_client (client, &dcm.header, GNUNET_NO);
680   }
681 }
682
683
684 /**
685  * Notify all clients about a change to existing session.
686  * Called from SESSIONS whenever there is a change in sessions
687  * or types processed by the respective peer.
688  *
689  * @param neighbour identity of the neighbour that changed status
690  * @param atsi performance information about neighbour
691  * @param atsi_count number of entries in 'ats' array
692  * @param tmap_old previous type map for the neighbour, NULL for disconnect
693  * @param tmap_new updated type map for the neighbour, NULL for disconnect
694  */
695 void
696 GSC_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
697                                             const struct GNUNET_ATS_Information *atsi,
698                                             unsigned int atsi_count,
699                                             const struct GSC_TypeMap *tmap_old,
700                                             const struct GSC_TypeMap *tmap_new)
701 {
702   struct GSC_Client *c;
703
704   for (c = client_head; c != NULL; c = c->next)
705     GSC_CLIENTS_notify_client_about_neighbour (c, neighbour, atsi,
706                                                atsi_count, 
707                                                tmap_old, tmap_new);
708 }
709
710
711 /**
712  * Deliver P2P message to interested clients.  Caller must have checked
713  * that the sending peer actually lists the given message type as one 
714  * of its types.
715  *
716  * @param sender peer who sent us the message 
717  * @param atsi performance information about neighbour
718  * @param atsi_count number of entries in 'ats' array
719  * @param msg the message
720  * @param msize number of bytes to transmit
721  * @param options options for checking which clients should
722  *        receive the message
723  */
724 void
725 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
726                              const struct GNUNET_ATS_Information *atsi,
727                              unsigned int atsi_count,
728                              const struct GNUNET_MessageHeader *msg,
729                              uint16_t msize,
730                              int options)
731 {
732   size_t size = msize + sizeof (struct NotifyTrafficMessage) +
733       atsi_count * sizeof (struct GNUNET_ATS_Information);
734   char buf[size];
735   struct NotifyTrafficMessage *ntm;
736   struct GNUNET_ATS_Information *a;
737
738   if (0 == options)
739   {
740     GNUNET_snprintf (buf, sizeof (buf),
741                      gettext_noop ("# bytes of messages of type %u received"),
742                      (unsigned int) ntohs (msg->type));
743     GNUNET_STATISTICS_update (GSC_stats, buf, msize, GNUNET_NO);
744   }
745   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
746   {
747     GNUNET_break (0);
748     /* recovery strategy: throw performance data away... */
749     atsi_count = 0;
750     size = msize + sizeof (struct NotifyTrafficMessage);
751   }
752 #if DEBUG_CORE
753   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754               "Core service passes message from `%4s' of type %u to client.\n",
755               GNUNET_i2s (sender),
756               (unsigned int) ntohs (msg->type));
757 #endif
758   GSC_SESSIONS_add_to_typemap (sender, ntohs (msg->type));
759   ntm = (struct NotifyTrafficMessage *) buf;
760   ntm->header.size = htons (size);
761   ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
762   ntm->ats_count = htonl (atsi_count);
763   ntm->peer = *sender;
764   a = &ntm->ats;
765   memcpy (a, atsi,
766           sizeof (struct GNUNET_ATS_Information) * atsi_count);
767   a[atsi_count].type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
768   a[atsi_count].value = htonl (0);
769   memcpy (&a[atsi_count + 1], msg, msize);
770   send_to_all_clients (&ntm->header, GNUNET_YES, 
771                        options, ntohs (msg->type));
772 }
773
774
775 /**
776  * Initialize clients subsystem.
777  *
778  * @param server handle to server clients connect to
779  */
780 void
781 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
782 {
783   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
784     {&handle_client_init, NULL,
785      GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
786     {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
787      GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
788      sizeof (struct GNUNET_MessageHeader)},
789     {&GSC_SESSIONS_handle_client_have_peer, NULL,
790      GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
791      sizeof (struct GNUNET_MessageHeader) +
792      sizeof (struct GNUNET_PeerIdentity)},
793     {&handle_client_send_request, NULL,
794      GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
795      sizeof (struct SendMessageRequest)},
796     {&handle_client_send, NULL,
797      GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
798     {NULL, NULL, 0, 0}
799   };
800
801   /* setup notification */
802   client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
803   notifier =
804       GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
805   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
806   GNUNET_SERVER_add_handlers (server, handlers);
807 }
808
809
810 /**
811  * Shutdown clients subsystem.
812  */
813 void
814 GSC_CLIENTS_done ()
815 {
816   struct GSC_Client *c;
817
818   while (NULL != (c = client_head))  
819     handle_client_disconnect (NULL, c->client_handle);
820   if (NULL != notifier)
821   {
822     GNUNET_SERVER_notification_context_destroy (notifier);
823     notifier = NULL;
824   }
825   GNUNET_SERVER_mst_destroy (client_mst);
826   client_mst = NULL;
827 }
828
829 /* end of gnunet-service-core_clients.c */