2 This file is part of GNUnet.
3 (C) 2009-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
29 #ifndef GNUNET_SERVER_LIB_H
30 #define GNUNET_SERVER_LIB_H
35 #if 0 /* keep Emacsens' auto-indent happy */
40 #include "gnunet_common.h"
41 #include "gnunet_connection_lib.h"
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
50 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
53 * Smallest supported message.
55 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
58 * @brief handle for a server
60 struct GNUNET_SERVER_Handle;
63 * @brief opaque handle for a client of the server
65 struct GNUNET_SERVER_Client;
68 * @brief opaque handle server returns for aborting transmission to a client.
70 struct GNUNET_SERVER_TransmitHandle;
74 * Functions with this signature are called whenever a message is
78 * @param client identification of the client
79 * @param message the actual message
81 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
82 struct GNUNET_SERVER_Client *client,
83 const struct GNUNET_MessageHeader
88 * Message handler. Each struct specifies how to handle on particular
89 * type of message received.
91 struct GNUNET_SERVER_MessageHandler
94 * Function to call for messages of "type".
96 GNUNET_SERVER_MessageCallback callback;
99 * Closure argument for "callback".
104 * Type of the message this handler covers.
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.
114 uint16_t expected_size;
120 * Create a new server.
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
128 * @return handle for the new server, NULL on error
129 * (typically, "port" already in use)
131 struct GNUNET_SERVER_Handle *
132 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
134 struct GNUNET_NETWORK_Handle **lsocks,
135 struct GNUNET_TIME_Relative idle_timeout,
139 * Create a new server.
141 * @param access function for access control
142 * @param access_cls closure for @a access
143 * @param serverAddr address toes listen on (including port), NULL terminated array
144 * @param socklen lengths of respective @a serverAddr
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
148 * @return handle for the new server, NULL on error
149 * (typically, "port" already in use)
151 struct GNUNET_SERVER_Handle *
152 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
153 struct sockaddr *const *serverAddr,
154 const socklen_t * socklen,
155 struct GNUNET_TIME_Relative idle_timeout,
160 * Suspend accepting connections from the listen socket temporarily.
161 * Resume activity using #GNUNET_SERVER_resume.
163 * @param server server to stop accepting connections.
166 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
170 * Resume accepting connections from the listen socket.
172 * @param server server to resume accepting connections.
175 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
179 * Stop the listen socket and get ready to shutdown the server once
180 * only clients marked using #GNUNET_SERVER_client_mark_monitor are
183 * @param server server to stop listening on
186 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
190 * Free resources held by this server.
192 * @param server server to destroy
195 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
199 * Add additional handlers to an existing server.
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).
212 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
213 const struct GNUNET_SERVER_MessageHandler
218 * Notify us when the server has enough space to transmit
219 * a message of the given size to the given client.
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)
232 struct GNUNET_SERVER_TransmitHandle *
233 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
235 struct GNUNET_TIME_Relative timeout,
236 GNUNET_CONNECTION_TransmitReadyNotify
237 callback, void *callback_cls);
241 * Abort transmission request.
243 * @param th request to abort
246 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
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.
259 * @param client the client to set the 'monitor' flag on
262 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
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.
272 * @param client the client to set the persistent flag on
275 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
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).
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
291 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
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).
300 * @param client the client to update
301 * @param timeout new timeout for activities on the socket
304 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
305 struct GNUNET_TIME_Relative timeout);
309 * Return user context associated with the given client.
310 * Note: you should probably use the macro (call without the underscore).
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
317 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
322 * Set user context to be associated with the given client.
323 * Note: you should probably use the macro (call without the underscore).
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)
330 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
336 * Return user context associated with the given client.
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 *'.
342 #define GNUNET_SERVER_client_get_user_context(client,type) (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
345 * Set user context to be associated with the given client.
347 * @param client client to query
348 * @param value pointer to user context
350 #define GNUNET_SERVER_client_set_user_context(client,value) GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
354 * Disable the warning the server issues if a message is not acknowledged
355 * in a timely fashion. Use this call if a client is intentionally delayed
356 * for a while. Only applies to the current message.
358 * @param client client for which to disable the warning
361 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
366 * Inject a message into the server, pretend it came
367 * from the specified client. Delivery of the message
368 * will happen instantly (if a handler is installed;
369 * otherwise the call does nothing).
371 * @param server the server receiving the message
372 * @param sender the "pretended" sender of the message
374 * @param message message to transmit
375 * @return #GNUNET_OK if the message was OK and the
376 * connection can stay open
377 * #GNUNET_SYSERR if the connection to the
378 * client should be shut down
381 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
382 struct GNUNET_SERVER_Client *sender,
383 const struct GNUNET_MessageHeader *message);
387 * Add a TCP socket-based connection to the set of handles managed by
388 * this server. Use this function for outgoing (P2P) connections that
389 * we initiated (and where this server should process incoming
392 * @param server the server to use
393 * @param connection the connection to manage (client must
394 * stop using this connection from now on)
395 * @return the client handle (client should call
396 * #GNUNET_SERVER_client_drop on the return value eventually)
398 struct GNUNET_SERVER_Client *
399 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
400 struct GNUNET_CONNECTION_Handle *connection);
404 * Notify the server that the given client handle should
405 * be kept (keeps the connection up if possible, increments
406 * the internal reference counter).
408 * @param client the client to keep
411 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
415 * Notify the server that the given client handle is no
416 * longer required. Decrements the reference counter. If
417 * that counter reaches zero an inactive connection maybe
420 * @param client the client to drop
423 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
427 * Obtain the network address of the other party.
429 * @param client the client to get the address for
430 * @param addr where to store the address
431 * @param addrlen where to store the length of @a addr
432 * @return #GNUNET_OK on success
435 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
436 void **addr, size_t *addrlen);
440 * Functions with this signature are called whenever a client
441 * is disconnected on the network level.
444 * @param client identification of the client; NULL
445 * for the last call when the server is destroyed
447 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
448 struct GNUNET_SERVER_Client *client);
452 * Functions with this signature are called whenever a client
453 * is connected on the network level.
456 * @param client identification of the client
458 typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
459 struct GNUNET_SERVER_Client *client);
463 * Ask the server to notify us whenever a client disconnects.
464 * This function is called whenever the actual network connection
465 * is closed; the reference count may be zero or larger than zero
466 * at this point. If the server is destroyed before this
467 * notification is explicitly cancelled, the 'callback' will
468 * once be called with a 'client' argument of NULL to indicate
469 * that the server itself is now gone (and that the callback
470 * won't be called anymore and also can no longer be cancelled).
472 * @param server the server manageing the clients
473 * @param callback function to call on disconnect
474 * @param callback_cls closure for @a callback
477 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
478 GNUNET_SERVER_DisconnectCallback callback,
483 * Ask the server to notify us whenever a client connects.
484 * This function is called whenever the actual network connection
485 * is opened. If the server is destroyed before this
486 * notification is explicitly cancelled, the @a callback will
487 * once be called with a 'client' argument of NULL to indicate
488 * that the server itself is now gone (and that the callback
489 * won't be called anymore and also can no longer be cancelled).
491 * @param server the server manageing the clients
492 * @param callback function to call on sconnect
493 * @param callback_cls closure for @a callback
496 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
497 GNUNET_SERVER_ConnectCallback callback,
502 * Ask the server to stop notifying us whenever a client disconnects.
503 * Arguments must match exactly those given to
504 * #GNUNET_SERVER_disconnect_notify. It is not necessary to call this
505 * function during shutdown of the server; in fact, most applications
506 * will never use this function.
508 * @param server the server manageing the clients
509 * @param callback function to call on disconnect
510 * @param callback_cls closure for @a callback
513 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
514 GNUNET_SERVER_DisconnectCallback
515 callback, void *callback_cls);
519 * Ask the server to stop notifying us whenever a client connects.
520 * Arguments must match exactly those given to
521 * #GNUNET_SERVER_connect_notify. It is not necessary to call this
522 * function during shutdown of the server; in fact, most applications
523 * will never use this function.
525 * @param server the server manageing the clients
526 * @param callback function to call on connect
527 * @param callback_cls closure for @a callback
530 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
531 GNUNET_SERVER_ConnectCallback callback,
536 * Ask the server to disconnect from the given client. This is the
537 * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
538 * except that it allows dropping of a client even when not handling a
539 * message from that client.
541 * @param client the client to disconnect from
544 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
548 * Disable the "CORK" feature for communication with the given client,
549 * forcing the OS to immediately flush the buffer on transmission
550 * instead of potentially buffering multiple messages.
552 * @param client handle to the client
553 * @return #GNUNET_OK on success
556 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
560 * The tansmit context is the key datastructure for a conveniance API
561 * used for transmission of complex results to the client followed
562 * ONLY by signaling receive_done with success or error
564 struct GNUNET_SERVER_TransmitContext;
568 * Create a new transmission context for the given client.
570 * @param client client to create the context for.
571 * @return NULL on error
573 struct GNUNET_SERVER_TransmitContext *
574 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
578 * Append a message to the transmission context.
579 * All messages in the context will be sent by
580 * the #GNUNET_SERVER_transmit_context_run method.
582 * @param tc context to use
583 * @param data what to append to the result message
584 * @param length length of @a data
585 * @param type type of the message
588 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
589 *tc, const void *data,
590 size_t length, uint16_t type);
594 * Append a message to the transmission context.
595 * All messages in the context will be sent by
596 * the transmit_context_run method.
598 * @param tc context to use
599 * @param msg message to append
602 GNUNET_SERVER_transmit_context_append_message (struct
603 GNUNET_SERVER_TransmitContext
605 const struct GNUNET_MessageHeader
610 * Execute a transmission context. If there is an error in the
611 * transmission, the receive_done method will be called with an error
612 * code (#GNUNET_SYSERR), otherwise with #GNUNET_OK.
614 * @param tc transmission context to use
615 * @param timeout when to time out and abort the transmission
618 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
619 struct GNUNET_TIME_Relative timeout);
623 * Destroy a transmission context. This function must not be called
624 * after #GNUNET_SERVER_transmit_context_run.
626 * @param tc transmission context to destroy
627 * @param success code to give to #GNUNET_SERVER_receive_done for
628 * the client: #GNUNET_OK to keep the connection open and
629 * continue to receive
630 * #GNUNET_NO to close the connection (normal behavior)
631 * #GNUNET_SYSERR to close the connection (signal
635 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
640 * The notification context is the key datastructure for a conveniance
641 * API used for transmission of notifications to the client until the
642 * client disconnects or is disconnected (or the notification context
643 * is destroyed, in which case we disconnect these clients).
644 * Essentially, all (notification) messages are queued up until the
645 * client is able to read them.
647 struct GNUNET_SERVER_NotificationContext;
651 * Create a new notification context.
653 * @param server server for which this function creates the context
654 * @param queue_length maximum number of messages to keep in
655 * the notification queue; optional messages are dropped
656 * if the queue gets longer than this number of messages
657 * @return handle to the notification context
659 struct GNUNET_SERVER_NotificationContext *
660 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
661 unsigned int queue_length);
665 * Destroy the context, force disconnect for all clients.
667 * @param nc context to destroy.
670 GNUNET_SERVER_notification_context_destroy (struct
671 GNUNET_SERVER_NotificationContext
676 * Add a client to the notification context.
678 * @param nc context to modify
679 * @param client client to add
682 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
684 struct GNUNET_SERVER_Client *client);
688 * Send a message to a particular client; must have
689 * already been added to the notification context.
691 * @param nc context to modify
692 * @param client client to transmit to
693 * @param msg message to send
694 * @param can_drop can this message be dropped due to queue length limitations
697 GNUNET_SERVER_notification_context_unicast (struct
698 GNUNET_SERVER_NotificationContext
700 struct GNUNET_SERVER_Client *client,
701 const struct GNUNET_MessageHeader
706 * Send a message to all clients of this context.
708 * @param nc context to modify
709 * @param msg message to send
710 * @param can_drop can this message be dropped due to queue length limitations
713 GNUNET_SERVER_notification_context_broadcast (struct
714 GNUNET_SERVER_NotificationContext
716 const struct GNUNET_MessageHeader
721 * Handle to a message stream tokenizer.
723 struct GNUNET_SERVER_MessageStreamTokenizer;
727 * Functions with this signature are called whenever a
728 * complete message is received by the tokenizer.
730 * Do not call #GNUNET_SERVER_mst_destroy from within
731 * the scope of this callback.
734 * @param client identification of the client
735 * @param message the actual message
736 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
738 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
740 GNUNET_MessageHeader *
745 * Create a message stream tokenizer.
747 * @param cb function to call on completed messages
748 * @param cb_cls closure for @a cb
749 * @return handle to tokenizer
751 struct GNUNET_SERVER_MessageStreamTokenizer *
752 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
757 * Add incoming data to the receive buffer and call the
758 * callback for all complete messages.
760 * @param mst tokenizer to use
761 * @param client_identity ID of client for which this is a buffer,
762 * can be NULL (will be passed back to 'cb')
763 * @param buf input data to add
764 * @param size number of bytes in @a buf
765 * @param purge should any excess bytes in the buffer be discarded
766 * (i.e. for packet-based services like UDP)
767 * @param one_shot only call callback once, keep rest of message in buffer
768 * @return #GNUNET_OK if we are done processing (need more data)
769 * #GNUNET_NO if one_shot was set and we have another message ready
770 * #GNUNET_SYSERR if the data stream is corrupt
773 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
774 void *client_identity, const char *buf, size_t size,
775 int purge, int one_shot);
779 * Destroys a tokenizer.
781 * @param mst tokenizer to destroy
784 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
788 * Signature of a function to create a custom tokenizer.
790 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
791 * @param client handle to client the tokenzier will be used for
792 * @return handle to custom tokenizer ('mst')
794 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
795 struct GNUNET_SERVER_Client *client);
799 * Signature of a function to destroy a custom tokenizer.
801 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
802 * @param mst custom tokenizer handle
804 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
808 * Signature of a function to receive data for a custom tokenizer.
810 * @param cls closure from #GNUNET_SERVER_set_callbacks
811 * @param mst custom tokenizer handle
812 * @param client_identity ID of client for which this is a buffer,
813 * can be NULL (will be passed back to 'cb')
814 * @param buf input data to add
815 * @param size number of bytes in @a buf
816 * @param purge should any excess bytes in the buffer be discarded
817 * (i.e. for packet-based services like UDP)
818 * @param one_shot only call callback once, keep rest of message in buffer
819 * @return #GNUNET_OK if we are done processing (need more data)
820 * #GNUNET_NO if one_shot was set and we have another message ready
821 * #GNUNET_SYSERR if the data stream is corrupt
823 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
824 struct GNUNET_SERVER_Client *client,
825 const char *buf, size_t size,
826 int purge, int one_shot);
830 * Change functions used by the server to tokenize the message stream.
831 * (very rarely used).
833 * @param server server to modify
834 * @param create new tokenizer initialization function
835 * @param destroy new tokenizer destruction function
836 * @param receive new tokenizer receive function
837 * @param cls closure for @a create, @a receive and @a destroy
840 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
841 GNUNET_SERVER_MstCreateCallback create,
842 GNUNET_SERVER_MstDestroyCallback destroy,
843 GNUNET_SERVER_MstReceiveCallback receive,
847 #if 0 /* keep Emacsens' auto-indent happy */
854 /** @} */ /* end of group server */
856 /* ifndef GNUNET_SERVER_LIB_H */
858 /* end of gnunet_server_lib.h */