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