2 This file is part of GNUnet.
3 (C) 2009, 2010 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 2, 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
25 * @author Christian Grothoff
28 #ifndef GNUNET_SERVER_LIB_H
29 #define GNUNET_SERVER_LIB_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_common.h"
40 #include "gnunet_connection_lib.h"
44 * Largest supported message.
46 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
49 * Smallest supported message.
51 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
54 * @brief handle for a server
56 struct GNUNET_SERVER_Handle;
59 * @brief opaque handle for a client of the server
61 struct GNUNET_SERVER_Client;
64 * @brief opaque handle server returns for aborting transmission to a client.
66 struct GNUNET_SERVER_TransmitHandle;
70 * Functions with this signature are called whenever a message is
74 * @param client identification of the client
75 * @param message the actual message
77 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
78 struct GNUNET_SERVER_Client *
80 const struct GNUNET_MessageHeader
86 * Message handler. Each struct specifies how to handle on particular
87 * type of message received.
89 struct GNUNET_SERVER_MessageHandler
92 * Function to call for messages of "type".
94 GNUNET_SERVER_MessageCallback callback;
97 * Closure argument for "callback".
102 * Type of the message this handler covers.
107 * Expected size of messages of this type. Use 0 for
108 * variable-size. If non-zero, messages of the given
109 * type will be discarded (and the connection closed)
110 * if they do not have the right size.
112 uint16_t expected_size;
118 * Create a new server.
120 * @param access function for access control
121 * @param access_cls closure for access
122 * @param lsocks NULL-terminated array of listen sockets
123 * @param idle_timeout after how long should we timeout idle connections?
124 * @param require_found if YES, connections sending messages of unknown type
126 * @return handle for the new server, NULL on error
127 * (typically, "port" already in use)
129 struct GNUNET_SERVER_Handle *
130 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
132 struct GNUNET_NETWORK_Handle **lsocks,
133 struct GNUNET_TIME_Relative idle_timeout,
137 * Create a new server.
139 * @param access function for access control
140 * @param access_cls closure for access
141 * @param serverAddr address toes listen on (including port), NULL terminated array
142 * @param socklen lengths of respective serverAddr
143 * @param idle_timeout after how long should we timeout idle connections?
144 * @param require_found if YES, connections sending messages of unknown type
146 * @return handle for the new server, NULL on error
147 * (typically, "port" already in use)
149 struct GNUNET_SERVER_Handle *
150 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
151 struct sockaddr *const *serverAddr,
152 const socklen_t * socklen,
153 struct GNUNET_TIME_Relative idle_timeout,
158 * Suspend accepting connections from the listen socket temporarily.
160 * @param server server to stop accepting connections.
163 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
167 * Resume accepting connections from the listen socket.
169 * @param server server to stop accepting connections.
172 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
176 * Stop the listen socket and get ready to shutdown the server
177 * once only 'monitor' clients are left.
179 * @param server server to stop listening on
182 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
186 * Free resources held by this server.
188 * @param server server to destroy
191 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
195 * Add additional handlers to an existing server.
197 * @param server the server to add handlers to
198 * @param handlers array of message handlers for
199 * incoming messages; the last entry must
200 * have "NULL" for the "callback"; multiple
201 * entries for the same type are allowed,
202 * they will be called in order of occurence.
203 * These handlers can be removed later;
204 * the handlers array must exist until removed
205 * (or server is destroyed).
208 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
209 const struct GNUNET_SERVER_MessageHandler
214 * Notify us when the server has enough space to transmit
215 * a message of the given size to the given client.
217 * @param client client to transmit message to
218 * @param size requested amount of buffer space
219 * @param timeout after how long should we give up (and call
220 * notify with buf NULL and size 0)?
221 * @param callback function to call when space is available
222 * @param callback_cls closure for callback
223 * @return non-NULL if the notify callback was queued; can be used
224 * to cancel the request using
225 * GNUNET_SERVER_notify_transmit_ready_cancel.
226 * NULL if we are already going to notify someone else (busy)
228 struct GNUNET_SERVER_TransmitHandle *
229 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
231 struct GNUNET_TIME_Relative timeout,
232 GNUNET_CONNECTION_TransmitReadyNotify
233 callback, void *callback_cls);
237 * Abort transmission request.
239 * @param th request to abort
242 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
246 * Set the 'monitor' flag on this client. Clients which have been
247 * marked as 'monitors' won't prevent the server from shutting down
248 * once 'GNUNET_SERVER_stop_listening' has been invoked. The idea is
249 * that for "normal" clients we likely want to allow them to process
250 * their requests; however, monitor-clients are likely to 'never'
251 * disconnect during shutdown and thus will not be considered when
252 * determining if the server should continue to exist after
253 * 'GNUNET_SERVER_destroy' has been called.
255 * @param client the client to set the 'monitor' flag on
258 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
262 * Set the persistent flag on this client, used to setup client connection
263 * to only be killed when the service it's connected to is actually dead.
265 * @param client the client to set the persistent flag on
268 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
272 * Resume receiving from this client, we are done processing the
273 * current request. This function must be called from within each
274 * GNUNET_SERVER_MessageCallback (or its respective continuations).
276 * @param client client we were processing a message of
277 * @param success GNUNET_OK to keep the connection open and
278 * continue to receive
279 * GNUNET_NO to close the connection (normal behavior)
280 * GNUNET_SYSERR to close the connection (signal
284 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
288 * Change the timeout for a particular client. Decreasing the timeout
289 * may not go into effect immediately (only after the previous timeout
290 * times out or activity happens on the socket).
292 * @param client the client to update
293 * @param timeout new timeout for activities on the socket
296 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
297 struct GNUNET_TIME_Relative timeout);
301 * Disable the warning the server issues if a message is not acknowledged
302 * in a timely fashion. Use this call if a client is intentionally delayed
303 * for a while. Only applies to the current message.
305 * @param client client for which to disable the warning
308 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
313 * Inject a message into the server, pretend it came
314 * from the specified client. Delivery of the message
315 * will happen instantly (if a handler is installed;
316 * otherwise the call does nothing).
318 * @param server the server receiving the message
319 * @param sender the "pretended" sender of the message
321 * @param message message to transmit
322 * @return GNUNET_OK if the message was OK and the
323 * connection can stay open
324 * GNUNET_SYSERR if the connection to the
325 * client should be shut down
328 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
329 struct GNUNET_SERVER_Client *sender,
330 const struct GNUNET_MessageHeader *message);
334 * Add a TCP socket-based connection to the set of handles managed by
335 * this server. Use this function for outgoing (P2P) connections that
336 * we initiated (and where this server should process incoming
339 * @param server the server to use
340 * @param connection the connection to manage (client must
341 * stop using this connection from now on)
342 * @return the client handle (client should call
343 * "client_drop" on the return value eventually)
345 struct GNUNET_SERVER_Client *
346 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
347 struct GNUNET_CONNECTION_Handle *connection);
351 * Notify the server that the given client handle should
352 * be kept (keeps the connection up if possible, increments
353 * the internal reference counter).
355 * @param client the client to keep
358 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
362 * Notify the server that the given client handle is no
363 * longer required. Decrements the reference counter. If
364 * that counter reaches zero an inactive connection maybe
367 * @param client the client to drop
370 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
374 * Obtain the network address of the other party.
376 * @param client the client to get the address for
377 * @param addr where to store the address
378 * @param addrlen where to store the length of the address
379 * @return GNUNET_OK on success
382 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
383 void **addr, size_t * addrlen);
387 * Functions with this signature are called whenever a client
388 * is disconnected on the network level.
391 * @param client identification of the client; NULL
392 * for the last call when the server is destroyed
394 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
395 struct GNUNET_SERVER_Client *
399 * Functions with this signature are called whenever a client
400 * is connected on the network level.
403 * @param client identification of the client
405 typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
406 struct GNUNET_SERVER_Client *client);
411 * Ask the server to notify us whenever a client disconnects.
412 * This function is called whenever the actual network connection
413 * is closed; the reference count may be zero or larger than zero
414 * at this point. If the server is destroyed before this
415 * notification is explicitly cancelled, the 'callback' will
416 * once be called with a 'client' argument of NULL to indicate
417 * that the server itself is now gone (and that the callback
418 * won't be called anymore and also can no longer be cancelled).
420 * @param server the server manageing the clients
421 * @param callback function to call on disconnect
422 * @param callback_cls closure for callback
425 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
426 GNUNET_SERVER_DisconnectCallback callback,
431 * Ask the server to notify us whenever a client connects.
432 * This function is called whenever the actual network connection
435 * @param server the server manageing the clients
436 * @param callback function to call on sconnect
437 * @param callback_cls closure for callback
440 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
441 GNUNET_SERVER_ConnectCallback callback, void *callback_cls);
445 * Ask the server to stop notifying us whenever a client disconnects.
447 * @param server the server manageing the clients
448 * @param callback function to call on disconnect
449 * @param callback_cls closure for callback
452 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
453 GNUNET_SERVER_DisconnectCallback
454 callback, void *callback_cls);
458 * Ask the server to stop notifying us whenever a client connects.
460 * @param server the server manageing the clients
461 * @param callback function to call on connect
462 * @param callback_cls closure for callback
465 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
466 GNUNET_SERVER_ConnectCallback callback, void *callback_cls);
470 * Ask the server to disconnect from the given client.
471 * This is the same as returning GNUNET_SYSERR from a message
472 * handler, except that it allows dropping of a client even
473 * when not handling a message from that client.
475 * @param client the client to disconnect from
478 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
482 * Disable the "CORK" feature for communication with the given client,
483 * forcing the OS to immediately flush the buffer on transmission
484 * instead of potentially buffering multiple messages.
486 * @param client handle to the client
487 * @return GNUNET_OK on success
490 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
494 * The tansmit context is the key datastructure for a conveniance API
495 * used for transmission of complex results to the client followed
496 * ONLY by signaling receive_done with success or error
498 struct GNUNET_SERVER_TransmitContext;
502 * Create a new transmission context for the
505 * @param client client to create the context for.
506 * @return NULL on error
508 struct GNUNET_SERVER_TransmitContext *
509 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
513 * Append a message to the transmission context.
514 * All messages in the context will be sent by
515 * the transmit_context_run method.
517 * @param tc context to use
518 * @param data what to append to the result message
519 * @param length length of data
520 * @param type type of the message
523 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
524 *tc, const void *data,
525 size_t length, uint16_t type);
529 * Append a message to the transmission context.
530 * All messages in the context will be sent by
531 * the transmit_context_run method.
533 * @param tc context to use
534 * @param msg message to append
537 GNUNET_SERVER_transmit_context_append_message (struct
538 GNUNET_SERVER_TransmitContext
540 const struct GNUNET_MessageHeader
545 * Execute a transmission context. If there is an error in the
546 * transmission, the receive_done method will be called with an error
547 * code (GNUNET_SYSERR), otherwise with GNUNET_OK.
549 * @param tc transmission context to use
550 * @param timeout when to time out and abort the transmission
553 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
554 struct GNUNET_TIME_Relative timeout);
558 * Destroy a transmission context. This function must not be called
559 * after 'GNUNET_SERVER_transmit_context_run'.
561 * @param tc transmission context to destroy
562 * @param success code to give to 'GNUNET_SERVER_receive_done' for
563 * the client: GNUNET_OK to keep the connection open and
564 * continue to receive
565 * GNUNET_NO to close the connection (normal behavior)
566 * GNUNET_SYSERR to close the connection (signal
570 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
575 * The notification context is the key datastructure for a conveniance
576 * API used for transmission of notifications to the client until the
577 * client disconnects or is disconnected (or the notification context
578 * is destroyed, in which case we disconnect these clients).
579 * Essentially, all (notification) messages are queued up until the
580 * client is able to read them.
582 struct GNUNET_SERVER_NotificationContext;
586 * Create a new notification context.
588 * @param server server for which this function creates the context
589 * @param queue_length maximum number of messages to keep in
590 * the notification queue; optional messages are dropped
591 * if the queue gets longer than this number of messages
592 * @return handle to the notification context
594 struct GNUNET_SERVER_NotificationContext *
595 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
596 unsigned int queue_length);
600 * Destroy the context, force disconnect for all clients.
602 * @param nc context to destroy.
605 GNUNET_SERVER_notification_context_destroy (struct
606 GNUNET_SERVER_NotificationContext
611 * Add a client to the notification context.
613 * @param nc context to modify
614 * @param client client to add
617 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
619 struct GNUNET_SERVER_Client *client);
623 * Send a message to a particular client; must have
624 * already been added to the notification context.
626 * @param nc context to modify
627 * @param client client to transmit to
628 * @param msg message to send
629 * @param can_drop can this message be dropped due to queue length limitations
632 GNUNET_SERVER_notification_context_unicast (struct
633 GNUNET_SERVER_NotificationContext
635 struct GNUNET_SERVER_Client *client,
636 const struct GNUNET_MessageHeader
641 * Send a message to all clients of this context.
643 * @param nc context to modify
644 * @param msg message to send
645 * @param can_drop can this message be dropped due to queue length limitations
648 GNUNET_SERVER_notification_context_broadcast (struct
649 GNUNET_SERVER_NotificationContext
651 const struct GNUNET_MessageHeader
657 * Handle to a message stream tokenizer.
659 struct GNUNET_SERVER_MessageStreamTokenizer;
662 * Functions with this signature are called whenever a
663 * complete message is received by the tokenizer.
665 * Do not call GNUNET_SERVER_mst_destroy in callback
668 * @param client identification of the client
669 * @param message the actual message
671 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
673 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
675 GNUNET_MessageHeader *
680 * Create a message stream tokenizer.
682 * @param cb function to call on completed messages
683 * @param cb_cls closure for cb
684 * @return handle to tokenizer
686 struct GNUNET_SERVER_MessageStreamTokenizer *
687 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
692 * Add incoming data to the receive buffer and call the
693 * callback for all complete messages.
695 * @param mst tokenizer to use
696 * @param client_identity ID of client for which this is a buffer,
697 * can be NULL (will be passed back to 'cb')
698 * @param buf input data to add
699 * @param size number of bytes in buf
700 * @param purge should any excess bytes in the buffer be discarded
701 * (i.e. for packet-based services like UDP)
702 * @param one_shot only call callback once, keep rest of message in buffer
703 * @return GNUNET_OK if we are done processing (need more data)
704 * GNUNET_NO if one_shot was set and we have another message ready
705 * GNUNET_SYSERR if the data stream is corrupt
708 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
709 void *client_identity, const char *buf, size_t size,
710 int purge, int one_shot);
714 * Destroys a tokenizer.
716 * @param mst tokenizer to destroy
719 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
723 * Signature of a function to create a custom tokenizer.
725 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
726 * @param client handle to client the tokenzier will be used for
727 * @return handle to custom tokenizer ('mst')
729 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
730 struct GNUNET_SERVER_Client *client);
733 * Signature of a function to destroy a custom tokenizer.
735 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
736 * @param mst custom tokenizer handle
738 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
741 * Signature of a function to destroy a custom tokenizer.
743 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
744 * @param mst custom tokenizer handle
745 * @param client_identity ID of client for which this is a buffer,
746 * can be NULL (will be passed back to 'cb')
747 * @param buf input data to add
748 * @param size number of bytes in buf
749 * @param purge should any excess bytes in the buffer be discarded
750 * (i.e. for packet-based services like UDP)
751 * @param one_shot only call callback once, keep rest of message in buffer
752 * @return GNUNET_OK if we are done processing (need more data)
753 * GNUNET_NO if one_shot was set and we have another message ready
754 * GNUNET_SYSERR if the data stream is corrupt
756 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
757 struct GNUNET_SERVER_Client *client,
758 const char *buf, size_t size,
759 int purge, int one_shot);
763 * Change functions used by the server to tokenize the message stream.
764 * (very rarely used).
766 * @param server server to modify
767 * @param create new tokenizer initialization function
768 * @param destroy new tokenizer destruction function
769 * @param receive new tokenizer receive function
770 * @param cls closure for 'create', 'receive', 'destroy'
773 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
774 GNUNET_SERVER_MstCreateCallback create,
775 GNUNET_SERVER_MstDestroyCallback destroy,
776 GNUNET_SERVER_MstReceiveCallback receive,
780 #if 0 /* keep Emacsens' auto-indent happy */
788 /* ifndef GNUNET_SERVER_LIB_H */
790 /* end of gnunet_server_lib.h */