blob: 28e649c67e03b8f08b89ddf4fa7482727754b43b [file] [log] [blame]
Andy Greena0da8a82010-11-08 17:12:19 +00001/*
2 * libwebsockets - small server side websockets and web server implementation
Andy Greene77ddd82010-11-13 10:03:47 +00003 *
Andy Green7b405452013-02-01 10:50:15 +08004 * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
Andy Greena0da8a82010-11-08 17:12:19 +00005 *
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
Andy Greenab990e42010-10-31 12:42:52 +000022#ifndef __LIBWEBSOCKET_H__
23#define __LIBWEBSOCKET_H__
24
Andy Greena11fe942011-09-25 10:30:26 +010025#ifdef __cplusplus
26extern "C" {
Paulo Roberto Urio4144e832012-06-04 09:04:33 +080027#include <cstddef>
Andy Greena11fe942011-09-25 10:30:26 +010028#endif
29
Peter Hinz56885f32011-03-02 22:03:47 +000030#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>
Paulo Roberto Urio4144e832012-06-04 09:04:33 +080037#include <stddef.h>
Andy Green2a480ea2012-04-12 10:54:22 +080038#include "../win32port/win32helpers/websock-w32.h"
Peter Hinz56885f32011-03-02 22:03:47 +000039
Andy Greena3147882012-04-12 11:02:06 +080040#include "../win32port/win32helpers/gettimeofday.h"
Michel Archambault03bec392011-05-24 17:15:25 +010041
Oleg Golosovskiy2ef75052011-10-04 20:20:14 +080042#define strcasecmp stricmp
Andy Greendfb23042013-01-17 12:26:48 +080043#define getdtablesize() 30000
Oleg Golosovskiy2ef75052011-10-04 20:20:14 +080044
David Brooks06fbcee2012-05-02 06:02:45 +080045typedef int ssize_t;
46
David Galeano9454e212011-09-26 12:17:20 +010047#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
Joakim Soderbergf272cb02013-02-13 09:29:26 +080055#else // NOT WIN32
Andy Green9f990342011-02-12 11:57:45 +000056#include <poll.h>
Andy Greendfb23042013-01-17 12:26:48 +080057#include <unistd.h>
Peter Hinz56885f32011-03-02 22:03:47 +000058#endif
Andy Green9f990342011-02-12 11:57:45 +000059
Peter Young27b32372013-01-17 10:10:10 +080060#include <assert.h>
61
David Galeano9454e212011-09-26 12:17:20 +010062#ifndef LWS_EXTERN
63#define LWS_EXTERN extern
64#endif
65
Andy Green4739e5c2011-01-22 12:51:57 +000066#define CONTEXT_PORT_NO_LISTEN 0
Andy Greena41314f2011-05-23 10:00:03 +010067#define MAX_MUX_RECURSION 2
Andy Greenbfb051f2011-02-09 08:49:14 +000068
Andy Green43db0452013-01-10 19:50:35 +080069enum lws_log_levels {
70 LLL_ERR = 1 << 0,
71 LLL_WARN = 1 << 1,
Andy Green7c19c342013-01-19 12:18:07 +080072 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,
Andy Greend636e352013-01-29 12:36:17 +080079 LLL_LATENCY = 1 << 9,
Andy Green43db0452013-01-10 19:50:35 +080080
Andy Greend636e352013-01-29 12:36:17 +080081 LLL_COUNT = 10 /* set to count of valid flags */
Andy Green43db0452013-01-10 19:50:35 +080082};
83
Joakim Soderbergf272cb02013-02-13 09:29:26 +080084LWS_EXTERN void _lws_log(int filter, const char *format, ...);
Andy Green69e43642013-01-19 11:58:07 +080085
Andy Greend636e352013-01-29 12:36:17 +080086/* notice, warn and log are always compiled in */
Andy Green69e43642013-01-19 11:58:07 +080087#define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
88#define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
89#define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
Andy Green69e43642013-01-19 11:58:07 +080090/*
Andy Greend636e352013-01-29 12:36:17 +080091 * weaker logging can be deselected at configure time using --disable-debug
Andy Green69e43642013-01-19 11:58:07 +080092 * that gets rid of the overhead of checking while keeping _warn and _err
93 * active
94 */
95#ifdef _DEBUG
Andy Greend636e352013-01-29 12:36:17 +080096
Andy Green69e43642013-01-19 11:58:07 +080097#define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
98#define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
99#define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
100#define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
Andy Greend636e352013-01-29 12:36:17 +0800101#define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
Andy Green69e43642013-01-19 11:58:07 +0800102#define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
Andy Greend636e352013-01-29 12:36:17 +0800103#define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800104LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
Andy Greend636e352013-01-29 12:36:17 +0800105
Andy Green69e43642013-01-19 11:58:07 +0800106#else /* no debug */
Andy Greend636e352013-01-29 12:36:17 +0800107
Andy Green69e43642013-01-19 11:58:07 +0800108#define lwsl_info(...)
109#define lwsl_debug(...)
110#define lwsl_parser(...)
111#define lwsl_header(...)
112#define lwsl_ext(...)
113#define lwsl_client(...)
Andy Greend636e352013-01-29 12:36:17 +0800114#define lwsl_latency(...)
Andy Green69e43642013-01-19 11:58:07 +0800115#define lwsl_hexdump(a, b)
Andy Greend636e352013-01-29 12:36:17 +0800116
Andy Green69e43642013-01-19 11:58:07 +0800117#endif
118
Andy Greenbfb051f2011-02-09 08:49:14 +0000119enum libwebsocket_context_options {
Andy Greenc6bf2c22011-02-20 11:10:47 +0000120 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
Andy Green788c4a82012-10-22 12:29:57 +0100121 LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
Andy Greenbfb051f2011-02-09 08:49:14 +0000122};
Andy Green8014b292011-01-30 20:57:25 +0000123
Andy Green775c0dd2010-10-29 14:15:22 +0100124enum libwebsocket_callback_reasons {
125 LWS_CALLBACK_ESTABLISHED,
David Brooks80a44972012-04-20 12:18:47 +0800126 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
Andy Green2b57a342013-02-06 15:15:25 +0900127 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
Andy Green90c7cbc2011-01-27 06:26:52 +0000128 LWS_CALLBACK_CLIENT_ESTABLISHED,
Andy Green775c0dd2010-10-29 14:15:22 +0100129 LWS_CALLBACK_CLOSED,
Andy Green775c0dd2010-10-29 14:15:22 +0100130 LWS_CALLBACK_RECEIVE,
Andy Green4739e5c2011-01-22 12:51:57 +0000131 LWS_CALLBACK_CLIENT_RECEIVE,
Andy Greena6cbece2011-01-27 20:06:03 +0000132 LWS_CALLBACK_CLIENT_RECEIVE_PONG,
Andy Green90c7cbc2011-01-27 06:26:52 +0000133 LWS_CALLBACK_CLIENT_WRITEABLE,
Andy Green9e4c2b62011-03-07 20:47:39 +0000134 LWS_CALLBACK_SERVER_WRITEABLE,
Andy Greena2b0ab02010-11-11 12:28:29 +0000135 LWS_CALLBACK_HTTP,
Andy Greend280b6e2013-01-15 13:40:23 +0800136 LWS_CALLBACK_HTTP_FILE_COMPLETION,
Andy Green54cb3462013-02-14 22:23:54 +0800137 LWS_CALLBACK_HTTP_WRITEABLE,
Andy Green07034092011-02-13 08:37:12 +0000138 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
Andy Greenc85619d2011-02-13 08:25:26 +0000139 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
Andy Green0894bda2011-02-19 09:09:11 +0000140 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
Andy Greenc6bf2c22011-02-20 11:10:47 +0000141 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Andy Green6901cb32011-02-21 08:06:47 +0000142 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
Andy Green385e7ad2011-03-01 21:06:02 +0000143 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
Andy Greenc5114822011-03-06 10:29:35 +0000144 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
Andy Greenc6517fa2011-03-06 13:15:29 +0000145 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
Andy Greena7109e62013-02-11 12:05:54 +0800146 LWS_CALLBACK_PROTOCOL_INIT,
147 LWS_CALLBACK_PROTOCOL_DESTROY,
Andy Green3221f922011-02-12 13:14:11 +0000148 /* external poll() management support */
149 LWS_CALLBACK_ADD_POLL_FD,
150 LWS_CALLBACK_DEL_POLL_FD,
151 LWS_CALLBACK_SET_MODE_POLL_FD,
152 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green5fd8a5e2010-10-31 11:57:17 +0000153};
154
Andy Green3182ece2013-01-20 17:08:31 +0800155#ifndef LWS_NO_EXTENSIONS
Andy Greenc5114822011-03-06 10:29:35 +0000156enum libwebsocket_extension_callback_reasons {
Andy Greena41314f2011-05-23 10:00:03 +0100157 LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT,
158 LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT,
159 LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT,
160 LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT,
Andy Greenc5114822011-03-06 10:29:35 +0000161 LWS_EXT_CALLBACK_CONSTRUCT,
Andy Green2366b1c2011-03-06 13:15:31 +0000162 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
Andy Green68b45042011-05-25 21:41:57 +0100163 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
Andy Green09226502011-05-28 10:19:19 +0100164 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
Andy Greenc5114822011-03-06 10:29:35 +0000165 LWS_EXT_CALLBACK_DESTROY,
Andy Greena41314f2011-05-23 10:00:03 +0100166 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
167 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED,
Andy Green98a717c2011-03-06 13:14:15 +0000168 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green3b84c002011-03-06 13:14:42 +0000169 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
Andy Greena41314f2011-05-23 10:00:03 +0100170 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
171 LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
Andy Greenc44159f2011-03-07 07:08:18 +0000172 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
Andy Greena41314f2011-05-23 10:00:03 +0100173 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
174 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
175 LWS_EXT_CALLBACK_1HZ,
176 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
177 LWS_EXT_CALLBACK_IS_WRITEABLE,
David Galeanoe2cf9922013-01-09 18:06:55 +0800178 LWS_EXT_CALLBACK_PAYLOAD_TX,
179 LWS_EXT_CALLBACK_PAYLOAD_RX,
Andy Greenc5114822011-03-06 10:29:35 +0000180};
Andy Green3182ece2013-01-20 17:08:31 +0800181#endif
Andy Greenc5114822011-03-06 10:29:35 +0000182
Andy Green5fd8a5e2010-10-31 11:57:17 +0000183enum libwebsocket_write_protocol {
184 LWS_WRITE_TEXT,
185 LWS_WRITE_BINARY,
Andy Green5d9d94b2011-03-07 20:47:41 +0000186 LWS_WRITE_CONTINUATION,
Andy Green38e57bb2011-01-19 12:20:27 +0000187 LWS_WRITE_HTTP,
188
Andy Green5e1fa172011-02-10 09:07:05 +0000189 /* special 04+ opcodes */
Andy Green38e57bb2011-01-19 12:20:27 +0000190
191 LWS_WRITE_CLOSE,
192 LWS_WRITE_PING,
Andy Green90c7cbc2011-01-27 06:26:52 +0000193 LWS_WRITE_PONG,
194
Andy Greenbd96d802011-01-30 08:24:31 +0000195 /* flags */
196
197 LWS_WRITE_NO_FIN = 0x40,
198 /*
199 * client packet payload goes out on wire unmunged
200 * only useful for security tests since normal servers cannot
201 * decode the content if used
202 */
203 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
Andy Greenff95d7a2010-10-28 22:36:01 +0100204};
205
Andy Greenc85619d2011-02-13 08:25:26 +0000206/*
207 * you need these to look at headers that have been parsed if using the
208 * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
209 * list below is absent, .token = NULL and token_len = 0. Otherwise .token
210 * points to .token_len chars containing that header content.
211 */
212
213struct lws_tokens {
214 char *token;
215 int token_len;
216};
217
218enum lws_token_indexes {
219 WSI_TOKEN_GET_URI,
220 WSI_TOKEN_HOST,
221 WSI_TOKEN_CONNECTION,
222 WSI_TOKEN_KEY1,
223 WSI_TOKEN_KEY2,
224 WSI_TOKEN_PROTOCOL,
225 WSI_TOKEN_UPGRADE,
226 WSI_TOKEN_ORIGIN,
227 WSI_TOKEN_DRAFT,
228 WSI_TOKEN_CHALLENGE,
229
230 /* new for 04 */
231 WSI_TOKEN_KEY,
232 WSI_TOKEN_VERSION,
233 WSI_TOKEN_SWORIGIN,
234
235 /* new for 05 */
236 WSI_TOKEN_EXTENSIONS,
237
238 /* client receives these */
239 WSI_TOKEN_ACCEPT,
240 WSI_TOKEN_NONCE,
241 WSI_TOKEN_HTTP,
Andy Greena41314f2011-05-23 10:00:03 +0100242 WSI_TOKEN_MUXURL,
Andy Greenc85619d2011-02-13 08:25:26 +0000243
Andy Greene77fb802013-02-11 13:04:45 +0800244 /* use token storage to stash these */
245
246 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
247 _WSI_TOKEN_CLIENT_PEER_ADDRESS,
248 _WSI_TOKEN_CLIENT_URI,
249 _WSI_TOKEN_CLIENT_HOST,
250 _WSI_TOKEN_CLIENT_ORIGIN,
251
Andy Greenc85619d2011-02-13 08:25:26 +0000252 /* always last real token index*/
253 WSI_TOKEN_COUNT,
254 /* parser state additions */
255 WSI_TOKEN_NAME_PART,
256 WSI_TOKEN_SKIPPING,
257 WSI_TOKEN_SKIPPING_SAW_CR,
Andy Greena41314f2011-05-23 10:00:03 +0100258 WSI_PARSING_COMPLETE,
259 WSI_INIT_TOKEN_MUXURL,
Andy Greenc85619d2011-02-13 08:25:26 +0000260};
261
Andy Green687b0182011-02-26 11:04:01 +0000262/*
David Galeano2915a802013-01-09 15:26:08 +0800263 * From RFC 6455
Andy Green687b0182011-02-26 11:04:01 +0000264 1000
265
David Galeano2915a802013-01-09 15:26:08 +0800266 1000 indicates a normal closure, meaning that the purpose for
267 which the connection was established has been fulfilled.
Andy Green687b0182011-02-26 11:04:01 +0000268
269 1001
270
271 1001 indicates that an endpoint is "going away", such as a server
David Galeano2915a802013-01-09 15:26:08 +0800272 going down or a browser having navigated away from a page.
Andy Green687b0182011-02-26 11:04:01 +0000273
274 1002
275
276 1002 indicates that an endpoint is terminating the connection due
277 to a protocol error.
278
279 1003
280
281 1003 indicates that an endpoint is terminating the connection
David Galeano2915a802013-01-09 15:26:08 +0800282 because it has received a type of data it cannot accept (e.g., an
283 endpoint that understands only text data MAY send this if it
284 receives a binary message).
Andy Green687b0182011-02-26 11:04:01 +0000285
286 1004
287
David Galeano2915a802013-01-09 15:26:08 +0800288 Reserved. The specific meaning might be defined in the future.
289
290 1005
291
292 1005 is a reserved value and MUST NOT be set as a status code in a
293 Close control frame by an endpoint. It is designated for use in
294 applications expecting a status code to indicate that no status
295 code was actually present.
296
297 1006
298
299 1006 is a reserved value and MUST NOT be set as a status code in a
300 Close control frame by an endpoint. It is designated for use in
301 applications expecting a status code to indicate that the
302 connection was closed abnormally, e.g., without sending or
303 receiving a Close control frame.
304
305 1007
306
307 1007 indicates that an endpoint is terminating the connection
308 because it has received data within a message that was not
309 consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
310 data within a text message).
311
312 1008
313
314 1008 indicates that an endpoint is terminating the connection
315 because it has received a message that violates its policy. This
316 is a generic status code that can be returned when there is no
317 other more suitable status code (e.g., 1003 or 1009) or if there
318 is a need to hide specific details about the policy.
319
320 1009
321
322 1009 indicates that an endpoint is terminating the connection
323 because it has received a message that is too big for it to
324 process.
325
326 1010
327
328 1010 indicates that an endpoint (client) is terminating the
329 connection because it has expected the server to negotiate one or
330 more extension, but the server didn't return them in the response
331 message of the WebSocket handshake. The list of extensions that
332 are needed SHOULD appear in the /reason/ part of the Close frame.
333 Note that this status code is not used by the server, because it
334 can fail the WebSocket handshake instead.
335
336 1011
337
338 1011 indicates that a server is terminating the connection because
339 it encountered an unexpected condition that prevented it from
340 fulfilling the request.
341
342 1015
343
344 1015 is a reserved value and MUST NOT be set as a status code in a
345 Close control frame by an endpoint. It is designated for use in
346 applications expecting a status code to indicate that the
347 connection was closed due to a failure to perform a TLS handshake
348 (e.g., the server certificate can't be verified).
Andy Green687b0182011-02-26 11:04:01 +0000349*/
350
351enum lws_close_status {
352 LWS_CLOSE_STATUS_NOSTATUS = 0,
353 LWS_CLOSE_STATUS_NORMAL = 1000,
354 LWS_CLOSE_STATUS_GOINGAWAY = 1001,
355 LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
356 LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
David Galeano2915a802013-01-09 15:26:08 +0800357 LWS_CLOSE_STATUS_RESERVED = 1004,
358 LWS_CLOSE_STATUS_NO_STATUS = 1005,
359 LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
360 LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
361 LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
362 LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
363 LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
Andy Greenb5b23192013-02-11 17:13:32 +0800364 LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
365 LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
Andy Green687b0182011-02-26 11:04:01 +0000366};
367
Andy Green775c0dd2010-10-29 14:15:22 +0100368struct libwebsocket;
Andy Greenb45993c2010-12-18 15:13:50 +0000369struct libwebsocket_context;
Andy Green3182ece2013-01-20 17:08:31 +0800370/* needed even with extensions disabled for create context */
Andy Green46c2ea02011-03-22 09:04:01 +0000371struct libwebsocket_extension;
Andy Greenff95d7a2010-10-28 22:36:01 +0100372
Andy Green8f037e42010-12-19 22:13:26 +0000373/**
David Brooks6c6a3d32012-04-20 12:13:50 +0800374 * callback_function() - User server actions
Andy Green32375b72011-02-19 08:32:53 +0000375 * @context: Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000376 * @wsi: Opaque websocket instance pointer
377 * @reason: The reason for the call
378 * @user: Pointer to per-session user data allocated by library
379 * @in: Pointer used for some callback reasons
380 * @len: Length set for some callback reasons
381 *
382 * This callback is the way the user controls what is served. All the
383 * protocol detail is hidden and handled by the library.
Andy Green6964bb52011-01-23 16:50:33 +0000384 *
Andy Green8f037e42010-12-19 22:13:26 +0000385 * For each connection / session there is user data allocated that is
386 * pointed to by "user". You set the size of this user data area when
387 * the library is initialized with libwebsocket_create_server.
Andy Green6964bb52011-01-23 16:50:33 +0000388 *
Andy Green8f037e42010-12-19 22:13:26 +0000389 * You get an opportunity to initialize user data when called back with
390 * LWS_CALLBACK_ESTABLISHED reason.
391 *
Andy Green2b57a342013-02-06 15:15:25 +0900392 * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
Andy Green90c7cbc2011-01-27 06:26:52 +0000393 * an incoming client
394 *
David Brooks80a44972012-04-20 12:18:47 +0800395 * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
396 * been unable to complete a handshake with the remote server
397 *
Andy Green2b57a342013-02-06 15:15:25 +0900398 * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
Andy Greenb5b23192013-02-11 17:13:32 +0800399 * client user code to examine the http headers
400 * and decide to reject the connection. If the
401 * content in the headers is interesting to the
402 * client (url, etc) it needs to copy it out at
403 * this point since it will be destroyed before
404 * the CLIENT_ESTABLISHED call
Andy Green2b57a342013-02-06 15:15:25 +0900405 *
David Brooks80a44972012-04-20 12:18:47 +0800406 * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
Andy Green90c7cbc2011-01-27 06:26:52 +0000407 * a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000408 *
409 * LWS_CALLBACK_CLOSED: when the websocket session ends
410 *
Andy Green90c7cbc2011-01-27 06:26:52 +0000411 * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
412 * remote client, it can be found at *in and is
413 * len bytes long
414 *
Andy Greena6cbece2011-01-27 20:06:03 +0000415 * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
416 * they appear with this callback reason. PONG
417 * packets only exist in 04+ protocol
418 *
Andy Green90c7cbc2011-01-27 06:26:52 +0000419 * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
420 * client connection, it can be found at *in and
421 * is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000422 *
Andy Green6964bb52011-01-23 16:50:33 +0000423 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
Andy Green8f037e42010-12-19 22:13:26 +0000424 * asking to upgrade the connection to a websocket
425 * one. This is a chance to serve http content,
426 * for example, to send a script to the client
427 * which will then open the websockets connection.
Andy Green6964bb52011-01-23 16:50:33 +0000428 * @in points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000429 * libwebsockets_serve_http_file() makes it very
430 * simple to send back a file to the client.
Andy Green24b588b2013-01-13 09:53:18 +0800431 * Normally after sending the file you are done
432 * with the http connection, since the rest of the
433 * activity will come by websockets from the script
434 * that was delivered by http, so you will want to
435 * return 1; to close and free up the connection.
436 * That's important because it uses a slot in the
437 * total number of client connections allowed set
438 * by MAX_CLIENTS.
Andy Green90c7cbc2011-01-27 06:26:52 +0000439 *
Andy Green54cb3462013-02-14 22:23:54 +0800440 * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
441 * link now.
442 *
Andy Greend280b6e2013-01-15 13:40:23 +0800443 * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
444 * http link has completed.
445 *
Andy Green9e4c2b62011-03-07 20:47:39 +0000446 * LWS_CALLBACK_CLIENT_WRITEABLE:
447 * LWS_CALLBACK_SERVER_WRITEABLE: If you call
Andy Green90c7cbc2011-01-27 06:26:52 +0000448 * libwebsocket_callback_on_writable() on a connection, you will
Andy Green9e4c2b62011-03-07 20:47:39 +0000449 * get one of these callbacks coming when the connection socket
450 * is able to accept another write packet without blocking.
451 * If it already was able to take another packet without blocking,
452 * you'll get this callback at the next call to the service loop
453 * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
454 * and servers get LWS_CALLBACK_SERVER_WRITEABLE.
Andy Greene7981dc2011-02-12 21:24:03 +0000455 *
Andy Green07034092011-02-13 08:37:12 +0000456 * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
457 * the server at network level; the connection is accepted but then
458 * passed to this callback to decide whether to hang up immediately
Edwin van den Oetelaar8c8a8e12013-02-20 20:56:59 +0800459 * or not, based on the client IP. @in contains the connection
Andy Green07034092011-02-13 08:37:12 +0000460 * socket's descriptor. Return non-zero to terminate
461 * the connection before sending or receiving anything.
Andy Greenb5b23192013-02-11 17:13:32 +0800462 * Because this happens immediately after the network connection
Andy Green07034092011-02-13 08:37:12 +0000463 * from the client, there's no websocket protocol selected yet so
464 * this callback is issued only to protocol 0.
465 *
Andy Greenb5b23192013-02-11 17:13:32 +0800466 * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
467 * been received and parsed from the client, but the response is
468 * not sent yet. Return non-zero to disallow the connection.
Andy Greenc85619d2011-02-13 08:25:26 +0000469 * @user is a pointer to an array of struct lws_tokens, you can
470 * use the header enums lws_token_indexes from libwebsockets.h
471 * to check for and read the supported header presence and
472 * content before deciding to allow the handshake to proceed or
473 * to kill the connection.
474 *
Andy Greenb5b23192013-02-11 17:13:32 +0800475 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
476 * including OpenSSL support, this callback allows your user code
477 * to perform extra SSL_CTX_load_verify_locations() or similar
Andy Green0894bda2011-02-19 09:09:11 +0000478 * calls to direct OpenSSL where to find certificates the client
479 * can use to confirm the remote server identity. @user is the
480 * OpenSSL SSL_CTX*
Andy Greenc85619d2011-02-13 08:25:26 +0000481 *
Andy Greenc6bf2c22011-02-20 11:10:47 +0000482 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
483 * including OpenSSL support, this callback allows your user code
484 * to load extra certifcates into the server which allow it to
485 * verify the validity of certificates returned by clients. @user
486 * is the server's OpenSSL SSL_CTX*
487 *
Andy Green6901cb32011-02-21 08:06:47 +0000488 * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
489 * libwebsockets context was created with the option
490 * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
491 * callback is generated during OpenSSL verification of the cert
492 * sent from the client. It is sent to protocol[0] callback as
493 * no protocol has been negotiated on the connection yet.
494 * Notice that the libwebsockets context and wsi are both NULL
495 * during this callback. See
496 * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
497 * to understand more detail about the OpenSSL callback that
498 * generates this libwebsockets callback and the meanings of the
499 * arguments passed. In this callback, @user is the x509_ctx,
500 * @in is the ssl pointer and @len is preverify_ok
501 * Notice that this callback maintains libwebsocket return
502 * conventions, return 0 to mean the cert is OK or 1 to fail it.
503 * This also means that if you don't handle this callback then
504 * the default callback action of returning 0 allows the client
505 * certificates.
506 *
Andy Green385e7ad2011-03-01 21:06:02 +0000507 * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
508 * when a client handshake is being compiled. @user is NULL,
509 * @in is a char **, it's pointing to a char * which holds the
510 * next location in the header buffer where you can add
511 * headers, and @len is the remaining space in the header buffer,
512 * which is typically some hundreds of bytes. So, to add a canned
513 * cookie, your handler code might look similar to:
514 *
515 * char **p = (char **)in;
516 *
Andy Greenb5b23192013-02-11 17:13:32 +0800517 * if (len < 100)
518 * return 1;
Andy Green385e7ad2011-03-01 21:06:02 +0000519 *
520 * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
521 *
522 * return 0;
523 *
524 * Notice if you add anything, you just have to take care about
525 * the CRLF on the line you added. Obviously this callback is
526 * optional, if you don't handle it everything is fine.
527 *
Andy Greenb5b23192013-02-11 17:13:32 +0800528 * Notice the callback is coming to protocols[0] all the time,
Andy Green385e7ad2011-03-01 21:06:02 +0000529 * because there is no specific protocol handshook yet.
530 *
Andy Greenb5b23192013-02-11 17:13:32 +0800531 * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
Andy Greenc5114822011-03-06 10:29:35 +0000532 * sees that it does support a requested extension, before
533 * accepting the extension by additing to the list sent back to
534 * the client it gives this callback just to check that it's okay
535 * to use that extension. It calls back to the requested protocol
536 * and with @in being the extension name, @len is 0 and @user is
537 * valid. Note though at this time the ESTABLISHED callback hasn't
538 * happened yet so if you initialize @user content there, @user
539 * content during this callback might not be useful for anything.
540 * Notice this callback comes to protocols[0].
541 *
Andy Greenc6517fa2011-03-06 13:15:29 +0000542 * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client
543 * connection is being prepared to start a handshake to a server,
544 * each supported extension is checked with protocols[0] callback
545 * with this reason, giving the user code a chance to suppress the
546 * claim to support that extension by returning non-zero. If
547 * unhandled, by default 0 will be returned and the extension
548 * support included in the header to the server. Notice this
549 * callback comes to protocols[0].
550 *
Andy Greena7109e62013-02-11 12:05:54 +0800551 * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can
552 * do initial setup / allocations etc
553 *
554 * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating
555 * this protocol won't get used at all after this callback, the
556 * context is getting destroyed. Take the opportunity to
557 * deallocate everything that was allocated by the protocol.
558 *
Andy Greene7981dc2011-02-12 21:24:03 +0000559 * The next four reasons are optional and only need taking care of if you
Andy Greenb5b23192013-02-11 17:13:32 +0800560 * will be integrating libwebsockets sockets into an external polling
561 * array.
562 *
563 * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
564 * internally, but in the case you are integrating with another
565 * server you will need to have libwebsocket sockets share a
566 * polling array with the other server. This and the other
567 * POLL_FD related callbacks let you put your specialized
568 * poll array interface code in the callback for protocol 0, the
569 * first protocol you support, usually the HTTP protocol in the
570 * serving case. This callback happens when a socket needs to be
Andy Green50097dd2013-02-15 22:36:30 +0800571 * added to the polling loop: @in contains the fd, and
Andy Greenb5b23192013-02-11 17:13:32 +0800572 * @len is the events bitmap (like, POLLIN). If you are using the
Andy Greene7981dc2011-02-12 21:24:03 +0000573 * internal polling loop (the "service" callback), you can just
Andy Greenb5b23192013-02-11 17:13:32 +0800574 * ignore these callbacks.
Andy Greene7981dc2011-02-12 21:24:03 +0000575 *
Andy Greenb5b23192013-02-11 17:13:32 +0800576 * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
Andy Green50097dd2013-02-15 22:36:30 +0800577 * needs to be removed from an external polling array. @in is
Andy Greenb5b23192013-02-11 17:13:32 +0800578 * the socket desricptor. If you are using the internal polling
579 * loop, you can just ignore it.
Andy Greene7981dc2011-02-12 21:24:03 +0000580 *
Andy Greenb5b23192013-02-11 17:13:32 +0800581 * LWS_CALLBACK_SET_MODE_POLL_FD: This callback happens when libwebsockets
Andy Green50097dd2013-02-15 22:36:30 +0800582 * wants to modify the events for the socket descriptor in @in.
Andy Greene7981dc2011-02-12 21:24:03 +0000583 * The handler should OR @len on to the events member of the pollfd
Andy Greenb5b23192013-02-11 17:13:32 +0800584 * struct for this socket descriptor. If you are using the
Andy Greene7981dc2011-02-12 21:24:03 +0000585 * internal polling loop, you can just ignore it.
586 *
587 * LWS_CALLBACK_CLEAR_MODE_POLL_FD: This callback occurs when libwebsockets
Andy Green50097dd2013-02-15 22:36:30 +0800588 * wants to modify the events for the socket descriptor in @in.
Andy Greene7981dc2011-02-12 21:24:03 +0000589 * The handler should AND ~@len on to the events member of the
Andy Greenb5b23192013-02-11 17:13:32 +0800590 * pollfd struct for this socket descriptor. If you are using the
Andy Greene7981dc2011-02-12 21:24:03 +0000591 * internal polling loop, you can just ignore it.
Andy Green8f037e42010-12-19 22:13:26 +0000592 */
Andy Greenb5b23192013-02-11 17:13:32 +0800593LWS_EXTERN int callback(struct libwebsocket_context *context,
Andy Green62c54d22011-02-14 09:14:25 +0000594 struct libwebsocket *wsi,
Andy Green6964bb52011-01-23 16:50:33 +0000595 enum libwebsocket_callback_reasons reason, void *user,
Andy Green8f037e42010-12-19 22:13:26 +0000596 void *in, size_t len);
597
Andy Greenb5b23192013-02-11 17:13:32 +0800598typedef int (callback_function)(struct libwebsocket_context *context,
David Brooks6c6a3d32012-04-20 12:13:50 +0800599 struct libwebsocket *wsi,
600 enum libwebsocket_callback_reasons reason, void *user,
601 void *in, size_t len);
602
Andy Green3182ece2013-01-20 17:08:31 +0800603#ifndef LWS_NO_EXTENSIONS
Andy Green57b4e9a2011-03-06 13:14:46 +0000604/**
David Brooks6c6a3d32012-04-20 12:13:50 +0800605 * extension_callback_function() - Hooks to allow extensions to operate
Andy Green57b4e9a2011-03-06 13:14:46 +0000606 * @context: Websockets context
Andy Green46c2ea02011-03-22 09:04:01 +0000607 * @ext: This extension
Andy Green57b4e9a2011-03-06 13:14:46 +0000608 * @wsi: Opaque websocket instance pointer
609 * @reason: The reason for the call
610 * @user: Pointer to per-session user data allocated by library
611 * @in: Pointer used for some callback reasons
612 * @len: Length set for some callback reasons
613 *
614 * Each extension that is active on a particular connection receives
615 * callbacks during the connection lifetime to allow the extension to
616 * operate on websocket data and manage itself.
617 *
618 * Libwebsockets takes care of allocating and freeing "user" memory for
619 * each active extension on each connection. That is what is pointed to
620 * by the @user parameter.
621 *
622 * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to
623 * select this extension from the list provided by the client,
624 * just before the server will send back the handshake accepting
625 * the connection with this extension active. This gives the
626 * extension a chance to initialize its connection context found
627 * in @user.
628 *
Andy Greenb5b23192013-02-11 17:13:32 +0800629 * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT
Andy Green2366b1c2011-03-06 13:15:31 +0000630 * but called when client is instantiating this extension. Some
631 * extensions will work the same on client and server side and then
632 * you can just merge handlers for both CONSTRUCTS.
633 *
Andy Green57b4e9a2011-03-06 13:14:46 +0000634 * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was
635 * being used on is about to be closed and deallocated. It's the
636 * last chance for the extension to deallocate anything it has
637 * allocated in the user data (pointed to by @user) before the
Andy Green2366b1c2011-03-06 13:15:31 +0000638 * user data is deleted. This same callback is used whether you
639 * are in client or server instantiation context.
Andy Green57b4e9a2011-03-06 13:14:46 +0000640 *
641 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on
642 * a connection, and a packet of data arrived at the connection,
643 * it is passed to this callback to give the extension a chance to
644 * change the data, eg, decompress it. @user is pointing to the
645 * extension's private connection context data, @in is pointing
646 * to an lws_tokens struct, it consists of a char * pointer called
647 * token, and an int called token_len. At entry, these are
648 * set to point to the received buffer and set to the content
649 * length. If the extension will grow the content, it should use
650 * a new buffer allocated in its private user context data and
651 * set the pointed-to lws_tokens members to point to its buffer.
652 *
653 * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as
654 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
655 * extension a chance to change websocket data just before it will
656 * be sent out. Using the same lws_token pointer scheme in @in,
657 * the extension can change the buffer and the length to be
658 * transmitted how it likes. Again if it wants to grow the
659 * buffer safely, it should copy the data into its own buffer and
660 * set the lws_tokens token pointer to it.
661 */
Andy Greenb5b23192013-02-11 17:13:32 +0800662LWS_EXTERN int extension_callback(struct libwebsocket_context *context,
Andy Green46c2ea02011-03-22 09:04:01 +0000663 struct libwebsocket_extension *ext,
Andy Green57b4e9a2011-03-06 13:14:46 +0000664 struct libwebsocket *wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800665 enum libwebsocket_extension_callback_reasons reason,
666 void *user, void *in, size_t len);
Andy Green57b4e9a2011-03-06 13:14:46 +0000667
Andy Greenb5b23192013-02-11 17:13:32 +0800668typedef int (extension_callback_function)(struct libwebsocket_context *context,
David Brooks6c6a3d32012-04-20 12:13:50 +0800669 struct libwebsocket_extension *ext,
670 struct libwebsocket *wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800671 enum libwebsocket_extension_callback_reasons reason,
672 void *user, void *in, size_t len);
Andy Green3182ece2013-01-20 17:08:31 +0800673#endif
Andy Green57b4e9a2011-03-06 13:14:46 +0000674
Andy Green4f3943a2010-11-12 10:44:16 +0000675/**
Andy Green6964bb52011-01-23 16:50:33 +0000676 * struct libwebsocket_protocols - List of protocols and handlers server
677 * supports.
Andy Green4f3943a2010-11-12 10:44:16 +0000678 * @name: Protocol name that must match the one given in the client
Andy Green6964bb52011-01-23 16:50:33 +0000679 * Javascript new WebSocket(url, 'protocol') name
Andy Green4f3943a2010-11-12 10:44:16 +0000680 * @callback: The service callback used for this protocol. It allows the
Andy Green6964bb52011-01-23 16:50:33 +0000681 * service action for an entire protocol to be encapsulated in
682 * the protocol-specific callback
Andy Green4f3943a2010-11-12 10:44:16 +0000683 * @per_session_data_size: Each new connection using this protocol gets
Andy Green6964bb52011-01-23 16:50:33 +0000684 * this much memory allocated on connection establishment and
685 * freed on connection takedown. A pointer to this per-connection
686 * allocation is passed into the callback in the 'user' parameter
Andy Green54495112013-02-06 21:10:16 +0900687 * @rx_buffer_size: if you want atomic frames delivered to the callback, you
Andy Greenb5b23192013-02-11 17:13:32 +0800688 * should set this to the size of the biggest legal frame that
689 * you support. If the frame size is exceeded, there is no
690 * error, but the buffer will spill to the user callback when
691 * full, which you can detect by using
692 * libwebsockets_remaining_packet_payload(). Notice that you
693 * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING
694 * and post-padding are automatically also allocated on top.
Andy Greenb45993c2010-12-18 15:13:50 +0000695 * @owning_server: the server init call fills in this opaque pointer when
Andy Green6964bb52011-01-23 16:50:33 +0000696 * registering this protocol with the server.
Andy Greenb45993c2010-12-18 15:13:50 +0000697 * @protocol_index: which protocol we are starting from zero
Andy Greene77ddd82010-11-13 10:03:47 +0000698 *
Andy Green6964bb52011-01-23 16:50:33 +0000699 * This structure represents one protocol supported by the server. An
700 * array of these structures is passed to libwebsocket_create_server()
701 * allows as many protocols as you like to be handled by one server.
Andy Green4f3943a2010-11-12 10:44:16 +0000702 */
703
704struct libwebsocket_protocols {
705 const char *name;
David Brooks6c6a3d32012-04-20 12:13:50 +0800706 callback_function *callback;
Andy Green4f3943a2010-11-12 10:44:16 +0000707 size_t per_session_data_size;
Andy Green54495112013-02-06 21:10:16 +0900708 size_t rx_buffer_size;
Andy Greenb45993c2010-12-18 15:13:50 +0000709
710 /*
711 * below are filled in on server init and can be left uninitialized,
712 * no need for user to use them directly either
713 */
Andy Green6964bb52011-01-23 16:50:33 +0000714
Andy Greenb45993c2010-12-18 15:13:50 +0000715 struct libwebsocket_context *owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +0000716 int protocol_index;
Andy Green4f3943a2010-11-12 10:44:16 +0000717};
718
Andy Green3182ece2013-01-20 17:08:31 +0800719#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000720/**
721 * struct libwebsocket_extension - An extension we know how to cope with
722 *
723 * @name: Formal extension name, eg, "deflate-stream"
724 * @callback: Service callback
Andy Greenb5b23192013-02-11 17:13:32 +0800725 * @per_session_data_size: Libwebsockets will auto-malloc this much
726 * memory for the use of the extension, a pointer
Andy Greend6e09112011-03-05 16:12:15 +0000727 * to it comes in the @user callback parameter
Peter Pentchevc74964e2013-02-07 16:17:13 +0200728 * @per_context_private_data: Optional storage for this extension that
Andy Greenb5b23192013-02-11 17:13:32 +0800729 * is per-context, so it can track stuff across
730 * all sessions, etc, if it wants
Andy Greend6e09112011-03-05 16:12:15 +0000731 */
732
733struct libwebsocket_extension {
734 const char *name;
David Brooks6c6a3d32012-04-20 12:13:50 +0800735 extension_callback_function *callback;
Andy Greend6e09112011-03-05 16:12:15 +0000736 size_t per_session_data_size;
Andy Greenb5b23192013-02-11 17:13:32 +0800737 void *per_context_private_data;
Andy Greend6e09112011-03-05 16:12:15 +0000738};
Andy Green3182ece2013-01-20 17:08:31 +0800739#endif
Andy Greend6e09112011-03-05 16:12:15 +0000740
Andy Green1b265272013-02-09 14:01:09 +0800741/**
742 * struct lws_context_creation_info: parameters to create context with
743 *
744 * @port: Port to listen on... you can use 0 to suppress listening on
745 * any port, that's what you want if you are not running a
746 * websocket server at all but just using it as a client
Andy Green7a506ba2013-02-12 10:13:02 +0800747 * @iface: NULL to bind the listen socket to all interfaces, or the
Andy Green1b265272013-02-09 14:01:09 +0800748 * interface name, eg, "eth2"
749 * @protocols: Array of structures listing supported protocols and a protocol-
750 * specific callback for each one. The list is ended with an
751 * entry that has a NULL callback pointer.
752 * It's not const because we write the owning_server member
753 * @extensions: NULL or array of libwebsocket_extension structs listing the
754 * extensions this context supports. If you configured with
755 * --without-extensions, you should give NULL here.
756 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
757 * to listen using SSL, set to the filepath to fetch the
758 * server cert from, otherwise NULL for unencrypted
759 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
760 * else ignored
761 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green2672fb22013-02-22 09:54:35 +0800762 * @ssl_cipher_list: List of valid ciphers to use (eg,
763 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
764 * or you can leave it as NULL to get "DEFAULT"
Andy Green1b265272013-02-09 14:01:09 +0800765 * @gid: group id to change to after setting listen socket, or -1.
766 * @uid: user id to change to after setting listen socket, or -1.
767 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
768 * @user: optional user pointer that can be recovered via the context
Andy Greenb5b23192013-02-11 17:13:32 +0800769 * pointer using libwebsocket_context_user
Andy Greena690cd02013-02-09 12:25:31 +0800770 * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to
771 * all libwebsocket sockets, client or server
772 * @ka_probes: if ka_time was nonzero, after the timeout expires how many
773 * times to try to get a response from the peer before giving up
774 * and killing the connection
775 * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes
776 * attempt
Andy Green1b265272013-02-09 14:01:09 +0800777 */
778
779struct lws_context_creation_info {
780 int port;
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100781 const char *iface;
Andy Green1b265272013-02-09 14:01:09 +0800782 struct libwebsocket_protocols *protocols;
783 struct libwebsocket_extension *extensions;
784 const char *ssl_cert_filepath;
785 const char *ssl_private_key_filepath;
786 const char *ssl_ca_filepath;
Andy Green2672fb22013-02-22 09:54:35 +0800787 const char *ssl_cipher_list;
Andy Green1b265272013-02-09 14:01:09 +0800788 int gid;
789 int uid;
790 unsigned int options;
791 void *user;
Andy Greena690cd02013-02-09 12:25:31 +0800792 int ka_time;
793 int ka_probes;
794 int ka_interval;
795
Andy Green1b265272013-02-09 14:01:09 +0800796};
797
Andy Green43db0452013-01-10 19:50:35 +0800798LWS_EXTERN
Andy Greenb5b23192013-02-11 17:13:32 +0800799void lws_set_log_level(int level,
800 void (*log_emit_function)(int level, const char *line));
Andy Greend6e09112011-03-05 16:12:15 +0000801
Andy Greenc11db202013-01-19 11:12:16 +0800802LWS_EXTERN void
803lwsl_emit_syslog(int level, const char *line);
804
David Galeano9454e212011-09-26 12:17:20 +0100805LWS_EXTERN struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +0800806libwebsocket_create_context(struct lws_context_creation_info *info);
Andy Greenff95d7a2010-10-28 22:36:01 +0100807
David Galeano9454e212011-09-26 12:17:20 +0100808LWS_EXTERN void
Darin Willitsc19456f2011-02-14 17:52:39 +0000809libwebsocket_context_destroy(struct libwebsocket_context *context);
Andy Green6964bb52011-01-23 16:50:33 +0000810
David Galeano9454e212011-09-26 12:17:20 +0100811LWS_EXTERN int
Darin Willitsc19456f2011-02-14 17:52:39 +0000812libwebsocket_service(struct libwebsocket_context *context, int timeout_ms);
Andy Greene92cd172011-01-19 13:11:55 +0000813
David Galeano9454e212011-09-26 12:17:20 +0100814LWS_EXTERN int
Darin Willitsc19456f2011-02-14 17:52:39 +0000815libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green9f990342011-02-12 11:57:45 +0000816 struct pollfd *pollfd);
817
Alon Levy0291eb32012-10-19 11:21:56 +0200818LWS_EXTERN void *
819libwebsocket_context_user(struct libwebsocket_context *context);
820
Andy Green4ea60062010-10-30 12:15:07 +0100821/*
822 * IMPORTANT NOTICE!
Andy Greene77ddd82010-11-13 10:03:47 +0000823 *
Andy Green5fd8a5e2010-10-31 11:57:17 +0000824 * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
825 * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
Andy Green4ea60062010-10-30 12:15:07 +0100826 * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
Andy Greene77ddd82010-11-13 10:03:47 +0000827 *
Andy Green4ea60062010-10-30 12:15:07 +0100828 * This allows us to add protocol info before and after the data, and send as
829 * one packet on the network without payload copying, for maximum efficiency.
Andy Greene77ddd82010-11-13 10:03:47 +0000830 *
Andy Green4ea60062010-10-30 12:15:07 +0100831 * So for example you need this kind of code to use libwebsocket_write with a
Andy Greene77ddd82010-11-13 10:03:47 +0000832 * 128-byte payload
833 *
Andy Greenab990e42010-10-31 12:42:52 +0000834 * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
Andy Greene77ddd82010-11-13 10:03:47 +0000835 *
Andy Greenab990e42010-10-31 12:42:52 +0000836 * // fill your part of the buffer... for example here it's all zeros
837 * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
Andy Greene77ddd82010-11-13 10:03:47 +0000838 *
Andy Greenab990e42010-10-31 12:42:52 +0000839 * libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128);
Andy Greene77ddd82010-11-13 10:03:47 +0000840 *
Andy Green5fd8a5e2010-10-31 11:57:17 +0000841 * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
842 * use the whole buffer without taking care of the above.
Andy Green4ea60062010-10-30 12:15:07 +0100843 */
844
Andy Green687b0182011-02-26 11:04:01 +0000845/*
846 * this is the frame nonce plus two header plus 8 length
Andy Greena41314f2011-05-23 10:00:03 +0100847 * there's an additional two for mux extension per mux nesting level
Andy Green687b0182011-02-26 11:04:01 +0000848 * 2 byte prepend on close will already fit because control frames cannot use
849 * the big length style
850 */
Andy Green4739e5c2011-01-22 12:51:57 +0000851
Andy Greena41314f2011-05-23 10:00:03 +0100852#define LWS_SEND_BUFFER_PRE_PADDING (4 + 10 + (2 * MAX_MUX_RECURSION))
David Galeanoe2cf9922013-01-09 18:06:55 +0800853#define LWS_SEND_BUFFER_POST_PADDING 4
Andy Green4ea60062010-10-30 12:15:07 +0100854
David Galeano9454e212011-09-26 12:17:20 +0100855LWS_EXTERN int
Andy Greene77ddd82010-11-13 10:03:47 +0000856libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len,
Andy Green5fd8a5e2010-10-31 11:57:17 +0000857 enum libwebsocket_write_protocol protocol);
Andy Greena2b0ab02010-11-11 12:28:29 +0000858
David Galeano9454e212011-09-26 12:17:20 +0100859LWS_EXTERN int
Andy Greend280b6e2013-01-15 13:40:23 +0800860libwebsockets_serve_http_file(struct libwebsocket_context *context,
861 struct libwebsocket *wsi, const char *file,
Andy Greene77ddd82010-11-13 10:03:47 +0000862 const char *content_type);
Andy Greend280b6e2013-01-15 13:40:23 +0800863LWS_EXTERN int
864libwebsockets_serve_http_file_fragment(struct libwebsocket_context *context,
865 struct libwebsocket *wsi);
Andy Greenab990e42010-10-31 12:42:52 +0000866
David Galeano9454e212011-09-26 12:17:20 +0100867LWS_EXTERN const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +0000868libwebsockets_get_protocol(struct libwebsocket *wsi);
869
David Galeano9454e212011-09-26 12:17:20 +0100870LWS_EXTERN int
Darin Willitsc19456f2011-02-14 17:52:39 +0000871libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green62c54d22011-02-14 09:14:25 +0000872 struct libwebsocket *wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000873
David Galeano9454e212011-09-26 12:17:20 +0100874LWS_EXTERN int
Andy Green90c7cbc2011-01-27 06:26:52 +0000875libwebsocket_callback_on_writable_all_protocol(
876 const struct libwebsocket_protocols *protocol);
877
David Galeano9454e212011-09-26 12:17:20 +0100878LWS_EXTERN int
Andy Greena6cbece2011-01-27 20:06:03 +0000879libwebsocket_get_socket_fd(struct libwebsocket *wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000880
David Galeano9454e212011-09-26 12:17:20 +0100881LWS_EXTERN int
Andy Green82c3d542011-03-07 21:16:31 +0000882libwebsocket_is_final_fragment(struct libwebsocket *wsi);
883
David Galeanoe2cf9922013-01-09 18:06:55 +0800884LWS_EXTERN unsigned char
885libwebsocket_get_reserved_bits(struct libwebsocket *wsi);
886
David Galeano9454e212011-09-26 12:17:20 +0100887LWS_EXTERN int
Andy Green90c7cbc2011-01-27 06:26:52 +0000888libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable);
889
David Galeano9454e212011-09-26 12:17:20 +0100890LWS_EXTERN size_t
Andy Green38e57bb2011-01-19 12:20:27 +0000891libwebsockets_remaining_packet_payload(struct libwebsocket *wsi);
892
David Galeano9454e212011-09-26 12:17:20 +0100893LWS_EXTERN struct libwebsocket *
Andy Green4739e5c2011-01-22 12:51:57 +0000894libwebsocket_client_connect(struct libwebsocket_context *clients,
895 const char *address,
896 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +0000897 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +0000898 const char *path,
899 const char *host,
900 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +0000901 const char *protocol,
902 int ietf_version_or_minus_one);
Andy Green4739e5c2011-01-22 12:51:57 +0000903
David Brooks2c60d952012-04-20 12:19:01 +0800904LWS_EXTERN struct libwebsocket *
905libwebsocket_client_connect_extended(struct libwebsocket_context *clients,
906 const char *address,
907 int port,
908 int ssl_connection,
909 const char *path,
910 const char *host,
911 const char *origin,
912 const char *protocol,
913 int ietf_version_or_minus_one,
914 void *userdata);
915
David Galeano9454e212011-09-26 12:17:20 +0100916LWS_EXTERN const char *
Darin Willitsc19456f2011-02-14 17:52:39 +0000917libwebsocket_canonical_hostname(struct libwebsocket_context *context);
Andy Green2ac5a6f2011-01-28 10:00:18 +0000918
Andy Green4739e5c2011-01-22 12:51:57 +0000919
David Galeano9454e212011-09-26 12:17:20 +0100920LWS_EXTERN void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800921libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
922 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000923 char *rip, int rip_len);
924
David Galeano9454e212011-09-26 12:17:20 +0100925LWS_EXTERN int
Andy Green8acc1942011-03-07 20:47:40 +0000926libwebsockets_get_random(struct libwebsocket_context *context,
927 void *buf, int len);
928
David Galeano9454e212011-09-26 12:17:20 +0100929LWS_EXTERN int
Andy Green279a3032013-01-17 10:05:39 +0800930lws_daemonize(const char *_lock_path);
931
932LWS_EXTERN int
Andy Green043a0ba2011-04-25 23:54:11 +0800933lws_send_pipe_choked(struct libwebsocket *wsi);
934
Andy Green2fd3f2f2013-01-18 09:49:20 +0800935LWS_EXTERN int
936lws_frame_is_binary(struct libwebsocket *wsi);
937
David Galeano9454e212011-09-26 12:17:20 +0100938LWS_EXTERN unsigned char *
Andy Green2836c642011-03-07 20:47:41 +0000939libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md);
940
David Galeano9454e212011-09-26 12:17:20 +0100941LWS_EXTERN int
Andy Greenf94e0582011-05-14 15:07:56 +0200942lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
943
David Galeano9454e212011-09-26 12:17:20 +0100944LWS_EXTERN int
Andy Greenf94e0582011-05-14 15:07:56 +0200945lws_b64_decode_string(const char *in, char *out, int out_size);
946
Andy Green7b405452013-02-01 10:50:15 +0800947LWS_EXTERN const char *
948lws_get_library_version(void);
949
Andy Green16ab3182013-02-10 18:02:31 +0800950/* access to headers... only valid while headers valid */
951
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800952LWS_EXTERN int
Andy Green16ab3182013-02-10 18:02:31 +0800953lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h);
954
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800955LWS_EXTERN int
Andy Greenb5b23192013-02-11 17:13:32 +0800956lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len,
957 enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +0800958
Andy Greenea0642a2013-01-29 06:52:00 +0800959/*
960 * Note: this is not normally needed as a user api. It's provided in case it is
961 * useful when integrating with other app poll loop service code.
962 */
963
964LWS_EXTERN int
965libwebsocket_read(struct libwebsocket_context *context,
966 struct libwebsocket *wsi,
967 unsigned char *buf, size_t len);
968
Andy Green3182ece2013-01-20 17:08:31 +0800969#ifndef LWS_NO_EXTENSIONS
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800970LWS_EXTERN struct libwebsocket_extension *libwebsocket_get_internal_extensions();
Andy Green3182ece2013-01-20 17:08:31 +0800971#endif
Andy Green4cd87a02011-03-06 13:15:32 +0000972
Peter Hinz56885f32011-03-02 22:03:47 +0000973#ifdef __cplusplus
974}
975#endif
Andy Greena11fe942011-09-25 10:30:26 +0100976
Andy Greenab990e42010-10-31 12:42:52 +0000977#endif