blob: 5f118ad765abe818ee4d941ca908ea9cbb03bfab [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 Green8c1f6022016-01-26 20:56:56 +08004 * Copyright (C) 2010 - 2016 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
Andy Green6a8099b2016-02-21 21:25:48 +080025
26#if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK)
27#define _GNU_SOURCE
28#endif
29
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020030#ifdef LWS_HAVE_SYS_TYPES_H
Andy Greene40aa9b2014-04-02 21:02:54 +080031#include <sys/types.h>
Joakim Soderberg91de9332013-02-06 15:30:33 +090032#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090033
Andy Green7c212cc2010-11-08 20:20:42 +000034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Patrick Ganstererb13eed42014-03-30 10:19:23 +020037#include <time.h>
Andy Green7c212cc2010-11-08 20:20:42 +000038#include <ctype.h>
Peter Young26757a72013-01-17 10:08:16 +080039#include <limits.h>
Peter Hinz56885f32011-03-02 22:03:47 +000040#include <stdarg.h>
Andy Green112f9802015-12-04 07:22:44 +080041#include <assert.h>
Andy Green8c1f6022016-01-26 20:56:56 +080042#if LWS_MAX_SMP > 1
43#include <pthread.h>
44#endif
Peter Hinz56885f32011-03-02 22:03:47 +000045
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020046#ifdef LWS_HAVE_SYS_STAT_H
Peter Hinz56885f32011-03-02 22:03:47 +000047#include <sys/stat.h>
Patrick Gansterere5720a32014-02-28 00:57:19 +010048#endif
Peter Hinz56885f32011-03-02 22:03:47 +000049
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010050#if defined(WIN32) || defined(_WIN32)
Stephan Eberleb820e2c2015-10-23 08:10:55 +020051#if (WINVER < 0x0501)
52#undef WINVER
53#undef _WIN32_WINNT
54#define WINVER 0x0501
55#define _WIN32_WINNT WINVER
56#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090057#define LWS_NO_DAEMONIZE
Patrick Gansterer2dbd8372014-02-28 12:37:52 +010058#define LWS_ERRNO WSAGetLastError()
59#define LWS_EAGAIN WSAEWOULDBLOCK
60#define LWS_EALREADY WSAEALREADY
61#define LWS_EINPROGRESS WSAEINPROGRESS
62#define LWS_EINTR WSAEINTR
63#define LWS_EISCONN WSAEISCONN
64#define LWS_EWOULDBLOCK WSAEWOULDBLOCK
Patrick Ganstererb47f87b2014-03-30 09:18:05 +020065#define LWS_POLLHUP (FD_CLOSE)
Patrick Gansterer0fc37b62014-03-28 15:44:56 +010066#define LWS_POLLIN (FD_READ | FD_ACCEPT)
67#define LWS_POLLOUT (FD_WRITE)
Patrick Gansterer73882e42014-03-29 08:25:58 +010068#define MSG_NOSIGNAL 0
69#define SHUT_RDWR SD_BOTH
70#define SOL_TCP IPPROTO_TCP
Andy Green8c1f6022016-01-26 20:56:56 +080071#define SHUT_WR SD_SEND
Joakim Soderberg4c531232013-02-06 15:26:58 +090072
Andy Green158e8042014-04-02 14:25:10 +080073#define compatible_close(fd) closesocket(fd)
Andy Greenaa775fd2015-12-26 08:56:58 +080074#define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
Andy Greenc53f7ca2015-11-14 07:35:27 +080075#define lws_socket_is_valid(x) (!!x)
Andy Green40110e82015-12-14 08:52:03 +080076#define LWS_SOCK_INVALID 0
Peter Hinz56885f32011-03-02 22:03:47 +000077#include <winsock2.h>
Andy Greeneee0d8a2015-12-17 15:15:12 +080078#include <ws2tcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000079#include <windows.h>
Joakim Soderbergd2f5b192014-04-07 11:28:08 +020080#include <tchar.h>
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020081#ifdef LWS_HAVE_IN6ADDR_H
Andy Green0f58db32014-04-12 11:10:35 +080082#include <in6addr.h>
83#endif
Joakim Soderbergd2f5b192014-04-07 11:28:08 +020084#include <mstcpip.h>
85
86#ifndef __func__
87#define __func__ __FUNCTION__
88#endif
89
Patrick Gansterer6bb4b622014-04-15 18:39:26 +020090#ifdef _WIN32_WCE
91#define vsnprintf _vsnprintf
Andy Green7d22c292016-03-04 10:53:51 +080092#else
93#ifdef LWS_HAVE__VSNPRINTF
94#define vsnprintf _vsnprintf
95#endif
96#endif
97
98#ifdef LWS_HAVE__SNPRINTF
99#define snprintf _snprintf
Patrick Gansterer6bb4b622014-04-15 18:39:26 +0200100#endif
101
Andy Green158e8042014-04-02 14:25:10 +0800102#else /* not windows --> */
Andy Green8c0d3c02015-11-02 20:34:12 +0800103
Patrick Ganstererb13eed42014-03-30 10:19:23 +0200104#include <fcntl.h>
Patrick Ganstererb13eed42014-03-30 10:19:23 +0200105#include <strings.h>
106#include <unistd.h>
Andy Greene77ddd82010-11-13 10:03:47 +0000107#include <sys/types.h>
Andy Green5f2a8152015-11-02 08:21:08 +0800108#ifndef MBED_OPERATORS
Andy Green8c0d3c02015-11-02 20:34:12 +0800109#ifndef __cplusplus
110#include <errno.h>
111#endif
Andy Green5f2a8152015-11-02 08:21:08 +0800112#include <netdb.h>
113#include <signal.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000114#include <sys/socket.h>
Andy Greene40aa9b2014-04-02 21:02:54 +0800115#ifdef LWS_BUILTIN_GETIFADDRS
116 #include <getifaddrs.h>
117#else
118 #include <ifaddrs.h>
119#endif
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800120#if defined (__ANDROID__)
121#include <syslog.h>
122#else
Andy Greene40aa9b2014-04-02 21:02:54 +0800123#include <sys/syslog.h>
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800124#endif
Andy Greene40aa9b2014-04-02 21:02:54 +0800125#include <sys/un.h>
126#include <sys/socket.h>
127#include <netdb.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000128#include <netinet/in.h>
Andy Green6c939552011-03-08 08:56:57 +0000129#include <netinet/tcp.h>
Andy Greenb45993c2010-12-18 15:13:50 +0000130#include <arpa/inet.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000131#include <poll.h>
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800132#ifdef LWS_USE_LIBEV
Andy Green86ed65f2016-02-14 09:27:41 +0800133#include <ev.h>
134#endif
135#ifdef LWS_USE_LIBUV
Alex Hultmana43c2ac2016-02-01 08:31:54 +0800136#include <uv.h>
Andy Green86ed65f2016-02-14 09:27:41 +0800137#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000138#include <sys/mman.h>
Andy Green5f2a8152015-11-02 08:21:08 +0800139
140#endif /* MBED */
141
142#ifndef LWS_NO_FORK
143#ifdef LWS_HAVE_SYS_PRCTL_H
144#include <sys/prctl.h>
145#endif
146#endif
147
Andy Green038d5822011-02-14 20:58:26 +0000148#include <sys/time.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000149
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100150#define LWS_ERRNO errno
151#define LWS_EAGAIN EAGAIN
152#define LWS_EALREADY EALREADY
153#define LWS_EINPROGRESS EINPROGRESS
154#define LWS_EINTR EINTR
155#define LWS_EISCONN EISCONN
156#define LWS_EWOULDBLOCK EWOULDBLOCK
Patrick Ganstererb47f87b2014-03-30 09:18:05 +0200157#define LWS_POLLHUP (POLLHUP|POLLERR)
158#define LWS_POLLIN (POLLIN)
159#define LWS_POLLOUT (POLLOUT)
Andy Green158e8042014-04-02 14:25:10 +0800160#define compatible_close(fd) close(fd)
Andy Green158e8042014-04-02 14:25:10 +0800161#define lws_set_blocking_send(wsi)
Andy Green2cd30742015-11-02 13:10:33 +0800162
163#ifdef MBED_OPERATORS
164#define lws_socket_is_valid(x) ((x) != NULL)
165#define LWS_SOCK_INVALID (NULL)
166#else
Andy Greenc53f7ca2015-11-14 07:35:27 +0800167#define lws_socket_is_valid(x) (x >= 0)
168#define LWS_SOCK_INVALID (-1)
Peter Hinz56885f32011-03-02 22:03:47 +0000169#endif
Andy Green2cd30742015-11-02 13:10:33 +0800170#endif
Peter Hinz56885f32011-03-02 22:03:47 +0000171
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200172#ifndef LWS_HAVE_BZERO
Andy Greene5ea1f92014-11-18 18:25:24 +0800173#ifndef bzero
Patrick Gansterer4a837272014-02-28 13:17:49 +0100174#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
175#endif
Andy Greene5ea1f92014-11-18 18:25:24 +0800176#endif
Patrick Gansterer4a837272014-02-28 13:17:49 +0100177
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200178#ifndef LWS_HAVE_STRERROR
Patrick Gansterer9d614912014-02-28 00:59:53 +0100179#define strerror(x) ""
180#endif
181
Andy Green7c212cc2010-11-08 20:20:42 +0000182#ifdef LWS_OPENSSL_SUPPORT
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200183#ifdef USE_WOLFSSL
ABruines80a70682015-08-09 22:56:32 +0200184#ifdef USE_OLD_CYASSL
185#include <cyassl/openssl/ssl.h>
186#include <cyassl/error-ssl.h>
187#else
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200188#include <wolfssl/openssl/ssl.h>
189#include <wolfssl/error-ssl.h>
ABruines80a70682015-08-09 22:56:32 +0200190#endif /* not USE_OLD_CYASSL */
Andy Green23c5f2e2013-02-06 15:43:00 +0900191#else
Andy Green7c212cc2010-11-08 20:20:42 +0000192#include <openssl/ssl.h>
193#include <openssl/evp.h>
194#include <openssl/err.h>
Andy Green70dfebd2010-12-20 09:35:03 +0000195#include <openssl/md5.h>
Andy Greene2522172011-01-18 17:14:03 +0000196#include <openssl/sha.h>
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200197#endif /* not USE_WOLFSSL */
Darin Willitsdb9ba422011-02-14 20:56:24 +0000198#endif
199
Andy Green7c212cc2010-11-08 20:20:42 +0000200#include "libwebsockets.h"
201
Andy Green8c0d3c02015-11-02 20:34:12 +0800202#if defined(MBED_OPERATORS)
203#undef compatible_close
204#define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
Andy Green11f27342015-11-08 12:10:26 +0800205#ifndef BIG_ENDIAN
206#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
207#endif
208#ifndef LITTLE_ENDIAN
209#define LITTLE_ENDIAN 1234
210#endif
211#ifndef BYTE_ORDER
212#define BYTE_ORDER LITTLE_ENDIAN
213#endif
Andy Green8c0d3c02015-11-02 20:34:12 +0800214#endif
215
Andy Green158e8042014-04-02 14:25:10 +0800216#if defined(WIN32) || defined(_WIN32)
217
218#ifndef BIG_ENDIAN
219#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
220#endif
221#ifndef LITTLE_ENDIAN
222#define LITTLE_ENDIAN 1234
223#endif
224#ifndef BYTE_ORDER
225#define BYTE_ORDER LITTLE_ENDIAN
226#endif
Andy Green11f27342015-11-08 12:10:26 +0800227#ifndef u_int64_t
Andy Green158e8042014-04-02 14:25:10 +0800228typedef unsigned __int64 u_int64_t;
Andy Green11f27342015-11-08 12:10:26 +0800229#endif
Andy Green158e8042014-04-02 14:25:10 +0800230
231#undef __P
232#ifndef __P
233#if __STDC__
234#define __P(protos) protos
235#else
236#define __P(protos) ()
237#endif
238#endif
239
240#else
241
242#include <sys/stat.h>
243#include <sys/cdefs.h>
244#include <sys/time.h>
245
246#if defined(__APPLE__)
247#include <machine/endian.h>
248#elif defined(__FreeBSD__)
249#include <sys/endian.h>
250#elif defined(__linux__)
251#include <endian.h>
252#endif
253
Andy Green8c0d3c02015-11-02 20:34:12 +0800254#ifdef __cplusplus
255extern "C" {
256#endif
Alejandro Meryead8afe2014-12-07 03:36:11 +0100257#include <stddef.h>
258
259#ifndef container_of
260#define container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
261#endif
262
emironova49d0842014-09-16 14:05:13 +0400263#if defined(__QNX__)
264 #include <gulliver.h>
265 #if defined(__LITTLEENDIAN__)
266 #define BYTE_ORDER __LITTLEENDIAN__
267 #define LITTLE_ENDIAN __LITTLEENDIAN__
268 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
269 #endif
270 #if defined(__BIGENDIAN__)
271 #define BYTE_ORDER __BIGENDIAN__
272 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
273 #define BIG_ENDIAN __BIGENDIAN__
274 #endif
275#endif
276
Andy Green158e8042014-04-02 14:25:10 +0800277#if !defined(BYTE_ORDER)
278# define BYTE_ORDER __BYTE_ORDER
279#endif
280#if !defined(LITTLE_ENDIAN)
281# define LITTLE_ENDIAN __LITTLE_ENDIAN
282#endif
283#if !defined(BIG_ENDIAN)
284# define BIG_ENDIAN __BIG_ENDIAN
285#endif
286
287#endif
288
Darin Willitsc19456f2011-02-14 17:52:39 +0000289/*
290 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
291 * but happily have something equivalent in the SO_NOSIGPIPE flag.
292 */
293#ifdef __APPLE__
Andy Green6ee372f2012-04-09 15:09:01 +0800294#define MSG_NOSIGNAL SO_NOSIGPIPE
Darin Willitsc19456f2011-02-14 17:52:39 +0000295#endif
296
Bud Davis229bfec2015-01-30 10:13:01 +0800297#ifdef _WIN32
298#ifndef FD_HASHTABLE_MODULUS
299#define FD_HASHTABLE_MODULUS 32
300#endif
301#endif
302
Andy Green8c1f6022016-01-26 20:56:56 +0800303#ifndef LWS_DEF_HEADER_LEN
304#define LWS_DEF_HEADER_LEN 1024
Andy Greenc0d6b632013-01-12 23:42:17 +0800305#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800306#ifndef LWS_DEF_HEADER_POOL
307#define LWS_DEF_HEADER_POOL 16
Andy Green3df58002015-12-25 12:44:12 +0800308#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800309#ifndef LWS_MAX_PROTOCOLS
Andy Greend91d5e82013-02-10 16:00:47 +0800310#define LWS_MAX_PROTOCOLS 5
Andy Greenc0d6b632013-01-12 23:42:17 +0800311#endif
312#ifndef LWS_MAX_EXTENSIONS_ACTIVE
Andy Greend738f842016-01-19 04:32:14 +0800313#define LWS_MAX_EXTENSIONS_ACTIVE 2
Andy Greenc0d6b632013-01-12 23:42:17 +0800314#endif
Andy Green67112662016-01-11 11:34:01 +0800315#ifndef LWS_MAX_EXT_OFFERS
316#define LWS_MAX_EXT_OFFERS 8
317#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800318#ifndef SPEC_LATEST_SUPPORTED
Andy Greend85cb202011-09-25 09:32:54 +0100319#define SPEC_LATEST_SUPPORTED 13
Andy Greenc0d6b632013-01-12 23:42:17 +0800320#endif
321#ifndef AWAITING_TIMEOUT
Andy Green8c1f6022016-01-26 20:56:56 +0800322#define AWAITING_TIMEOUT 20
Andy Greenc0d6b632013-01-12 23:42:17 +0800323#endif
324#ifndef CIPHERS_LIST_STRING
David Galeanof177f2a2013-01-10 10:15:19 +0800325#define CIPHERS_LIST_STRING "DEFAULT"
Andy Greenc0d6b632013-01-12 23:42:17 +0800326#endif
Andy Greena824d182013-01-15 20:52:29 +0800327#ifndef LWS_SOMAXCONN
328#define LWS_SOMAXCONN SOMAXCONN
329#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000330
Andy Greene2522172011-01-18 17:14:03 +0000331#define MAX_WEBSOCKET_04_KEY_LEN 128
Andy Green54495112013-02-06 21:10:16 +0900332#define LWS_MAX_SOCKET_IO_BUF 4096
Andy Greenc0d6b632013-01-12 23:42:17 +0800333
334#ifndef SYSTEM_RANDOM_FILEPATH
Andy Green4739e5c2011-01-22 12:51:57 +0000335#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
Andy Greenc0d6b632013-01-12 23:42:17 +0800336#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800337
Andy Green5fd55cd2011-04-23 10:54:53 +0100338enum lws_websocket_opcodes_07 {
Andy Green54806b12015-12-17 17:03:59 +0800339 LWSWSOPC_CONTINUATION = 0,
340 LWSWSOPC_TEXT_FRAME = 1,
341 LWSWSOPC_BINARY_FRAME = 2,
Andy Greena41314f2011-05-23 10:00:03 +0100342
Andy Green54806b12015-12-17 17:03:59 +0800343 LWSWSOPC_NOSPEC__MUX = 7,
Andy Greena41314f2011-05-23 10:00:03 +0100344
345 /* control extensions 8+ */
346
Andy Green54806b12015-12-17 17:03:59 +0800347 LWSWSOPC_CLOSE = 8,
348 LWSWSOPC_PING = 9,
349 LWSWSOPC_PONG = 0xa,
Andy Green5fd55cd2011-04-23 10:54:53 +0100350};
351
Andy Greena41314f2011-05-23 10:00:03 +0100352
Andy Green7c212cc2010-11-08 20:20:42 +0000353enum lws_connection_states {
Andy Green54806b12015-12-17 17:03:59 +0800354 LWSS_HTTP,
355 LWSS_HTTP_ISSUING_FILE,
356 LWSS_HTTP_HEADERS,
357 LWSS_HTTP_BODY,
358 LWSS_DEAD_SOCKET,
359 LWSS_ESTABLISHED,
Andy Greena661ee52016-02-29 13:18:30 +0800360 LWSS_CLIENT_HTTP_ESTABLISHED,
Andy Green54806b12015-12-17 17:03:59 +0800361 LWSS_CLIENT_UNCONNECTED,
362 LWSS_RETURNED_CLOSE_ALREADY,
363 LWSS_AWAITING_CLOSE_ACK,
364 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
Andy Green8c1f6022016-01-26 20:56:56 +0800365 LWSS_SHUTDOWN,
Andy Green40110e82015-12-14 08:52:03 +0800366
Andy Green54806b12015-12-17 17:03:59 +0800367 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
368 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
369 LWSS_HTTP2_ESTABLISHED,
Andy Green6a8099b2016-02-21 21:25:48 +0800370
Andy Green2d8d35a2016-02-29 14:19:16 +0800371 LWSS_CGI,
Andy Green7c212cc2010-11-08 20:20:42 +0000372};
373
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400374enum http_version {
375 HTTP_VERSION_1_0,
376 HTTP_VERSION_1_1,
377};
378
379enum http_connection_type {
380 HTTP_CONNECTION_CLOSE,
381 HTTP_CONNECTION_KEEP_ALIVE
382};
383
Andy Green024eb6c2014-10-08 12:00:53 +0800384enum lws_pending_protocol_send {
385 LWS_PPS_NONE,
386 LWS_PPS_HTTP2_MY_SETTINGS,
387 LWS_PPS_HTTP2_ACK_SETTINGS,
Andy Greenbbbf07a2014-10-27 16:46:44 +0800388 LWS_PPS_HTTP2_PONG,
Andy Green024eb6c2014-10-08 12:00:53 +0800389};
390
Andy Green7c212cc2010-11-08 20:20:42 +0000391enum lws_rx_parse_state {
392 LWS_RXPS_NEW,
Andy Greene77ddd82010-11-13 10:03:47 +0000393
Andy Green67112662016-01-11 11:34:01 +0800394 LWS_RXPS_04_mask_1,
395 LWS_RXPS_04_mask_2,
396 LWS_RXPS_04_mask_3,
Andy Green3e5eb782011-01-18 18:14:26 +0000397
398 LWS_RXPS_04_FRAME_HDR_1,
Andy Green38e57bb2011-01-19 12:20:27 +0000399 LWS_RXPS_04_FRAME_HDR_LEN,
400 LWS_RXPS_04_FRAME_HDR_LEN16_2,
401 LWS_RXPS_04_FRAME_HDR_LEN16_1,
402 LWS_RXPS_04_FRAME_HDR_LEN64_8,
403 LWS_RXPS_04_FRAME_HDR_LEN64_7,
404 LWS_RXPS_04_FRAME_HDR_LEN64_6,
405 LWS_RXPS_04_FRAME_HDR_LEN64_5,
406 LWS_RXPS_04_FRAME_HDR_LEN64_4,
407 LWS_RXPS_04_FRAME_HDR_LEN64_3,
408 LWS_RXPS_04_FRAME_HDR_LEN64_2,
409 LWS_RXPS_04_FRAME_HDR_LEN64_1,
Andy Green3e5eb782011-01-18 18:14:26 +0000410
Andy Green283d0a22011-04-24 05:46:23 +0100411 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
412 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
413 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
414 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
415
Andy Green7c212cc2010-11-08 20:20:42 +0000416 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
417};
418
419
Andy Green0d338332011-02-12 11:57:43 +0000420enum connection_mode {
Andy Green54806b12015-12-17 17:03:59 +0800421 LWSCM_HTTP_SERVING,
Andy Greena661ee52016-02-29 13:18:30 +0800422 LWSCM_HTTP_CLIENT, /* we are client to someone else's server */
Andy Green54806b12015-12-17 17:03:59 +0800423 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
Andy Greena661ee52016-02-29 13:18:30 +0800424 LWSCM_HTTP_CLIENT_ACCEPTED, /* actual HTTP service going on */
Andy Green54806b12015-12-17 17:03:59 +0800425 LWSCM_PRE_WS_SERVING_ACCEPT,
Andy Greend280b6e2013-01-15 13:40:23 +0800426
Andy Green54806b12015-12-17 17:03:59 +0800427 LWSCM_WS_SERVING,
428 LWSCM_WS_CLIENT,
Andy Green40110e82015-12-14 08:52:03 +0800429
Andy Green54806b12015-12-17 17:03:59 +0800430 LWSCM_HTTP2_SERVING,
Andy Green0d338332011-02-12 11:57:43 +0000431
Andy Greene2160712013-01-28 12:19:10 +0800432 /* transient, ssl delay hiding */
Andy Green54806b12015-12-17 17:03:59 +0800433 LWSCM_SSL_ACK_PENDING,
Andy Green8c1f6022016-01-26 20:56:56 +0800434 LWSCM_SSL_INIT,
Andy Greene2160712013-01-28 12:19:10 +0800435
Andy Greenbe93fef2011-02-14 20:25:43 +0000436 /* transient modes */
Andy Green54806b12015-12-17 17:03:59 +0800437 LWSCM_WSCL_WAITING_CONNECT,
438 LWSCM_WSCL_WAITING_PROXY_REPLY,
439 LWSCM_WSCL_ISSUE_HANDSHAKE,
440 LWSCM_WSCL_ISSUE_HANDSHAKE2,
441 LWSCM_WSCL_WAITING_SSL,
442 LWSCM_WSCL_WAITING_SERVER_REPLY,
443 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
444 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
Andy Greenbe93fef2011-02-14 20:25:43 +0000445
Andy Green0d338332011-02-12 11:57:43 +0000446 /* special internal types */
Andy Green54806b12015-12-17 17:03:59 +0800447 LWSCM_SERVER_LISTENER,
Andy Green6a8099b2016-02-21 21:25:48 +0800448 LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
Andy Green0d338332011-02-12 11:57:43 +0000449};
450
Andy Greenca0a1292013-03-16 11:24:23 +0800451enum {
452 LWS_RXFLOW_ALLOW = (1 << 0),
453 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
454};
455
Andy Green1fb95e82015-12-26 17:20:34 +0800456/* this is not usable directly by user code any more, lws_close_reason() */
457#define LWS_WRITE_CLOSE 4
458
Andy Green4b85c1d2015-12-04 11:08:32 +0800459struct lws_protocols;
460struct lws;
Andy Greene92cd172011-01-19 13:11:55 +0000461
Andy Green86ed65f2016-02-14 09:27:41 +0800462#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
463
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800464struct lws_io_watcher {
Andy Green86ed65f2016-02-14 09:27:41 +0800465#ifdef LWS_USE_LIBEV
466 ev_io ev_watcher;
467#endif
468#ifdef LWS_USE_LIBUV
469 uv_poll_t uv_watcher;
470#endif
471 struct lws_context *context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800472};
473
474struct lws_signal_watcher {
Andy Green86ed65f2016-02-14 09:27:41 +0800475#ifdef LWS_USE_LIBEV
476 ev_signal ev_watcher;
477#endif
478#ifdef LWS_USE_LIBUV
479 uv_signal_t uv_watcher;
480#endif
481 struct lws_context *context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800482};
Andy Green86ed65f2016-02-14 09:27:41 +0800483#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800484
Bud Davis229bfec2015-01-30 10:13:01 +0800485#ifdef _WIN32
486#define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
Andy Green3ef579b2015-12-04 09:23:56 +0800487struct lws_fd_hashtable {
Andy Green4b85c1d2015-12-04 11:08:32 +0800488 struct lws **wsi;
Bud Davis229bfec2015-01-30 10:13:01 +0800489 int length;
490};
491#endif
492
Andy Green3df58002015-12-25 12:44:12 +0800493/*
494 * This is totally opaque to code using the library. It's exported as a
495 * forward-reference pointer-only declaration; the user can use the pointer with
496 * other APIs to get information out of it.
497 */
498
499struct lws_fragments {
500 unsigned short offset;
501 unsigned short len;
502 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
503};
504
505/*
506 * these are assigned from a pool held in the context.
507 * Both client and server mode uses them for http header analysis
508 */
509
510struct allocated_headers {
Andy Green2c218e72016-02-15 12:37:04 +0800511 struct lws *wsi; /* owner */
Andy Greende132b92015-12-25 13:48:20 +0800512 char *data; /* prepared by context init to point to dedicated storage */
513 /*
514 * the randomly ordered fragments, indexed by frag_index and
515 * lws_fragments->nfrag for continuation.
516 */
517 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
Andy Green8c1f6022016-01-26 20:56:56 +0800518 time_t assigned;
Andy Green3df58002015-12-25 12:44:12 +0800519 /*
520 * for each recognized token, frag_index says which frag[] his data
521 * starts in (0 means the token did not appear)
522 * the actual header data gets dumped as it comes in, into data[]
523 */
524 unsigned char frag_index[WSI_TOKEN_COUNT];
Andy Green2c218e72016-02-15 12:37:04 +0800525 unsigned char rx[2048];
526 unsigned int rxpos;
527 unsigned int rxlen;
528
Andy Green3df58002015-12-25 12:44:12 +0800529#ifndef LWS_NO_CLIENT
530 char initial_handshake_hash_base64[30];
Andy Green3df58002015-12-25 12:44:12 +0800531#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800532
533 unsigned short pos;
534 unsigned char in_use;
535 unsigned char nfrag;
Andy Green3df58002015-12-25 12:44:12 +0800536};
537
Andy Greend3a55052016-01-19 03:34:24 +0800538/*
539 * so we can have n connections being serviced simultaneously,
540 * these things need to be isolated per-thread.
541 */
542
543struct lws_context_per_thread {
Andy Green8c1f6022016-01-26 20:56:56 +0800544#if LWS_MAX_SMP > 1
545 pthread_mutex_t lock;
546#endif
Andy Greend3a55052016-01-19 03:34:24 +0800547 struct lws_pollfd *fds;
548 struct lws *rx_draining_ext_list;
549 struct lws *tx_draining_ext_list;
Andy Green8c1f6022016-01-26 20:56:56 +0800550 struct lws *timeout_list;
Andy Green38a1cbb2016-02-27 11:03:27 +0800551 struct lws_context *context;
Andy Green6a8099b2016-02-21 21:25:48 +0800552#ifdef LWS_WITH_CGI
553 struct lws_cgi *cgi_list;
554#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800555 void *http_header_data;
556 struct allocated_headers *ah_pool;
557 struct lws *ah_wait_list;
558 int ah_wait_list_length;
Andy Greend3a55052016-01-19 03:34:24 +0800559#ifdef LWS_OPENSSL_SUPPORT
560 struct lws *pending_read_list; /* linked list */
561#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800562#ifndef LWS_NO_SERVER
563 struct lws *wsi_listening;
564#endif
Andy Green86ed65f2016-02-14 09:27:41 +0800565#if defined(LWS_USE_LIBEV)
566 struct ev_loop *io_loop_ev;
567#endif
568#if defined(LWS_USE_LIBUV)
569 uv_loop_t *io_loop_uv;
570 uv_signal_t signals[8];
Andy Green38a1cbb2016-02-27 11:03:27 +0800571 uv_timer_t uv_timeout_watcher;
Andy Green86ed65f2016-02-14 09:27:41 +0800572#endif
573#if defined(LWS_USE_LIBEV)
574 struct lws_io_watcher w_accept;
575#endif
576#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
577 struct lws_signal_watcher w_sigint;
578 unsigned char ev_loop_foreign:1;
579#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800580 lws_sockfd_type lserv_fd;
581
582 unsigned long count_conns;
Andy Greend3a55052016-01-19 03:34:24 +0800583 /*
584 * usable by anything in the service code, but only if the scope
585 * does not last longer than the service action (since next service
586 * of any socket can likewise use it and overwrite)
587 */
588 unsigned char *serv_buf;
589#ifdef _WIN32
590 WSAEVENT *events;
591#else
592 int dummy_pipe_fds[2];
593#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800594 unsigned int fds_count;
595
596 short ah_count_in_use;
Andy Green38a1cbb2016-02-27 11:03:27 +0800597 unsigned char tid;
Andy Greend3a55052016-01-19 03:34:24 +0800598};
599
600/*
601 * the rest is managed per-context, that includes
602 *
603 * - processwide single fd -> wsi lookup
604 * - contextwide headers pool
605 * - contextwide ssl context
606 * - contextwide proxy
607 */
608
Andy Green4b85c1d2015-12-04 11:08:32 +0800609struct lws_context {
Andy Greenaa775fd2015-12-26 08:56:58 +0800610 time_t last_timeout_check_s;
611 struct lws_plat_file_ops fops;
Andy Greend3a55052016-01-19 03:34:24 +0800612 struct lws_context_per_thread pt[LWS_MAX_SMP];
Bud Davis229bfec2015-01-30 10:13:01 +0800613#ifdef _WIN32
614/* different implementation between unix and windows */
Andy Green3ef579b2015-12-04 09:23:56 +0800615 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
Bud Davis229bfec2015-01-30 10:13:01 +0800616#else
Andy Green4b85c1d2015-12-04 11:08:32 +0800617 struct lws **lws_lookup; /* fd to wsi */
Bud Davis229bfec2015-01-30 10:13:01 +0800618#endif
Mattias Lundberg03bb8f92014-02-18 10:06:57 +0100619 const char *iface;
Andy Greenaa775fd2015-12-26 08:56:58 +0800620 const struct lws_token_limits *token_limits;
621 void *user_space;
Andy Greend738f842016-01-19 04:32:14 +0800622
Andy Greenaa775fd2015-12-26 08:56:58 +0800623 const struct lws_protocols *protocols;
Andy Greend3a55052016-01-19 03:34:24 +0800624
Andy Greenaa775fd2015-12-26 08:56:58 +0800625#ifdef LWS_OPENSSL_SUPPORT
626 SSL_CTX *ssl_ctx;
627 SSL_CTX *ssl_client_ctx;
Andy Greenaa775fd2015-12-26 08:56:58 +0800628#endif
629#ifndef LWS_NO_EXTENSIONS
630 const struct lws_extension *extensions;
631#endif
Andy Green86ed65f2016-02-14 09:27:41 +0800632#if defined(LWS_USE_LIBEV)
633 lws_ev_signal_cb_t * lws_ev_sigint_cb;
634#endif
635#if defined(LWS_USE_LIBUV)
636 lws_uv_signal_cb_t * lws_uv_sigint_cb;
637#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800638 char http_proxy_address[128];
639 char proxy_basic_auth_token[128];
640 char canonical_hostname[128];
641#ifdef LWS_LATENCY
642 unsigned long worst_latency;
643 char worst_latency_info[256];
644#endif
Andy Greenb8b247d2013-01-22 07:20:08 +0800645
Andy Greenaa775fd2015-12-26 08:56:58 +0800646 int max_fds;
647 int listen_port;
Andy Green86ed65f2016-02-14 09:27:41 +0800648#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
Andy Greenaa775fd2015-12-26 08:56:58 +0800649 int use_ev_sigint;
650#endif
Andy Green24cba922013-01-19 13:56:10 +0800651 int started_with_parent;
652
Andy Green44eee682011-02-10 09:32:24 +0000653 int fd_random;
Andy Green54806b12015-12-17 17:03:59 +0800654 int lserv_mod;
Andy Green86ed65f2016-02-14 09:27:41 +0800655 int count_wsi_allocated;
Andy Greenaa775fd2015-12-26 08:56:58 +0800656 unsigned int http_proxy_port;
657 unsigned int options;
Andy Greend3a55052016-01-19 03:34:24 +0800658 unsigned int fd_limit_per_thread;
Andy Green200a6a22016-02-15 20:36:02 +0800659 unsigned int timeout_secs;
Andy Green6ee372f2012-04-09 15:09:01 +0800660
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100661 /*
662 * set to the Thread ID that's doing the service loop just before entry
663 * to poll indicates service thread likely idling in poll()
664 * volatile because other threads may check it as part of processing
665 * for pollfd event change.
666 */
667 volatile int service_tid;
Andy Greenc35b36b2015-12-24 13:00:54 +0800668 int service_tid_detected;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100669
Andy Greenaa775fd2015-12-26 08:56:58 +0800670 int count_protocols;
Andy Greena690cd02013-02-09 12:25:31 +0800671 int ka_time;
672 int ka_probes;
673 int ka_interval;
674
Andy Greenb45993c2010-12-18 15:13:50 +0000675#ifdef LWS_OPENSSL_SUPPORT
676 int use_ssl;
James Devine5b34c972013-12-14 11:41:29 +0800677 int allow_non_ssl_on_ssl_port;
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +0800678 unsigned int user_supplied_ssl_ctx:1;
Andy Greend3a55052016-01-19 03:34:24 +0800679#define lws_ssl_anybody_has_buffered_read(w) \
680 (w->context->use_ssl && \
681 w->context->pt[(int)w->tsi].pending_read_list)
682#define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
683 (c->use_ssl && \
684 c->pt[(int)t].pending_read_list)
Andy Green52815602015-01-29 08:36:18 +0800685#else
686#define lws_ssl_anybody_has_buffered_read(ctx) (0)
Andy Greend3a55052016-01-19 03:34:24 +0800687#define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
Andy Greenb45993c2010-12-18 15:13:50 +0000688#endif
Andy Green40110e82015-12-14 08:52:03 +0800689
Andy Green3df58002015-12-25 12:44:12 +0800690 short max_http_header_data;
691 short max_http_header_pool;
Andy Greend3a55052016-01-19 03:34:24 +0800692 short count_threads;
Andy Green9a9d5ea2016-01-18 11:49:41 +0800693
694 unsigned int being_destroyed:1;
Andy Green86ed65f2016-02-14 09:27:41 +0800695 unsigned int requested_kill:1;
Andy Greenb45993c2010-12-18 15:13:50 +0000696};
697
Andy Green86ed65f2016-02-14 09:27:41 +0800698LWS_EXTERN void
699lws_close_free_wsi_final(struct lws *wsi);
700LWS_EXTERN void
701lws_libuv_closehandle(struct lws *wsi);
702
Andy Greena717df22014-04-11 13:14:37 +0800703enum {
704 LWS_EV_READ = (1 << 0),
705 LWS_EV_WRITE = (1 << 1),
706 LWS_EV_START = (1 << 2),
707 LWS_EV_STOP = (1 << 3),
Andy Green86ed65f2016-02-14 09:27:41 +0800708
709 LWS_EV_PREPARE_DELETION = (1 << 31),
Andy Greena717df22014-04-11 13:14:37 +0800710};
711
Andy Green86ed65f2016-02-14 09:27:41 +0800712#if defined(LWS_USE_LIBEV)
Andy Greena717df22014-04-11 13:14:37 +0800713LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800714lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
Andy Greena717df22014-04-11 13:14:37 +0800715LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800716lws_libev_io(struct lws *wsi, int flags);
Andy Greena717df22014-04-11 13:14:37 +0800717LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800718lws_libev_init_fd_table(struct lws_context *context);
Andy Greena717df22014-04-11 13:14:37 +0800719LWS_EXTERN void
Andy Green86ed65f2016-02-14 09:27:41 +0800720lws_libev_destroyloop(struct lws_context *context, int tsi);
721LWS_EXTERN void
722lws_libev_run(const struct lws_context *context, int tsi);
723#define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
724LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800725#else
Andy Green86ed65f2016-02-14 09:27:41 +0800726#define lws_libev_accept(_a, _b) ((void) 0)
727#define lws_libev_io(_a, _b) ((void) 0)
728#define lws_libev_init_fd_table(_a) (0)
729#define lws_libev_run(_a, _b) ((void) 0)
730#define lws_libev_destroyloop(_a, _b) ((void) 0)
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800731#define LWS_LIBEV_ENABLED(context) (0)
Andy Green86ed65f2016-02-14 09:27:41 +0800732#if LWS_POSIX
Andy Greena717df22014-04-11 13:14:37 +0800733#define lws_feature_status_libev(_a) \
734 lwsl_notice("libev support not compiled in\n")
Andy Green8c0d3c02015-11-02 20:34:12 +0800735#else
736#define lws_feature_status_libev(_a)
737#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800738#endif
739
Andy Green86ed65f2016-02-14 09:27:41 +0800740#if defined(LWS_USE_LIBUV)
741LWS_EXTERN void
742lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
743LWS_EXTERN void
744lws_libuv_io(struct lws *wsi, int flags);
745LWS_EXTERN int
746lws_libuv_init_fd_table(struct lws_context *context);
747LWS_EXTERN void
748lws_libuv_run(const struct lws_context *context, int tsi);
749LWS_EXTERN void
750lws_libuv_destroyloop(struct lws_context *context, int tsi);
751#define LWS_LIBUV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBUV)
752LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
753#else
754#define lws_libuv_accept(_a, _b) ((void) 0)
755#define lws_libuv_io(_a, _b) ((void) 0)
756#define lws_libuv_init_fd_table(_a) (0)
757#define lws_libuv_run(_a, _b) ((void) 0)
758#define lws_libuv_destroyloop(_a, _b) ((void) 0)
759#define LWS_LIBUV_ENABLED(context) (0)
760#if LWS_POSIX
761#define lws_feature_status_libuv(_a) \
762 lwsl_notice("libuv support not compiled in\n")
763#else
764#define lws_feature_status_libuv(_a)
765#endif
766#endif
767
768
Andy Green055f2972014-03-24 16:09:25 +0800769#ifdef LWS_USE_IPV6
Andy Greendc8a3a82015-12-06 09:15:27 +0800770#define LWS_IPV6_ENABLED(context) \
771 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
James Devine3f13ea22014-03-24 16:09:25 +0800772#else
773#define LWS_IPV6_ENABLED(context) (0)
774#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800775
Andy Greenb1a9e502013-11-10 15:15:21 +0800776enum uri_path_states {
777 URIPS_IDLE,
778 URIPS_SEEN_SLASH,
779 URIPS_SEEN_SLASH_DOT,
780 URIPS_SEEN_SLASH_DOT_DOT,
781};
782
783enum uri_esc_states {
784 URIES_IDLE,
785 URIES_SEEN_PERCENT,
786 URIES_SEEN_PERCENT_H1,
787};
Andy Greenf3d3b402011-02-09 07:16:34 +0000788
Andy Green024eb6c2014-10-08 12:00:53 +0800789/* notice that these union members:
Andy Green40110e82015-12-14 08:52:03 +0800790 *
Andy Green024eb6c2014-10-08 12:00:53 +0800791 * hdr
792 * http
793 * http2
Andy Green40110e82015-12-14 08:52:03 +0800794 *
Andy Green024eb6c2014-10-08 12:00:53 +0800795 * all have a pointer to allocated_headers struct as their first member.
Andy Green40110e82015-12-14 08:52:03 +0800796 *
Andy Green024eb6c2014-10-08 12:00:53 +0800797 * It means for allocated_headers access, the three union paths can all be
Peter Pentchevbb085da2015-12-03 15:55:11 +0200798 * used interchangeably to access the same data
Andy Green024eb6c2014-10-08 12:00:53 +0800799 */
800
Andy Greena661ee52016-02-29 13:18:30 +0800801
802#ifndef LWS_NO_CLIENT
803struct client_info_stash {
804 char address[256];
805 char path[1024];
806 char host[256];
807 char origin[256];
808 char protocol[256];
809 char method[16];
810};
811#endif
812
Andy Green2d8d35a2016-02-29 14:19:16 +0800813struct _lws_header_related {
814 /* MUST be first in struct */
815 struct allocated_headers *ah;
816 struct lws *ah_wait_list;
817 unsigned char *preamble_rx;
818#ifndef LWS_NO_CLIENT
819 struct client_info_stash *stash;
820#endif
821 unsigned int preamble_rx_len;
822 enum uri_path_states ups;
823 enum uri_esc_states ues;
824 short lextable_pos;
825 unsigned short current_token_limit;
826#ifndef LWS_NO_CLIENT
827 unsigned short c_port;
828#endif
829 char esc_stash;
830 char post_literal_equal;
831 unsigned char parser_state; /* enum lws_token_indexes */
832 char redirects;
833};
834
Andy Green84fd9492013-11-09 11:40:32 +0800835struct _lws_http_mode_related {
Andy Green024eb6c2014-10-08 12:00:53 +0800836 /* MUST be first in struct */
Andy Green84fd9492013-11-09 11:40:32 +0800837 struct allocated_headers *ah; /* mirroring _lws_header_related */
Andy Green8c1f6022016-01-26 20:56:56 +0800838 struct lws *ah_wait_list;
Andy Green2d8d35a2016-02-29 14:19:16 +0800839 unsigned char *preamble_rx;
840#ifndef LWS_NO_CLIENT
841 struct client_info_stash *stash;
842#endif
843 unsigned int preamble_rx_len;
Andy Green8c1f6022016-01-26 20:56:56 +0800844 struct lws *new_wsi_list;
Andy Green84fd9492013-11-09 11:40:32 +0800845 unsigned long filepos;
846 unsigned long filelen;
Andy Greenaa775fd2015-12-26 08:56:58 +0800847 lws_filefd_type fd;
kapejodce64fb02013-11-19 13:38:16 +0100848
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400849 enum http_version request_version;
850 enum http_connection_type connection_type;
Andy Green2cd30742015-11-02 13:10:33 +0800851 unsigned int content_length;
852 unsigned int content_remain;
Andy Green84fd9492013-11-09 11:40:32 +0800853};
854
Andy Green024eb6c2014-10-08 12:00:53 +0800855#ifdef LWS_USE_HTTP2
856
857enum lws_http2_settings {
858 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
859 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
860 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
861 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
862 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
863 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
Andy Green40110e82015-12-14 08:52:03 +0800864
Andy Green024eb6c2014-10-08 12:00:53 +0800865 LWS_HTTP2_SETTINGS__COUNT /* always last */
Andy Greena54f2322014-09-30 09:43:14 +0800866};
867
Andy Green024eb6c2014-10-08 12:00:53 +0800868enum lws_http2_wellknown_frame_types {
869 LWS_HTTP2_FRAME_TYPE_DATA,
870 LWS_HTTP2_FRAME_TYPE_HEADERS,
871 LWS_HTTP2_FRAME_TYPE_PRIORITY,
872 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
873 LWS_HTTP2_FRAME_TYPE_SETTINGS,
874 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
875 LWS_HTTP2_FRAME_TYPE_PING,
876 LWS_HTTP2_FRAME_TYPE_GOAWAY,
877 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
878 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
Andy Green40110e82015-12-14 08:52:03 +0800879
Andy Green024eb6c2014-10-08 12:00:53 +0800880 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
881};
882
Andy Green91b05892014-10-17 08:38:44 +0800883enum lws_http2_flags {
884 LWS_HTTP2_FLAG_END_STREAM = 1,
885 LWS_HTTP2_FLAG_END_HEADERS = 4,
886 LWS_HTTP2_FLAG_PADDED = 8,
887 LWS_HTTP2_FLAG_PRIORITY = 0x20,
888
889 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
890};
891
Andy Green024eb6c2014-10-08 12:00:53 +0800892#define LWS_HTTP2_STREAM_ID_MASTER 0
893#define LWS_HTTP2_FRAME_HEADER_LENGTH 9
894#define LWS_HTTP2_SETTINGS_LENGTH 6
895
896struct http2_settings {
897 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
898};
899
Andy Greenecc2e722014-10-09 16:57:47 +0800900enum http2_hpack_state {
Andy Green40110e82015-12-14 08:52:03 +0800901
Andy Green200f3852014-10-18 12:23:05 +0800902 /* optional before first header block */
903 HPKS_OPT_PADDING,
904 HKPS_OPT_E_DEPENDENCY,
905 HKPS_OPT_WEIGHT,
Andy Green40110e82015-12-14 08:52:03 +0800906
Andy Green200f3852014-10-18 12:23:05 +0800907 /* header block */
Andy Greenecc2e722014-10-09 16:57:47 +0800908 HPKS_TYPE,
Andy Green40110e82015-12-14 08:52:03 +0800909
Andy Green2add6342014-10-12 08:38:16 +0800910 HPKS_IDX_EXT,
Andy Green40110e82015-12-14 08:52:03 +0800911
Andy Greenecc2e722014-10-09 16:57:47 +0800912 HPKS_HLEN,
913 HPKS_HLEN_EXT,
914
915 HPKS_DATA,
Andy Green40110e82015-12-14 08:52:03 +0800916
Andy Green200f3852014-10-18 12:23:05 +0800917 /* optional after last header block */
918 HKPS_OPT_DISCARD_PADDING,
Andy Greenecc2e722014-10-09 16:57:47 +0800919};
920
Andy Green2add6342014-10-12 08:38:16 +0800921enum http2_hpack_type {
922 HPKT_INDEXED_HDR_7,
923 HPKT_INDEXED_HDR_6_VALUE_INCR,
924 HPKT_LITERAL_HDR_VALUE_INCR,
925 HPKT_INDEXED_HDR_4_VALUE,
926 HPKT_LITERAL_HDR_VALUE,
927 HPKT_SIZE_5
928};
929
Andy Green200f3852014-10-18 12:23:05 +0800930struct hpack_dt_entry {
931 int token; /* additions that don't map to a token are ignored */
932 int arg_offset;
933 int arg_len;
934};
935
936struct hpack_dynamic_table {
937 struct hpack_dt_entry *entries;
938 char *args;
939 int pos;
940 int next;
941 int num_entries;
942 int args_length;
943};
944
Andy Green024eb6c2014-10-08 12:00:53 +0800945struct _lws_http2_related {
Andy Green40110e82015-12-14 08:52:03 +0800946 /*
Andy Green024eb6c2014-10-08 12:00:53 +0800947 * having this first lets us also re-use all HTTP union code
948 * and in turn, http_mode_related has allocated headers in right
949 * place so we can use the header apis on the wsi directly still
950 */
951 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
952
953 struct http2_settings my_settings;
954 struct http2_settings peer_settings;
Andy Green40110e82015-12-14 08:52:03 +0800955
Andy Green4b85c1d2015-12-04 11:08:32 +0800956 struct lws *parent_wsi;
957 struct lws *next_child_wsi;
Andy Green024eb6c2014-10-08 12:00:53 +0800958
Andy Green200f3852014-10-18 12:23:05 +0800959 struct hpack_dynamic_table *hpack_dyn_table;
Andy Greenaa775fd2015-12-26 08:56:58 +0800960 struct lws *stream_wsi;
961 unsigned char ping_payload[8];
962 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
Andy Green40110e82015-12-14 08:52:03 +0800963
Andy Green024eb6c2014-10-08 12:00:53 +0800964 unsigned int count;
Andy Green024eb6c2014-10-08 12:00:53 +0800965 unsigned int length;
966 unsigned int stream_id;
Andy Greenaa775fd2015-12-26 08:56:58 +0800967 enum http2_hpack_state hpack;
968 enum http2_hpack_type hpack_type;
969 unsigned int header_index;
970 unsigned int hpack_len;
971 unsigned int hpack_e_dep;
972 int tx_credit;
973 unsigned int my_stream_id;
974 unsigned int child_count;
975 int my_priority;
Andy Green67112662016-01-11 11:34:01 +0800976
Andy Green91b05892014-10-17 08:38:44 +0800977 unsigned int END_STREAM:1;
978 unsigned int END_HEADERS:1;
Andy Green1cea5812014-10-19 07:36:20 +0800979 unsigned int send_END_STREAM:1;
Andy Green7df53c52014-10-22 15:37:28 +0800980 unsigned int GOING_AWAY;
981 unsigned int requested_POLLOUT:1;
Andy Green97ee57f2014-10-29 09:39:08 +0800982 unsigned int waiting_tx_credit:1;
Andy Greenecc2e722014-10-09 16:57:47 +0800983 unsigned int huff:1;
984 unsigned int value:1;
Andy Green40110e82015-12-14 08:52:03 +0800985
Andy Greenaa775fd2015-12-26 08:56:58 +0800986 unsigned short round_robin_POLLOUT;
987 unsigned short count_POLLOUT_children;
988 unsigned short hpack_pos;
989
990 unsigned char type;
991 unsigned char flags;
992 unsigned char frame_state;
993 unsigned char padding;
994 unsigned char hpack_m;
Andy Green024eb6c2014-10-08 12:00:53 +0800995 unsigned char initialized;
Andy Green024eb6c2014-10-08 12:00:53 +0800996};
997
Andy Green7df53c52014-10-22 15:37:28 +0800998#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
Andy Green024eb6c2014-10-08 12:00:53 +0800999
1000#endif
1001
Andy Green623a98d2013-01-21 11:04:23 +08001002struct _lws_websocket_related {
Andy Green2c218e72016-02-15 12:37:04 +08001003 /* cheapest way to deal with ah overlap with ws union transition */
Andy Green26d42492016-02-24 12:40:21 +08001004 struct _lws_header_related hdr;
Andy Green67112662016-01-11 11:34:01 +08001005 char *rx_ubuf;
Andy Green4019aab2016-01-30 11:43:10 +08001006 unsigned int rx_ubuf_alloc;
Andy Green67112662016-01-11 11:34:01 +08001007 struct lws *rx_draining_ext_list;
1008 struct lws *tx_draining_ext_list;
Andy Greende132b92015-12-25 13:48:20 +08001009 size_t rx_packet_length;
Andy Green67112662016-01-11 11:34:01 +08001010 unsigned int rx_ubuf_head;
1011 unsigned char mask[4];
Andy Green1fb95e82015-12-26 17:20:34 +08001012 /* Also used for close content... control opcode == < 128 */
Andy Green67112662016-01-11 11:34:01 +08001013 unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
Andy Green1fb95e82015-12-26 17:20:34 +08001014
Andy Greenaa775fd2015-12-26 08:56:58 +08001015 unsigned char ping_payload_len;
Andy Green67112662016-01-11 11:34:01 +08001016 unsigned char mask_idx;
Andy Green623a98d2013-01-21 11:04:23 +08001017 unsigned char opcode;
Andy Green623a98d2013-01-21 11:04:23 +08001018 unsigned char rsv;
Andy Green67112662016-01-11 11:34:01 +08001019 unsigned char rsv_first_msg;
Andy Green1fb95e82015-12-26 17:20:34 +08001020 /* zero if no info, or length including 2-byte close code */
1021 unsigned char close_in_ping_buffer_len;
Andy Green0c7b38b2015-12-29 09:46:03 +08001022 unsigned char utf8;
Andy Green67112662016-01-11 11:34:01 +08001023 unsigned char stashed_write_type;
1024 unsigned char tx_draining_stashed_wp;
Andy Greende132b92015-12-25 13:48:20 +08001025
1026 unsigned int final:1;
Andy Greend91d5e82013-02-10 16:00:47 +08001027 unsigned int frame_is_binary:1;
Andy Greend91d5e82013-02-10 16:00:47 +08001028 unsigned int all_zero_nonce:1;
Andy Greend91d5e82013-02-10 16:00:47 +08001029 unsigned int this_frame_masked:1;
Andy Green1f4267b2013-10-17 08:09:19 +08001030 unsigned int inside_frame:1; /* next write will be more of frame */
1031 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
Andy Green40d5abc2015-04-17 20:29:58 +08001032 unsigned int payload_is_close:1; /* process as PONG, but it is close */
Andy Greenba38a7e2015-12-25 13:14:09 +08001033 unsigned int ping_pending_flag:1;
Andy Green977734e2015-12-28 16:51:08 +08001034 unsigned int continuation_possible:1;
Andy Green91d624e2015-12-28 17:05:40 +08001035 unsigned int owed_a_fin:1;
Andy Green9b81d3c2015-12-29 12:28:48 +08001036 unsigned int check_utf8:1;
1037 unsigned int defeat_check_utf8:1;
Andy Green67112662016-01-11 11:34:01 +08001038 unsigned int pmce_compressed_message:1;
1039 unsigned int stashed_write_pending:1;
1040 unsigned int rx_draining_ext:1;
1041 unsigned int tx_draining_ext:1;
Andy Green623a98d2013-01-21 11:04:23 +08001042};
1043
Andy Green6a8099b2016-02-21 21:25:48 +08001044#ifdef LWS_WITH_CGI
1045
1046/* wsi who is master of the cgi points to an lws_cgi */
1047
1048struct lws_cgi {
1049 struct lws_cgi *cgi_list;
1050 struct lws *stdwsi[3]; /* points to the associated stdin/out/err wsis */
1051 struct lws *wsi; /* owner */
1052 int pipe_fds[3][2];
1053 int pid;
1054
1055 unsigned int being_closed:1;
1056};
Andy Greenc3c2d6d2016-03-09 07:41:59 +08001057
Andy Green6a8099b2016-02-21 21:25:48 +08001058#endif
1059
Andy Green4b85c1d2015-12-04 11:08:32 +08001060struct lws {
Andy Green623a98d2013-01-21 11:04:23 +08001061
Andy Greenaa775fd2015-12-26 08:56:58 +08001062 /* structs */
1063 /* members with mutually exclusive lifetimes are unionized */
1064
1065 union u {
1066 struct _lws_http_mode_related http;
1067#ifdef LWS_USE_HTTP2
1068 struct _lws_http2_related http2;
1069#endif
1070 struct _lws_header_related hdr;
1071 struct _lws_websocket_related ws;
1072 } u;
1073
Andy Green623a98d2013-01-21 11:04:23 +08001074 /* lifetime members */
1075
Andy Green86ed65f2016-02-14 09:27:41 +08001076#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
Andy Green40110e82015-12-14 08:52:03 +08001077 struct lws_io_watcher w_read;
Andy Green86ed65f2016-02-14 09:27:41 +08001078#endif
1079#if defined(LWS_USE_LIBEV)
Andy Green40110e82015-12-14 08:52:03 +08001080 struct lws_io_watcher w_write;
Andy Green86ed65f2016-02-14 09:27:41 +08001081#endif
Andy Greende132b92015-12-25 13:48:20 +08001082 time_t pending_timeout_limit;
Andy Greenaa775fd2015-12-26 08:56:58 +08001083
1084 /* pointers */
1085
Andy Green40110e82015-12-14 08:52:03 +08001086 struct lws_context *context;
Andy Green494418a2016-03-02 09:17:22 +08001087 struct lws *parent; /* points to parent, if any */
1088 struct lws *child_list; /* points to first child */
1089 struct lws *sibling_list; /* subsequent children at same level */
Andy Green6a8099b2016-02-21 21:25:48 +08001090#ifdef LWS_WITH_CGI
1091 struct lws_cgi *cgi; /* wsi being cgi master have one of these */
Andy Green6a8099b2016-02-21 21:25:48 +08001092#endif
Andy Green4b85c1d2015-12-04 11:08:32 +08001093 const struct lws_protocols *protocol;
Andy Greend738f842016-01-19 04:32:14 +08001094 struct lws *timeout_list;
1095 struct lws **timeout_list_prev;
Andy Greende132b92015-12-25 13:48:20 +08001096 void *user_space;
1097 /* rxflow handling */
1098 unsigned char *rxflow_buffer;
1099 /* truncated send handling */
1100 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
Andy Green3182ece2013-01-20 17:08:31 +08001101#ifndef LWS_NO_EXTENSIONS
Andy Greend2ac22c2015-12-11 10:45:35 +08001102 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green67112662016-01-11 11:34:01 +08001103 void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green3182ece2013-01-20 17:08:31 +08001104#endif
Andy Greende132b92015-12-25 13:48:20 +08001105#ifdef LWS_OPENSSL_SUPPORT
1106 SSL *ssl;
1107 BIO *client_bio;
1108 struct lws *pending_read_list_prev, *pending_read_list_next;
1109#endif
Andy Greenaa775fd2015-12-26 08:56:58 +08001110#ifdef LWS_LATENCY
1111 unsigned long action_start;
1112 unsigned long latency_start;
1113#endif
1114 /* pointer / int */
Andy Green3b193862015-11-02 13:13:44 +08001115 lws_sockfd_type sock;
Andy Greende132b92015-12-25 13:48:20 +08001116
Andy Greenaa775fd2015-12-26 08:56:58 +08001117 /* ints */
Andy Greendfb23042013-01-17 12:26:48 +08001118 int position_in_fds_table;
Andy Green024eb6c2014-10-08 12:00:53 +08001119 int rxflow_len;
1120 int rxflow_pos;
Andy Green54806b12015-12-17 17:03:59 +08001121 unsigned int trunc_alloc_len; /* size of malloc */
1122 unsigned int trunc_offset; /* where we are in terms of spilling */
1123 unsigned int trunc_len; /* how much is buffered */
Andy Green2764eba2013-12-09 14:16:17 +08001124
Andy Greende132b92015-12-25 13:48:20 +08001125 unsigned int hdr_parsing_completed:1;
1126 unsigned int user_space_externally_allocated:1;
1127 unsigned int socket_is_permanently_unusable:1;
1128 unsigned int rxflow_change_to:2;
Andy Green83af28a2016-02-28 10:55:31 +08001129 unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
Andy Greena661ee52016-02-29 13:18:30 +08001130#ifndef LWS_NO_CLIENT
1131 unsigned int do_ws:1; /* whether we are doing http or ws flow */
1132#endif
Andy Greende132b92015-12-25 13:48:20 +08001133#ifndef LWS_NO_EXTENSIONS
1134 unsigned int extension_data_pending:1;
1135#endif
1136#ifdef LWS_OPENSSL_SUPPORT
1137 unsigned int use_ssl:2;
1138 unsigned int upgraded:1;
1139#endif
Andy Greenaa775fd2015-12-26 08:56:58 +08001140#ifdef _WIN32
1141 unsigned int sock_send_blocking:1;
1142#endif
Andy Green0f9904f2016-03-17 15:26:49 +08001143#ifdef LWS_OPENSSL_SUPPORT
1144 unsigned int redirect_to_https:1;
1145#endif
Andy Greende132b92015-12-25 13:48:20 +08001146
Andy Greenaa775fd2015-12-26 08:56:58 +08001147 /* chars */
Andy Greende132b92015-12-25 13:48:20 +08001148#ifndef LWS_NO_EXTENSIONS
Andy Green67112662016-01-11 11:34:01 +08001149 unsigned char count_act_ext;
Andy Greende132b92015-12-25 13:48:20 +08001150#endif
1151 unsigned char ietf_spec_revision;
1152 char mode; /* enum connection_mode */
1153 char state; /* enum lws_connection_states */
Andy Green8c1f6022016-01-26 20:56:56 +08001154 char state_pre_close;
Andy Greende132b92015-12-25 13:48:20 +08001155 char lws_rx_parse_state; /* enum lws_rx_parse_state */
1156 char rx_frame_type; /* enum lws_write_protocol */
1157 char pending_timeout; /* enum pending_timeout */
Andy Greend738f842016-01-19 04:32:14 +08001158 char pps; /* enum lws_pending_protocol_send */
Andy Greend3a55052016-01-19 03:34:24 +08001159 char tsi; /* thread service index we belong to */
Andy Green6a8099b2016-02-21 21:25:48 +08001160#ifdef LWS_WITH_CGI
1161 char cgi_channel; /* which of stdin/out/err */
Andy Greenc3c2d6d2016-03-09 07:41:59 +08001162 char hdr_state;
Andy Green6a8099b2016-02-21 21:25:48 +08001163#endif
Andy Green7c212cc2010-11-08 20:20:42 +00001164};
1165
Andy Green3d67f512014-04-03 07:29:50 +08001166LWS_EXTERN int log_level;
1167
Andy Greenc7939442016-03-12 08:18:58 +08001168LWS_EXTERN int
1169lws_socket_bind(struct lws_context *context, int sockfd, int port,
1170 const char *iface);
1171
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001172LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +08001173lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
Andy Green508946c2013-02-12 10:19:08 +08001174
Andy Green34f3dd22014-04-03 07:42:50 +08001175LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001176remove_wsi_socket_from_fds(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +08001177LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001178lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
Andy Green34f3dd22014-04-03 07:42:50 +08001179
Andy Greend636e352013-01-29 12:36:17 +08001180#ifndef LWS_LATENCY
Andy Greendc8a3a82015-12-06 09:15:27 +08001181static inline void
1182lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1183 int ret, int completion) {
Andy Green40110e82015-12-14 08:52:03 +08001184 do {
1185 (void)context; (void)wsi; (void)action; (void)ret;
1186 (void)completion;
Andy Greendc8a3a82015-12-06 09:15:27 +08001187 } while (0);
1188}
1189static inline void
1190lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1191 do { (void)context; (void)wsi; } while (0);
1192}
Andy Greend636e352013-01-29 12:36:17 +08001193#else
1194#define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1195extern void
Andy Greendc8a3a82015-12-06 09:15:27 +08001196lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1197 int ret, int completion);
Andy Greend636e352013-01-29 12:36:17 +08001198#endif
1199
Andy Greendc8a3a82015-12-06 09:15:27 +08001200LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +08001201lws_set_protocol_write_pending(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001202 enum lws_pending_protocol_send pend);
Andy Greene99a83c2016-01-20 16:56:06 +08001203LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001204lws_client_rx_sm(struct lws *wsi, unsigned char c);
Andy Green4739e5c2011-01-22 12:51:57 +00001205
Andy Greene99a83c2016-01-20 16:56:06 +08001206LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001207lws_parse(struct lws *wsi, unsigned char c);
Andy Greenb45993c2010-12-18 15:13:50 +00001208
Andy Greene99a83c2016-01-20 16:56:06 +08001209LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001210lws_http_action(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +08001211
1212LWS_EXTERN int
Andy Greendf736162011-01-18 15:39:02 +00001213lws_b64_selftest(void);
Andy Greenbfb051f2011-02-09 08:49:14 +00001214
Andy Green2c218e72016-02-15 12:37:04 +08001215LWS_EXTERN int
1216lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
1217
1218LWS_EXTERN int
1219lws_service_flag_pending(struct lws_context *context, int tsi);
1220
Andy Green2cd30742015-11-02 13:10:33 +08001221#if defined(_WIN32) || defined(MBED_OPERATORS)
Andy Green4b85c1d2015-12-04 11:08:32 +08001222LWS_EXTERN struct lws *
Andy Green1fa76852015-12-14 11:17:16 +08001223wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
Andy Green0d338332011-02-12 11:57:43 +00001224
Andy Green40110e82015-12-14 08:52:03 +08001225LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001226insert_wsi(struct lws_context *context, struct lws *wsi);
Bud Davis229bfec2015-01-30 10:13:01 +08001227
1228LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001229delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
Bud Davis229bfec2015-01-30 10:13:01 +08001230#else
Andy Green40110e82015-12-14 08:52:03 +08001231#define wsi_from_fd(A,B) A->lws_lookup[B]
Andy Green8c1f6022016-01-26 20:56:56 +08001232#define insert_wsi(A,B) assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
Bud Davis229bfec2015-01-30 10:13:01 +08001233#define delete_from_fd(A,B) A->lws_lookup[B]=0
1234#endif
1235
Andy Greene99a83c2016-01-20 16:56:06 +08001236LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001237insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
Darin Willitsc19456f2011-02-14 17:52:39 +00001238
Andy Greene99a83c2016-01-20 16:56:06 +08001239LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001240lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
Andy Greend44bf7f2011-03-06 10:29:38 +00001241
Andy Green95a7b5d2011-03-06 10:29:39 +00001242
Andy Greene99a83c2016-01-20 16:56:06 +08001243LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001244lws_service_timeout_check(struct lws *wsi, unsigned int sec);
Andy Greena41314f2011-05-23 10:00:03 +01001245
Andy Greene99a83c2016-01-20 16:56:06 +08001246LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001247lws_client_connect_2(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001248
Andy Greene99a83c2016-01-20 16:56:06 +08001249LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1250lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
1251 const char *path, const char *host);
Andy Green809d69a2016-01-14 11:37:56 +08001252
Andy Greene99a83c2016-01-20 16:56:06 +08001253LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001254lws_create_new_server_wsi(struct lws_context *context);
Andy Greena41314f2011-05-23 10:00:03 +01001255
Andy Greene99a83c2016-01-20 16:56:06 +08001256LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001257lws_generate_client_handshake(struct lws *wsi, char *pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001258
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001259LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001260lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
Andy Green91b05892014-10-17 08:38:44 +08001261
Andy Green2d8d35a2016-02-29 14:19:16 +08001262LWS_EXTERN struct lws *
1263lws_client_connect_via_info2(struct lws *wsi);
1264
Andy Greencdb9bf92014-04-12 10:07:02 +08001265/*
1266 * EXTENSIONS
1267 */
1268
Andy Green3182ece2013-01-20 17:08:31 +08001269#ifndef LWS_NO_EXTENSIONS
Andy Greencdb9bf92014-04-12 10:07:02 +08001270LWS_VISIBLE void
1271lws_context_init_extensions(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001272 struct lws_context *context);
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001273LWS_EXTERN int
Andy Greene99a83c2016-01-20 16:56:06 +08001274lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
Andy Green6ee372f2012-04-09 15:09:01 +08001275 void *v, size_t len);
Andy Greena41314f2011-05-23 10:00:03 +01001276
Andy Green2c24ec02014-04-02 19:45:42 +08001277LWS_EXTERN int
Andy Greene99a83c2016-01-20 16:56:06 +08001278lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
Andy Green2c24ec02014-04-02 19:45:42 +08001279LWS_EXTERN int
Andy Greene99a83c2016-01-20 16:56:06 +08001280lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1281 void *arg, int len);
Andy Green67112662016-01-11 11:34:01 +08001282
Andy Green2c24ec02014-04-02 19:45:42 +08001283#else
Andy Green6b5de702015-12-15 21:15:58 +08001284#define lws_any_extension_handled(_a, _b, _c, _d) (0)
Andy Green67112662016-01-11 11:34:01 +08001285#define lws_ext_cb_active(_a, _b, _c, _d) (0)
Andy Green54806b12015-12-17 17:03:59 +08001286#define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
Andy Greenb49a9952014-04-03 10:11:04 +08001287#define lws_issue_raw_ext_access lws_issue_raw
Andy Greencdb9bf92014-04-12 10:07:02 +08001288#define lws_context_init_extensions(_a, _b)
Andy Green3182ece2013-01-20 17:08:31 +08001289#endif
Andy Greena41314f2011-05-23 10:00:03 +01001290
Andy Greene99a83c2016-01-20 16:56:06 +08001291LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001292lws_client_interpret_server_handshake(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001293
Andy Greene99a83c2016-01-20 16:56:06 +08001294LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001295lws_rx_sm(struct lws *wsi, unsigned char c);
Andy Greena41314f2011-05-23 10:00:03 +01001296
Alex Hultman599cad92016-03-17 09:34:15 +08001297LWS_EXTERN void
1298lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
1299
Andy Greene99a83c2016-01-20 16:56:06 +08001300LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001301lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
Andy Green09226502011-05-28 10:19:19 +01001302
Andy Green44c11612014-11-08 11:18:47 +08001303LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001304lws_union_transition(struct lws *wsi, enum connection_mode mode);
Andy Green44c11612014-11-08 11:18:47 +08001305
Andy Greene99a83c2016-01-20 16:56:06 +08001306LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Denis Osvald034e5142015-12-21 18:06:38 +01001307user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
Andy Green00c6d152015-12-17 07:54:44 +08001308 enum lws_callback_reasons reason, void *user,
1309 void *in, size_t len);
Andy Green024eb6c2014-10-08 12:00:53 +08001310#ifdef LWS_USE_HTTP2
Andy Green4b85c1d2015-12-04 11:08:32 +08001311LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1312struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
Andy Green024eb6c2014-10-08 12:00:53 +08001313LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001314lws_http2_interpret_settings_payload(struct http2_settings *settings,
1315 unsigned char *buf, int len);
Andy Green024eb6c2014-10-08 12:00:53 +08001316LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1317LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001318lws_http2_parser(struct lws *wsi, unsigned char c);
Andy Greendc8a3a82015-12-06 09:15:27 +08001319LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1320 struct lws *wsi);
1321LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1322 unsigned int sid, unsigned int len,
1323 unsigned char *buf);
Andy Green4b85c1d2015-12-04 11:08:32 +08001324LWS_EXTERN struct lws *
1325lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
Andy Green11c05bf2015-12-16 18:19:08 +08001326LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
Andy Green2add6342014-10-12 08:38:16 +08001327 unsigned char c);
Andy Green917f43a2014-10-12 14:31:47 +08001328LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001329lws_add_http2_header_by_name(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001330 const unsigned char *name,
1331 const unsigned char *value, int length,
1332 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001333LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001334lws_add_http2_header_by_token(struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001335 enum lws_token_indexes token,
Andy Greendc8a3a82015-12-06 09:15:27 +08001336 const unsigned char *value, int length,
1337 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001338LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001339lws_add_http2_header_status(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001340 unsigned int code, unsigned char **p,
Andy Green917f43a2014-10-12 14:31:47 +08001341 unsigned char *end);
Andy Green7df53c52014-10-22 15:37:28 +08001342LWS_EXTERN
Andy Green4b85c1d2015-12-04 11:08:32 +08001343void lws_http2_configure_if_upgraded(struct lws *wsi);
Andy Green7df53c52014-10-22 15:37:28 +08001344#else
1345#define lws_http2_configure_if_upgraded(x)
Andy Green024eb6c2014-10-08 12:00:53 +08001346#endif
Andy Green706961d2013-01-17 16:50:35 +08001347
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001348LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001349lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
Andy Greena690cd02013-02-09 12:25:31 +08001350
Andy Greene99a83c2016-01-20 16:56:06 +08001351LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greene3d141d2016-02-27 11:42:22 +08001352lws_header_table_attach(struct lws *wsi, int autoservice);
Andy Green16ab3182013-02-10 18:02:31 +08001353
Andrew Canaday37718812014-11-07 11:20:59 +08001354LWS_EXTERN int
Andy Greene3d141d2016-02-27 11:42:22 +08001355lws_header_table_detach(struct lws *wsi, int autoservice);
Andrew Canaday37718812014-11-07 11:20:59 +08001356
Andy Green4019aab2016-01-30 11:43:10 +08001357LWS_EXTERN void
Andy Greene3d141d2016-02-27 11:42:22 +08001358lws_header_table_reset(struct lws *wsi, int autoservice);
Andy Green4019aab2016-01-30 11:43:10 +08001359
Andy Greene99a83c2016-01-20 16:56:06 +08001360LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001361lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001362
Andy Greene99a83c2016-01-20 16:56:06 +08001363LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green11c05bf2015-12-16 18:19:08 +08001364lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
Andy Greenb5b23192013-02-11 17:13:32 +08001365
Andy Green2af4d5b2013-02-18 16:30:10 +08001366LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001367lws_ensure_user_space(struct lws *wsi);
Andy Green2af4d5b2013-02-18 16:30:10 +08001368
Andy Green158e8042014-04-02 14:25:10 +08001369LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001370lws_change_pollfd(struct lws *wsi, int _and, int _or);
Andy Green91f19d82013-12-21 11:18:34 +08001371
Andy Greenb5b23192013-02-11 17:13:32 +08001372#ifndef LWS_NO_SERVER
Andy Greene38031a2014-04-03 08:24:29 +08001373int lws_context_init_server(struct lws_context_creation_info *info,
Andy Greenaa775fd2015-12-26 08:56:58 +08001374 struct lws_context *context);
Andy Greend7340c12014-04-10 14:08:10 +08001375LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001376handshake_0405(struct lws_context *context, struct lws *wsi);
Andy Greene99a83c2016-01-20 16:56:06 +08001377LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green67112662016-01-11 11:34:01 +08001378lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
Andy Greencdb9bf92014-04-12 10:07:02 +08001379LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001380lws_server_get_canonical_hostname(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001381 struct lws_context_creation_info *info);
Andy Greene38031a2014-04-03 08:24:29 +08001382#else
1383#define lws_context_init_server(_a, _b) (0)
Andy Green3ef579b2015-12-04 09:23:56 +08001384#define lws_interpret_incoming_packet(_a, _b, _c) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001385#define lws_server_get_canonical_hostname(_a, _b)
Andy Greenb5b23192013-02-11 17:13:32 +08001386#endif
1387
1388#ifndef LWS_NO_DAEMONIZE
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001389LWS_EXTERN int get_daemonize_pid();
Andy Greencdb9bf92014-04-12 10:07:02 +08001390#else
1391#define get_daemonize_pid() (0)
Andy Greenb5b23192013-02-11 17:13:32 +08001392#endif
1393
Andy Green2cd30742015-11-02 13:10:33 +08001394#if !defined(MBED_OPERATORS)
Andy Greene99a83c2016-01-20 16:56:06 +08001395LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001396interface_to_sa(struct lws_context *context, const char *ifname,
1397 struct sockaddr_in *addr, size_t addrlen);
Andy Green2cd30742015-11-02 13:10:33 +08001398#endif
Andy Greenb25b85f2014-04-03 23:34:09 +08001399LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1400
Andy Green1a308e42014-04-08 07:26:30 +01001401enum lws_ssl_capable_status {
1402 LWS_SSL_CAPABLE_ERROR = -1,
1403 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1404};
1405
Darin Willitsc19456f2011-02-14 17:52:39 +00001406#ifndef LWS_OPENSSL_SUPPORT
Andy Greencdb9bf92014-04-12 10:07:02 +08001407#define LWS_SSL_ENABLED(context) (0)
Andy Greenc57037a2014-04-03 10:17:00 +08001408#define lws_context_init_server_ssl(_a, _b) (0)
1409#define lws_ssl_destroy(_a)
Andy Green2eedea92014-04-03 14:33:48 +08001410#define lws_context_init_http2_ssl(_a)
Andy Green02138122014-04-06 06:26:35 +01001411#define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1412#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 +02001413#define lws_ssl_pending lws_ssl_pending_no_ssl
Andy Green8c1f6022016-01-26 20:56:56 +08001414#define lws_server_socket_service_ssl(_b, _c) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001415#define lws_ssl_close(_a) (0)
1416#define lws_ssl_context_destroy(_a)
Andy Green6b5de702015-12-15 21:15:58 +08001417#define lws_ssl_remove_wsi_from_buffered_list(_a)
Andy Greenb5b23192013-02-11 17:13:32 +08001418#else
Andy Greencdb9bf92014-04-12 10:07:02 +08001419#define LWS_SSL_ENABLED(context) (context->use_ssl)
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001420LWS_EXTERN int openssl_websocket_private_data_index;
Andy Greene99a83c2016-01-20 16:56:06 +08001421LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001422lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
Andy Greene99a83c2016-01-20 16:56:06 +08001423LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001424lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
Andy Greene99a83c2016-01-20 16:56:06 +08001425LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001426lws_ssl_pending(struct lws *wsi);
Andy Greene99a83c2016-01-20 16:56:06 +08001427LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green8c1f6022016-01-26 20:56:56 +08001428lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
Andy Greencdb9bf92014-04-12 10:07:02 +08001429LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001430lws_ssl_close(struct lws *wsi);
Andy Greencdb9bf92014-04-12 10:07:02 +08001431LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001432lws_ssl_context_destroy(struct lws_context *context);
Andy Green52815602015-01-29 08:36:18 +08001433LWS_VISIBLE void
Andy Green6b5de702015-12-15 21:15:58 +08001434lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
Andy Greenc57037a2014-04-03 10:17:00 +08001435#ifndef LWS_NO_SERVER
1436LWS_EXTERN int
1437lws_context_init_server_ssl(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001438 struct lws_context *context);
Andy Greenc57037a2014-04-03 10:17:00 +08001439#else
1440#define lws_context_init_server_ssl(_a, _b) (0)
1441#endif
1442LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001443lws_ssl_destroy(struct lws_context *context);
Andy Green2eedea92014-04-03 14:33:48 +08001444
1445/* HTTP2-related */
1446
1447#ifdef LWS_USE_HTTP2
1448LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001449lws_context_init_http2_ssl(struct lws_context *context);
Andy Green2eedea92014-04-03 14:33:48 +08001450#else
1451#define lws_context_init_http2_ssl(_a)
1452#endif
Darin Willitsc19456f2011-02-14 17:52:39 +00001453#endif
Andy Greena654fc02014-04-03 07:16:40 +08001454
Andy Green8c1f6022016-01-26 20:56:56 +08001455#if LWS_MAX_SMP > 1
1456static LWS_INLINE void
1457lws_pt_mutex_init(struct lws_context_per_thread *pt)
1458{
1459 pthread_mutex_init(&pt->lock, NULL);
1460}
1461static LWS_INLINE void
1462lws_pt_lock(struct lws_context_per_thread *pt)
1463{
1464 pthread_mutex_lock(&pt->lock);
1465}
1466
1467static LWS_INLINE void
1468lws_pt_unlock(struct lws_context_per_thread *pt)
1469{
1470 pthread_mutex_unlock(&pt->lock);
1471}
1472#else
1473#define lws_pt_mutex_init(_a) (void)(_a)
1474#define lws_pt_lock(_a) (void)(_a)
1475#define lws_pt_unlock(_a) (void)(_a)
1476#endif
1477
Andy Greene99a83c2016-01-20 16:56:06 +08001478LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001479lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001480
Andy Greene99a83c2016-01-20 16:56:06 +08001481LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001482lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001483
Andy Greene99a83c2016-01-20 16:56:06 +08001484LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001485lws_ssl_pending_no_ssl(struct lws *wsi);
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001486
Andy Greena654fc02014-04-03 07:16:40 +08001487#ifndef LWS_NO_CLIENT
Andy Greendc8a3a82015-12-06 09:15:27 +08001488LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1489 struct lws *wsi,
1490 struct lws_pollfd *pollfd);
Andy Greenc57037a2014-04-03 10:17:00 +08001491#ifdef LWS_OPENSSL_SUPPORT
Andy Greendc8a3a82015-12-06 09:15:27 +08001492LWS_EXTERN int
1493lws_context_init_client_ssl(struct lws_context_creation_info *info,
Andy Green4b85c1d2015-12-04 11:08:32 +08001494 struct lws_context *context);
Andy Greene38031a2014-04-03 08:24:29 +08001495#else
Andy Greenc57037a2014-04-03 10:17:00 +08001496 #define lws_context_init_client_ssl(_a, _b) (0)
1497#endif
Andy Greene99a83c2016-01-20 16:56:06 +08001498LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001499lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1500LWS_EXTERN void
1501lws_decode_ssl_error(void);
Andy Greenc57037a2014-04-03 10:17:00 +08001502#else
1503#define lws_context_init_client_ssl(_a, _b) (0)
Andy Greenaad2eac2014-04-03 09:03:37 +08001504#define lws_handshake_client(_a, _b, _c) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001505#endif
Andy Green44e0b082015-12-28 14:24:49 +08001506
1507LWS_EXTERN int
1508_lws_rx_flow_control(struct lws *wsi);
1509
Andy Green8c1f6022016-01-26 20:56:56 +08001510LWS_EXTERN int
1511_lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1512
Andy Greena654fc02014-04-03 07:16:40 +08001513#ifndef LWS_NO_SERVER
Andy Greendc8a3a82015-12-06 09:15:27 +08001514LWS_EXTERN int
1515lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1516 struct lws_pollfd *pollfd);
1517LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001518lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
Andy Greenb3d21f12015-12-25 09:12:08 +08001519LWS_EXTERN int
Andy Green8c1f6022016-01-26 20:56:56 +08001520_lws_server_listen_accept_flow_control(struct lws *twsi, int on);
Andy Greend99476b2014-04-03 08:40:05 +08001521#else
Andy Greenb49a9952014-04-03 10:11:04 +08001522#define lws_server_socket_service(_a, _b, _c) (0)
Andy Green11c05bf2015-12-16 18:19:08 +08001523#define lws_handshake_server(_a, _b, _c) (0)
Andy Greenb3d21f12015-12-25 09:12:08 +08001524#define _lws_server_listen_accept_flow_control(a, b) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001525#endif
Andy Green40110e82015-12-14 08:52:03 +08001526
Andy Greendc8a3a82015-12-06 09:15:27 +08001527LWS_EXTERN int
1528lws_get_addresses(struct lws_context *context, void *ads, char *name,
1529 int name_len, char *rip, int rip_len);
Andy Greena654fc02014-04-03 07:16:40 +08001530
Andy Green6a8099b2016-02-21 21:25:48 +08001531LWS_EXTERN int
1532lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1533
Andy Greena654fc02014-04-03 07:16:40 +08001534/*
Alejandro Merycdc97172014-12-04 23:15:27 +01001535 * custom allocator
1536 */
Andy Greene99a83c2016-01-20 16:56:06 +08001537LWS_EXTERN void *
Alejandro Merycdc97172014-12-04 23:15:27 +01001538lws_realloc(void *ptr, size_t size);
1539
Andy Greene99a83c2016-01-20 16:56:06 +08001540LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
Alejandro Merycdc97172014-12-04 23:15:27 +01001541lws_zalloc(size_t size);
1542
1543#define lws_malloc(S) lws_realloc(NULL, S)
1544#define lws_free(P) lws_realloc(P, 0)
Andy Green54806b12015-12-17 17:03:59 +08001545#define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
Alejandro Merycdc97172014-12-04 23:15:27 +01001546
Andy Greendc8a3a82015-12-06 09:15:27 +08001547/* lws_plat_ */
Andy Greena654fc02014-04-03 07:16:40 +08001548LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001549lws_plat_delete_socket_from_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001550 struct lws *wsi, int m);
Andy Greena654fc02014-04-03 07:16:40 +08001551LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001552lws_plat_insert_socket_into_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001553 struct lws *wsi);
Andy Greena654fc02014-04-03 07:16:40 +08001554LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001555lws_plat_service_periodic(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001556
1557LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001558lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1559 struct lws_pollfd *pfd);
Andy Greena654fc02014-04-03 07:16:40 +08001560LWS_EXTERN int
1561lws_plat_context_early_init(void);
1562LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001563lws_plat_context_early_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001564LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001565lws_plat_context_late_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001566LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001567lws_poll_listen_fd(struct lws_pollfd *fd);
Andy Greena654fc02014-04-03 07:16:40 +08001568LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001569lws_plat_service(struct lws_context *context, int timeout_ms);
Andy Greend3a55052016-01-19 03:34:24 +08001570LWS_EXTERN LWS_VISIBLE int
1571lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
Andy Greena654fc02014-04-03 07:16:40 +08001572LWS_EXTERN int
Andy Green4386e362015-12-10 07:14:16 +08001573lws_plat_init(struct lws_context *context,
1574 struct lws_context_creation_info *info);
Andy Greena654fc02014-04-03 07:16:40 +08001575LWS_EXTERN void
1576lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1577LWS_EXTERN unsigned long long
1578time_in_microseconds(void);
Andy Greene99a83c2016-01-20 16:56:06 +08001579LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
Andy Greena654fc02014-04-03 07:16:40 +08001580lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
Andy Green8c0d3c02015-11-02 20:34:12 +08001581
Andy Greene99a83c2016-01-20 16:56:06 +08001582LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green86c1ef12015-12-30 11:43:36 +08001583lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
1584
Andy Green8c0d3c02015-11-02 20:34:12 +08001585#ifdef __cplusplus
1586};
1587#endif