2 This file is part of GNUnet.
3 (C) 2009 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"
41 #include "gnunet_scheduler_lib.h"
45 * Largest supported message.
47 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
51 * @brief handle for a server
53 struct GNUNET_SERVER_Handle;
57 * @brief opaque handle for a client of the server
59 struct GNUNET_SERVER_Client;
63 * Functions with this signature are called whenever a message is
67 * @param client identification of the client
68 * @param message the actual message
70 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
71 struct GNUNET_SERVER_Client *
74 GNUNET_MessageHeader *
80 * Message handler. Each struct specifies how to handle on particular
81 * type of message received.
83 struct GNUNET_SERVER_MessageHandler
86 * Function to call for messages of "type".
88 GNUNET_SERVER_MessageCallback callback;
91 * Closure argument for "callback".
96 * Type of the message this handler covers.
101 * Expected size of messages of this type. Use 0 for
102 * variable-size. If non-zero, messages of the given
103 * type will be discarded (and the connection closed)
104 * if they do not have the right size.
106 uint16_t expected_size;
112 * Create a new server.
114 * @param sched scheduler to use
115 * @param access function for access control
116 * @param access_cls closure for access
117 * @param serverAddr address toes listen on (including port), NULL terminated array
118 * @param socklen lengths of respective serverAddr
119 * @param maxbuf maximum write buffer size for accepted sockets
120 * @param idle_timeout after how long should we timeout idle connections?
121 * @param require_found if YES, connections sending messages of unknown type
123 * @return handle for the new server, NULL on error
124 * (typically, "port" already in use)
126 struct GNUNET_SERVER_Handle *GNUNET_SERVER_create (struct
127 GNUNET_SCHEDULER_Handle
129 GNUNET_CONNECTION_AccessCheck
130 access, void *access_cls,
131 struct sockaddr *const*serverAddr,
132 const socklen_t *socklen,
134 struct GNUNET_TIME_Relative
140 * Free resources held by this server.
142 * @param s server to destroy
144 void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
148 * Add additional handlers to an existing server.
150 * @param server the server to add handlers to
151 * @param handlers array of message handlers for
152 * incoming messages; the last entry must
153 * have "NULL" for the "callback"; multiple
154 * entries for the same type are allowed,
155 * they will be called in order of occurence.
156 * These handlers can be removed later;
157 * the handlers array must exist until removed
158 * (or server is destroyed).
161 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
162 const struct GNUNET_SERVER_MessageHandler
167 * Notify us when the server has enough space to transmit
168 * a message of the given size to the given client.
170 * @param client client to transmit message to
171 * @param size requested amount of buffer space
172 * @param timeout after how long should we give up (and call
173 * notify with buf NULL and size 0)?
174 * @param callback function to call when space is available
175 * @param callback_cls closure for callback
176 * @return non-NULL if the notify callback was queued; can be used
177 * to cancel the request using
178 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
179 * NULL if we are already going to notify someone else (busy)
181 struct GNUNET_CONNECTION_TransmitHandle
182 *GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
184 struct GNUNET_TIME_Relative timeout,
185 GNUNET_CONNECTION_TransmitReadyNotify
186 callback, void *callback_cls);
190 * Set the persistent flag on this client, used to setup client connection
191 * to only be killed when the service it's connected to is actually dead.
193 * @param client the client to set the persistent flag on
196 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
199 * Resume receiving from this client, we are done processing the
200 * current request. This function must be called from within each
201 * GNUNET_SERVER_MessageCallback (or its respective continuations).
203 * @param client client we were processing a message of
204 * @param success GNUNET_OK to keep the connection open and
205 * continue to receive
206 * GNUNET_NO to close the connection (normal behavior)
207 * GNUNET_SYSERR to close the connection (signal
211 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
215 * Inject a message into the server, pretend it came
216 * from the specified client. Delivery of the message
217 * will happen instantly (if a handler is installed;
218 * otherwise the call does nothing).
220 * @param server the server receiving the message
221 * @param sender the "pretended" sender of the message
223 * @param message message to transmit
224 * @return GNUNET_OK if the message was OK and the
225 * connection can stay open
226 * GNUNET_SYSERR if the connection to the
227 * client should be shut down
230 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
231 struct GNUNET_SERVER_Client *sender,
232 const struct GNUNET_MessageHeader *message);
236 * Add a TCP socket-based connection to the set of handles managed by
237 * this server. Use this function for outgoing (P2P) connections that
238 * we initiated (and where this server should process incoming
241 * @param server the server to use
242 * @param connection the connection to manage (client must
243 * stop using this connection from now on)
244 * @return the client handle (client should call
245 * "client_drop" on the return value eventually)
247 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_socket (struct
251 GNUNET_CONNECTION_Handle
256 * Receive data from the given connection. This function should call
257 * "receiver" asynchronously using the scheduler. It must return
261 * @param sched scheduler to use
262 * @param max maximum number of bytes to read
263 * @param timeout maximum amount of time to wait (use -1 for "forever")
264 * @param receiver function to call with received data
265 * @param receiver_cls closure for receiver
268 (*GNUNET_SERVER_ReceiveCallback) (void *cls,
270 struct GNUNET_TIME_Relative timeout,
271 GNUNET_CONNECTION_Receiver
272 receiver, void *receiver_cls);
276 * Cancel receive request.
280 typedef void (*GNUNET_SERVER_ReceiveCancelCallback) (void *cls);
284 * Notify us when the connection is ready to transmit size bytes.
287 * @param size number of bytes to be ready for sending
288 * @param timeout after how long should we give up (and call
289 * notify with buf NULL and size 0)?
290 * @param notify function to call
291 * @param notify_cls closure for notify
292 * @return a handle that can be used to cancel
293 * the transmission request or NULL if
294 * queueing a transmission request failed
296 typedef void *(*GNUNET_SERVER_TransmitReadyCallback) (void *cls,
301 GNUNET_CONNECTION_TransmitReadyNotify
307 * Cancel an earlier transmit notification request.
310 * @param ctx handle that was returned by the TransmitReadyCallback
312 typedef void (*GNUNET_SERVER_TransmitReadyCancelCallback) (void *cls,
317 * Check if connection is still valid (no fatal errors have happened so far).
320 * @return GNUNET_YES if valid, GNUNET_NO otherwise
322 typedef int (*GNUNET_SERVER_CheckCallback) (void *cls);
326 * Destroy this connection (free resources).
329 * @pararm persist when connection is closed, "leak" socket
331 typedef void (*GNUNET_SERVER_DestroyCallback) (void *cls, int persist);
335 * Add an arbitrary connection to the set of handles managed by this
336 * server. This can be used if a sending and receiving does not
337 * really go over the network (internal transmission) or for servers
340 * @param server the server to use
341 * @param chandle opaque handle for the connection
342 * @param creceive receive function for the connection
343 * @param ccancel cancel receive function for the connection
344 * @param cnotify transmit notification function for the connection
345 * @param cnotify_cancel transmit notification cancellation function for the connection
346 * @param ccheck function to test if the connection is still up
347 * @param cdestroy function to close and free the connection
348 * @return the client handle (client should call
349 * "client_drop" on the return value eventually)
351 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_callback (struct
355 GNUNET_SERVER_ReceiveCallback
357 GNUNET_SERVER_ReceiveCancelCallback
359 GNUNET_SERVER_TransmitReadyCallback
361 GNUNET_SERVER_TransmitReadyCancelCallback
363 GNUNET_SERVER_CheckCallback
365 GNUNET_SERVER_DestroyCallback
370 * Notify the server that the given client handle should
371 * be kept (keeps the connection up if possible, increments
372 * the internal reference counter).
374 * @param client the client to keep
376 void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
380 * Notify the server that the given client handle is no
381 * longer required. Decrements the reference counter. If
382 * that counter reaches zero an inactive connection maybe
385 * @param client the client to drop
387 void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
391 * Obtain the network address of the other party.
393 * @param client the client to get the address for
394 * @param addr where to store the address
395 * @param addrlen where to store the length of the address
396 * @return GNUNET_OK on success
398 int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
399 void **addr, size_t * addrlen);
403 * Functions with this signature are called whenever a client
404 * is disconnected on the network level.
407 * @param client identification of the client; NULL
408 * for the last call when the server is destroyed
410 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
411 struct GNUNET_SERVER_Client
416 * Ask the server to notify us whenever a client disconnects.
417 * This function is called whenever the actual network connection
418 * is closed; the reference count may be zero or larger than zero
419 * at this point. If the server is destroyed before this
420 * notification is explicitly cancelled, the 'callback' will
421 * once be called with a 'client' argument of NULL to indicate
422 * that the server itself is now gone (and that the callback
423 * won't be called anymore and also can no longer be cancelled).
425 * @param server the server manageing the clients
426 * @param callback function to call on disconnect
427 * @param callback_cls closure for callback
429 void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
430 GNUNET_SERVER_DisconnectCallback
431 callback, void *callback_cls);
435 * Ask the server to stop notifying us whenever a client disconnects.
437 * @param server the server manageing the clients
438 * @param callback function to call on disconnect
439 * @param callback_cls closure for callback
441 void GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
442 GNUNET_SERVER_DisconnectCallback
443 callback, void *callback_cls);
447 * Ask the server to disconnect from the given client.
448 * This is the same as returning GNUNET_SYSERR from a message
449 * handler, except that it allows dropping of a client even
450 * when not handling a message from that client.
452 * @param client the client to disconnect from
454 void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
458 * Configure this server's connections to continue handling client
459 * requests as usual even after we get a shutdown signal. The change
460 * only applies to clients that connect to the server from the outside
461 * using TCP after this call. Clients managed previously or those
462 * added using GNUNET_SERVER_connect_socket and
463 * GNUNET_SERVER_connect_callback are not affected by this option.
465 * @param h server handle
466 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
469 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h,
475 * The tansmit context is the key datastructure for a conveniance API
476 * used for transmission of complex results to the client followed
477 * ONLY by signaling receive_done with success or error
479 struct GNUNET_SERVER_TransmitContext;
483 * Create a new transmission context for the
486 * @param client client to create the context for.
487 * @return NULL on error
489 struct GNUNET_SERVER_TransmitContext
490 *GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client
495 * Append a message to the transmission context.
496 * All messages in the context will be sent by
497 * the transmit_context_run method.
499 * @param tc context to use
500 * @param data what to append to the result message
501 * @param length length of data
502 * @param type type of the message
505 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
506 *tc, const void *data, size_t length,
511 * Append a message to the transmission context.
512 * All messages in the context will be sent by
513 * the transmit_context_run method.
515 * @param tc context to use
516 * @param msg message to append
519 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext
520 *tc, const struct GNUNET_MessageHeader *msg);
524 * Execute a transmission context. If there is
525 * an error in the transmission, the receive_done
526 * method will be called with an error code (GNUNET_SYSERR),
527 * otherwise with GNUNET_OK.
529 * @param tc transmission context to use
530 * @param timeout when to time out and abort the transmission
533 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
534 struct GNUNET_TIME_Relative timeout);
539 * The notification context is the key datastructure for a conveniance
540 * API used for transmission of notifications to the client until the
541 * client disconnects (or the notification context is destroyed, in
542 * which case we disconnect these clients). Essentially, all
543 * (notification) messages are queued up until the client is able to
546 struct GNUNET_SERVER_NotificationContext;
550 * Create a new notification context.
552 * @param server server for which this function creates the context
553 * @param queue_length maximum number of messages to keep in
554 * the notification queue; optional messages are dropped
555 * it the queue gets longer than this number of messages
556 * @return handle to the notification context
558 struct GNUNET_SERVER_NotificationContext *
559 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
560 unsigned int queue_length);
564 * Destroy the context, force disconnect for all clients.
566 * @param nc context to destroy.
569 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
573 * Add a client to the notification context.
575 * @param nc context to modify
576 * @param client client to add
579 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
580 struct GNUNET_SERVER_Client *client);
584 * Send a message to a particular client; must have
585 * already been added to the notification context.
587 * @param nc context to modify
588 * @param client client to transmit to
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_unicast (struct GNUNET_SERVER_NotificationContext *nc,
594 struct GNUNET_SERVER_Client *client,
595 const struct GNUNET_MessageHeader *msg,
600 * Send a message to all clients of this context.
602 * @param nc context to modify
603 * @param msg message to send
604 * @param can_drop can this message be dropped due to queue length limitations
607 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
608 const struct GNUNET_MessageHeader *msg,
613 #if 0 /* keep Emacsens' auto-indent happy */
621 /* ifndef GNUNET_SERVER_LIB_H */
623 /* end of gnunet_server_lib.h */