blob: 15b6f84f6a0e8049e59ad3ed1ed6932b3912258f [file] [log] [blame]
Andy Green7c212cc2010-11-08 20:20:42 +00001/*
2 * libwebsockets - small server side websockets and web server implementation
Andy Greene77ddd82010-11-13 10:03:47 +00003 *
Andy Green40110e82015-12-14 08:52:03 +08004 * Copyright (C) 2010 - 2015 Andy Green <andy@warmcat.com>
Andy Green7c212cc2010-11-08 20:20:42 +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 */
Joakim Soderberg91de9332013-02-06 15:30:33 +090021
Joakim Soderberg4c531232013-02-06 15:26:58 +090022#include "lws_config.h"
Roger A. Light7a474b42015-06-26 11:40:54 +020023#include "lws_config_private.h"
Andy Greene40aa9b2014-04-02 21:02:54 +080024
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020025#ifdef LWS_HAVE_SYS_TYPES_H
Andy Greene40aa9b2014-04-02 21:02:54 +080026#include <sys/types.h>
Joakim Soderberg91de9332013-02-06 15:30:33 +090027#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090028
Andy Green7c212cc2010-11-08 20:20:42 +000029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Patrick Ganstererb13eed42014-03-30 10:19:23 +020032#include <time.h>
Andy Green7c212cc2010-11-08 20:20:42 +000033#include <ctype.h>
Peter Young26757a72013-01-17 10:08:16 +080034#include <limits.h>
Peter Hinz56885f32011-03-02 22:03:47 +000035#include <stdarg.h>
Andy Green112f9802015-12-04 07:22:44 +080036#include <assert.h>
Peter Hinz56885f32011-03-02 22:03:47 +000037
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020038#ifdef LWS_HAVE_SYS_STAT_H
Peter Hinz56885f32011-03-02 22:03:47 +000039#include <sys/stat.h>
Patrick Gansterere5720a32014-02-28 00:57:19 +010040#endif
Peter Hinz56885f32011-03-02 22:03:47 +000041
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010042#if defined(WIN32) || defined(_WIN32)
Stephan Eberleb820e2c2015-10-23 08:10:55 +020043#if (WINVER < 0x0501)
44#undef WINVER
45#undef _WIN32_WINNT
46#define WINVER 0x0501
47#define _WIN32_WINNT WINVER
48#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090049#define LWS_NO_DAEMONIZE
Patrick Gansterer2dbd8372014-02-28 12:37:52 +010050#define LWS_ERRNO WSAGetLastError()
51#define LWS_EAGAIN WSAEWOULDBLOCK
52#define LWS_EALREADY WSAEALREADY
53#define LWS_EINPROGRESS WSAEINPROGRESS
54#define LWS_EINTR WSAEINTR
55#define LWS_EISCONN WSAEISCONN
56#define LWS_EWOULDBLOCK WSAEWOULDBLOCK
Patrick Ganstererb47f87b2014-03-30 09:18:05 +020057#define LWS_POLLHUP (FD_CLOSE)
Patrick Gansterer0fc37b62014-03-28 15:44:56 +010058#define LWS_POLLIN (FD_READ | FD_ACCEPT)
59#define LWS_POLLOUT (FD_WRITE)
Patrick Gansterer73882e42014-03-29 08:25:58 +010060#define MSG_NOSIGNAL 0
61#define SHUT_RDWR SD_BOTH
62#define SOL_TCP IPPROTO_TCP
Joakim Soderberg4c531232013-02-06 15:26:58 +090063
Andy Green158e8042014-04-02 14:25:10 +080064#define compatible_close(fd) closesocket(fd)
Andy Greenaa775fd2015-12-26 08:56:58 +080065#define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
Andy Greenc53f7ca2015-11-14 07:35:27 +080066#define lws_socket_is_valid(x) (!!x)
Andy Green40110e82015-12-14 08:52:03 +080067#define LWS_SOCK_INVALID 0
Peter Hinz56885f32011-03-02 22:03:47 +000068#include <winsock2.h>
Andy Greeneee0d8a2015-12-17 15:15:12 +080069#include <ws2tcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000070#include <windows.h>
Joakim Soderbergd2f5b192014-04-07 11:28:08 +020071#include <tchar.h>
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020072#ifdef LWS_HAVE_IN6ADDR_H
Andy Green0f58db32014-04-12 11:10:35 +080073#include <in6addr.h>
74#endif
Joakim Soderbergd2f5b192014-04-07 11:28:08 +020075#include <mstcpip.h>
76
77#ifndef __func__
78#define __func__ __FUNCTION__
79#endif
80
Patrick Gansterer6bb4b622014-04-15 18:39:26 +020081#ifdef _WIN32_WCE
82#define vsnprintf _vsnprintf
83#endif
84
Andy Green158e8042014-04-02 14:25:10 +080085#else /* not windows --> */
Andy Green8c0d3c02015-11-02 20:34:12 +080086
Patrick Ganstererb13eed42014-03-30 10:19:23 +020087#include <fcntl.h>
Patrick Ganstererb13eed42014-03-30 10:19:23 +020088#include <strings.h>
89#include <unistd.h>
Andy Greene77ddd82010-11-13 10:03:47 +000090#include <sys/types.h>
Andy Green5f2a8152015-11-02 08:21:08 +080091#ifndef MBED_OPERATORS
Andy Green8c0d3c02015-11-02 20:34:12 +080092#ifndef __cplusplus
93#include <errno.h>
94#endif
Andy Green5f2a8152015-11-02 08:21:08 +080095#include <netdb.h>
96#include <signal.h>
Andy Green7c212cc2010-11-08 20:20:42 +000097#include <sys/socket.h>
Andy Greene40aa9b2014-04-02 21:02:54 +080098#ifdef LWS_BUILTIN_GETIFADDRS
99 #include <getifaddrs.h>
100#else
101 #include <ifaddrs.h>
102#endif
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800103#if defined (__ANDROID__)
104#include <syslog.h>
105#else
Andy Greene40aa9b2014-04-02 21:02:54 +0800106#include <sys/syslog.h>
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800107#endif
Andy Greene40aa9b2014-04-02 21:02:54 +0800108#include <sys/un.h>
109#include <sys/socket.h>
110#include <netdb.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000111#include <netinet/in.h>
Andy Green6c939552011-03-08 08:56:57 +0000112#include <netinet/tcp.h>
Andy Greenb45993c2010-12-18 15:13:50 +0000113#include <arpa/inet.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000114#include <poll.h>
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800115#ifdef LWS_USE_LIBEV
116#include <ev.h>
117#endif /* LWS_USE_LIBEV */
Andy Green7c212cc2010-11-08 20:20:42 +0000118#include <sys/mman.h>
Andy Green5f2a8152015-11-02 08:21:08 +0800119
120#endif /* MBED */
121
122#ifndef LWS_NO_FORK
123#ifdef LWS_HAVE_SYS_PRCTL_H
124#include <sys/prctl.h>
125#endif
126#endif
127
Andy Green038d5822011-02-14 20:58:26 +0000128#include <sys/time.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000129
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100130#define LWS_ERRNO errno
131#define LWS_EAGAIN EAGAIN
132#define LWS_EALREADY EALREADY
133#define LWS_EINPROGRESS EINPROGRESS
134#define LWS_EINTR EINTR
135#define LWS_EISCONN EISCONN
136#define LWS_EWOULDBLOCK EWOULDBLOCK
Patrick Ganstererb47f87b2014-03-30 09:18:05 +0200137#define LWS_POLLHUP (POLLHUP|POLLERR)
138#define LWS_POLLIN (POLLIN)
139#define LWS_POLLOUT (POLLOUT)
Andy Green158e8042014-04-02 14:25:10 +0800140#define compatible_close(fd) close(fd)
Andy Green158e8042014-04-02 14:25:10 +0800141#define lws_set_blocking_send(wsi)
Andy Green2cd30742015-11-02 13:10:33 +0800142
143#ifdef MBED_OPERATORS
144#define lws_socket_is_valid(x) ((x) != NULL)
145#define LWS_SOCK_INVALID (NULL)
146#else
Andy Greenc53f7ca2015-11-14 07:35:27 +0800147#define lws_socket_is_valid(x) (x >= 0)
148#define LWS_SOCK_INVALID (-1)
Peter Hinz56885f32011-03-02 22:03:47 +0000149#endif
Andy Green2cd30742015-11-02 13:10:33 +0800150#endif
Peter Hinz56885f32011-03-02 22:03:47 +0000151
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200152#ifndef LWS_HAVE_BZERO
Andy Greene5ea1f92014-11-18 18:25:24 +0800153#ifndef bzero
Patrick Gansterer4a837272014-02-28 13:17:49 +0100154#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
155#endif
Andy Greene5ea1f92014-11-18 18:25:24 +0800156#endif
Patrick Gansterer4a837272014-02-28 13:17:49 +0100157
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200158#ifndef LWS_HAVE_STRERROR
Patrick Gansterer9d614912014-02-28 00:59:53 +0100159#define strerror(x) ""
160#endif
161
Andy Green7c212cc2010-11-08 20:20:42 +0000162#ifdef LWS_OPENSSL_SUPPORT
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200163#ifdef USE_WOLFSSL
ABruines80a70682015-08-09 22:56:32 +0200164#ifdef USE_OLD_CYASSL
165#include <cyassl/openssl/ssl.h>
166#include <cyassl/error-ssl.h>
167#else
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200168#include <wolfssl/openssl/ssl.h>
169#include <wolfssl/error-ssl.h>
ABruines80a70682015-08-09 22:56:32 +0200170#endif /* not USE_OLD_CYASSL */
Andy Green23c5f2e2013-02-06 15:43:00 +0900171#else
Andy Green7c212cc2010-11-08 20:20:42 +0000172#include <openssl/ssl.h>
173#include <openssl/evp.h>
174#include <openssl/err.h>
Andy Green70dfebd2010-12-20 09:35:03 +0000175#include <openssl/md5.h>
Andy Greene2522172011-01-18 17:14:03 +0000176#include <openssl/sha.h>
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200177#endif /* not USE_WOLFSSL */
Darin Willitsdb9ba422011-02-14 20:56:24 +0000178#endif
179
Andy Green7c212cc2010-11-08 20:20:42 +0000180#include "libwebsockets.h"
181
Andy Green8c0d3c02015-11-02 20:34:12 +0800182#if defined(MBED_OPERATORS)
183#undef compatible_close
184#define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
Andy Green11f27342015-11-08 12:10:26 +0800185#ifndef BIG_ENDIAN
186#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
187#endif
188#ifndef LITTLE_ENDIAN
189#define LITTLE_ENDIAN 1234
190#endif
191#ifndef BYTE_ORDER
192#define BYTE_ORDER LITTLE_ENDIAN
193#endif
Andy Green8c0d3c02015-11-02 20:34:12 +0800194#endif
195
Andy Green158e8042014-04-02 14:25:10 +0800196#if defined(WIN32) || defined(_WIN32)
197
198#ifndef BIG_ENDIAN
199#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
200#endif
201#ifndef LITTLE_ENDIAN
202#define LITTLE_ENDIAN 1234
203#endif
204#ifndef BYTE_ORDER
205#define BYTE_ORDER LITTLE_ENDIAN
206#endif
Andy Green11f27342015-11-08 12:10:26 +0800207#ifndef u_int64_t
Andy Green158e8042014-04-02 14:25:10 +0800208typedef unsigned __int64 u_int64_t;
Andy Green11f27342015-11-08 12:10:26 +0800209#endif
Andy Green158e8042014-04-02 14:25:10 +0800210
211#undef __P
212#ifndef __P
213#if __STDC__
214#define __P(protos) protos
215#else
216#define __P(protos) ()
217#endif
218#endif
219
220#else
221
222#include <sys/stat.h>
223#include <sys/cdefs.h>
224#include <sys/time.h>
225
226#if defined(__APPLE__)
227#include <machine/endian.h>
228#elif defined(__FreeBSD__)
229#include <sys/endian.h>
230#elif defined(__linux__)
231#include <endian.h>
232#endif
233
Andy Green8c0d3c02015-11-02 20:34:12 +0800234#ifdef __cplusplus
235extern "C" {
236#endif
Alejandro Meryead8afe2014-12-07 03:36:11 +0100237#include <stddef.h>
238
239#ifndef container_of
240#define container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
241#endif
242
emironova49d0842014-09-16 14:05:13 +0400243#if defined(__QNX__)
244 #include <gulliver.h>
245 #if defined(__LITTLEENDIAN__)
246 #define BYTE_ORDER __LITTLEENDIAN__
247 #define LITTLE_ENDIAN __LITTLEENDIAN__
248 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
249 #endif
250 #if defined(__BIGENDIAN__)
251 #define BYTE_ORDER __BIGENDIAN__
252 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
253 #define BIG_ENDIAN __BIGENDIAN__
254 #endif
255#endif
256
Andy Green158e8042014-04-02 14:25:10 +0800257#if !defined(BYTE_ORDER)
258# define BYTE_ORDER __BYTE_ORDER
259#endif
260#if !defined(LITTLE_ENDIAN)
261# define LITTLE_ENDIAN __LITTLE_ENDIAN
262#endif
263#if !defined(BIG_ENDIAN)
264# define BIG_ENDIAN __BIG_ENDIAN
265#endif
266
267#endif
268
Darin Willitsc19456f2011-02-14 17:52:39 +0000269/*
270 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
271 * but happily have something equivalent in the SO_NOSIGPIPE flag.
272 */
273#ifdef __APPLE__
Andy Green6ee372f2012-04-09 15:09:01 +0800274#define MSG_NOSIGNAL SO_NOSIGPIPE
Darin Willitsc19456f2011-02-14 17:52:39 +0000275#endif
276
Bud Davis229bfec2015-01-30 10:13:01 +0800277#ifdef _WIN32
278#ifndef FD_HASHTABLE_MODULUS
279#define FD_HASHTABLE_MODULUS 32
280#endif
281#endif
282
Andy Greenc0d6b632013-01-12 23:42:17 +0800283#ifndef LWS_MAX_HEADER_LEN
Andy Green16ab3182013-02-10 18:02:31 +0800284#define LWS_MAX_HEADER_LEN 1024
Andy Greenc0d6b632013-01-12 23:42:17 +0800285#endif
Andy Green3df58002015-12-25 12:44:12 +0800286#ifndef LWS_MAX_HEADER_POOL
287#define LWS_MAX_HEADER_POOL 16
288#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800289#ifndef LWS_MAX_PROTOCOLS
Andy Greend91d5e82013-02-10 16:00:47 +0800290#define LWS_MAX_PROTOCOLS 5
Andy Greenc0d6b632013-01-12 23:42:17 +0800291#endif
292#ifndef LWS_MAX_EXTENSIONS_ACTIVE
Andy Greend91d5e82013-02-10 16:00:47 +0800293#define LWS_MAX_EXTENSIONS_ACTIVE 3
Andy Greenc0d6b632013-01-12 23:42:17 +0800294#endif
295#ifndef SPEC_LATEST_SUPPORTED
Andy Greend85cb202011-09-25 09:32:54 +0100296#define SPEC_LATEST_SUPPORTED 13
Andy Greenc0d6b632013-01-12 23:42:17 +0800297#endif
298#ifndef AWAITING_TIMEOUT
Andy Greenc0b0c3d2015-11-20 18:52:48 +0800299#define AWAITING_TIMEOUT 5
Andy Greenc0d6b632013-01-12 23:42:17 +0800300#endif
301#ifndef CIPHERS_LIST_STRING
David Galeanof177f2a2013-01-10 10:15:19 +0800302#define CIPHERS_LIST_STRING "DEFAULT"
Andy Greenc0d6b632013-01-12 23:42:17 +0800303#endif
Andy Greena824d182013-01-15 20:52:29 +0800304#ifndef LWS_SOMAXCONN
305#define LWS_SOMAXCONN SOMAXCONN
306#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000307
Andy Greene2522172011-01-18 17:14:03 +0000308#define MAX_WEBSOCKET_04_KEY_LEN 128
Andy Green54495112013-02-06 21:10:16 +0900309#define LWS_MAX_SOCKET_IO_BUF 4096
Andy Greenc0d6b632013-01-12 23:42:17 +0800310
311#ifndef SYSTEM_RANDOM_FILEPATH
Andy Green4739e5c2011-01-22 12:51:57 +0000312#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
Andy Greenc0d6b632013-01-12 23:42:17 +0800313#endif
314#ifndef LWS_MAX_ZLIB_CONN_BUFFER
315#define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
316#endif
317
Andy Green65b0e912013-01-16 07:59:47 +0800318/*
319 * if not in a connection storm, check for incoming
320 * connections this many normal connection services
321 */
Andy Green54806b12015-12-17 17:03:59 +0800322#define LWS_lserv_mod 10
Andy Greene2522172011-01-18 17:14:03 +0000323
Andy Green5fd55cd2011-04-23 10:54:53 +0100324enum lws_websocket_opcodes_07 {
Andy Green54806b12015-12-17 17:03:59 +0800325 LWSWSOPC_CONTINUATION = 0,
326 LWSWSOPC_TEXT_FRAME = 1,
327 LWSWSOPC_BINARY_FRAME = 2,
Andy Greena41314f2011-05-23 10:00:03 +0100328
Andy Green54806b12015-12-17 17:03:59 +0800329 LWSWSOPC_NOSPEC__MUX = 7,
Andy Greena41314f2011-05-23 10:00:03 +0100330
331 /* control extensions 8+ */
332
Andy Green54806b12015-12-17 17:03:59 +0800333 LWSWSOPC_CLOSE = 8,
334 LWSWSOPC_PING = 9,
335 LWSWSOPC_PONG = 0xa,
Andy Green5fd55cd2011-04-23 10:54:53 +0100336};
337
Andy Greena41314f2011-05-23 10:00:03 +0100338
Andy Green7c212cc2010-11-08 20:20:42 +0000339enum lws_connection_states {
Andy Green54806b12015-12-17 17:03:59 +0800340 LWSS_HTTP,
341 LWSS_HTTP_ISSUING_FILE,
342 LWSS_HTTP_HEADERS,
343 LWSS_HTTP_BODY,
344 LWSS_DEAD_SOCKET,
345 LWSS_ESTABLISHED,
346 LWSS_CLIENT_UNCONNECTED,
347 LWSS_RETURNED_CLOSE_ALREADY,
348 LWSS_AWAITING_CLOSE_ACK,
349 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
Andy Green40110e82015-12-14 08:52:03 +0800350
Andy Green54806b12015-12-17 17:03:59 +0800351 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
352 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
353 LWSS_HTTP2_ESTABLISHED,
Andy Green7c212cc2010-11-08 20:20:42 +0000354};
355
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400356enum http_version {
357 HTTP_VERSION_1_0,
358 HTTP_VERSION_1_1,
359};
360
361enum http_connection_type {
362 HTTP_CONNECTION_CLOSE,
363 HTTP_CONNECTION_KEEP_ALIVE
364};
365
Andy Green024eb6c2014-10-08 12:00:53 +0800366enum lws_pending_protocol_send {
367 LWS_PPS_NONE,
368 LWS_PPS_HTTP2_MY_SETTINGS,
369 LWS_PPS_HTTP2_ACK_SETTINGS,
Andy Greenbbbf07a2014-10-27 16:46:44 +0800370 LWS_PPS_HTTP2_PONG,
Andy Green024eb6c2014-10-08 12:00:53 +0800371};
372
Andy Green7c212cc2010-11-08 20:20:42 +0000373enum lws_rx_parse_state {
374 LWS_RXPS_NEW,
Andy Greene77ddd82010-11-13 10:03:47 +0000375
Andy Green3e5eb782011-01-18 18:14:26 +0000376 LWS_RXPS_04_MASK_NONCE_1,
377 LWS_RXPS_04_MASK_NONCE_2,
378 LWS_RXPS_04_MASK_NONCE_3,
379
380 LWS_RXPS_04_FRAME_HDR_1,
Andy Green38e57bb2011-01-19 12:20:27 +0000381 LWS_RXPS_04_FRAME_HDR_LEN,
382 LWS_RXPS_04_FRAME_HDR_LEN16_2,
383 LWS_RXPS_04_FRAME_HDR_LEN16_1,
384 LWS_RXPS_04_FRAME_HDR_LEN64_8,
385 LWS_RXPS_04_FRAME_HDR_LEN64_7,
386 LWS_RXPS_04_FRAME_HDR_LEN64_6,
387 LWS_RXPS_04_FRAME_HDR_LEN64_5,
388 LWS_RXPS_04_FRAME_HDR_LEN64_4,
389 LWS_RXPS_04_FRAME_HDR_LEN64_3,
390 LWS_RXPS_04_FRAME_HDR_LEN64_2,
391 LWS_RXPS_04_FRAME_HDR_LEN64_1,
Andy Green3e5eb782011-01-18 18:14:26 +0000392
Andy Green283d0a22011-04-24 05:46:23 +0100393 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
394 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
395 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
396 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
397
Andy Green7c212cc2010-11-08 20:20:42 +0000398 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
399};
400
401
Andy Green0d338332011-02-12 11:57:43 +0000402enum connection_mode {
Andy Green54806b12015-12-17 17:03:59 +0800403 LWSCM_HTTP_SERVING,
404 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
405 LWSCM_PRE_WS_SERVING_ACCEPT,
Andy Greend280b6e2013-01-15 13:40:23 +0800406
Andy Green54806b12015-12-17 17:03:59 +0800407 LWSCM_WS_SERVING,
408 LWSCM_WS_CLIENT,
Andy Green40110e82015-12-14 08:52:03 +0800409
Andy Green54806b12015-12-17 17:03:59 +0800410 LWSCM_HTTP2_SERVING,
Andy Green0d338332011-02-12 11:57:43 +0000411
Andy Greene2160712013-01-28 12:19:10 +0800412 /* transient, ssl delay hiding */
Andy Green54806b12015-12-17 17:03:59 +0800413 LWSCM_SSL_ACK_PENDING,
Andy Greene2160712013-01-28 12:19:10 +0800414
Andy Greenbe93fef2011-02-14 20:25:43 +0000415 /* transient modes */
Andy Green54806b12015-12-17 17:03:59 +0800416 LWSCM_WSCL_WAITING_CONNECT,
417 LWSCM_WSCL_WAITING_PROXY_REPLY,
418 LWSCM_WSCL_ISSUE_HANDSHAKE,
419 LWSCM_WSCL_ISSUE_HANDSHAKE2,
420 LWSCM_WSCL_WAITING_SSL,
421 LWSCM_WSCL_WAITING_SERVER_REPLY,
422 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
423 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
Andy Greenbe93fef2011-02-14 20:25:43 +0000424
Andy Green0d338332011-02-12 11:57:43 +0000425 /* special internal types */
Andy Green54806b12015-12-17 17:03:59 +0800426 LWSCM_SERVER_LISTENER,
Andy Green0d338332011-02-12 11:57:43 +0000427};
428
Andy Greenca0a1292013-03-16 11:24:23 +0800429enum {
430 LWS_RXFLOW_ALLOW = (1 << 0),
431 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
432};
433
Andy Green1fb95e82015-12-26 17:20:34 +0800434/* this is not usable directly by user code any more, lws_close_reason() */
435#define LWS_WRITE_CLOSE 4
436
Andy Green4b85c1d2015-12-04 11:08:32 +0800437struct lws_protocols;
438struct lws;
Andy Greene92cd172011-01-19 13:11:55 +0000439
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800440#ifdef LWS_USE_LIBEV
441struct lws_io_watcher {
442 struct ev_io watcher;
Andy Green4b85c1d2015-12-04 11:08:32 +0800443 struct lws_context* context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800444};
445
446struct lws_signal_watcher {
447 struct ev_signal watcher;
Andy Green4b85c1d2015-12-04 11:08:32 +0800448 struct lws_context* context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800449};
450#endif /* LWS_USE_LIBEV */
451
Bud Davis229bfec2015-01-30 10:13:01 +0800452#ifdef _WIN32
453#define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
Andy Green3ef579b2015-12-04 09:23:56 +0800454struct lws_fd_hashtable {
Andy Green4b85c1d2015-12-04 11:08:32 +0800455 struct lws **wsi;
Bud Davis229bfec2015-01-30 10:13:01 +0800456 int length;
457};
458#endif
459
Andy Green3df58002015-12-25 12:44:12 +0800460/*
461 * This is totally opaque to code using the library. It's exported as a
462 * forward-reference pointer-only declaration; the user can use the pointer with
463 * other APIs to get information out of it.
464 */
465
466struct lws_fragments {
467 unsigned short offset;
468 unsigned short len;
469 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
470};
471
472/*
473 * these are assigned from a pool held in the context.
474 * Both client and server mode uses them for http header analysis
475 */
476
477struct allocated_headers {
Andy Greende132b92015-12-25 13:48:20 +0800478 char *data; /* prepared by context init to point to dedicated storage */
479 /*
480 * the randomly ordered fragments, indexed by frag_index and
481 * lws_fragments->nfrag for continuation.
482 */
483 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
Andy Green3df58002015-12-25 12:44:12 +0800484 /*
485 * for each recognized token, frag_index says which frag[] his data
486 * starts in (0 means the token did not appear)
487 * the actual header data gets dumped as it comes in, into data[]
488 */
489 unsigned char frag_index[WSI_TOKEN_COUNT];
Andy Green3df58002015-12-25 12:44:12 +0800490#ifndef LWS_NO_CLIENT
491 char initial_handshake_hash_base64[30];
492 unsigned short c_port;
493#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800494
495 unsigned short pos;
496 unsigned char in_use;
497 unsigned char nfrag;
Andy Green3df58002015-12-25 12:44:12 +0800498};
499
Andy Green4b85c1d2015-12-04 11:08:32 +0800500struct lws_context {
Andy Greenaa775fd2015-12-26 08:56:58 +0800501 time_t last_timeout_check_s;
502 struct lws_plat_file_ops fops;
Patrick Gansterer0fc37b62014-03-28 15:44:56 +0100503#ifdef _WIN32
504 WSAEVENT *events;
505#endif
Andy Green4b85c1d2015-12-04 11:08:32 +0800506 struct lws_pollfd *fds;
Bud Davis229bfec2015-01-30 10:13:01 +0800507#ifdef _WIN32
508/* different implementation between unix and windows */
Andy Green3ef579b2015-12-04 09:23:56 +0800509 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
Bud Davis229bfec2015-01-30 10:13:01 +0800510#else
Andy Green4b85c1d2015-12-04 11:08:32 +0800511 struct lws **lws_lookup; /* fd to wsi */
Bud Davis229bfec2015-01-30 10:13:01 +0800512#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800513#ifdef LWS_USE_LIBEV
514 struct ev_loop* io_loop;
515 struct lws_io_watcher w_accept;
516 struct lws_signal_watcher w_sigint;
Andrew Canadaya8f47c92015-04-26 22:50:59 -0400517 lws_ev_signal_cb* lws_ev_sigint_cb;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800518#endif /* LWS_USE_LIBEV */
Mattias Lundberg03bb8f92014-02-18 10:06:57 +0100519 const char *iface;
Andy Greenaa775fd2015-12-26 08:56:58 +0800520 const struct lws_token_limits *token_limits;
521 void *user_space;
522#ifndef LWS_NO_SERVER
523 struct lws *wsi_listening;
524#endif
525 const struct lws_protocols *protocols;
526 void *http_header_data;
527 struct allocated_headers *ah_pool;
528#ifdef LWS_OPENSSL_SUPPORT
529 SSL_CTX *ssl_ctx;
530 SSL_CTX *ssl_client_ctx;
531 struct lws *pending_read_list; /* linked list */
532#endif
533#ifndef LWS_NO_EXTENSIONS
534 const struct lws_extension *extensions;
535#endif
Andy Green44eee682011-02-10 09:32:24 +0000536
Andy Greenb8b247d2013-01-22 07:20:08 +0800537 /*
538 * usable by anything in the service code, but only if the scope
539 * does not last longer than the service action (since next service
540 * of any socket can likewise use it and overwrite)
541 */
Andy Green54806b12015-12-17 17:03:59 +0800542 unsigned char serv_buf[LWS_MAX_SOCKET_IO_BUF];
Andy Greenaa775fd2015-12-26 08:56:58 +0800543 char http_proxy_address[128];
544 char proxy_basic_auth_token[128];
545 char canonical_hostname[128];
546#ifdef LWS_LATENCY
547 unsigned long worst_latency;
548 char worst_latency_info[256];
549#endif
Andy Greenb8b247d2013-01-22 07:20:08 +0800550
Andy Greenaa775fd2015-12-26 08:56:58 +0800551 lws_sockfd_type lserv_fd;
552
553 int fds_count;
554 int max_fds;
555 int listen_port;
556#ifdef LWS_USE_LIBEV
557 int use_ev_sigint;
558#endif
Andy Green24cba922013-01-19 13:56:10 +0800559 int started_with_parent;
560
Andy Green44eee682011-02-10 09:32:24 +0000561 int fd_random;
Andy Green54806b12015-12-17 17:03:59 +0800562 int lserv_mod;
563 int lserv_count;
Andy Green54806b12015-12-17 17:03:59 +0800564 int lserv_seen;
Andy Greenaa775fd2015-12-26 08:56:58 +0800565 unsigned int http_proxy_port;
566 unsigned int options;
Andy Green6ee372f2012-04-09 15:09:01 +0800567
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100568 /*
569 * set to the Thread ID that's doing the service loop just before entry
570 * to poll indicates service thread likely idling in poll()
571 * volatile because other threads may check it as part of processing
572 * for pollfd event change.
573 */
574 volatile int service_tid;
Andy Greenc35b36b2015-12-24 13:00:54 +0800575 int service_tid_detected;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100576#ifndef _WIN32
577 int dummy_pipe_fds[2];
578#endif
579
Andy Greenaa775fd2015-12-26 08:56:58 +0800580 int count_protocols;
Andy Greena690cd02013-02-09 12:25:31 +0800581 int ka_time;
582 int ka_probes;
583 int ka_interval;
584
Andy Greenb45993c2010-12-18 15:13:50 +0000585#ifdef LWS_OPENSSL_SUPPORT
586 int use_ssl;
James Devine5b34c972013-12-14 11:41:29 +0800587 int allow_non_ssl_on_ssl_port;
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +0800588 unsigned int user_supplied_ssl_ctx:1;
Andy Greenaa775fd2015-12-26 08:56:58 +0800589#define lws_ssl_anybody_has_buffered_read(ctx) \
590 (ctx->use_ssl && ctx->pending_read_list)
Andy Green52815602015-01-29 08:36:18 +0800591#else
592#define lws_ssl_anybody_has_buffered_read(ctx) (0)
Andy Greenb45993c2010-12-18 15:13:50 +0000593#endif
Andy Green40110e82015-12-14 08:52:03 +0800594
Andy Green3df58002015-12-25 12:44:12 +0800595 short max_http_header_data;
596 short max_http_header_pool;
597 short ah_count_in_use;
Andy Greenb45993c2010-12-18 15:13:50 +0000598};
599
Andy Greena717df22014-04-11 13:14:37 +0800600enum {
601 LWS_EV_READ = (1 << 0),
602 LWS_EV_WRITE = (1 << 1),
603 LWS_EV_START = (1 << 2),
604 LWS_EV_STOP = (1 << 3),
605};
606
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800607#ifdef LWS_USE_LIBEV
608#define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
Andy Greena717df22014-04-11 13:14:37 +0800609LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
610LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800611lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
Andy Greena717df22014-04-11 13:14:37 +0800612LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800613lws_libev_io(struct lws *wsi, int flags);
Andy Greena717df22014-04-11 13:14:37 +0800614LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800615lws_libev_init_fd_table(struct lws_context *context);
Andy Greena717df22014-04-11 13:14:37 +0800616LWS_EXTERN void
Andy Greena01fb522015-12-14 07:51:15 +0800617lws_libev_run(const struct lws_context *context);
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800618#else
619#define LWS_LIBEV_ENABLED(context) (0)
Andy Green8c0d3c02015-11-02 20:34:12 +0800620#ifdef LWS_POSIX
Andy Greena717df22014-04-11 13:14:37 +0800621#define lws_feature_status_libev(_a) \
622 lwsl_notice("libev support not compiled in\n")
Andy Green8c0d3c02015-11-02 20:34:12 +0800623#else
624#define lws_feature_status_libev(_a)
625#endif
Andy Green11c05bf2015-12-16 18:19:08 +0800626#define lws_libev_accept(_a, _b) ((void) 0)
627#define lws_libev_io(_a, _b) ((void) 0)
Andy Greena717df22014-04-11 13:14:37 +0800628#define lws_libev_init_fd_table(_a) (0)
vpeter47c00fc32014-04-27 13:35:28 +0200629#define lws_libev_run(_a) ((void) 0)
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800630#endif
631
Andy Green055f2972014-03-24 16:09:25 +0800632#ifdef LWS_USE_IPV6
Andy Greendc8a3a82015-12-06 09:15:27 +0800633#define LWS_IPV6_ENABLED(context) \
634 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
James Devine3f13ea22014-03-24 16:09:25 +0800635#else
636#define LWS_IPV6_ENABLED(context) (0)
637#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800638
Andy Greenb1a9e502013-11-10 15:15:21 +0800639enum uri_path_states {
640 URIPS_IDLE,
641 URIPS_SEEN_SLASH,
642 URIPS_SEEN_SLASH_DOT,
643 URIPS_SEEN_SLASH_DOT_DOT,
644};
645
646enum uri_esc_states {
647 URIES_IDLE,
648 URIES_SEEN_PERCENT,
649 URIES_SEEN_PERCENT_H1,
650};
Andy Greenf3d3b402011-02-09 07:16:34 +0000651
Andy Green024eb6c2014-10-08 12:00:53 +0800652/* notice that these union members:
Andy Green40110e82015-12-14 08:52:03 +0800653 *
Andy Green024eb6c2014-10-08 12:00:53 +0800654 * hdr
655 * http
656 * http2
Andy Green40110e82015-12-14 08:52:03 +0800657 *
Andy Green024eb6c2014-10-08 12:00:53 +0800658 * all have a pointer to allocated_headers struct as their first member.
Andy Green40110e82015-12-14 08:52:03 +0800659 *
Andy Green024eb6c2014-10-08 12:00:53 +0800660 * It means for allocated_headers access, the three union paths can all be
Peter Pentchevbb085da2015-12-03 15:55:11 +0200661 * used interchangeably to access the same data
Andy Green024eb6c2014-10-08 12:00:53 +0800662 */
663
Andy Green84fd9492013-11-09 11:40:32 +0800664struct _lws_http_mode_related {
Andy Green024eb6c2014-10-08 12:00:53 +0800665 /* MUST be first in struct */
Andy Green84fd9492013-11-09 11:40:32 +0800666 struct allocated_headers *ah; /* mirroring _lws_header_related */
Andy Green84fd9492013-11-09 11:40:32 +0800667 unsigned long filepos;
668 unsigned long filelen;
Andy Greenaa775fd2015-12-26 08:56:58 +0800669 lws_filefd_type fd;
kapejodce64fb02013-11-19 13:38:16 +0100670
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400671 enum http_version request_version;
672 enum http_connection_type connection_type;
Andy Green2cd30742015-11-02 13:10:33 +0800673 unsigned int content_length;
674 unsigned int content_remain;
Andy Green84fd9492013-11-09 11:40:32 +0800675};
676
Andy Green024eb6c2014-10-08 12:00:53 +0800677#ifdef LWS_USE_HTTP2
678
679enum lws_http2_settings {
680 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
681 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
682 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
683 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
684 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
685 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
Andy Green40110e82015-12-14 08:52:03 +0800686
Andy Green024eb6c2014-10-08 12:00:53 +0800687 LWS_HTTP2_SETTINGS__COUNT /* always last */
Andy Greena54f2322014-09-30 09:43:14 +0800688};
689
Andy Green024eb6c2014-10-08 12:00:53 +0800690enum lws_http2_wellknown_frame_types {
691 LWS_HTTP2_FRAME_TYPE_DATA,
692 LWS_HTTP2_FRAME_TYPE_HEADERS,
693 LWS_HTTP2_FRAME_TYPE_PRIORITY,
694 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
695 LWS_HTTP2_FRAME_TYPE_SETTINGS,
696 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
697 LWS_HTTP2_FRAME_TYPE_PING,
698 LWS_HTTP2_FRAME_TYPE_GOAWAY,
699 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
700 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
Andy Green40110e82015-12-14 08:52:03 +0800701
Andy Green024eb6c2014-10-08 12:00:53 +0800702 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
703};
704
Andy Green91b05892014-10-17 08:38:44 +0800705enum lws_http2_flags {
706 LWS_HTTP2_FLAG_END_STREAM = 1,
707 LWS_HTTP2_FLAG_END_HEADERS = 4,
708 LWS_HTTP2_FLAG_PADDED = 8,
709 LWS_HTTP2_FLAG_PRIORITY = 0x20,
710
711 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
712};
713
Andy Green024eb6c2014-10-08 12:00:53 +0800714#define LWS_HTTP2_STREAM_ID_MASTER 0
715#define LWS_HTTP2_FRAME_HEADER_LENGTH 9
716#define LWS_HTTP2_SETTINGS_LENGTH 6
717
718struct http2_settings {
719 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
720};
721
Andy Greenecc2e722014-10-09 16:57:47 +0800722enum http2_hpack_state {
Andy Green40110e82015-12-14 08:52:03 +0800723
Andy Green200f3852014-10-18 12:23:05 +0800724 /* optional before first header block */
725 HPKS_OPT_PADDING,
726 HKPS_OPT_E_DEPENDENCY,
727 HKPS_OPT_WEIGHT,
Andy Green40110e82015-12-14 08:52:03 +0800728
Andy Green200f3852014-10-18 12:23:05 +0800729 /* header block */
Andy Greenecc2e722014-10-09 16:57:47 +0800730 HPKS_TYPE,
Andy Green40110e82015-12-14 08:52:03 +0800731
Andy Green2add6342014-10-12 08:38:16 +0800732 HPKS_IDX_EXT,
Andy Green40110e82015-12-14 08:52:03 +0800733
Andy Greenecc2e722014-10-09 16:57:47 +0800734 HPKS_HLEN,
735 HPKS_HLEN_EXT,
736
737 HPKS_DATA,
Andy Green40110e82015-12-14 08:52:03 +0800738
Andy Green200f3852014-10-18 12:23:05 +0800739 /* optional after last header block */
740 HKPS_OPT_DISCARD_PADDING,
Andy Greenecc2e722014-10-09 16:57:47 +0800741};
742
Andy Green2add6342014-10-12 08:38:16 +0800743enum http2_hpack_type {
744 HPKT_INDEXED_HDR_7,
745 HPKT_INDEXED_HDR_6_VALUE_INCR,
746 HPKT_LITERAL_HDR_VALUE_INCR,
747 HPKT_INDEXED_HDR_4_VALUE,
748 HPKT_LITERAL_HDR_VALUE,
749 HPKT_SIZE_5
750};
751
Andy Green200f3852014-10-18 12:23:05 +0800752struct hpack_dt_entry {
753 int token; /* additions that don't map to a token are ignored */
754 int arg_offset;
755 int arg_len;
756};
757
758struct hpack_dynamic_table {
759 struct hpack_dt_entry *entries;
760 char *args;
761 int pos;
762 int next;
763 int num_entries;
764 int args_length;
765};
766
Andy Green024eb6c2014-10-08 12:00:53 +0800767struct _lws_http2_related {
Andy Green40110e82015-12-14 08:52:03 +0800768 /*
Andy Green024eb6c2014-10-08 12:00:53 +0800769 * having this first lets us also re-use all HTTP union code
770 * and in turn, http_mode_related has allocated headers in right
771 * place so we can use the header apis on the wsi directly still
772 */
773 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
774
775 struct http2_settings my_settings;
776 struct http2_settings peer_settings;
Andy Green40110e82015-12-14 08:52:03 +0800777
Andy Green4b85c1d2015-12-04 11:08:32 +0800778 struct lws *parent_wsi;
779 struct lws *next_child_wsi;
Andy Green024eb6c2014-10-08 12:00:53 +0800780
Andy Green200f3852014-10-18 12:23:05 +0800781 struct hpack_dynamic_table *hpack_dyn_table;
Andy Greenaa775fd2015-12-26 08:56:58 +0800782 struct lws *stream_wsi;
783 unsigned char ping_payload[8];
784 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
Andy Green40110e82015-12-14 08:52:03 +0800785
Andy Green024eb6c2014-10-08 12:00:53 +0800786 unsigned int count;
Andy Green024eb6c2014-10-08 12:00:53 +0800787 unsigned int length;
788 unsigned int stream_id;
Andy Greenaa775fd2015-12-26 08:56:58 +0800789 enum http2_hpack_state hpack;
790 enum http2_hpack_type hpack_type;
791 unsigned int header_index;
792 unsigned int hpack_len;
793 unsigned int hpack_e_dep;
794 int tx_credit;
795 unsigned int my_stream_id;
796 unsigned int child_count;
797 int my_priority;
798
Andy Green91b05892014-10-17 08:38:44 +0800799 unsigned int END_STREAM:1;
800 unsigned int END_HEADERS:1;
Andy Green1cea5812014-10-19 07:36:20 +0800801 unsigned int send_END_STREAM:1;
Andy Green7df53c52014-10-22 15:37:28 +0800802 unsigned int GOING_AWAY;
803 unsigned int requested_POLLOUT:1;
Andy Green97ee57f2014-10-29 09:39:08 +0800804 unsigned int waiting_tx_credit:1;
Andy Greenecc2e722014-10-09 16:57:47 +0800805 unsigned int huff:1;
806 unsigned int value:1;
Andy Green40110e82015-12-14 08:52:03 +0800807
Andy Greenaa775fd2015-12-26 08:56:58 +0800808 unsigned short round_robin_POLLOUT;
809 unsigned short count_POLLOUT_children;
810 unsigned short hpack_pos;
811
812 unsigned char type;
813 unsigned char flags;
814 unsigned char frame_state;
815 unsigned char padding;
816 unsigned char hpack_m;
Andy Green024eb6c2014-10-08 12:00:53 +0800817 unsigned char initialized;
Andy Green024eb6c2014-10-08 12:00:53 +0800818};
819
Andy Green7df53c52014-10-22 15:37:28 +0800820#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
Andy Green024eb6c2014-10-08 12:00:53 +0800821
822#endif
823
Andy Green623a98d2013-01-21 11:04:23 +0800824struct _lws_header_related {
Andy Green024eb6c2014-10-08 12:00:53 +0800825 /* MUST be first in struct */
Andy Green16ab3182013-02-10 18:02:31 +0800826 struct allocated_headers *ah;
Andy Greenb1a9e502013-11-10 15:15:21 +0800827 enum uri_path_states ups;
828 enum uri_esc_states ues;
Andy Greende132b92015-12-25 13:48:20 +0800829 short lextable_pos;
830 unsigned short current_token_limit;
Andy Greenb1a9e502013-11-10 15:15:21 +0800831 char esc_stash;
Andy Green3ba035d2015-12-18 15:40:03 +0800832 char post_literal_equal;
Andy Greende132b92015-12-25 13:48:20 +0800833 unsigned char parser_state; /* enum lws_token_indexes */
Andy Green623a98d2013-01-21 11:04:23 +0800834};
835
836struct _lws_websocket_related {
Andy Green54495112013-02-06 21:10:16 +0900837 char *rx_user_buffer;
Andy Greende132b92015-12-25 13:48:20 +0800838 size_t rx_packet_length;
Andy Green2cd30742015-11-02 13:10:33 +0800839 unsigned int rx_user_buffer_head;
Andy Green54806b12015-12-17 17:03:59 +0800840 unsigned char mask_nonce[4];
Andy Green1fb95e82015-12-26 17:20:34 +0800841 /* Also used for close content... control opcode == < 128 */
Andy Green7dbf21e2015-12-28 13:40:54 +0800842 unsigned char ping_payload_buf[128 - 3 + LWS_SEND_BUFFER_PRE_PADDING];
Andy Green1fb95e82015-12-26 17:20:34 +0800843
Andy Greenaa775fd2015-12-26 08:56:58 +0800844 unsigned char ping_payload_len;
Andy Green623a98d2013-01-21 11:04:23 +0800845 unsigned char frame_mask_index;
Andy Green623a98d2013-01-21 11:04:23 +0800846 unsigned char opcode;
Andy Green623a98d2013-01-21 11:04:23 +0800847 unsigned char rsv;
Andy Green1fb95e82015-12-26 17:20:34 +0800848 /* zero if no info, or length including 2-byte close code */
849 unsigned char close_in_ping_buffer_len;
Andy Greende132b92015-12-25 13:48:20 +0800850
851 unsigned int final:1;
Andy Greend91d5e82013-02-10 16:00:47 +0800852 unsigned int frame_is_binary:1;
Andy Greend91d5e82013-02-10 16:00:47 +0800853 unsigned int all_zero_nonce:1;
Andy Greend91d5e82013-02-10 16:00:47 +0800854 unsigned int this_frame_masked:1;
Andy Green1f4267b2013-10-17 08:09:19 +0800855 unsigned int inside_frame:1; /* next write will be more of frame */
856 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
Andy Green40d5abc2015-04-17 20:29:58 +0800857 unsigned int payload_is_close:1; /* process as PONG, but it is close */
Andy Greenba38a7e2015-12-25 13:14:09 +0800858 unsigned int ping_pending_flag:1;
Andy Green977734e2015-12-28 16:51:08 +0800859 unsigned int continuation_possible:1;
Andy Green623a98d2013-01-21 11:04:23 +0800860};
861
Andy Green4b85c1d2015-12-04 11:08:32 +0800862struct lws {
Andy Green623a98d2013-01-21 11:04:23 +0800863
Andy Greenaa775fd2015-12-26 08:56:58 +0800864 /* structs */
865 /* members with mutually exclusive lifetimes are unionized */
866
867 union u {
868 struct _lws_http_mode_related http;
869#ifdef LWS_USE_HTTP2
870 struct _lws_http2_related http2;
871#endif
872 struct _lws_header_related hdr;
873 struct _lws_websocket_related ws;
874 } u;
875
Andy Green623a98d2013-01-21 11:04:23 +0800876 /* lifetime members */
877
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800878#ifdef LWS_USE_LIBEV
Andy Green40110e82015-12-14 08:52:03 +0800879 struct lws_io_watcher w_read;
880 struct lws_io_watcher w_write;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800881#endif /* LWS_USE_LIBEV */
Andy Greende132b92015-12-25 13:48:20 +0800882 time_t pending_timeout_limit;
Andy Greenaa775fd2015-12-26 08:56:58 +0800883
884 /* pointers */
885
Andy Green40110e82015-12-14 08:52:03 +0800886 struct lws_context *context;
Andy Green4b85c1d2015-12-04 11:08:32 +0800887 const struct lws_protocols *protocol;
Andy Greende132b92015-12-25 13:48:20 +0800888 void *user_space;
889 /* rxflow handling */
890 unsigned char *rxflow_buffer;
891 /* truncated send handling */
892 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
Andy Green3182ece2013-01-20 17:08:31 +0800893#ifndef LWS_NO_EXTENSIONS
Andy Greend2ac22c2015-12-11 10:45:35 +0800894 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green6ee372f2012-04-09 15:09:01 +0800895 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green3182ece2013-01-20 17:08:31 +0800896#endif
Andy Greende132b92015-12-25 13:48:20 +0800897#ifdef LWS_OPENSSL_SUPPORT
898 SSL *ssl;
899 BIO *client_bio;
900 struct lws *pending_read_list_prev, *pending_read_list_next;
901#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800902#ifdef LWS_LATENCY
903 unsigned long action_start;
904 unsigned long latency_start;
905#endif
906 /* pointer / int */
Andy Green3b193862015-11-02 13:13:44 +0800907 lws_sockfd_type sock;
Andy Greende132b92015-12-25 13:48:20 +0800908
Andy Greenaa775fd2015-12-26 08:56:58 +0800909 /* ints */
Andy Greende132b92015-12-25 13:48:20 +0800910 enum lws_pending_protocol_send pps;
Andy Greendfb23042013-01-17 12:26:48 +0800911 int position_in_fds_table;
Andy Green024eb6c2014-10-08 12:00:53 +0800912 int rxflow_len;
913 int rxflow_pos;
Andy Green54806b12015-12-17 17:03:59 +0800914 unsigned int trunc_alloc_len; /* size of malloc */
915 unsigned int trunc_offset; /* where we are in terms of spilling */
916 unsigned int trunc_len; /* how much is buffered */
Andy Green2764eba2013-12-09 14:16:17 +0800917
Andy Greende132b92015-12-25 13:48:20 +0800918 unsigned int hdr_parsing_completed:1;
919 unsigned int user_space_externally_allocated:1;
920 unsigned int socket_is_permanently_unusable:1;
921 unsigned int rxflow_change_to:2;
922#ifndef LWS_NO_EXTENSIONS
923 unsigned int extension_data_pending:1;
924#endif
925#ifdef LWS_OPENSSL_SUPPORT
926 unsigned int use_ssl:2;
927 unsigned int upgraded:1;
928#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800929#ifdef _WIN32
930 unsigned int sock_send_blocking:1;
931#endif
Andy Greende132b92015-12-25 13:48:20 +0800932
Andy Greenaa775fd2015-12-26 08:56:58 +0800933 /* chars */
Andy Greende132b92015-12-25 13:48:20 +0800934#ifndef LWS_NO_EXTENSIONS
935 unsigned char count_active_extensions;
936#endif
937 unsigned char ietf_spec_revision;
938 char mode; /* enum connection_mode */
939 char state; /* enum lws_connection_states */
940 char lws_rx_parse_state; /* enum lws_rx_parse_state */
941 char rx_frame_type; /* enum lws_write_protocol */
942 char pending_timeout; /* enum pending_timeout */
Andy Green7c212cc2010-11-08 20:20:42 +0000943};
944
Andy Green3d67f512014-04-03 07:29:50 +0800945LWS_EXTERN int log_level;
946
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800947LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +0800948lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
Andy Green508946c2013-02-12 10:19:08 +0800949
Andy Green34f3dd22014-04-03 07:42:50 +0800950LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +0800951remove_wsi_socket_from_fds(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +0800952LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800953lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
Andy Green34f3dd22014-04-03 07:42:50 +0800954
Andy Greend636e352013-01-29 12:36:17 +0800955#ifndef LWS_LATENCY
Andy Greendc8a3a82015-12-06 09:15:27 +0800956static inline void
957lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
958 int ret, int completion) {
Andy Green40110e82015-12-14 08:52:03 +0800959 do {
960 (void)context; (void)wsi; (void)action; (void)ret;
961 (void)completion;
Andy Greendc8a3a82015-12-06 09:15:27 +0800962 } while (0);
963}
964static inline void
965lws_latency_pre(struct lws_context *context, struct lws *wsi) {
966 do { (void)context; (void)wsi; } while (0);
967}
Andy Greend636e352013-01-29 12:36:17 +0800968#else
969#define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
970extern void
Andy Greendc8a3a82015-12-06 09:15:27 +0800971lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
972 int ret, int completion);
Andy Greend636e352013-01-29 12:36:17 +0800973#endif
974
Andy Greendc8a3a82015-12-06 09:15:27 +0800975LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +0800976lws_set_protocol_write_pending(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +0800977 enum lws_pending_protocol_send pend);
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800978LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800979lws_client_rx_sm(struct lws *wsi, unsigned char c);
Andy Green4739e5c2011-01-22 12:51:57 +0000980
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800981LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +0800982lws_parse(struct lws *wsi, unsigned char c);
Andy Greenb45993c2010-12-18 15:13:50 +0000983
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800984LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +0800985lws_http_action(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +0800986
987LWS_EXTERN int
Andy Greendf736162011-01-18 15:39:02 +0000988lws_b64_selftest(void);
Andy Greenbfb051f2011-02-09 08:49:14 +0000989
Andy Green2cd30742015-11-02 13:10:33 +0800990#if defined(_WIN32) || defined(MBED_OPERATORS)
Andy Green4b85c1d2015-12-04 11:08:32 +0800991LWS_EXTERN struct lws *
Andy Green1fa76852015-12-14 11:17:16 +0800992wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
Andy Green0d338332011-02-12 11:57:43 +0000993
Andy Green40110e82015-12-14 08:52:03 +0800994LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800995insert_wsi(struct lws_context *context, struct lws *wsi);
Bud Davis229bfec2015-01-30 10:13:01 +0800996
997LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800998delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
Bud Davis229bfec2015-01-30 10:13:01 +0800999#else
Andy Green40110e82015-12-14 08:52:03 +08001000#define wsi_from_fd(A,B) A->lws_lookup[B]
Bud Davis229bfec2015-01-30 10:13:01 +08001001#define insert_wsi(A,B) A->lws_lookup[B->sock]=B
1002#define delete_from_fd(A,B) A->lws_lookup[B]=0
1003#endif
1004
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001005LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001006insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
Darin Willitsc19456f2011-02-14 17:52:39 +00001007
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001008LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001009lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
Andy Greend44bf7f2011-03-06 10:29:38 +00001010
Andy Green95a7b5d2011-03-06 10:29:39 +00001011
Andy Green1c6e1422013-02-20 19:11:31 +08001012LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001013lws_service_timeout_check(struct lws *wsi, unsigned int sec);
Andy Greena41314f2011-05-23 10:00:03 +01001014
Andy Green4b85c1d2015-12-04 11:08:32 +08001015LWS_EXTERN struct lws *
Andy Green6b5de702015-12-15 21:15:58 +08001016lws_client_connect_2(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001017
Andy Green4b85c1d2015-12-04 11:08:32 +08001018LWS_EXTERN struct lws *
1019lws_create_new_server_wsi(struct lws_context *context);
Andy Greena41314f2011-05-23 10:00:03 +01001020
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001021LWS_EXTERN char *
Andy Green6b5de702015-12-15 21:15:58 +08001022lws_generate_client_handshake(struct lws *wsi, char *pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001023
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001024LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001025lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
Andy Green91b05892014-10-17 08:38:44 +08001026
Andy Greencdb9bf92014-04-12 10:07:02 +08001027/*
1028 * EXTENSIONS
1029 */
1030
Andy Green3182ece2013-01-20 17:08:31 +08001031#ifndef LWS_NO_EXTENSIONS
Andy Greencdb9bf92014-04-12 10:07:02 +08001032LWS_VISIBLE void
1033lws_context_init_extensions(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001034 struct lws_context *context);
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001035LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001036lws_any_extension_handled(struct lws *wsi,
Andy Green4b85c1d2015-12-04 11:08:32 +08001037 enum lws_extension_callback_reasons r,
Andy Green6ee372f2012-04-09 15:09:01 +08001038 void *v, size_t len);
Andy Greena41314f2011-05-23 10:00:03 +01001039
Andy Green2c24ec02014-04-02 19:45:42 +08001040LWS_EXTERN int
Andy Green54806b12015-12-17 17:03:59 +08001041lws_ext_cb_wsi_active_exts(struct lws *wsi, int reason,
Andy Greendc8a3a82015-12-06 09:15:27 +08001042 void *buf, int len);
Andy Green2c24ec02014-04-02 19:45:42 +08001043LWS_EXTERN int
Andy Green54806b12015-12-17 17:03:59 +08001044lws_ext_cb_all_exts(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001045 struct lws *wsi, int reason,
1046 void *arg, int len);
Andy Green2c24ec02014-04-02 19:45:42 +08001047#else
Andy Green6b5de702015-12-15 21:15:58 +08001048#define lws_any_extension_handled(_a, _b, _c, _d) (0)
Andy Green54806b12015-12-17 17:03:59 +08001049#define lws_ext_cb_wsi_active_exts(_a, _b, _c, _d) (0)
1050#define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
Andy Greenb49a9952014-04-03 10:11:04 +08001051#define lws_issue_raw_ext_access lws_issue_raw
Andy Greencdb9bf92014-04-12 10:07:02 +08001052#define lws_context_init_extensions(_a, _b)
Andy Green3182ece2013-01-20 17:08:31 +08001053#endif
Andy Greena41314f2011-05-23 10:00:03 +01001054
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001055LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001056lws_client_interpret_server_handshake(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001057
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001058LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001059lws_rx_sm(struct lws *wsi, unsigned char c);
Andy Greena41314f2011-05-23 10:00:03 +01001060
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001061LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001062lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
Andy Green09226502011-05-28 10:19:19 +01001063
Andy Green44c11612014-11-08 11:18:47 +08001064LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001065lws_union_transition(struct lws *wsi, enum connection_mode mode);
Andy Green44c11612014-11-08 11:18:47 +08001066
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001067LWS_EXTERN int
Andy Green00c6d152015-12-17 07:54:44 +08001068user_callback_handle_rxflow(callback_function, struct lws *wsi,
1069 enum lws_callback_reasons reason, void *user,
1070 void *in, size_t len);
Andy Green024eb6c2014-10-08 12:00:53 +08001071#ifdef LWS_USE_HTTP2
Andy Green4b85c1d2015-12-04 11:08:32 +08001072LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1073struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
Andy Green024eb6c2014-10-08 12:00:53 +08001074LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001075lws_http2_interpret_settings_payload(struct http2_settings *settings,
1076 unsigned char *buf, int len);
Andy Green024eb6c2014-10-08 12:00:53 +08001077LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1078LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001079lws_http2_parser(struct lws *wsi, unsigned char c);
Andy Greendc8a3a82015-12-06 09:15:27 +08001080LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1081 struct lws *wsi);
1082LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1083 unsigned int sid, unsigned int len,
1084 unsigned char *buf);
Andy Green4b85c1d2015-12-04 11:08:32 +08001085LWS_EXTERN struct lws *
1086lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
Andy Green11c05bf2015-12-16 18:19:08 +08001087LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
Andy Green2add6342014-10-12 08:38:16 +08001088 unsigned char c);
Andy Green917f43a2014-10-12 14:31:47 +08001089LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001090lws_add_http2_header_by_name(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001091 const unsigned char *name,
1092 const unsigned char *value, int length,
1093 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001094LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001095lws_add_http2_header_by_token(struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001096 enum lws_token_indexes token,
Andy Greendc8a3a82015-12-06 09:15:27 +08001097 const unsigned char *value, int length,
1098 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001099LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001100lws_add_http2_header_status(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001101 unsigned int code, unsigned char **p,
Andy Green917f43a2014-10-12 14:31:47 +08001102 unsigned char *end);
Andy Green7df53c52014-10-22 15:37:28 +08001103LWS_EXTERN
Andy Green4b85c1d2015-12-04 11:08:32 +08001104void lws_http2_configure_if_upgraded(struct lws *wsi);
Andy Green7df53c52014-10-22 15:37:28 +08001105#else
1106#define lws_http2_configure_if_upgraded(x)
Andy Green024eb6c2014-10-08 12:00:53 +08001107#endif
Andy Green706961d2013-01-17 16:50:35 +08001108
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001109LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001110lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
Andy Greena690cd02013-02-09 12:25:31 +08001111
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001112LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001113lws_allocate_header_table(struct lws *wsi);
Andy Green16ab3182013-02-10 18:02:31 +08001114
Andrew Canaday37718812014-11-07 11:20:59 +08001115LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001116lws_free_header_table(struct lws *wsi);
Andrew Canaday37718812014-11-07 11:20:59 +08001117
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001118LWS_EXTERN char *
Andy Green4b85c1d2015-12-04 11:08:32 +08001119lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001120
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001121LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001122lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
Andy Greenb5b23192013-02-11 17:13:32 +08001123
Andy Green2af4d5b2013-02-18 16:30:10 +08001124LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001125lws_ensure_user_space(struct lws *wsi);
Andy Green2af4d5b2013-02-18 16:30:10 +08001126
Andy Green158e8042014-04-02 14:25:10 +08001127LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001128lws_change_pollfd(struct lws *wsi, int _and, int _or);
Andy Green91f19d82013-12-21 11:18:34 +08001129
Andy Greenb5b23192013-02-11 17:13:32 +08001130#ifndef LWS_NO_SERVER
Andy Greene38031a2014-04-03 08:24:29 +08001131int lws_context_init_server(struct lws_context_creation_info *info,
Andy Greenaa775fd2015-12-26 08:56:58 +08001132 struct lws_context *context);
Andy Greend7340c12014-04-10 14:08:10 +08001133LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001134handshake_0405(struct lws_context *context, struct lws *wsi);
1135LWS_EXTERN int
1136lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
Andy Greencdb9bf92014-04-12 10:07:02 +08001137LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001138lws_server_get_canonical_hostname(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001139 struct lws_context_creation_info *info);
Andy Greene38031a2014-04-03 08:24:29 +08001140#else
1141#define lws_context_init_server(_a, _b) (0)
Andy Green3ef579b2015-12-04 09:23:56 +08001142#define lws_interpret_incoming_packet(_a, _b, _c) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001143#define lws_server_get_canonical_hostname(_a, _b)
Andy Greenb5b23192013-02-11 17:13:32 +08001144#endif
1145
1146#ifndef LWS_NO_DAEMONIZE
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001147LWS_EXTERN int get_daemonize_pid();
Andy Greencdb9bf92014-04-12 10:07:02 +08001148#else
1149#define get_daemonize_pid() (0)
Andy Greenb5b23192013-02-11 17:13:32 +08001150#endif
1151
Andy Green2cd30742015-11-02 13:10:33 +08001152#if !defined(MBED_OPERATORS)
Andy Greendc8a3a82015-12-06 09:15:27 +08001153LWS_EXTERN int
1154interface_to_sa(struct lws_context *context, const char *ifname,
1155 struct sockaddr_in *addr, size_t addrlen);
Andy Green2cd30742015-11-02 13:10:33 +08001156#endif
Andy Greenb25b85f2014-04-03 23:34:09 +08001157LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1158
Andy Green1a308e42014-04-08 07:26:30 +01001159enum lws_ssl_capable_status {
1160 LWS_SSL_CAPABLE_ERROR = -1,
1161 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1162};
1163
Darin Willitsc19456f2011-02-14 17:52:39 +00001164#ifndef LWS_OPENSSL_SUPPORT
Andy Greencdb9bf92014-04-12 10:07:02 +08001165#define LWS_SSL_ENABLED(context) (0)
Andy Greenc57037a2014-04-03 10:17:00 +08001166#define lws_context_init_server_ssl(_a, _b) (0)
1167#define lws_ssl_destroy(_a)
Andy Green2eedea92014-04-03 14:33:48 +08001168#define lws_context_init_http2_ssl(_a)
Andy Green02138122014-04-06 06:26:35 +01001169#define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1170#define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001171#define lws_ssl_pending lws_ssl_pending_no_ssl
Andy Green6b5de702015-12-15 21:15:58 +08001172#define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001173#define lws_ssl_close(_a) (0)
1174#define lws_ssl_context_destroy(_a)
Andy Green6b5de702015-12-15 21:15:58 +08001175#define lws_ssl_remove_wsi_from_buffered_list(_a)
Andy Greenb5b23192013-02-11 17:13:32 +08001176#else
Andy Greencdb9bf92014-04-12 10:07:02 +08001177#define LWS_SSL_ENABLED(context) (context->use_ssl)
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001178LWS_EXTERN int openssl_websocket_private_data_index;
Andy Green02138122014-04-06 06:26:35 +01001179LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001180lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
Andy Green02138122014-04-06 06:26:35 +01001181LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001182lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
Andy Greencdb9bf92014-04-12 10:07:02 +08001183LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001184lws_ssl_pending(struct lws *wsi);
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001185LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001186lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
1187 lws_sockfd_type accept_fd,
Andy Greendc8a3a82015-12-06 09:15:27 +08001188 struct lws_pollfd *pollfd);
Andy Greencdb9bf92014-04-12 10:07:02 +08001189LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001190lws_ssl_close(struct lws *wsi);
Andy Greencdb9bf92014-04-12 10:07:02 +08001191LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001192lws_ssl_context_destroy(struct lws_context *context);
Andy Green52815602015-01-29 08:36:18 +08001193LWS_VISIBLE void
Andy Green6b5de702015-12-15 21:15:58 +08001194lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
Andy Greenc57037a2014-04-03 10:17:00 +08001195#ifndef LWS_NO_SERVER
1196LWS_EXTERN int
1197lws_context_init_server_ssl(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001198 struct lws_context *context);
Andy Greenc57037a2014-04-03 10:17:00 +08001199#else
1200#define lws_context_init_server_ssl(_a, _b) (0)
1201#endif
1202LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001203lws_ssl_destroy(struct lws_context *context);
Andy Green2eedea92014-04-03 14:33:48 +08001204
1205/* HTTP2-related */
1206
1207#ifdef LWS_USE_HTTP2
1208LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001209lws_context_init_http2_ssl(struct lws_context *context);
Andy Green2eedea92014-04-03 14:33:48 +08001210#else
1211#define lws_context_init_http2_ssl(_a)
1212#endif
Darin Willitsc19456f2011-02-14 17:52:39 +00001213#endif
Andy Greena654fc02014-04-03 07:16:40 +08001214
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001215LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001216lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001217
1218LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001219lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001220
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001221LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001222lws_ssl_pending_no_ssl(struct lws *wsi);
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001223
Andy Greena654fc02014-04-03 07:16:40 +08001224#ifndef LWS_NO_CLIENT
Andy Greendc8a3a82015-12-06 09:15:27 +08001225LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1226 struct lws *wsi,
1227 struct lws_pollfd *pollfd);
Andy Greenc57037a2014-04-03 10:17:00 +08001228#ifdef LWS_OPENSSL_SUPPORT
Andy Greendc8a3a82015-12-06 09:15:27 +08001229LWS_EXTERN int
1230lws_context_init_client_ssl(struct lws_context_creation_info *info,
Andy Green4b85c1d2015-12-04 11:08:32 +08001231 struct lws_context *context);
Andy Greene38031a2014-04-03 08:24:29 +08001232#else
Andy Greenc57037a2014-04-03 10:17:00 +08001233 #define lws_context_init_client_ssl(_a, _b) (0)
1234#endif
Andy Greendc8a3a82015-12-06 09:15:27 +08001235LWS_EXTERN int
1236lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1237LWS_EXTERN void
1238lws_decode_ssl_error(void);
Andy Greenc57037a2014-04-03 10:17:00 +08001239#else
1240#define lws_context_init_client_ssl(_a, _b) (0)
Andy Greenaad2eac2014-04-03 09:03:37 +08001241#define lws_handshake_client(_a, _b, _c) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001242#endif
Andy Green44e0b082015-12-28 14:24:49 +08001243
1244LWS_EXTERN int
1245_lws_rx_flow_control(struct lws *wsi);
1246
Andy Greena654fc02014-04-03 07:16:40 +08001247#ifndef LWS_NO_SERVER
Andy Greendc8a3a82015-12-06 09:15:27 +08001248LWS_EXTERN int
1249lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1250 struct lws_pollfd *pollfd);
1251LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001252lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
Andy Greenb3d21f12015-12-25 09:12:08 +08001253LWS_EXTERN int
1254_lws_server_listen_accept_flow_control(struct lws_context *context, int on);
Andy Greend99476b2014-04-03 08:40:05 +08001255#else
Andy Greenb49a9952014-04-03 10:11:04 +08001256#define lws_server_socket_service(_a, _b, _c) (0)
Andy Green11c05bf2015-12-16 18:19:08 +08001257#define lws_handshake_server(_a, _b, _c) (0)
Andy Greenb3d21f12015-12-25 09:12:08 +08001258#define _lws_server_listen_accept_flow_control(a, b) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001259#endif
Andy Green40110e82015-12-14 08:52:03 +08001260
Andy Greendc8a3a82015-12-06 09:15:27 +08001261LWS_EXTERN int
1262lws_get_addresses(struct lws_context *context, void *ads, char *name,
1263 int name_len, char *rip, int rip_len);
Andy Greena654fc02014-04-03 07:16:40 +08001264
1265/*
Alejandro Merycdc97172014-12-04 23:15:27 +01001266 * custom allocator
1267 */
1268LWS_EXTERN void*
1269lws_realloc(void *ptr, size_t size);
1270
1271LWS_EXTERN void*
1272lws_zalloc(size_t size);
1273
1274#define lws_malloc(S) lws_realloc(NULL, S)
1275#define lws_free(P) lws_realloc(P, 0)
Andy Green54806b12015-12-17 17:03:59 +08001276#define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
Alejandro Merycdc97172014-12-04 23:15:27 +01001277
Andy Greendc8a3a82015-12-06 09:15:27 +08001278/* lws_plat_ */
Andy Greena654fc02014-04-03 07:16:40 +08001279LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001280lws_plat_delete_socket_from_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001281 struct lws *wsi, int m);
Andy Greena654fc02014-04-03 07:16:40 +08001282LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001283lws_plat_insert_socket_into_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001284 struct lws *wsi);
Andy Greena654fc02014-04-03 07:16:40 +08001285LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001286lws_plat_service_periodic(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001287
1288LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001289lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1290 struct lws_pollfd *pfd);
Andy Greena654fc02014-04-03 07:16:40 +08001291LWS_EXTERN int
1292lws_plat_context_early_init(void);
1293LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001294lws_plat_context_early_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001295LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001296lws_plat_context_late_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001297LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001298lws_poll_listen_fd(struct lws_pollfd *fd);
Andy Greena654fc02014-04-03 07:16:40 +08001299LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001300lws_plat_service(struct lws_context *context, int timeout_ms);
Andy Greena654fc02014-04-03 07:16:40 +08001301LWS_EXTERN int
Andy Green4386e362015-12-10 07:14:16 +08001302lws_plat_init(struct lws_context *context,
1303 struct lws_context_creation_info *info);
Andy Greena654fc02014-04-03 07:16:40 +08001304LWS_EXTERN void
1305lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1306LWS_EXTERN unsigned long long
1307time_in_microseconds(void);
1308LWS_EXTERN const char *
1309lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
Andy Green8c0d3c02015-11-02 20:34:12 +08001310
1311#ifdef __cplusplus
1312};
1313#endif