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 @e 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 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
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 *server_addr,
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 *handlers);
217 * Notify us when the server has enough space to transmit
218 * a message of the given size to the given client.
220 * @param client client to transmit message to
221 * @param size requested amount of buffer space
222 * @param timeout after how long should we give up (and call
223 * notify with buf NULL and size 0)?
224 * @param callback function to call when space is available
225 * @param callback_cls closure for @a callback
226 * @return non-NULL if the notify callback was queued; can be used
227 * to cancel the request using
228 * #GNUNET_SERVER_notify_transmit_ready_cancel.
229 * NULL if we are already going to notify someone else (busy)
231 struct GNUNET_SERVER_TransmitHandle *
232 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
234 struct GNUNET_TIME_Relative timeout,
235 GNUNET_CONNECTION_TransmitReadyNotify callback,
240 * Abort transmission request.
242 * @param th request to abort
245 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
249 * Set the 'monitor' flag on this client. Clients which have been
250 * marked as 'monitors' won't prevent the server from shutting down
251 * once #GNUNET_SERVER_stop_listening has been invoked. The idea is
252 * that for "normal" clients we likely want to allow them to process
253 * their requests; however, monitor-clients are likely to 'never'
254 * disconnect during shutdown and thus will not be considered when
255 * determining if the server should continue to exist after
256 * #GNUNET_SERVER_destroy has been called.
258 * @param client the client to set the 'monitor' flag on
261 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
265 * Set the persistent flag on this client, used to setup client
266 * connection to only be killed when the process of the service it's
267 * connected to is actually dead. This API is used during shutdown
268 * signalling within ARM, and it is not expected that typical users
269 * of the API would need this function.
271 * @param client the client to set the persistent flag on
274 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
278 * Resume receiving from this client, we are done processing the
279 * current request. This function must be called from within each
280 * #GNUNET_SERVER_MessageCallback (or its respective continuations).
282 * @param client client we were processing a message of
283 * @param success #GNUNET_OK to keep the connection open and
284 * continue to receive
285 * #GNUNET_NO to close the connection (normal behavior)
286 * #GNUNET_SYSERR to close the connection (signal
290 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
295 * Change the timeout for a particular client. Decreasing the timeout
296 * may not go into effect immediately (only after the previous timeout
297 * times out or activity happens on the socket).
299 * @param client the client to update
300 * @param timeout new timeout for activities on the socket
303 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
304 struct GNUNET_TIME_Relative timeout);
308 * Return user context associated with the given client.
309 * Note: you should probably use the macro (call without the underscore).
311 * @param client client to query
312 * @param size number of bytes in user context struct (for verification only)
313 * @return pointer to user context
316 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
321 * Set user context to be associated with the given client.
322 * Note: you should probably use the macro (call without the underscore).
324 * @param client client to query
325 * @param ptr pointer to user context
326 * @param size number of bytes in user context struct (for verification only)
329 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
335 * Return user context associated with the given client.
337 * @param client client to query
338 * @param type expected return type (i.e. 'struct Foo')
339 * @return pointer to user context of type 'type *'.
341 #define GNUNET_SERVER_client_get_user_context(client,type) \
342 (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) \
351 GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
355 * Disable the warning the server issues if a message is not acknowledged
356 * in a timely fashion. Use this call if a client is intentionally delayed
357 * for a while. Only applies to the current message.
359 * @param client client for which to disable the warning
362 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
367 * Inject a message into the server, pretend it came
368 * from the specified client. Delivery of the message
369 * will happen instantly (if a handler is installed;
370 * otherwise the call does nothing).
372 * @param server the server receiving the message
373 * @param sender the "pretended" sender of the message
375 * @param message message to transmit
376 * @return #GNUNET_OK if the message was OK and the
377 * connection can stay open
378 * #GNUNET_SYSERR if the connection to the
379 * client should be shut down
382 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
383 struct GNUNET_SERVER_Client *sender,
384 const struct GNUNET_MessageHeader *message);
388 * Add a TCP socket-based connection to the set of handles managed by
389 * this server. Use this function for outgoing (P2P) connections that
390 * we initiated (and where this server should process incoming
393 * @param server the server to use
394 * @param connection the connection to manage (client must
395 * stop using this connection from now on)
396 * @return the client handle (client should call
397 * #GNUNET_SERVER_client_drop on the return value eventually)
399 struct GNUNET_SERVER_Client *
400 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
401 struct GNUNET_CONNECTION_Handle *connection);
405 * Notify the server that the given client handle should
406 * be kept (keeps the connection up if possible, increments
407 * the internal reference counter).
409 * @param client the client to keep
412 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
416 * Notify the server that the given client handle is no
417 * longer required. Decrements the reference counter. If
418 * that counter reaches zero an inactive connection maybe
421 * @param client the client to drop
424 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
428 * Obtain the network address of the other party.
430 * @param client the client to get the address for
431 * @param addr where to store the address
432 * @param addrlen where to store the length of @a addr
433 * @return #GNUNET_OK on success
436 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
437 void **addr, size_t *addrlen);
441 * Functions with this signature are called whenever a client
442 * is disconnected on the network level.
445 * @param client identification of the client; NULL
446 * for the last call when the server is destroyed
448 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
449 struct GNUNET_SERVER_Client *client);
453 * Functions with this signature are called whenever a client
454 * is connected on the network level.
457 * @param client identification of the client
459 typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
460 struct GNUNET_SERVER_Client *client);
464 * Ask the server to notify us whenever a client disconnects.
465 * This function is called whenever the actual network connection
466 * is closed; the reference count may be zero or larger than zero
467 * at this point. If the server is destroyed before this
468 * notification is explicitly cancelled, the 'callback' will
469 * once be called with a 'client' argument of NULL to indicate
470 * that the server itself is now gone (and that the callback
471 * won't be called anymore and also can no longer be cancelled).
473 * @param server the server manageing the clients
474 * @param callback function to call on disconnect
475 * @param callback_cls closure for @a callback
478 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
479 GNUNET_SERVER_DisconnectCallback callback,
484 * Ask the server to notify us whenever a client connects.
485 * This function is called whenever the actual network connection
486 * is opened. If the server is destroyed before this
487 * notification is explicitly cancelled, the @a callback will
488 * once be called with a 'client' argument of NULL to indicate
489 * that the server itself is now gone (and that the callback
490 * won't be called anymore and also can no longer be cancelled).
492 * @param server the server manageing the clients
493 * @param callback function to call on sconnect
494 * @param callback_cls closure for @a callback
497 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
498 GNUNET_SERVER_ConnectCallback callback,
503 * Ask the server to stop notifying us whenever a client disconnects.
504 * Arguments must match exactly those given to
505 * #GNUNET_SERVER_disconnect_notify. It is not necessary to call this
506 * function during shutdown of the server; in fact, most applications
507 * will never use this function.
509 * @param server the server manageing the clients
510 * @param callback function to call on disconnect
511 * @param callback_cls closure for @a callback
514 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
515 GNUNET_SERVER_DisconnectCallback callback,
520 * Ask the server to stop notifying us whenever a client connects.
521 * Arguments must match exactly those given to
522 * #GNUNET_SERVER_connect_notify. It is not necessary to call this
523 * function during shutdown of the server; in fact, most applications
524 * will never use this function.
526 * @param server the server manageing the clients
527 * @param callback function to call on connect
528 * @param callback_cls closure for @a callback
531 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
532 GNUNET_SERVER_ConnectCallback callback,
537 * Ask the server to disconnect from the given client. This is the
538 * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
539 * except that it allows dropping of a client even when not handling a
540 * message from that client.
542 * @param client the client to disconnect from
545 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
549 * Disable the "CORK" feature for communication with the given client,
550 * forcing the OS to immediately flush the buffer on transmission
551 * instead of potentially buffering multiple messages.
553 * @param client handle to the client
554 * @return #GNUNET_OK on success
557 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
561 * The tansmit context is the key datastructure for a conveniance API
562 * used for transmission of complex results to the client followed
563 * ONLY by signaling receive_done with success or error
565 struct GNUNET_SERVER_TransmitContext;
569 * Create a new transmission context for the given client.
571 * @param client client to create the context for.
572 * @return NULL on error
574 struct GNUNET_SERVER_TransmitContext *
575 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
579 * Append a message to the transmission context.
580 * All messages in the context will be sent by
581 * the #GNUNET_SERVER_transmit_context_run method.
583 * @param tc context to use
584 * @param data what to append to the result message
585 * @param length length of @a data
586 * @param type type of the message
589 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext *tc,
591 size_t length, uint16_t type);
595 * Append a message to the transmission context.
596 * All messages in the context will be sent by
597 * the transmit_context_run method.
599 * @param tc context to use
600 * @param msg message to append
603 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext *tc,
604 const struct GNUNET_MessageHeader *msg);
608 * Execute a transmission context. If there is an error in the
609 * transmission, the receive_done method will be called with an error
610 * code (#GNUNET_SYSERR), otherwise with #GNUNET_OK.
612 * @param tc transmission context to use
613 * @param timeout when to time out and abort the transmission
616 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
617 struct GNUNET_TIME_Relative timeout);
621 * Destroy a transmission context. This function must not be called
622 * after #GNUNET_SERVER_transmit_context_run.
624 * @param tc transmission context to destroy
625 * @param success code to give to #GNUNET_SERVER_receive_done for
626 * the client: #GNUNET_OK to keep the connection open and
627 * continue to receive
628 * #GNUNET_NO to close the connection (normal behavior)
629 * #GNUNET_SYSERR to close the connection (signal
633 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext *tc,
638 * The notification context is the key datastructure for a conveniance
639 * API used for transmission of notifications to the client until the
640 * client disconnects or is disconnected (or the notification context
641 * is destroyed, in which case we disconnect these clients).
642 * Essentially, all (notification) messages are queued up until the
643 * client is able to read them.
645 struct GNUNET_SERVER_NotificationContext;
649 * Create a new notification context.
651 * @param server server for which this function creates the context
652 * @param queue_length maximum number of messages to keep in
653 * the notification queue; optional messages are dropped
654 * if the queue gets longer than this number of messages
655 * @return handle to the notification context
657 struct GNUNET_SERVER_NotificationContext *
658 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
659 unsigned int queue_length);
663 * Destroy the context, force disconnect for all clients.
665 * @param nc context to destroy.
668 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
672 * Add a client to the notification context.
674 * @param nc context to modify
675 * @param client client to add
678 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
679 struct GNUNET_SERVER_Client *client);
683 * Send a message to a particular client; must have
684 * already been added to the notification context.
686 * @param nc context to modify
687 * @param client client to transmit to
688 * @param msg message to send
689 * @param can_drop can this message be dropped due to queue length limitations
692 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
693 struct GNUNET_SERVER_Client *client,
694 const struct GNUNET_MessageHeader *msg,
699 * Send a message to all clients of this context.
701 * @param nc context to modify
702 * @param msg message to send
703 * @param can_drop can this message be dropped due to queue length limitations
706 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
707 const struct GNUNET_MessageHeader *msg,
712 * Handle to a message stream tokenizer.
714 struct GNUNET_SERVER_MessageStreamTokenizer;
718 * Functions with this signature are called whenever a
719 * complete message is received by the tokenizer.
721 * Do not call #GNUNET_SERVER_mst_destroy from within
722 * the scope of this callback.
725 * @param client identification of the client
726 * @param message the actual message
727 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
729 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
730 const struct GNUNET_MessageHeader *message);
734 * Create a message stream tokenizer.
736 * @param cb function to call on completed messages
737 * @param cb_cls closure for @a cb
738 * @return handle to tokenizer
740 struct GNUNET_SERVER_MessageStreamTokenizer *
741 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
746 * Add incoming data to the receive buffer and call the
747 * callback for all complete messages.
749 * @param mst tokenizer to use
750 * @param client_identity ID of client for which this is a buffer,
751 * can be NULL (will be passed back to 'cb')
752 * @param buf input data to add
753 * @param size number of bytes in @a buf
754 * @param purge should any excess bytes in the buffer be discarded
755 * (i.e. for packet-based services like UDP)
756 * @param one_shot only call callback once, keep rest of message in buffer
757 * @return #GNUNET_OK if we are done processing (need more data)
758 * #GNUNET_NO if one_shot was set and we have another message ready
759 * #GNUNET_SYSERR if the data stream is corrupt
762 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
763 void *client_identity,
764 const char *buf, size_t size,
765 int purge, int one_shot);
769 * Destroys a tokenizer.
771 * @param mst tokenizer to destroy
774 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
778 * Signature of a function to create a custom tokenizer.
780 * @param cls closure from #GNUNET_SERVER_set_callbacks
781 * @param client handle to client the tokenzier will be used for
782 * @return handle to custom tokenizer ('mst')
784 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
785 struct GNUNET_SERVER_Client *client);
789 * Signature of a function to destroy a custom tokenizer.
791 * @param cls closure from #GNUNET_SERVER_set_callbacks
792 * @param mst custom tokenizer handle
794 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
798 * Signature of a function to receive data for a custom tokenizer.
800 * @param cls closure from #GNUNET_SERVER_set_callbacks
801 * @param mst custom tokenizer handle
802 * @param client_identity ID of client for which this is a buffer,
803 * can be NULL (will be passed back to 'cb')
804 * @param buf input data to add
805 * @param size number of bytes in @a buf
806 * @param purge should any excess bytes in the buffer be discarded
807 * (i.e. for packet-based services like UDP)
808 * @param one_shot only call callback once, keep rest of message in buffer
809 * @return #GNUNET_OK if we are done processing (need more data)
810 * #GNUNET_NO if one_shot was set and we have another message ready
811 * #GNUNET_SYSERR if the data stream is corrupt
813 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
814 struct GNUNET_SERVER_Client *client,
815 const char *buf, size_t size,
816 int purge, int one_shot);
820 * Change functions used by the server to tokenize the message stream.
821 * (very rarely used).
823 * @param server server to modify
824 * @param create new tokenizer initialization function
825 * @param destroy new tokenizer destruction function
826 * @param receive new tokenizer receive function
827 * @param cls closure for @a create, @a receive and @a destroy
830 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
831 GNUNET_SERVER_MstCreateCallback create,
832 GNUNET_SERVER_MstDestroyCallback destroy,
833 GNUNET_SERVER_MstReceiveCallback receive,
837 #if 0 /* keep Emacsens' auto-indent happy */
844 /** @} */ /* end of group server */
846 /* ifndef GNUNET_SERVER_LIB_H */
848 /* end of gnunet_server_lib.h */