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