2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
25 * Library for building GNUnet network servers
27 * @defgroup server Server library
28 * Library for building GNUnet network servers
30 * Provides functions for a server that communicates with clients.
32 * @see [Documentation](https://gnunet.org/ipc)
37 #ifndef GNUNET_SERVER_LIB_H
38 #define GNUNET_SERVER_LIB_H
43 #if 0 /* keep Emacsens' auto-indent happy */
48 #include "gnunet_common.h"
49 #include "gnunet_connection_lib.h"
53 * Largest supported message (to be precise, one byte more
54 * than the largest possible message, so tests involving
55 * this value should check for messages being smaller than
58 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
61 * Smallest supported message.
63 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
66 * @brief handle for a server
68 struct GNUNET_SERVER_Handle;
71 * @brief opaque handle for a client of the server
73 struct GNUNET_SERVER_Client;
76 * @brief opaque handle server returns for aborting transmission to a client.
78 struct GNUNET_SERVER_TransmitHandle;
82 * Functions with this signature are called whenever a message is
86 * @param client identification of the client
87 * @param message the actual message
90 (*GNUNET_SERVER_MessageCallback) (void *cls,
91 struct GNUNET_SERVER_Client *client,
92 const struct GNUNET_MessageHeader *message);
96 * Message handler. Each struct specifies how to handle on particular
97 * type of message received.
99 struct GNUNET_SERVER_MessageHandler
102 * Function to call for messages of "type".
104 GNUNET_SERVER_MessageCallback callback;
107 * Closure argument for @e callback.
112 * Type of the message this handler covers.
117 * Expected size of messages of this type. Use 0 for
118 * variable-size. If non-zero, messages of the given
119 * type will be discarded (and the connection closed)
120 * if they do not have the right size.
122 uint16_t expected_size;
128 * Create a new server.
130 * @param access_cb function for access control
131 * @param access_cb_cls closure for @a access_cb
132 * @param lsocks NULL-terminated array of listen sockets
133 * @param idle_timeout after how long should we timeout idle connections?
134 * @param require_found if #GNUNET_YES, connections sending messages of unknown type
136 * @return handle for the new server, NULL on error
137 * (typically, "port" already in use)
139 struct GNUNET_SERVER_Handle *
140 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access_cb,
142 struct GNUNET_NETWORK_Handle **lsocks,
143 struct GNUNET_TIME_Relative idle_timeout,
147 * Create a new server.
149 * @param access_cb function for access control
150 * @param access_cb_cls closure for @a access_cb
151 * @param server_addr address toes listen on (including port), NULL terminated array
152 * @param socklen lengths of respective @a server_addr
153 * @param idle_timeout after how long should we timeout idle connections?
154 * @param require_found if #GNUNET_YES, connections sending messages of unknown type
156 * @return handle for the new server, NULL on error
157 * (typically, "port" already in use)
159 struct GNUNET_SERVER_Handle *
160 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access_cb,
162 struct sockaddr *const *server_addr,
163 const socklen_t * socklen,
164 struct GNUNET_TIME_Relative idle_timeout,
169 * Suspend accepting connections from the listen socket temporarily.
170 * Resume activity using #GNUNET_SERVER_resume.
172 * @param server server to stop accepting connections.
175 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
179 * Resume accepting connections from the listen socket.
181 * @param server server to resume accepting connections.
184 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
188 * Stop the listen socket and get ready to shutdown the server once
189 * only clients marked using #GNUNET_SERVER_client_mark_monitor are
192 * @param server server to stop listening on
195 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
199 * Free resources held by this server.
201 * @param server server to destroy
204 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
208 * Add additional handlers to an existing server.
210 * @param server the server to add handlers to
211 * @param handlers array of message handlers for
212 * incoming messages; the last entry must
213 * have "NULL" for the "callback"; multiple
214 * entries for the same type are allowed,
215 * they will be called in order of occurence.
216 * These handlers can be removed later;
217 * the handlers array must exist until removed
218 * (or server is destroyed).
221 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
222 const struct GNUNET_SERVER_MessageHandler *handlers);
226 * Notify us when the server has enough space to transmit
227 * a message of the given size to the given client.
229 * @param client client to transmit message to
230 * @param size requested amount of buffer space
231 * @param timeout after how long should we give up (and call
232 * notify with buf NULL and size 0)?
233 * @param callback function to call when space is available
234 * @param callback_cls closure for @a callback
235 * @return non-NULL if the notify callback was queued; can be used
236 * to cancel the request using
237 * #GNUNET_SERVER_notify_transmit_ready_cancel.
238 * NULL if we are already going to notify someone else (busy)
240 struct GNUNET_SERVER_TransmitHandle *
241 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
243 struct GNUNET_TIME_Relative timeout,
244 GNUNET_CONNECTION_TransmitReadyNotify callback,
249 * Abort transmission request.
251 * @param th request to abort
254 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
258 * Set the 'monitor' flag on this client. Clients which have been
259 * marked as 'monitors' won't prevent the server from shutting down
260 * once #GNUNET_SERVER_stop_listening has been invoked. The idea is
261 * that for "normal" clients we likely want to allow them to process
262 * their requests; however, monitor-clients are likely to 'never'
263 * disconnect during shutdown and thus will not be considered when
264 * determining if the server should continue to exist after
265 * #GNUNET_SERVER_destroy has been called.
267 * @param client the client to set the 'monitor' flag on
270 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
274 * Set the persistent flag on this client, used to setup client
275 * connection to only be killed when the process of the service it's
276 * connected to is actually dead. This API is used during shutdown
277 * signalling within ARM, and it is not expected that typical users
278 * of the API would need this function.
280 * @param client the client to set the persistent flag on
283 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
287 * Resume receiving from this client, we are done processing the
288 * current request. This function must be called from within each
289 * #GNUNET_SERVER_MessageCallback (or its respective continuations).
291 * @param client client we were processing a message of
292 * @param success #GNUNET_OK to keep the connection open and
293 * continue to receive
294 * #GNUNET_NO to close the connection (normal behavior)
295 * #GNUNET_SYSERR to close the connection (signal
299 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
304 * Change the timeout for a particular client. Decreasing the timeout
305 * may not go into effect immediately (only after the previous timeout
306 * times out or activity happens on the socket).
308 * @param client the client to update
309 * @param timeout new timeout for activities on the socket
312 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
313 struct GNUNET_TIME_Relative timeout);
317 * Return user context associated with the given client.
318 * Note: you should probably use the macro (call without the underscore).
320 * @param client client to query
321 * @param size number of bytes in user context struct (for verification only)
322 * @return pointer to user context
325 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
330 * Set user context to be associated with the given client.
331 * Note: you should probably use the macro (call without the underscore).
333 * @param client client to query
334 * @param ptr pointer to user context
335 * @param size number of bytes in user context struct (for verification only)
338 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
344 * Return user context associated with the given client.
346 * @param client client to query
347 * @param type expected return type (i.e. 'struct Foo')
348 * @return pointer to user context of type 'type *'.
350 #define GNUNET_SERVER_client_get_user_context(client,type) \
351 (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
354 * Set user context to be associated with the given client.
356 * @param client client to query
357 * @param value pointer to user context
359 #define GNUNET_SERVER_client_set_user_context(client,value) \
360 GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
364 * Disable the warning the server issues if a message is not acknowledged
365 * in a timely fashion. Use this call if a client is intentionally delayed
366 * for a while. Only applies to the current message.
368 * @param client client for which to disable the warning
371 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
376 * Inject a message into the server, pretend it came
377 * from the specified client. Delivery of the message
378 * will happen instantly (if a handler is installed;
379 * otherwise the call does nothing).
381 * @param server the server receiving the message
382 * @param sender the "pretended" sender of the message
384 * @param message message to transmit
385 * @return #GNUNET_OK if the message was OK and the
386 * connection can stay open
387 * #GNUNET_SYSERR if the connection to the
388 * client should be shut down
391 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
392 struct GNUNET_SERVER_Client *sender,
393 const struct GNUNET_MessageHeader *message);
397 * Add a TCP socket-based connection to the set of handles managed by
398 * this server. Use this function for outgoing (P2P) connections that
399 * we initiated (and where this server should process incoming
402 * @param server the server to use
403 * @param connection the connection to manage (client must
404 * stop using this connection from now on)
405 * @return the client handle
407 struct GNUNET_SERVER_Client *
408 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
409 struct GNUNET_CONNECTION_Handle *connection);
413 * Notify the server that the given client handle should
414 * be kept (keeps the connection up if possible, increments
415 * the internal reference counter).
417 * @param client the client to keep
420 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
424 * Notify the server that the given client handle is no
425 * longer required. Decrements the reference counter. If
426 * that counter reaches zero an inactive connection maybe
429 * @param client the client to drop
432 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
436 * Obtain the network address of the other party.
438 * @param client the client to get the address for
439 * @param addr where to store the address
440 * @param addrlen where to store the length of @a addr
441 * @return #GNUNET_OK on success
444 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
445 void **addr, size_t *addrlen);
449 * Functions with this signature are called whenever a client
450 * is disconnected on the network level.
453 * @param client identification of the client; NULL
454 * for the last call when the server is destroyed
456 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
457 struct GNUNET_SERVER_Client *client);
461 * Functions with this signature are called whenever a client
462 * is connected on the network level.
465 * @param client identification of the client
467 typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
468 struct GNUNET_SERVER_Client *client);
472 * Ask the server to notify us whenever a client disconnects.
473 * This function is called whenever the actual network connection
474 * is closed; the reference count may be zero or larger than zero
475 * at this point. If the server is destroyed before this
476 * notification is explicitly cancelled, the 'callback' will
477 * once be called with a 'client' argument of NULL to indicate
478 * that the server itself is now gone (and that the callback
479 * won't be called anymore and also can no longer be cancelled).
481 * @param server the server manageing the clients
482 * @param callback function to call on disconnect
483 * @param callback_cls closure for @a callback
486 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
487 GNUNET_SERVER_DisconnectCallback callback,
492 * Ask the server to notify us whenever a client connects.
493 * This function is called whenever the actual network connection
494 * is opened. If the server is destroyed before this
495 * notification is explicitly cancelled, the @a callback will
496 * once be called with a 'client' argument of NULL to indicate
497 * that the server itself is now gone (and that the callback
498 * won't be called anymore and also can no longer be cancelled).
500 * @param server the server manageing the clients
501 * @param callback function to call on sconnect
502 * @param callback_cls closure for @a callback
505 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
506 GNUNET_SERVER_ConnectCallback callback,
511 * Ask the server to stop notifying us whenever a client disconnects.
512 * Arguments must match exactly those given to
513 * #GNUNET_SERVER_disconnect_notify. It is not necessary to call this
514 * function during shutdown of the server; in fact, most applications
515 * will never use this function.
517 * @param server the server manageing the clients
518 * @param callback function to call on disconnect
519 * @param callback_cls closure for @a callback
522 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
523 GNUNET_SERVER_DisconnectCallback callback,
528 * Ask the server to stop notifying us whenever a client connects.
529 * Arguments must match exactly those given to
530 * #GNUNET_SERVER_connect_notify. It is not necessary to call this
531 * function during shutdown of the server; in fact, most applications
532 * will never use this function.
534 * @param server the server manageing the clients
535 * @param callback function to call on connect
536 * @param callback_cls closure for @a callback
539 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
540 GNUNET_SERVER_ConnectCallback callback,
545 * Ask the server to disconnect from the given client. This is the
546 * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
547 * except that it allows dropping of a client even when not handling a
548 * message from that client.
550 * @param client the client to disconnect from
553 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
557 * Disable the "CORK" feature for communication with the given client,
558 * forcing the OS to immediately flush the buffer on transmission
559 * instead of potentially buffering multiple messages.
561 * @param client handle to the client
562 * @return #GNUNET_OK on success
565 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
569 * The tansmit context is the key datastructure for a conveniance API
570 * used for transmission of complex results to the client followed
571 * ONLY by signaling receive_done with success or error
573 struct GNUNET_SERVER_TransmitContext;
577 * Create a new transmission context for the given client.
579 * @param client client to create the context for.
580 * @return NULL on error
582 struct GNUNET_SERVER_TransmitContext *
583 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
587 * Append a message to the transmission context.
588 * All messages in the context will be sent by
589 * the #GNUNET_SERVER_transmit_context_run method.
591 * @param tc context to use
592 * @param data what to append to the result message
593 * @param length length of @a data
594 * @param type type of the message
597 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext *tc,
599 size_t length, uint16_t type);
603 * Append a message to the transmission context.
604 * All messages in the context will be sent by
605 * the transmit_context_run method.
607 * @param tc context to use
608 * @param msg message to append
611 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext *tc,
612 const struct GNUNET_MessageHeader *msg);
616 * Execute a transmission context. If there is an error in the
617 * transmission, the receive_done method will be called with an error
618 * code (#GNUNET_SYSERR), otherwise with #GNUNET_OK.
620 * @param tc transmission context to use
621 * @param timeout when to time out and abort the transmission
624 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
625 struct GNUNET_TIME_Relative timeout);
629 * Destroy a transmission context. This function must not be called
630 * after #GNUNET_SERVER_transmit_context_run.
632 * @param tc transmission context to destroy
633 * @param success code to give to #GNUNET_SERVER_receive_done for
634 * the client: #GNUNET_OK to keep the connection open and
635 * continue to receive
636 * #GNUNET_NO to close the connection (normal behavior)
637 * #GNUNET_SYSERR to close the connection (signal
641 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext *tc,
646 * The notification context is the key datastructure for a conveniance
647 * API used for transmission of notifications to the client until the
648 * client disconnects or is disconnected (or the notification context
649 * is destroyed, in which case we disconnect these clients).
650 * Essentially, all (notification) messages are queued up until the
651 * client is able to read them.
653 struct GNUNET_SERVER_NotificationContext;
657 * Create a new notification context.
659 * @param server server for which this function creates the context
660 * @param queue_length maximum number of messages to keep in
661 * the notification queue; optional messages are dropped
662 * if the queue gets longer than this number of messages
663 * @return handle to the notification context
665 struct GNUNET_SERVER_NotificationContext *
666 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
667 unsigned int queue_length);
671 * Destroy the context, force disconnect for all clients.
673 * @param nc context to destroy.
676 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
680 * Add a client to the notification context.
682 * @param nc context to modify
683 * @param client client to add
686 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
687 struct GNUNET_SERVER_Client *client);
691 * Send a message to a particular client; must have
692 * already been added to the notification context.
694 * @param nc context to modify
695 * @param client client to transmit to
696 * @param msg message to send
697 * @param can_drop can this message be dropped due to queue length limitations
700 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
701 struct GNUNET_SERVER_Client *client,
702 const struct GNUNET_MessageHeader *msg,
707 * Send a message to all clients of this context.
709 * @param nc context to modify
710 * @param msg message to send
711 * @param can_drop can this message be dropped due to queue length limitations
714 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
715 const struct GNUNET_MessageHeader *msg,
720 * Return active number of subscribers in this context.
722 * @param nc context to query
723 * @return number of current subscribers
726 GNUNET_SERVER_notification_context_get_size (struct GNUNET_SERVER_NotificationContext *nc);
730 * Handle to a message stream tokenizer.
732 struct GNUNET_SERVER_MessageStreamTokenizer;
736 * Functions with this signature are called whenever a
737 * complete message is received by the tokenizer.
739 * Do not call #GNUNET_SERVER_mst_destroy from within
740 * the scope of this callback.
743 * @param client identification of the client
744 * @param message the actual message
745 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
747 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
748 const struct GNUNET_MessageHeader *message);
752 * Create a message stream tokenizer.
754 * @param cb function to call on completed messages
755 * @param cb_cls closure for @a cb
756 * @return handle to tokenizer
758 struct GNUNET_SERVER_MessageStreamTokenizer *
759 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
764 * Add incoming data to the receive buffer and call the
765 * callback for all complete messages.
767 * @param mst tokenizer to use
768 * @param client_identity ID of client for which this is a buffer,
769 * can be NULL (will be passed back to 'cb')
770 * @param buf input data to add
771 * @param size number of bytes in @a buf
772 * @param purge should any excess bytes in the buffer be discarded
773 * (i.e. for packet-based services like UDP)
774 * @param one_shot only call callback once, keep rest of message in buffer
775 * @return #GNUNET_OK if we are done processing (need more data)
776 * #GNUNET_NO if one_shot was set and we have another message ready
777 * #GNUNET_SYSERR if the data stream is corrupt
780 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
781 void *client_identity,
782 const char *buf, size_t size,
783 int purge, int one_shot);
787 * Destroys a tokenizer.
789 * @param mst tokenizer to destroy
792 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
796 * Signature of a function to create a custom tokenizer.
798 * @param cls closure from #GNUNET_SERVER_set_callbacks
799 * @param client handle to client the tokenzier will be used for
800 * @return handle to custom tokenizer ('mst')
802 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
803 struct GNUNET_SERVER_Client *client);
807 * Signature of a function to destroy a custom tokenizer.
809 * @param cls closure from #GNUNET_SERVER_set_callbacks
810 * @param mst custom tokenizer handle
812 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
816 * Signature of a function to receive data for a custom tokenizer.
818 * @param cls closure from #GNUNET_SERVER_set_callbacks
819 * @param mst custom tokenizer handle
820 * @param client_identity ID of client for which this is a buffer,
821 * can be NULL (will be passed back to 'cb')
822 * @param buf input data to add
823 * @param size number of bytes in @a buf
824 * @param purge should any excess bytes in the buffer be discarded
825 * (i.e. for packet-based services like UDP)
826 * @param one_shot only call callback once, keep rest of message in buffer
827 * @return #GNUNET_OK if we are done processing (need more data)
828 * #GNUNET_NO if one_shot was set and we have another message ready
829 * #GNUNET_SYSERR if the data stream is corrupt
831 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
832 struct GNUNET_SERVER_Client *client,
833 const char *buf, size_t size,
834 int purge, int one_shot);
838 * Change functions used by the server to tokenize the message stream.
839 * (very rarely used).
841 * @param server server to modify
842 * @param create new tokenizer initialization function
843 * @param destroy new tokenizer destruction function
844 * @param receive new tokenizer receive function
845 * @param cls closure for @a create, @a receive and @a destroy
848 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
849 GNUNET_SERVER_MstCreateCallback create,
850 GNUNET_SERVER_MstDestroyCallback destroy,
851 GNUNET_SERVER_MstReceiveCallback receive,
855 #if 0 /* keep Emacsens' auto-indent happy */
862 /* ifndef GNUNET_SERVER_LIB_H */
865 /** @} */ /* end of group server */
867 /* end of gnunet_server_lib.h */