2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_neighbours.h"
33 #include "gnunet-service-core_sessions.h"
34 #include "gnunet-service-core_typemap.h"
39 * How many messages do we queue up at most for optional
40 * notifications to a client? (this can cause notifications
41 * about outgoing messages to be dropped).
43 #define MAX_NOTIFY_QUEUE 1024
47 * Data structure for each client connected to the CORE service.
52 * Clients are kept in a linked list.
54 struct GSC_Client *next;
57 * Clients are kept in a linked list.
59 struct GSC_Client *prev;
62 * Handle for the client with the server API.
64 struct GNUNET_SERVER_Client *client_handle;
67 * Array of the types of messages this peer cares
68 * about (with @e tcnt entries). Allocated as part
69 * of this client struct, do not free!
71 const uint16_t *types;
74 * Map of peer identities to active transmission requests of this
75 * client to the peer (of type `struct GSC_ClientActiveRequest`).
77 struct GNUNET_CONTAINER_MultiPeerMap *requests;
80 * Map containing all peers that this client knows we're connected to.
82 struct GNUNET_CONTAINER_MultiPeerMap *connectmap;
85 * Options for messages this client cares about,
86 * see GNUNET_CORE_OPTION_ values.
91 * Number of types of incoming messages this client
92 * specifically cares about. Size of the @e types array.
100 * Big "or" of all client options.
102 static uint32_t all_client_options;
105 * Head of linked list of our clients.
107 static struct GSC_Client *client_head;
110 * Tail of linked list of our clients.
112 static struct GSC_Client *client_tail;
115 * Context for notifications we need to send to our clients.
117 static struct GNUNET_SERVER_NotificationContext *notifier;
120 * Tokenizer for messages received from clients.
122 static struct GNUNET_SERVER_MessageStreamTokenizer *client_mst;
126 * Lookup our client struct given the server's client handle.
128 * @param client server client handle to look up
129 * @return our client handle for the client
131 static struct GSC_Client *
132 find_client (struct GNUNET_SERVER_Client *client)
134 struct GSC_Client *c;
137 while ((c != NULL) && (c->client_handle != client))
144 * Send a message to one of our clients.
146 * @param client target for the message
147 * @param msg message to transmit
148 * @param can_drop could this message be dropped if the
149 * client's queue is getting too large?
152 send_to_client (struct GSC_Client *client,
153 const struct GNUNET_MessageHeader *msg,
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Preparing to send %u bytes of message of type %u to client.\n",
158 (unsigned int) ntohs (msg->size),
159 (unsigned int) ntohs (msg->type));
160 GNUNET_SERVER_notification_context_unicast (notifier, client->client_handle,
166 * Send a message to one of our clients.
168 * @param client target for the message
169 * @param msg message to transmit
170 * @param can_drop could this message be dropped if the
171 * client's queue is getting too large?
174 GSC_CLIENTS_send_to_client (struct GNUNET_SERVER_Client *client,
175 const struct GNUNET_MessageHeader *msg,
178 struct GSC_Client *c;
180 c = find_client (client);
186 send_to_client (c, msg, can_drop);
191 * Test if the client is interested in messages of the given type.
193 * @param type message type
194 * @param c client to test
195 * @return #GNUNET_YES if @a c is interested, #GNUNET_NO if not.
198 type_match (uint16_t type, struct GSC_Client *c)
202 if (c->tcnt == 0 && c->options != 0)
203 return GNUNET_YES; /* peer without handlers and inbound/outbond
204 callbacks matches ALL */
205 for (i = 0; i < c->tcnt; i++)
206 if (type == c->types[i])
213 * Send a message to all of our current clients that have the right
216 * @param partner origin (or destination) of the message (used to check that this peer is
217 * known to be connected to the respective client)
218 * @param msg message to multicast
219 * @param can_drop can this message be discarded if the queue is too long
220 * @param options mask to use
221 * @param type type of the embedded message, 0 for none
224 send_to_all_clients (const struct GNUNET_PeerIdentity *partner,
225 const struct GNUNET_MessageHeader *msg,
230 struct GSC_Client *c;
233 for (c = client_head; NULL != c; c = c->next)
235 tm = type_match (type, c);
236 if (! ( (0 != (c->options & options)) ||
237 ( (0 != (options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) &&
238 (GNUNET_YES == tm) ) ) )
239 continue; /* neither options nor type match permit the message */
240 if ( (0 != (options & GNUNET_CORE_OPTION_SEND_HDR_INBOUND)) &&
241 ( (0 != (c->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) ||
242 (GNUNET_YES == tm) ) )
244 if ( (0 != (options & GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND)) &&
245 (0 != (c->options & GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND)) )
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Sending %u message with %u bytes to client interested in messages of type %u.\n",
251 (unsigned int) type);
252 GNUNET_assert ( (0 == (c->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) ||
253 (GNUNET_YES != tm) ||
255 GNUNET_CONTAINER_multipeermap_contains (c->connectmap,
257 send_to_client (c, msg, can_drop);
263 * Handle #GNUNET_MESSAGE_TYPE_CORE_INIT request.
266 * @param client new client that sent #GNUNET_MESSAGE_TYPE_CORE_INIT
267 * @param message the `struct InitMessage` (presumably)
270 handle_client_init (void *cls,
271 struct GNUNET_SERVER_Client *client,
272 const struct GNUNET_MessageHeader *message)
274 const struct InitMessage *im;
275 struct InitReplyMessage irm;
276 struct GSC_Client *c;
278 const uint16_t *types;
282 /* check that we don't have an entry already */
283 c = find_client (client);
287 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
290 msize = ntohs (message->size);
291 if (msize < sizeof (struct InitMessage))
294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
297 GNUNET_SERVER_notification_context_add (notifier, client);
298 im = (const struct InitMessage *) message;
299 types = (const uint16_t *) &im[1];
300 msize -= sizeof (struct InitMessage);
301 c = GNUNET_malloc (sizeof (struct GSC_Client) + msize);
302 c->client_handle = client;
303 c->tcnt = msize / sizeof (uint16_t);
304 c->options = ntohl (im->options);
305 all_client_options |= c->options;
306 c->types = (const uint16_t *) &c[1];
307 c->connectmap = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_NO);
308 GNUNET_assert (GNUNET_YES ==
309 GNUNET_CONTAINER_multipeermap_put (c->connectmap,
312 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
313 wtypes = (uint16_t *) & c[1];
314 for (i = 0; i < c->tcnt; i++)
315 wtypes[i] = ntohs (types[i]);
316 GSC_TYPEMAP_add (wtypes, c->tcnt);
317 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, c);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Client connecting to core service is interested in %u message types\n",
320 (unsigned int) c->tcnt);
321 /* send init reply message */
322 irm.header.size = htons (sizeof (struct InitReplyMessage));
323 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
324 irm.reserved = htonl (0);
325 irm.my_identity = GSC_my_identity;
326 send_to_client (c, &irm.header, GNUNET_NO);
327 GSC_SESSIONS_notify_client_about_sessions (c);
328 GNUNET_SERVER_receive_done (client, GNUNET_OK);
333 * Handle #GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST message.
336 * @param client new client that sent a #GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST
337 * @param message the `struct SendMessageRequest` (presumably)
340 handle_client_send_request (void *cls,
341 struct GNUNET_SERVER_Client *client,
342 const struct GNUNET_MessageHeader *message)
344 const struct SendMessageRequest *req;
345 struct GSC_Client *c;
346 struct GSC_ClientActiveRequest *car;
349 req = (const struct SendMessageRequest *) message;
350 c = find_client (client);
353 /* client did not send INIT first! */
355 GNUNET_SERVER_receive_done (client,
359 if (NULL == c->requests)
360 c->requests = GNUNET_CONTAINER_multipeermap_create (16,
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Client asked for transmission to `%s'\n",
364 GNUNET_i2s (&req->peer));
369 sizeof (struct GNUNET_PeerIdentity)));
370 if ((! is_loopback) &&
372 GNUNET_CONTAINER_multipeermap_contains (c->connectmap,
375 /* neighbour must have disconnected since request was issued,
376 * ignore (client will realize it once it processes the
377 * disconnect notification) */
378 GNUNET_STATISTICS_update (GSC_stats,
380 ("# send requests dropped (disconnected)"), 1,
382 GNUNET_SERVER_receive_done (client,
387 car = GNUNET_CONTAINER_multipeermap_get (c->requests,
391 /* create new entry */
392 car = GNUNET_new (struct GSC_ClientActiveRequest);
393 GNUNET_assert (GNUNET_OK ==
394 GNUNET_CONTAINER_multipeermap_put (c->requests,
397 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
398 car->client_handle = c;
402 /* dequeue and recycle memory from pending request, there can only
403 be at most one per client and peer */
404 GSC_SESSIONS_dequeue_request (car);
406 car->target = req->peer;
407 car->received_time = GNUNET_TIME_absolute_get ();
408 car->deadline = GNUNET_TIME_absolute_ntoh (req->deadline);
409 car->priority = (enum GNUNET_CORE_Priority) ntohl (req->priority);
410 car->msize = ntohs (req->size);
411 car->smr_id = req->smr_id;
412 car->was_solicited = GNUNET_NO;
415 /* loopback, satisfy immediately */
416 GSC_CLIENTS_solicit_request (car);
417 GNUNET_SERVER_receive_done (client, GNUNET_OK);
420 GSC_SESSIONS_queue_request (car);
421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
426 * Closure for the #client_tokenizer_callback().
428 struct TokenizerContext
432 * Active request handle for the message.
434 struct GSC_ClientActiveRequest *car;
437 * How important is this message.
439 enum GNUNET_CORE_Priority priority;
442 * Is corking allowed (set only once we have the real message).
450 * Handle #GNUNET_MESSAGE_TYPE_CORE_SEND request.
453 * @param client the client issuing the request
454 * @param message the `struct SendMessage`
457 handle_client_send (void *cls,
458 struct GNUNET_SERVER_Client *client,
459 const struct GNUNET_MessageHeader *message)
461 const struct SendMessage *sm;
462 struct GSC_Client *c;
463 struct TokenizerContext tc;
465 struct GNUNET_TIME_Relative delay;
466 struct GNUNET_TIME_Relative overdue;
468 msize = ntohs (message->size);
469 if (msize < sizeof (struct SendMessage))
472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
475 sm = (const struct SendMessage *) message;
476 msize -= sizeof (struct SendMessage);
477 GNUNET_break (0 == ntohl (sm->reserved));
478 c = find_client (client);
481 /* client did not send INIT first! */
483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
487 = GNUNET_CONTAINER_multipeermap_get (c->requests,
491 /* Must have been that we first approved the request, then got disconnected
492 * (which triggered removal of the 'car') and now the client gives us a message
493 * just *before* the client learns about the disconnect. Theoretically, we
494 * might also now be *again* connected. So this can happen (but should be
495 * rare). If it does happen, the message is discarded. */
496 GNUNET_STATISTICS_update (GSC_stats,
498 ("# messages discarded (session disconnected)"),
500 GNUNET_SERVER_receive_done (client,
504 delay = GNUNET_TIME_absolute_get_duration (tc.car->received_time);
505 overdue = GNUNET_TIME_absolute_get_duration (tc.car->deadline);
506 tc.cork = ntohl (sm->cork);
507 tc.priority = (enum GNUNET_CORE_Priority) ntohl (sm->priority);
508 if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
510 "Client waited %s for transmission of %u bytes to `%s'%s\n",
511 GNUNET_STRINGS_relative_time_to_string (delay,
514 GNUNET_i2s (&sm->peer),
515 tc.cork ? "" : " (corked)");
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Client waited %s for transmission of %u bytes to `%s'%s\n",
519 GNUNET_STRINGS_relative_time_to_string (delay,
522 GNUNET_i2s (&sm->peer),
523 tc.cork ? "" : " (corked)");
525 GNUNET_assert (GNUNET_YES ==
526 GNUNET_CONTAINER_multipeermap_remove (c->requests,
529 GNUNET_SERVER_mst_receive (client_mst, &tc,
530 (const char *) &sm[1],
534 GSC_SESSIONS_dequeue_request (tc.car);
535 GNUNET_free (tc.car);
536 GNUNET_SERVER_receive_done (client,
542 * Functions with this signature are called whenever a complete
543 * message is received by the tokenizer. Used by the 'client_mst' for
544 * dispatching messages from clients to either the SESSION subsystem
545 * or other CLIENT (for loopback).
548 * @param client reservation request (`struct GSC_ClientActiveRequest`)
549 * @param message the actual message
552 client_tokenizer_callback (void *cls,
554 const struct GNUNET_MessageHeader *message)
556 struct TokenizerContext *tc = client;
557 struct GSC_ClientActiveRequest *car = tc->car;
560 GNUNET_snprintf (buf, sizeof (buf),
561 gettext_noop ("# bytes of messages of type %u received"),
562 (unsigned int) ntohs (message->type));
563 GNUNET_STATISTICS_update (GSC_stats,
565 ntohs (message->size),
568 memcmp (&car->target,
570 sizeof (struct GNUNET_PeerIdentity)))
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Delivering message of type %u to myself\n",
574 ntohs (message->type));
575 GSC_CLIENTS_deliver_message (&GSC_my_identity, message,
576 ntohs (message->size),
577 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
578 GSC_CLIENTS_deliver_message (&GSC_my_identity, message,
579 sizeof (struct GNUNET_MessageHeader),
580 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
581 GSC_CLIENTS_deliver_message (&GSC_my_identity, message,
582 ntohs (message->size),
583 GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
584 GSC_CLIENTS_deliver_message (&GSC_my_identity, message,
585 sizeof (struct GNUNET_MessageHeader),
586 GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Delivering message of type %u and size %u to %s\n",
592 ntohs (message->type), ntohs (message->size),
593 GNUNET_i2s (&car->target));
594 GSC_CLIENTS_deliver_message (&car->target, message,
595 ntohs (message->size),
596 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
597 GSC_CLIENTS_deliver_message (&car->target, message,
598 sizeof (struct GNUNET_MessageHeader),
599 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
600 GSC_SESSIONS_transmit (car,
610 * Free client request records.
613 * @param key identity of peer for which this is an active request
614 * @param value the `struct GSC_ClientActiveRequest` to free
615 * @return #GNUNET_YES (continue iteration)
618 destroy_active_client_request (void *cls,
619 const struct GNUNET_PeerIdentity *key,
622 struct GSC_ClientActiveRequest *car = value;
624 GNUNET_assert (GNUNET_YES ==
625 GNUNET_CONTAINER_multipeermap_remove (car->
626 client_handle->requests,
629 GSC_SESSIONS_dequeue_request (car);
636 * A client disconnected, clean up.
639 * @param client identification of the client
642 handle_client_disconnect (void *cls,
643 struct GNUNET_SERVER_Client *client)
645 struct GSC_Client *c;
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Client %p has disconnected from core service.\n",
652 c = find_client (client);
654 return; /* client never sent INIT */
655 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, c);
656 if (c->requests != NULL)
658 GNUNET_CONTAINER_multipeermap_iterate (c->requests,
659 &destroy_active_client_request,
661 GNUNET_CONTAINER_multipeermap_destroy (c->requests);
663 GNUNET_CONTAINER_multipeermap_destroy (c->connectmap);
664 c->connectmap = NULL;
665 GSC_TYPEMAP_remove (c->types, c->tcnt);
668 /* recalculate 'all_client_options' */
669 all_client_options = 0;
670 for (c = client_head; NULL != c ; c = c->next)
671 all_client_options |= c->options;
676 * Tell a client that we are ready to receive the message.
678 * @param car request that is now ready; the responsibility
679 * for the handle remains shared between CLIENTS
680 * and SESSIONS after this call.
683 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
685 struct GSC_Client *c;
686 struct SendMessageReady smr;
687 struct GNUNET_TIME_Relative delay;
688 struct GNUNET_TIME_Relative left;
690 c = car->client_handle;
692 GNUNET_CONTAINER_multipeermap_contains (c->connectmap,
695 /* connection has gone down since, drop request */
697 memcmp (&car->target,
699 sizeof (struct GNUNET_PeerIdentity)));
700 GSC_SESSIONS_dequeue_request (car);
701 GSC_CLIENTS_reject_request (car,
705 delay = GNUNET_TIME_absolute_get_duration (car->received_time);
706 left = GNUNET_TIME_absolute_get_duration (car->deadline);
707 if (left.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
708 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
709 "Client waited %s for permission to transmit to `%s'%s (priority %u)\n",
710 GNUNET_STRINGS_relative_time_to_string (delay,
712 GNUNET_i2s (&car->target),
713 (0 == left.rel_value_us)
717 smr.header.size = htons (sizeof (struct SendMessageReady));
718 smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
719 smr.size = htons (car->msize);
720 smr.smr_id = car->smr_id;
721 smr.peer = car->target;
722 send_to_client (c, &smr.header, GNUNET_NO);
727 * We will never be ready to transmit the given message in (disconnect
728 * or invalid request). Frees resources associated with @a car. We
729 * don't explicitly tell the client, he'll learn with the disconnect
730 * (or violated the protocol).
732 * @param car request that now permanently failed; the
733 * responsibility for the handle is now returned
734 * to CLIENTS (SESSIONS is done with it).
735 * @param drop_client #GNUNET_YES if the client violated the protocol
736 * and we should thus drop the connection
739 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car,
742 GNUNET_assert (GNUNET_YES ==
743 GNUNET_CONTAINER_multipeermap_remove (car->
744 client_handle->requests,
747 if (GNUNET_YES == drop_client)
748 GNUNET_SERVER_client_disconnect (car->client_handle->client_handle);
754 * Notify a particular client about a change to existing connection to
755 * one of our neighbours (check if the client is interested). Called
756 * from 'GSC_SESSIONS_notify_client_about_sessions'.
758 * @param client client to notify
759 * @param neighbour identity of the neighbour that changed status
760 * @param tmap_old previous type map for the neighbour, NULL for connect
761 * @param tmap_new updated type map for the neighbour, NULL for disconnect
764 GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
765 const struct GNUNET_PeerIdentity *neighbour,
766 const struct GSC_TypeMap *tmap_old,
767 const struct GSC_TypeMap *tmap_new)
769 struct ConnectNotifyMessage *cnm;
771 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
772 struct DisconnectNotifyMessage dcm;
776 old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
777 new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
778 if (old_match == new_match)
780 GNUNET_assert (old_match ==
781 GNUNET_CONTAINER_multipeermap_contains (client->connectmap,
783 return; /* no change */
785 if (old_match == GNUNET_NO)
788 GNUNET_assert (GNUNET_NO ==
789 GNUNET_CONTAINER_multipeermap_contains (client->connectmap,
791 GNUNET_assert (GNUNET_YES ==
792 GNUNET_CONTAINER_multipeermap_put (client->connectmap,
795 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
796 size = sizeof (struct ConnectNotifyMessage);
797 cnm = (struct ConnectNotifyMessage *) buf;
798 cnm->header.size = htons (size);
799 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
800 cnm->reserved = htonl (0);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Sending `%s' message to client.\n",
804 cnm->peer = *neighbour;
805 send_to_client (client, &cnm->header, GNUNET_NO);
809 /* send disconnect */
810 GNUNET_assert (GNUNET_YES ==
811 GNUNET_CONTAINER_multipeermap_contains (client->connectmap,
813 GNUNET_assert (GNUNET_YES ==
814 GNUNET_CONTAINER_multipeermap_remove (client->connectmap,
817 dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
818 dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
819 dcm.reserved = htonl (0);
820 dcm.peer = *neighbour;
821 send_to_client (client, &dcm.header, GNUNET_NO);
827 * Notify all clients about a change to existing session.
828 * Called from SESSIONS whenever there is a change in sessions
829 * or types processed by the respective peer.
831 * @param neighbour identity of the neighbour that changed status
832 * @param tmap_old previous type map for the neighbour, NULL for connect
833 * @param tmap_new updated type map for the neighbour, NULL for disconnect
836 GSC_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
837 const struct GSC_TypeMap *tmap_old,
838 const struct GSC_TypeMap *tmap_new)
840 struct GSC_Client *c;
842 for (c = client_head; NULL != c; c = c->next)
843 GSC_CLIENTS_notify_client_about_neighbour (c, neighbour,
849 * Deliver P2P message to interested clients. Caller must have checked
850 * that the sending peer actually lists the given message type as one
853 * @param sender peer who sent us the message
854 * @param msg the message
855 * @param msize number of bytes to transmit
856 * @param options options for checking which clients should
857 * receive the message
860 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
861 const struct GNUNET_MessageHeader *msg,
865 size_t size = msize + sizeof (struct NotifyTrafficMessage);
866 char buf[size] GNUNET_ALIGN;
867 struct NotifyTrafficMessage *ntm;
869 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
872 /* recovery strategy: throw performance data away... */
873 size = msize + sizeof (struct NotifyTrafficMessage);
875 if (! ( (0 != (all_client_options & options)) ||
876 (0 != (options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) ))
877 return; /* no client cares about this message notification */
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Core service passes message from `%s' of type %u to client.\n",
881 (unsigned int) ntohs (msg->type));
882 GSC_SESSIONS_add_to_typemap (sender, ntohs (msg->type));
883 ntm = (struct NotifyTrafficMessage *) buf;
884 ntm->header.size = htons (size);
885 if (0 != (options & (GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_INBOUND)))
886 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
888 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
893 send_to_all_clients (sender,
902 * Initialize clients subsystem.
904 * @param server handle to server clients connect to
907 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
909 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
910 {&handle_client_init, NULL,
911 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
912 {&GSC_KX_handle_client_monitor_peers, NULL,
913 GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS,
914 sizeof (struct GNUNET_MessageHeader)},
915 {&handle_client_send_request, NULL,
916 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
917 sizeof (struct SendMessageRequest)},
918 {&handle_client_send, NULL,
919 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
923 /* setup notification */
924 client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
926 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
927 GNUNET_SERVER_disconnect_notify (server,
928 &handle_client_disconnect, NULL);
929 GNUNET_SERVER_add_handlers (server, handlers);
934 * Shutdown clients subsystem.
939 struct GSC_Client *c;
941 while (NULL != (c = client_head))
942 handle_client_disconnect (NULL, c->client_handle);
943 if (NULL != notifier)
945 GNUNET_SERVER_notification_context_destroy (notifier);
948 if (NULL != client_mst)
950 GNUNET_SERVER_mst_destroy (client_mst);
955 /* end of gnunet-service-core_clients.c */