- wip
[oweals/gnunet.git] / src / include / gnunet_server_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2013 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 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., 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  * @author Christian Grothoff
25  * @defgroup server functions for a server that communicates with clients
26  * @{
27  */
28
29 #ifndef GNUNET_SERVER_LIB_H
30 #define GNUNET_SERVER_LIB_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "gnunet_common.h"
41 #include "gnunet_connection_lib.h"
42
43
44 /**
45  * Largest supported message (to be precise, one byte more
46  * than the largest possible message, so tests involving
47  * this value should check for messages being smaller than
48  * this value).
49  */
50 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
51
52 /**
53  * Smallest supported message.
54  */
55 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
56
57 /**
58  * @brief handle for a server
59  */
60 struct GNUNET_SERVER_Handle;
61
62 /**
63  * @brief opaque handle for a client of the server
64  */
65 struct GNUNET_SERVER_Client;
66
67 /**
68  * @brief opaque handle server returns for aborting transmission to a client.
69  */
70 struct GNUNET_SERVER_TransmitHandle;
71
72
73 /**
74  * Functions with this signature are called whenever a message is
75  * received.
76  *
77  * @param cls closure
78  * @param client identification of the client
79  * @param message the actual message
80  */
81 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
82                                                struct GNUNET_SERVER_Client *client,
83                                                const struct GNUNET_MessageHeader
84                                                *message);
85
86
87 /**
88  * Message handler.  Each struct specifies how to handle on particular
89  * type of message received.
90  */
91 struct GNUNET_SERVER_MessageHandler
92 {
93   /**
94    * Function to call for messages of "type".
95    */
96   GNUNET_SERVER_MessageCallback callback;
97
98   /**
99    * Closure argument for "callback".
100    */
101   void *callback_cls;
102
103   /**
104    * Type of the message this handler covers.
105    */
106   uint16_t type;
107
108   /**
109    * Expected size of messages of this type.  Use 0 for
110    * variable-size.  If non-zero, messages of the given
111    * type will be discarded (and the connection closed)
112    * if they do not have the right size.
113    */
114   uint16_t expected_size;
115
116 };
117
118
119 /**
120  * Create a new server.
121  *
122  * @param access function for access control
123  * @param access_cls closure for @a access
124  * @param lsocks NULL-terminated array of listen sockets
125  * @param idle_timeout after how long should we timeout idle connections?
126  * @param require_found if #GNUNET_YES, connections sending messages of unknown type
127  *        will be closed
128  * @return handle for the new server, NULL on error
129  *         (typically, "port" already in use)
130  */
131 struct GNUNET_SERVER_Handle *
132 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
133                                    void *access_cls,
134                                    struct GNUNET_NETWORK_Handle **lsocks,
135                                    struct GNUNET_TIME_Relative idle_timeout,
136                                    int require_found);
137
138 /**
139  * Create a new server.
140  *
141  * @param access function for access control
142  * @param access_cls closure for @a access
143  * @param server_addr address toes listen on (including port), NULL terminated array
144  * @param socklen lengths of respective @a server_addr
145  * @param idle_timeout after how long should we timeout idle connections?
146  * @param require_found if #GNUNET_YES, connections sending messages of unknown type
147  *        will be closed
148  * @return handle for the new server, NULL on error
149  *         (typically, "port" already in use)
150  */
151 struct GNUNET_SERVER_Handle *
152 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
153                       struct sockaddr *const *server_addr,
154                       const socklen_t * socklen,
155                       struct GNUNET_TIME_Relative idle_timeout,
156                       int require_found);
157
158
159 /**
160  * Suspend accepting connections from the listen socket temporarily.
161  * Resume activity using #GNUNET_SERVER_resume.
162  *
163  * @param server server to stop accepting connections.
164  */
165 void
166 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
167
168
169 /**
170  * Resume accepting connections from the listen socket.
171  *
172  * @param server server to resume accepting connections.
173  */
174 void
175 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
176
177
178 /**
179  * Stop the listen socket and get ready to shutdown the server once
180  * only clients marked using #GNUNET_SERVER_client_mark_monitor are
181  * left.
182  *
183  * @param server server to stop listening on
184  */
185 void
186 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
187
188
189 /**
190  * Free resources held by this server.
191  *
192  * @param server server to destroy
193  */
194 void
195 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
196
197
198 /**
199  * Add additional handlers to an existing server.
200  *
201  * @param server the server to add handlers to
202  * @param handlers array of message handlers for
203  *        incoming messages; the last entry must
204  *        have "NULL" for the "callback"; multiple
205  *        entries for the same type are allowed,
206  *        they will be called in order of occurence.
207  *        These handlers can be removed later;
208  *        the handlers array must exist until removed
209  *        (or server is destroyed).
210  */
211 void
212 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
213                             const struct GNUNET_SERVER_MessageHandler
214                             *handlers);
215
216
217 /**
218  * Notify us when the server has enough space to transmit
219  * a message of the given size to the given client.
220  *
221  * @param client client to transmit message to
222  * @param size requested amount of buffer space
223  * @param timeout after how long should we give up (and call
224  *        notify with buf NULL and size 0)?
225  * @param callback function to call when space is available
226  * @param callback_cls closure for @a callback
227  * @return non-NULL if the notify callback was queued; can be used
228  *           to cancel the request using
229  *           #GNUNET_SERVER_notify_transmit_ready_cancel.
230  *         NULL if we are already going to notify someone else (busy)
231  */
232 struct GNUNET_SERVER_TransmitHandle *
233 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
234                                      size_t size,
235                                      struct GNUNET_TIME_Relative timeout,
236                                      GNUNET_CONNECTION_TransmitReadyNotify
237                                      callback, void *callback_cls);
238
239
240 /**
241  * Abort transmission request.
242  *
243  * @param th request to abort
244  */
245 void
246 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
247
248
249 /**
250  * Set the 'monitor' flag on this client.  Clients which have been
251  * marked as 'monitors' won't prevent the server from shutting down
252  * once #GNUNET_SERVER_stop_listening has been invoked.  The idea is
253  * that for "normal" clients we likely want to allow them to process
254  * their requests; however, monitor-clients are likely to 'never'
255  * disconnect during shutdown and thus will not be considered when
256  * determining if the server should continue to exist after
257  * #GNUNET_SERVER_destroy has been called.
258  *
259  * @param client the client to set the 'monitor' flag on
260  */
261 void
262 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
263
264
265 /**
266  * Set the persistent flag on this client, used to setup client
267  * connection to only be killed when the process of the service it's
268  * connected to is actually dead.  This API is used during shutdown
269  * signalling within ARM, and it is not expected that typical users
270  * of the API would need this function.
271  *
272  * @param client the client to set the persistent flag on
273  */
274 void
275 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
276
277
278 /**
279  * Resume receiving from this client, we are done processing the
280  * current request.  This function must be called from within each
281  * #GNUNET_SERVER_MessageCallback (or its respective continuations).
282  *
283  * @param client client we were processing a message of
284  * @param success #GNUNET_OK to keep the connection open and
285  *                          continue to receive
286  *                #GNUNET_NO to close the connection (normal behavior)
287  *                #GNUNET_SYSERR to close the connection (signal
288  *                          serious error)
289  */
290 void
291 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
292                             int success);
293
294
295 /**
296  * Change the timeout for a particular client.  Decreasing the timeout
297  * may not go into effect immediately (only after the previous timeout
298  * times out or activity happens on the socket).
299  *
300  * @param client the client to update
301  * @param timeout new timeout for activities on the socket
302  */
303 void
304 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
305                                   struct GNUNET_TIME_Relative timeout);
306
307
308 /**
309  * Return user context associated with the given client.
310  * Note: you should probably use the macro (call without the underscore).
311  *
312  * @param client client to query
313  * @param size number of bytes in user context struct (for verification only)
314  * @return pointer to user context
315  */
316 void *
317 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
318                                         size_t size);
319
320
321 /**
322  * Set user context to be associated with the given client.
323  * Note: you should probably use the macro (call without the underscore).
324  *
325  * @param client client to query
326  * @param ptr pointer to user context
327  * @param size number of bytes in user context struct (for verification only)
328  */
329 void
330 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
331                                         void *ptr,
332                                         size_t size);
333
334
335 /**
336  * Return user context associated with the given client.
337  *
338  * @param client client to query
339  * @param type expected return type (i.e. 'struct Foo')
340  * @return pointer to user context of type 'type *'.
341  */
342 #define GNUNET_SERVER_client_get_user_context(client,type)              \
343   (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
344
345 /**
346  * Set user context to be associated with the given client.
347  *
348  * @param client client to query
349  * @param value pointer to user context
350  */
351 #define GNUNET_SERVER_client_set_user_context(client,value)             \
352   GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
353
354
355 /**
356  * Disable the warning the server issues if a message is not acknowledged
357  * in a timely fashion.  Use this call if a client is intentionally delayed
358  * for a while.  Only applies to the current message.
359  *
360  * @param client client for which to disable the warning
361  */
362 void
363 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
364                                             *client);
365
366
367 /**
368  * Inject a message into the server, pretend it came
369  * from the specified client.  Delivery of the message
370  * will happen instantly (if a handler is installed;
371  * otherwise the call does nothing).
372  *
373  * @param server the server receiving the message
374  * @param sender the "pretended" sender of the message
375  *        can be NULL!
376  * @param message message to transmit
377  * @return #GNUNET_OK if the message was OK and the
378  *                   connection can stay open
379  *         #GNUNET_SYSERR if the connection to the
380  *         client should be shut down
381  */
382 int
383 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
384                       struct GNUNET_SERVER_Client *sender,
385                       const struct GNUNET_MessageHeader *message);
386
387
388 /**
389  * Add a TCP socket-based connection to the set of handles managed by
390  * this server.  Use this function for outgoing (P2P) connections that
391  * we initiated (and where this server should process incoming
392  * messages).
393  *
394  * @param server the server to use
395  * @param connection the connection to manage (client must
396  *        stop using this connection from now on)
397  * @return the client handle (client should call
398  *         #GNUNET_SERVER_client_drop on the return value eventually)
399  */
400 struct GNUNET_SERVER_Client *
401 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
402                               struct GNUNET_CONNECTION_Handle *connection);
403
404
405 /**
406  * Notify the server that the given client handle should
407  * be kept (keeps the connection up if possible, increments
408  * the internal reference counter).
409  *
410  * @param client the client to keep
411  */
412 void
413 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
414
415
416 /**
417  * Notify the server that the given client handle is no
418  * longer required.  Decrements the reference counter.  If
419  * that counter reaches zero an inactive connection maybe
420  * closed.
421  *
422  * @param client the client to drop
423  */
424 void
425 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
426
427
428 /**
429  * Obtain the network address of the other party.
430  *
431  * @param client the client to get the address for
432  * @param addr where to store the address
433  * @param addrlen where to store the length of @a addr
434  * @return #GNUNET_OK on success
435  */
436 int
437 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
438                                   void **addr, size_t *addrlen);
439
440
441 /**
442  * Functions with this signature are called whenever a client
443  * is disconnected on the network level.
444  *
445  * @param cls closure
446  * @param client identification of the client; NULL
447  *        for the last call when the server is destroyed
448  */
449 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
450                                                   struct GNUNET_SERVER_Client *client);
451
452
453 /**
454  * Functions with this signature are called whenever a client
455  * is connected on the network level.
456  *
457  * @param cls closure
458  * @param client identification of the client
459  */
460 typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
461                                                struct GNUNET_SERVER_Client *client);
462
463
464 /**
465  * Ask the server to notify us whenever a client disconnects.
466  * This function is called whenever the actual network connection
467  * is closed; the reference count may be zero or larger than zero
468  * at this point.  If the server is destroyed before this
469  * notification is explicitly cancelled, the 'callback' will
470  * once be called with a 'client' argument of NULL to indicate
471  * that the server itself is now gone (and that the callback
472  * won't be called anymore and also can no longer be cancelled).
473  *
474  * @param server the server manageing the clients
475  * @param callback function to call on disconnect
476  * @param callback_cls closure for @a callback
477  */
478 void
479 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
480                                  GNUNET_SERVER_DisconnectCallback callback,
481                                  void *callback_cls);
482
483
484 /**
485  * Ask the server to notify us whenever a client connects.
486  * This function is called whenever the actual network connection
487  * is opened. If the server is destroyed before this
488  * notification is explicitly cancelled, the @a callback will
489  * once be called with a 'client' argument of NULL to indicate
490  * that the server itself is now gone (and that the callback
491  * won't be called anymore and also can no longer be cancelled).
492  *
493  * @param server the server manageing the clients
494  * @param callback function to call on sconnect
495  * @param callback_cls closure for @a callback
496  */
497 void
498 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
499                               GNUNET_SERVER_ConnectCallback callback,
500                               void *callback_cls);
501
502
503 /**
504  * Ask the server to stop notifying us whenever a client disconnects.
505  * Arguments must match exactly those given to
506  * #GNUNET_SERVER_disconnect_notify.  It is not necessary to call this
507  * function during shutdown of the server; in fact, most applications
508  * will never use this function.
509  *
510  * @param server the server manageing the clients
511  * @param callback function to call on disconnect
512  * @param callback_cls closure for @a callback
513  */
514 void
515 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
516                                         GNUNET_SERVER_DisconnectCallback
517                                         callback, void *callback_cls);
518
519
520 /**
521  * Ask the server to stop notifying us whenever a client connects.
522  * Arguments must match exactly those given to
523  * #GNUNET_SERVER_connect_notify.  It is not necessary to call this
524  * function during shutdown of the server; in fact, most applications
525  * will never use this function.
526  *
527  * @param server the server manageing the clients
528  * @param callback function to call on connect
529  * @param callback_cls closure for @a callback
530  */
531 void
532 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
533                                      GNUNET_SERVER_ConnectCallback callback,
534                                      void *callback_cls);
535
536
537 /**
538  * Ask the server to disconnect from the given client.  This is the
539  * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
540  * except that it allows dropping of a client even when not handling a
541  * message from that client.
542  *
543  * @param client the client to disconnect from
544  */
545 void
546 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
547
548
549 /**
550  * Disable the "CORK" feature for communication with the given client,
551  * forcing the OS to immediately flush the buffer on transmission
552  * instead of potentially buffering multiple messages.
553  *
554  * @param client handle to the client
555  * @return #GNUNET_OK on success
556  */
557 int
558 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
559
560
561 /**
562  * The tansmit context is the key datastructure for a conveniance API
563  * used for transmission of complex results to the client followed
564  * ONLY by signaling receive_done with success or error
565  */
566 struct GNUNET_SERVER_TransmitContext;
567
568
569 /**
570  * Create a new transmission context for the given client.
571  *
572  * @param client client to create the context for.
573  * @return NULL on error
574  */
575 struct GNUNET_SERVER_TransmitContext *
576 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
577
578
579 /**
580  * Append a message to the transmission context.
581  * All messages in the context will be sent by
582  * the #GNUNET_SERVER_transmit_context_run method.
583  *
584  * @param tc context to use
585  * @param data what to append to the result message
586  * @param length length of @a data
587  * @param type type of the message
588  */
589 void
590 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
591                                             *tc, const void *data,
592                                             size_t length, uint16_t type);
593
594
595 /**
596  * Append a message to the transmission context.
597  * All messages in the context will be sent by
598  * the transmit_context_run method.
599  *
600  * @param tc context to use
601  * @param msg message to append
602  */
603 void
604 GNUNET_SERVER_transmit_context_append_message (struct
605                                                GNUNET_SERVER_TransmitContext
606                                                *tc,
607                                                const struct GNUNET_MessageHeader
608                                                *msg);
609
610
611 /**
612  * Execute a transmission context.  If there is an error in the
613  * transmission, the receive_done method will be called with an error
614  * code (#GNUNET_SYSERR), otherwise with #GNUNET_OK.
615  *
616  * @param tc transmission context to use
617  * @param timeout when to time out and abort the transmission
618  */
619 void
620 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
621                                     struct GNUNET_TIME_Relative timeout);
622
623
624 /**
625  * Destroy a transmission context.  This function must not be called
626  * after #GNUNET_SERVER_transmit_context_run.
627  *
628  * @param tc transmission context to destroy
629  * @param success code to give to #GNUNET_SERVER_receive_done  for
630  *        the client: #GNUNET_OK to keep the connection open and
631  *                          continue to receive
632  *                #GNUNET_NO to close the connection (normal behavior)
633  *                #GNUNET_SYSERR to close the connection (signal
634  *                          serious error)
635  */
636 void
637 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
638                                         *tc, int success);
639
640
641 /**
642  * The notification context is the key datastructure for a conveniance
643  * API used for transmission of notifications to the client until the
644  * client disconnects or is disconnected (or the notification context
645  * is destroyed, in which case we disconnect these clients).
646  * Essentially, all (notification) messages are queued up until the
647  * client is able to read them.
648  */
649 struct GNUNET_SERVER_NotificationContext;
650
651
652 /**
653  * Create a new notification context.
654  *
655  * @param server server for which this function creates the context
656  * @param queue_length maximum number of messages to keep in
657  *        the notification queue; optional messages are dropped
658  *        if the queue gets longer than this number of messages
659  * @return handle to the notification context
660  */
661 struct GNUNET_SERVER_NotificationContext *
662 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
663                                            unsigned int queue_length);
664
665
666 /**
667  * Destroy the context, force disconnect for all clients.
668  *
669  * @param nc context to destroy.
670  */
671 void
672 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
673
674
675 /**
676  * Add a client to the notification context.
677  *
678  * @param nc context to modify
679  * @param client client to add
680  */
681 void
682 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
683                                         struct GNUNET_SERVER_Client *client);
684
685
686 /**
687  * Send a message to a particular client; must have
688  * already been added to the notification context.
689  *
690  * @param nc context to modify
691  * @param client client to transmit to
692  * @param msg message to send
693  * @param can_drop can this message be dropped due to queue length limitations
694  */
695 void
696 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
697                                             struct GNUNET_SERVER_Client *client,
698                                             const struct GNUNET_MessageHeader *msg,
699                                             int can_drop);
700
701
702 /**
703  * Send a message to all clients of this context.
704  *
705  * @param nc context to modify
706  * @param msg message to send
707  * @param can_drop can this message be dropped due to queue length limitations
708  */
709 void
710 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
711                                               const struct GNUNET_MessageHeader *msg,
712                                               int can_drop);
713
714
715 /**
716  * Handle to a message stream tokenizer.
717  */
718 struct GNUNET_SERVER_MessageStreamTokenizer;
719
720
721 /**
722  * Functions with this signature are called whenever a
723  * complete message is received by the tokenizer.
724  *
725  * Do not call #GNUNET_SERVER_mst_destroy from within
726  * the scope of this callback.
727  *
728  * @param cls closure
729  * @param client identification of the client
730  * @param message the actual message
731  * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
732  */
733 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
734                                                         const struct
735                                                         GNUNET_MessageHeader *
736                                                         message);
737
738
739 /**
740  * Create a message stream tokenizer.
741  *
742  * @param cb function to call on completed messages
743  * @param cb_cls closure for @a cb
744  * @return handle to tokenizer
745  */
746 struct GNUNET_SERVER_MessageStreamTokenizer *
747 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
748                           void *cb_cls);
749
750
751 /**
752  * Add incoming data to the receive buffer and call the
753  * callback for all complete messages.
754  *
755  * @param mst tokenizer to use
756  * @param client_identity ID of client for which this is a buffer,
757  *        can be NULL (will be passed back to 'cb')
758  * @param buf input data to add
759  * @param size number of bytes in @a buf
760  * @param purge should any excess bytes in the buffer be discarded
761  *       (i.e. for packet-based services like UDP)
762  * @param one_shot only call callback once, keep rest of message in buffer
763  * @return #GNUNET_OK if we are done processing (need more data)
764  *         #GNUNET_NO if one_shot was set and we have another message ready
765  *         #GNUNET_SYSERR if the data stream is corrupt
766  */
767 int
768 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
769                            void *client_identity, const char *buf, size_t size,
770                            int purge, int one_shot);
771
772
773 /**
774  * Destroys a tokenizer.
775  *
776  * @param mst tokenizer to destroy
777  */
778 void
779 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
780
781
782 /**
783  * Signature of a function to create a custom tokenizer.
784  *
785  * @param cls closure from 'GNUNET_SERVER_set_callbacks'
786  * @param client handle to client the tokenzier will be used for
787  * @return handle to custom tokenizer ('mst')
788  */
789 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
790                                                   struct GNUNET_SERVER_Client *client);
791
792
793 /**
794  * Signature of a function to destroy a custom tokenizer.
795  *
796  * @param cls closure from 'GNUNET_SERVER_set_callbacks'
797  * @param mst custom tokenizer handle
798  */
799 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
800
801
802 /**
803  * Signature of a function to receive data for a custom tokenizer.
804  *
805  * @param cls closure from #GNUNET_SERVER_set_callbacks
806  * @param mst custom tokenizer handle
807  * @param client_identity ID of client for which this is a buffer,
808  *        can be NULL (will be passed back to 'cb')
809  * @param buf input data to add
810  * @param size number of bytes in @a buf
811  * @param purge should any excess bytes in the buffer be discarded
812  *       (i.e. for packet-based services like UDP)
813  * @param one_shot only call callback once, keep rest of message in buffer
814  * @return #GNUNET_OK if we are done processing (need more data)
815  *         #GNUNET_NO if one_shot was set and we have another message ready
816  *         #GNUNET_SYSERR if the data stream is corrupt
817  */
818 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
819                                                  struct GNUNET_SERVER_Client *client,
820                                                  const char *buf, size_t size,
821                                                  int purge, int one_shot);
822
823
824 /**
825  * Change functions used by the server to tokenize the message stream.
826  * (very rarely used).
827  *
828  * @param server server to modify
829  * @param create new tokenizer initialization function
830  * @param destroy new tokenizer destruction function
831  * @param receive new tokenizer receive function
832  * @param cls closure for @a create, @a receive and @a destroy
833  */
834 void
835 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
836                              GNUNET_SERVER_MstCreateCallback create,
837                              GNUNET_SERVER_MstDestroyCallback destroy,
838                              GNUNET_SERVER_MstReceiveCallback receive,
839                              void *cls);
840
841
842 #if 0                           /* keep Emacsens' auto-indent happy */
843 {
844 #endif
845 #ifdef __cplusplus
846 }
847 #endif
848
849 /** @} */ /* end of group server */
850
851 /* ifndef GNUNET_SERVER_LIB_H */
852 #endif
853 /* end of gnunet_server_lib.h */