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 * Stop the listen socket and get ready to shutdown the server
159 * once only 'monitor' clients are left.
161 * @param server server to stop listening on
164 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
168 * Free resources held by this server.
170 * @param server server to destroy
173 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
177 * Add additional handlers to an existing server.
179 * @param server the server to add handlers to
180 * @param handlers array of message handlers for
181 * incoming messages; the last entry must
182 * have "NULL" for the "callback"; multiple
183 * entries for the same type are allowed,
184 * they will be called in order of occurence.
185 * These handlers can be removed later;
186 * the handlers array must exist until removed
187 * (or server is destroyed).
190 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
191 const struct GNUNET_SERVER_MessageHandler
196 * Notify us when the server has enough space to transmit
197 * a message of the given size to the given client.
199 * @param client client to transmit message to
200 * @param size requested amount of buffer space
201 * @param timeout after how long should we give up (and call
202 * notify with buf NULL and size 0)?
203 * @param callback function to call when space is available
204 * @param callback_cls closure for callback
205 * @return non-NULL if the notify callback was queued; can be used
206 * to cancel the request using
207 * GNUNET_SERVER_notify_transmit_ready_cancel.
208 * NULL if we are already going to notify someone else (busy)
210 struct GNUNET_SERVER_TransmitHandle *
211 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
213 struct GNUNET_TIME_Relative timeout,
214 GNUNET_CONNECTION_TransmitReadyNotify
215 callback, void *callback_cls);
219 * Abort transmission request.
221 * @param th request to abort
224 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
228 * Set the 'monitor' flag on this client. Clients which have been
229 * marked as 'monitors' won't prevent the server from shutting down
230 * once 'GNUNET_SERVER_stop_listening' has been invoked. The idea is
231 * that for "normal" clients we likely want to allow them to process
232 * their requests; however, monitor-clients are likely to 'never'
233 * disconnect during shutdown and thus will not be considered when
234 * determining if the server should continue to exist after
235 * 'GNUNET_SERVER_destroy' has been called.
237 * @param client the client to set the 'monitor' flag on
240 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
244 * Set the persistent flag on this client, used to setup client connection
245 * to only be killed when the service it's connected to is actually dead.
247 * @param client the client to set the persistent flag on
250 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
254 * Resume receiving from this client, we are done processing the
255 * current request. This function must be called from within each
256 * GNUNET_SERVER_MessageCallback (or its respective continuations).
258 * @param client client we were processing a message of
259 * @param success GNUNET_OK to keep the connection open and
260 * continue to receive
261 * GNUNET_NO to close the connection (normal behavior)
262 * GNUNET_SYSERR to close the connection (signal
266 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
270 * Change the timeout for a particular client. Decreasing the timeout
271 * may not go into effect immediately (only after the previous timeout
272 * times out or activity happens on the socket).
274 * @param client the client to update
275 * @param timeout new timeout for activities on the socket
278 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
279 struct GNUNET_TIME_Relative timeout);
283 * Disable the warning the server issues if a message is not acknowledged
284 * in a timely fashion. Use this call if a client is intentionally delayed
285 * for a while. Only applies to the current message.
287 * @param client client for which to disable the warning
290 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
295 * Inject a message into the server, pretend it came
296 * from the specified client. Delivery of the message
297 * will happen instantly (if a handler is installed;
298 * otherwise the call does nothing).
300 * @param server the server receiving the message
301 * @param sender the "pretended" sender of the message
303 * @param message message to transmit
304 * @return GNUNET_OK if the message was OK and the
305 * connection can stay open
306 * GNUNET_SYSERR if the connection to the
307 * client should be shut down
310 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
311 struct GNUNET_SERVER_Client *sender,
312 const struct GNUNET_MessageHeader *message);
316 * Add a TCP socket-based connection to the set of handles managed by
317 * this server. Use this function for outgoing (P2P) connections that
318 * we initiated (and where this server should process incoming
321 * @param server the server to use
322 * @param connection the connection to manage (client must
323 * stop using this connection from now on)
324 * @return the client handle (client should call
325 * "client_drop" on the return value eventually)
327 struct GNUNET_SERVER_Client *
328 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
329 struct GNUNET_CONNECTION_Handle *connection);
333 * Notify the server that the given client handle should
334 * be kept (keeps the connection up if possible, increments
335 * the internal reference counter).
337 * @param client the client to keep
340 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
344 * Notify the server that the given client handle is no
345 * longer required. Decrements the reference counter. If
346 * that counter reaches zero an inactive connection maybe
349 * @param client the client to drop
352 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
356 * Obtain the network address of the other party.
358 * @param client the client to get the address for
359 * @param addr where to store the address
360 * @param addrlen where to store the length of the address
361 * @return GNUNET_OK on success
364 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
365 void **addr, size_t * addrlen);
369 * Functions with this signature are called whenever a client
370 * is disconnected on the network level.
373 * @param client identification of the client; NULL
374 * for the last call when the server is destroyed
376 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
377 struct GNUNET_SERVER_Client *
382 * Ask the server to notify us whenever a client disconnects.
383 * This function is called whenever the actual network connection
384 * is closed; the reference count may be zero or larger than zero
385 * at this point. If the server is destroyed before this
386 * notification is explicitly cancelled, the 'callback' will
387 * once be called with a 'client' argument of NULL to indicate
388 * that the server itself is now gone (and that the callback
389 * won't be called anymore and also can no longer be cancelled).
391 * @param server the server manageing the clients
392 * @param callback function to call on disconnect
393 * @param callback_cls closure for callback
396 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
397 GNUNET_SERVER_DisconnectCallback callback,
402 * Ask the server to stop notifying us whenever a client disconnects.
404 * @param server the server manageing the clients
405 * @param callback function to call on disconnect
406 * @param callback_cls closure for callback
409 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
410 GNUNET_SERVER_DisconnectCallback
411 callback, void *callback_cls);
415 * Ask the server to disconnect from the given client.
416 * This is the same as returning GNUNET_SYSERR from a message
417 * handler, except that it allows dropping of a client even
418 * when not handling a message from that client.
420 * @param client the client to disconnect from
423 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
427 * Disable the "CORK" feature for communication with the given client,
428 * forcing the OS to immediately flush the buffer on transmission
429 * instead of potentially buffering multiple messages.
431 * @param client handle to the client
432 * @return GNUNET_OK on success
435 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
439 * The tansmit context is the key datastructure for a conveniance API
440 * used for transmission of complex results to the client followed
441 * ONLY by signaling receive_done with success or error
443 struct GNUNET_SERVER_TransmitContext;
447 * Create a new transmission context for the
450 * @param client client to create the context for.
451 * @return NULL on error
453 struct GNUNET_SERVER_TransmitContext *
454 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
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 data what to append to the result message
464 * @param length length of data
465 * @param type type of the message
468 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
469 *tc, const void *data,
470 size_t length, uint16_t type);
474 * Append a message to the transmission context.
475 * All messages in the context will be sent by
476 * the transmit_context_run method.
478 * @param tc context to use
479 * @param msg message to append
482 GNUNET_SERVER_transmit_context_append_message (struct
483 GNUNET_SERVER_TransmitContext
485 const struct GNUNET_MessageHeader
490 * Execute a transmission context. If there is an error in the
491 * transmission, the receive_done method will be called with an error
492 * code (GNUNET_SYSERR), otherwise with GNUNET_OK.
494 * @param tc transmission context to use
495 * @param timeout when to time out and abort the transmission
498 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
499 struct GNUNET_TIME_Relative timeout);
503 * Destroy a transmission context. This function must not be called
504 * after 'GNUNET_SERVER_transmit_context_run'.
506 * @param tc transmission context to destroy
507 * @param success code to give to 'GNUNET_SERVER_receive_done' for
508 * the client: GNUNET_OK to keep the connection open and
509 * continue to receive
510 * GNUNET_NO to close the connection (normal behavior)
511 * GNUNET_SYSERR to close the connection (signal
515 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
520 * The notification context is the key datastructure for a conveniance
521 * API used for transmission of notifications to the client until the
522 * client disconnects (or the notification context is destroyed, in
523 * which case we disconnect these clients). Essentially, all
524 * (notification) messages are queued up until the client is able to
527 struct GNUNET_SERVER_NotificationContext;
531 * Create a new notification context.
533 * @param server server for which this function creates the context
534 * @param queue_length maximum number of messages to keep in
535 * the notification queue; optional messages are dropped
536 * if the queue gets longer than this number of messages
537 * @return handle to the notification context
539 struct GNUNET_SERVER_NotificationContext *
540 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
541 unsigned int queue_length);
545 * Destroy the context, force disconnect for all clients.
547 * @param nc context to destroy.
550 GNUNET_SERVER_notification_context_destroy (struct
551 GNUNET_SERVER_NotificationContext
556 * Add a client to the notification context.
558 * @param nc context to modify
559 * @param client client to add
562 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
564 struct GNUNET_SERVER_Client *client);
568 * Send a message to a particular client; must have
569 * already been added to the notification context.
571 * @param nc context to modify
572 * @param client client to transmit to
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_unicast (struct
578 GNUNET_SERVER_NotificationContext
580 struct GNUNET_SERVER_Client *client,
581 const struct GNUNET_MessageHeader
586 * Send a message to all clients of this context.
588 * @param nc context to modify
589 * @param msg message to send
590 * @param can_drop can this message be dropped due to queue length limitations
593 GNUNET_SERVER_notification_context_broadcast (struct
594 GNUNET_SERVER_NotificationContext
596 const struct GNUNET_MessageHeader
602 * Handle to a message stream tokenizer.
604 struct GNUNET_SERVER_MessageStreamTokenizer;
607 * Functions with this signature are called whenever a
608 * complete message is received by the tokenizer.
610 * Do not call GNUNET_SERVER_mst_destroy in callback
613 * @param client identification of the client
614 * @param message the actual message
616 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
618 typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
620 GNUNET_MessageHeader *
625 * Create a message stream tokenizer.
627 * @param cb function to call on completed messages
628 * @param cb_cls closure for cb
629 * @return handle to tokenizer
631 struct GNUNET_SERVER_MessageStreamTokenizer *
632 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
637 * Add incoming data to the receive buffer and call the
638 * callback for all complete messages.
640 * @param mst tokenizer to use
641 * @param client_identity ID of client for which this is a buffer,
642 * can be NULL (will be passed back to 'cb')
643 * @param buf input data to add
644 * @param size number of bytes in buf
645 * @param purge should any excess bytes in the buffer be discarded
646 * (i.e. for packet-based services like UDP)
647 * @param one_shot only call callback once, keep rest of message in buffer
648 * @return GNUNET_OK if we are done processing (need more data)
649 * GNUNET_NO if one_shot was set and we have another message ready
650 * GNUNET_SYSERR if the data stream is corrupt
653 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
654 void *client_identity, const char *buf, size_t size,
655 int purge, int one_shot);
659 * Destroys a tokenizer.
661 * @param mst tokenizer to destroy
664 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
668 * Signature of a function to create a custom tokenizer.
670 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
671 * @param client handle to client the tokenzier will be used for
672 * @return handle to custom tokenizer ('mst')
674 typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
675 struct GNUNET_SERVER_Client *client);
678 * Signature of a function to destroy a custom tokenizer.
680 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
681 * @param mst custom tokenizer handle
683 typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
686 * Signature of a function to destroy a custom tokenizer.
688 * @param cls closure from 'GNUNET_SERVER_set_callbacks'
689 * @param mst custom tokenizer handle
690 * @param client_identity ID of client for which this is a buffer,
691 * can be NULL (will be passed back to 'cb')
692 * @param buf input data to add
693 * @param size number of bytes in buf
694 * @param purge should any excess bytes in the buffer be discarded
695 * (i.e. for packet-based services like UDP)
696 * @param one_shot only call callback once, keep rest of message in buffer
697 * @return GNUNET_OK if we are done processing (need more data)
698 * GNUNET_NO if one_shot was set and we have another message ready
699 * GNUNET_SYSERR if the data stream is corrupt
701 typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
702 struct GNUNET_SERVER_Client *client,
703 const char *buf, size_t size,
704 int purge, int one_shot);
708 * Change functions used by the server to tokenize the message stream.
709 * (very rarely used).
711 * @param server server to modify
712 * @param create new tokenizer initialization function
713 * @param destroy new tokenizer destruction function
714 * @param receive new tokenizer receive function
715 * @param cls closure for 'create', 'receive', 'destroy'
718 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
719 GNUNET_SERVER_MstCreateCallback create,
720 GNUNET_SERVER_MstDestroyCallback destroy,
721 GNUNET_SERVER_MstReceiveCallback receive,
725 #if 0 /* keep Emacsens' auto-indent happy */
733 /* ifndef GNUNET_SERVER_LIB_H */
735 /* end of gnunet_server_lib.h */