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 * Free resources held by this server.
160 * @param s server to destroy
163 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
167 * Add additional handlers to an existing server.
169 * @param server the server to add handlers to
170 * @param handlers array of message handlers for
171 * incoming messages; the last entry must
172 * have "NULL" for the "callback"; multiple
173 * entries for the same type are allowed,
174 * they will be called in order of occurence.
175 * These handlers can be removed later;
176 * the handlers array must exist until removed
177 * (or server is destroyed).
180 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
181 const struct GNUNET_SERVER_MessageHandler
186 * Notify us when the server has enough space to transmit
187 * a message of the given size to the given client.
189 * @param client client to transmit message to
190 * @param size requested amount of buffer space
191 * @param timeout after how long should we give up (and call
192 * notify with buf NULL and size 0)?
193 * @param callback function to call when space is available
194 * @param callback_cls closure for callback
195 * @return non-NULL if the notify callback was queued; can be used
196 * to cancel the request using
197 * GNUNET_SERVER_notify_transmit_ready_cancel.
198 * NULL if we are already going to notify someone else (busy)
200 struct GNUNET_SERVER_TransmitHandle *
201 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
203 struct GNUNET_TIME_Relative timeout,
204 GNUNET_CONNECTION_TransmitReadyNotify
205 callback, void *callback_cls);
209 * Abort transmission request.
211 * @param th request to abort
214 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
218 * Set the persistent flag on this client, used to setup client connection
219 * to only be killed when the service it's connected to is actually dead.
221 * @param client the client to set the persistent flag on
224 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
227 * Resume receiving from this client, we are done processing the
228 * current request. This function must be called from within each
229 * GNUNET_SERVER_MessageCallback (or its respective continuations).
231 * @param client client we were processing a message of
232 * @param success GNUNET_OK to keep the connection open and
233 * continue to receive
234 * GNUNET_NO to close the connection (normal behavior)
235 * GNUNET_SYSERR to close the connection (signal
239 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
243 * Change the timeout for a particular client. Decreasing the timeout
244 * may not go into effect immediately (only after the previous timeout
245 * times out or activity happens on the socket).
247 * @param client the client to update
248 * @param timeout new timeout for activities on the socket
251 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
252 struct GNUNET_TIME_Relative timeout);
256 * Disable the warning the server issues if a message is not acknowledged
257 * in a timely fashion. Use this call if a client is intentionally delayed
258 * for a while. Only applies to the current message.
260 * @param client client for which to disable the warning
263 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
268 * Inject a message into the server, pretend it came
269 * from the specified client. Delivery of the message
270 * will happen instantly (if a handler is installed;
271 * otherwise the call does nothing).
273 * @param server the server receiving the message
274 * @param sender the "pretended" sender of the message
276 * @param message message to transmit
277 * @return GNUNET_OK if the message was OK and the
278 * connection can stay open
279 * GNUNET_SYSERR if the connection to the
280 * client should be shut down
283 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
284 struct GNUNET_SERVER_Client *sender,
285 const struct GNUNET_MessageHeader *message);
289 * Add a TCP socket-based connection to the set of handles managed by
290 * this server. Use this function for outgoing (P2P) connections that
291 * we initiated (and where this server should process incoming
294 * @param server the server to use
295 * @param connection the connection to manage (client must
296 * stop using this connection from now on)
297 * @return the client handle (client should call
298 * "client_drop" on the return value eventually)
300 struct GNUNET_SERVER_Client *
301 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
302 struct GNUNET_CONNECTION_Handle *connection);
306 * Notify the server that the given client handle should
307 * be kept (keeps the connection up if possible, increments
308 * the internal reference counter).
310 * @param client the client to keep
313 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
317 * Notify the server that the given client handle is no
318 * longer required. Decrements the reference counter. If
319 * that counter reaches zero an inactive connection maybe
322 * @param client the client to drop
325 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
329 * Obtain the network address of the other party.
331 * @param client the client to get the address for
332 * @param addr where to store the address
333 * @param addrlen where to store the length of the address
334 * @return GNUNET_OK on success
337 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
338 void **addr, size_t * addrlen);
342 * Functions with this signature are called whenever a client
343 * is disconnected on the network level.
346 * @param client identification of the client; NULL
347 * for the last call when the server is destroyed
349 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
350 struct GNUNET_SERVER_Client *
355 * Ask the server to notify us whenever a client disconnects.
356 * This function is called whenever the actual network connection
357 * is closed; the reference count may be zero or larger than zero
358 * at this point. If the server is destroyed before this
359 * notification is explicitly cancelled, the 'callback' will
360 * once be called with a 'client' argument of NULL to indicate
361 * that the server itself is now gone (and that the callback
362 * won't be called anymore and also can no longer be cancelled).
364 * @param server the server manageing the clients
365 * @param callback function to call on disconnect
366 * @param callback_cls closure for callback
369 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
370 GNUNET_SERVER_DisconnectCallback callback,
375 * Ask the server to stop notifying us whenever a client disconnects.
377 * @param server the server manageing the clients
378 * @param callback function to call on disconnect
379 * @param callback_cls closure for callback
382 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
383 GNUNET_SERVER_DisconnectCallback
384 callback, void *callback_cls);
388 * Ask the server to disconnect from the given client.
389 * This is the same as returning GNUNET_SYSERR from a message
390 * handler, except that it allows dropping of a client even
391 * when not handling a message from that client.
393 * @param client the client to disconnect from
396 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
400 * Configure this server's connections to continue handling client
401 * requests as usual even after we get a shutdown signal. The change
402 * only applies to clients that connect to the server from the outside
403 * using TCP after this call. Clients managed previously or those
404 * added using GNUNET_SERVER_connect_socket and
405 * GNUNET_SERVER_connect_callback are not affected by this option.
407 * @param h server handle
408 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
411 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore);
416 * Disable the "CORK" feature for communication with the given client,
417 * forcing the OS to immediately flush the buffer on transmission
418 * instead of potentially buffering multiple messages.
420 * @param client handle to the client
421 * @return GNUNET_OK on success
424 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
428 * The tansmit context is the key datastructure for a conveniance API
429 * used for transmission of complex results to the client followed
430 * ONLY by signaling receive_done with success or error
432 struct GNUNET_SERVER_TransmitContext;
436 * Create a new transmission context for the
439 * @param client client to create the context for.
440 * @return NULL on error
442 struct GNUNET_SERVER_TransmitContext *
443 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
447 * Append a message to the transmission context.
448 * All messages in the context will be sent by
449 * the transmit_context_run method.
451 * @param tc context to use
452 * @param data what to append to the result message
453 * @param length length of data
454 * @param type type of the message
457 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
458 *tc, const void *data,
459 size_t length, uint16_t type);
463 * Append a message to the transmission context.
464 * All messages in the context will be sent by
465 * the transmit_context_run method.
467 * @param tc context to use
468 * @param msg message to append
471 GNUNET_SERVER_transmit_context_append_message (struct
472 GNUNET_SERVER_TransmitContext
474 const struct GNUNET_MessageHeader
479 * Execute a transmission context. If there is an error in the
480 * transmission, the receive_done method will be called with an error
481 * code (GNUNET_SYSERR), otherwise with GNUNET_OK.
483 * @param tc transmission context to use
484 * @param timeout when to time out and abort the transmission
487 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
488 struct GNUNET_TIME_Relative timeout);
492 * Destroy a transmission context. This function must not be called
493 * after 'GNUNET_SERVER_transmit_context_run'.
495 * @param tc transmission context to destroy
496 * @param success code to give to 'GNUNET_SERVER_receive_done' for
497 * the client: GNUNET_OK to keep the connection open and
498 * continue to receive
499 * GNUNET_NO to close the connection (normal behavior)
500 * GNUNET_SYSERR to close the connection (signal
504 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
509 * The notification context is the key datastructure for a conveniance
510 * API used for transmission of notifications to the client until the
511 * client disconnects (or the notification context is destroyed, in
512 * which case we disconnect these clients). Essentially, all
513 * (notification) messages are queued up until the client is able to
516 struct GNUNET_SERVER_NotificationContext;
520 * Create a new notification context.
522 * @param server server for which this function creates the context
523 * @param queue_length maximum number of messages to keep in
524 * the notification queue; optional messages are dropped
525 * if the queue gets longer than this number of messages
526 * @return handle to the notification context
528 struct GNUNET_SERVER_NotificationContext *
529 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
530 unsigned int queue_length);
534 * Destroy the context, force disconnect for all clients.
536 * @param nc context to destroy.
539 GNUNET_SERVER_notification_context_destroy (struct
540 GNUNET_SERVER_NotificationContext
545 * Add a client to the notification context.
547 * @param nc context to modify
548 * @param client client to add
551 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
553 struct GNUNET_SERVER_Client *client);
557 * Send a message to a particular client; must have
558 * already been added to the notification context.
560 * @param nc context to modify
561 * @param client client to transmit to
562 * @param msg message to send
563 * @param can_drop can this message be dropped due to queue length limitations
566 GNUNET_SERVER_notification_context_unicast (struct
567 GNUNET_SERVER_NotificationContext
569 struct GNUNET_SERVER_Client *client,
570 const struct GNUNET_MessageHeader
575 * Send a message to all clients of this context.
577 * @param nc context to modify
578 * @param msg message to send
579 * @param can_drop can this message be dropped due to queue length limitations
582 GNUNET_SERVER_notification_context_broadcast (struct
583 GNUNET_SERVER_NotificationContext
585 const struct GNUNET_MessageHeader
591 * Handle to a message stream tokenizer.
593 struct GNUNET_SERVER_MessageStreamTokenizer;
596 * Functions with this signature are called whenever a
597 * complete message is received by the tokenizer.
600 * @param client identification of the client
601 * @param message the actual message
603 typedef void (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
605 GNUNET_MessageHeader *
610 * Create a message stream tokenizer.
612 * @param cb function to call on completed messages
613 * @param cb_cls closure for cb
614 * @return handle to tokenizer
616 struct GNUNET_SERVER_MessageStreamTokenizer *
617 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
622 * Add incoming data to the receive buffer and call the
623 * callback for all complete messages.
625 * @param mst tokenizer to use
626 * @param client_identity ID of client for which this is a buffer,
627 * can be NULL (will be passed back to 'cb')
628 * @param buf input data to add
629 * @param size number of bytes in buf
630 * @param purge should any excess bytes in the buffer be discarded
631 * (i.e. for packet-based services like UDP)
632 * @param one_shot only call callback once, keep rest of message in buffer
633 * @return GNUNET_OK if we are done processing (need more data)
634 * GNUNET_NO if one_shot was set and we have another message ready
635 * GNUNET_SYSERR if the data stream is corrupt
638 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
639 void *client_identity, const char *buf, size_t size,
640 int purge, int one_shot);
644 * Destroys a tokenizer.
646 * @param mst tokenizer to destroy
649 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
653 * Signature of a function to create a custom tokenizer.
655 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
656 * @param client handle to client the tokenzier will be used for
657 * @return handle to custom tokenizer ('mst')
659 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
660 struct GNUNET_SERVER_Client *client);
663 * Signature of a function to destroy a custom tokenizer.
665 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
666 * @param mst custom tokenizer handle
668 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
671 * Signature of a function to destroy a custom tokenizer.
673 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
674 * @param mst custom tokenizer handle
675 * @param client_identity ID of client for which this is a buffer,
676 * can be NULL (will be passed back to 'cb')
677 * @param buf input data to add
678 * @param size number of bytes in buf
679 * @param purge should any excess bytes in the buffer be discarded
680 * (i.e. for packet-based services like UDP)
681 * @param one_shot only call callback once, keep rest of message in buffer
682 * @return GNUNET_OK if we are done processing (need more data)
683 * GNUNET_NO if one_shot was set and we have another message ready
684 * GNUNET_SYSERR if the data stream is corrupt
686 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
687 struct GNUNET_SERVER_Client *client,
688 const char *buf, size_t size,
689 int purge, int one_shot);
693 * Change functions used by the server to tokenize the message stream.
694 * (very rarely used).
696 * @param server server to modify
697 * @param create new tokenizer initialization function
698 * @param destroy new tokenizer destruction function
699 * @param receive new tokenizer receive function
700 * @param cls closure for 'create', 'receive', 'destroy'
703 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
704 GNUNET_SERVER_MstCreateCallback create,
705 GNUNET_SERVER_MstDestroyCallback destroy,
706 GNUNET_SERVER_MstReceiveCallback receive,
710 #if 0 /* keep Emacsens' auto-indent happy */
718 /* ifndef GNUNET_SERVER_LIB_H */
720 /* end of gnunet_server_lib.h */