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"
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).
41 #define MAX_NOTIFY_QUEUE 1024
45 * Data structure for each client connected to the core service.
50 * Clients are kept in a linked list.
52 struct GSC_Client *next;
55 * Clients are kept in a linked list.
57 struct GSC_Client *prev;
60 * Handle for the client with the server API.
62 struct GNUNET_SERVER_Client *client_handle;
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!
69 const uint16_t *types;
72 * Map of peer identities to active transmission requests of this
73 * client to the peer (of type 'struct GSC_ClientActiveRequest').
75 struct GNUNET_CONTAINER_MultiHashMap *requests;
78 * Options for messages this client cares about,
79 * see GNUNET_CORE_OPTION_ values.
84 * Number of types of incoming messages this client
85 * specifically cares about. Size of the "types" array.
93 * Head of linked list of our clients.
95 static struct GSC_Client *client_head;
98 * Tail of linked list of our clients.
100 static struct GSC_Client *client_tail;
103 * Context for notifications we need to send to our clients.
105 static struct GNUNET_SERVER_NotificationContext *notifier;
108 * Tokenizer for messages received from clients.
110 static struct GNUNET_SERVER_MessageStreamTokenizer *client_mst;
114 * Lookup our client struct given the server's client handle.
116 * @param client server client handle to look up
117 * @return our client handle for the client
119 static struct GSC_Client *
120 find_client (struct GNUNET_SERVER_Client *client)
122 struct GSC_Client *c;
125 while ((c != NULL) && (c->client_handle != client))
132 * Send a message to one of our clients.
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?
140 send_to_client (struct GSC_Client *client,
141 const struct GNUNET_MessageHeader *msg,
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));
150 GNUNET_SERVER_notification_context_unicast (notifier, client->client_handle,
156 * Send a message to one of our clients.
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?
164 GSC_CLIENTS_send_to_client (struct GNUNET_SERVER_Client *client,
165 const struct GNUNET_MessageHeader *msg,
168 struct GSC_Client *c;
170 c = find_client (client);
176 send_to_client (c, msg, can_drop);
181 * Test if the client is interested in messages of the given type.
183 * @param type message type
184 * @param c client to test
185 * @return GNUNET_YES if 'c' is interested, GNUNET_NO if not.
188 type_match (uint16_t type,
189 struct GSC_Client *c)
193 for (i=0;i<c->tcnt;i++)
194 if (type == c->types[i])
201 * Send a message to all of our current clients that have the right
204 * @param msg message to multicast
205 * @param can_drop can this message be discarded if the queue is too long
206 * @param options mask to use
207 * @param type type of the embedded message, 0 for none
210 send_to_all_clients (const struct GNUNET_MessageHeader *msg,
215 struct GSC_Client *c;
217 for (c = client_head; c != NULL; c = c->next)
219 if (! ( (0 != (c->options & options)) ||
220 ( (0 != (options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)) &&
221 (GNUNET_YES == type_match (type, c)) ) ) )
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Sending message of type %u to client.\n",
226 (unsigned int) ntohs (msg->type));
228 send_to_client (c, msg, can_drop);
234 * Handle CORE_INIT request.
237 * @param client new client that sent INIT
238 * @param message the 'struct InitMessage' (presumably)
241 handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
242 const struct GNUNET_MessageHeader *message)
244 const struct InitMessage *im;
245 struct InitReplyMessage irm;
246 struct GSC_Client *c;
248 const uint16_t *types;
252 /* check that we don't have an entry already */
253 c = find_client (client);
257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
260 msize = ntohs (message->size);
261 if (msize < sizeof (struct InitMessage))
264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
267 GNUNET_SERVER_notification_context_add (notifier, client);
268 im = (const struct InitMessage *) message;
269 types = (const uint16_t *) &im[1];
270 msize -= sizeof (struct InitMessage);
271 c = GNUNET_malloc (sizeof (struct GSC_Client) + msize);
272 c->client_handle = client;
273 c->tcnt = msize / sizeof (uint16_t);
274 c->options = ntohl (im->options);
275 c->types = (const uint16_t *) &c[1];
276 wtypes = (uint16_t *) & c[1];
277 for (i = 0; i < c->tcnt; i++)
278 wtypes[i] = ntohs (types[i]);
279 GSC_TYPEMAP_add (wtypes, c->tcnt);
280 GNUNET_CONTAINER_DLL_insert (client_head,
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Client connecting to core service is interested in %u message types\n",
286 (unsigned int) c->tcnt);
288 /* send init reply message */
289 irm.header.size = htons (sizeof (struct InitReplyMessage));
290 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
291 irm.reserved = htonl (0);
292 irm.my_identity = GSC_my_identity;
293 send_to_client (c, &irm.header, GNUNET_NO);
294 if (0 != (c->options & GNUNET_CORE_OPTION_SEND_CONNECT))
295 GSC_SESSIONS_notify_client_about_sessions (c);
296 GNUNET_SERVER_receive_done (client, GNUNET_OK);
301 * Handle CORE_SEND_REQUEST message.
304 * @param client new client that sent CORE_SEND_REQUEST
305 * @param message the 'struct InitMessage' (presumably)
308 handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
309 const struct GNUNET_MessageHeader *message)
311 const struct SendMessageRequest *req;
312 struct GSC_Client *c;
313 struct GSC_ClientActiveRequest *car;
315 req = (const struct SendMessageRequest *) message;
316 c = find_client (client);
319 /* client did not send INIT first! */
321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
324 if (c->requests == NULL)
325 c->requests = GNUNET_CONTAINER_multihashmap_create (16);
326 car = GNUNET_CONTAINER_multihashmap_get (c->requests, &req->peer.hashPubKey);
329 /* create new entry */
330 car = GNUNET_malloc (sizeof (struct GSC_ClientActiveRequest));
331 GNUNET_assert (GNUNET_OK ==
332 GNUNET_CONTAINER_multihashmap_put (c->requests,
333 &req->peer.hashPubKey,
335 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
336 car->client_handle = c;
338 car->target = req->peer;
339 car->deadline = GNUNET_TIME_absolute_ntoh (req->deadline);
340 car->priority = ntohl (req->priority);
341 car->msize = ntohs (req->size);
342 car->smr_id = req->smr_id;
343 car->was_solicited = GNUNET_NO;
345 memcmp (&req->peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
346 GSC_CLIENTS_solicit_request (car);
348 GSC_SESSIONS_queue_request (car);
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
354 * Closure for the 'client_tokenizer_callback'.
356 struct TokenizerContext
360 * Active request handle for the message.
362 struct GSC_ClientActiveRequest *car;
365 * Is corking allowed (set only once we have the real message).
373 * Handle CORE_SEND request.
376 * @param client the client issuing the request
377 * @param message the "struct SendMessage"
380 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
381 const struct GNUNET_MessageHeader *message)
383 const struct SendMessage *sm;
384 struct GSC_Client *c;
385 struct TokenizerContext tc;
388 msize = ntohs (message->size);
390 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
396 sm = (const struct SendMessage *) message;
397 msize -= sizeof (struct SendMessage);
398 GNUNET_break (0 == ntohl (sm->reserved));
399 c = find_client (client);
402 /* client did not send INIT first! */
404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
407 tc.car = GNUNET_CONTAINER_multihashmap_get (c->requests, &sm->peer.hashPubKey);
410 /* client did not request transmission first! */
412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
415 GNUNET_assert (GNUNET_YES ==
416 GNUNET_CONTAINER_multihashmap_remove (c->requests,
417 &sm->peer.hashPubKey,
419 tc.cork = ntohl (sm->cork);
420 GNUNET_SERVER_mst_receive (client_mst,
422 (const char*) &sm[1], msize,
426 memcmp (&tc.car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
427 GSC_SESSIONS_dequeue_request (tc.car);
428 GNUNET_free (tc.car);
429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
434 * Functions with this signature are called whenever a complete
435 * message is received by the tokenizer. Used by the 'client_mst' for
436 * dispatching messages from clients to either the SESSION subsystem
437 * or other CLIENT (for loopback).
440 * @param client reservation request ('struct GSC_ClientActiveRequest')
441 * @param message the actual message
444 client_tokenizer_callback (void *cls, void *client,
445 const struct GNUNET_MessageHeader *message)
447 struct TokenizerContext *tc = cls;
448 struct GSC_ClientActiveRequest *car = tc->car;
451 memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
453 GSC_CLIENTS_deliver_message (&GSC_my_identity,
456 ntohs (message->size),
457 GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
458 GSC_CLIENTS_deliver_message (&GSC_my_identity,
461 sizeof (struct GNUNET_MessageHeader),
462 GNUNET_CORE_OPTION_SEND_HDR_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
465 GSC_SESSIONS_transmit (car, message, tc->cork);
470 * Free client request records.
473 * @param key identity of peer for which this is an active request
474 * @param value the 'struct GSC_ClientActiveRequest' to free
475 * @return GNUNET_YES (continue iteration)
478 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
481 struct GSC_ClientActiveRequest *car = value;
483 GNUNET_assert (GNUNET_YES ==
484 GNUNET_CONTAINER_multihashmap_remove (car->client_handle->requests,
485 &car->target.hashPubKey,
487 GSC_SESSIONS_dequeue_request (car);
494 * A client disconnected, clean up.
497 * @param client identification of the client
500 handle_client_disconnect (void *cls,
501 struct GNUNET_SERVER_Client *client)
503 struct GSC_Client *c;
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Client %p has disconnected from core service.\n", client);
511 c = find_client (client);
513 return; /* client never sent INIT */
514 GNUNET_CONTAINER_DLL_remove (client_head,
517 if (c->requests != NULL)
519 GNUNET_CONTAINER_multihashmap_iterate (c->requests,
520 &destroy_active_client_request,
522 GNUNET_CONTAINER_multihashmap_destroy (c->requests);
524 GSC_TYPEMAP_remove (c->types, c->tcnt);
530 * Tell a client that we are ready to receive the message.
532 * @param car request that is now ready; the responsibility
533 * for the handle remains shared between CLIENTS
534 * and SESSIONS after this call.
537 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
539 struct GSC_Client *c;
540 struct SendMessageReady smr;
542 c = car->client_handle;
543 smr.header.size = htons (sizeof (struct SendMessageReady));
544 smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
545 smr.size = htons (car->msize);
546 smr.smr_id = car->smr_id;
547 smr.peer = car->target;
548 send_to_client (c, &smr.header, GNUNET_NO);
553 * Tell a client that we will never be ready to receive the
554 * given message in time (disconnect or timeout).
556 * @param car request that now permanently failed; the
557 * responsibility for the handle is now returned
558 * to CLIENTS (SESSIONS is done with it).
561 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
563 GNUNET_assert (GNUNET_YES ==
564 destroy_active_client_request (NULL, &car->target.hashPubKey, car));
569 * Notify a particular client about a change to existing connection to
570 * one of our neighbours (check if the client is interested). Called
571 * from 'GSC_SESSIONS_notify_client_about_sessions'.
573 * @param client client to notify
574 * @param neighbour identity of the neighbour that changed status
575 * @param atsi performance information about neighbour
576 * @param atsi_count number of entries in 'ats' array
577 * @param tmap_old previous type map for the neighbour, NULL for disconnect
578 * @param tmap_new updated type map for the neighbour, NULL for disconnect
581 GSC_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
582 const struct GNUNET_PeerIdentity *neighbour,
583 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
584 unsigned int atsi_count,
585 const struct GSC_TypeMap *tmap_old,
586 const struct GSC_TypeMap *tmap_new)
588 struct ConnectNotifyMessage *cnm;
590 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
591 struct GNUNET_TRANSPORT_ATS_Information *a;
592 struct DisconnectNotifyMessage dcm;
596 old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
597 new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
598 if (old_match == new_match)
599 return; /* no change */
600 if (old_match == GNUNET_NO)
604 sizeof (struct ConnectNotifyMessage) +
605 (atsi_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
606 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
609 /* recovery strategy: throw away performance data */
611 size = sizeof (struct ConnectNotifyMessage);
613 cnm = (struct ConnectNotifyMessage *) buf;
614 cnm->header.size = htons (size);
615 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
616 cnm->ats_count = htonl (atsi_count);
619 sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
620 a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
621 a[atsi_count].value = htonl (0);
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "Sending `%s' message to client.\n",
627 cnm->peer = *neighbour;
628 send_to_client (client, &cnm->header, GNUNET_NO);
632 /* send disconnect */
633 dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
634 dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
635 dcm.reserved = htonl (0);
636 dcm.peer = *neighbour;
637 send_to_client (client, &dcm.header, GNUNET_NO);
643 * Notify all clients about a change to existing session.
644 * Called from SESSIONS whenever there is a change in sessions
645 * or types processed by the respective peer.
647 * @param neighbour identity of the neighbour that changed status
648 * @param atsi performance information about neighbour
649 * @param atsi_count number of entries in 'ats' array
650 * @param tmap_old previous type map for the neighbour, NULL for disconnect
651 * @param tmap_new updated type map for the neighbour, NULL for disconnect
654 GSC_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
655 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
656 unsigned int atsi_count,
657 const struct GSC_TypeMap *tmap_old,
658 const struct GSC_TypeMap *tmap_new)
660 struct GSC_Client *c;
662 for (c = client_head; c != NULL; c = c->next)
663 GSC_CLIENTS_notify_client_about_neighbour (c, neighbour, atsi,
670 * Deliver P2P message to interested clients. Caller must have checked
671 * that the sending peer actually lists the given message type as one
674 * @param sender peer who sent us the message
675 * @param atsi performance information about neighbour
676 * @param atsi_count number of entries in 'ats' array
677 * @param msg the message
678 * @param msize number of bytes to transmit
679 * @param options options for checking which clients should
680 * receive the message
683 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
684 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
685 unsigned int atsi_count,
686 const struct GNUNET_MessageHeader *msg,
690 size_t size = msize + sizeof (struct NotifyTrafficMessage) +
691 atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
693 struct NotifyTrafficMessage *ntm;
694 struct GNUNET_TRANSPORT_ATS_Information *a;
698 GNUNET_snprintf (buf, sizeof (buf),
699 gettext_noop ("# bytes of messages of type %u received"),
700 (unsigned int) ntohs (msg->type));
701 GNUNET_STATISTICS_update (GSC_stats, buf, msize, GNUNET_NO);
703 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
706 /* recovery strategy: throw performance data away... */
708 size = msize + sizeof (struct NotifyTrafficMessage);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Core service passes message from `%4s' of type %u to client.\n",
714 (unsigned int) ntohs (msg->type));
716 GSC_SESSIONS_add_to_typemap (sender, ntohs (msg->type));
717 ntm = (struct NotifyTrafficMessage *) buf;
718 ntm->header.size = htons (size);
719 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
720 ntm->ats_count = htonl (atsi_count);
724 sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
725 a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
726 a[atsi_count].value = htonl (0);
727 memcpy (&a[atsi_count + 1], msg, msize);
728 send_to_all_clients (&ntm->header, GNUNET_YES,
729 options, ntohs (msg->type));
734 * Initialize clients subsystem.
736 * @param server handle to server clients connect to
739 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
741 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
742 {&handle_client_init, NULL,
743 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
744 {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
745 GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
746 sizeof (struct GNUNET_MessageHeader)},
747 {&GSC_SESSIONS_handle_client_have_peer, NULL,
748 GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
749 sizeof (struct GNUNET_MessageHeader) +
750 sizeof (struct GNUNET_PeerIdentity)},
751 {&handle_client_send_request, NULL,
752 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
753 sizeof (struct SendMessageRequest)},
754 {&handle_client_send, NULL,
755 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
759 /* setup notification */
760 client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
762 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
763 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
764 GNUNET_SERVER_add_handlers (server, handlers);
769 * Shutdown clients subsystem.
774 struct GSC_Client *c;
776 while (NULL != (c = client_head))
777 handle_client_disconnect (NULL, c->client_handle);
778 GNUNET_SERVER_notification_context_destroy (notifier);
780 GNUNET_SERVER_mst_destroy (client_mst);
784 /* end of gnunet-service-core_clients.c */