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,
144 #if DEBUG_CORE_CLIENT
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)) ) ) )
223 #if DEBUG_CORE_CLIENT > 1
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,
283 #if DEBUG_CORE_CLIENT
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 * Handle CORE_SEND request.
357 * @param client the client issuing the request
358 * @param message the "struct SendMessage"
361 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
362 const struct GNUNET_MessageHeader *message)
364 const struct SendMessage *sm;
365 struct GSC_Client *c;
366 struct GSC_ClientActiveRequest *car;
369 msize = ntohs (message->size);
371 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
377 sm = (const struct SendMessage *) message;
378 msize -= sizeof (struct SendMessage);
379 GNUNET_break (0 == ntohl (sm->reserved));
380 c = find_client (client);
383 /* client did not send INIT first! */
385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
388 car = GNUNET_CONTAINER_multihashmap_get (c->requests, &sm->peer.hashPubKey);
391 /* client did not request transmission first! */
393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
396 GNUNET_assert (GNUNET_YES ==
397 GNUNET_CONTAINER_multihashmap_remove (c->requests,
398 &sm->peer.hashPubKey,
400 car->cork = ntohs (sm->cork);
401 GNUNET_SERVER_mst_receive (client_mst,
403 (const char*) &sm[1], msize,
407 memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
408 GSC_SESSIONS_dequeue_request (car);
410 GNUNET_SERVER_receive_done (client, GNUNET_OK);
415 * Functions with this signature are called whenever a complete
416 * message is received by the tokenizer. Used by the 'client_mst' for
417 * dispatching messages from clients to either the SESSION subsystem
418 * or other CLIENT (for loopback).
421 * @param client reservation request ('struct GSC_ClientActiveRequest')
422 * @param message the actual message
425 client_tokenizer_callback (void *cls, void *client,
426 const struct GNUNET_MessageHeader *message)
428 struct GSC_ClientActiveRequest *car = client;
431 memcmp (&car->target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
433 GSC_CLIENTS_deliver_message (&GSC_my_identity,
436 ntohs (message->size),
437 GNUNET_CORE_OPTION_SEND_FULL_INBOUND | GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
438 GSC_CLIENTS_deliver_message (&GSC_my_identity,
441 sizeof (struct GNUNET_MessageHeader),
442 GNUNET_CORE_OPTION_SEND_HDR_INBOUND | GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
445 GSC_SESSIONS_transmit (car, message, car->cork);
450 * Free client request records.
453 * @param key identity of peer for which this is an active request
454 * @param value the 'struct GSC_ClientActiveRequest' to free
455 * @return GNUNET_YES (continue iteration)
458 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
461 struct GSC_ClientActiveRequest *car = value;
463 GNUNET_assert (GNUNET_YES ==
464 GNUNET_CONTAINER_multihashmap_remove (car->client_handle->requests,
465 &car->target.hashPubKey,
467 GSC_SESSIONS_dequeue_request (car);
474 * A client disconnected, clean up.
477 * @param client identification of the client
480 handle_client_disconnect (void *cls,
481 struct GNUNET_SERVER_Client *client)
483 struct GSC_Client *c;
487 #if DEBUG_CORE_CLIENT
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Client %p has disconnected from core service.\n", client);
491 c = find_client (client);
493 return; /* client never sent INIT */
494 GNUNET_CONTAINER_DLL_remove (client_head,
497 if (c->requests != NULL)
499 GNUNET_CONTAINER_multihashmap_iterate (c->requests,
500 &destroy_active_client_request,
502 GNUNET_CONTAINER_multihashmap_destroy (c->requests);
504 GSC_TYPEMAP_remove (c->types, c->tcnt);
510 * Tell a client that we are ready to receive the message.
512 * @param car request that is now ready; the responsibility
513 * for the handle remains shared between CLIENTS
514 * and SESSIONS after this call.
517 GSC_CLIENTS_solicit_request (struct GSC_ClientActiveRequest *car)
519 struct GSC_Client *c;
520 struct SendMessageReady smr;
522 c = car->client_handle;
523 smr.header.size = htons (sizeof (struct SendMessageReady));
524 smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
525 smr.size = htons (car->msize);
526 smr.smr_id = car->smr_id;
527 smr.peer = car->target;
528 send_to_client (c, &smr.header, GNUNET_NO);
533 * Tell a client that we will never be ready to receive the
534 * given message in time (disconnect or timeout).
536 * @param car request that now permanently failed; the
537 * responsibility for the handle is now returned
538 * to CLIENTS (SESSIONS is done with it).
541 GSC_CLIENTS_reject_request (struct GSC_ClientActiveRequest *car)
543 GNUNET_assert (GNUNET_YES ==
544 destroy_active_client_request (NULL, &car->target.hashPubKey, car));
549 * Notify a particular client about a change to existing connection to
550 * one of our neighbours (check if the client is interested). Called
551 * from 'GSC_SESSIONS_notify_client_about_sessions'.
553 * @param client client to notify
554 * @param neighbour identity of the neighbour that changed status
555 * @param atsi performance information about neighbour
556 * @param atsi_count number of entries in 'ats' array
557 * @param tmap_old previous type map for the neighbour, NULL for disconnect
558 * @param tmap_new updated type map for the neighbour, NULL for disconnect
561 GDS_CLIENTS_notify_client_about_neighbour (struct GSC_Client *client,
562 const struct GNUNET_PeerIdentity *neighbour,
563 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
564 unsigned int atsi_count,
565 const struct GSC_TypeMap *tmap_old,
566 const struct GSC_TypeMap *tmap_new)
568 struct ConnectNotifyMessage *cnm;
570 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
571 struct GNUNET_TRANSPORT_ATS_Information *a;
572 struct DisconnectNotifyMessage dcm;
576 old_match = GSC_TYPEMAP_test_match (tmap_old, client->types, client->tcnt);
577 new_match = GSC_TYPEMAP_test_match (tmap_new, client->types, client->tcnt);
578 if (old_match == new_match)
579 return; /* no change */
580 if (old_match == GNUNET_NO)
584 sizeof (struct ConnectNotifyMessage) +
585 (atsi_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
586 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
589 /* recovery strategy: throw away performance data */
591 size = sizeof (struct ConnectNotifyMessage);
593 cnm = (struct ConnectNotifyMessage *) buf;
594 cnm->header.size = htons (size);
595 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
596 cnm->ats_count = htonl (atsi_count);
599 sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
600 a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
601 a[atsi_count].value = htonl (0);
602 #if DEBUG_CORE_CLIENT
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Sending `%s' message to client.\n",
607 cnm->peer = *neighbour;
608 send_to_client (client, &cnm->header, GNUNET_NO);
612 /* send disconnect */
613 dcm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
614 dcm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
615 dcm.reserved = htonl (0);
616 dcm.peer = *neighbour;
617 send_to_client (client, &dcm.header, GNUNET_NO);
623 * Notify all clients about a change to existing session.
624 * Called from SESSIONS whenever there is a change in sessions
625 * or types processed by the respective peer.
627 * @param neighbour identity of the neighbour that changed status
628 * @param atsi performance information about neighbour
629 * @param atsi_count number of entries in 'ats' array
630 * @param tmap_old previous type map for the neighbour, NULL for disconnect
631 * @param tmap_new updated type map for the neighbour, NULL for disconnect
634 GDS_CLIENTS_notify_clients_about_neighbour (const struct GNUNET_PeerIdentity *neighbour,
635 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
636 unsigned int atsi_count,
637 const struct GSC_TypeMap *tmap_old,
638 const struct GSC_TypeMap *tmap_new)
640 struct GSC_Client *c;
642 for (c = client_head; c != NULL; c = c->next)
643 GDS_CLIENTS_notify_client_about_neighbour (c, neighbour, atsi,
650 * Deliver P2P message to interested clients. Caller must have checked
651 * that the sending peer actually lists the given message type as one
654 * @param sender peer who sent us the message
655 * @param atsi performance information about neighbour
656 * @param atsi_count number of entries in 'ats' array
657 * @param msg the message
658 * @param msize number of bytes to transmit
659 * @param options options for checking which clients should
660 * receive the message
663 GSC_CLIENTS_deliver_message (const struct GNUNET_PeerIdentity *sender,
664 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
665 unsigned int atsi_count,
666 const struct GNUNET_MessageHeader *msg,
670 size_t size = msize + sizeof (struct NotifyTrafficMessage) +
671 atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
673 struct NotifyTrafficMessage *ntm;
674 struct GNUNET_TRANSPORT_ATS_Information *a;
678 GNUNET_snprintf (buf, sizeof (buf),
679 gettext_noop ("# bytes of messages of type %u received"),
680 (unsigned int) ntohs (msg->type));
681 GNUNET_STATISTICS_update (GSC_stats, buf, msize, GNUNET_NO);
683 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
686 /* recovery strategy: throw performance data away... */
688 size = msize + sizeof (struct NotifyTrafficMessage);
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Core service passes message from `%4s' of type %u to client.\n",
694 (unsigned int) ntohs (msg->type));
696 ntm = (struct NotifyTrafficMessage *) buf;
697 ntm->header.size = htons (size);
698 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
699 ntm->ats_count = htonl (atsi_count);
703 sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
704 a[atsi_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
705 a[atsi_count].value = htonl (0);
706 memcpy (&a[atsi_count + 1], msg, msize);
707 send_to_all_clients (&ntm->header, GNUNET_YES,
708 options, ntohs (msg->type));
713 * Initialize clients subsystem.
715 * @param server handle to server clients connect to
718 GSC_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
720 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
721 {&handle_client_init, NULL,
722 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
723 {&GSC_SESSIONS_handle_client_iterate_peers, NULL,
724 GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
725 sizeof (struct GNUNET_MessageHeader)},
726 {&GSC_SESSIONS_handle_client_have_peer, NULL,
727 GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
728 sizeof (struct GNUNET_MessageHeader) +
729 sizeof (struct GNUNET_PeerIdentity)},
730 {&handle_client_send_request, NULL,
731 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
732 sizeof (struct SendMessageRequest)},
733 {&handle_client_send, NULL,
734 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
738 /* setup notification */
739 client_mst = GNUNET_SERVER_mst_create (&client_tokenizer_callback, NULL);
741 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
742 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
743 GNUNET_SERVER_add_handlers (server, handlers);
748 * Shutdown clients subsystem.
753 struct GSC_Client *c;
755 while (NULL != (c = client_head))
756 handle_client_disconnect (NULL, c->client_handle);
757 GNUNET_SERVER_notification_context_destroy (notifier);
759 GNUNET_SERVER_mst_destroy (client_mst);
763 /* end of gnunet-service-core_clients.c */