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;
60 * @brief opaque handle for a client of the server
62 struct GNUNET_SERVER_Client;
66 * Functions with this signature are called whenever a message is
70 * @param client identification of the client
71 * @param message the actual message
73 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
74 struct GNUNET_SERVER_Client *
76 const struct GNUNET_MessageHeader
82 * Message handler. Each struct specifies how to handle on particular
83 * type of message received.
85 struct GNUNET_SERVER_MessageHandler
88 * Function to call for messages of "type".
90 GNUNET_SERVER_MessageCallback callback;
93 * Closure argument for "callback".
98 * Type of the message this handler covers.
103 * Expected size of messages of this type. Use 0 for
104 * variable-size. If non-zero, messages of the given
105 * type will be discarded (and the connection closed)
106 * if they do not have the right size.
108 uint16_t expected_size;
114 * Create a new server.
116 * @param access function for access control
117 * @param access_cls closure for access
118 * @param lsocks NULL-terminated array of listen sockets
119 * @param idle_timeout after how long should we timeout idle connections?
120 * @param require_found if YES, connections sending messages of unknown type
122 * @return handle for the new server, NULL on error
123 * (typically, "port" already in use)
125 struct GNUNET_SERVER_Handle *
126 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
128 struct GNUNET_NETWORK_Handle **lsocks,
129 struct GNUNET_TIME_Relative idle_timeout,
133 * Create a new server.
135 * @param access function for access control
136 * @param access_cls closure for access
137 * @param serverAddr address toes listen on (including port), NULL terminated array
138 * @param socklen lengths of respective serverAddr
139 * @param idle_timeout after how long should we timeout idle connections?
140 * @param require_found if YES, connections sending messages of unknown type
142 * @return handle for the new server, NULL on error
143 * (typically, "port" already in use)
145 struct GNUNET_SERVER_Handle *
146 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
147 struct sockaddr *const *serverAddr,
148 const socklen_t * socklen,
149 struct GNUNET_TIME_Relative idle_timeout,
154 * Free resources held by this server.
156 * @param s server to destroy
159 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
163 * Add additional handlers to an existing server.
165 * @param server the server to add handlers to
166 * @param handlers array of message handlers for
167 * incoming messages; the last entry must
168 * have "NULL" for the "callback"; multiple
169 * entries for the same type are allowed,
170 * they will be called in order of occurence.
171 * These handlers can be removed later;
172 * the handlers array must exist until removed
173 * (or server is destroyed).
176 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
177 const struct GNUNET_SERVER_MessageHandler
182 * Notify us when the server has enough space to transmit
183 * a message of the given size to the given client.
185 * @param client client to transmit message to
186 * @param size requested amount of buffer space
187 * @param timeout after how long should we give up (and call
188 * notify with buf NULL and size 0)?
189 * @param callback function to call when space is available
190 * @param callback_cls closure for callback
191 * @return non-NULL if the notify callback was queued; can be used
192 * to cancel the request using
193 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
194 * NULL if we are already going to notify someone else (busy)
196 struct GNUNET_CONNECTION_TransmitHandle *
197 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
199 struct GNUNET_TIME_Relative timeout,
200 GNUNET_CONNECTION_TransmitReadyNotify
201 callback, void *callback_cls);
205 * Set the persistent flag on this client, used to setup client connection
206 * to only be killed when the service it's connected to is actually dead.
208 * @param client the client to set the persistent flag on
211 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
214 * Resume receiving from this client, we are done processing the
215 * current request. This function must be called from within each
216 * GNUNET_SERVER_MessageCallback (or its respective continuations).
218 * @param client client we were processing a message of
219 * @param success GNUNET_OK to keep the connection open and
220 * continue to receive
221 * GNUNET_NO to close the connection (normal behavior)
222 * GNUNET_SYSERR to close the connection (signal
226 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
230 * Change the timeout for a particular client. Decreasing the timeout
231 * may not go into effect immediately (only after the previous timeout
232 * times out or activity happens on the socket).
234 * @param client the client to update
235 * @param timeout new timeout for activities on the socket
238 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
239 struct GNUNET_TIME_Relative timeout);
243 * Set if a client should finish a pending write when disconnecting.
246 GNUNET_SERVER_client_set_finish_pending_write (struct GNUNET_SERVER_Client *client,
251 * Disable the warning the server issues if a message is not acknowledged
252 * in a timely fashion. Use this call if a client is intentionally delayed
253 * for a while. Only applies to the current message.
255 * @param client client for which to disable the warning
258 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
263 * Inject a message into the server, pretend it came
264 * from the specified client. Delivery of the message
265 * will happen instantly (if a handler is installed;
266 * otherwise the call does nothing).
268 * @param server the server receiving the message
269 * @param sender the "pretended" sender of the message
271 * @param message message to transmit
272 * @return GNUNET_OK if the message was OK and the
273 * connection can stay open
274 * GNUNET_SYSERR if the connection to the
275 * client should be shut down
278 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
279 struct GNUNET_SERVER_Client *sender,
280 const struct GNUNET_MessageHeader *message);
284 * Add a TCP socket-based connection to the set of handles managed by
285 * this server. Use this function for outgoing (P2P) connections that
286 * we initiated (and where this server should process incoming
289 * @param server the server to use
290 * @param connection the connection to manage (client must
291 * stop using this connection from now on)
292 * @return the client handle (client should call
293 * "client_drop" on the return value eventually)
295 struct GNUNET_SERVER_Client *
296 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
297 struct GNUNET_CONNECTION_Handle *connection);
301 * Notify the server that the given client handle should
302 * be kept (keeps the connection up if possible, increments
303 * the internal reference counter).
305 * @param client the client to keep
308 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
312 * Notify the server that the given client handle is no
313 * longer required. Decrements the reference counter. If
314 * that counter reaches zero an inactive connection maybe
317 * @param client the client to drop
320 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
324 * Obtain the network address of the other party.
326 * @param client the client to get the address for
327 * @param addr where to store the address
328 * @param addrlen where to store the length of the address
329 * @return GNUNET_OK on success
332 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
333 void **addr, size_t * addrlen);
337 * Functions with this signature are called whenever a client
338 * is disconnected on the network level.
341 * @param client identification of the client; NULL
342 * for the last call when the server is destroyed
344 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
345 struct GNUNET_SERVER_Client *
350 * Ask the server to notify us whenever a client disconnects.
351 * This function is called whenever the actual network connection
352 * is closed; the reference count may be zero or larger than zero
353 * at this point. If the server is destroyed before this
354 * notification is explicitly cancelled, the 'callback' will
355 * once be called with a 'client' argument of NULL to indicate
356 * that the server itself is now gone (and that the callback
357 * won't be called anymore and also can no longer be cancelled).
359 * @param server the server manageing the clients
360 * @param callback function to call on disconnect
361 * @param callback_cls closure for callback
364 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
365 GNUNET_SERVER_DisconnectCallback callback,
370 * Ask the server to stop notifying us whenever a client disconnects.
372 * @param server the server manageing the clients
373 * @param callback function to call on disconnect
374 * @param callback_cls closure for callback
377 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
378 GNUNET_SERVER_DisconnectCallback
379 callback, void *callback_cls);
383 * Ask the server to disconnect from the given client.
384 * This is the same as returning GNUNET_SYSERR from a message
385 * handler, except that it allows dropping of a client even
386 * when not handling a message from that client.
388 * @param client the client to disconnect from
391 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
395 * Configure this server's connections to continue handling client
396 * requests as usual even after we get a shutdown signal. The change
397 * only applies to clients that connect to the server from the outside
398 * using TCP after this call. Clients managed previously or those
399 * added using GNUNET_SERVER_connect_socket and
400 * GNUNET_SERVER_connect_callback are not affected by this option.
402 * @param h server handle
403 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
406 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore);
411 * Disable the "CORK" feature for communication with the given client,
412 * forcing the OS to immediately flush the buffer on transmission
413 * instead of potentially buffering multiple messages.
415 * @param client handle to the client
416 * @return GNUNET_OK on success
419 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
423 * The tansmit context is the key datastructure for a conveniance API
424 * used for transmission of complex results to the client followed
425 * ONLY by signaling receive_done with success or error
427 struct GNUNET_SERVER_TransmitContext;
431 * Create a new transmission context for the
434 * @param client client to create the context for.
435 * @return NULL on error
437 struct GNUNET_SERVER_TransmitContext *
438 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
442 * Append a message to the transmission context.
443 * All messages in the context will be sent by
444 * the transmit_context_run method.
446 * @param tc context to use
447 * @param data what to append to the result message
448 * @param length length of data
449 * @param type type of the message
452 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
453 *tc, const void *data,
454 size_t length, uint16_t type);
458 * Append a message to the transmission context.
459 * All messages in the context will be sent by
460 * the transmit_context_run method.
462 * @param tc context to use
463 * @param msg message to append
466 GNUNET_SERVER_transmit_context_append_message (struct
467 GNUNET_SERVER_TransmitContext
469 const struct GNUNET_MessageHeader
474 * Execute a transmission context. If there is an error in the
475 * transmission, the receive_done method will be called with an error
476 * code (GNUNET_SYSERR), otherwise with GNUNET_OK.
478 * @param tc transmission context to use
479 * @param timeout when to time out and abort the transmission
482 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
483 struct GNUNET_TIME_Relative timeout);
487 * Destroy a transmission context. This function must not be called
488 * after 'GNUNET_SERVER_transmit_context_run'.
490 * @param tc transmission context to destroy
491 * @param success code to give to 'GNUNET_SERVER_receive_done' for
492 * the client: GNUNET_OK to keep the connection open and
493 * continue to receive
494 * GNUNET_NO to close the connection (normal behavior)
495 * GNUNET_SYSERR to close the connection (signal
499 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
504 * The notification context is the key datastructure for a conveniance
505 * API used for transmission of notifications to the client until the
506 * client disconnects (or the notification context is destroyed, in
507 * which case we disconnect these clients). Essentially, all
508 * (notification) messages are queued up until the client is able to
511 struct GNUNET_SERVER_NotificationContext;
515 * Create a new notification context.
517 * @param server server for which this function creates the context
518 * @param queue_length maximum number of messages to keep in
519 * the notification queue; optional messages are dropped
520 * if the queue gets longer than this number of messages
521 * @return handle to the notification context
523 struct GNUNET_SERVER_NotificationContext *
524 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
525 unsigned int queue_length);
529 * Destroy the context, force disconnect for all clients.
531 * @param nc context to destroy.
534 GNUNET_SERVER_notification_context_destroy (struct
535 GNUNET_SERVER_NotificationContext
540 * Add a client to the notification context.
542 * @param nc context to modify
543 * @param client client to add
546 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
548 struct GNUNET_SERVER_Client *client);
552 * Send a message to a particular client; must have
553 * already been added to the notification context.
555 * @param nc context to modify
556 * @param client client to transmit to
557 * @param msg message to send
558 * @param can_drop can this message be dropped due to queue length limitations
561 GNUNET_SERVER_notification_context_unicast (struct
562 GNUNET_SERVER_NotificationContext
564 struct GNUNET_SERVER_Client *client,
565 const struct GNUNET_MessageHeader
570 * Send a message to all clients of this context.
572 * @param nc context to modify
573 * @param msg message to send
574 * @param can_drop can this message be dropped due to queue length limitations
577 GNUNET_SERVER_notification_context_broadcast (struct
578 GNUNET_SERVER_NotificationContext
580 const struct GNUNET_MessageHeader
586 * Handle to a message stream tokenizer.
588 struct GNUNET_SERVER_MessageStreamTokenizer;
591 * Functions with this signature are called whenever a
592 * complete message is received by the tokenizer.
595 * @param client identification of the client
596 * @param message the actual message
598 typedef void (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
600 GNUNET_MessageHeader *
605 * Create a message stream tokenizer.
607 * @param cb function to call on completed messages
608 * @param cb_cls closure for cb
609 * @return handle to tokenizer
611 struct GNUNET_SERVER_MessageStreamTokenizer *
612 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
617 * Add incoming data to the receive buffer and call the
618 * callback for all complete messages.
620 * @param mst tokenizer to use
621 * @param client_identity ID of client for which this is a buffer,
622 * can be NULL (will be passed back to 'cb')
623 * @param buf input data to add
624 * @param size number of bytes in buf
625 * @param purge should any excess bytes in the buffer be discarded
626 * (i.e. for packet-based services like UDP)
627 * @param one_shot only call callback once, keep rest of message in buffer
628 * @return GNUNET_OK if we are done processing (need more data)
629 * GNUNET_NO if one_shot was set and we have another message ready
630 * GNUNET_SYSERR if the data stream is corrupt
633 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
634 void *client_identity, const char *buf, size_t size,
635 int purge, int one_shot);
639 * Destroys a tokenizer.
641 * @param mst tokenizer to destroy
644 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
648 * Signature of a function to create a custom tokenizer.
650 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
651 * @param client handle to client the tokenzier will be used for
652 * @return handle to custom tokenizer ('mst')
654 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
655 struct GNUNET_SERVER_Client *client);
658 * Signature of a function to destroy a custom tokenizer.
660 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
661 * @param mst custom tokenizer handle
663 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
666 * Signature of a function to destroy a custom tokenizer.
668 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
669 * @param mst custom tokenizer handle
670 * @param client_identity ID of client for which this is a buffer,
671 * can be NULL (will be passed back to 'cb')
672 * @param buf input data to add
673 * @param size number of bytes in buf
674 * @param purge should any excess bytes in the buffer be discarded
675 * (i.e. for packet-based services like UDP)
676 * @param one_shot only call callback once, keep rest of message in buffer
677 * @return GNUNET_OK if we are done processing (need more data)
678 * GNUNET_NO if one_shot was set and we have another message ready
679 * GNUNET_SYSERR if the data stream is corrupt
681 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
682 struct GNUNET_SERVER_Client *client,
683 const char *buf, size_t size,
684 int purge, int one_shot);
688 * Change functions used by the server to tokenize the message stream.
689 * (very rarely used).
691 * @param server server to modify
692 * @param create new tokenizer initialization function
693 * @param destroy new tokenizer destruction function
694 * @param receive new tokenizer receive function
695 * @param cls closure for 'create', 'receive', 'destroy'
698 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
699 GNUNET_SERVER_MstCreateCallback create,
700 GNUNET_SERVER_MstDestroyCallback destroy,
701 GNUNET_SERVER_MstReceiveCallback receive,
705 #if 0 /* keep Emacsens' auto-indent happy */
713 /* ifndef GNUNET_SERVER_LIB_H */
715 /* end of gnunet_server_lib.h */