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