add shutdown-ignore option to server
[oweals/gnunet.git] / src / include / gnunet_server_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_server_lib.h
23  * @brief library for building GNUnet network servers
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_SERVER_LIB_H
29 #define GNUNET_SERVER_LIB_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40 #include "gnunet_connection_lib.h"
41 #include "gnunet_scheduler_lib.h"
42
43
44 /**
45  * Largest supported message.
46  */
47 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
48
49
50 /**
51  * @brief handle for a server
52  */
53 struct GNUNET_SERVER_Handle;
54
55
56 /**
57  * @brief opaque handle for a client of the server
58  */
59 struct GNUNET_SERVER_Client;
60
61
62 /**
63  * Functions with this signature are called whenever a message is
64  * received.
65  *
66  * @param cls closure
67  * @param client identification of the client
68  * @param message the actual message
69  */
70 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
71                                                struct GNUNET_SERVER_Client *
72                                                client,
73                                                const struct
74                                                GNUNET_MessageHeader *
75                                                message);
76
77
78
79 /**
80  * Message handler.  Each struct specifies how to handle on particular
81  * type of message received.
82  */
83 struct GNUNET_SERVER_MessageHandler
84 {
85   /**
86    * Function to call for messages of "type".
87    */
88   GNUNET_SERVER_MessageCallback callback;
89
90   /**
91    * Closure argument for "callback".
92    */
93   void *callback_cls;
94
95   /**
96    * Type of the message this handler covers.
97    */
98   uint16_t type;
99
100   /**
101    * Expected size of messages of this type.  Use 0 for
102    * variable-size.  If non-zero, messages of the given
103    * type will be discarded (and the connection closed)
104    * if they do not have the right size.
105    */
106   uint16_t expected_size;
107
108 };
109
110
111 /**
112  * Create a new server.
113  *
114  * @param sched scheduler to use
115  * @param access function for access control
116  * @param access_cls closure for access
117  * @param serverAddr address to listen on (including port), use NULL
118  *        for internal server (no listening)
119  * @param socklen length of serverAddr
120  * @param maxbuf maximum write buffer size for accepted sockets
121  * @param idle_timeout after how long should we timeout idle connections?
122  * @param require_found if YES, connections sending messages of unknown type
123  *        will be closed
124  * @return handle for the new server, NULL on error
125  *         (typically, "port" already in use)
126  */
127 struct GNUNET_SERVER_Handle *GNUNET_SERVER_create (struct
128                                                    GNUNET_SCHEDULER_Handle
129                                                    *sched,
130                                                    GNUNET_CONNECTION_AccessCheck
131                                                    access, void *access_cls,
132                                                    const struct sockaddr
133                                                    *serverAddr,
134                                                    socklen_t socklen,
135                                                    size_t maxbuf,
136                                                    struct GNUNET_TIME_Relative
137                                                    idle_timeout,
138                                                    int require_found);
139
140
141 /**
142  * Free resources held by this server.
143  */
144 void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
145
146
147 /**
148  * Add additional handlers to an existing server.
149  *
150  * @param server the server to add handlers to
151  * @param handlers array of message handlers for
152  *        incoming messages; the last entry must
153  *        have "NULL" for the "callback"; multiple
154  *        entries for the same type are allowed,
155  *        they will be called in order of occurence.
156  *        These handlers can be removed later;
157  *        the handlers array must exist until removed
158  *        (or server is destroyed).
159  */
160 void
161 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
162                             const struct GNUNET_SERVER_MessageHandler
163                             *handlers);
164
165
166 /**
167  * Notify us when the server has enough space to transmit
168  * a message of the given size to the given client.
169  *
170  * @param client client to transmit message to
171  * @param size requested amount of buffer space
172  * @param timeout after how long should we give up (and call
173  *        notify with buf NULL and size 0)?
174  * @param callback function to call when space is available
175  * @param callback_cls closure for callback
176  * @return non-NULL if the notify callback was queued; can be used
177  *           to cancel the request using
178  *           GNUNET_CONNECTION_notify_transmit_ready_cancel.
179  *         NULL if we are already going to notify someone else (busy)
180  */
181 struct GNUNET_CONNECTION_TransmitHandle
182   *GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
183                                         size_t size,
184                                         struct GNUNET_TIME_Relative timeout,
185                                         GNUNET_CONNECTION_TransmitReadyNotify
186                                         callback, void *callback_cls);
187
188
189 /**
190  * Resume receiving from this client, we are done processing the
191  * current request.  This function must be called from within each
192  * GNUNET_SERVER_MessageCallback (or its respective continuations).
193  *
194  * @param client client we were processing a message of
195  * @param success GNUNET_OK to keep the connection open and
196  *                          continue to receive
197  *                GNUNET_SYSERR to close the connection (signal
198  *                          serious error)
199  */
200 void
201 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
202
203
204 /**
205  * Inject a message into the server, pretend it came
206  * from the specified client.  Delivery of the message
207  * will happen instantly (if a handler is installed;
208  * otherwise the call does nothing).
209  *
210  * @param server the server receiving the message
211  * @param sender the "pretended" sender of the message
212  *        can be NULL!
213  * @param message message to transmit
214  * @return GNUNET_OK if the message was OK and the
215  *                   connection can stay open
216  *         GNUNET_SYSERR if the connection to the
217  *         client should be shut down
218  */
219 int
220 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
221                       struct GNUNET_SERVER_Client *sender,
222                       const struct GNUNET_MessageHeader *message);
223
224
225 /**
226  * Add a TCP socket-based connection to the set of handles managed by
227  * this server.  Use this function for outgoing (P2P) connections that
228  * we initiated (and where this server should process incoming
229  * messages).
230  *
231  * @param server the server to use
232  * @param connection the connection to manage (client must
233  *        stop using this connection from now on)
234  * @return the client handle (client should call
235  *         "client_drop" on the return value eventually)
236  */
237 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_socket (struct
238                                                            GNUNET_SERVER_Handle
239                                                            *server,
240                                                            struct
241                                                            GNUNET_CONNECTION_Handle
242                                                            *connection);
243
244
245 /**
246  * Receive data from the given connection.  This function should call
247  * "receiver" asynchronously using the scheduler.  It must return
248  * "immediately".
249  *
250  * @param cls closure
251  * @param sched scheduler to use
252  * @param max maximum number of bytes to read
253  * @param timeout maximum amount of time to wait (use -1 for "forever")
254  * @param receiver function to call with received data
255  * @param receiver_cls closure for receiver
256  */
257 typedef void
258   (*GNUNET_SERVER_ReceiveCallback) (void *cls,
259                                     size_t max,
260                                     struct GNUNET_TIME_Relative timeout,
261                                     GNUNET_CONNECTION_Receiver
262                                     receiver, void *receiver_cls);
263
264
265 /**
266  * Cancel receive request.
267  *
268  * @param cls closure
269  */
270 typedef void (*GNUNET_SERVER_ReceiveCancelCallback) (void *cls);
271
272
273 /**
274  * Notify us when the connection is ready to transmit size bytes.
275  *
276  * @param cls closure
277  * @param size number of bytes to be ready for sending
278  * @param timeout after how long should we give up (and call
279  *        notify with buf NULL and size 0)?
280  * @param notify function to call
281  * @param notify_cls closure for notify
282  * @return a handle that can be used to cancel
283  *         the transmission request or NULL if
284  *         queueing a transmission request failed
285  */
286 typedef void *(*GNUNET_SERVER_TransmitReadyCallback) (void *cls,
287                                                       size_t size,
288                                                       struct
289                                                       GNUNET_TIME_Relative
290                                                       timeout,
291                                                       GNUNET_CONNECTION_TransmitReadyNotify
292                                                       notify,
293                                                       void *notify_cls);
294
295
296 /**
297  * Cancel an earlier transmit notification request.
298  *
299  * @param cls closure
300  * @param ctx handle that was returned by the TransmitReadyCallback
301  */
302 typedef void (*GNUNET_SERVER_TransmitReadyCancelCallback) (void *cls,
303                                                            void *ctx);
304
305
306 /**
307  * Check if connection is still valid (no fatal errors have happened so far).
308  *
309  * @param cls closure
310  * @return GNUNET_YES if valid, GNUNET_NO otherwise
311  */
312 typedef int (*GNUNET_SERVER_CheckCallback) (void *cls);
313
314
315 /**
316  * Destroy this connection (free resources).
317  *
318  * @param cls closure
319  */
320 typedef void (*GNUNET_SERVER_DestroyCallback) (void *cls);
321
322
323 /**
324  * Add an arbitrary connection to the set of handles managed by this
325  * server.  This can be used if a sending and receiving does not
326  * really go over the network (internal transmission) or for servers
327  * using UDP.
328  *
329  * @param server the server to use
330  * @param chandle opaque handle for the connection
331  * @param creceive receive function for the connection
332  * @param ccancel cancel receive function for the connection
333  * @param cnotify transmit notification function for the connection
334  * @param cnotify_cancel transmit notification cancellation function for the connection
335  * @param ccheck function to test if the connection is still up
336  * @param cdestroy function to close and free the connection
337  * @return the client handle (client should call
338  *         "client_drop" on the return value eventually)
339  */
340 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_callback (struct
341                                                              GNUNET_SERVER_Handle
342                                                              *server,
343                                                              void *chandle,
344                                                              GNUNET_SERVER_ReceiveCallback
345                                                              creceive,
346                                                              GNUNET_SERVER_ReceiveCancelCallback
347                                                              ccancel,
348                                                              GNUNET_SERVER_TransmitReadyCallback
349                                                              cnotify,
350                                                              GNUNET_SERVER_TransmitReadyCancelCallback
351                                                              cnotify_cancel,
352                                                              GNUNET_SERVER_CheckCallback
353                                                              ccheck,
354                                                              GNUNET_SERVER_DestroyCallback
355                                                              cdestroy);
356
357
358 /**
359  * Notify the server that the given client handle should
360  * be kept (keeps the connection up if possible, increments
361  * the internal reference counter).
362  *
363  * @param client the client to keep
364  */
365 void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
366
367
368 /**
369  * Notify the server that the given client handle is no
370  * longer required.  Decrements the reference counter.  If
371  * that counter reaches zero an inactive connection maybe
372  * closed.
373  *
374  * @param client the client to drop
375  */
376 void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
377
378
379 /**
380  * Obtain the network address of the other party.
381  *
382  * @param client the client to get the address for
383  * @param addr where to store the address
384  * @param addrlen where to store the length of the address
385  * @return GNUNET_OK on success
386  */
387 int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
388                                       void **addr, size_t * addrlen);
389
390
391 /**
392  * Functions with this signature are called whenever a client
393  * is disconnected on the network level.
394  *
395  * @param cls closure
396  * @param client identification of the client
397  */
398 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
399                                                   struct GNUNET_SERVER_Client
400                                                   * client);
401
402
403 /**
404  * Ask the server to notify us whenever a client disconnects.
405  * This function is called whenever the actual network connection
406  * is closed; the reference count may be zero or larger than zero
407  * at this point.
408  *
409  * @param server the server manageing the clients
410  * @param callback function to call on disconnect
411  * @param callback_cls closure for callback
412  */
413 void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
414                                       GNUNET_SERVER_DisconnectCallback
415                                       callback, void *callback_cls);
416
417
418 /**
419  * Ask the server to disconnect from the given client.
420  * This is the same as returning GNUNET_SYSERR from a message
421  * handler, except that it allows dropping of a client even
422  * when not handling a message from that client.
423  *
424  * @param client the client to disconnect from
425  */
426 void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
427
428
429 /**
430  * Configure this server's connections to continue handling client
431  * requests as usual even after we get a shutdown signal.  The change
432  * only applies to clients that connect to the server from the outside
433  * using TCP after this call.  Clients managed previously or those
434  * added using GNUNET_SERVER_connect_socket and
435  * GNUNET_SERVER_connect_callback are not affected by this option.
436  *
437  * @param h server handle
438  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
439  */
440 void
441 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h,
442                                int do_ignore);
443
444
445 /**
446  * The tansmit context is the key datastructure for a conveniance API
447  * used for transmission of complex results to the client followed
448  * ONLY by signaling receive_done with success or error
449  */
450 struct GNUNET_SERVER_TransmitContext;
451
452
453 /**
454  * Create a new transmission context for the
455  * given client.
456  *
457  * @param client client to create the context for.
458  * @return NULL on error
459  */
460 struct GNUNET_SERVER_TransmitContext
461   *GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client
462                                           *client);
463
464
465 /**
466  * Append a message to the transmission context.
467  * All messages in the context will be sent by
468  * the transmit_context_run method.
469  *
470  * @param tc context to use
471  * @param data what to append to the result message
472  * @param length length of data
473  * @param type type of the message
474  */
475 void
476 GNUNET_SERVER_transmit_context_append (struct GNUNET_SERVER_TransmitContext
477                                        *tc, const void *data, size_t length,
478                                        uint16_t type);
479
480 /**
481  * Execute a transmission context.  If there is
482  * an error in the transmission, the receive_done
483  * method will be called with an error code (GNUNET_SYSERR),
484  * otherwise with GNUNET_OK.
485  *
486  * @param tc transmission context to use
487  * @param timeout when to time out and abort the transmission
488  */
489 void
490 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
491                                     struct GNUNET_TIME_Relative timeout);
492
493
494
495 #if 0                           /* keep Emacsens' auto-indent happy */
496 {
497 #endif
498 #ifdef __cplusplus
499 }
500 #endif
501
502
503 /* ifndef GNUNET_SERVER_LIB_H */
504 #endif
505 /* end of gnunet_server_lib.h */