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"
41 #include "gnunet_scheduler_lib.h"
45 * Largest supported message.
47 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
50 * Largest supported message.
52 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
55 * @brief handle for a server
57 struct GNUNET_SERVER_Handle;
61 * @brief opaque handle for a client of the server
63 struct GNUNET_SERVER_Client;
67 * Functions with this signature are called whenever a message is
71 * @param client identification of the client
72 * @param message the actual message
74 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
75 struct GNUNET_SERVER_Client *
78 GNUNET_MessageHeader *
84 * Message handler. Each struct specifies how to handle on particular
85 * type of message received.
87 struct GNUNET_SERVER_MessageHandler
90 * Function to call for messages of "type".
92 GNUNET_SERVER_MessageCallback callback;
95 * Closure argument for "callback".
100 * Type of the message this handler covers.
105 * Expected size of messages of this type. Use 0 for
106 * variable-size. If non-zero, messages of the given
107 * type will be discarded (and the connection closed)
108 * if they do not have the right size.
110 uint16_t expected_size;
116 * Create a new server.
118 * @param sched scheduler to use
119 * @param access function for access control
120 * @param access_cls closure for access
121 * @param lsocks NULL-terminated array of listen sockets
122 * @param idle_timeout after how long should we timeout idle connections?
123 * @param require_found if YES, connections sending messages of unknown type
125 * @return handle for the new server, NULL on error
126 * (typically, "port" already in use)
128 struct GNUNET_SERVER_Handle *
129 GNUNET_SERVER_create_with_sockets (struct GNUNET_SCHEDULER_Handle *sched,
130 GNUNET_CONNECTION_AccessCheck access, void *access_cls,
131 struct GNUNET_NETWORK_Handle **lsocks,
132 struct GNUNET_TIME_Relative
137 * Create a new server.
139 * @param sched scheduler to use
140 * @param access function for access control
141 * @param access_cls closure for access
142 * @param serverAddr address toes listen on (including port), NULL terminated array
143 * @param socklen lengths of respective serverAddr
144 * @param idle_timeout after how long should we timeout idle connections?
145 * @param require_found if YES, connections sending messages of unknown type
147 * @return handle for the new server, NULL on error
148 * (typically, "port" already in use)
150 struct GNUNET_SERVER_Handle *GNUNET_SERVER_create (struct
151 GNUNET_SCHEDULER_Handle
153 GNUNET_CONNECTION_AccessCheck
154 access, void *access_cls,
155 struct sockaddr *const*serverAddr,
156 const socklen_t *socklen,
157 struct GNUNET_TIME_Relative
163 * Free resources held by this server.
165 * @param s server to destroy
167 void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
171 * Add additional handlers to an existing server.
173 * @param server the server to add handlers to
174 * @param handlers array of message handlers for
175 * incoming messages; the last entry must
176 * have "NULL" for the "callback"; multiple
177 * entries for the same type are allowed,
178 * they will be called in order of occurence.
179 * These handlers can be removed later;
180 * the handlers array must exist until removed
181 * (or server is destroyed).
184 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
185 const struct GNUNET_SERVER_MessageHandler
190 * Notify us when the server has enough space to transmit
191 * a message of the given size to the given client.
193 * @param client client to transmit message to
194 * @param size requested amount of buffer space
195 * @param timeout after how long should we give up (and call
196 * notify with buf NULL and size 0)?
197 * @param callback function to call when space is available
198 * @param callback_cls closure for callback
199 * @return non-NULL if the notify callback was queued; can be used
200 * to cancel the request using
201 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
202 * NULL if we are already going to notify someone else (busy)
204 struct GNUNET_CONNECTION_TransmitHandle
205 *GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
207 struct GNUNET_TIME_Relative timeout,
208 GNUNET_CONNECTION_TransmitReadyNotify
209 callback, void *callback_cls);
213 * Set the persistent flag on this client, used to setup client connection
214 * to only be killed when the service it's connected to is actually dead.
216 * @param client the client to set the persistent flag on
219 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
222 * Resume receiving from this client, we are done processing the
223 * current request. This function must be called from within each
224 * GNUNET_SERVER_MessageCallback (or its respective continuations).
226 * @param client client we were processing a message of
227 * @param success GNUNET_OK to keep the connection open and
228 * continue to receive
229 * GNUNET_NO to close the connection (normal behavior)
230 * GNUNET_SYSERR to close the connection (signal
234 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
238 * Inject a message into the server, pretend it came
239 * from the specified client. Delivery of the message
240 * will happen instantly (if a handler is installed;
241 * otherwise the call does nothing).
243 * @param server the server receiving the message
244 * @param sender the "pretended" sender of the message
246 * @param message message to transmit
247 * @return GNUNET_OK if the message was OK and the
248 * connection can stay open
249 * GNUNET_SYSERR if the connection to the
250 * client should be shut down
253 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
254 struct GNUNET_SERVER_Client *sender,
255 const struct GNUNET_MessageHeader *message);
259 * Add a TCP socket-based connection to the set of handles managed by
260 * this server. Use this function for outgoing (P2P) connections that
261 * we initiated (and where this server should process incoming
264 * @param server the server to use
265 * @param connection the connection to manage (client must
266 * stop using this connection from now on)
267 * @return the client handle (client should call
268 * "client_drop" on the return value eventually)
270 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_socket (struct
274 GNUNET_CONNECTION_Handle
279 * Notify the server that the given client handle should
280 * be kept (keeps the connection up if possible, increments
281 * the internal reference counter).
283 * @param client the client to keep
285 void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
289 * Notify the server that the given client handle is no
290 * longer required. Decrements the reference counter. If
291 * that counter reaches zero an inactive connection maybe
294 * @param client the client to drop
296 void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
300 * Obtain the network address of the other party.
302 * @param client the client to get the address for
303 * @param addr where to store the address
304 * @param addrlen where to store the length of the address
305 * @return GNUNET_OK on success
307 int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
308 void **addr, size_t * addrlen);
312 * Functions with this signature are called whenever a client
313 * is disconnected on the network level.
316 * @param client identification of the client; NULL
317 * for the last call when the server is destroyed
319 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
320 struct GNUNET_SERVER_Client
325 * Ask the server to notify us whenever a client disconnects.
326 * This function is called whenever the actual network connection
327 * is closed; the reference count may be zero or larger than zero
328 * at this point. If the server is destroyed before this
329 * notification is explicitly cancelled, the 'callback' will
330 * once be called with a 'client' argument of NULL to indicate
331 * that the server itself is now gone (and that the callback
332 * won't be called anymore and also can no longer be cancelled).
334 * @param server the server manageing the clients
335 * @param callback function to call on disconnect
336 * @param callback_cls closure for callback
338 void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
339 GNUNET_SERVER_DisconnectCallback
340 callback, void *callback_cls);
344 * Ask the server to stop notifying us whenever a client disconnects.
346 * @param server the server manageing the clients
347 * @param callback function to call on disconnect
348 * @param callback_cls closure for callback
350 void GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
351 GNUNET_SERVER_DisconnectCallback
352 callback, void *callback_cls);
356 * Ask the server to disconnect from the given client.
357 * This is the same as returning GNUNET_SYSERR from a message
358 * handler, except that it allows dropping of a client even
359 * when not handling a message from that client.
361 * @param client the client to disconnect from
363 void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
367 * Configure this server's connections to continue handling client
368 * requests as usual even after we get a shutdown signal. The change
369 * only applies to clients that connect to the server from the outside
370 * using TCP after this call. Clients managed previously or those
371 * added using GNUNET_SERVER_connect_socket and
372 * GNUNET_SERVER_connect_callback are not affected by this option.
374 * @param h server handle
375 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
378 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h,
384 * The tansmit context is the key datastructure for a conveniance API
385 * used for transmission of complex results to the client followed
386 * ONLY by signaling receive_done with success or error
388 struct GNUNET_SERVER_TransmitContext;
392 * Create a new transmission context for the
395 * @param client client to create the context for.
396 * @return NULL on error
398 struct GNUNET_SERVER_TransmitContext
399 *GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client
404 * Append a message to the transmission context.
405 * All messages in the context will be sent by
406 * the transmit_context_run method.
408 * @param tc context to use
409 * @param data what to append to the result message
410 * @param length length of data
411 * @param type type of the message
414 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
415 *tc, const void *data, size_t length,
420 * Append a message to the transmission context.
421 * All messages in the context will be sent by
422 * the transmit_context_run method.
424 * @param tc context to use
425 * @param msg message to append
428 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext
429 *tc, const struct GNUNET_MessageHeader *msg);
433 * Execute a transmission context. If there is
434 * an error in the transmission, the receive_done
435 * method will be called with an error code (GNUNET_SYSERR),
436 * otherwise with GNUNET_OK.
438 * @param tc transmission context to use
439 * @param timeout when to time out and abort the transmission
442 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
443 struct GNUNET_TIME_Relative timeout);
448 * The notification context is the key datastructure for a conveniance
449 * API used for transmission of notifications to the client until the
450 * client disconnects (or the notification context is destroyed, in
451 * which case we disconnect these clients). Essentially, all
452 * (notification) messages are queued up until the client is able to
455 struct GNUNET_SERVER_NotificationContext;
459 * Create a new notification context.
461 * @param server server for which this function creates the context
462 * @param queue_length maximum number of messages to keep in
463 * the notification queue; optional messages are dropped
464 * it the queue gets longer than this number of messages
465 * @return handle to the notification context
467 struct GNUNET_SERVER_NotificationContext *
468 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
469 unsigned int queue_length);
473 * Destroy the context, force disconnect for all clients.
475 * @param nc context to destroy.
478 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
482 * Add a client to the notification context.
484 * @param nc context to modify
485 * @param client client to add
488 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
489 struct GNUNET_SERVER_Client *client);
493 * Send a message to a particular client; must have
494 * already been added to the notification context.
496 * @param nc context to modify
497 * @param client client to transmit to
498 * @param msg message to send
499 * @param can_drop can this message be dropped due to queue length limitations
502 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
503 struct GNUNET_SERVER_Client *client,
504 const struct GNUNET_MessageHeader *msg,
509 * Send a message to all clients of this context.
511 * @param nc context to modify
512 * @param msg message to send
513 * @param can_drop can this message be dropped due to queue length limitations
516 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
517 const struct GNUNET_MessageHeader *msg,
523 * Handle to a message stream tokenizer.
525 struct GNUNET_SERVER_MessageStreamTokenizer;
528 * Functions with this signature are called whenever a
529 * complete message is received by the tokenizer.
532 * @param client identification of the client
533 * @param message the actual message
535 typedef void (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
538 GNUNET_MessageHeader *
543 * Create a message stream tokenizer.
545 * @param cb function to call on completed messages
546 * @param cb_cls closure for cb
547 * @return handle to tokenizer
549 struct GNUNET_SERVER_MessageStreamTokenizer *
550 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
555 * Add incoming data to the receive buffer and call the
556 * callback for all complete messages.
558 * @param mst tokenizer to use
559 * @param client_identity ID of client for which this is a buffer,
560 * can be NULL (will be passed back to 'cb')
561 * @param buf input data to add
562 * @param size number of bytes in buf
563 * @param purge should any excess bytes in the buffer be discarded
564 * (i.e. for packet-based services like UDP)
565 * @param one_shot only call callback once, keep rest of message in buffer
566 * @return GNUNET_OK if we are done processing (need more data)
567 * GNUNET_NO if one_shot was set and we have another message ready
568 * GNUNET_SYSERR if the data stream is corrupt
571 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
572 void *client_identity,
580 * Destroys a tokenizer.
582 * @param mst tokenizer to destroy
585 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
588 #if 0 /* keep Emacsens' auto-indent happy */
596 /* ifndef GNUNET_SERVER_LIB_H */
598 /* end of gnunet_server_lib.h */