-add reverse autoadd; with test
[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_server_lib.h"
48 #include "gnunet_mq_lib.h"
49
50
51 /**
52  * Get the list of addresses that a server for the given service
53  * should bind to.
54  *
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
62  *              array (on success)
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
69  *              set to NULL).
70  */
71 int
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);
76
77
78 /**
79  * Function called by the service's run
80  * method to run service-specific setup code.
81  *
82  * @param cls closure
83  * @param server the initialized server
84  * @param cfg configuration to use
85  */
86 typedef void
87 (*GNUNET_SERVICE_Main) (void *cls,
88                         struct GNUNET_SERVER_Handle *server,
89                         const struct GNUNET_CONFIGURATION_Handle *cfg);
90
91
92 /**
93  * Options for the service (bitmask).
94  */
95 enum GNUNET_SERVICE_Options
96 {
97   /**
98    * Use defaults.  Terminates all client connections and the listen
99    * sockets immediately upon receiving the shutdown signal.
100    */
101   GNUNET_SERVICE_OPTION_NONE = 0,
102
103   /**
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().
107    */
108   GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
109
110   /**
111    * Trigger a SOFT server shutdown on signals, allowing active
112    * non-monitor clients to complete their transactions.
113    */
114   GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
115 };
116
117
118 /**
119  * Run a standard GNUnet service startup sequence (initialize loggers
120  * and configuration, parse options).
121  *
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
130  * @deprecated
131  */
132 int
133 GNUNET_SERVICE_run (int argc,
134                     char *const *argv,
135                     const char *service_name,
136                     enum GNUNET_SERVICE_Options options,
137                     GNUNET_SERVICE_Main task,
138                     void *task_cls);
139
140
141 /**
142  * Opaque handle for a service.
143  */
144 struct GNUNET_SERVICE_Context;
145
146
147 /**
148  * Run a service startup sequence within an existing
149  * initialized system.
150  *
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
155  * @deprecated
156  */
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);
161
162
163 /**
164  * Obtain the server used by a service.  Note that the server must NOT
165  * be destroyed by the caller.
166  *
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
169  * @deprecated
170  */
171 struct GNUNET_SERVER_Handle *
172 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx);
173
174
175 /**
176  * Get the NULL-terminated array of listen sockets for this service.
177  *
178  * @param ctx service context to query
179  * @return NULL if there are no listen sockets, otherwise NULL-terminated
180  *              array of listen sockets.
181  * @deprecated
182  */
183 struct GNUNET_NETWORK_Handle *const *
184 GNUNET_SERVICE_get_listen_sockets (struct GNUNET_SERVICE_Context *ctx);
185
186
187 /**
188  * Stop a service that was started with #GNUNET_SERVICE_start.
189  *
190  * @param sctx the service context returned from the start function
191  * @deprecated
192  */
193 void
194 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
195
196
197 /* **************** NEW SERVICE API ********************** */
198
199 /**
200  * Handle to a service.
201  */
202 struct GNUNET_SERVICE_Handle;
203
204
205 /**
206  * Handle to a client that is connected to a service.
207  */
208 struct GNUNET_SERVICE_Client;
209
210
211 /**
212  * Callback to initialize a service, called exactly once when the service is run.
213  *
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
217  */
218 typedef void
219 (*GNUNET_SERVICE_InitCallback)(void *cls,
220                                const struct GNUNET_CONFIGURATION_Handle *cfg,
221                                struct GNUNET_SERVICE_Handle *sh);
222
223
224 /**
225  * Callback to be called when a client connects to the service.
226  *
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
231  */
232 typedef void *
233 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
234                                  struct GNUNET_SERVICE_Client *c,
235                                  struct GNUNET_MQ_Handle *mq);
236
237
238 /**
239  * Callback to be called when a client disconnected from the service
240  *
241  * @param cls closure for the service
242  * @param c the client that disconnected
243  * @param internal_cls the client-specific (`internal') closure
244  */
245 typedef void
246 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
247                                     struct GNUNET_SERVICE_Client *c,
248                                     void *internal_cls);
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 */