stuff
[oweals/gnunet.git] / src / include / gnunet_server_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_server_lib.h
23  * @brief library for building GNUnet network servers
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_SERVER_LIB_H
29 #define GNUNET_SERVER_LIB_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40 #include "gnunet_connection_lib.h"
41 #include "gnunet_scheduler_lib.h"
42
43
44 /**
45  * Largest supported message.
46  */
47 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
48
49
50 /**
51  * @brief handle for a server
52  */
53 struct GNUNET_SERVER_Handle;
54
55
56 /**
57  * @brief opaque handle for a client of the server
58  */
59 struct GNUNET_SERVER_Client;
60
61
62 /**
63  * Functions with this signature are called whenever a message is
64  * received.
65  *
66  * @param cls closure
67  * @param client identification of the client
68  * @param message the actual message
69  */
70 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
71                                                struct GNUNET_SERVER_Client *
72                                                client,
73                                                const struct
74                                                GNUNET_MessageHeader *
75                                                message);
76
77
78
79 /**
80  * Message handler.  Each struct specifies how to handle on particular
81  * type of message received.
82  */
83 struct GNUNET_SERVER_MessageHandler
84 {
85   /**
86    * Function to call for messages of "type".
87    */
88   GNUNET_SERVER_MessageCallback callback;
89
90   /**
91    * Closure argument for "callback".
92    */
93   void *callback_cls;
94
95   /**
96    * Type of the message this handler covers.
97    */
98   uint16_t type;
99
100   /**
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.
105    */
106   uint16_t expected_size;
107
108 };
109
110
111 /**
112  * Create a new server.
113  *
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
122  *        will be closed
123  * @return handle for the new server, NULL on error
124  *         (typically, "port" already in use)
125  */
126 struct GNUNET_SERVER_Handle *GNUNET_SERVER_create (struct
127                                                    GNUNET_SCHEDULER_Handle
128                                                    *sched,
129                                                    GNUNET_CONNECTION_AccessCheck
130                                                    access, void *access_cls,
131                                                    struct sockaddr *const*serverAddr,
132                                                    const socklen_t *socklen,
133                                                    size_t maxbuf,
134                                                    struct GNUNET_TIME_Relative
135                                                    idle_timeout,
136                                                    int require_found);
137
138
139 /**
140  * Free resources held by this server.
141  *
142  * @param s server to destroy
143  */
144 void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
145
146
147 /**
148  * Add additional handlers to an existing server.
149  *
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).
159  */
160 void
161 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
162                             const struct GNUNET_SERVER_MessageHandler
163                             *handlers);
164
165
166 /**
167  * Notify us when the server has enough space to transmit
168  * a message of the given size to the given client.
169  *
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)
180  */
181 struct GNUNET_CONNECTION_TransmitHandle
182   *GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
183                                         size_t size,
184                                         struct GNUNET_TIME_Relative timeout,
185                                         GNUNET_CONNECTION_TransmitReadyNotify
186                                         callback, void *callback_cls);
187
188
189 /**
190  * Resume receiving from this client, we are done processing the
191  * current request.  This function must be called from within each
192  * GNUNET_SERVER_MessageCallback (or its respective continuations).
193  *
194  * @param client client we were processing a message of
195  * @param success GNUNET_OK to keep the connection open and
196  *                          continue to receive
197  *                GNUNET_NO to close the connection (normal behavior)
198  *                GNUNET_SYSERR to close the connection (signal
199  *                          serious error)
200  */
201 void
202 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success);
203
204
205 /**
206  * Inject a message into the server, pretend it came
207  * from the specified client.  Delivery of the message
208  * will happen instantly (if a handler is installed;
209  * otherwise the call does nothing).
210  *
211  * @param server the server receiving the message
212  * @param sender the "pretended" sender of the message
213  *        can be NULL!
214  * @param message message to transmit
215  * @return GNUNET_OK if the message was OK and the
216  *                   connection can stay open
217  *         GNUNET_SYSERR if the connection to the
218  *         client should be shut down
219  */
220 int
221 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
222                       struct GNUNET_SERVER_Client *sender,
223                       const struct GNUNET_MessageHeader *message);
224
225
226 /**
227  * Add a TCP socket-based connection to the set of handles managed by
228  * this server.  Use this function for outgoing (P2P) connections that
229  * we initiated (and where this server should process incoming
230  * messages).
231  *
232  * @param server the server to use
233  * @param connection the connection to manage (client must
234  *        stop using this connection from now on)
235  * @return the client handle (client should call
236  *         "client_drop" on the return value eventually)
237  */
238 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_socket (struct
239                                                            GNUNET_SERVER_Handle
240                                                            *server,
241                                                            struct
242                                                            GNUNET_CONNECTION_Handle
243                                                            *connection);
244
245
246 /**
247  * Receive data from the given connection.  This function should call
248  * "receiver" asynchronously using the scheduler.  It must return
249  * "immediately".
250  *
251  * @param cls closure
252  * @param sched scheduler to use
253  * @param max maximum number of bytes to read
254  * @param timeout maximum amount of time to wait (use -1 for "forever")
255  * @param receiver function to call with received data
256  * @param receiver_cls closure for receiver
257  */
258 typedef void
259   (*GNUNET_SERVER_ReceiveCallback) (void *cls,
260                                     size_t max,
261                                     struct GNUNET_TIME_Relative timeout,
262                                     GNUNET_CONNECTION_Receiver
263                                     receiver, void *receiver_cls);
264
265
266 /**
267  * Cancel receive request.
268  *
269  * @param cls closure
270  */
271 typedef void (*GNUNET_SERVER_ReceiveCancelCallback) (void *cls);
272
273
274 /**
275  * Notify us when the connection is ready to transmit size bytes.
276  *
277  * @param cls closure
278  * @param size number of bytes to be ready for sending
279  * @param timeout after how long should we give up (and call
280  *        notify with buf NULL and size 0)?
281  * @param notify function to call
282  * @param notify_cls closure for notify
283  * @return a handle that can be used to cancel
284  *         the transmission request or NULL if
285  *         queueing a transmission request failed
286  */
287 typedef void *(*GNUNET_SERVER_TransmitReadyCallback) (void *cls,
288                                                       size_t size,
289                                                       struct
290                                                       GNUNET_TIME_Relative
291                                                       timeout,
292                                                       GNUNET_CONNECTION_TransmitReadyNotify
293                                                       notify,
294                                                       void *notify_cls);
295
296
297 /**
298  * Cancel an earlier transmit notification request.
299  *
300  * @param cls closure
301  * @param ctx handle that was returned by the TransmitReadyCallback
302  */
303 typedef void (*GNUNET_SERVER_TransmitReadyCancelCallback) (void *cls,
304                                                            void *ctx);
305
306
307 /**
308  * Check if connection is still valid (no fatal errors have happened so far).
309  *
310  * @param cls closure
311  * @return GNUNET_YES if valid, GNUNET_NO otherwise
312  */
313 typedef int (*GNUNET_SERVER_CheckCallback) (void *cls);
314
315
316 /**
317  * Destroy this connection (free resources).
318  *
319  * @param cls closure
320  */
321 typedef void (*GNUNET_SERVER_DestroyCallback) (void *cls);
322
323
324 /**
325  * Add an arbitrary connection to the set of handles managed by this
326  * server.  This can be used if a sending and receiving does not
327  * really go over the network (internal transmission) or for servers
328  * using UDP.
329  *
330  * @param server the server to use
331  * @param chandle opaque handle for the connection
332  * @param creceive receive function for the connection
333  * @param ccancel cancel receive function for the connection
334  * @param cnotify transmit notification function for the connection
335  * @param cnotify_cancel transmit notification cancellation function for the connection
336  * @param ccheck function to test if the connection is still up
337  * @param cdestroy function to close and free the connection
338  * @return the client handle (client should call
339  *         "client_drop" on the return value eventually)
340  */
341 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_callback (struct
342                                                              GNUNET_SERVER_Handle
343                                                              *server,
344                                                              void *chandle,
345                                                              GNUNET_SERVER_ReceiveCallback
346                                                              creceive,
347                                                              GNUNET_SERVER_ReceiveCancelCallback
348                                                              ccancel,
349                                                              GNUNET_SERVER_TransmitReadyCallback
350                                                              cnotify,
351                                                              GNUNET_SERVER_TransmitReadyCancelCallback
352                                                              cnotify_cancel,
353                                                              GNUNET_SERVER_CheckCallback
354                                                              ccheck,
355                                                              GNUNET_SERVER_DestroyCallback
356                                                              cdestroy);
357
358
359 /**
360  * Notify the server that the given client handle should
361  * be kept (keeps the connection up if possible, increments
362  * the internal reference counter).
363  *
364  * @param client the client to keep
365  */
366 void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
367
368
369 /**
370  * Notify the server that the given client handle is no
371  * longer required.  Decrements the reference counter.  If
372  * that counter reaches zero an inactive connection maybe
373  * closed.
374  *
375  * @param client the client to drop
376  */
377 void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
378
379
380 /**
381  * Obtain the network address of the other party.
382  *
383  * @param client the client to get the address for
384  * @param addr where to store the address
385  * @param addrlen where to store the length of the address
386  * @return GNUNET_OK on success
387  */
388 int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
389                                       void **addr, size_t * addrlen);
390
391
392 /**
393  * Functions with this signature are called whenever a client
394  * is disconnected on the network level.
395  *
396  * @param cls closure
397  * @param client identification of the client; NULL
398  *        for the last call when the server is destroyed
399  */
400 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
401                                                   struct GNUNET_SERVER_Client
402                                                   * client);
403
404
405 /**
406  * Ask the server to notify us whenever a client disconnects.
407  * This function is called whenever the actual network connection
408  * is closed; the reference count may be zero or larger than zero
409  * at this point.  If the server is destroyed before this 
410  * notification is explicitly cancelled, the 'callback' will
411  * once be called with a 'client' argument of NULL to indicate
412  * that the server itself is now gone (and that the callback
413  * won't be called anymore and also can no longer be cancelled).
414  *
415  * @param server the server manageing the clients
416  * @param callback function to call on disconnect
417  * @param callback_cls closure for callback
418  */
419 void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
420                                       GNUNET_SERVER_DisconnectCallback
421                                       callback, void *callback_cls);
422
423
424 /**
425  * Ask the server to stop notifying us whenever a client disconnects.
426  *
427  * @param server the server manageing the clients
428  * @param callback function to call on disconnect
429  * @param callback_cls closure for callback
430  */
431 void GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
432                                              GNUNET_SERVER_DisconnectCallback
433                                              callback, void *callback_cls);
434
435
436 /**
437  * Ask the server to disconnect from the given client.
438  * This is the same as returning GNUNET_SYSERR from a message
439  * handler, except that it allows dropping of a client even
440  * when not handling a message from that client.
441  *
442  * @param client the client to disconnect from
443  */
444 void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
445
446
447 /**
448  * Configure this server's connections to continue handling client
449  * requests as usual even after we get a shutdown signal.  The change
450  * only applies to clients that connect to the server from the outside
451  * using TCP after this call.  Clients managed previously or those
452  * added using GNUNET_SERVER_connect_socket and
453  * GNUNET_SERVER_connect_callback are not affected by this option.
454  *
455  * @param h server handle
456  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
457  */
458 void
459 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h,
460                                int do_ignore);
461
462
463
464 /**
465  * The tansmit context is the key datastructure for a conveniance API
466  * used for transmission of complex results to the client followed
467  * ONLY by signaling receive_done with success or error
468  */
469 struct GNUNET_SERVER_TransmitContext;
470
471
472 /**
473  * Create a new transmission context for the
474  * given client.
475  *
476  * @param client client to create the context for.
477  * @return NULL on error
478  */
479 struct GNUNET_SERVER_TransmitContext
480   *GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client
481                                           *client);
482
483
484 /**
485  * Append a message to the transmission context.
486  * All messages in the context will be sent by
487  * the transmit_context_run method.
488  *
489  * @param tc context to use
490  * @param data what to append to the result message
491  * @param length length of data
492  * @param type type of the message
493  */
494 void
495 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
496                                             *tc, const void *data, size_t length,
497                                             uint16_t type);
498
499
500 /**
501  * Append a message to the transmission context.
502  * All messages in the context will be sent by
503  * the transmit_context_run method.
504  *
505  * @param tc context to use
506  * @param msg message to append
507  */
508 void
509 GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext
510                                                *tc, const struct GNUNET_MessageHeader *msg);
511
512
513 /**
514  * Execute a transmission context.  If there is
515  * an error in the transmission, the receive_done
516  * method will be called with an error code (GNUNET_SYSERR),
517  * otherwise with GNUNET_OK.
518  *
519  * @param tc transmission context to use
520  * @param timeout when to time out and abort the transmission
521  */
522 void
523 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
524                                     struct GNUNET_TIME_Relative timeout);
525
526
527
528 /**
529  * The notification context is the key datastructure for a conveniance
530  * API used for transmission of notifications to the client until the
531  * client disconnects (or the notification context is destroyed, in
532  * which case we disconnect these clients).  Essentially, all
533  * (notification) messages are queued up until the client is able to
534  * read them.
535  */
536 struct GNUNET_SERVER_NotificationContext;
537
538
539 /**
540  * Create a new notification context.
541  *
542  * @param server server for which this function creates the context
543  * @param queue_length maximum number of messages to keep in
544  *        the notification queue; optional messages are dropped
545  *        it the queue gets longer than this number of messages
546  * @return handle to the notification context
547  */
548 struct GNUNET_SERVER_NotificationContext *
549 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
550                                            unsigned int queue_length);
551
552
553 /**
554  * Destroy the context, force disconnect for all clients.
555  *
556  * @param nc context to destroy.
557  */
558 void
559 GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
560
561
562 /**
563  * Add a client to the notification context.
564  *
565  * @param nc context to modify
566  * @param client client to add
567  */
568 void
569 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
570                                         struct GNUNET_SERVER_Client *client);
571
572
573 /**
574  * Send a message to a particular client; must have
575  * already been added to the notification context.
576  *
577  * @param nc context to modify
578  * @param client client to transmit to
579  * @param msg message to send
580  * @param can_drop can this message be dropped due to queue length limitations
581  */
582 void
583 GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
584                                             struct GNUNET_SERVER_Client *client,
585                                             const struct GNUNET_MessageHeader *msg,
586                                             int can_drop);
587
588
589 /**
590  * Send a message to all clients of this context.
591  *
592  * @param nc context to modify
593  * @param msg message to send
594  * @param can_drop can this message be dropped due to queue length limitations
595  */
596 void
597 GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
598                                               const struct GNUNET_MessageHeader *msg,
599                                               int can_drop);
600
601
602
603 #if 0                           /* keep Emacsens' auto-indent happy */
604 {
605 #endif
606 #ifdef __cplusplus
607 }
608 #endif
609
610
611 /* ifndef GNUNET_SERVER_LIB_H */
612 #endif
613 /* end of gnunet_server_lib.h */