-more cleanup and input validation fixes
[oweals/gnunet.git] / src / include / gnunet_service_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_service_lib.h
23  * @brief functions related to starting services
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_SERVICE_LIB_H
28 #define GNUNET_SERVICE_LIB_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_configuration_lib.h"
39 #include "gnunet_server_lib.h"
40
41
42 /**
43  * Get the list of addresses that a server for the given service
44  * should bind to.
45  *
46  * @param service_name name of the service
47  * @param cfg configuration (which specifies the addresses)
48  * @param addrs set (call by reference) to an array of pointers to the
49  *              addresses the server should bind to and listen on; the
50  *              array will be NULL-terminated (on success)
51  * @param addr_lens set (call by reference) to an array of the lengths
52  *              of the respective 'struct sockaddr' struct in the 'addrs'
53  *              array (on success)
54  * @return number of addresses found on success,
55  *              GNUNET_SYSERR if the configuration
56  *              did not specify reasonable finding information or
57  *              if it specified a hostname that could not be resolved;
58  *              GNUNET_NO if the number of addresses configured is
59  *              zero (in this case, '*addrs' and '*addr_lens' will be
60  *              set to NULL).
61  */
62 int
63 GNUNET_SERVICE_get_server_addresses (const char *service_name,
64                                      const struct GNUNET_CONFIGURATION_Handle
65                                      *cfg, struct sockaddr ***addrs,
66                                      socklen_t ** addr_lens);
67
68
69 /**
70  * Function called by the service's run
71  * method to run service-specific setup code.
72  *
73  * @param cls closure
74  * @param server the initialized server
75  * @param cfg configuration to use
76  */
77 typedef void (*GNUNET_SERVICE_Main) (void *cls,
78                                      struct GNUNET_SERVER_Handle * server,
79                                      const struct GNUNET_CONFIGURATION_Handle *
80                                      cfg);
81
82
83 /**
84  * Options for the service (bitmask).
85  */
86 enum GNUNET_SERVICE_Options
87 {
88   /**
89    * Use defaults.
90    */
91   GNUNET_SERVICE_OPTION_NONE = 0,
92
93   /**
94    * Do not trigger server shutdown on signals, allow for the user
95    * to terminate the server explicitly when needed.
96    */
97   GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
98
99   /**
100    * Trigger a SOFT server shutdown on signals, allowing active
101    * non-monitor clients to complete their transactions.
102    */
103   GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
104 };
105
106
107 /**
108  * Run a standard GNUnet service startup sequence (initialize loggers
109  * and configuration, parse options).
110  *
111  * @param argc number of command line arguments
112  * @param argv command line arguments
113  * @param service_name our service name
114  * @param options service options
115  * @param task main task of the service
116  * @param task_cls closure for task
117  * @return GNUNET_SYSERR on error, GNUNET_OK
118  *         if we shutdown nicely
119  */
120 int
121 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
122                     enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
123                     void *task_cls);
124
125
126 /**
127  * Opaque handle for a service.
128  */
129 struct GNUNET_SERVICE_Context;
130
131 /**
132  * Run a service startup sequence within an existing
133  * initialized system.
134  *
135  * @param service_name our service name
136  * @param cfg configuration to use
137  * @param options service options
138  * @return NULL on error, service handle
139  */
140 struct GNUNET_SERVICE_Context *
141 GNUNET_SERVICE_start (const char *service_name,
142                       const struct GNUNET_CONFIGURATION_Handle *cfg,
143                       enum GNUNET_SERVICE_Options options);
144
145
146 /**
147  * Obtain the server used by a service.  Note that the server must NOT
148  * be destroyed by the caller.
149  *
150  * @param ctx the service context returned from the start function
151  * @return handle to the server for this service, NULL if there is none
152  */
153 struct GNUNET_SERVER_Handle *
154 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx);
155
156
157 /**
158  * Stop a service that was started with "GNUNET_SERVICE_start".
159  *
160  * @param sctx the service context returned from the start function
161  */
162 void
163 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
164
165
166 #if 0                           /* keep Emacsens' auto-indent happy */
167 {
168 #endif
169 #ifdef __cplusplus
170 }
171 #endif
172
173 /* ifndef GNUNET_SERVICE_LIB_H */
174 #endif
175 /* end of gnunet_service_lib.h */