2 This file is part of GNUnet.
3 Copyright (C) 2016 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 util/service_new.c
23 * @brief functions related to starting services (redesign)
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_resolver_service.h"
34 * Information the service tracks per listen operation.
36 struct ServiceListenContext
42 struct ServiceListenContext *next;
47 struct ServiceListenContext *prev;
50 * Service this listen context belongs to.
52 struct GNUNET_SERVICE_Handle *sh;
55 * Socket we are listening on.
57 struct GNUNET_NETWORK_Handle *listen_socket;
60 * Task scheduled to do the listening.
62 struct GNUNET_SCHEDULER_Task *listen_task;
68 * Handle to a service.
70 struct GNUNET_SERVICE_Handle
75 const struct GNUNET_CONFIGURATION_Handle *cfg;
78 * Name of our service.
80 const char *service_name;
83 * Main service-specific task to run.
85 GNUNET_SERVICE_InitCallback service_init_cb;
88 * Function to call when clients connect.
90 GNUNET_SERVICE_ConnectHandler connect_cb;
93 * Function to call when clients disconnect / are disconnected.
95 GNUNET_SERVICE_DisconnectHandler disconnect_cb;
98 * Closure for @e service_init_cb, @e connect_cb, @e disconnect_cb.
103 * DLL of listen sockets used to accept new connections.
105 struct ServiceListenContext *slc_head;
108 * DLL of listen sockets used to accept new connections.
110 struct ServiceListenContext *slc_tail;
113 * Our clients, kept in a DLL.
115 struct GNUNET_SERVICE_Client *clients_head;
118 * Our clients, kept in a DLL.
120 struct GNUNET_SERVICE_Client *clients_tail;
123 * Message handlers to use for all clients.
125 const struct GNUNET_MQ_MessageHandler *handlers;
128 * Closure for @e task.
133 * IPv4 addresses that are not allowed to connect.
135 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
138 * IPv6 addresses that are not allowed to connect.
140 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
143 * IPv4 addresses that are allowed to connect (if not
144 * set, all are allowed).
146 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
149 * IPv6 addresses that are allowed to connect (if not
150 * set, all are allowed).
152 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
155 * Do we require a matching UID for UNIX domain socket connections?
156 * #GNUNET_NO means that the UID does not have to match (however,
157 * @e match_gid may still impose other access control checks).
162 * Do we require a matching GID for UNIX domain socket connections?
163 * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
164 * checking that the client's UID is in our group OR that the
165 * client's GID is our GID. If both "match_gid" and @e match_uid are
166 * #GNUNET_NO, all users on the local system have access.
171 * Set to #GNUNET_YES if we got a shutdown signal and terminate
172 * the service if #have_non_monitor_clients() returns #GNUNET_YES.
179 enum GNUNET_SERVICE_Options options;
185 * Handle to a client that is connected to a service.
187 struct GNUNET_SERVICE_Client
193 struct GNUNET_SERVICE_Client *next;
198 struct GNUNET_SERVICE_Client *prev;
201 * Server that this client belongs to.
203 struct GNUNET_SERVER_Handle *sh;
206 * Socket of this client.
208 struct GNUNET_NETWORK_Handle *sock;
211 * Message queue for the client.
213 struct GNUNET_MQ_Handle *mq;
216 * Tokenizer we use for processing incoming data.
218 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
221 * Task that warns about missing calls to
222 * #GNUNET_SERVICE_client_continue().
224 struct GNUNET_SCHEDULER_Task *warn_task;
227 * Task that receives data from the client to
228 * pass it to the handlers.
230 struct GNUNET_SCHEDULER_Task *recv_task;
233 * Task that transmit data to the client.
235 struct GNUNET_SCHEDULER_Task *send_task;
238 * User context value, value returned from
239 * the connect callback.
244 * Persist the file handle for this client no matter what happens,
245 * force the OS to close once the process actually dies. Should only
246 * be used in special cases!
251 * Is this client a 'monitor' client that should not be counted
252 * when deciding on destroying the server during soft shutdown?
253 * (see also #GNUNET_SERVICE_start)
258 * Type of last message processed (for warn_no_receive_done).
265 * Check if any of the clients we have left are unrelated to
268 * @param sh service to check clients for
269 * @return #GNUNET_YES if we have non-monitoring clients left
272 have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
274 struct GNUNET_SERVICE_Client *client;
276 for (client = sh->clients_head;NULL != client; client = client->next)
278 if (client->is_monitor)
287 * Shutdown task triggered when a service should be terminated.
288 * This considers active clients and the service options to see
289 * how this specific service is to be terminated, and depending
290 * on this proceeds with the shutdown logic.
292 * @param cls our `struct GNUNET_SERVICE_Handle`
295 service_main (void *cls)
297 struct GNUNET_SERVICE_Handle *sh = cls;
298 struct GNUNET_SERVICE_Client *client;
303 case GNUNET_SERVICE_OPTION_NONE:
304 GNUNET_SERVICE_shutdown (sh);
306 case GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN:
307 /* This task should never be run if we are using
308 the manual shutdown. */
311 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
312 sh->got_shutdown = GNUNET_YES;
313 GNUNET_SERVICE_suspend (sh);
314 if (GNUNET_NO == have_non_monitor_clients (sh))
315 GNUNET_SERVICE_shutdown (sh);
322 * First task run by any service. Initializes our shutdown task,
323 * starts the listening operation on our listen sockets and launches
324 * the custom logic of the application service.
326 * @param cls our `struct GNUNET_SERVICE_Handle`
329 service_main (void *cls)
331 struct GNUNET_SERVICE_Handle *sh = cls;
333 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options)
334 GNUNET_SCHEDULER_add_shutdown (&service_shutdown,
336 GNUNET_SERVICE_resume (sh);
337 sh->service_init_cb (sh->cb_cls,
344 * Creates the "main" function for a GNUnet service. You
345 * should almost always use the #GNUNET_SERVICE_MAIN macro
346 * instead of calling this function directly (except
347 * for ARM, which should call this function directly).
349 * The function will launch the service with the name @a service_name
350 * using the @a service_options to configure its shutdown
351 * behavior. Once the service is ready, the @a init_cb will be called
352 * for service-specific initialization. @a init_cb will be given the
353 * service handler which can be used to control the service's
354 * availability. When clients connect or disconnect, the respective
355 * @a connect_cb or @a disconnect_cb functions will be called. For
356 * messages received from the clients, the respective @a handlers will
357 * be invoked; for the closure of the handlers we use the return value
358 * from the @a connect_cb invocation of the respective client.
360 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
361 * message to receive further messages from this client. If
362 * #GNUNET_SERVICE_client_continue() is not called within a short
363 * time, a warning will be logged. If delays are expected, services
364 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
365 * disable the warning.
367 * Clients sending invalid messages (based on @a handlers) will be
368 * dropped. Additionally, clients can be dropped at any time using
369 * #GNUNET_SERVICE_client_drop().
371 * @param argc number of command-line arguments in @a argv
372 * @param argv array of command-line arguments
373 * @param service_name name of the service to run
374 * @param options options controlling shutdown of the service
375 * @param service_init_cb function to call once the service is ready
376 * @param connect_cb function to call whenever a client connects
377 * @param disconnect_cb function to call whenever a client disconnects
378 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
379 * @param handlers NULL-terminated array of message handlers for the service,
380 * the closure will be set to the value returned by
381 * the @a connect_cb for the respective connection
382 * @return 0 on success, non-zero on error
385 GNUNET_SERVICE_ruN_ (int argc,
387 const char *service_name,
388 enum GNUNET_SERVICE_Options options,
389 GNUNET_SERVICE_InitCallback service_init_cb,
390 GNUNET_SERVICE_ConnectHandler connect_cb,
391 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
393 const struct GNUNET_MQ_MessageHandler *handlers)
395 struct GNUNET_SERVICE_Handle sh;
397 // FIXME: setup (parse command line, configuration, init sh)
398 GNUNET_SCHEDULER_run (&service_main,
406 * Suspend accepting connections from the listen socket temporarily.
407 * Resume activity using #GNUNET_SERVICE_resume.
409 * @param sh service to stop accepting connections.
412 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
414 struct ServiceListenContext *slc;
416 for (slc = slc_head; NULL != slc; slc = slc->next)
418 if (NULL != slc->listen_task)
420 GNUNET_SCHEDULER_cancel (slc->listen_task);
421 slc->listen_task = NULL;
428 * Signature of functions implementing the sending functionality of a
431 * @param mq the message queue
432 * @param msg the message to send
433 * @param impl_state state of the implementation
436 service_mq_send (struct GNUNET_MQ_Handle *mq,
437 const struct GNUNET_MessageHeader *msg,
440 struct GNUNET_SERVICE_Client *client = cls;
442 // FIXME 1: setup "client->send_task" for transmission.
443 // FIXME 2: I seriously hope we do not need to make a copy of `msg`!
444 // OPTIMIZATION: ideally, we'd like the ability to peak at the rest of
445 // the queue and transmit more than one message if possible.
450 * Implementation function that cancels the currently sent message.
452 * @param mq message queue
453 * @param impl_state state specific to the implementation
456 service_mq_cancel (struct GNUNET_MQ_Handle *mq,
459 struct GNUNET_SERVICE_Client *client = cls;
461 // FIXME: stop transmission! (must be possible, otherwise
462 // we must have told MQ that the message was sent!)
467 * Generic error handler, called with the appropriate
468 * error code and the same closure specified at the creation of
470 * Not every message queue implementation supports an error handler.
473 * @param error error code
476 service_mq_error_handler (void *cls,
477 enum GNUNET_MQ_Error error)
479 struct GNUNET_SERVICE_Client *client = cls;
486 * Functions with this signature are called whenever a
487 * complete message is received by the tokenizer for a client.
489 * Do not call #GNUNET_SERVER_mst_destroy() from within
490 * the scope of this callback.
492 * @param cls closure with the `struct GNUNET_SERVICE_Client *`
493 * @param client closure with the `struct GNUNET_SERVICE_Client *`
494 * @param message the actual message
495 * @return #GNUNET_OK on success (always)
498 service_client_mst_cb (void *cls,
500 const struct GNUNET_MessageHeader *message)
502 struct GNUNET_SERVICE_Client *client = cls;
504 GNUNET_MQ_inject_message (client->mq,
511 * A client sent us data. Receive and process it. If we are done,
512 * reschedule this task.
514 * @param cls the `struct GNUNET_SERVICE_Client` that sent us data.
517 service_client_recv (void *cls)
519 struct GNUNET_SERVICE_Client *client = cls;
521 // FIXME: read into buffer, pass to MST, then client->mq inject!
522 // FIXME: revise MST API to avoid the memcpy!
523 // i.e.: GNUNET_MST_read (client->sock);
528 * We have successfully accepted a connection from a client. Now
529 * setup the client (with the scheduler) and tell the application.
531 * @param sh service that accepted the client
532 * @param sock socket associated with the client
535 start_client (struct GNUNET_SERVICE_Handle *sh,
536 struct GNUNET_NETWORK_Handle *csock)
538 struct GNUNET_SERVICE_Client *client;
540 client = GNUNET_new (struct GNUNET_SERVICE_Client);
541 GNUNET_CONTAINER_DLL_insert (sh->clients_head,
545 client->sock = csock;
546 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
551 &service_mq_error_handler,
553 client->mst = GNUNET_SERVER_mst_create (&service_client_mst_cb,
555 client->user_context = sh->connect_cb (sh->cb_cls,
558 GNUNET_MQ_set_handlers_closure (client->mq,
559 client->user_context);
560 client->recv_task = GNUNET_SCHEDULER_add_read (client->sock,
561 &service_client_recv,
567 * We have a client. Accept the incoming socket(s) (and reschedule
570 * @param cls the `struct ServiceListenContext` of the ready listen socket
573 accept_client (void *cls)
575 struct ServiceListenContext *slc = cls;
577 slc->listen_task = NULL;
580 struct GNUNET_NETWORK_Handle *sock;
581 struct sockaddr_in *v4;
582 struct sockaddr_in6 *v6;
583 struct sockaddr_storage sa;
587 addrlen = sizeof (sa);
588 sock = GNUNET_NETWORK_socket_accept (slc->listen_socket,
589 (struct sockaddr *) &sa,
593 switch (sa.sa_family)
596 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
597 v4 = (const struct sockaddr_in *) addr;
598 ok = ( ( (NULL == sh->v4_allowed) ||
599 (check_ipv4_listed (sh->v4_allowed,
601 ( (NULL == sh->v4_denied) ||
602 (! check_ipv4_listed (sh->v4_denied,
606 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
607 v6 = (const struct sockaddr_in6 *) addr;
608 ok = ( ( (NULL == sh->v6_allowed) ||
609 (check_ipv6_listed (sh->v6_allowed,
611 ( (NULL == sh->v6_denied) ||
612 (! check_ipv6_listed (sh->v6_denied,
613 &i6->sin6_addr)) ) );
617 ok = GNUNET_OK; /* controlled using file-system ACL now */
621 LOG (GNUNET_ERROR_TYPE_WARNING,
622 _("Unknown address family %d\n"),
624 return GNUNET_SYSERR;
628 LOG (GNUNET_ERROR_TYPE_DEBUG,
629 "Service rejected incoming connection from %s due to policy.\n",
630 GNUNET_a2s ((const struct sockaddr *) &sa,
632 GNUNET_NETWORK_socket_close (sock);
635 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 "Service accepted incoming connection from %s.\n",
637 GNUNET_a2s ((const struct sockaddr *) &sa,
639 start_client (slc->sh,
642 slc->listen_task = GNUNET_SCHEDULER_add_read (slc->listen_socket,
649 * Resume accepting connections from the listen socket.
651 * @param sh service to resume accepting connections.
654 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
656 struct ServiceListenContext *slc;
658 for (slc = slc_head; NULL != slc; slc = slc->next)
660 GNUNET_assert (NULL == slc->listen_task);
661 slc->listen_task = GNUNET_SCHEDULER_add_read (slc->listen_socket,
669 * Continue receiving further messages from the given client.
670 * Must be called after each message received.
672 * @param c the client to continue receiving from
675 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
677 GNUNET_break (0); // not implemented
682 * Disable the warning the server issues if a message is not
683 * acknowledged in a timely fashion. Use this call if a client is
684 * intentionally delayed for a while. Only applies to the current
687 * @param c client for which to disable the warning
690 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
692 GNUNET_break (NULL != c->warn_task);
693 if (NULL != c->warn_task)
695 GNUNET_SCHEDULER_cancel (c->warn_task);
702 * Ask the server to disconnect from the given client. This is the
703 * same as returning #GNUNET_SYSERR within the check procedure when
704 * handling a message, wexcept that it allows dropping of a client even
705 * when not handling a message from that client. The `disconnect_cb`
706 * will be called on @a c even if the application closes the connection
707 * using this function.
709 * @param c client to disconnect now
712 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
714 struct GNUNET_SERVICE_Handle *sh = c->sh;
716 GNUNET_CONTAINER_DLL_remove (sh->clients_head,
719 sh->disconnect_cb (sh->cb_cls,
722 if (NULL != c->warn_task)
724 GNUNET_SCHEDULER_cancel (c->warn_task);
727 if (NULL != c->recv_task)
729 GNUNET_SCHEDULER_cancel (c->recv_task);
732 if (NULL != c->send_task)
734 GNUNET_SCHEDULER_cancel (c->send_task);
737 GNUNET_SERVER_mst_destroy (c->mst);
738 GNUNET_MQ_destroy (c->mq);
739 if (GNUNET_NO == c->persist)
741 GNUNET_NETWORK_socket_close (c->sock);
745 GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
748 if ( (GNUNET_YES == sh->got_shutdown) &&
749 (GNUNET_NO == have_non_monitor_clients (sh)) )
750 GNUNET_SERVICE_shutdown (sh);
755 * Explicitly stops the service.
757 * @param sh server to shutdown
760 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
762 struct GNUNET_SERVICE_Client *client;
764 GNUNET_SERVICE_suspend (sh);
765 sh->got_shutdown = GNUNET_NO;
766 while (NULL != (client = sh->clients_head))
767 GNUNET_SERVICE_client_drop (client);
772 * Set the 'monitor' flag on this client. Clients which have been
773 * marked as 'monitors' won't prevent the server from shutting down
774 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
775 * that for "normal" clients we likely want to allow them to process
776 * their requests; however, monitor-clients are likely to 'never'
777 * disconnect during shutdown and thus will not be considered when
778 * determining if the server should continue to exist after
779 * shutdown has been triggered.
781 * @param c client to mark as a monitor
784 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
786 c->is_monitor = GNUNET_YES;
787 if ( (GNUNET_YES == sh->got_shutdown) &&
788 (GNUNET_NO == have_non_monitor_clients (sh)) )
789 GNUNET_SERVICE_shutdown (sh);
794 * Set the persist option on this client. Indicates that the
795 * underlying socket or fd should never really be closed. Used for
796 * indicating process death.
798 * @param c client to persist the socket (never to be closed)
801 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
803 c->persist = GNUNET_YES;
807 /* end of service_new.c */