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.
35 #ifndef GNUNET_SERVER_LIB_H
36 #define GNUNET_SERVER_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_common.h"
47 #include "gnunet_connection_lib.h"
51 * Largest supported message (to be precise, one byte more
52 * than the largest possible message, so tests involving
53 * this value should check for messages being smaller than
56 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
59 * Smallest supported message.
61 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
64 * @brief handle for a server
66 struct GNUNET_SERVER_Handle;
69 * @brief opaque handle for a client of the server
71 struct GNUNET_SERVER_Client;
74 * @brief opaque handle server returns for aborting transmission to a client.
76 struct GNUNET_SERVER_TransmitHandle;
80 * Functions with this signature are called whenever a message is
84 * @param client identification of the client
85 * @param message the actual message
88 (*GNUNET_SERVER_MessageCallback) (void *cls,
89 struct GNUNET_SERVER_Client *client,
90 const struct GNUNET_MessageHeader *message);
94 * Message handler. Each struct specifies how to handle on particular
95 * type of message received.
97 struct GNUNET_SERVER_MessageHandler
100 * Function to call for messages of "type".
102 GNUNET_SERVER_MessageCallback callback;
105 * Closure argument for @e callback.
110 * Type of the message this handler covers.
115 * Expected size of messages of this type. Use 0 for
116 * variable-size. If non-zero, messages of the given
117 * type will be discarded (and the connection closed)
118 * if they do not have the right size.
120 uint16_t expected_size;
126 * Create a new server.
128 * @param access_cb function for access control
129 * @param access_cb_cls closure for @a access_cb
130 * @param lsocks NULL-terminated array of listen sockets
131 * @param idle_timeout after how long should we timeout idle connections?
132 * @param require_found if #GNUNET_YES, connections sending messages of unknown type
134 * @return handle for the new server, NULL on error
135 * (typically, "port" already in use)
137 struct GNUNET_SERVER_Handle *
138 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access_cb,
140 struct GNUNET_NETWORK_Handle **lsocks,
141 struct GNUNET_TIME_Relative idle_timeout,
145 * Create a new server.
147 * @param access_cb function for access control
148 * @param access_cb_cls closure for @a access_cb
149 * @param server_addr address toes listen on (including port), NULL terminated array
150 * @param socklen lengths of respective @a server_addr
151 * @param idle_timeout after how long should we timeout idle connections?
152 * @param require_found if #GNUNET_YES, connections sending messages of unknown type
154 * @return handle for the new server, NULL on error
155 * (typically, "port" already in use)
157 struct GNUNET_SERVER_Handle *
158 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access_cb,
160 struct sockaddr *const *server_addr,
161 const socklen_t * socklen,
162 struct GNUNET_TIME_Relative idle_timeout,
167 * Suspend accepting connections from the listen socket temporarily.
168 * Resume activity using #GNUNET_SERVER_resume.
170 * @param server server to stop accepting connections.
173 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
177 * Resume accepting connections from the listen socket.
179 * @param server server to resume accepting connections.
182 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
186 * Stop the listen socket and get ready to shutdown the server once
187 * only clients marked using #GNUNET_SERVER_client_mark_monitor are
190 * @param server server to stop listening on
193 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
197 * Free resources held by this server.
199 * @param server server to destroy
202 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
206 * Add additional handlers to an existing server.
208 * @param server the server to add handlers to
209 * @param handlers array of message handlers for
210 * incoming messages; the last entry must
211 * have "NULL" for the "callback"; multiple
212 * entries for the same type are allowed,
213 * they will be called in order of occurence.
214 * These handlers can be removed later;
215 * the handlers array must exist until removed
216 * (or server is destroyed).
219 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
220 const struct GNUNET_SERVER_MessageHandler *handlers);
224 * Notify us when the server has enough space to transmit
225 * a message of the given size to the given client.
227 * @param client client to transmit message to
228 * @param size requested amount of buffer space
229 * @param timeout after how long should we give up (and call
230 * notify with buf NULL and size 0)?
231 * @param callback function to call when space is available
232 * @param callback_cls closure for @a callback
233 * @return non-NULL if the notify callback was queued; can be used
234 * to cancel the request using
235 * #GNUNET_SERVER_notify_transmit_ready_cancel.
236 * NULL if we are already going to notify someone else (busy)
238 struct GNUNET_SERVER_TransmitHandle *
239 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
241 struct GNUNET_TIME_Relative timeout,
242 GNUNET_CONNECTION_TransmitReadyNotify callback,
247 * Abort transmission request.
249 * @param th request to abort
252 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
256 * Set the 'monitor' flag on this client. Clients which have been
257 * marked as 'monitors' won't prevent the server from shutting down
258 * once #GNUNET_SERVER_stop_listening has been invoked. The idea is
259 * that for "normal" clients we likely want to allow them to process
260 * their requests; however, monitor-clients are likely to 'never'
261 * disconnect during shutdown and thus will not be considered when
262 * determining if the server should continue to exist after
263 * #GNUNET_SERVER_destroy has been called.
265 * @param client the client to set the 'monitor' flag on
268 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
272 * Set the persistent flag on this client, used to setup client
273 * connection to only be killed when the process of the service it's
274 * connected to is actually dead. This API is used during shutdown
275 * signalling within ARM, and it is not expected that typical users
276 * of the API would need this function.
278 * @param client the client to set the persistent flag on
281 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
285 * Resume receiving from this client, we are done processing the
286 * current request. This function must be called from within each
287 * #GNUNET_SERVER_MessageCallback (or its respective continuations).
289 * @param client client we were processing a message of
290 * @param success #GNUNET_OK to keep the connection open and
291 * continue to receive
292 * #GNUNET_NO to close the connection (normal behavior)
293 * #GNUNET_SYSERR to close the connection (signal
297 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
302 * Change the timeout for a particular client. Decreasing the timeout
303 * may not go into effect immediately (only after the previous timeout
304 * times out or activity happens on the socket).
306 * @param client the client to update
307 * @param timeout new timeout for activities on the socket
310 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
311 struct GNUNET_TIME_Relative timeout);
315 * Return user context associated with the given client.
316 * Note: you should probably use the macro (call without the underscore).
318 * @param client client to query
319 * @param size number of bytes in user context struct (for verification only)
320 * @return pointer to user context
323 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
328 * Set user context to be associated with the given client.
329 * Note: you should probably use the macro (call without the underscore).
331 * @param client client to query
332 * @param ptr pointer to user context
333 * @param size number of bytes in user context struct (for verification only)
336 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
342 * Return user context associated with the given client.
344 * @param client client to query
345 * @param type expected return type (i.e. 'struct Foo')
346 * @return pointer to user context of type 'type *'.
348 #define GNUNET_SERVER_client_get_user_context(client,type) \
349 (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
352 * Set user context to be associated with the given client.
354 * @param client client to query
355 * @param value pointer to user context
357 #define GNUNET_SERVER_client_set_user_context(client,value) \
358 GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
362 * Disable the warning the server issues if a message is not acknowledged
363 * in a timely fashion. Use this call if a client is intentionally delayed
364 * for a while. Only applies to the current message.
366 * @param client client for which to disable the warning
369 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
374 * Inject a message into the server, pretend it came
375 * from the specified client. Delivery of the message
376 * will happen instantly (if a handler is installed;
377 * otherwise the call does nothing).
379 * @param server the server receiving the message
380 * @param sender the "pretended" sender of the message
382 * @param message message to transmit
383 * @return #GNUNET_OK if the message was OK and the
384 * connection can stay open
385 * #GNUNET_SYSERR if the connection to the
386 * client should be shut down
389 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
390 struct GNUNET_SERVER_Client *sender,
391 const struct GNUNET_MessageHeader *message);
395 * Add a TCP socket-based connection to the set of handles managed by
396 * this server. Use this function for outgoing (P2P) connections that
397 * we initiated (and where this server should process incoming
400 * @param server the server to use
401 * @param connection the connection to manage (client must
402 * stop using this connection from now on)
403 * @return the client handle
405 struct GNUNET_SERVER_Client *
406 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
407 struct GNUNET_CONNECTION_Handle *connection);
411 * Notify the server that the given client handle should
412 * be kept (keeps the connection up if possible, increments
413 * the internal reference counter).
415 * @param client the client to keep
418 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
422 * Notify the server that the given client handle is no
423 * longer required. Decrements the reference counter. If
424 * that counter reaches zero an inactive connection maybe
427 * @param client the client to drop
430 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
434 * Obtain the network address of the other party.
436 * @param client the client to get the address for
437 * @param addr where to store the address
438 * @param addrlen where to store the length of @a addr
439 * @return #GNUNET_OK on success
442 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
443 void **addr, size_t *addrlen);
447 * Functions with this signature are called whenever a client
448 * is disconnected on the network level.
451 * @param client identification of the client; NULL
452 * for the last call when the server is destroyed
454 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
455 struct GNUNET_SERVER_Client *client);
459 * Functions with this signature are called whenever a client
460 * is connected on the network level.
463 * @param client identification of the client
465 typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
466 struct GNUNET_SERVER_Client *client);
470 * Ask the server to notify us whenever a client disconnects.
471 * This function is called whenever the actual network connection
472 * is closed; the reference count may be zero or larger than zero
473 * at this point. If the server is destroyed before this
474 * notification is explicitly cancelled, the 'callback' will
475 * once be called with a 'client' argument of NULL to indicate
476 * that the server itself is now gone (and that the callback
477 * won't be called anymore and also can no longer be cancelled).
479 * @param server the server manageing the clients
480 * @param callback function to call on disconnect
481 * @param callback_cls closure for @a callback
484 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
485 GNUNET_SERVER_DisconnectCallback callback,
490 * Ask the server to notify us whenever a client connects.
491 * This function is called whenever the actual network connection
492 * is opened. If the server is destroyed before this
493 * notification is explicitly cancelled, the @a callback will
494 * once be called with a 'client' argument of NULL to indicate
495 * that the server itself is now gone (and that the callback
496 * won't be called anymore and also can no longer be cancelled).
498 * @param server the server manageing the clients
499 * @param callback function to call on sconnect
500 * @param callback_cls closure for @a callback
503 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
504 GNUNET_SERVER_ConnectCallback callback,
509 * Ask the server to stop notifying us whenever a client disconnects.
510 * Arguments must match exactly those given to
511 * #GNUNET_SERVER_disconnect_notify. It is not necessary to call this
512 * function during shutdown of the server; in fact, most applications
513 * will never use this function.
515 * @param server the server manageing the clients
516 * @param callback function to call on disconnect
517 * @param callback_cls closure for @a callback
520 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
521 GNUNET_SERVER_DisconnectCallback callback,
526 * Ask the server to stop notifying us whenever a client connects.
527 * Arguments must match exactly those given to
528 * #GNUNET_SERVER_connect_notify. It is not necessary to call this
529 * function during shutdown of the server; in fact, most applications
530 * will never use this function.
532 * @param server the server manageing the clients
533 * @param callback function to call on connect
534 * @param callback_cls closure for @a callback
537 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
538 GNUNET_SERVER_ConnectCallback callback,
543 * Ask the server to disconnect from the given client. This is the
544 * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
545 * except that it allows dropping of a client even when not handling a
546 * message from that client.
548 * @param client the client to disconnect from
551 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
555 * Disable the "CORK" feature for communication with the given client,
556 * forcing the OS to immediately flush the buffer on transmission
557 * instead of potentially buffering multiple messages.
559 * @param client handle to the client
560 * @return #GNUNET_OK on success
563 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
567 * The tansmit context is the key datastructure for a conveniance API
568 * used for transmission of complex results to the client followed
569 * ONLY by signaling receive_done with success or error
571 struct GNUNET_SERVER_TransmitContext;
575 * Create a new transmission context for the given client.
577 * @param client client to create the context for.
578 * @return NULL on error
580 struct GNUNET_SERVER_TransmitContext *
581 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
585 * Append a message to the transmission context.
586 * All messages in the context will be sent by
587 * the #GNUNET_SERVER_transmit_context_run method.
589 * @param tc context to use
590 * @param data what to append to the result message
591 * @param length length of @a data
592 * @param type type of the message
595 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext *tc,
597 size_t length, uint16_t type);
601 * Append a message to the transmission context.
602 * All messages in the context will be sent by
603 * the transmit_context_run method.
605 * @param tc context to use
606 * @param msg message to append
609 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext *tc,
610 const struct GNUNET_MessageHeader *msg);
614 * Execute a transmission context. If there is an error in the
615 * transmission, the receive_done method will be called with an error
616 * code (#GNUNET_SYSERR), otherwise with #GNUNET_OK.
618 * @param tc transmission context to use
619 * @param timeout when to time out and abort the transmission
622 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
623 struct GNUNET_TIME_Relative timeout);
627 * Destroy a transmission context. This function must not be called
628 * after #GNUNET_SERVER_transmit_context_run.
630 * @param tc transmission context to destroy
631 * @param success code to give to #GNUNET_SERVER_receive_done for
632 * the client: #GNUNET_OK to keep the connection open and
633 * continue to receive
634 * #GNUNET_NO to close the connection (normal behavior)
635 * #GNUNET_SYSERR to close the connection (signal
639 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext *tc,
644 * The notification context is the key datastructure for a conveniance
645 * API used for transmission of notifications to the client until the
646 * client disconnects or is disconnected (or the notification context
647 * is destroyed, in which case we disconnect these clients).
648 * Essentially, all (notification) messages are queued up until the
649 * client is able to read them.
651 struct GNUNET_SERVER_NotificationContext;
655 * Create a new notification context.
657 * @param server server for which this function creates the context
658 * @param queue_length maximum number of messages to keep in
659 * the notification queue; optional messages are dropped
660 * if the queue gets longer than this number of messages
661 * @return handle to the notification context
663 struct GNUNET_SERVER_NotificationContext *
664 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
665 unsigned int queue_length);
669 * Destroy the context, force disconnect for all clients.
671 * @param nc context to destroy.
674 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
678 * Add a client to the notification context.
680 * @param nc context to modify
681 * @param client client to add
684 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
685 struct GNUNET_SERVER_Client *client);
689 * Send a message to a particular client; must have
690 * already been added to the notification context.
692 * @param nc context to modify
693 * @param client client to transmit to
694 * @param msg message to send
695 * @param can_drop can this message be dropped due to queue length limitations
698 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
699 struct GNUNET_SERVER_Client *client,
700 const struct GNUNET_MessageHeader *msg,
705 * Send a message to all clients of this context.
707 * @param nc context to modify
708 * @param msg message to send
709 * @param can_drop can this message be dropped due to queue length limitations
712 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
713 const struct GNUNET_MessageHeader *msg,
718 * Return active number of subscribers in this context.
720 * @param nc context to query
721 * @return number of current subscribers
724 GNUNET_SERVER_notification_context_get_size (struct GNUNET_SERVER_NotificationContext *nc);
728 * Handle to a message stream tokenizer.
730 struct GNUNET_SERVER_MessageStreamTokenizer;
734 * Functions with this signature are called whenever a
735 * complete message is received by the tokenizer.
737 * Do not call #GNUNET_SERVER_mst_destroy from within
738 * the scope of this callback.
741 * @param client identification of the client
742 * @param message the actual message
743 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
745 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
746 const struct GNUNET_MessageHeader *message);
750 * Create a message stream tokenizer.
752 * @param cb function to call on completed messages
753 * @param cb_cls closure for @a cb
754 * @return handle to tokenizer
756 struct GNUNET_SERVER_MessageStreamTokenizer *
757 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
762 * Add incoming data to the receive buffer and call the
763 * callback for all complete messages.
765 * @param mst tokenizer to use
766 * @param client_identity ID of client for which this is a buffer,
767 * can be NULL (will be passed back to 'cb')
768 * @param buf input data to add
769 * @param size number of bytes in @a buf
770 * @param purge should any excess bytes in the buffer be discarded
771 * (i.e. for packet-based services like UDP)
772 * @param one_shot only call callback once, keep rest of message in buffer
773 * @return #GNUNET_OK if we are done processing (need more data)
774 * #GNUNET_NO if one_shot was set and we have another message ready
775 * #GNUNET_SYSERR if the data stream is corrupt
778 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
779 void *client_identity,
780 const char *buf, size_t size,
781 int purge, int one_shot);
785 * Destroys a tokenizer.
787 * @param mst tokenizer to destroy
790 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
794 * Signature of a function to create a custom tokenizer.
796 * @param cls closure from #GNUNET_SERVER_set_callbacks
797 * @param client handle to client the tokenzier will be used for
798 * @return handle to custom tokenizer ('mst')
800 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
801 struct GNUNET_SERVER_Client *client);
805 * Signature of a function to destroy a custom tokenizer.
807 * @param cls closure from #GNUNET_SERVER_set_callbacks
808 * @param mst custom tokenizer handle
810 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
814 * Signature of a function to receive data for a custom tokenizer.
816 * @param cls closure from #GNUNET_SERVER_set_callbacks
817 * @param mst custom tokenizer handle
818 * @param client_identity ID of client for which this is a buffer,
819 * can be NULL (will be passed back to 'cb')
820 * @param buf input data to add
821 * @param size number of bytes in @a buf
822 * @param purge should any excess bytes in the buffer be discarded
823 * (i.e. for packet-based services like UDP)
824 * @param one_shot only call callback once, keep rest of message in buffer
825 * @return #GNUNET_OK if we are done processing (need more data)
826 * #GNUNET_NO if one_shot was set and we have another message ready
827 * #GNUNET_SYSERR if the data stream is corrupt
829 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
830 struct GNUNET_SERVER_Client *client,
831 const char *buf, size_t size,
832 int purge, int one_shot);
836 * Change functions used by the server to tokenize the message stream.
837 * (very rarely used).
839 * @param server server to modify
840 * @param create new tokenizer initialization function
841 * @param destroy new tokenizer destruction function
842 * @param receive new tokenizer receive function
843 * @param cls closure for @a create, @a receive and @a destroy
846 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
847 GNUNET_SERVER_MstCreateCallback create,
848 GNUNET_SERVER_MstDestroyCallback destroy,
849 GNUNET_SERVER_MstReceiveCallback receive,
853 #if 0 /* keep Emacsens' auto-indent happy */
860 /* ifndef GNUNET_SERVER_LIB_H */
863 /** @} */ /* end of group server */
865 /* end of gnunet_server_lib.h */