2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file core/gnunet-service-core_clients.c
23 * @brief code for managing interactions with clients of core service
24 * @author Christian Grothoff
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"
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).
42 #define MAX_NOTIFY_QUEUE 1024
46 * Data structure for each client connected to the core service.
51 * Clients are kept in a linked list.
53 struct GSC_Client *next;
56 * Clients are kept in a linked list.
58 struct GSC_Client *prev;
61 * Handle for the client with the server API.
63 struct GNUNET_SERVER_Client *client_handle;
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!
70 const uint16_t *types;
73 * Map of peer identities to active transmission requests of this
74 * client to the peer (of type 'struct GSC_ClientActiveRequest').
76 struct GNUNET_CONTAINER_MultiHashMap *requests;
79 * Map containing all peers that this client knows we're connected to.
81 struct GNUNET_CONTAINER_MultiHashMap *connectmap;
84 * Options for messages this client cares about,
85 * see GNUNET_CORE_OPTION_ values.
90 * Number of types of incoming messages this client
91 * specifically cares about. Size of the "types" array.
99 * Big "or" of all client options.
101 static uint32_t all_client_options;
104 * Head of linked list of our clients.
106 static struct GSC_Client *client_head;
109 * Tail of linked list of our clients.
111 static struct GSC_Client *client_tail;
114 * Context for notifications we need to send to our clients.
116 static struct GNUNET_SERVER_NotificationContext *notifier;
119 * Tokenizer for messages received from clients.
121 static struct GNUNET_SERVER_MessageStreamTokenizer *client_mst;
125 * Lookup our client struct given the server's client handle.
127 * @param client server client handle to look up
128 * @return our client handle for the client
130 static struct GSC_Client *
131 find_client (struct GNUNET_SERVER_Client *client)
133 struct GSC_Client *c;
136 while ((c != NULL) && (c->client_handle != client))
143 * Send a message to one of our clients.
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?
151 send_to_client (struct GSC_Client *client,
152 const struct GNUNET_MessageHeader *msg, int can_drop)
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,
164 * Send a message to one of our clients.
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?
172 GSC_CLIENTS_send_to_client (struct GNUNET_SERVER_Client *client,
173 const struct GNUNET_MessageHeader *msg,
176 struct GSC_Client *c;
178 c = find_client (client);
184 send_to_client (c, msg, can_drop);
189 * Test if the client is interested in messages of the given type.
191 * @param type message type
192 * @param c client to test
193 * @return GNUNET_YES if 'c' is interested, GNUNET_NO if not.
196 type_match (uint16_t type, struct GSC_Client *c)
201 return GNUNET_YES; /* peer without handlers matches ALL */
202 for (i = 0; i < c->tcnt; i++)
203 if (type == c->types[i])
210 * Send a message to all of our current clients that have the right
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
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)
225 struct GSC_Client *c;
228 for (c = client_head; c != NULL; c = c->next)
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) ) )
239 if ( (0 != (options & GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND)) &&
240 (0 != (c->options & GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND)) )
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 "Sending %u message with %u bytes to client interested in messages of type %u.\n",
246 (unsigned int) type);
247 GNUNET_assert ( (0 == (c->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) ||
248 (GNUNET_YES != tm) ||
250 GNUNET_CONTAINER_multihashmap_contains (c->connectmap,
251 &partner->hashPubKey)) );
252 send_to_client (c, msg, can_drop);
258 * Handle CORE_INIT request.
261 * @param client new client that sent INIT
262 * @param message the 'struct InitMessage' (presumably)
265 handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
266 const struct GNUNET_MessageHeader *message)
268 const struct InitMessage *im;
269 struct InitReplyMessage irm;
270 struct GSC_Client *c;
272 const uint16_t *types;
276 /* check that we don't have an entry already */
277 c = find_client (client);
281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
284 msize = ntohs (message->size);
285 if (msize < sizeof (struct InitMessage))
288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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,
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);
327 * Handle CORE_SEND_REQUEST message.
330 * @param client new client that sent CORE_SEND_REQUEST
331 * @param message the 'struct SendMessageRequest' (presumably)
334 handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
335 const struct GNUNET_MessageHeader *message)
337 const struct SendMessageRequest *req;
338 struct GSC_Client *c;
339 struct GSC_ClientActiveRequest *car;
342 req = (const struct SendMessageRequest *) message;
343 c = find_client (client);
346 /* client did not send INIT first! */
348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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));
358 memcmp (&req->peer, &GSC_my_identity,
359 sizeof (struct GNUNET_PeerIdentity)));
360 if ((!is_loopback) &&
362 GNUNET_CONTAINER_multihashmap_contains (c->connectmap,
363 &req->peer.hashPubKey)))
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,
370 ("# send requests dropped (disconnected)"), 1,
372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
376 car = GNUNET_CONTAINER_multihashmap_get (c->requests, &req->peer.hashPubKey);
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,
385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
386 car->client_handle = c;
390 GSC_SESSIONS_dequeue_request (car);
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;
400 /* loopback, satisfy immediately */
401 GSC_CLIENTS_solicit_request (car);
402 GNUNET_SERVER_receive_done (client, GNUNET_OK);
405 GSC_SESSIONS_queue_request (car);
406 GNUNET_SERVER_receive_done (client, GNUNET_OK);
411 * Closure for the 'client_tokenizer_callback'.
413 struct TokenizerContext
417 * Active request handle for the message.
419 struct GSC_ClientActiveRequest *car;
422 * Is corking allowed (set only once we have the real message).
430 * Handle CORE_SEND request.
433 * @param client the client issuing the request
434 * @param message the "struct SendMessage"
437 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
438 const struct GNUNET_MessageHeader *message)
440 const struct SendMessage *sm;
441 struct GSC_Client *c;
442 struct TokenizerContext tc;
445 msize = ntohs (message->size);
447 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
453 sm = (const struct SendMessage *) message;
454 msize -= sizeof (struct SendMessage);
455 GNUNET_break (0 == ntohl (sm->reserved));
456 c = find_client (client);
459 /* client did not send INIT first! */
461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
465 GNUNET_CONTAINER_multihashmap_get (c->requests, &sm->peer.hashPubKey);
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,
475 ("# messages discarded (session disconnected)"),
477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
480 GNUNET_assert (GNUNET_YES ==
481 GNUNET_CONTAINER_multihashmap_remove (c->requests,
482 &sm->peer.hashPubKey,
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);
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);
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).
506 * @param client reservation request ('struct GSC_ClientActiveRequest')
507 * @param message the actual message
510 client_tokenizer_callback (void *cls, void *client,
511 const struct GNUNET_MessageHeader *message)
513 struct TokenizerContext *tc = client;
514 struct GSC_ClientActiveRequest *car = tc->car;
517 memcmp (&car->target, &GSC_my_identity,
518 sizeof (struct GNUNET_PeerIdentity)))
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);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Delivering message of type %u to %s\n", ntohs (message->type),
540 GNUNET_i2s (&car->target));
541 GSC_CLIENTS_deliver_message (&car->target, NULL, 0, message,
542 ntohs (message->size),
543 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
544 GSC_CLIENTS_deliver_message (&car->target, NULL, 0, message,
545 sizeof (struct GNUNET_MessageHeader),
546 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
547 GSC_SESSIONS_transmit (car, message, tc->cork);
553 * Free client request records.
556 * @param key identity of peer for which this is an active request
557 * @param value the 'struct GSC_ClientActiveRequest' to free
558 * @return GNUNET_YES (continue iteration)
561 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
564 struct GSC_ClientActiveRequest *car = value;
566 GNUNET_assert (GNUNET_YES ==
567 GNUNET_CONTAINER_multihashmap_remove (car->
568 client_handle->requests,
569 &car->target.hashPubKey,
571 GSC_SESSIONS_dequeue_request (car);
578 * A client disconnected, clean up.
581 * @param client identification of the client
584 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
586 struct GSC_Client *c;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Client %p has disconnected from core service.\n", client);
592 c = find_client (client);
594 return; /* client never sent INIT */
595 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, c);
596 if (c->requests != NULL)
598 GNUNET_CONTAINER_multihashmap_iterate (c->requests,
599 &destroy_active_client_request,
601 GNUNET_CONTAINER_multihashmap_destroy (c->requests);
603 GNUNET_CONTAINER_multihashmap_destroy (c->connectmap);
604 c->connectmap = NULL;
605 GSC_TYPEMAP_remove (c->types, c->tcnt);
608 /* recalculate 'all_client_options' */
609 all_client_options = 0;
610 for (c = client_head; NULL != c ; c = c->next)
611 all_client_options |= c->options;
616 * Tell a client that we are ready to receive the message.
618 * @param car request that is now ready; the responsibility
619 * for the handle remains shared between CLIENTS
620 * and SESSIONS after this call.
623 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
625 struct GSC_Client *c;
626 struct SendMessageReady smr;
628 c = car->client_handle;
630 GNUNET_CONTAINER_multihashmap_contains (c->connectmap,
631 &car->target.hashPubKey))
633 /* connection has gone down since, drop request */
635 memcmp (&car->target, &GSC_my_identity,
636 sizeof (struct GNUNET_PeerIdentity)));
637 GSC_SESSIONS_dequeue_request (car);
638 GSC_CLIENTS_reject_request (car);
641 smr.header.size = htons (sizeof (struct SendMessageReady));
642 smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
643 smr.size = htons (car->msize);
644 smr.smr_id = car->smr_id;
645 smr.peer = car->target;
646 send_to_client (c, &smr.header, GNUNET_NO);
651 * Tell a client that we will never be ready to receive the
652 * given message in time (disconnect or timeout).
654 * @param car request that now permanently failed; the
655 * responsibility for the handle is now returned
656 * to CLIENTS (SESSIONS is done with it).
659 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
661 GNUNET_assert (GNUNET_YES ==
662 GNUNET_CONTAINER_multihashmap_remove (car->
663 client_handle->requests,
664 &car->target.hashPubKey,
671 * Notify a particular client about a change to existing connection to
672 * one of our neighbours (check if the client is interested). Called
673 * from 'GSC_SESSIONS_notify_client_about_sessions'.
675 * @param client client to notify
676 * @param neighbour identity of the neighbour that changed status
677 * @param atsi performance information about neighbour
678 * @param atsi_count number of entries in 'ats' array
679 * @param tmap_old previous type map for the neighbour, NULL for disconnect
680 * @param tmap_new updated type map for the neighbour, NULL for disconnect
683 GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
684 const struct GNUNET_PeerIdentity
686 const struct GNUNET_ATS_Information
687 *atsi, unsigned int atsi_count,
688 const struct GSC_TypeMap *tmap_old,
689 const struct GSC_TypeMap *tmap_new)
691 struct ConnectNotifyMessage *cnm;
693 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
694 struct GNUNET_ATS_Information *a;
695 struct DisconnectNotifyMessage dcm;
699 old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
700 new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
701 if (old_match == new_match)
703 GNUNET_assert (old_match ==
704 GNUNET_CONTAINER_multihashmap_contains (client->connectmap,
705 &neighbour->hashPubKey));
706 return; /* no change */
708 if (old_match == GNUNET_NO)
711 GNUNET_assert (GNUNET_NO ==
712 GNUNET_CONTAINER_multihashmap_contains (client->connectmap,
713 &neighbour->hashPubKey));
714 GNUNET_assert (GNUNET_YES ==
715 GNUNET_CONTAINER_multihashmap_put (client->connectmap,
716 &neighbour->hashPubKey,
718 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
720 sizeof (struct ConnectNotifyMessage) +
721 (atsi_count) * sizeof (struct GNUNET_ATS_Information);
722 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
725 /* recovery strategy: throw away performance data */
727 size = sizeof (struct ConnectNotifyMessage);
729 cnm = (struct ConnectNotifyMessage *) buf;
730 cnm->header.size = htons (size);
731 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
732 cnm->ats_count = htonl (atsi_count);
733 a = (struct GNUNET_ATS_Information *) &cnm[1];
734 memcpy (a, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
737 cnm->peer = *neighbour;
738 send_to_client (client, &cnm->header, GNUNET_NO);
742 /* send disconnect */
743 GNUNET_assert (GNUNET_YES ==
744 GNUNET_CONTAINER_multihashmap_contains (client->connectmap,
745 &neighbour->hashPubKey));
746 GNUNET_assert (GNUNET_YES ==
747 GNUNET_CONTAINER_multihashmap_remove (client->connectmap,
748 &neighbour->hashPubKey,
750 dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
751 dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
752 dcm.reserved = htonl (0);
753 dcm.peer = *neighbour;
754 send_to_client (client, &dcm.header, GNUNET_NO);
760 * Notify all clients about a change to existing session.
761 * Called from SESSIONS whenever there is a change in sessions
762 * or types processed by the respective peer.
764 * @param neighbour identity of the neighbour that changed status
765 * @param atsi performance information about neighbour
766 * @param atsi_count number of entries in 'ats' array
767 * @param tmap_old previous type map for the neighbour, NULL for disconnect
768 * @param tmap_new updated type map for the neighbour, NULL for disconnect
771 GSC_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity
773 const struct GNUNET_ATS_Information
774 *atsi, unsigned int atsi_count,
775 const struct GSC_TypeMap *tmap_old,
776 const struct GSC_TypeMap *tmap_new)
778 struct GSC_Client *c;
780 for (c = client_head; c != NULL; c = c->next)
781 GSC_CLIENTS_notify_client_about_neighbour (c, neighbour, atsi, atsi_count,
787 * Deliver P2P message to interested clients. Caller must have checked
788 * that the sending peer actually lists the given message type as one
791 * @param sender peer who sent us the message
792 * @param atsi performance information about neighbour
793 * @param atsi_count number of entries in 'ats' array
794 * @param msg the message
795 * @param msize number of bytes to transmit
796 * @param options options for checking which clients should
797 * receive the message
800 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
801 const struct GNUNET_ATS_Information *atsi,
802 unsigned int atsi_count,
803 const struct GNUNET_MessageHeader *msg,
808 msize + sizeof (struct NotifyTrafficMessage) +
809 atsi_count * sizeof (struct GNUNET_ATS_Information);
810 char buf[size] GNUNET_ALIGN;
811 struct NotifyTrafficMessage *ntm;
812 struct GNUNET_ATS_Information *a;
816 GNUNET_snprintf (buf, sizeof (buf),
817 gettext_noop ("# bytes of messages of type %u received"),
818 (unsigned int) ntohs (msg->type));
819 GNUNET_STATISTICS_update (GSC_stats, buf, msize, GNUNET_NO);
821 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
824 /* recovery strategy: throw performance data away... */
826 size = msize + sizeof (struct NotifyTrafficMessage);
828 if (! ( (0 != (all_client_options & options)) ||
829 (0 != (options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) ))
830 return; /* no client cares about this message notification */
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Core service passes message from `%4s' of type %u to client.\n",
833 GNUNET_i2s (sender), (unsigned int) ntohs (msg->type));
834 GSC_SESSIONS_add_to_typemap (sender, ntohs (msg->type));
835 ntm = (struct NotifyTrafficMessage *) buf;
836 ntm->header.size = htons (size);
837 if (0 != (options & (GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_INBOUND)))
838 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
840 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
841 ntm->ats_count = htonl (atsi_count);
843 a = (struct GNUNET_ATS_Information*) &ntm[1];
844 memcpy (a, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
845 memcpy (&a[atsi_count], msg, msize);
846 send_to_all_clients (sender, &ntm->header, GNUNET_YES, options,
852 * Initialize clients subsystem.
854 * @param server handle to server clients connect to
857 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
859 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
860 {&handle_client_init, NULL,
861 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
862 {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
863 GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
864 sizeof (struct GNUNET_MessageHeader)},
865 {&GSC_SESSIONS_handle_client_have_peer, NULL,
866 GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
867 sizeof (struct GNUNET_MessageHeader) +
868 sizeof (struct GNUNET_PeerIdentity)},
869 {&handle_client_send_request, NULL,
870 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
871 sizeof (struct SendMessageRequest)},
872 {&handle_client_send, NULL,
873 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
877 /* setup notification */
878 client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
880 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
881 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
882 GNUNET_SERVER_add_handlers (server, handlers);
887 * Shutdown clients subsystem.
892 struct GSC_Client *c;
894 while (NULL != (c = client_head))
895 handle_client_disconnect (NULL, c->client_handle);
896 if (NULL != notifier)
898 GNUNET_SERVER_notification_context_destroy (notifier);
901 GNUNET_SERVER_mst_destroy (client_mst);
905 /* end of gnunet-service-core_clients.c */