Merge branch 'master' into getopt
[oweals/gnunet.git] / src / include / gnunet_service_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * Functions related to starting services
26  *
27  * @defgroup service  Service library
28  * Start service processes.
29  *
30  * @see [Documentation](https://gnunet.org/developer-handbook-util-services)
31  *
32  * @{
33  */
34
35 #ifndef GNUNET_SERVICE_LIB_H
36 #define GNUNET_SERVICE_LIB_H
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 #include "gnunet_configuration_lib.h"
47 #include "gnunet_mq_lib.h"
48
49 /**
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.
54  */
55 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
56
57 /**
58  * Smallest supported message. NOTE: legacy name.
59  */
60 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
61
62 /**
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.
67  */
68 #define GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69
70
71 /**
72  * Options for the service (bitmask).
73  */
74 enum GNUNET_SERVICE_Options
75 {
76   /**
77    * Use defaults.  Terminates all client connections and the listen
78    * sockets immediately upon receiving the shutdown signal.
79    */
80   GNUNET_SERVICE_OPTION_NONE = 0,
81
82   /**
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().
86    */
87   GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
88
89   /**
90    * Trigger a SOFT server shutdown on signals, allowing active
91    * non-monitor clients to complete their transactions.
92    */
93   GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
94 };
95
96
97
98
99 /**
100  * Opaque handle for a service.
101  */
102 struct GNUNET_SERVICE_Context;
103
104
105 /**
106  * Run a service startup sequence within an existing
107  * initialized system.
108  *
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
113  * @deprecated
114  */
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);
119
120
121 /**
122  * Get the NULL-terminated array of listen sockets for this service.
123  *
124  * @param ctx service context to query
125  * @return NULL if there are no listen sockets, otherwise NULL-terminated
126  *              array of listen sockets.
127  * @deprecated
128  */
129 struct GNUNET_NETWORK_Handle *const *
130 GNUNET_SERVICE_get_listen_sockets (struct GNUNET_SERVICE_Context *ctx);
131
132
133 /**
134  * Stop a service that was started with #GNUNET_SERVICE_start.
135  *
136  * @param sctx the service context returned from the start function
137  * @deprecated
138  */
139 void
140 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
141
142
143 /* **************** NEW SERVICE API ********************** */
144
145 /**
146  * Handle to a service.
147  */
148 struct GNUNET_SERVICE_Handle;
149
150
151 /**
152  * Handle to a client that is connected to a service.
153  */
154 struct GNUNET_SERVICE_Client;
155
156
157 /**
158  * Callback to initialize a service, called exactly once when the service is run.
159  *
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
163  */
164 typedef void
165 (*GNUNET_SERVICE_InitCallback)(void *cls,
166                                const struct GNUNET_CONFIGURATION_Handle *cfg,
167                                struct GNUNET_SERVICE_Handle *sh);
168
169
170 /**
171  * Callback to be called when a client connects to the service.
172  *
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
177  */
178 typedef void *
179 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
180                                  struct GNUNET_SERVICE_Client *c,
181                                  struct GNUNET_MQ_Handle *mq);
182
183
184 /**
185  * Callback to be called when a client disconnected from the service
186  *
187  * @param cls closure for the service
188  * @param c the client that disconnected
189  * @param internal_cls the client-specific (`internal') closure
190  */
191 typedef void
192 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
193                                     struct GNUNET_SERVICE_Client *c,
194                                     void *internal_cls);
195
196
197 /**
198  * Low-level function to start a service if the scheduler
199  * is already running.  Should only be used directly in
200  * special cases.
201  *
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.
209  *
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.
216  *
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().
220  *
221  * The service must be stopped using #GNUNET_SERVICE_stoP().
222  *
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
232  */
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,
238                       void *cls,
239                       const struct GNUNET_MQ_MessageHandler *handlers);
240
241
242 /**
243  * Stops a service that was started with #GNUNET_SERVICE_starT().
244  *
245  * @param srv service to stop
246  */
247 void
248 GNUNET_SERVICE_stoP (struct GNUNET_SERVICE_Handle *srv);
249
250
251 /**
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).
256  *
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.
267  *
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.
274  *
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().
278  *
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
291  */
292 int
293 GNUNET_SERVICE_ruN_ (int argc,
294                      char *const *argv,
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,
300                      void *cls,
301                      const struct GNUNET_MQ_MessageHandler *handlers);
302
303
304 /**
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.
310  *
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.
321  *
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.
328  *
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().
332  *
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
344  *
345  * Sample invocation:
346  * <code>
347  * GNUNET_SERVICE_MAIN
348  * ("resolver",
349  *  GNUNET_SERVICE_OPTION_NONE,
350  *  &init_cb,
351  *  &connect_cb,
352  *  &disconnect_cb,
353  *  closure_for_cb,
354  *  GNUNET_MQ_hd_var_size (get,
355  *                         GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
356  *                         struct GNUNET_RESOLVER_GetMessage,
357  *                         NULL),
358  *  GNUNET_MQ_handler_end ());
359  * </code>
360  */
361 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,...) \
362   int \
363   main (int argc,\
364         char *const *argv)\
365   { \
366     struct GNUNET_MQ_MessageHandler mh[] = { \
367       __VA_ARGS__ \
368     };                        \
369     return GNUNET_SERVICE_ruN_ (argc, \
370                                 argv, \
371                                 service_name, \
372                                 service_options, \
373                                 init_cb, \
374                                 connect_cb, \
375                                 disconnect_cb, \
376                                 cls, \
377                                 mh); \
378   }
379
380
381 /**
382  * Suspend accepting connections from the listen socket temporarily.
383  * Resume activity using #GNUNET_SERVICE_resume.
384  *
385  * @param sh service to stop accepting connections.
386  */
387 void
388 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
389
390
391 /**
392  * Resume accepting connections from the listen socket.
393  *
394  * @param sh service to resume accepting connections.
395  */
396 void
397 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
398
399
400 /**
401  * Continue receiving further messages from the given client.
402  * Must be called after each message received.
403  *
404  * @param c the client to continue receiving from
405  */
406 void
407 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
408
409
410 /**
411  * Obtain the message queue of @a c.  Convenience function.
412  *
413  * @param c the client to continue receiving from
414  * @return the message queue of @a c
415  */
416 struct GNUNET_MQ_Handle *
417 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
418
419
420 /**
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
424  * message.
425  *
426  * @param c client for which to disable the warning
427  */
428 void
429 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
430
431
432 /**
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.
439  *
440  * @param c client to disconnect now
441  */
442 void
443 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
444
445
446 /**
447  * Explicitly stops the service.
448  *
449  * @param sh server to shutdown
450  */
451 void
452 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
453
454
455 /**
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.
464  *
465  * @param c client to mark as a monitor
466  */
467 void
468 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
469
470
471 /**
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.
475  *
476  * @param c client to persist the socket (never to be closed)
477  */
478 void
479 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
480
481
482 #if 0                           /* keep Emacsens' auto-indent happy */
483 {
484 #endif
485 #ifdef __cplusplus
486 }
487 #endif
488
489 /* ifndef GNUNET_SERVICE_LIB_H */
490 #endif
491
492 /** @} */  /* end of group service */
493
494 /* end of gnunet_service_lib.h */