move to new client API: remove old client API
[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  * Function called by the service's run
53  * method to run service-specific setup code.
54  *
55  * @param cls closure
56  * @param server the initialized server
57  * @param cfg configuration to use
58  */
59 typedef void
60 (*GNUNET_SERVICE_Main) (void *cls,
61                         struct GNUNET_SERVER_Handle *server,
62                         const struct GNUNET_CONFIGURATION_Handle *cfg);
63
64
65 /**
66  * Options for the service (bitmask).
67  */
68 enum GNUNET_SERVICE_Options
69 {
70   /**
71    * Use defaults.  Terminates all client connections and the listen
72    * sockets immediately upon receiving the shutdown signal.
73    */
74   GNUNET_SERVICE_OPTION_NONE = 0,
75
76   /**
77    * Do not trigger server shutdown on signal at all; instead, allow
78    * for the user to terminate the server explicitly when needed
79    * by calling #GNUNET_SERVICE_shutdown().
80    */
81   GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
82
83   /**
84    * Trigger a SOFT server shutdown on signals, allowing active
85    * non-monitor clients to complete their transactions.
86    */
87   GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
88 };
89
90
91 /**
92  * Run a standard GNUnet service startup sequence (initialize loggers
93  * and configuration, parse options).
94  *
95  * @param argc number of command line arguments in @a argv
96  * @param argv command line arguments
97  * @param service_name our service name
98  * @param options service options
99  * @param task main task of the service
100  * @param task_cls closure for @a task
101  * @return #GNUNET_SYSERR on error, #GNUNET_OK
102  *         if we shutdown nicely
103  * @deprecated
104  */
105 int
106 GNUNET_SERVICE_run (int argc,
107                     char *const *argv,
108                     const char *service_name,
109                     enum GNUNET_SERVICE_Options options,
110                     GNUNET_SERVICE_Main task,
111                     void *task_cls);
112
113
114 /**
115  * Opaque handle for a service.
116  */
117 struct GNUNET_SERVICE_Context;
118
119
120 /**
121  * Run a service startup sequence within an existing
122  * initialized system.
123  *
124  * @param service_name our service name
125  * @param cfg configuration to use
126  * @param options service options
127  * @return NULL on error, service handle
128  * @deprecated
129  */
130 struct GNUNET_SERVICE_Context *
131 GNUNET_SERVICE_start (const char *service_name,
132                       const struct GNUNET_CONFIGURATION_Handle *cfg,
133                       enum GNUNET_SERVICE_Options options);
134
135
136 /**
137  * Obtain the server used by a service.  Note that the server must NOT
138  * be destroyed by the caller.
139  *
140  * @param ctx the service context returned from the start function
141  * @return handle to the server for this service, NULL if there is none
142  * @deprecated
143  */
144 struct GNUNET_SERVER_Handle *
145 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx);
146
147
148 /**
149  * Get the NULL-terminated array of listen sockets for this service.
150  *
151  * @param ctx service context to query
152  * @return NULL if there are no listen sockets, otherwise NULL-terminated
153  *              array of listen sockets.
154  * @deprecated
155  */
156 struct GNUNET_NETWORK_Handle *const *
157 GNUNET_SERVICE_get_listen_sockets (struct GNUNET_SERVICE_Context *ctx);
158
159
160 /**
161  * Stop a service that was started with #GNUNET_SERVICE_start.
162  *
163  * @param sctx the service context returned from the start function
164  * @deprecated
165  */
166 void
167 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
168
169
170 /* **************** NEW SERVICE API ********************** */
171
172 /**
173  * Handle to a service.
174  */
175 struct GNUNET_SERVICE_Handle;
176
177
178 /**
179  * Handle to a client that is connected to a service.
180  */
181 struct GNUNET_SERVICE_Client;
182
183
184 /**
185  * Callback to initialize a service, called exactly once when the service is run.
186  *
187  * @param cls closure passed to #GNUNET_SERVICE_MAIN
188  * @param cfg configuration to use for this service
189  * @param sh handle to the newly create service
190  */
191 typedef void
192 (*GNUNET_SERVICE_InitCallback)(void *cls,
193                                const struct GNUNET_CONFIGURATION_Handle *cfg,
194                                struct GNUNET_SERVICE_Handle *sh);
195
196
197 /**
198  * Callback to be called when a client connects to the service.
199  *
200  * @param cls closure for the service
201  * @param c the new client that connected to the service
202  * @param mq the message queue used to send messages to the client
203  * @return the client-specific (`internal') closure
204  */
205 typedef void *
206 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
207                                  struct GNUNET_SERVICE_Client *c,
208                                  struct GNUNET_MQ_Handle *mq);
209
210
211 /**
212  * Callback to be called when a client disconnected from the service
213  *
214  * @param cls closure for the service
215  * @param c the client that disconnected
216  * @param internal_cls the client-specific (`internal') closure
217  */
218 typedef void
219 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
220                                     struct GNUNET_SERVICE_Client *c,
221                                     void *internal_cls);
222
223
224 /**
225  * Low-level function to start a service if the scheduler
226  * is already running.  Should only be used directly in
227  * special cases.
228  *
229  * The function will launch the service with the name @a service_name
230  * using the @a service_options to configure its shutdown
231  * behavior. When clients connect or disconnect, the respective
232  * @a connect_cb or @a disconnect_cb functions will be called. For
233  * messages received from the clients, the respective @a handlers will
234  * be invoked; for the closure of the handlers we use the return value
235  * from the @a connect_cb invocation of the respective client.
236  *
237  * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
238  * message to receive further messages from this client.  If
239  * #GNUNET_SERVICE_client_continue() is not called within a short
240  * time, a warning will be logged. If delays are expected, services
241  * should call #GNUNET_SERVICE_client_disable_continue_warning() to
242  * disable the warning.
243  *
244  * Clients sending invalid messages (based on @a handlers) will be
245  * dropped. Additionally, clients can be dropped at any time using
246  * #GNUNET_SERVICE_client_drop().
247  *
248  * The service must be stopped using #GNUNET_SERVICE_stoP().
249  *
250  * @param service_name name of the service to run
251  * @param cfg configuration to use
252  * @param connect_cb function to call whenever a client connects
253  * @param disconnect_cb function to call whenever a client disconnects
254  * @param cls closure argument for @a connect_cb and @a disconnect_cb
255  * @param handlers NULL-terminated array of message handlers for the service,
256  *                 the closure will be set to the value returned by
257  *                 the @a connect_cb for the respective connection
258  * @return NULL on error
259  */
260 struct GNUNET_SERVICE_Handle *
261 GNUNET_SERVICE_starT (const char *service_name,
262                       const struct GNUNET_CONFIGURATION_Handle *cfg,
263                       GNUNET_SERVICE_ConnectHandler connect_cb,
264                       GNUNET_SERVICE_DisconnectHandler disconnect_cb,
265                       void *cls,
266                       const struct GNUNET_MQ_MessageHandler *handlers);
267
268
269 /**
270  * Stops a service that was started with #GNUNET_SERVICE_starT().
271  *
272  * @param srv service to stop
273  */
274 void
275 GNUNET_SERVICE_stoP (struct GNUNET_SERVICE_Handle *srv);
276
277
278 /**
279  * Creates the "main" function for a GNUnet service.  You
280  * should almost always use the #GNUNET_SERVICE_MAIN macro
281  * instead of calling this function directly (except
282  * for ARM, which should call this function directly).
283  *
284  * The function will launch the service with the name @a service_name
285  * using the @a service_options to configure its shutdown
286  * behavior. Once the service is ready, the @a init_cb will be called
287  * for service-specific initialization.  @a init_cb will be given the
288  * service handler which can be used to control the service's
289  * availability.  When clients connect or disconnect, the respective
290  * @a connect_cb or @a disconnect_cb functions will be called. For
291  * messages received from the clients, the respective @a handlers will
292  * be invoked; for the closure of the handlers we use the return value
293  * from the @a connect_cb invocation of the respective client.
294  *
295  * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
296  * message to receive further messages from this client.  If
297  * #GNUNET_SERVICE_client_continue() is not called within a short
298  * time, a warning will be logged. If delays are expected, services
299  * should call #GNUNET_SERVICE_client_disable_continue_warning() to
300  * disable the warning.
301  *
302  * Clients sending invalid messages (based on @a handlers) will be
303  * dropped. Additionally, clients can be dropped at any time using
304  * #GNUNET_SERVICE_client_drop().
305  *
306  * @param argc number of command-line arguments in @a argv
307  * @param argv array of command-line arguments
308  * @param service_name name of the service to run
309  * @param options options controlling shutdown of the service
310  * @param service_init_cb function to call once the service is ready
311  * @param connect_cb function to call whenever a client connects
312  * @param disconnect_cb function to call whenever a client disconnects
313  * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
314  * @param handlers NULL-terminated array of message handlers for the service,
315  *                 the closure will be set to the value returned by
316  *                 the @a connect_cb for the respective connection
317  * @return 0 on success, non-zero on error
318  */
319 int
320 GNUNET_SERVICE_ruN_ (int argc,
321                      char *const *argv,
322                      const char *service_name,
323                      enum GNUNET_SERVICE_Options options,
324                      GNUNET_SERVICE_InitCallback service_init_cb,
325                      GNUNET_SERVICE_ConnectHandler connect_cb,
326                      GNUNET_SERVICE_DisconnectHandler disconnect_cb,
327                      void *cls,
328                      const struct GNUNET_MQ_MessageHandler *handlers);
329
330
331 /**
332  * Creates the "main" function for a GNUnet service.  You
333  * MUST use this macro to define GNUnet services (except
334  * for ARM, which MUST NOT use the macro).  The reason is
335  * the GNUnet-as-a-library project, where we will not define
336  * a main function anywhere but in ARM.
337  *
338  * The macro will launch the service with the name @a service_name
339  * using the @a service_options to configure its shutdown
340  * behavior. Once the service is ready, the @a init_cb will be called
341  * for service-specific initialization.  @a init_cb will be given the
342  * service handler which can be used to control the service's
343  * availability.  When clients connect or disconnect, the respective
344  * @a connect_cb or @a disconnect_cb functions will be called. For
345  * messages received from the clients, the respective @a handlers will
346  * be invoked; for the closure of the handlers we use the return value
347  * from the @a connect_cb invocation of the respective client.
348  *
349  * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
350  * message to receive further messages from this client.  If
351  * #GNUNET_SERVICE_client_continue() is not called within a short
352  * time, a warning will be logged. If delays are expected, services
353  * should call #GNUNET_SERVICE_client_disable_continue_warning() to
354  * disable the warning.
355  *
356  * Clients sending invalid messages (based on @a handlers) will be
357  * dropped. Additionally, clients can be dropped at any time using
358  * #GNUNET_SERVICE_client_drop().
359  *
360  * @param service_name name of the service to run
361  * @param options options controlling shutdown of the service
362  * @param service_init_cb function to call once the service is ready
363  * @param connect_cb function to call whenever a client connects
364  * @param disconnect_cb function to call whenever a client disconnects
365  * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
366  * @param ... array of message handlers for the service, terminated
367  *            by #GNUNET_MQ_handler_end();
368  *                 the closure will be set to the value returned by
369  *                 the @a connect_cb for the respective connection
370  * @return 0 on success, non-zero on error
371  *
372  * Sample invocation:
373  * <code>
374  * GNUNET_SERVICE_MAIN
375  * ("resolver",
376  *  GNUNET_SERVICE_OPTION_NONE,
377  *  &init_cb,
378  *  &connect_cb,
379  *  &disconnect_cb,
380  *  closure_for_cb,
381  *  GNUNET_MQ_hd_var_size (get,
382  *                         GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
383  *                         struct GNUNET_RESOLVER_GetMessage,
384  *                         NULL),
385  *  GNUNET_MQ_handler_end ());
386  * </code>
387  */
388 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,...) \
389   int \
390   main (int argc,\
391         char *const *argv)\
392   { \
393     struct GNUNET_MQ_MessageHandler mh[] = { \
394       __VA_ARGS__ \
395     };                        \
396     return GNUNET_SERVICE_ruN_ (argc, \
397                                 argv, \
398                                 service_name, \
399                                 service_options, \
400                                 init_cb, \
401                                 connect_cb, \
402                                 disconnect_cb, \
403                                 cls, \
404                                 mh); \
405   }
406
407
408 /**
409  * Suspend accepting connections from the listen socket temporarily.
410  * Resume activity using #GNUNET_SERVICE_resume.
411  *
412  * @param sh service to stop accepting connections.
413  */
414 void
415 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
416
417
418 /**
419  * Resume accepting connections from the listen socket.
420  *
421  * @param sh service to resume accepting connections.
422  */
423 void
424 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
425
426
427 /**
428  * Continue receiving further messages from the given client.
429  * Must be called after each message received.
430  *
431  * @param c the client to continue receiving from
432  */
433 void
434 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
435
436
437 /**
438  * Obtain the message queue of @a c.  Convenience function.
439  *
440  * @param c the client to continue receiving from
441  * @return the message queue of @a c
442  */
443 struct GNUNET_MQ_Handle *
444 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
445
446
447 /**
448  * Disable the warning the server issues if a message is not
449  * acknowledged in a timely fashion.  Use this call if a client is
450  * intentionally delayed for a while.  Only applies to the current
451  * message.
452  *
453  * @param c client for which to disable the warning
454  */
455 void
456 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
457
458
459 /**
460  * Ask the server to disconnect from the given client.  This is the
461  * same as returning #GNUNET_SYSERR within the check procedure when
462  * handling a message, wexcept that it allows dropping of a client even
463  * when not handling a message from that client.  The `disconnect_cb`
464  * will be called on @a c even if the application closes the connection
465  * using this function.
466  *
467  * @param c client to disconnect now
468  */
469 void
470 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
471
472
473 /**
474  * Explicitly stops the service.
475  *
476  * @param sh server to shutdown
477  */
478 void
479 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
480
481
482 /**
483  * Set the 'monitor' flag on this client.  Clients which have been
484  * marked as 'monitors' won't prevent the server from shutting down
485  * once #GNUNET_SERVICE_stop_listening() has been invoked.  The idea is
486  * that for "normal" clients we likely want to allow them to process
487  * their requests; however, monitor-clients are likely to 'never'
488  * disconnect during shutdown and thus will not be considered when
489  * determining if the server should continue to exist after
490  * shutdown has been triggered.
491  *
492  * @param c client to mark as a monitor
493  */
494 void
495 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
496
497
498 /**
499  * Set the persist option on this client.  Indicates that the
500  * underlying socket or fd should never really be closed.  Used for
501  * indicating process death.
502  *
503  * @param c client to persist the socket (never to be closed)
504  */
505 void
506 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
507
508
509 #if 0                           /* keep Emacsens' auto-indent happy */
510 {
511 #endif
512 #ifdef __cplusplus
513 }
514 #endif
515
516 /* ifndef GNUNET_SERVICE_LIB_H */
517 #endif
518
519 /** @} */  /* end of group service */
520
521 /* end of gnunet_service_lib.h */