-starting with service_new implementation data structures
[oweals/gnunet.git] / src / util / service_new.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 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  * @file util/service_new.c
23  * @brief functions related to starting services (redesign)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_resolver_service.h"
31
32
33 /**
34  * Handle to a service.
35  */
36 struct GNUNET_SERVICE_Handle
37 {
38   /**
39    * Our configuration.
40    */
41   const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43   /**
44    * Name of our service.
45    */
46   const char *service_name;
47
48   /**
49    * Main service-specific task to run.
50    */
51   GNUNET_SERVICE_InitCallback service_init_cb;
52
53   /**
54    * Function to call when clients connect.
55    */
56   GNUNET_SERVICE_ConnectHandler connect_cb;
57
58   /**
59    * Function to call when clients disconnect / are disconnected.
60    */
61   GNUNET_SERVICE_DisconnectHandler disconnect_cb;
62
63   /**
64    * Closure for @e service_init_cb, @e connect_cb, @e disconnect_cb.
65    */
66   void *cb_cls;
67
68   /**
69    * Message handlers to use for all clients.
70    */
71   struct GNUNET_MQ_MessageHandler *handlers;
72
73   /**
74    * Closure for @e task.
75    */
76   void *task_cls;
77
78   /**
79    * IPv4 addresses that are not allowed to connect.
80    */
81   struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
82
83   /**
84    * IPv6 addresses that are not allowed to connect.
85    */
86   struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
87
88   /**
89    * IPv4 addresses that are allowed to connect (if not
90    * set, all are allowed).
91    */
92   struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
93
94   /**
95    * IPv6 addresses that are allowed to connect (if not
96    * set, all are allowed).
97    */
98   struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
99
100   /**
101    * Do we require a matching UID for UNIX domain socket connections?
102    * #GNUNET_NO means that the UID does not have to match (however,
103    * @e match_gid may still impose other access control checks).
104    */
105   int match_uid;
106
107   /**
108    * Do we require a matching GID for UNIX domain socket connections?
109    * Ignored if @e match_uid is #GNUNET_YES.  Note that this is about
110    * checking that the client's UID is in our group OR that the
111    * client's GID is our GID.  If both "match_gid" and @e match_uid are
112    * #GNUNET_NO, all users on the local system have access.
113    */
114   int match_gid;
115
116   /**
117    * Our options.
118    */
119   enum GNUNET_SERVICE_Options options;
120
121 };
122
123
124 /**
125  * Handle to a client that is connected to a service.
126  */
127 struct GNUNET_SERVICE_Client
128 {
129
130   /**
131    * Server that this client belongs to.
132    */
133   struct GNUNET_SERVER_Handle *server;
134
135   /**
136    * Task that warns about missing calls to
137    * #GNUNET_SERVICE_client_continue().
138    */
139   struct GNUNET_SCHEDULER_Task *warn_task;
140
141   /**
142    * User context value, value returned from
143    * the connect callback.
144    */
145   void *user_context;
146
147   /**
148    * Persist the file handle for this client no matter what happens,
149    * force the OS to close once the process actually dies.  Should only
150    * be used in special cases!
151    */
152   int persist;
153
154   /**
155    * Is this client a 'monitor' client that should not be counted
156    * when deciding on destroying the server during soft shutdown?
157    * (see also #GNUNET_SERVICE_start)
158    */
159   int is_monitor;
160
161   /**
162    * Type of last message processed (for warn_no_receive_done).
163    */
164   uint16_t warn_type;
165 };
166
167
168 /**
169  * Creates the "main" function for a GNUnet service.  You
170  * should almost always use the #GNUNET_SERVICE_MAIN macro
171  * instead of calling this function directly (except
172  * for ARM, which should call this function directly).
173  *
174  * The function will launch the service with the name @a service_name
175  * using the @a service_options to configure its shutdown
176  * behavior. Once the service is ready, the @a init_cb will be called
177  * for service-specific initialization.  @a init_cb will be given the
178  * service handler which can be used to control the service's
179  * availability.  When clients connect or disconnect, the respective
180  * @a connect_cb or @a disconnect_cb functions will be called. For
181  * messages received from the clients, the respective @a handlers will
182  * be invoked; for the closure of the handlers we use the return value
183  * from the @a connect_cb invocation of the respective client.
184  *
185  * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
186  * message to receive further messages from this client.  If
187  * #GNUNET_SERVICE_client_continue() is not called within a short
188  * time, a warning will be logged. If delays are expected, services
189  * should call #GNUNET_SERVICE_client_disable_continue_warning() to
190  * disable the warning.
191  *
192  * Clients sending invalid messages (based on @a handlers) will be
193  * dropped. Additionally, clients can be dropped at any time using
194  * #GNUNET_SERVICE_client_drop().
195  *
196  * @param argc number of command-line arguments in @a argv
197  * @param argv array of command-line arguments
198  * @param service_name name of the service to run
199  * @param options options controlling shutdown of the service
200  * @param service_init_cb function to call once the service is ready
201  * @param connect_cb function to call whenever a client connects
202  * @param disconnect_cb function to call whenever a client disconnects
203  * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
204  * @param handlers NULL-terminated array of message handlers for the service,
205  *                 the closure will be set to the value returned by
206  *                 the @a connect_cb for the respective connection
207  * @return 0 on success, non-zero on error
208  */
209 int
210 GNUNET_SERVICE_ruN_ (int argc,
211                      char *const *argv,
212                      const char *service_name,
213                      enum GNUNET_SERVICE_Options options,
214                      GNUNET_SERVICE_InitCallback service_init_cb,
215                      GNUNET_SERVICE_ConnectHandler connect_cb,
216                      GNUNET_SERVICE_DisconnectHandler disconnect_cb,
217                      void *cls,
218                      const struct GNUNET_MQ_MessageHandler *handlers)
219 {
220   GNUNET_break (0); // not implemented
221   return 1;
222 }
223
224
225 /**
226  * Suspend accepting connections from the listen socket temporarily.
227  * Resume activity using #GNUNET_SERVICE_resume.
228  *
229  * @param sh service to stop accepting connections.
230  */
231 void
232 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
233 {
234   GNUNET_break (0); // not implemented
235 }
236
237
238 /**
239  * Resume accepting connections from the listen socket.
240  *
241  * @param sh service to resume accepting connections.
242  */
243 void
244 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
245 {
246   GNUNET_break (0); // not implemented
247 }
248
249
250 /**
251  * Continue receiving further messages from the given client.
252  * Must be called after each message received.
253  *
254  * @param c the client to continue receiving from
255  */
256 void
257 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
258 {
259   GNUNET_break (0); // not implemented
260 }
261
262
263 /**
264  * Disable the warning the server issues if a message is not
265  * acknowledged in a timely fashion.  Use this call if a client is
266  * intentionally delayed for a while.  Only applies to the current
267  * message.
268  *
269  * @param c client for which to disable the warning
270  */
271 void
272 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
273 {
274   GNUNET_break (NULL != c->warn_task);
275   if (NULL != c->warn_task)
276   {
277     GNUNET_SCHEDULER_cancel (c->warn_task);
278     c->warn_task = NULL;
279   }
280 }
281
282
283 /**
284  * Ask the server to disconnect from the given client.  This is the
285  * same as returning #GNUNET_SYSERR within the check procedure when
286  * handling a message, wexcept that it allows dropping of a client even
287  * when not handling a message from that client.  The `disconnect_cb`
288  * will be called on @a c even if the application closes the connection
289  * using this function.
290  *
291  * @param c client to disconnect now
292  */
293 void
294 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
295 {
296   GNUNET_break (0); // not implemented
297 }
298
299
300 /**
301  * Stop the listen socket and get ready to shutdown the server once
302  * only clients marked using #GNUNET_SERVER_client_mark_monitor are
303  * left.
304  *
305  * @param sh server to stop listening on
306  */
307 void
308 GNUNET_SERVICE_stop_listening (struct GNUNET_SERVICE_Handle *sh)
309 {
310   GNUNET_break (0); // not implemented
311 }
312
313
314 /**
315  * Set the 'monitor' flag on this client.  Clients which have been
316  * marked as 'monitors' won't prevent the server from shutting down
317  * once #GNUNET_SERVICE_stop_listening() has been invoked.  The idea is
318  * that for "normal" clients we likely want to allow them to process
319  * their requests; however, monitor-clients are likely to 'never'
320  * disconnect during shutdown and thus will not be considered when
321  * determining if the server should continue to exist after
322  * shutdown has been triggered.
323  *
324  * @param c client to mark as a monitor
325  */
326 void
327 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
328 {
329   GNUNET_break (0); // not implemented
330 }
331
332
333 /**
334  * Set the persist option on this client.  Indicates that the
335  * underlying socket or fd should never really be closed.  Used for
336  * indicating process death.
337  *
338  * @param c client to persist the socket (never to be closed)
339  */
340 void
341 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
342 {
343   GNUNET_break (0); // not implemented
344 }
345
346
347
348 /* end of service_new.c */