cab2eb3b6121ca529088e2b862bd07281b4e533a
[profile/ivi/libwebsockets.git] / lib / libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #ifndef __LIBWEBSOCKET_H__
23 #define __LIBWEBSOCKET_H__
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #ifdef WIN32
30
31 #ifndef WIN32_LEAN_AND_MEAN
32 #define WIN32_LEAN_AND_MEAN
33 #endif
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
36 #include "websock-w32.h"
37
38 #include "gettimeofday.h"
39
40 #ifdef LWS_DLL
41 #ifdef LWS_INTERNAL
42 #define LWS_EXTERN extern __declspec(dllexport)
43 #else
44 #define LWS_EXTERN extern __declspec(dllimport)
45 #endif
46 #endif
47
48 #else
49 #include <poll.h>
50 #endif
51
52 #ifndef LWS_EXTERN
53 #define LWS_EXTERN extern
54 #endif
55
56 #define CONTEXT_PORT_NO_LISTEN 0
57 #define MAX_MUX_RECURSION 2
58
59 enum libwebsocket_context_options {
60         LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK = 1,
61         LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
62 };
63
64 enum libwebsocket_callback_reasons {
65         LWS_CALLBACK_ESTABLISHED,
66         LWS_CALLBACK_CLIENT_ESTABLISHED,
67         LWS_CALLBACK_CLOSED,
68         LWS_CALLBACK_RECEIVE,
69         LWS_CALLBACK_CLIENT_RECEIVE,
70         LWS_CALLBACK_CLIENT_RECEIVE_PONG,
71         LWS_CALLBACK_CLIENT_WRITEABLE,
72         LWS_CALLBACK_SERVER_WRITEABLE,
73         LWS_CALLBACK_HTTP,
74         LWS_CALLBACK_BROADCAST,
75         LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
76         LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
77         LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
78         LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
79         LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
80         LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
81         LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
82         LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
83         /* external poll() management support */
84         LWS_CALLBACK_ADD_POLL_FD,
85         LWS_CALLBACK_DEL_POLL_FD,
86         LWS_CALLBACK_SET_MODE_POLL_FD,
87         LWS_CALLBACK_CLEAR_MODE_POLL_FD,
88 };
89
90 enum libwebsocket_extension_callback_reasons {
91         LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT,
92         LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT,
93         LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT,
94         LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT,
95         LWS_EXT_CALLBACK_CONSTRUCT,
96         LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
97         LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
98         LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
99         LWS_EXT_CALLBACK_DESTROY,
100         LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
101         LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED,
102         LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
103         LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
104         LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
105         LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
106         LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
107         LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
108         LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
109         LWS_EXT_CALLBACK_1HZ,
110         LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
111         LWS_EXT_CALLBACK_IS_WRITEABLE,
112 };
113
114 enum libwebsocket_write_protocol {
115         LWS_WRITE_TEXT,
116         LWS_WRITE_BINARY,
117         LWS_WRITE_CONTINUATION,
118         LWS_WRITE_HTTP,
119
120         /* special 04+ opcodes */
121
122         LWS_WRITE_CLOSE,
123         LWS_WRITE_PING,
124         LWS_WRITE_PONG,
125
126         /* flags */
127
128         LWS_WRITE_NO_FIN = 0x40,
129         /*
130          * client packet payload goes out on wire unmunged
131          * only useful for security tests since normal servers cannot
132          * decode the content if used
133          */
134         LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
135 };
136
137 /*
138  * you need these to look at headers that have been parsed if using the
139  * LWS_CALLBACK_FILTER_CONNECTION callback.  If a header from the enum
140  * list below is absent, .token = NULL and token_len = 0.  Otherwise .token
141  * points to .token_len chars containing that header content.
142  */
143
144 struct lws_tokens {
145         char *token;
146         int token_len;
147 };
148
149 enum lws_token_indexes {
150         WSI_TOKEN_GET_URI,
151         WSI_TOKEN_HOST,
152         WSI_TOKEN_CONNECTION,
153         WSI_TOKEN_KEY1,
154         WSI_TOKEN_KEY2,
155         WSI_TOKEN_PROTOCOL,
156         WSI_TOKEN_UPGRADE,
157         WSI_TOKEN_ORIGIN,
158         WSI_TOKEN_DRAFT,
159         WSI_TOKEN_CHALLENGE,
160
161         /* new for 04 */
162         WSI_TOKEN_KEY,
163         WSI_TOKEN_VERSION,
164         WSI_TOKEN_SWORIGIN,
165
166         /* new for 05 */
167         WSI_TOKEN_EXTENSIONS,
168
169         /* client receives these */
170         WSI_TOKEN_ACCEPT,
171         WSI_TOKEN_NONCE,
172         WSI_TOKEN_HTTP,
173         WSI_TOKEN_MUXURL,
174
175         /* always last real token index*/
176         WSI_TOKEN_COUNT,
177         /* parser state additions */
178         WSI_TOKEN_NAME_PART,
179         WSI_TOKEN_SKIPPING,
180         WSI_TOKEN_SKIPPING_SAW_CR,
181         WSI_PARSING_COMPLETE,
182         WSI_INIT_TOKEN_MUXURL,
183 };
184
185 /*
186  * From 06 spec
187    1000
188
189       1000 indicates a normal closure, meaning whatever purpose the
190       connection was established for has been fulfilled.
191
192    1001
193
194       1001 indicates that an endpoint is "going away", such as a server
195       going down, or a browser having navigated away from a page.
196
197    1002
198
199       1002 indicates that an endpoint is terminating the connection due
200       to a protocol error.
201
202    1003
203
204       1003 indicates that an endpoint is terminating the connection
205       because it has received a type of data it cannot accept (e.g. an
206       endpoint that understands only text data may send this if it
207       receives a binary message.)
208
209    1004
210
211       1004 indicates that an endpoint is terminating the connection
212       because it has received a message that is too large.
213 */
214
215 enum lws_close_status {
216         LWS_CLOSE_STATUS_NOSTATUS = 0,
217         LWS_CLOSE_STATUS_NORMAL = 1000,
218         LWS_CLOSE_STATUS_GOINGAWAY = 1001,
219         LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
220         LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
221         LWS_CLOSE_STATUS_PAYLOAD_TOO_LARGE = 1004,
222 };
223
224 struct libwebsocket;
225 struct libwebsocket_context;
226 struct libwebsocket_extension;
227
228 /* document the generic callback (it's a fake prototype under this) */
229 /**
230  * callback() - User server actions
231  * @context:    Websockets context
232  * @wsi:        Opaque websocket instance pointer
233  * @reason:     The reason for the call
234  * @user:       Pointer to per-session user data allocated by library
235  * @in:         Pointer used for some callback reasons
236  * @len:        Length set for some callback reasons
237  *
238  *      This callback is the way the user controls what is served.  All the
239  *      protocol detail is hidden and handled by the library.
240  *
241  *      For each connection / session there is user data allocated that is
242  *      pointed to by "user".  You set the size of this user data area when
243  *      the library is initialized with libwebsocket_create_server.
244  *
245  *      You get an opportunity to initialize user data when called back with
246  *      LWS_CALLBACK_ESTABLISHED reason.
247  *
248  *      LWS_CALLBACK_ESTABLISHED:  after the server completes a handshake with
249  *                              an incoming client
250  *
251  *      LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
252  *                              a handshake with the remote server
253  *
254  *      LWS_CALLBACK_CLOSED: when the websocket session ends
255  *
256  *      LWS_CALLBACK_BROADCAST: signal to send to client (you would use
257  *                              libwebsocket_write() taking care about the
258  *                              special buffer requirements
259  *
260  *      LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
261  *                              remote client, it can be found at *in and is
262  *                              len bytes long
263  *
264  *      LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
265  *                              they appear with this callback reason.  PONG
266  *                              packets only exist in 04+ protocol
267  *
268  *      LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
269  *                              client connection, it can be found at *in and
270  *                              is len bytes long
271  *
272  *      LWS_CALLBACK_HTTP: an http request has come from a client that is not
273  *                              asking to upgrade the connection to a websocket
274  *                              one.  This is a chance to serve http content,
275  *                              for example, to send a script to the client
276  *                              which will then open the websockets connection.
277  *                              @in points to the URI path requested and
278  *                              libwebsockets_serve_http_file() makes it very
279  *                              simple to send back a file to the client.
280  *
281  *      LWS_CALLBACK_CLIENT_WRITEABLE:
282  *      LWS_CALLBACK_SERVER_WRITEABLE:   If you call
283  *              libwebsocket_callback_on_writable() on a connection, you will
284  *              get one of these callbacks coming when the connection socket
285  *              is able to accept another write packet without blocking.
286  *              If it already was able to take another packet without blocking,
287  *              you'll get this callback at the next call to the service loop
288  *              function.  Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
289  *              and servers get LWS_CALLBACK_SERVER_WRITEABLE.
290  *
291  *      LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
292  *              the server at network level; the connection is accepted but then
293  *              passed to this callback to decide whether to hang up immediately
294  *              or not, based on the client IP.  @user contains the connection
295  *              socket's descriptor.  Return non-zero to terminate
296  *              the connection before sending or receiving anything.
297  *              Because this happens immediately after the network connection
298  *              from the client, there's no websocket protocol selected yet so
299  *              this callback is issued only to protocol 0.
300  *
301  *      LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
302  *              been received and parsed from the client, but the response is
303  *              not sent yet.  Return non-zero to disallow the connection.
304  *              @user is a pointer to an array of struct lws_tokens, you can
305  *              use the header enums lws_token_indexes from libwebsockets.h
306  *              to check for and read the supported header presence and
307  *              content before deciding to allow the handshake to proceed or
308  *              to kill the connection.
309  *
310  *      LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
311  *              including OpenSSL support, this callback allows your user code
312  *              to perform extra SSL_CTX_load_verify_locations() or similar
313  *              calls to direct OpenSSL where to find certificates the client
314  *              can use to confirm the remote server identity.  @user is the
315  *              OpenSSL SSL_CTX*
316  *
317  *      LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
318  *              including OpenSSL support, this callback allows your user code
319  *              to load extra certifcates into the server which allow it to
320  *              verify the validity of certificates returned by clients.  @user
321  *              is the server's OpenSSL SSL_CTX*
322  *
323  *      LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
324  *              libwebsockets context was created with the option
325  *              LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
326  *              callback is generated during OpenSSL verification of the cert
327  *              sent from the client.  It is sent to protocol[0] callback as
328  *              no protocol has been negotiated on the connection yet.
329  *              Notice that the libwebsockets context and wsi are both NULL
330  *              during this callback.  See
331  *               http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
332  *              to understand more detail about the OpenSSL callback that
333  *              generates this libwebsockets callback and the meanings of the
334  *              arguments passed.  In this callback, @user is the x509_ctx,
335  *              @in is the ssl pointer and @len is preverify_ok
336  *              Notice that this callback maintains libwebsocket return
337  *              conventions, return 0 to mean the cert is OK or 1 to fail it.
338  *              This also means that if you don't handle this callback then
339  *              the default callback action of returning 0 allows the client
340  *              certificates.
341  *
342  *      LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
343  *              when a client handshake is being compiled.  @user is NULL,
344  *              @in is a char **, it's pointing to a char * which holds the
345  *              next location in the header buffer where you can add
346  *              headers, and @len is the remaining space in the header buffer,
347  *              which is typically some hundreds of bytes.  So, to add a canned
348  *              cookie, your handler code might look similar to:
349  *
350  *              char **p = (char **)in;
351  *
352  *              if (len < 100)
353  *                      return 1;
354  *
355  *              *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
356  *
357  *              return 0;
358  *
359  *              Notice if you add anything, you just have to take care about
360  *              the CRLF on the line you added.  Obviously this callback is
361  *              optional, if you don't handle it everything is fine.
362  *
363  *              Notice the callback is coming to protocols[0] all the time,
364  *              because there is no specific protocol handshook yet.
365  *
366  *      LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
367  *              sees that it does support a requested extension, before
368  *              accepting the extension by additing to the list sent back to
369  *              the client it gives this callback just to check that it's okay
370  *              to use that extension.  It calls back to the requested protocol
371  *              and with @in being the extension name, @len is 0 and @user is
372  *              valid.  Note though at this time the ESTABLISHED callback hasn't
373  *              happened yet so if you initialize @user content there, @user
374  *              content during this callback might not be useful for anything.
375  *              Notice this callback comes to protocols[0].
376  *
377  *      LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:        When a client
378  *              connection is being prepared to start a handshake to a server,
379  *              each supported extension is checked with protocols[0] callback
380  *              with this reason, giving the user code a chance to suppress the
381  *              claim to support that extension by returning non-zero.  If
382  *              unhandled, by default 0 will be returned and the extension
383  *              support included in the header to the server.  Notice this
384  *              callback comes to protocols[0].
385  *
386  *      The next four reasons are optional and only need taking care of if you
387  *      will be integrating libwebsockets sockets into an external polling
388  *      array.
389  * 
390  *      LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
391  *              internally, but in the case you are integrating with another
392  *              server you will need to have libwebsocket sockets share a
393  *              polling array with the other server.  This and the other
394  *              POLL_FD related callbacks let you put your specialized
395  *              poll array interface code in the callback for protocol 0, the
396  *              first protocol you support, usually the HTTP protocol in the
397  *              serving case.  This callback happens when a socket needs to be
398  *              added to the polling loop: @user contains the fd, and
399  *              @len is the events bitmap (like, POLLIN).  If you are using the
400  *              internal polling loop (the "service" callback), you can just
401  *              ignore these callbacks.
402  *
403  *      LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
404  *              needs to be removed from an external polling array.  @user is
405  *              the socket desricptor.  If you are using the internal polling
406  *              loop, you can just ignore it.
407  *
408  *      LWS_CALLBACK_SET_MODE_POLL_FD: This callback happens when libwebsockets
409  *              wants to modify the events for the socket descriptor in @user.
410  *              The handler should OR @len on to the events member of the pollfd
411  *              struct for this socket descriptor.  If you are using the
412  *              internal polling loop, you can just ignore it.
413  *
414  *      LWS_CALLBACK_CLEAR_MODE_POLL_FD: This callback occurs when libwebsockets
415  *              wants to modify the events for the socket descriptor in @user.
416  *              The handler should AND ~@len on to the events member of the
417  *              pollfd struct for this socket descriptor.  If you are using the
418  *              internal polling loop, you can just ignore it.
419  */
420 LWS_EXTERN int callback(struct libwebsocket_context * context,
421                         struct libwebsocket *wsi,
422                          enum libwebsocket_callback_reasons reason, void *user,
423                                                           void *in, size_t len);
424
425 /* document the generic extension callback (it's a fake prototype under this) */
426 /**
427  * extension_callback() - Hooks to allow extensions to operate
428  * @context:    Websockets context
429  * @ext:        This extension
430  * @wsi:        Opaque websocket instance pointer
431  * @reason:     The reason for the call
432  * @user:       Pointer to per-session user data allocated by library
433  * @in:         Pointer used for some callback reasons
434  * @len:        Length set for some callback reasons
435  *
436  *      Each extension that is active on a particular connection receives
437  *      callbacks during the connection lifetime to allow the extension to
438  *      operate on websocket data and manage itself.
439  *
440  *      Libwebsockets takes care of allocating and freeing "user" memory for
441  *      each active extension on each connection.  That is what is pointed to
442  *      by the @user parameter.
443  *
444  *      LWS_EXT_CALLBACK_CONSTRUCT:  called when the server has decided to
445  *              select this extension from the list provided by the client,
446  *              just before the server will send back the handshake accepting
447  *              the connection with this extension active.  This gives the
448  *              extension a chance to initialize its connection context found
449  *              in @user.
450  *
451  *      LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT
452  *              but called when client is instantiating this extension.  Some
453  *              extensions will work the same on client and server side and then
454  *              you can just merge handlers for both CONSTRUCTS.
455  *
456  *      LWS_EXT_CALLBACK_DESTROY:  called when the connection the extension was
457  *              being used on is about to be closed and deallocated.  It's the
458  *              last chance for the extension to deallocate anything it has
459  *              allocated in the user data (pointed to by @user) before the
460  *              user data is deleted.  This same callback is used whether you
461  *              are in client or server instantiation context.
462  *
463  *      LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on
464  *              a connection, and a packet of data arrived at the connection,
465  *              it is passed to this callback to give the extension a chance to
466  *              change the data, eg, decompress it.  @user is pointing to the
467  *              extension's private connection context data, @in is pointing
468  *              to an lws_tokens struct, it consists of a char * pointer called
469  *              token, and an int called token_len.  At entry, these are
470  *              set to point to the received buffer and set to the content
471  *              length.  If the extension will grow the content, it should use
472  *              a new buffer allocated in its private user context data and
473  *              set the pointed-to lws_tokens members to point to its buffer.
474  *
475  *      LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as
476  *              LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
477  *              extension a chance to change websocket data just before it will
478  *              be sent out.  Using the same lws_token pointer scheme in @in,
479  *              the extension can change the buffer and the length to be
480  *              transmitted how it likes.  Again if it wants to grow the
481  *              buffer safely, it should copy the data into its own buffer and
482  *              set the lws_tokens token pointer to it.
483  */
484
485 LWS_EXTERN int extension_callback(struct libwebsocket_context * context,
486                         struct libwebsocket_extension *ext,
487                         struct libwebsocket *wsi,
488                          enum libwebsocket_callback_reasons reason, void *user,
489                                                           void *in, size_t len);
490
491
492 /**
493  * struct libwebsocket_protocols -      List of protocols and handlers server
494  *                                      supports.
495  * @name:       Protocol name that must match the one given in the client
496  *              Javascript new WebSocket(url, 'protocol') name
497  * @callback:   The service callback used for this protocol.  It allows the
498  *              service action for an entire protocol to be encapsulated in
499  *              the protocol-specific callback
500  * @per_session_data_size:      Each new connection using this protocol gets
501  *              this much memory allocated on connection establishment and
502  *              freed on connection takedown.  A pointer to this per-connection
503  *              allocation is passed into the callback in the 'user' parameter
504  * @owning_server:      the server init call fills in this opaque pointer when
505  *              registering this protocol with the server.
506  * @broadcast_socket_port: the server init call fills this in with the
507  *              localhost port number used to forward broadcasts for this
508  *              protocol
509  * @broadcast_socket_user_fd:  the server init call fills this in ... the main()
510  *              process context can write to this socket to perform broadcasts
511  *              (use the libwebsockets_broadcast() api to do this instead,
512  *              it works from any process context)
513  * @protocol_index: which protocol we are starting from zero
514  *
515  *      This structure represents one protocol supported by the server.  An
516  *      array of these structures is passed to libwebsocket_create_server()
517  *      allows as many protocols as you like to be handled by one server.
518  */
519
520 struct libwebsocket_protocols {
521         const char *name;
522         int (*callback)(struct libwebsocket_context * context,
523                         struct libwebsocket *wsi,
524                         enum libwebsocket_callback_reasons reason, void *user,
525                                                           void *in, size_t len);
526         size_t per_session_data_size;
527
528         /*
529          * below are filled in on server init and can be left uninitialized,
530          * no need for user to use them directly either
531          */
532
533         struct libwebsocket_context *owning_server;
534         int broadcast_socket_port;
535         int broadcast_socket_user_fd;
536         int protocol_index;
537 };
538
539 /**
540  * struct libwebsocket_extension -      An extension we know how to cope with
541  *
542  * @name:                       Formal extension name, eg, "deflate-stream"
543  * @callback:                   Service callback
544  * @per_session_data_size:      Libwebsockets will auto-malloc this much
545  *                              memory for the use of the extension, a pointer
546  *                              to it comes in the @user callback parameter
547  */
548
549 struct libwebsocket_extension {
550         const char *name;
551         int (*callback)(struct libwebsocket_context *context,
552                         struct libwebsocket_extension *ext,
553                         struct libwebsocket *wsi,
554                         enum libwebsocket_extension_callback_reasons reason,
555                                               void *user, void *in, size_t len);
556         size_t per_session_data_size;
557         void * per_context_private_data;
558 };
559
560
561
562 LWS_EXTERN struct libwebsocket_context *
563 libwebsocket_create_context(int port, const char * interf,
564                   struct libwebsocket_protocols *protocols,
565                   struct libwebsocket_extension *extensions,
566                   const char *ssl_cert_filepath,
567                   const char *ssl_private_key_filepath, int gid, int uid,
568                   unsigned int options);
569
570 LWS_EXTERN void
571 libwebsocket_context_destroy(struct libwebsocket_context *context);
572
573 LWS_EXTERN int
574 libwebsockets_fork_service_loop(struct libwebsocket_context *context);
575
576 LWS_EXTERN int
577 libwebsocket_service(struct libwebsocket_context *context, int timeout_ms);
578
579 LWS_EXTERN int
580 libwebsocket_service_fd(struct libwebsocket_context *context,
581                                                          struct pollfd *pollfd);
582
583 /*
584  * IMPORTANT NOTICE!
585  *
586  * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
587  * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
588  * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
589  *
590  * This allows us to add protocol info before and after the data, and send as
591  * one packet on the network without payload copying, for maximum efficiency.
592  *
593  * So for example you need this kind of code to use libwebsocket_write with a
594  * 128-byte payload
595  *
596  *   char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
597  *
598  *   // fill your part of the buffer... for example here it's all zeros
599  *   memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
600  *
601  *   libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128);
602  *
603  * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
604  * use the whole buffer without taking care of the above.
605  */
606
607 /*
608  * this is the frame nonce plus two header plus 8 length
609  *   there's an additional two for mux extension per mux nesting level
610  * 2 byte prepend on close will already fit because control frames cannot use
611  * the big length style
612  */
613
614 #define LWS_SEND_BUFFER_PRE_PADDING (4 + 10 + (2 * MAX_MUX_RECURSION))
615 #define LWS_SEND_BUFFER_POST_PADDING 1
616
617 LWS_EXTERN int
618 libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len,
619                                      enum libwebsocket_write_protocol protocol);
620
621 LWS_EXTERN int
622 libwebsockets_serve_http_file(struct libwebsocket *wsi, const char *file,
623                                                      const char *content_type);
624
625 /* notice - you need the pre- and post- padding allocation for buf below */
626
627 LWS_EXTERN int
628 libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
629                                                 unsigned char *buf, size_t len);
630
631 LWS_EXTERN const struct libwebsocket_protocols *
632 libwebsockets_get_protocol(struct libwebsocket *wsi);
633
634 LWS_EXTERN int
635 libwebsocket_callback_on_writable(struct libwebsocket_context *context,
636                                                       struct libwebsocket *wsi);
637
638 LWS_EXTERN int
639 libwebsocket_callback_on_writable_all_protocol(
640                                  const struct libwebsocket_protocols *protocol);
641
642 LWS_EXTERN int
643 libwebsocket_get_socket_fd(struct libwebsocket *wsi);
644
645 LWS_EXTERN int
646 libwebsocket_is_final_fragment(struct libwebsocket *wsi);
647
648 LWS_EXTERN int
649 libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable);
650
651 LWS_EXTERN size_t
652 libwebsockets_remaining_packet_payload(struct libwebsocket *wsi);
653
654 LWS_EXTERN struct libwebsocket *
655 libwebsocket_client_connect(struct libwebsocket_context *clients,
656                               const char *address,
657                               int port,
658                               int ssl_connection,
659                               const char *path,
660                               const char *host,
661                               const char *origin,
662                               const char *protocol,
663                               int ietf_version_or_minus_one);
664
665 LWS_EXTERN const char *
666 libwebsocket_canonical_hostname(struct libwebsocket_context *context);
667
668
669 LWS_EXTERN void
670 libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
671                                         char *rip, int rip_len);
672
673 LWS_EXTERN void
674 libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd);
675
676 LWS_EXTERN void
677 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
678                                struct libwebsocket *wsi, enum lws_close_status);
679
680 LWS_EXTERN int
681 libwebsockets_get_random(struct libwebsocket_context *context,
682                                                             void *buf, int len);
683
684 LWS_EXTERN int
685 lws_send_pipe_choked(struct libwebsocket *wsi);
686
687 LWS_EXTERN unsigned char *
688 libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md);
689
690 LWS_EXTERN int
691 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
692
693 LWS_EXTERN int
694 lws_b64_decode_string(const char *in, char *out, int out_size);
695
696 LWS_EXTERN struct libwebsocket_extension libwebsocket_internal_extensions[];
697
698 #ifdef __cplusplus
699 }
700 #endif
701
702 #endif