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