2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 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 * @author Christian Grothoff
25 * Functions related to starting services
27 * @defgroup service Service library
28 * Start service processes.
30 * @see [Documentation](https://gnunet.org/developer-handbook-util-services)
35 #ifndef GNUNET_SERVICE_LIB_H
36 #define GNUNET_SERVICE_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_configuration_lib.h"
47 #include "gnunet_server_lib.h"
48 #include "gnunet_mq_lib.h"
52 * Get the list of addresses that a server for the given service
55 * @param service_name name of the service
56 * @param cfg configuration (which specifies the addresses)
57 * @param addrs set (call by reference) to an array of pointers to the
58 * addresses the server should bind to and listen on; the
59 * array will be NULL-terminated (on success)
60 * @param addr_lens set (call by reference) to an array of the lengths
61 * of the respective `struct sockaddr` struct in the @a addrs
63 * @return number of addresses found on success,
64 * #GNUNET_SYSERR if the configuration
65 * did not specify reasonable finding information or
66 * if it specified a hostname that could not be resolved;
67 * #GNUNET_NO if the number of addresses configured is
68 * zero (in this case, '* @a addrs' and '* @a addr_lens' will be
72 GNUNET_SERVICE_get_server_addresses (const char *service_name,
73 const struct GNUNET_CONFIGURATION_Handle *cfg,
74 struct sockaddr ***addrs,
75 socklen_t **addr_lens);
79 * Function called by the service's run
80 * method to run service-specific setup code.
83 * @param server the initialized server
84 * @param cfg configuration to use
87 (*GNUNET_SERVICE_Main) (void *cls,
88 struct GNUNET_SERVER_Handle *server,
89 const struct GNUNET_CONFIGURATION_Handle *cfg);
93 * Options for the service (bitmask).
95 enum GNUNET_SERVICE_Options
98 * Use defaults. Terminates all client connections and the listen
99 * sockets immediately upon receiving the shutdown signal.
101 GNUNET_SERVICE_OPTION_NONE = 0,
104 * Do not trigger server shutdown on signal at all; instead, allow
105 * for the user to terminate the server explicitly when needed
106 * by calling #GNUNET_SERVICE_shutdown().
108 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
111 * Trigger a SOFT server shutdown on signals, allowing active
112 * non-monitor clients to complete their transactions.
114 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
119 * Run a standard GNUnet service startup sequence (initialize loggers
120 * and configuration, parse options).
122 * @param argc number of command line arguments in @a argv
123 * @param argv command line arguments
124 * @param service_name our service name
125 * @param options service options
126 * @param task main task of the service
127 * @param task_cls closure for @a task
128 * @return #GNUNET_SYSERR on error, #GNUNET_OK
129 * if we shutdown nicely
133 GNUNET_SERVICE_run (int argc,
135 const char *service_name,
136 enum GNUNET_SERVICE_Options options,
137 GNUNET_SERVICE_Main task,
142 * Opaque handle for a service.
144 struct GNUNET_SERVICE_Context;
148 * Run a service startup sequence within an existing
149 * initialized system.
151 * @param service_name our service name
152 * @param cfg configuration to use
153 * @param options service options
154 * @return NULL on error, service handle
157 struct GNUNET_SERVICE_Context *
158 GNUNET_SERVICE_start (const char *service_name,
159 const struct GNUNET_CONFIGURATION_Handle *cfg,
160 enum GNUNET_SERVICE_Options options);
164 * Obtain the server used by a service. Note that the server must NOT
165 * be destroyed by the caller.
167 * @param ctx the service context returned from the start function
168 * @return handle to the server for this service, NULL if there is none
171 struct GNUNET_SERVER_Handle *
172 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx);
176 * Get the NULL-terminated array of listen sockets for this service.
178 * @param ctx service context to query
179 * @return NULL if there are no listen sockets, otherwise NULL-terminated
180 * array of listen sockets.
183 struct GNUNET_NETWORK_Handle *const *
184 GNUNET_SERVICE_get_listen_sockets (struct GNUNET_SERVICE_Context *ctx);
188 * Stop a service that was started with #GNUNET_SERVICE_start.
190 * @param sctx the service context returned from the start function
194 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
197 /* **************** NEW SERVICE API ********************** */
200 * Handle to a service.
202 struct GNUNET_SERVICE_Handle;
206 * Handle to a client that is connected to a service.
208 struct GNUNET_SERVICE_Client;
212 * Callback to initialize a service, called exactly once when the service is run.
214 * @param cls closure passed to #GNUNET_SERVICE_MAIN
215 * @param cfg configuration to use for this service
216 * @param sh handle to the newly create service
219 (*GNUNET_SERVICE_InitCallback)(void *cls,
220 const struct GNUNET_CONFIGURATION_Handle *cfg,
221 struct GNUNET_SERVICE_Handle *sh);
225 * Callback to be called when a client connects to the service.
227 * @param cls closure for the service
228 * @param c the new client that connected to the service
229 * @param mq the message queue used to send messages to the client
230 * @return the client-specific (`internal') closure
233 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
234 struct GNUNET_SERVICE_Client *c,
235 struct GNUNET_MQ_Handle *mq);
239 * Callback to be called when a client disconnected from the service
241 * @param cls closure for the service
242 * @param c the client that disconnected
243 * @param internal_cls the client-specific (`internal') closure
246 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
247 struct GNUNET_SERVICE_Client *c,
252 * Low-level function to start a service if the scheduler
253 * is already running. Should only be used directly in
256 * The function will launch the service with the name @a service_name
257 * using the @a service_options to configure its shutdown
258 * behavior. When clients connect or disconnect, the respective
259 * @a connect_cb or @a disconnect_cb functions will be called. For
260 * messages received from the clients, the respective @a handlers will
261 * be invoked; for the closure of the handlers we use the return value
262 * from the @a connect_cb invocation of the respective client.
264 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
265 * message to receive further messages from this client. If
266 * #GNUNET_SERVICE_client_continue() is not called within a short
267 * time, a warning will be logged. If delays are expected, services
268 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
269 * disable the warning.
271 * Clients sending invalid messages (based on @a handlers) will be
272 * dropped. Additionally, clients can be dropped at any time using
273 * #GNUNET_SERVICE_client_drop().
275 * The service must be stopped using #GNUNET_SERVICE_stoP().
277 * @param service_name name of the service to run
278 * @param cfg configuration to use
279 * @param connect_cb function to call whenever a client connects
280 * @param disconnect_cb function to call whenever a client disconnects
281 * @param cls closure argument for @a connect_cb and @a disconnect_cb
282 * @param handlers NULL-terminated array of message handlers for the service,
283 * the closure will be set to the value returned by
284 * the @a connect_cb for the respective connection
285 * @return NULL on error
287 struct GNUNET_SERVICE_Handle *
288 GNUNET_SERVICE_starT (const char *service_name,
289 const struct GNUNET_CONFIGURATION_Handle *cfg,
290 GNUNET_SERVICE_ConnectHandler connect_cb,
291 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
293 const struct GNUNET_MQ_MessageHandler *handlers);
297 * Stops a service that was started with #GNUNET_SERVICE_starT().
299 * @param srv service to stop
302 GNUNET_SERVICE_stoP (struct GNUNET_SERVICE_Handle *srv);
306 * Creates the "main" function for a GNUnet service. You
307 * should almost always use the #GNUNET_SERVICE_MAIN macro
308 * instead of calling this function directly (except
309 * for ARM, which should call this function directly).
311 * The function will launch the service with the name @a service_name
312 * using the @a service_options to configure its shutdown
313 * behavior. Once the service is ready, the @a init_cb will be called
314 * for service-specific initialization. @a init_cb will be given the
315 * service handler which can be used to control the service's
316 * availability. When clients connect or disconnect, the respective
317 * @a connect_cb or @a disconnect_cb functions will be called. For
318 * messages received from the clients, the respective @a handlers will
319 * be invoked; for the closure of the handlers we use the return value
320 * from the @a connect_cb invocation of the respective client.
322 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
323 * message to receive further messages from this client. If
324 * #GNUNET_SERVICE_client_continue() is not called within a short
325 * time, a warning will be logged. If delays are expected, services
326 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
327 * disable the warning.
329 * Clients sending invalid messages (based on @a handlers) will be
330 * dropped. Additionally, clients can be dropped at any time using
331 * #GNUNET_SERVICE_client_drop().
333 * @param argc number of command-line arguments in @a argv
334 * @param argv array of command-line arguments
335 * @param service_name name of the service to run
336 * @param options options controlling shutdown of the service
337 * @param service_init_cb function to call once the service is ready
338 * @param connect_cb function to call whenever a client connects
339 * @param disconnect_cb function to call whenever a client disconnects
340 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
341 * @param handlers NULL-terminated array of message handlers for the service,
342 * the closure will be set to the value returned by
343 * the @a connect_cb for the respective connection
344 * @return 0 on success, non-zero on error
347 GNUNET_SERVICE_ruN_ (int argc,
349 const char *service_name,
350 enum GNUNET_SERVICE_Options options,
351 GNUNET_SERVICE_InitCallback service_init_cb,
352 GNUNET_SERVICE_ConnectHandler connect_cb,
353 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
355 const struct GNUNET_MQ_MessageHandler *handlers);
359 * Creates the "main" function for a GNUnet service. You
360 * MUST use this macro to define GNUnet services (except
361 * for ARM, which MUST NOT use the macro). The reason is
362 * the GNUnet-as-a-library project, where we will not define
363 * a main function anywhere but in ARM.
365 * The macro will launch the service with the name @a service_name
366 * using the @a service_options to configure its shutdown
367 * behavior. Once the service is ready, the @a init_cb will be called
368 * for service-specific initialization. @a init_cb will be given the
369 * service handler which can be used to control the service's
370 * availability. When clients connect or disconnect, the respective
371 * @a connect_cb or @a disconnect_cb functions will be called. For
372 * messages received from the clients, the respective @a handlers will
373 * be invoked; for the closure of the handlers we use the return value
374 * from the @a connect_cb invocation of the respective client.
376 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
377 * message to receive further messages from this client. If
378 * #GNUNET_SERVICE_client_continue() is not called within a short
379 * time, a warning will be logged. If delays are expected, services
380 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
381 * disable the warning.
383 * Clients sending invalid messages (based on @a handlers) will be
384 * dropped. Additionally, clients can be dropped at any time using
385 * #GNUNET_SERVICE_client_drop().
387 * @param service_name name of the service to run
388 * @param options options controlling shutdown of the service
389 * @param service_init_cb function to call once the service is ready
390 * @param connect_cb function to call whenever a client connects
391 * @param disconnect_cb function to call whenever a client disconnects
392 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
393 * @param ... array of message handlers for the service, terminated
394 * by #GNUNET_MQ_handler_end();
395 * the closure will be set to the value returned by
396 * the @a connect_cb for the respective connection
397 * @return 0 on success, non-zero on error
401 * GNUNET_SERVICE_MAIN
403 * GNUNET_SERVICE_OPTION_NONE,
408 * GNUNET_MQ_hd_var_size (get,
409 * GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
410 * struct GNUNET_RESOLVER_GetMessage,
412 * GNUNET_MQ_handler_end ());
415 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,...) \
420 struct GNUNET_MQ_MessageHandler mh[] = { \
423 return GNUNET_SERVICE_ruN_ (argc, \
436 * Suspend accepting connections from the listen socket temporarily.
437 * Resume activity using #GNUNET_SERVICE_resume.
439 * @param sh service to stop accepting connections.
442 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
446 * Resume accepting connections from the listen socket.
448 * @param sh service to resume accepting connections.
451 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
455 * Continue receiving further messages from the given client.
456 * Must be called after each message received.
458 * @param c the client to continue receiving from
461 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
465 * Obtain the message queue of @a c. Convenience function.
467 * @param c the client to continue receiving from
468 * @return the message queue of @a c
470 struct GNUNET_MQ_Handle *
471 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
475 * Disable the warning the server issues if a message is not
476 * acknowledged in a timely fashion. Use this call if a client is
477 * intentionally delayed for a while. Only applies to the current
480 * @param c client for which to disable the warning
483 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
487 * Ask the server to disconnect from the given client. This is the
488 * same as returning #GNUNET_SYSERR within the check procedure when
489 * handling a message, wexcept that it allows dropping of a client even
490 * when not handling a message from that client. The `disconnect_cb`
491 * will be called on @a c even if the application closes the connection
492 * using this function.
494 * @param c client to disconnect now
497 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
501 * Explicitly stops the service.
503 * @param sh server to shutdown
506 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
510 * Set the 'monitor' flag on this client. Clients which have been
511 * marked as 'monitors' won't prevent the server from shutting down
512 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
513 * that for "normal" clients we likely want to allow them to process
514 * their requests; however, monitor-clients are likely to 'never'
515 * disconnect during shutdown and thus will not be considered when
516 * determining if the server should continue to exist after
517 * shutdown has been triggered.
519 * @param c client to mark as a monitor
522 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
526 * Set the persist option on this client. Indicates that the
527 * underlying socket or fd should never really be closed. Used for
528 * indicating process death.
530 * @param c client to persist the socket (never to be closed)
533 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
536 #if 0 /* keep Emacsens' auto-indent happy */
543 /* ifndef GNUNET_SERVICE_LIB_H */
546 /** @} */ /* end of group service */
548 /* end of gnunet_service_lib.h */