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 * Creates the "main" function for a GNUnet service. You
253 * should almost always use the #GNUNET_SERVICE_MAIN macro
254 * instead of calling this function directly (except
255 * for ARM, which should call this function directly).
257 * The function will launch the service with the name @a service_name
258 * using the @a service_options to configure its shutdown
259 * behavior. Once the service is ready, the @a init_cb will be called
260 * for service-specific initialization. @a init_cb will be given the
261 * service handler which can be used to control the service's
262 * availability. When clients connect or disconnect, the respective
263 * @a connect_cb or @a disconnect_cb functions will be called. For
264 * messages received from the clients, the respective @a handlers will
265 * be invoked; for the closure of the handlers we use the return value
266 * from the @a connect_cb invocation of the respective client.
268 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
269 * message to receive further messages from this client. If
270 * #GNUNET_SERVICE_client_continue() is not called within a short
271 * time, a warning will be logged. If delays are expected, services
272 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
273 * disable the warning.
275 * Clients sending invalid messages (based on @a handlers) will be
276 * dropped. Additionally, clients can be dropped at any time using
277 * #GNUNET_SERVICE_client_drop().
279 * @param argc number of command-line arguments in @a argv
280 * @param argv array of command-line arguments
281 * @param service_name name of the service to run
282 * @param options options controlling shutdown of the service
283 * @param service_init_cb function to call once the service is ready
284 * @param connect_cb function to call whenever a client connects
285 * @param disconnect_cb function to call whenever a client disconnects
286 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
287 * @param handlers NULL-terminated array of message handlers for the service,
288 * the closure will be set to the value returned by
289 * the @a connect_cb for the respective connection
290 * @return 0 on success, non-zero on error
293 GNUNET_SERVICE_ruN_ (int argc,
295 const char *service_name,
296 enum GNUNET_SERVICE_Options options,
297 GNUNET_SERVICE_InitCallback service_init_cb,
298 GNUNET_SERVICE_ConnectHandler connect_cb,
299 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
301 const struct GNUNET_MQ_MessageHandler *handlers);
305 * Creates the "main" function for a GNUnet service. You
306 * MUST use this macro to define GNUnet services (except
307 * for ARM, which MUST NOT use the macro). The reason is
308 * the GNUnet-as-a-library project, where we will not define
309 * a main function anywhere but in ARM.
311 * The macro 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 service_name name of the service to run
334 * @param options options controlling shutdown of the service
335 * @param service_init_cb function to call once the service is ready
336 * @param connect_cb function to call whenever a client connects
337 * @param disconnect_cb function to call whenever a client disconnects
338 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
339 * @param handlers NULL-terminated array of message handlers for the service,
340 * the closure will be set to the value returned by
341 * the @a connect_cb for the respective connection
342 * @return 0 on success, non-zero on error
344 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,handlers) \
349 return GNUNET_SERVICE_ruN_ (argc, \
362 * Suspend accepting connections from the listen socket temporarily.
363 * Resume activity using #GNUNET_SERVICE_resume.
365 * @param sh service to stop accepting connections.
368 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
372 * Resume accepting connections from the listen socket.
374 * @param sh service to resume accepting connections.
377 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
381 * Continue receiving further messages from the given client.
382 * Must be called after each message received.
384 * @param c the client to continue receiving from
387 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
391 * Disable the warning the server issues if a message is not
392 * acknowledged in a timely fashion. Use this call if a client is
393 * intentionally delayed for a while. Only applies to the current
396 * @param c client for which to disable the warning
399 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
403 * Ask the server to disconnect from the given client. This is the
404 * same as returning #GNUNET_SYSERR within the check procedure when
405 * handling a message, wexcept that it allows dropping of a client even
406 * when not handling a message from that client. The `disconnect_cb`
407 * will be called on @a c even if the application closes the connection
408 * using this function.
410 * @param c client to disconnect now
413 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
417 * Explicitly stops the service.
419 * @param sh server to shutdown
422 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
426 * Set the 'monitor' flag on this client. Clients which have been
427 * marked as 'monitors' won't prevent the server from shutting down
428 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
429 * that for "normal" clients we likely want to allow them to process
430 * their requests; however, monitor-clients are likely to 'never'
431 * disconnect during shutdown and thus will not be considered when
432 * determining if the server should continue to exist after
433 * shutdown has been triggered.
435 * @param c client to mark as a monitor
438 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
442 * Set the persist option on this client. Indicates that the
443 * underlying socket or fd should never really be closed. Used for
444 * indicating process death.
446 * @param c client to persist the socket (never to be closed)
449 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
452 #if 0 /* keep Emacsens' auto-indent happy */
459 /* ifndef GNUNET_SERVICE_LIB_H */
462 /** @} */ /* end of group service */
464 /* end of gnunet_service_lib.h */