disable warning if we intentionally throttle a connection
[oweals/gnunet.git] / src / include / gnunet_server_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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
42
43 /**
44  * Largest supported message.
45  */
46 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
47
48 /**
49  * Largest supported message.
50  */
51 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
52
53 /**
54  * @brief handle for a server
55  */
56 struct GNUNET_SERVER_Handle;
57
58
59 /**
60  * @brief opaque handle for a client of the server
61  */
62 struct GNUNET_SERVER_Client;
63
64
65 /**
66  * Functions with this signature are called whenever a message is
67  * received.
68  *
69  * @param cls closure
70  * @param client identification of the client
71  * @param message the actual message
72  */
73 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
74                                                struct GNUNET_SERVER_Client *
75                                                client,
76                                                const struct
77                                                GNUNET_MessageHeader *
78                                                message);
79
80
81
82 /**
83  * Message handler.  Each struct specifies how to handle on particular
84  * type of message received.
85  */
86 struct GNUNET_SERVER_MessageHandler
87 {
88   /**
89    * Function to call for messages of "type".
90    */
91   GNUNET_SERVER_MessageCallback callback;
92
93   /**
94    * Closure argument for "callback".
95    */
96   void *callback_cls;
97
98   /**
99    * Type of the message this handler covers.
100    */
101   uint16_t type;
102
103   /**
104    * Expected size of messages of this type.  Use 0 for
105    * variable-size.  If non-zero, messages of the given
106    * type will be discarded (and the connection closed)
107    * if they do not have the right size.
108    */
109   uint16_t expected_size;
110
111 };
112
113
114 /**
115  * Create a new server.
116  *
117  * @param access function for access control
118  * @param access_cls closure for access
119  * @param lsocks NULL-terminated array of listen sockets
120  * @param idle_timeout after how long should we timeout idle connections?
121  * @param require_found if YES, connections sending messages of unknown type
122  *        will be closed
123  * @return handle for the new server, NULL on error
124  *         (typically, "port" already in use)
125  */
126 struct GNUNET_SERVER_Handle *
127 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
128                                    struct GNUNET_NETWORK_Handle **lsocks,
129                                    struct GNUNET_TIME_Relative
130                                    idle_timeout,
131                                    int require_found);
132
133 /**
134  * Create a new server.
135  *
136  * @param access function for access control
137  * @param access_cls closure for access
138  * @param serverAddr address toes listen on (including port), NULL terminated array
139  * @param socklen lengths of respective serverAddr 
140  * @param idle_timeout after how long should we timeout idle connections?
141  * @param require_found if YES, connections sending messages of unknown type
142  *        will be closed
143  * @return handle for the new server, NULL on error
144  *         (typically, "port" already in use)
145  */
146 struct GNUNET_SERVER_Handle *GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck
147                                                    access, void *access_cls,
148                                                    struct sockaddr *const*serverAddr,
149                                                    const socklen_t *socklen,
150                                                    struct GNUNET_TIME_Relative
151                                                    idle_timeout,
152                                                    int require_found);
153
154
155 /**
156  * Free resources held by this server.
157  *
158  * @param s server to destroy
159  */
160 void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
161
162
163 /**
164  * Add additional handlers to an existing server.
165  *
166  * @param server the server to add handlers to
167  * @param handlers array of message handlers for
168  *        incoming messages; the last entry must
169  *        have "NULL" for the "callback"; multiple
170  *        entries for the same type are allowed,
171  *        they will be called in order of occurence.
172  *        These handlers can be removed later;
173  *        the handlers array must exist until removed
174  *        (or server is destroyed).
175  */
176 void
177 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
178                             const struct GNUNET_SERVER_MessageHandler
179                             *handlers);
180
181
182 /**
183  * Notify us when the server has enough space to transmit
184  * a message of the given size to the given client.
185  *
186  * @param client client to transmit message to
187  * @param size requested amount of buffer space
188  * @param timeout after how long should we give up (and call
189  *        notify with buf NULL and size 0)?
190  * @param callback function to call when space is available
191  * @param callback_cls closure for callback
192  * @return non-NULL if the notify callback was queued; can be used
193  *           to cancel the request using
194  *           GNUNET_CONNECTION_notify_transmit_ready_cancel.
195  *         NULL if we are already going to notify someone else (busy)
196  */
197 struct GNUNET_CONNECTION_TransmitHandle
198   *GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
199                                         size_t size,
200                                         struct GNUNET_TIME_Relative timeout,
201                                         GNUNET_CONNECTION_TransmitReadyNotify
202                                         callback, void *callback_cls);
203
204
205 /**
206  * Set the persistent flag on this client, used to setup client connection
207  * to only be killed when the service it's connected to is actually dead.
208  *
209  * @param client the client to set the persistent flag on
210  */
211 void
212 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
213
214 /**
215  * Resume receiving from this client, we are done processing the
216  * current request.  This function must be called from within each
217  * GNUNET_SERVER_MessageCallback (or its respective continuations).
218  *
219  * @param client client we were processing a message of
220  * @param success GNUNET_OK to keep the connection open and
221  *                          continue to receive
222  *                GNUNET_NO to close the connection (normal behavior)
223  *                GNUNET_SYSERR to close the connection (signal
224  *                          serious error)
225  */
226 void
227 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
228
229 /**
230  * Disable the warning the server issues if a message is not acknowledged
231  * in a timely fashion.  Use this call if a client is intentionally delayed
232  * for a while.  Only applies to the current message.
233  *
234  * @param client client for which to disable the warning
235  */
236 void
237 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client);
238
239
240 /**
241  * Inject a message into the server, pretend it came
242  * from the specified client.  Delivery of the message
243  * will happen instantly (if a handler is installed;
244  * otherwise the call does nothing).
245  *
246  * @param server the server receiving the message
247  * @param sender the "pretended" sender of the message
248  *        can be NULL!
249  * @param message message to transmit
250  * @return GNUNET_OK if the message was OK and the
251  *                   connection can stay open
252  *         GNUNET_SYSERR if the connection to the
253  *         client should be shut down
254  */
255 int
256 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
257                       struct GNUNET_SERVER_Client *sender,
258                       const struct GNUNET_MessageHeader *message);
259
260
261 /**
262  * Add a TCP socket-based connection to the set of handles managed by
263  * this server.  Use this function for outgoing (P2P) connections that
264  * we initiated (and where this server should process incoming
265  * messages).
266  *
267  * @param server the server to use
268  * @param connection the connection to manage (client must
269  *        stop using this connection from now on)
270  * @return the client handle (client should call
271  *         "client_drop" on the return value eventually)
272  */
273 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_socket (struct
274                                                            GNUNET_SERVER_Handle
275                                                            *server,
276                                                            struct
277                                                            GNUNET_CONNECTION_Handle
278                                                            *connection);
279
280
281 /**
282  * Notify the server that the given client handle should
283  * be kept (keeps the connection up if possible, increments
284  * the internal reference counter).
285  *
286  * @param client the client to keep
287  */
288 void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
289
290
291 /**
292  * Notify the server that the given client handle is no
293  * longer required.  Decrements the reference counter.  If
294  * that counter reaches zero an inactive connection maybe
295  * closed.
296  *
297  * @param client the client to drop
298  */
299 void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
300
301
302 /**
303  * Obtain the network address of the other party.
304  *
305  * @param client the client to get the address for
306  * @param addr where to store the address
307  * @param addrlen where to store the length of the address
308  * @return GNUNET_OK on success
309  */
310 int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
311                                       void **addr, size_t * addrlen);
312
313
314 /**
315  * Functions with this signature are called whenever a client
316  * is disconnected on the network level.
317  *
318  * @param cls closure
319  * @param client identification of the client; NULL
320  *        for the last call when the server is destroyed
321  */
322 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
323                                                   struct GNUNET_SERVER_Client
324                                                   * client);
325
326
327 /**
328  * Ask the server to notify us whenever a client disconnects.
329  * This function is called whenever the actual network connection
330  * is closed; the reference count may be zero or larger than zero
331  * at this point.  If the server is destroyed before this 
332  * notification is explicitly cancelled, the 'callback' will
333  * once be called with a 'client' argument of NULL to indicate
334  * that the server itself is now gone (and that the callback
335  * won't be called anymore and also can no longer be cancelled).
336  *
337  * @param server the server manageing the clients
338  * @param callback function to call on disconnect
339  * @param callback_cls closure for callback
340  */
341 void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
342                                       GNUNET_SERVER_DisconnectCallback
343                                       callback, void *callback_cls);
344
345
346 /**
347  * Ask the server to stop notifying us whenever a client disconnects.
348  *
349  * @param server the server manageing the clients
350  * @param callback function to call on disconnect
351  * @param callback_cls closure for callback
352  */
353 void GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
354                                              GNUNET_SERVER_DisconnectCallback
355                                              callback, void *callback_cls);
356
357
358 /**
359  * Ask the server to disconnect from the given client.
360  * This is the same as returning GNUNET_SYSERR from a message
361  * handler, except that it allows dropping of a client even
362  * when not handling a message from that client.
363  *
364  * @param client the client to disconnect from
365  */
366 void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
367
368
369 /**
370  * Configure this server's connections to continue handling client
371  * requests as usual even after we get a shutdown signal.  The change
372  * only applies to clients that connect to the server from the outside
373  * using TCP after this call.  Clients managed previously or those
374  * added using GNUNET_SERVER_connect_socket and
375  * GNUNET_SERVER_connect_callback are not affected by this option.
376  *
377  * @param h server handle
378  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
379  */
380 void
381 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h,
382                                int do_ignore);
383
384
385
386 /**
387  * The tansmit context is the key datastructure for a conveniance API
388  * used for transmission of complex results to the client followed
389  * ONLY by signaling receive_done with success or error
390  */
391 struct GNUNET_SERVER_TransmitContext;
392
393
394 /**
395  * Create a new transmission context for the
396  * given client.
397  *
398  * @param client client to create the context for.
399  * @return NULL on error
400  */
401 struct GNUNET_SERVER_TransmitContext
402   *GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client
403                                           *client);
404
405
406 /**
407  * Append a message to the transmission context.
408  * All messages in the context will be sent by
409  * the transmit_context_run method.
410  *
411  * @param tc context to use
412  * @param data what to append to the result message
413  * @param length length of data
414  * @param type type of the message
415  */
416 void
417 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
418                                             *tc, const void *data, size_t length,
419                                             uint16_t type);
420
421
422 /**
423  * Append a message to the transmission context.
424  * All messages in the context will be sent by
425  * the transmit_context_run method.
426  *
427  * @param tc context to use
428  * @param msg message to append
429  */
430 void
431 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext
432                                                *tc, const struct GNUNET_MessageHeader *msg);
433
434
435 /**
436  * Execute a transmission context.  If there is
437  * an error in the transmission, the receive_done
438  * method will be called with an error code (GNUNET_SYSERR),
439  * otherwise with GNUNET_OK.
440  *
441  * @param tc transmission context to use
442  * @param timeout when to time out and abort the transmission
443  */
444 void
445 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
446                                     struct GNUNET_TIME_Relative timeout);
447
448
449
450 /**
451  * The notification context is the key datastructure for a conveniance
452  * API used for transmission of notifications to the client until the
453  * client disconnects (or the notification context is destroyed, in
454  * which case we disconnect these clients).  Essentially, all
455  * (notification) messages are queued up until the client is able to
456  * read them.
457  */
458 struct GNUNET_SERVER_NotificationContext;
459
460
461 /**
462  * Create a new notification context.
463  *
464  * @param server server for which this function creates the context
465  * @param queue_length maximum number of messages to keep in
466  *        the notification queue; optional messages are dropped
467  *        if the queue gets longer than this number of messages
468  * @return handle to the notification context
469  */
470 struct GNUNET_SERVER_NotificationContext *
471 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
472                                            unsigned int queue_length);
473
474
475 /**
476  * Destroy the context, force disconnect for all clients.
477  *
478  * @param nc context to destroy.
479  */
480 void
481 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
482
483
484 /**
485  * Add a client to the notification context.
486  *
487  * @param nc context to modify
488  * @param client client to add
489  */
490 void
491 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
492                                         struct GNUNET_SERVER_Client *client);
493
494
495 /**
496  * Send a message to a particular client; must have
497  * already been added to the notification context.
498  *
499  * @param nc context to modify
500  * @param client client to transmit to
501  * @param msg message to send
502  * @param can_drop can this message be dropped due to queue length limitations
503  */
504 void
505 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
506                                             struct GNUNET_SERVER_Client *client,
507                                             const struct GNUNET_MessageHeader *msg,
508                                             int can_drop);
509
510
511 /**
512  * Send a message to all clients of this context.
513  *
514  * @param nc context to modify
515  * @param msg message to send
516  * @param can_drop can this message be dropped due to queue length limitations
517  */
518 void
519 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
520                                               const struct GNUNET_MessageHeader *msg,
521                                               int can_drop);
522
523
524
525 /**
526  * Handle to a message stream tokenizer.
527  */
528 struct GNUNET_SERVER_MessageStreamTokenizer;
529
530 /**
531  * Functions with this signature are called whenever a
532  * complete message is received by the tokenizer.
533  *
534  * @param cls closure
535  * @param client identification of the client
536  * @param message the actual message
537  */
538 typedef void (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
539                                                         void *client,
540                                                         const struct
541                                                         GNUNET_MessageHeader *
542                                                         message);
543
544
545 /**
546  * Create a message stream tokenizer.
547  *
548  * @param cb function to call on completed messages
549  * @param cb_cls closure for cb
550  * @return handle to tokenizer
551  */
552 struct GNUNET_SERVER_MessageStreamTokenizer *
553 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
554                           void *cb_cls);
555
556
557 /**
558  * Add incoming data to the receive buffer and call the
559  * callback for all complete messages.
560  *
561  * @param mst tokenizer to use
562  * @param client_identity ID of client for which this is a buffer,
563  *        can be NULL (will be passed back to 'cb')
564  * @param buf input data to add
565  * @param size number of bytes in buf
566  * @param purge should any excess bytes in the buffer be discarded 
567  *       (i.e. for packet-based services like UDP)
568  * @param one_shot only call callback once, keep rest of message in buffer
569  * @return GNUNET_OK if we are done processing (need more data)
570  *         GNUNET_NO if one_shot was set and we have another message ready
571  *         GNUNET_SYSERR if the data stream is corrupt
572  */
573 int
574 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
575                            void *client_identity,
576                            const char *buf,
577                            size_t size,
578                            int purge,
579                            int one_shot);
580
581
582 /**
583  * Destroys a tokenizer.
584  *
585  * @param mst tokenizer to destroy
586  */
587 void
588 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
589
590
591 #if 0                           /* keep Emacsens' auto-indent happy */
592 {
593 #endif
594 #ifdef __cplusplus
595 }
596 #endif
597
598
599 /* ifndef GNUNET_SERVER_LIB_H */
600 #endif
601 /* end of gnunet_server_lib.h */