blob: 1ab3e7459dc0f557d0d74558d6788fda8019209e [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
Markus Elfring75212332013-10-26 20:23:00 +080022#ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
23#define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
Andy Greenab990e42010-10-31 12:42:52 +000024
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
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010030#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +000031
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>
pmcdona0a0a7eb2013-12-18 10:17:25 +080038#include <basetsd.h>
Andreas Pakulatfe8589c2013-10-16 17:57:17 +020039#include "websock-w32.h"
Peter Hinz56885f32011-03-02 22:03:47 +000040
Andreas Pakulatfe8589c2013-10-16 17:57:17 +020041#include "gettimeofday.h"
Michel Archambault03bec392011-05-24 17:15:25 +010042
Oleg Golosovskiy2ef75052011-10-04 20:20:14 +080043#define strcasecmp stricmp
Andy Greendfb23042013-01-17 12:26:48 +080044#define getdtablesize() 30000
Oleg Golosovskiy2ef75052011-10-04 20:20:14 +080045
Andy Greenac1ebba2014-02-15 12:46:33 +080046#ifdef __MINGW64__
47#else
48#ifdef __MINGW32__
49#else
50
James Chen4ebedc22013-12-14 11:35:01 +080051#ifndef __SSIZE_T
52#define __SSIZE_T
53
54typedef SSIZE_T ssize_t;
55
56#endif // __SSIZE_T
Andy Greenac1ebba2014-02-15 12:46:33 +080057#endif
58#endif
David Brooks06fbcee2012-05-02 06:02:45 +080059
Andy Green3661d7b2013-04-26 07:37:16 +080060#define LWS_VISIBLE
61
David Galeano9454e212011-09-26 12:17:20 +010062#ifdef LWS_DLL
63#ifdef LWS_INTERNAL
64#define LWS_EXTERN extern __declspec(dllexport)
65#else
66#define LWS_EXTERN extern __declspec(dllimport)
67#endif
Andy Green3661d7b2013-04-26 07:37:16 +080068#else
69#define LWS_EXTERN
David Galeano9454e212011-09-26 12:17:20 +010070#endif
71
Joakim Soderbergf272cb02013-02-13 09:29:26 +080072#else // NOT WIN32
Andy Green3b3fa9e2013-12-25 16:34:37 +080073
74/* to get ppoll() */
Michael Haberlereca0e492014-01-10 23:40:14 +010075#ifndef _GNU_SOURCE
76#define _GNU_SOURCE
77#endif
Andy Green9f990342011-02-12 11:57:45 +000078#include <poll.h>
Andy Greendfb23042013-01-17 12:26:48 +080079#include <unistd.h>
Peter Pentchev9a4fef72013-03-30 09:52:21 +080080
81#if defined(__GNUC__)
82#define LWS_VISIBLE __attribute__((visibility("default")))
83#else
84#define LWS_VISIBLE
85#endif
86
Peter Hinz56885f32011-03-02 22:03:47 +000087#endif
Andy Green9f990342011-02-12 11:57:45 +000088
Peter Young27b32372013-01-17 10:10:10 +080089#include <assert.h>
90
David Galeano9454e212011-09-26 12:17:20 +010091#ifndef LWS_EXTERN
92#define LWS_EXTERN extern
93#endif
94
Andy Green4739e5c2011-01-22 12:51:57 +000095#define CONTEXT_PORT_NO_LISTEN 0
Andy Greena41314f2011-05-23 10:00:03 +010096#define MAX_MUX_RECURSION 2
Andy Greenbfb051f2011-02-09 08:49:14 +000097
Andy Green43db0452013-01-10 19:50:35 +080098enum lws_log_levels {
99 LLL_ERR = 1 << 0,
100 LLL_WARN = 1 << 1,
Andy Green7c19c342013-01-19 12:18:07 +0800101 LLL_NOTICE = 1 << 2,
102 LLL_INFO = 1 << 3,
103 LLL_DEBUG = 1 << 4,
104 LLL_PARSER = 1 << 5,
105 LLL_HEADER = 1 << 6,
106 LLL_EXT = 1 << 7,
107 LLL_CLIENT = 1 << 8,
Andy Greend636e352013-01-29 12:36:17 +0800108 LLL_LATENCY = 1 << 9,
Andy Green43db0452013-01-10 19:50:35 +0800109
Andy Greend636e352013-01-29 12:36:17 +0800110 LLL_COUNT = 10 /* set to count of valid flags */
Andy Green43db0452013-01-10 19:50:35 +0800111};
112
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800113LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
Andy Green69e43642013-01-19 11:58:07 +0800114
Andy Greend636e352013-01-29 12:36:17 +0800115/* notice, warn and log are always compiled in */
Andy Green69e43642013-01-19 11:58:07 +0800116#define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
117#define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
118#define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
Andy Green69e43642013-01-19 11:58:07 +0800119/*
Andy Greend636e352013-01-29 12:36:17 +0800120 * weaker logging can be deselected at configure time using --disable-debug
Andy Green69e43642013-01-19 11:58:07 +0800121 * that gets rid of the overhead of checking while keeping _warn and _err
122 * active
123 */
124#ifdef _DEBUG
Andy Greend636e352013-01-29 12:36:17 +0800125
Andy Green69e43642013-01-19 11:58:07 +0800126#define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
127#define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
128#define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
129#define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
Andy Greend636e352013-01-29 12:36:17 +0800130#define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
Andy Green69e43642013-01-19 11:58:07 +0800131#define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
Andy Greend636e352013-01-29 12:36:17 +0800132#define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800133LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
Andy Greend636e352013-01-29 12:36:17 +0800134
Andy Green69e43642013-01-19 11:58:07 +0800135#else /* no debug */
Andy Greend636e352013-01-29 12:36:17 +0800136
Andy Green69e43642013-01-19 11:58:07 +0800137#define lwsl_info(...)
138#define lwsl_debug(...)
139#define lwsl_parser(...)
140#define lwsl_header(...)
141#define lwsl_ext(...)
142#define lwsl_client(...)
Andy Greend636e352013-01-29 12:36:17 +0800143#define lwsl_latency(...)
Andy Green69e43642013-01-19 11:58:07 +0800144#define lwsl_hexdump(a, b)
Andy Greend636e352013-01-29 12:36:17 +0800145
Andy Green69e43642013-01-19 11:58:07 +0800146#endif
147
Andy Green4e7a1332013-11-11 07:30:33 +0800148#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
149
Andy Greenbfb051f2011-02-09 08:49:14 +0000150enum libwebsocket_context_options {
Andy Greenc6bf2c22011-02-20 11:10:47 +0000151 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
Andy Green788c4a82012-10-22 12:29:57 +0100152 LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
James Devine5b34c972013-12-14 11:41:29 +0800153 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = 8
Andy Greenbfb051f2011-02-09 08:49:14 +0000154};
Andy Green8014b292011-01-30 20:57:25 +0000155
Andy Green775c0dd2010-10-29 14:15:22 +0100156enum libwebsocket_callback_reasons {
157 LWS_CALLBACK_ESTABLISHED,
David Brooks80a44972012-04-20 12:18:47 +0800158 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
Andy Green2b57a342013-02-06 15:15:25 +0900159 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
Andy Green90c7cbc2011-01-27 06:26:52 +0000160 LWS_CALLBACK_CLIENT_ESTABLISHED,
Andy Green775c0dd2010-10-29 14:15:22 +0100161 LWS_CALLBACK_CLOSED,
Andy Green0c9563b2013-06-10 22:54:40 +0800162 LWS_CALLBACK_CLOSED_HTTP,
Andy Green775c0dd2010-10-29 14:15:22 +0100163 LWS_CALLBACK_RECEIVE,
Andy Green4739e5c2011-01-22 12:51:57 +0000164 LWS_CALLBACK_CLIENT_RECEIVE,
Andy Greena6cbece2011-01-27 20:06:03 +0000165 LWS_CALLBACK_CLIENT_RECEIVE_PONG,
Andy Green90c7cbc2011-01-27 06:26:52 +0000166 LWS_CALLBACK_CLIENT_WRITEABLE,
Andy Green9e4c2b62011-03-07 20:47:39 +0000167 LWS_CALLBACK_SERVER_WRITEABLE,
Andy Greena2b0ab02010-11-11 12:28:29 +0000168 LWS_CALLBACK_HTTP,
kapejodce64fb02013-11-19 13:38:16 +0100169 LWS_CALLBACK_HTTP_BODY,
170 LWS_CALLBACK_HTTP_BODY_COMPLETION,
Andy Greend280b6e2013-01-15 13:40:23 +0800171 LWS_CALLBACK_HTTP_FILE_COMPLETION,
Andy Green54cb3462013-02-14 22:23:54 +0800172 LWS_CALLBACK_HTTP_WRITEABLE,
Andy Green07034092011-02-13 08:37:12 +0000173 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
Andy Green19895bc2013-11-09 11:59:56 +0800174 LWS_CALLBACK_FILTER_HTTP_CONNECTION,
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200175 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
Andy Greenc85619d2011-02-13 08:25:26 +0000176 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
Andy Green0894bda2011-02-19 09:09:11 +0000177 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
Andy Greenc6bf2c22011-02-20 11:10:47 +0000178 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Andy Green6901cb32011-02-21 08:06:47 +0000179 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
Andy Green385e7ad2011-03-01 21:06:02 +0000180 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
Andy Greenc5114822011-03-06 10:29:35 +0000181 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
Andy Greenc6517fa2011-03-06 13:15:29 +0000182 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
Andy Greena7109e62013-02-11 12:05:54 +0800183 LWS_CALLBACK_PROTOCOL_INIT,
184 LWS_CALLBACK_PROTOCOL_DESTROY,
Andy Green76b6ea12014-02-15 19:25:50 +0800185 LWS_CALLBACK_WSI_CREATE, /* always protocol[0] */
186 LWS_CALLBACK_WSI_DESTROY, /* always protocol[0] */
Andy Green3b3fa9e2013-12-25 16:34:37 +0800187 LWS_CALLBACK_GET_THREAD_ID,
188
Andy Green3221f922011-02-12 13:14:11 +0000189 /* external poll() management support */
190 LWS_CALLBACK_ADD_POLL_FD,
191 LWS_CALLBACK_DEL_POLL_FD,
Michael Haberlerb3093052014-02-15 20:18:24 +0800192 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
Andy Green7a132792013-12-18 09:48:26 +0800193 LWS_CALLBACK_LOCK_POLL,
194 LWS_CALLBACK_UNLOCK_POLL,
Andy Green09f00582014-02-15 16:49:41 +0800195
196 LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
Andy Green5fd8a5e2010-10-31 11:57:17 +0000197};
198
Michael Haberlercb733062014-02-15 20:15:55 +0800199// argument structure for all external poll related calls
200// passed in via 'in'
201struct libwebsocket_pollargs {
202 int fd; // applicable file descriptor
203 int events; // the new event mask
204 int prev_events; // the previous event mask
205};
206
207
Andy Green3182ece2013-01-20 17:08:31 +0800208#ifndef LWS_NO_EXTENSIONS
Andy Greenc5114822011-03-06 10:29:35 +0000209enum libwebsocket_extension_callback_reasons {
Andy Greena41314f2011-05-23 10:00:03 +0100210 LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT,
211 LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT,
212 LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT,
213 LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT,
Andy Greenc5114822011-03-06 10:29:35 +0000214 LWS_EXT_CALLBACK_CONSTRUCT,
Andy Green2366b1c2011-03-06 13:15:31 +0000215 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
Andy Green68b45042011-05-25 21:41:57 +0100216 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
Andy Green09226502011-05-28 10:19:19 +0100217 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
Andy Greenc5114822011-03-06 10:29:35 +0000218 LWS_EXT_CALLBACK_DESTROY,
Andy Greena41314f2011-05-23 10:00:03 +0100219 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
220 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED,
Andy Green98a717c2011-03-06 13:14:15 +0000221 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green3b84c002011-03-06 13:14:42 +0000222 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
Andy Greena41314f2011-05-23 10:00:03 +0100223 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
224 LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
Andy Greenc44159f2011-03-07 07:08:18 +0000225 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
Andy Greena41314f2011-05-23 10:00:03 +0100226 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
227 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
228 LWS_EXT_CALLBACK_1HZ,
229 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
230 LWS_EXT_CALLBACK_IS_WRITEABLE,
David Galeanoe2cf9922013-01-09 18:06:55 +0800231 LWS_EXT_CALLBACK_PAYLOAD_TX,
232 LWS_EXT_CALLBACK_PAYLOAD_RX,
Andy Greenc5114822011-03-06 10:29:35 +0000233};
Andy Green3182ece2013-01-20 17:08:31 +0800234#endif
Andy Greenc5114822011-03-06 10:29:35 +0000235
Andy Green5fd8a5e2010-10-31 11:57:17 +0000236enum libwebsocket_write_protocol {
237 LWS_WRITE_TEXT,
238 LWS_WRITE_BINARY,
Andy Green5d9d94b2011-03-07 20:47:41 +0000239 LWS_WRITE_CONTINUATION,
Andy Green38e57bb2011-01-19 12:20:27 +0000240 LWS_WRITE_HTTP,
241
Andy Green5e1fa172011-02-10 09:07:05 +0000242 /* special 04+ opcodes */
Andy Green38e57bb2011-01-19 12:20:27 +0000243
244 LWS_WRITE_CLOSE,
245 LWS_WRITE_PING,
Andy Green90c7cbc2011-01-27 06:26:52 +0000246 LWS_WRITE_PONG,
247
Andy Greenbd96d802011-01-30 08:24:31 +0000248 /* flags */
249
250 LWS_WRITE_NO_FIN = 0x40,
251 /*
252 * client packet payload goes out on wire unmunged
253 * only useful for security tests since normal servers cannot
254 * decode the content if used
255 */
256 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
Andy Greenff95d7a2010-10-28 22:36:01 +0100257};
258
Andy Greenc85619d2011-02-13 08:25:26 +0000259/*
260 * you need these to look at headers that have been parsed if using the
261 * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
262 * list below is absent, .token = NULL and token_len = 0. Otherwise .token
263 * points to .token_len chars containing that header content.
264 */
265
266struct lws_tokens {
267 char *token;
268 int token_len;
269};
270
271enum lws_token_indexes {
272 WSI_TOKEN_GET_URI,
kapejodce64fb02013-11-19 13:38:16 +0100273 WSI_TOKEN_POST_URI,
Andy Greenc85619d2011-02-13 08:25:26 +0000274 WSI_TOKEN_HOST,
275 WSI_TOKEN_CONNECTION,
276 WSI_TOKEN_KEY1,
277 WSI_TOKEN_KEY2,
278 WSI_TOKEN_PROTOCOL,
279 WSI_TOKEN_UPGRADE,
280 WSI_TOKEN_ORIGIN,
281 WSI_TOKEN_DRAFT,
282 WSI_TOKEN_CHALLENGE,
283
284 /* new for 04 */
285 WSI_TOKEN_KEY,
286 WSI_TOKEN_VERSION,
287 WSI_TOKEN_SWORIGIN,
288
289 /* new for 05 */
290 WSI_TOKEN_EXTENSIONS,
291
292 /* client receives these */
293 WSI_TOKEN_ACCEPT,
294 WSI_TOKEN_NONCE,
295 WSI_TOKEN_HTTP,
Andy Greencc13c6f2013-11-09 10:09:09 +0800296
297 /* http-related */
298 WSI_TOKEN_HTTP_ACCEPT,
299 WSI_TOKEN_HTTP_IF_MODIFIED_SINCE,
300 WSI_TOKEN_HTTP_ACCEPT_ENCODING,
301 WSI_TOKEN_HTTP_ACCEPT_LANGUAGE,
302 WSI_TOKEN_HTTP_PRAGMA,
303 WSI_TOKEN_HTTP_CACHE_CONTROL,
304 WSI_TOKEN_HTTP_AUTHORIZATION,
305 WSI_TOKEN_HTTP_COOKIE,
kapejodce64fb02013-11-19 13:38:16 +0100306 WSI_TOKEN_HTTP_CONTENT_LENGTH,
Andy Greencc13c6f2013-11-09 10:09:09 +0800307 WSI_TOKEN_HTTP_CONTENT_TYPE,
308 WSI_TOKEN_HTTP_DATE,
309 WSI_TOKEN_HTTP_RANGE,
310 WSI_TOKEN_HTTP_REFERER,
Andy Green1e3f7b82013-11-13 07:45:17 +0800311 WSI_TOKEN_HTTP_URI_ARGS,
Andy Greencc13c6f2013-11-09 10:09:09 +0800312
313
Andy Greena41314f2011-05-23 10:00:03 +0100314 WSI_TOKEN_MUXURL,
Andy Greenc85619d2011-02-13 08:25:26 +0000315
Andy Greene77fb802013-02-11 13:04:45 +0800316 /* use token storage to stash these */
317
318 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
319 _WSI_TOKEN_CLIENT_PEER_ADDRESS,
320 _WSI_TOKEN_CLIENT_URI,
321 _WSI_TOKEN_CLIENT_HOST,
322 _WSI_TOKEN_CLIENT_ORIGIN,
323
Andy Greenc85619d2011-02-13 08:25:26 +0000324 /* always last real token index*/
325 WSI_TOKEN_COUNT,
326 /* parser state additions */
327 WSI_TOKEN_NAME_PART,
328 WSI_TOKEN_SKIPPING,
329 WSI_TOKEN_SKIPPING_SAW_CR,
Andy Greena41314f2011-05-23 10:00:03 +0100330 WSI_PARSING_COMPLETE,
331 WSI_INIT_TOKEN_MUXURL,
Andy Greenc85619d2011-02-13 08:25:26 +0000332};
333
Andy Green687b0182011-02-26 11:04:01 +0000334/*
David Galeano2915a802013-01-09 15:26:08 +0800335 * From RFC 6455
Andy Green687b0182011-02-26 11:04:01 +0000336 1000
337
David Galeano2915a802013-01-09 15:26:08 +0800338 1000 indicates a normal closure, meaning that the purpose for
339 which the connection was established has been fulfilled.
Andy Green687b0182011-02-26 11:04:01 +0000340
341 1001
342
343 1001 indicates that an endpoint is "going away", such as a server
David Galeano2915a802013-01-09 15:26:08 +0800344 going down or a browser having navigated away from a page.
Andy Green687b0182011-02-26 11:04:01 +0000345
346 1002
347
348 1002 indicates that an endpoint is terminating the connection due
349 to a protocol error.
350
351 1003
352
353 1003 indicates that an endpoint is terminating the connection
David Galeano2915a802013-01-09 15:26:08 +0800354 because it has received a type of data it cannot accept (e.g., an
355 endpoint that understands only text data MAY send this if it
356 receives a binary message).
Andy Green687b0182011-02-26 11:04:01 +0000357
358 1004
359
David Galeano2915a802013-01-09 15:26:08 +0800360 Reserved. The specific meaning might be defined in the future.
361
362 1005
363
364 1005 is a reserved value and MUST NOT be set as a status code in a
365 Close control frame by an endpoint. It is designated for use in
366 applications expecting a status code to indicate that no status
367 code was actually present.
368
369 1006
370
371 1006 is a reserved value and MUST NOT be set as a status code in a
372 Close control frame by an endpoint. It is designated for use in
373 applications expecting a status code to indicate that the
374 connection was closed abnormally, e.g., without sending or
375 receiving a Close control frame.
376
377 1007
378
379 1007 indicates that an endpoint is terminating the connection
380 because it has received data within a message that was not
381 consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
382 data within a text message).
383
384 1008
385
386 1008 indicates that an endpoint is terminating the connection
387 because it has received a message that violates its policy. This
388 is a generic status code that can be returned when there is no
389 other more suitable status code (e.g., 1003 or 1009) or if there
390 is a need to hide specific details about the policy.
391
392 1009
393
394 1009 indicates that an endpoint is terminating the connection
395 because it has received a message that is too big for it to
396 process.
397
398 1010
399
400 1010 indicates that an endpoint (client) is terminating the
401 connection because it has expected the server to negotiate one or
402 more extension, but the server didn't return them in the response
403 message of the WebSocket handshake. The list of extensions that
404 are needed SHOULD appear in the /reason/ part of the Close frame.
405 Note that this status code is not used by the server, because it
406 can fail the WebSocket handshake instead.
407
408 1011
409
410 1011 indicates that a server is terminating the connection because
411 it encountered an unexpected condition that prevented it from
412 fulfilling the request.
413
414 1015
415
416 1015 is a reserved value and MUST NOT be set as a status code in a
417 Close control frame by an endpoint. It is designated for use in
418 applications expecting a status code to indicate that the
419 connection was closed due to a failure to perform a TLS handshake
420 (e.g., the server certificate can't be verified).
Andy Green687b0182011-02-26 11:04:01 +0000421*/
422
423enum lws_close_status {
424 LWS_CLOSE_STATUS_NOSTATUS = 0,
425 LWS_CLOSE_STATUS_NORMAL = 1000,
426 LWS_CLOSE_STATUS_GOINGAWAY = 1001,
427 LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
428 LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
David Galeano2915a802013-01-09 15:26:08 +0800429 LWS_CLOSE_STATUS_RESERVED = 1004,
430 LWS_CLOSE_STATUS_NO_STATUS = 1005,
431 LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
432 LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
433 LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
434 LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
435 LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
Andy Greenb5b23192013-02-11 17:13:32 +0800436 LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
437 LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
Andy Green687b0182011-02-26 11:04:01 +0000438};
439
Andy Green4e7a1332013-11-11 07:30:33 +0800440enum http_status {
kapejodce64fb02013-11-19 13:38:16 +0100441 HTTP_STATUS_OK = 200,
442
Andy Green4e7a1332013-11-11 07:30:33 +0800443 HTTP_STATUS_BAD_REQUEST = 400,
444 HTTP_STATUS_UNAUTHORIZED,
445 HTTP_STATUS_PAYMENT_REQUIRED,
446 HTTP_STATUS_FORBIDDEN,
447 HTTP_STATUS_NOT_FOUND,
448 HTTP_STATUS_METHOD_NOT_ALLOWED,
449 HTTP_STATUS_NOT_ACCEPTABLE,
450 HTTP_STATUS_PROXY_AUTH_REQUIRED,
451 HTTP_STATUS_REQUEST_TIMEOUT,
452 HTTP_STATUS_CONFLICT,
453 HTTP_STATUS_GONE,
454 HTTP_STATUS_LENGTH_REQUIRED,
455 HTTP_STATUS_PRECONDITION_FAILED,
456 HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
457 HTTP_STATUS_REQ_URI_TOO_LONG,
458 HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
459 HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
460 HTTP_STATUS_EXPECTATION_FAILED,
461
462 HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
463 HTTP_STATUS_NOT_IMPLEMENTED,
464 HTTP_STATUS_BAD_GATEWAY,
465 HTTP_STATUS_SERVICE_UNAVAILABLE,
466 HTTP_STATUS_GATEWAY_TIMEOUT,
467 HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
468};
469
Andy Green775c0dd2010-10-29 14:15:22 +0100470struct libwebsocket;
Andy Greenb45993c2010-12-18 15:13:50 +0000471struct libwebsocket_context;
Andy Green3182ece2013-01-20 17:08:31 +0800472/* needed even with extensions disabled for create context */
Andy Green46c2ea02011-03-22 09:04:01 +0000473struct libwebsocket_extension;
Andy Greenff95d7a2010-10-28 22:36:01 +0100474
Andy Green8f037e42010-12-19 22:13:26 +0000475/**
David Brooks6c6a3d32012-04-20 12:13:50 +0800476 * callback_function() - User server actions
Andy Green32375b72011-02-19 08:32:53 +0000477 * @context: Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000478 * @wsi: Opaque websocket instance pointer
479 * @reason: The reason for the call
480 * @user: Pointer to per-session user data allocated by library
481 * @in: Pointer used for some callback reasons
482 * @len: Length set for some callback reasons
483 *
484 * This callback is the way the user controls what is served. All the
485 * protocol detail is hidden and handled by the library.
Andy Green6964bb52011-01-23 16:50:33 +0000486 *
Andy Green8f037e42010-12-19 22:13:26 +0000487 * For each connection / session there is user data allocated that is
488 * pointed to by "user". You set the size of this user data area when
489 * the library is initialized with libwebsocket_create_server.
Andy Green6964bb52011-01-23 16:50:33 +0000490 *
Andy Green8f037e42010-12-19 22:13:26 +0000491 * You get an opportunity to initialize user data when called back with
492 * LWS_CALLBACK_ESTABLISHED reason.
493 *
Andy Green2b57a342013-02-06 15:15:25 +0900494 * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
Andy Green90c7cbc2011-01-27 06:26:52 +0000495 * an incoming client
496 *
David Brooks80a44972012-04-20 12:18:47 +0800497 * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
498 * been unable to complete a handshake with the remote server
499 *
Andy Green2b57a342013-02-06 15:15:25 +0900500 * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
Andy Greenb5b23192013-02-11 17:13:32 +0800501 * client user code to examine the http headers
502 * and decide to reject the connection. If the
503 * content in the headers is interesting to the
504 * client (url, etc) it needs to copy it out at
505 * this point since it will be destroyed before
506 * the CLIENT_ESTABLISHED call
Andy Green2b57a342013-02-06 15:15:25 +0900507 *
David Brooks80a44972012-04-20 12:18:47 +0800508 * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
Andy Green90c7cbc2011-01-27 06:26:52 +0000509 * a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000510 *
511 * LWS_CALLBACK_CLOSED: when the websocket session ends
512 *
Andy Green0c9563b2013-06-10 22:54:40 +0800513 * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
514 *
Andy Green90c7cbc2011-01-27 06:26:52 +0000515 * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
516 * remote client, it can be found at *in and is
517 * len bytes long
518 *
Andy Greena6cbece2011-01-27 20:06:03 +0000519 * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
520 * they appear with this callback reason. PONG
521 * packets only exist in 04+ protocol
522 *
Andy Green90c7cbc2011-01-27 06:26:52 +0000523 * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
524 * client connection, it can be found at *in and
525 * is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000526 *
Andy Green6964bb52011-01-23 16:50:33 +0000527 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
Andy Green8f037e42010-12-19 22:13:26 +0000528 * asking to upgrade the connection to a websocket
529 * one. This is a chance to serve http content,
530 * for example, to send a script to the client
531 * which will then open the websockets connection.
Andy Green6964bb52011-01-23 16:50:33 +0000532 * @in points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000533 * libwebsockets_serve_http_file() makes it very
534 * simple to send back a file to the client.
Andy Green24b588b2013-01-13 09:53:18 +0800535 * Normally after sending the file you are done
536 * with the http connection, since the rest of the
537 * activity will come by websockets from the script
538 * that was delivered by http, so you will want to
539 * return 1; to close and free up the connection.
540 * That's important because it uses a slot in the
541 * total number of client connections allowed set
542 * by MAX_CLIENTS.
Andy Green90c7cbc2011-01-27 06:26:52 +0000543 *
kapejodce64fb02013-11-19 13:38:16 +0100544 * LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http
545 * request body HTTP connection is now available in @in.
546 *
547 * LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request
548 * body has been delivered
549 *
Andy Green54cb3462013-02-14 22:23:54 +0800550 * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
551 * link now.
552 *
Andy Greend280b6e2013-01-15 13:40:23 +0800553 * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
554 * http link has completed.
555 *
Andy Green9e4c2b62011-03-07 20:47:39 +0000556 * LWS_CALLBACK_CLIENT_WRITEABLE:
557 * LWS_CALLBACK_SERVER_WRITEABLE: If you call
Andy Green90c7cbc2011-01-27 06:26:52 +0000558 * libwebsocket_callback_on_writable() on a connection, you will
Andy Green9e4c2b62011-03-07 20:47:39 +0000559 * get one of these callbacks coming when the connection socket
560 * is able to accept another write packet without blocking.
561 * If it already was able to take another packet without blocking,
562 * you'll get this callback at the next call to the service loop
563 * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
564 * and servers get LWS_CALLBACK_SERVER_WRITEABLE.
Andy Greene7981dc2011-02-12 21:24:03 +0000565 *
Andy Green07034092011-02-13 08:37:12 +0000566 * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
567 * the server at network level; the connection is accepted but then
568 * passed to this callback to decide whether to hang up immediately
Edwin van den Oetelaar8c8a8e12013-02-20 20:56:59 +0800569 * or not, based on the client IP. @in contains the connection
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200570 * socket's descriptor. Since the client connection information is
571 * not available yet, @wsi still pointing to the main server socket.
572 * Return non-zero to terminate the connection before sending or
573 * receiving anything. Because this happens immediately after the
574 * network connection from the client, there's no websocket protocol
575 * selected yet so this callback is issued only to protocol 0.
Andy Green19895bc2013-11-09 11:59:56 +0800576 *
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200577 * LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: A new client just had
578 * been connected, accepted, and instantiated into the pool. This
579 * callback allows setting any relevant property to it. Because this
580 * happens immediately after the instantiation of a new client,
581 * there's no websocket protocol selected yet so this callback is
582 * issued only to protocol 0. Only @wsi is defined, pointing to the
583 * new client, and the return value is ignored.
584 *
Andy Green19895bc2013-11-09 11:59:56 +0800585 * LWS_CALLBACK_FILTER_HTTP_CONNECTION: called when the request has
586 * been received and parsed from the client, but the response is
587 * not sent yet. Return non-zero to disallow the connection.
588 * @user is a pointer to the connection user space allocation,
589 * @in is the URI, eg, "/"
590 * In your handler you can use the public APIs
591 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
592 * headers using the header enums lws_token_indexes from
593 * libwebsockets.h to check for and read the supported header
594 * presence and content before deciding to allow the http
595 * connection to proceed or to kill the connection.
Andy Green07034092011-02-13 08:37:12 +0000596 *
Andy Greenb5b23192013-02-11 17:13:32 +0800597 * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
598 * been received and parsed from the client, but the response is
599 * not sent yet. Return non-zero to disallow the connection.
Andy Green96d48fd2013-09-18 08:32:55 +0800600 * @user is a pointer to the connection user space allocation,
601 * @in is the requested protocol name
602 * In your handler you can use the public APIs
603 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
604 * headers using the header enums lws_token_indexes from
605 * libwebsockets.h to check for and read the supported header
606 * presence and content before deciding to allow the handshake
607 * to proceed or to kill the connection.
Andy Greenc85619d2011-02-13 08:25:26 +0000608 *
Andy Greenb5b23192013-02-11 17:13:32 +0800609 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
610 * including OpenSSL support, this callback allows your user code
611 * to perform extra SSL_CTX_load_verify_locations() or similar
Andy Green0894bda2011-02-19 09:09:11 +0000612 * calls to direct OpenSSL where to find certificates the client
613 * can use to confirm the remote server identity. @user is the
614 * OpenSSL SSL_CTX*
Andy Greenc85619d2011-02-13 08:25:26 +0000615 *
Andy Greenc6bf2c22011-02-20 11:10:47 +0000616 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
617 * including OpenSSL support, this callback allows your user code
618 * to load extra certifcates into the server which allow it to
619 * verify the validity of certificates returned by clients. @user
620 * is the server's OpenSSL SSL_CTX*
621 *
Andy Green6901cb32011-02-21 08:06:47 +0000622 * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
623 * libwebsockets context was created with the option
624 * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
625 * callback is generated during OpenSSL verification of the cert
626 * sent from the client. It is sent to protocol[0] callback as
627 * no protocol has been negotiated on the connection yet.
628 * Notice that the libwebsockets context and wsi are both NULL
629 * during this callback. See
630 * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
631 * to understand more detail about the OpenSSL callback that
632 * generates this libwebsockets callback and the meanings of the
633 * arguments passed. In this callback, @user is the x509_ctx,
634 * @in is the ssl pointer and @len is preverify_ok
635 * Notice that this callback maintains libwebsocket return
636 * conventions, return 0 to mean the cert is OK or 1 to fail it.
637 * This also means that if you don't handle this callback then
638 * the default callback action of returning 0 allows the client
639 * certificates.
640 *
Andy Green385e7ad2011-03-01 21:06:02 +0000641 * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
642 * when a client handshake is being compiled. @user is NULL,
643 * @in is a char **, it's pointing to a char * which holds the
644 * next location in the header buffer where you can add
645 * headers, and @len is the remaining space in the header buffer,
646 * which is typically some hundreds of bytes. So, to add a canned
647 * cookie, your handler code might look similar to:
648 *
649 * char **p = (char **)in;
650 *
Andy Greenb5b23192013-02-11 17:13:32 +0800651 * if (len < 100)
652 * return 1;
Andy Green385e7ad2011-03-01 21:06:02 +0000653 *
654 * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
655 *
656 * return 0;
657 *
658 * Notice if you add anything, you just have to take care about
659 * the CRLF on the line you added. Obviously this callback is
660 * optional, if you don't handle it everything is fine.
661 *
Andy Greenb5b23192013-02-11 17:13:32 +0800662 * Notice the callback is coming to protocols[0] all the time,
Andy Green385e7ad2011-03-01 21:06:02 +0000663 * because there is no specific protocol handshook yet.
664 *
Andy Greenb5b23192013-02-11 17:13:32 +0800665 * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
Andy Greenc5114822011-03-06 10:29:35 +0000666 * sees that it does support a requested extension, before
667 * accepting the extension by additing to the list sent back to
668 * the client it gives this callback just to check that it's okay
669 * to use that extension. It calls back to the requested protocol
670 * and with @in being the extension name, @len is 0 and @user is
671 * valid. Note though at this time the ESTABLISHED callback hasn't
672 * happened yet so if you initialize @user content there, @user
673 * content during this callback might not be useful for anything.
674 * Notice this callback comes to protocols[0].
675 *
Andy Greenc6517fa2011-03-06 13:15:29 +0000676 * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client
677 * connection is being prepared to start a handshake to a server,
678 * each supported extension is checked with protocols[0] callback
679 * with this reason, giving the user code a chance to suppress the
680 * claim to support that extension by returning non-zero. If
681 * unhandled, by default 0 will be returned and the extension
682 * support included in the header to the server. Notice this
683 * callback comes to protocols[0].
684 *
Andy Greena7109e62013-02-11 12:05:54 +0800685 * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can
686 * do initial setup / allocations etc
687 *
688 * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating
689 * this protocol won't get used at all after this callback, the
690 * context is getting destroyed. Take the opportunity to
691 * deallocate everything that was allocated by the protocol.
692 *
Andy Green76b6ea12014-02-15 19:25:50 +0800693 * LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
694 *
695 * LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
696 *
Andy Greene7981dc2011-02-12 21:24:03 +0000697 * The next four reasons are optional and only need taking care of if you
Andy Greenb5b23192013-02-11 17:13:32 +0800698 * will be integrating libwebsockets sockets into an external polling
699 * array.
700 *
701 * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
702 * internally, but in the case you are integrating with another
703 * server you will need to have libwebsocket sockets share a
704 * polling array with the other server. This and the other
705 * POLL_FD related callbacks let you put your specialized
706 * poll array interface code in the callback for protocol 0, the
707 * first protocol you support, usually the HTTP protocol in the
708 * serving case. This callback happens when a socket needs to be
Andy Green50097dd2013-02-15 22:36:30 +0800709 * added to the polling loop: @in contains the fd, and
Andy Greenb5b23192013-02-11 17:13:32 +0800710 * @len is the events bitmap (like, POLLIN). If you are using the
Andy Greene7981dc2011-02-12 21:24:03 +0000711 * internal polling loop (the "service" callback), you can just
Andy Greenb5b23192013-02-11 17:13:32 +0800712 * ignore these callbacks.
Andy Greene7981dc2011-02-12 21:24:03 +0000713 *
Andy Greenb5b23192013-02-11 17:13:32 +0800714 * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
Andy Green50097dd2013-02-15 22:36:30 +0800715 * needs to be removed from an external polling array. @in is
Andy Greenb5b23192013-02-11 17:13:32 +0800716 * the socket desricptor. If you are using the internal polling
717 * loop, you can just ignore it.
Andy Greene7981dc2011-02-12 21:24:03 +0000718 *
Andy Greenb5b23192013-02-11 17:13:32 +0800719 * LWS_CALLBACK_SET_MODE_POLL_FD: This callback happens when libwebsockets
Andy Green50097dd2013-02-15 22:36:30 +0800720 * wants to modify the events for the socket descriptor in @in.
Andy Greene7981dc2011-02-12 21:24:03 +0000721 * The handler should OR @len on to the events member of the pollfd
Andy Greenb5b23192013-02-11 17:13:32 +0800722 * struct for this socket descriptor. If you are using the
Andy Greene7981dc2011-02-12 21:24:03 +0000723 * internal polling loop, you can just ignore it.
724 *
725 * LWS_CALLBACK_CLEAR_MODE_POLL_FD: This callback occurs when libwebsockets
Andy Green50097dd2013-02-15 22:36:30 +0800726 * wants to modify the events for the socket descriptor in @in.
Andy Greene7981dc2011-02-12 21:24:03 +0000727 * The handler should AND ~@len on to the events member of the
Andy Greenb5b23192013-02-11 17:13:32 +0800728 * pollfd struct for this socket descriptor. If you are using the
Andy Greene7981dc2011-02-12 21:24:03 +0000729 * internal polling loop, you can just ignore it.
Andy Green8f037e42010-12-19 22:13:26 +0000730 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800731LWS_VISIBLE LWS_EXTERN int callback(struct libwebsocket_context *context,
Andy Green62c54d22011-02-14 09:14:25 +0000732 struct libwebsocket *wsi,
Andy Green6964bb52011-01-23 16:50:33 +0000733 enum libwebsocket_callback_reasons reason, void *user,
Andy Green8f037e42010-12-19 22:13:26 +0000734 void *in, size_t len);
735
Andy Greenb5b23192013-02-11 17:13:32 +0800736typedef int (callback_function)(struct libwebsocket_context *context,
David Brooks6c6a3d32012-04-20 12:13:50 +0800737 struct libwebsocket *wsi,
738 enum libwebsocket_callback_reasons reason, void *user,
739 void *in, size_t len);
740
Andy Green3182ece2013-01-20 17:08:31 +0800741#ifndef LWS_NO_EXTENSIONS
Andy Green57b4e9a2011-03-06 13:14:46 +0000742/**
David Brooks6c6a3d32012-04-20 12:13:50 +0800743 * extension_callback_function() - Hooks to allow extensions to operate
Andy Green57b4e9a2011-03-06 13:14:46 +0000744 * @context: Websockets context
Andy Green46c2ea02011-03-22 09:04:01 +0000745 * @ext: This extension
Andy Green57b4e9a2011-03-06 13:14:46 +0000746 * @wsi: Opaque websocket instance pointer
747 * @reason: The reason for the call
748 * @user: Pointer to per-session user data allocated by library
749 * @in: Pointer used for some callback reasons
750 * @len: Length set for some callback reasons
751 *
752 * Each extension that is active on a particular connection receives
753 * callbacks during the connection lifetime to allow the extension to
754 * operate on websocket data and manage itself.
755 *
756 * Libwebsockets takes care of allocating and freeing "user" memory for
757 * each active extension on each connection. That is what is pointed to
758 * by the @user parameter.
759 *
760 * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to
761 * select this extension from the list provided by the client,
762 * just before the server will send back the handshake accepting
763 * the connection with this extension active. This gives the
764 * extension a chance to initialize its connection context found
765 * in @user.
766 *
Andy Greenb5b23192013-02-11 17:13:32 +0800767 * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT
Andy Green2366b1c2011-03-06 13:15:31 +0000768 * but called when client is instantiating this extension. Some
769 * extensions will work the same on client and server side and then
770 * you can just merge handlers for both CONSTRUCTS.
771 *
Andy Green57b4e9a2011-03-06 13:14:46 +0000772 * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was
773 * being used on is about to be closed and deallocated. It's the
774 * last chance for the extension to deallocate anything it has
775 * allocated in the user data (pointed to by @user) before the
Andy Green2366b1c2011-03-06 13:15:31 +0000776 * user data is deleted. This same callback is used whether you
777 * are in client or server instantiation context.
Andy Green57b4e9a2011-03-06 13:14:46 +0000778 *
779 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on
780 * a connection, and a packet of data arrived at the connection,
781 * it is passed to this callback to give the extension a chance to
782 * change the data, eg, decompress it. @user is pointing to the
783 * extension's private connection context data, @in is pointing
784 * to an lws_tokens struct, it consists of a char * pointer called
785 * token, and an int called token_len. At entry, these are
786 * set to point to the received buffer and set to the content
787 * length. If the extension will grow the content, it should use
788 * a new buffer allocated in its private user context data and
789 * set the pointed-to lws_tokens members to point to its buffer.
790 *
791 * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as
792 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
793 * extension a chance to change websocket data just before it will
794 * be sent out. Using the same lws_token pointer scheme in @in,
795 * the extension can change the buffer and the length to be
796 * transmitted how it likes. Again if it wants to grow the
797 * buffer safely, it should copy the data into its own buffer and
798 * set the lws_tokens token pointer to it.
799 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800800LWS_VISIBLE LWS_EXTERN int extension_callback(struct libwebsocket_context *context,
Andy Green46c2ea02011-03-22 09:04:01 +0000801 struct libwebsocket_extension *ext,
Andy Green57b4e9a2011-03-06 13:14:46 +0000802 struct libwebsocket *wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800803 enum libwebsocket_extension_callback_reasons reason,
804 void *user, void *in, size_t len);
Andy Green57b4e9a2011-03-06 13:14:46 +0000805
Andy Greenb5b23192013-02-11 17:13:32 +0800806typedef int (extension_callback_function)(struct libwebsocket_context *context,
David Brooks6c6a3d32012-04-20 12:13:50 +0800807 struct libwebsocket_extension *ext,
808 struct libwebsocket *wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800809 enum libwebsocket_extension_callback_reasons reason,
810 void *user, void *in, size_t len);
Andy Green3182ece2013-01-20 17:08:31 +0800811#endif
Andy Green57b4e9a2011-03-06 13:14:46 +0000812
Andy Green4f3943a2010-11-12 10:44:16 +0000813/**
Andy Green6964bb52011-01-23 16:50:33 +0000814 * struct libwebsocket_protocols - List of protocols and handlers server
815 * supports.
Andy Green4f3943a2010-11-12 10:44:16 +0000816 * @name: Protocol name that must match the one given in the client
Andy Green6964bb52011-01-23 16:50:33 +0000817 * Javascript new WebSocket(url, 'protocol') name
Andy Green4f3943a2010-11-12 10:44:16 +0000818 * @callback: The service callback used for this protocol. It allows the
Andy Green6964bb52011-01-23 16:50:33 +0000819 * service action for an entire protocol to be encapsulated in
820 * the protocol-specific callback
Andy Green4f3943a2010-11-12 10:44:16 +0000821 * @per_session_data_size: Each new connection using this protocol gets
Andy Green6964bb52011-01-23 16:50:33 +0000822 * this much memory allocated on connection establishment and
823 * freed on connection takedown. A pointer to this per-connection
824 * allocation is passed into the callback in the 'user' parameter
Andy Green54495112013-02-06 21:10:16 +0900825 * @rx_buffer_size: if you want atomic frames delivered to the callback, you
Andy Greenb5b23192013-02-11 17:13:32 +0800826 * should set this to the size of the biggest legal frame that
827 * you support. If the frame size is exceeded, there is no
828 * error, but the buffer will spill to the user callback when
829 * full, which you can detect by using
830 * libwebsockets_remaining_packet_payload(). Notice that you
831 * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING
832 * and post-padding are automatically also allocated on top.
Andy Green1f4267b2013-10-17 08:09:19 +0800833 * @no_buffer_all_partial_tx: Leave at zero if you want the library to take
834 * care of all partial tx for you. It's useful if you only have
835 * small tx packets and the chance of any truncated send is small
836 * enough any additional malloc / buffering overhead is less
837 * painful than writing the code to deal with partial sends. For
838 * protocols where you stream big blocks, set to nonzero and use
839 * the return value from libwebsocket_write() to manage how much
840 * got send yourself.
Andy Greenb45993c2010-12-18 15:13:50 +0000841 * @owning_server: the server init call fills in this opaque pointer when
Andy Green6964bb52011-01-23 16:50:33 +0000842 * registering this protocol with the server.
Andy Greenb45993c2010-12-18 15:13:50 +0000843 * @protocol_index: which protocol we are starting from zero
Andy Greene77ddd82010-11-13 10:03:47 +0000844 *
Andy Green6964bb52011-01-23 16:50:33 +0000845 * This structure represents one protocol supported by the server. An
846 * array of these structures is passed to libwebsocket_create_server()
847 * allows as many protocols as you like to be handled by one server.
Andy Green4f3943a2010-11-12 10:44:16 +0000848 */
849
850struct libwebsocket_protocols {
851 const char *name;
David Brooks6c6a3d32012-04-20 12:13:50 +0800852 callback_function *callback;
Andy Green4f3943a2010-11-12 10:44:16 +0000853 size_t per_session_data_size;
Andy Green54495112013-02-06 21:10:16 +0900854 size_t rx_buffer_size;
Andy Green1f4267b2013-10-17 08:09:19 +0800855 int no_buffer_all_partial_tx;
Andy Greenb45993c2010-12-18 15:13:50 +0000856
857 /*
858 * below are filled in on server init and can be left uninitialized,
859 * no need for user to use them directly either
860 */
Andy Green6964bb52011-01-23 16:50:33 +0000861
Andy Greenb45993c2010-12-18 15:13:50 +0000862 struct libwebsocket_context *owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +0000863 int protocol_index;
Andy Green4f3943a2010-11-12 10:44:16 +0000864};
865
Andy Green3182ece2013-01-20 17:08:31 +0800866#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +0000867/**
868 * struct libwebsocket_extension - An extension we know how to cope with
869 *
870 * @name: Formal extension name, eg, "deflate-stream"
871 * @callback: Service callback
Andy Greenb5b23192013-02-11 17:13:32 +0800872 * @per_session_data_size: Libwebsockets will auto-malloc this much
873 * memory for the use of the extension, a pointer
Andy Greend6e09112011-03-05 16:12:15 +0000874 * to it comes in the @user callback parameter
Peter Pentchevc74964e2013-02-07 16:17:13 +0200875 * @per_context_private_data: Optional storage for this extension that
Andy Greenb5b23192013-02-11 17:13:32 +0800876 * is per-context, so it can track stuff across
877 * all sessions, etc, if it wants
Andy Greend6e09112011-03-05 16:12:15 +0000878 */
879
880struct libwebsocket_extension {
881 const char *name;
David Brooks6c6a3d32012-04-20 12:13:50 +0800882 extension_callback_function *callback;
Andy Greend6e09112011-03-05 16:12:15 +0000883 size_t per_session_data_size;
Andy Greenb5b23192013-02-11 17:13:32 +0800884 void *per_context_private_data;
Andy Greend6e09112011-03-05 16:12:15 +0000885};
Andy Green3182ece2013-01-20 17:08:31 +0800886#endif
Andy Greend6e09112011-03-05 16:12:15 +0000887
Andy Green1b265272013-02-09 14:01:09 +0800888/**
889 * struct lws_context_creation_info: parameters to create context with
890 *
891 * @port: Port to listen on... you can use 0 to suppress listening on
892 * any port, that's what you want if you are not running a
893 * websocket server at all but just using it as a client
Andy Green7a506ba2013-02-12 10:13:02 +0800894 * @iface: NULL to bind the listen socket to all interfaces, or the
Andy Green1b265272013-02-09 14:01:09 +0800895 * interface name, eg, "eth2"
896 * @protocols: Array of structures listing supported protocols and a protocol-
897 * specific callback for each one. The list is ended with an
898 * entry that has a NULL callback pointer.
899 * It's not const because we write the owning_server member
900 * @extensions: NULL or array of libwebsocket_extension structs listing the
901 * extensions this context supports. If you configured with
902 * --without-extensions, you should give NULL here.
903 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
904 * to listen using SSL, set to the filepath to fetch the
905 * server cert from, otherwise NULL for unencrypted
906 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
907 * else ignored
908 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green2672fb22013-02-22 09:54:35 +0800909 * @ssl_cipher_list: List of valid ciphers to use (eg,
910 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
911 * or you can leave it as NULL to get "DEFAULT"
Andy Green1b265272013-02-09 14:01:09 +0800912 * @gid: group id to change to after setting listen socket, or -1.
913 * @uid: user id to change to after setting listen socket, or -1.
914 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
915 * @user: optional user pointer that can be recovered via the context
Andy Greenb5b23192013-02-11 17:13:32 +0800916 * pointer using libwebsocket_context_user
Andy Greena690cd02013-02-09 12:25:31 +0800917 * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to
918 * all libwebsocket sockets, client or server
919 * @ka_probes: if ka_time was nonzero, after the timeout expires how many
920 * times to try to get a response from the peer before giving up
921 * and killing the connection
922 * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes
923 * attempt
Andy Green1b265272013-02-09 14:01:09 +0800924 */
925
926struct lws_context_creation_info {
927 int port;
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100928 const char *iface;
Andy Green1b265272013-02-09 14:01:09 +0800929 struct libwebsocket_protocols *protocols;
930 struct libwebsocket_extension *extensions;
931 const char *ssl_cert_filepath;
932 const char *ssl_private_key_filepath;
933 const char *ssl_ca_filepath;
Andy Green2672fb22013-02-22 09:54:35 +0800934 const char *ssl_cipher_list;
Andy Green1b265272013-02-09 14:01:09 +0800935 int gid;
936 int uid;
937 unsigned int options;
938 void *user;
Andy Greena690cd02013-02-09 12:25:31 +0800939 int ka_time;
940 int ka_probes;
941 int ka_interval;
942
Andy Green1b265272013-02-09 14:01:09 +0800943};
944
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800945LWS_VISIBLE LWS_EXTERN
Andy Greenb5b23192013-02-11 17:13:32 +0800946void lws_set_log_level(int level,
947 void (*log_emit_function)(int level, const char *line));
Andy Greend6e09112011-03-05 16:12:15 +0000948
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800949LWS_VISIBLE LWS_EXTERN void
Andy Greenc11db202013-01-19 11:12:16 +0800950lwsl_emit_syslog(int level, const char *line);
951
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800952LWS_VISIBLE LWS_EXTERN struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +0800953libwebsocket_create_context(struct lws_context_creation_info *info);
shysb4e800e2013-10-24 22:12:03 +0800954
955LWS_VISIBLE LWS_EXTERN int
956libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy);
Andy Greenff95d7a2010-10-28 22:36:01 +0100957
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800958LWS_VISIBLE LWS_EXTERN void
Darin Willitsc19456f2011-02-14 17:52:39 +0000959libwebsocket_context_destroy(struct libwebsocket_context *context);
Andy Green6964bb52011-01-23 16:50:33 +0000960
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800961LWS_VISIBLE LWS_EXTERN int
Darin Willitsc19456f2011-02-14 17:52:39 +0000962libwebsocket_service(struct libwebsocket_context *context, int timeout_ms);
Andy Greene92cd172011-01-19 13:11:55 +0000963
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800964LWS_VISIBLE LWS_EXTERN int
Darin Willitsc19456f2011-02-14 17:52:39 +0000965libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green9f990342011-02-12 11:57:45 +0000966 struct pollfd *pollfd);
967
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800968LWS_VISIBLE LWS_EXTERN void *
Alon Levy0291eb32012-10-19 11:21:56 +0200969libwebsocket_context_user(struct libwebsocket_context *context);
970
Andy Green9beedc12013-09-18 08:47:15 +0800971enum pending_timeout {
972 NO_PENDING_TIMEOUT = 0,
973 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
Andy Green5dc62ea2013-09-20 20:26:12 +0800974 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
Andy Green9beedc12013-09-18 08:47:15 +0800975 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
976 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
977 PENDING_TIMEOUT_AWAITING_PING,
978 PENDING_TIMEOUT_CLOSE_ACK,
979 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
980 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
981 PENDING_TIMEOUT_SSL_ACCEPT,
kapejodce64fb02013-11-19 13:38:16 +0100982 PENDING_TIMEOUT_HTTP_CONTENT,
Andy Green9beedc12013-09-18 08:47:15 +0800983};
984
985LWS_EXTERN void
986libwebsocket_set_timeout(struct libwebsocket *wsi,
987 enum pending_timeout reason, int secs);
988
Andy Green4ea60062010-10-30 12:15:07 +0100989/*
990 * IMPORTANT NOTICE!
Andy Greene77ddd82010-11-13 10:03:47 +0000991 *
Andy Green5fd8a5e2010-10-31 11:57:17 +0000992 * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
993 * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
Andy Green4ea60062010-10-30 12:15:07 +0100994 * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
Andy Greene77ddd82010-11-13 10:03:47 +0000995 *
Andy Green4ea60062010-10-30 12:15:07 +0100996 * This allows us to add protocol info before and after the data, and send as
997 * one packet on the network without payload copying, for maximum efficiency.
Andy Greene77ddd82010-11-13 10:03:47 +0000998 *
Andy Green4ea60062010-10-30 12:15:07 +0100999 * So for example you need this kind of code to use libwebsocket_write with a
Andy Greene77ddd82010-11-13 10:03:47 +00001000 * 128-byte payload
1001 *
Andy Greenab990e42010-10-31 12:42:52 +00001002 * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
Andy Greene77ddd82010-11-13 10:03:47 +00001003 *
Andy Greenab990e42010-10-31 12:42:52 +00001004 * // fill your part of the buffer... for example here it's all zeros
1005 * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
Andy Greene77ddd82010-11-13 10:03:47 +00001006 *
Alex Rhatushnyak9f2246e2013-03-09 12:01:47 +08001007 * libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128,
1008 * LWS_WRITE_TEXT);
Andy Greene77ddd82010-11-13 10:03:47 +00001009 *
Andy Green5fd8a5e2010-10-31 11:57:17 +00001010 * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
1011 * use the whole buffer without taking care of the above.
Andy Green4ea60062010-10-30 12:15:07 +01001012 */
1013
Andy Green687b0182011-02-26 11:04:01 +00001014/*
1015 * this is the frame nonce plus two header plus 8 length
Andy Greena41314f2011-05-23 10:00:03 +01001016 * there's an additional two for mux extension per mux nesting level
Andy Green687b0182011-02-26 11:04:01 +00001017 * 2 byte prepend on close will already fit because control frames cannot use
1018 * the big length style
1019 */
Andy Green4739e5c2011-01-22 12:51:57 +00001020
Andy Greena41314f2011-05-23 10:00:03 +01001021#define LWS_SEND_BUFFER_PRE_PADDING (4 + 10 + (2 * MAX_MUX_RECURSION))
David Galeanoe2cf9922013-01-09 18:06:55 +08001022#define LWS_SEND_BUFFER_POST_PADDING 4
Andy Green4ea60062010-10-30 12:15:07 +01001023
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001024LWS_VISIBLE LWS_EXTERN int
Andy Greene77ddd82010-11-13 10:03:47 +00001025libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len,
Andy Green5fd8a5e2010-10-31 11:57:17 +00001026 enum libwebsocket_write_protocol protocol);
Andy Greena2b0ab02010-11-11 12:28:29 +00001027
Gabriel Gritsch890f8e92014-02-15 16:20:25 +08001028/* helper for case where buffer may be const */
1029static inline int
1030libwebsocket_write_http(struct libwebsocket *wsi,
1031 const unsigned char *buf, size_t len)
1032{
1033 return libwebsocket_write(wsi, (unsigned char *)buf, len,
1034 LWS_WRITE_HTTP);
1035}
1036
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001037LWS_VISIBLE LWS_EXTERN int
Andy Greend280b6e2013-01-15 13:40:23 +08001038libwebsockets_serve_http_file(struct libwebsocket_context *context,
1039 struct libwebsocket *wsi, const char *file,
Andy Green79002562013-11-09 11:04:35 +08001040 const char *content_type, const char *other_headers);
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001041LWS_VISIBLE LWS_EXTERN int
Andy Greend280b6e2013-01-15 13:40:23 +08001042libwebsockets_serve_http_file_fragment(struct libwebsocket_context *context,
1043 struct libwebsocket *wsi);
Andy Greenab990e42010-10-31 12:42:52 +00001044
Andy Green4e7a1332013-11-11 07:30:33 +08001045LWS_VISIBLE int libwebsockets_return_http_status(
1046 struct libwebsocket_context *context,
1047 struct libwebsocket *wsi, unsigned int code,
1048 const char *html_body);
1049
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001050LWS_VISIBLE LWS_EXTERN const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00001051libwebsockets_get_protocol(struct libwebsocket *wsi);
1052
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001053LWS_VISIBLE LWS_EXTERN int
Darin Willitsc19456f2011-02-14 17:52:39 +00001054libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green62c54d22011-02-14 09:14:25 +00001055 struct libwebsocket *wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +00001056
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001057LWS_VISIBLE LWS_EXTERN int
Andy Green90c7cbc2011-01-27 06:26:52 +00001058libwebsocket_callback_on_writable_all_protocol(
1059 const struct libwebsocket_protocols *protocol);
1060
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001061LWS_VISIBLE LWS_EXTERN int
Andy Greene39e6ef2014-02-15 16:36:38 +08001062libwebsocket_callback_all_protocol(
1063 const struct libwebsocket_protocols *protocol, int reason);
1064
1065LWS_VISIBLE LWS_EXTERN int
Andy Greena6cbece2011-01-27 20:06:03 +00001066libwebsocket_get_socket_fd(struct libwebsocket *wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +00001067
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001068LWS_VISIBLE LWS_EXTERN int
Andy Green82c3d542011-03-07 21:16:31 +00001069libwebsocket_is_final_fragment(struct libwebsocket *wsi);
1070
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001071LWS_VISIBLE LWS_EXTERN unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08001072libwebsocket_get_reserved_bits(struct libwebsocket *wsi);
1073
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001074LWS_VISIBLE LWS_EXTERN int
Andy Green90c7cbc2011-01-27 06:26:52 +00001075libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable);
1076
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001077LWS_VISIBLE LWS_EXTERN void
Andy Greenb55451c2013-03-16 12:32:27 +08001078libwebsocket_rx_flow_allow_all_protocol(
1079 const struct libwebsocket_protocols *protocol);
1080
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001081LWS_VISIBLE LWS_EXTERN size_t
Andy Green38e57bb2011-01-19 12:20:27 +00001082libwebsockets_remaining_packet_payload(struct libwebsocket *wsi);
1083
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001084LWS_VISIBLE LWS_EXTERN struct libwebsocket *
Andy Green4739e5c2011-01-22 12:51:57 +00001085libwebsocket_client_connect(struct libwebsocket_context *clients,
1086 const char *address,
1087 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +00001088 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +00001089 const char *path,
1090 const char *host,
1091 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +00001092 const char *protocol,
1093 int ietf_version_or_minus_one);
Andy Green4739e5c2011-01-22 12:51:57 +00001094
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001095LWS_VISIBLE LWS_EXTERN struct libwebsocket *
David Brooks2c60d952012-04-20 12:19:01 +08001096libwebsocket_client_connect_extended(struct libwebsocket_context *clients,
1097 const char *address,
1098 int port,
1099 int ssl_connection,
1100 const char *path,
1101 const char *host,
1102 const char *origin,
1103 const char *protocol,
1104 int ietf_version_or_minus_one,
1105 void *userdata);
1106
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001107LWS_VISIBLE LWS_EXTERN const char *
Darin Willitsc19456f2011-02-14 17:52:39 +00001108libwebsocket_canonical_hostname(struct libwebsocket_context *context);
Andy Green2ac5a6f2011-01-28 10:00:18 +00001109
Andy Green4739e5c2011-01-22 12:51:57 +00001110
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001111LWS_VISIBLE LWS_EXTERN void
Andy Greenaaf0b9f2013-01-30 08:12:20 +08001112libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
1113 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +00001114 char *rip, int rip_len);
1115
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001116LWS_VISIBLE LWS_EXTERN int
Andy Green8acc1942011-03-07 20:47:40 +00001117libwebsockets_get_random(struct libwebsocket_context *context,
1118 void *buf, int len);
1119
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001120LWS_VISIBLE LWS_EXTERN int
Andy Green279a3032013-01-17 10:05:39 +08001121lws_daemonize(const char *_lock_path);
1122
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001123LWS_VISIBLE LWS_EXTERN int
Andy Green043a0ba2011-04-25 23:54:11 +08001124lws_send_pipe_choked(struct libwebsocket *wsi);
1125
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001126LWS_VISIBLE LWS_EXTERN int
Andy Green2fd3f2f2013-01-18 09:49:20 +08001127lws_frame_is_binary(struct libwebsocket *wsi);
1128
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001129LWS_VISIBLE LWS_EXTERN unsigned char *
Andy Green2836c642011-03-07 20:47:41 +00001130libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md);
1131
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001132LWS_VISIBLE LWS_EXTERN int
Andy Greenf94e0582011-05-14 15:07:56 +02001133lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
1134
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001135LWS_VISIBLE LWS_EXTERN int
Andy Greenf94e0582011-05-14 15:07:56 +02001136lws_b64_decode_string(const char *in, char *out, int out_size);
1137
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001138LWS_VISIBLE LWS_EXTERN const char *
Andy Green7b405452013-02-01 10:50:15 +08001139lws_get_library_version(void);
1140
Andy Green16ab3182013-02-10 18:02:31 +08001141/* access to headers... only valid while headers valid */
1142
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001143LWS_VISIBLE LWS_EXTERN int
Andy Green16ab3182013-02-10 18:02:31 +08001144lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h);
1145
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001146LWS_VISIBLE LWS_EXTERN int
Andy Greenb5b23192013-02-11 17:13:32 +08001147lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len,
1148 enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001149
Andy Greenea0642a2013-01-29 06:52:00 +08001150/*
1151 * Note: this is not normally needed as a user api. It's provided in case it is
1152 * useful when integrating with other app poll loop service code.
1153 */
1154
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001155LWS_VISIBLE LWS_EXTERN int
Andy Greenea0642a2013-01-29 06:52:00 +08001156libwebsocket_read(struct libwebsocket_context *context,
1157 struct libwebsocket *wsi,
1158 unsigned char *buf, size_t len);
1159
Andy Green3182ece2013-01-20 17:08:31 +08001160#ifndef LWS_NO_EXTENSIONS
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001161LWS_VISIBLE LWS_EXTERN struct libwebsocket_extension *libwebsocket_get_internal_extensions();
Andy Green3182ece2013-01-20 17:08:31 +08001162#endif
Andy Green4cd87a02011-03-06 13:15:32 +00001163
Peter Hinz56885f32011-03-02 22:03:47 +00001164#ifdef __cplusplus
1165}
1166#endif
Andy Greena11fe942011-09-25 10:30:26 +01001167
Andy Greenab990e42010-10-31 12:42:52 +00001168#endif