d9663ac826fd42faf37353bfb29e297d9dbfd86b
[oweals/gnunet.git] / src / include / gnunet_server_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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
42
43 /**
44  * Largest supported message.
45  */
46 #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
47
48 /**
49  * Largest supported message.
50  */
51 #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
52
53 /**
54  * @brief handle for a server
55  */
56 struct GNUNET_SERVER_Handle;
57
58
59 /**
60  * @brief opaque handle for a client of the server
61  */
62 struct GNUNET_SERVER_Client;
63
64
65 /**
66  * Functions with this signature are called whenever a message is
67  * received.
68  *
69  * @param cls closure
70  * @param client identification of the client
71  * @param message the actual message
72  */
73 typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
74                                                struct GNUNET_SERVER_Client *
75                                                client,
76                                                const struct GNUNET_MessageHeader
77                                                * message);
78
79
80
81 /**
82  * Message handler.  Each struct specifies how to handle on particular
83  * type of message received.
84  */
85 struct GNUNET_SERVER_MessageHandler
86 {
87   /**
88    * Function to call for messages of "type".
89    */
90   GNUNET_SERVER_MessageCallback callback;
91
92   /**
93    * Closure argument for "callback".
94    */
95   void *callback_cls;
96
97   /**
98    * Type of the message this handler covers.
99    */
100   uint16_t type;
101
102   /**
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.
107    */
108   uint16_t expected_size;
109
110 };
111
112
113 /**
114  * Create a new server.
115  *
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
121  *        will be closed
122  * @return handle for the new server, NULL on error
123  *         (typically, "port" already in use)
124  */
125 struct GNUNET_SERVER_Handle
126     *GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
127                                         void *access_cls,
128                                         struct GNUNET_NETWORK_Handle **lsocks,
129                                         struct GNUNET_TIME_Relative
130                                         idle_timeout, int require_found);
131
132 /**
133  * Create a new server.
134  *
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
141  *        will be closed
142  * @return handle for the new server, NULL on error
143  *         (typically, "port" already in use)
144  */
145 struct GNUNET_SERVER_Handle *GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck
146                                                    access, void *access_cls,
147                                                    struct sockaddr *const
148                                                    *serverAddr,
149                                                    const socklen_t * socklen,
150                                                    struct GNUNET_TIME_Relative
151                                                    idle_timeout,
152                                                    int require_found);
153
154
155 /**
156  * Free resources held by this server.
157  *
158  * @param s server to destroy
159  */
160 void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s);
161
162
163 /**
164  * Add additional handlers to an existing server.
165  *
166  * @param server the server to add handlers to
167  * @param handlers array of message handlers for
168  *        incoming messages; the last entry must
169  *        have "NULL" for the "callback"; multiple
170  *        entries for the same type are allowed,
171  *        they will be called in order of occurence.
172  *        These handlers can be removed later;
173  *        the handlers array must exist until removed
174  *        (or server is destroyed).
175  */
176 void GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
177                                  const struct GNUNET_SERVER_MessageHandler
178                                  *handlers);
179
180
181 /**
182  * Notify us when the server has enough space to transmit
183  * a message of the given size to the given client.
184  *
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)
195  */
196 struct GNUNET_CONNECTION_TransmitHandle
197     *GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
198                                           size_t size,
199                                           struct GNUNET_TIME_Relative timeout,
200                                           GNUNET_CONNECTION_TransmitReadyNotify
201                                           callback, void *callback_cls);
202
203
204 /**
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.
207  *
208  * @param client the client to set the persistent flag on
209  */
210 void GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
211
212 /**
213  * Resume receiving from this client, we are done processing the
214  * current request.  This function must be called from within each
215  * GNUNET_SERVER_MessageCallback (or its respective continuations).
216  *
217  * @param client client we were processing a message of
218  * @param success GNUNET_OK to keep the connection open and
219  *                          continue to receive
220  *                GNUNET_NO to close the connection (normal behavior)
221  *                GNUNET_SYSERR to close the connection (signal
222  *                          serious error)
223  */
224 void GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
225                                  int success);
226
227
228 /**
229  * Change the timeout for a particular client.  Decreasing the timeout
230  * may not go into effect immediately (only after the previous timeout
231  * times out or activity happens on the socket).
232  *
233  * @param client the client to update
234  * @param timeout new timeout for activities on the socket
235  */
236 void GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
237                                        struct GNUNET_TIME_Relative timeout);
238
239
240 /**
241  * Disable the warning the server issues if a message is not acknowledged
242  * in a timely fashion.  Use this call if a client is intentionally delayed
243  * for a while.  Only applies to the current message.
244  *
245  * @param client client for which to disable the warning
246  */
247 void GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
248                                                  *client);
249
250
251 /**
252  * Inject a message into the server, pretend it came
253  * from the specified client.  Delivery of the message
254  * will happen instantly (if a handler is installed;
255  * otherwise the call does nothing).
256  *
257  * @param server the server receiving the message
258  * @param sender the "pretended" sender of the message
259  *        can be NULL!
260  * @param message message to transmit
261  * @return GNUNET_OK if the message was OK and the
262  *                   connection can stay open
263  *         GNUNET_SYSERR if the connection to the
264  *         client should be shut down
265  */
266 int GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
267                           struct GNUNET_SERVER_Client *sender,
268                           const struct GNUNET_MessageHeader *message);
269
270
271 /**
272  * Add a TCP socket-based connection to the set of handles managed by
273  * this server.  Use this function for outgoing (P2P) connections that
274  * we initiated (and where this server should process incoming
275  * messages).
276  *
277  * @param server the server to use
278  * @param connection the connection to manage (client must
279  *        stop using this connection from now on)
280  * @return the client handle (client should call
281  *         "client_drop" on the return value eventually)
282  */
283 struct GNUNET_SERVER_Client *GNUNET_SERVER_connect_socket (struct
284                                                            GNUNET_SERVER_Handle
285                                                            *server,
286                                                            struct
287                                                            GNUNET_CONNECTION_Handle
288                                                            *connection);
289
290
291 /**
292  * Notify the server that the given client handle should
293  * be kept (keeps the connection up if possible, increments
294  * the internal reference counter).
295  *
296  * @param client the client to keep
297  */
298 void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
299
300
301 /**
302  * Notify the server that the given client handle is no
303  * longer required.  Decrements the reference counter.  If
304  * that counter reaches zero an inactive connection maybe
305  * closed.
306  *
307  * @param client the client to drop
308  */
309 void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
310
311
312 /**
313  * Obtain the network address of the other party.
314  *
315  * @param client the client to get the address for
316  * @param addr where to store the address
317  * @param addrlen where to store the length of the address
318  * @return GNUNET_OK on success
319  */
320 int GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
321                                       void **addr, size_t * addrlen);
322
323
324 /**
325  * Functions with this signature are called whenever a client
326  * is disconnected on the network level.
327  *
328  * @param cls closure
329  * @param client identification of the client; NULL
330  *        for the last call when the server is destroyed
331  */
332 typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
333                                                   struct GNUNET_SERVER_Client *
334                                                   client);
335
336
337 /**
338  * Ask the server to notify us whenever a client disconnects.
339  * This function is called whenever the actual network connection
340  * is closed; the reference count may be zero or larger than zero
341  * at this point.  If the server is destroyed before this 
342  * notification is explicitly cancelled, the 'callback' will
343  * once be called with a 'client' argument of NULL to indicate
344  * that the server itself is now gone (and that the callback
345  * won't be called anymore and also can no longer be cancelled).
346  *
347  * @param server the server manageing the clients
348  * @param callback function to call on disconnect
349  * @param callback_cls closure for callback
350  */
351 void GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
352                                       GNUNET_SERVER_DisconnectCallback callback,
353                                       void *callback_cls);
354
355
356 /**
357  * Ask the server to stop notifying us whenever a client disconnects.
358  *
359  * @param server the server manageing the clients
360  * @param callback function to call on disconnect
361  * @param callback_cls closure for callback
362  */
363 void GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle
364                                              *server,
365                                              GNUNET_SERVER_DisconnectCallback
366                                              callback, void *callback_cls);
367
368
369 /**
370  * Ask the server to disconnect from the given client.
371  * This is the same as returning GNUNET_SYSERR from a message
372  * handler, except that it allows dropping of a client even
373  * when not handling a message from that client.
374  *
375  * @param client the client to disconnect from
376  */
377 void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
378
379
380 /**
381  * Configure this server's connections to continue handling client
382  * requests as usual even after we get a shutdown signal.  The change
383  * only applies to clients that connect to the server from the outside
384  * using TCP after this call.  Clients managed previously or those
385  * added using GNUNET_SERVER_connect_socket and
386  * GNUNET_SERVER_connect_callback are not affected by this option.
387  *
388  * @param h server handle
389  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
390  */
391 void GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h,
392                                     int do_ignore);
393
394
395
396 /**
397  * Disable the "CORK" feature for communication with the given client,
398  * forcing the OS to immediately flush the buffer on transmission
399  * instead of potentially buffering multiple messages.
400  *
401  * @param client handle to the client
402  * @return GNUNET_OK on success
403  */
404 int GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
405
406
407 /**
408  * The tansmit context is the key datastructure for a conveniance API
409  * used for transmission of complex results to the client followed
410  * ONLY by signaling receive_done with success or error
411  */
412 struct GNUNET_SERVER_TransmitContext;
413
414
415 /**
416  * Create a new transmission context for the
417  * given client.
418  *
419  * @param client client to create the context for.
420  * @return NULL on error
421  */
422 struct GNUNET_SERVER_TransmitContext
423     *GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client
424                                             *client);
425
426
427 /**
428  * Append a message to the transmission context.
429  * All messages in the context will be sent by
430  * the transmit_context_run method.
431  *
432  * @param tc context to use
433  * @param data what to append to the result message
434  * @param length length of data
435  * @param type type of the message
436  */
437 void GNUNET_SERVER_transmit_context_append_data (struct
438                                                  GNUNET_SERVER_TransmitContext
439                                                  *tc, const void *data,
440                                                  size_t length, uint16_t type);
441
442
443 /**
444  * Append a message to the transmission context.
445  * All messages in the context will be sent by
446  * the transmit_context_run method.
447  *
448  * @param tc context to use
449  * @param msg message to append
450  */
451 void GNUNET_SERVER_transmit_context_append_message (struct
452                                                     GNUNET_SERVER_TransmitContext
453                                                     *tc,
454                                                     const struct
455                                                     GNUNET_MessageHeader *msg);
456
457
458 /**
459  * Execute a transmission context.  If there is
460  * an error in the transmission, the receive_done
461  * method will be called with an error code (GNUNET_SYSERR),
462  * otherwise with GNUNET_OK.
463  *
464  * @param tc transmission context to use
465  * @param timeout when to time out and abort the transmission
466  */
467 void GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext
468                                          *tc,
469                                          struct GNUNET_TIME_Relative timeout);
470
471
472
473 /**
474  * The notification context is the key datastructure for a conveniance
475  * API used for transmission of notifications to the client until the
476  * client disconnects (or the notification context is destroyed, in
477  * which case we disconnect these clients).  Essentially, all
478  * (notification) messages are queued up until the client is able to
479  * read them.
480  */
481 struct GNUNET_SERVER_NotificationContext;
482
483
484 /**
485  * Create a new notification context.
486  *
487  * @param server server for which this function creates the context
488  * @param queue_length maximum number of messages to keep in
489  *        the notification queue; optional messages are dropped
490  *        if the queue gets longer than this number of messages
491  * @return handle to the notification context
492  */
493 struct GNUNET_SERVER_NotificationContext
494     *GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle
495                                                 *server,
496                                                 unsigned int queue_length);
497
498
499 /**
500  * Destroy the context, force disconnect for all clients.
501  *
502  * @param nc context to destroy.
503  */
504 void GNUNET_SERVER_notification_context_destroy (struct
505                                                  GNUNET_SERVER_NotificationContext
506                                                  *nc);
507
508
509 /**
510  * Add a client to the notification context.
511  *
512  * @param nc context to modify
513  * @param client client to add
514  */
515 void GNUNET_SERVER_notification_context_add (struct
516                                              GNUNET_SERVER_NotificationContext
517                                              *nc,
518                                              struct GNUNET_SERVER_Client
519                                              *client);
520
521
522 /**
523  * Send a message to a particular client; must have
524  * already been added to the notification context.
525  *
526  * @param nc context to modify
527  * @param client client to transmit to
528  * @param msg message to send
529  * @param can_drop can this message be dropped due to queue length limitations
530  */
531 void GNUNET_SERVER_notification_context_unicast (struct
532                                                  GNUNET_SERVER_NotificationContext
533                                                  *nc,
534                                                  struct GNUNET_SERVER_Client
535                                                  *client,
536                                                  const struct
537                                                  GNUNET_MessageHeader *msg,
538                                                  int can_drop);
539
540
541 /**
542  * Send a message to all clients of this context.
543  *
544  * @param nc context to modify
545  * @param msg message to send
546  * @param can_drop can this message be dropped due to queue length limitations
547  */
548 void GNUNET_SERVER_notification_context_broadcast (struct
549                                                    GNUNET_SERVER_NotificationContext
550                                                    *nc,
551                                                    const struct
552                                                    GNUNET_MessageHeader *msg,
553                                                    int can_drop);
554
555
556
557 /**
558  * Handle to a message stream tokenizer.
559  */
560 struct GNUNET_SERVER_MessageStreamTokenizer;
561
562 /**
563  * Functions with this signature are called whenever a
564  * complete message is received by the tokenizer.
565  *
566  * @param cls closure
567  * @param client identification of the client
568  * @param message the actual message
569  */
570 typedef void (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
571                                                         const struct
572                                                         GNUNET_MessageHeader *
573                                                         message);
574
575
576 /**
577  * Create a message stream tokenizer.
578  *
579  * @param cb function to call on completed messages
580  * @param cb_cls closure for cb
581  * @return handle to tokenizer
582  */
583 struct GNUNET_SERVER_MessageStreamTokenizer
584     *GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
585                                void *cb_cls);
586
587
588 /**
589  * Add incoming data to the receive buffer and call the
590  * callback for all complete messages.
591  *
592  * @param mst tokenizer to use
593  * @param client_identity ID of client for which this is a buffer,
594  *        can be NULL (will be passed back to 'cb')
595  * @param buf input data to add
596  * @param size number of bytes in buf
597  * @param purge should any excess bytes in the buffer be discarded 
598  *       (i.e. for packet-based services like UDP)
599  * @param one_shot only call callback once, keep rest of message in buffer
600  * @return GNUNET_OK if we are done processing (need more data)
601  *         GNUNET_NO if one_shot was set and we have another message ready
602  *         GNUNET_SYSERR if the data stream is corrupt
603  */
604 int GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
605                                void *client_identity, const char *buf,
606                                size_t size, int purge, int one_shot);
607
608
609 /**
610  * Destroys a tokenizer.
611  *
612  * @param mst tokenizer to destroy
613  */
614 void GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer
615                                 *mst);
616
617
618 #if 0                           /* keep Emacsens' auto-indent happy */
619 {
620 #endif
621 #ifdef __cplusplus
622 }
623 #endif
624
625
626 /* ifndef GNUNET_SERVER_LIB_H */
627 #endif
628 /* end of gnunet_server_lib.h */