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_mq_lib.h"
50 * Largest supported message (to be precise, one byte more
51 * than the largest possible message, so tests involving
52 * this value should check for messages being smaller than
53 * this value). NOTE: legacy name.
55 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
58 * Smallest supported message. NOTE: legacy name.
60 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
63 * Timeout we use on TCP connect before trying another
64 * result from the DNS resolver. Actual value used
65 * is this value divided by the number of address families.
66 * Default is 5s. NOTE: legacy name.
68 #define GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
72 * Options for the service (bitmask).
74 enum GNUNET_SERVICE_Options
77 * Use defaults. Terminates all client connections and the listen
78 * sockets immediately upon receiving the shutdown signal.
80 GNUNET_SERVICE_OPTION_NONE = 0,
83 * Do not trigger server shutdown on signal at all; instead, allow
84 * for the user to terminate the server explicitly when needed
85 * by calling #GNUNET_SERVICE_shutdown().
87 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
90 * Trigger a SOFT server shutdown on signals, allowing active
91 * non-monitor clients to complete their transactions.
93 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
100 * Opaque handle for a service.
102 struct GNUNET_SERVICE_Context;
106 * Run a service startup sequence within an existing
107 * initialized system.
109 * @param service_name our service name
110 * @param cfg configuration to use
111 * @param options service options
112 * @return NULL on error, service handle
115 struct GNUNET_SERVICE_Context *
116 GNUNET_SERVICE_start (const char *service_name,
117 const struct GNUNET_CONFIGURATION_Handle *cfg,
118 enum GNUNET_SERVICE_Options options);
122 * Get the NULL-terminated array of listen sockets for this service.
124 * @param ctx service context to query
125 * @return NULL if there are no listen sockets, otherwise NULL-terminated
126 * array of listen sockets.
129 struct GNUNET_NETWORK_Handle *const *
130 GNUNET_SERVICE_get_listen_sockets (struct GNUNET_SERVICE_Context *ctx);
134 * Stop a service that was started with #GNUNET_SERVICE_start.
136 * @param sctx the service context returned from the start function
140 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
143 /* **************** NEW SERVICE API ********************** */
146 * Handle to a service.
148 struct GNUNET_SERVICE_Handle;
152 * Handle to a client that is connected to a service.
154 struct GNUNET_SERVICE_Client;
158 * Callback to initialize a service, called exactly once when the service is run.
160 * @param cls closure passed to #GNUNET_SERVICE_MAIN
161 * @param cfg configuration to use for this service
162 * @param sh handle to the newly create service
165 (*GNUNET_SERVICE_InitCallback)(void *cls,
166 const struct GNUNET_CONFIGURATION_Handle *cfg,
167 struct GNUNET_SERVICE_Handle *sh);
171 * Callback to be called when a client connects to the service.
173 * @param cls closure for the service
174 * @param c the new client that connected to the service
175 * @param mq the message queue used to send messages to the client
176 * @return the client-specific (`internal') closure
179 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
180 struct GNUNET_SERVICE_Client *c,
181 struct GNUNET_MQ_Handle *mq);
185 * Callback to be called when a client disconnected from the service
187 * @param cls closure for the service
188 * @param c the client that disconnected
189 * @param internal_cls the client-specific (`internal') closure
192 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
193 struct GNUNET_SERVICE_Client *c,
198 * Low-level function to start a service if the scheduler
199 * is already running. Should only be used directly in
202 * The function will launch the service with the name @a service_name
203 * using the @a service_options to configure its shutdown
204 * behavior. When clients connect or disconnect, the respective
205 * @a connect_cb or @a disconnect_cb functions will be called. For
206 * messages received from the clients, the respective @a handlers will
207 * be invoked; for the closure of the handlers we use the return value
208 * from the @a connect_cb invocation of the respective client.
210 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
211 * message to receive further messages from this client. If
212 * #GNUNET_SERVICE_client_continue() is not called within a short
213 * time, a warning will be logged. If delays are expected, services
214 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
215 * disable the warning.
217 * Clients sending invalid messages (based on @a handlers) will be
218 * dropped. Additionally, clients can be dropped at any time using
219 * #GNUNET_SERVICE_client_drop().
221 * The service must be stopped using #GNUNET_SERVICE_stoP().
223 * @param service_name name of the service to run
224 * @param cfg configuration to use
225 * @param connect_cb function to call whenever a client connects
226 * @param disconnect_cb function to call whenever a client disconnects
227 * @param cls closure argument for @a connect_cb and @a disconnect_cb
228 * @param handlers NULL-terminated array of message handlers for the service,
229 * the closure will be set to the value returned by
230 * the @a connect_cb for the respective connection
231 * @return NULL on error
233 struct GNUNET_SERVICE_Handle *
234 GNUNET_SERVICE_starT (const char *service_name,
235 const struct GNUNET_CONFIGURATION_Handle *cfg,
236 GNUNET_SERVICE_ConnectHandler connect_cb,
237 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
239 const struct GNUNET_MQ_MessageHandler *handlers);
243 * Stops a service that was started with #GNUNET_SERVICE_starT().
245 * @param srv service to stop
248 GNUNET_SERVICE_stoP (struct GNUNET_SERVICE_Handle *srv);
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 ... array of message handlers for the service, terminated
340 * by #GNUNET_MQ_handler_end();
341 * the closure will be set to the value returned by
342 * the @a connect_cb for the respective connection
343 * @return 0 on success, non-zero on error
347 * GNUNET_SERVICE_MAIN
349 * GNUNET_SERVICE_OPTION_NONE,
354 * GNUNET_MQ_hd_var_size (get,
355 * GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
356 * struct GNUNET_RESOLVER_GetMessage,
358 * GNUNET_MQ_handler_end ());
361 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,...) \
366 struct GNUNET_MQ_MessageHandler mh[] = { \
369 return GNUNET_SERVICE_ruN_ (argc, \
382 * Suspend accepting connections from the listen socket temporarily.
383 * Resume activity using #GNUNET_SERVICE_resume.
385 * @param sh service to stop accepting connections.
388 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
392 * Resume accepting connections from the listen socket.
394 * @param sh service to resume accepting connections.
397 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
401 * Continue receiving further messages from the given client.
402 * Must be called after each message received.
404 * @param c the client to continue receiving from
407 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
411 * Obtain the message queue of @a c. Convenience function.
413 * @param c the client to continue receiving from
414 * @return the message queue of @a c
416 struct GNUNET_MQ_Handle *
417 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
421 * Disable the warning the server issues if a message is not
422 * acknowledged in a timely fashion. Use this call if a client is
423 * intentionally delayed for a while. Only applies to the current
426 * @param c client for which to disable the warning
429 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
433 * Ask the server to disconnect from the given client. This is the
434 * same as returning #GNUNET_SYSERR within the check procedure when
435 * handling a message, wexcept that it allows dropping of a client even
436 * when not handling a message from that client. The `disconnect_cb`
437 * will be called on @a c even if the application closes the connection
438 * using this function.
440 * @param c client to disconnect now
443 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
447 * Explicitly stops the service.
449 * @param sh server to shutdown
452 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
456 * Set the 'monitor' flag on this client. Clients which have been
457 * marked as 'monitors' won't prevent the server from shutting down
458 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
459 * that for "normal" clients we likely want to allow them to process
460 * their requests; however, monitor-clients are likely to 'never'
461 * disconnect during shutdown and thus will not be considered when
462 * determining if the server should continue to exist after
463 * shutdown has been triggered.
465 * @param c client to mark as a monitor
468 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
472 * Set the persist option on this client. Indicates that the
473 * underlying socket or fd should never really be closed. Used for
474 * indicating process death.
476 * @param c client to persist the socket (never to be closed)
479 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
482 #if 0 /* keep Emacsens' auto-indent happy */
489 /* ifndef GNUNET_SERVICE_LIB_H */
492 /** @} */ /* end of group service */
494 /* end of gnunet_service_lib.h */