blob: bb1fbd2a71b711881ba044bbfc07100d5e6d70fd [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 Green4b85c1d2015-12-04 11:08:32 +0800434struct lws_protocols;
435struct lws;
Andy Greene92cd172011-01-19 13:11:55 +0000436
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800437#ifdef LWS_USE_LIBEV
438struct lws_io_watcher {
439 struct ev_io watcher;
Andy Green4b85c1d2015-12-04 11:08:32 +0800440 struct lws_context* context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800441};
442
443struct lws_signal_watcher {
444 struct ev_signal watcher;
Andy Green4b85c1d2015-12-04 11:08:32 +0800445 struct lws_context* context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800446};
447#endif /* LWS_USE_LIBEV */
448
Bud Davis229bfec2015-01-30 10:13:01 +0800449#ifdef _WIN32
450#define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
Andy Green3ef579b2015-12-04 09:23:56 +0800451struct lws_fd_hashtable {
Andy Green4b85c1d2015-12-04 11:08:32 +0800452 struct lws **wsi;
Bud Davis229bfec2015-01-30 10:13:01 +0800453 int length;
454};
455#endif
456
Andy Green3df58002015-12-25 12:44:12 +0800457/*
458 * This is totally opaque to code using the library. It's exported as a
459 * forward-reference pointer-only declaration; the user can use the pointer with
460 * other APIs to get information out of it.
461 */
462
463struct lws_fragments {
464 unsigned short offset;
465 unsigned short len;
466 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
467};
468
469/*
470 * these are assigned from a pool held in the context.
471 * Both client and server mode uses them for http header analysis
472 */
473
474struct allocated_headers {
Andy Greende132b92015-12-25 13:48:20 +0800475 char *data; /* prepared by context init to point to dedicated storage */
476 /*
477 * the randomly ordered fragments, indexed by frag_index and
478 * lws_fragments->nfrag for continuation.
479 */
480 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
Andy Green3df58002015-12-25 12:44:12 +0800481 /*
482 * for each recognized token, frag_index says which frag[] his data
483 * starts in (0 means the token did not appear)
484 * the actual header data gets dumped as it comes in, into data[]
485 */
486 unsigned char frag_index[WSI_TOKEN_COUNT];
Andy Green3df58002015-12-25 12:44:12 +0800487#ifndef LWS_NO_CLIENT
488 char initial_handshake_hash_base64[30];
489 unsigned short c_port;
490#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800491
492 unsigned short pos;
493 unsigned char in_use;
494 unsigned char nfrag;
Andy Green3df58002015-12-25 12:44:12 +0800495};
496
Andy Green4b85c1d2015-12-04 11:08:32 +0800497struct lws_context {
Andy Greenaa775fd2015-12-26 08:56:58 +0800498 time_t last_timeout_check_s;
499 struct lws_plat_file_ops fops;
Patrick Gansterer0fc37b62014-03-28 15:44:56 +0100500#ifdef _WIN32
501 WSAEVENT *events;
502#endif
Andy Green4b85c1d2015-12-04 11:08:32 +0800503 struct lws_pollfd *fds;
Bud Davis229bfec2015-01-30 10:13:01 +0800504#ifdef _WIN32
505/* different implementation between unix and windows */
Andy Green3ef579b2015-12-04 09:23:56 +0800506 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
Bud Davis229bfec2015-01-30 10:13:01 +0800507#else
Andy Green4b85c1d2015-12-04 11:08:32 +0800508 struct lws **lws_lookup; /* fd to wsi */
Bud Davis229bfec2015-01-30 10:13:01 +0800509#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800510#ifdef LWS_USE_LIBEV
511 struct ev_loop* io_loop;
512 struct lws_io_watcher w_accept;
513 struct lws_signal_watcher w_sigint;
Andrew Canadaya8f47c92015-04-26 22:50:59 -0400514 lws_ev_signal_cb* lws_ev_sigint_cb;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800515#endif /* LWS_USE_LIBEV */
Mattias Lundberg03bb8f92014-02-18 10:06:57 +0100516 const char *iface;
Andy Greenaa775fd2015-12-26 08:56:58 +0800517 const struct lws_token_limits *token_limits;
518 void *user_space;
519#ifndef LWS_NO_SERVER
520 struct lws *wsi_listening;
521#endif
522 const struct lws_protocols *protocols;
523 void *http_header_data;
524 struct allocated_headers *ah_pool;
525#ifdef LWS_OPENSSL_SUPPORT
526 SSL_CTX *ssl_ctx;
527 SSL_CTX *ssl_client_ctx;
528 struct lws *pending_read_list; /* linked list */
529#endif
530#ifndef LWS_NO_EXTENSIONS
531 const struct lws_extension *extensions;
532#endif
Andy Green44eee682011-02-10 09:32:24 +0000533
Andy Greenb8b247d2013-01-22 07:20:08 +0800534 /*
535 * usable by anything in the service code, but only if the scope
536 * does not last longer than the service action (since next service
537 * of any socket can likewise use it and overwrite)
538 */
Andy Green54806b12015-12-17 17:03:59 +0800539 unsigned char serv_buf[LWS_MAX_SOCKET_IO_BUF];
Andy Greenaa775fd2015-12-26 08:56:58 +0800540 char http_proxy_address[128];
541 char proxy_basic_auth_token[128];
542 char canonical_hostname[128];
543#ifdef LWS_LATENCY
544 unsigned long worst_latency;
545 char worst_latency_info[256];
546#endif
Andy Greenb8b247d2013-01-22 07:20:08 +0800547
Andy Greenaa775fd2015-12-26 08:56:58 +0800548 lws_sockfd_type lserv_fd;
549
550 int fds_count;
551 int max_fds;
552 int listen_port;
553#ifdef LWS_USE_LIBEV
554 int use_ev_sigint;
555#endif
Andy Green24cba922013-01-19 13:56:10 +0800556 int started_with_parent;
557
Andy Green44eee682011-02-10 09:32:24 +0000558 int fd_random;
Andy Green54806b12015-12-17 17:03:59 +0800559 int lserv_mod;
560 int lserv_count;
Andy Green54806b12015-12-17 17:03:59 +0800561 int lserv_seen;
Andy Greenaa775fd2015-12-26 08:56:58 +0800562 unsigned int http_proxy_port;
563 unsigned int options;
Andy Green6ee372f2012-04-09 15:09:01 +0800564
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100565 /*
566 * set to the Thread ID that's doing the service loop just before entry
567 * to poll indicates service thread likely idling in poll()
568 * volatile because other threads may check it as part of processing
569 * for pollfd event change.
570 */
571 volatile int service_tid;
Andy Greenc35b36b2015-12-24 13:00:54 +0800572 int service_tid_detected;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100573#ifndef _WIN32
574 int dummy_pipe_fds[2];
575#endif
576
Andy Greenaa775fd2015-12-26 08:56:58 +0800577 int count_protocols;
Andy Greena690cd02013-02-09 12:25:31 +0800578 int ka_time;
579 int ka_probes;
580 int ka_interval;
581
Andy Greenb45993c2010-12-18 15:13:50 +0000582#ifdef LWS_OPENSSL_SUPPORT
583 int use_ssl;
James Devine5b34c972013-12-14 11:41:29 +0800584 int allow_non_ssl_on_ssl_port;
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +0800585 unsigned int user_supplied_ssl_ctx:1;
Andy Greenaa775fd2015-12-26 08:56:58 +0800586#define lws_ssl_anybody_has_buffered_read(ctx) \
587 (ctx->use_ssl && ctx->pending_read_list)
Andy Green52815602015-01-29 08:36:18 +0800588#else
589#define lws_ssl_anybody_has_buffered_read(ctx) (0)
Andy Greenb45993c2010-12-18 15:13:50 +0000590#endif
Andy Green40110e82015-12-14 08:52:03 +0800591
Andy Green3df58002015-12-25 12:44:12 +0800592 short max_http_header_data;
593 short max_http_header_pool;
594 short ah_count_in_use;
Andy Greenb45993c2010-12-18 15:13:50 +0000595};
596
Andy Greena717df22014-04-11 13:14:37 +0800597enum {
598 LWS_EV_READ = (1 << 0),
599 LWS_EV_WRITE = (1 << 1),
600 LWS_EV_START = (1 << 2),
601 LWS_EV_STOP = (1 << 3),
602};
603
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800604#ifdef LWS_USE_LIBEV
605#define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
Andy Greena717df22014-04-11 13:14:37 +0800606LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
607LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800608lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
Andy Greena717df22014-04-11 13:14:37 +0800609LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800610lws_libev_io(struct lws *wsi, int flags);
Andy Greena717df22014-04-11 13:14:37 +0800611LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800612lws_libev_init_fd_table(struct lws_context *context);
Andy Greena717df22014-04-11 13:14:37 +0800613LWS_EXTERN void
Andy Greena01fb522015-12-14 07:51:15 +0800614lws_libev_run(const struct lws_context *context);
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800615#else
616#define LWS_LIBEV_ENABLED(context) (0)
Andy Green8c0d3c02015-11-02 20:34:12 +0800617#ifdef LWS_POSIX
Andy Greena717df22014-04-11 13:14:37 +0800618#define lws_feature_status_libev(_a) \
619 lwsl_notice("libev support not compiled in\n")
Andy Green8c0d3c02015-11-02 20:34:12 +0800620#else
621#define lws_feature_status_libev(_a)
622#endif
Andy Green11c05bf2015-12-16 18:19:08 +0800623#define lws_libev_accept(_a, _b) ((void) 0)
624#define lws_libev_io(_a, _b) ((void) 0)
Andy Greena717df22014-04-11 13:14:37 +0800625#define lws_libev_init_fd_table(_a) (0)
vpeter47c00fc32014-04-27 13:35:28 +0200626#define lws_libev_run(_a) ((void) 0)
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800627#endif
628
Andy Green055f2972014-03-24 16:09:25 +0800629#ifdef LWS_USE_IPV6
Andy Greendc8a3a82015-12-06 09:15:27 +0800630#define LWS_IPV6_ENABLED(context) \
631 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
James Devine3f13ea22014-03-24 16:09:25 +0800632#else
633#define LWS_IPV6_ENABLED(context) (0)
634#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800635
Andy Greenb1a9e502013-11-10 15:15:21 +0800636enum uri_path_states {
637 URIPS_IDLE,
638 URIPS_SEEN_SLASH,
639 URIPS_SEEN_SLASH_DOT,
640 URIPS_SEEN_SLASH_DOT_DOT,
641};
642
643enum uri_esc_states {
644 URIES_IDLE,
645 URIES_SEEN_PERCENT,
646 URIES_SEEN_PERCENT_H1,
647};
Andy Greenf3d3b402011-02-09 07:16:34 +0000648
Andy Green024eb6c2014-10-08 12:00:53 +0800649/* notice that these union members:
Andy Green40110e82015-12-14 08:52:03 +0800650 *
Andy Green024eb6c2014-10-08 12:00:53 +0800651 * hdr
652 * http
653 * http2
Andy Green40110e82015-12-14 08:52:03 +0800654 *
Andy Green024eb6c2014-10-08 12:00:53 +0800655 * all have a pointer to allocated_headers struct as their first member.
Andy Green40110e82015-12-14 08:52:03 +0800656 *
Andy Green024eb6c2014-10-08 12:00:53 +0800657 * It means for allocated_headers access, the three union paths can all be
Peter Pentchevbb085da2015-12-03 15:55:11 +0200658 * used interchangeably to access the same data
Andy Green024eb6c2014-10-08 12:00:53 +0800659 */
660
Andy Green84fd9492013-11-09 11:40:32 +0800661struct _lws_http_mode_related {
Andy Green024eb6c2014-10-08 12:00:53 +0800662 /* MUST be first in struct */
Andy Green84fd9492013-11-09 11:40:32 +0800663 struct allocated_headers *ah; /* mirroring _lws_header_related */
Andy Green84fd9492013-11-09 11:40:32 +0800664 unsigned long filepos;
665 unsigned long filelen;
Andy Greenaa775fd2015-12-26 08:56:58 +0800666 lws_filefd_type fd;
kapejodce64fb02013-11-19 13:38:16 +0100667
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400668 enum http_version request_version;
669 enum http_connection_type connection_type;
Andy Green2cd30742015-11-02 13:10:33 +0800670 unsigned int content_length;
671 unsigned int content_remain;
Andy Green84fd9492013-11-09 11:40:32 +0800672};
673
Andy Green024eb6c2014-10-08 12:00:53 +0800674#ifdef LWS_USE_HTTP2
675
676enum lws_http2_settings {
677 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
678 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
679 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
680 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
681 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
682 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
Andy Green40110e82015-12-14 08:52:03 +0800683
Andy Green024eb6c2014-10-08 12:00:53 +0800684 LWS_HTTP2_SETTINGS__COUNT /* always last */
Andy Greena54f2322014-09-30 09:43:14 +0800685};
686
Andy Green024eb6c2014-10-08 12:00:53 +0800687enum lws_http2_wellknown_frame_types {
688 LWS_HTTP2_FRAME_TYPE_DATA,
689 LWS_HTTP2_FRAME_TYPE_HEADERS,
690 LWS_HTTP2_FRAME_TYPE_PRIORITY,
691 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
692 LWS_HTTP2_FRAME_TYPE_SETTINGS,
693 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
694 LWS_HTTP2_FRAME_TYPE_PING,
695 LWS_HTTP2_FRAME_TYPE_GOAWAY,
696 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
697 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
Andy Green40110e82015-12-14 08:52:03 +0800698
Andy Green024eb6c2014-10-08 12:00:53 +0800699 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
700};
701
Andy Green91b05892014-10-17 08:38:44 +0800702enum lws_http2_flags {
703 LWS_HTTP2_FLAG_END_STREAM = 1,
704 LWS_HTTP2_FLAG_END_HEADERS = 4,
705 LWS_HTTP2_FLAG_PADDED = 8,
706 LWS_HTTP2_FLAG_PRIORITY = 0x20,
707
708 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
709};
710
Andy Green024eb6c2014-10-08 12:00:53 +0800711#define LWS_HTTP2_STREAM_ID_MASTER 0
712#define LWS_HTTP2_FRAME_HEADER_LENGTH 9
713#define LWS_HTTP2_SETTINGS_LENGTH 6
714
715struct http2_settings {
716 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
717};
718
Andy Greenecc2e722014-10-09 16:57:47 +0800719enum http2_hpack_state {
Andy Green40110e82015-12-14 08:52:03 +0800720
Andy Green200f3852014-10-18 12:23:05 +0800721 /* optional before first header block */
722 HPKS_OPT_PADDING,
723 HKPS_OPT_E_DEPENDENCY,
724 HKPS_OPT_WEIGHT,
Andy Green40110e82015-12-14 08:52:03 +0800725
Andy Green200f3852014-10-18 12:23:05 +0800726 /* header block */
Andy Greenecc2e722014-10-09 16:57:47 +0800727 HPKS_TYPE,
Andy Green40110e82015-12-14 08:52:03 +0800728
Andy Green2add6342014-10-12 08:38:16 +0800729 HPKS_IDX_EXT,
Andy Green40110e82015-12-14 08:52:03 +0800730
Andy Greenecc2e722014-10-09 16:57:47 +0800731 HPKS_HLEN,
732 HPKS_HLEN_EXT,
733
734 HPKS_DATA,
Andy Green40110e82015-12-14 08:52:03 +0800735
Andy Green200f3852014-10-18 12:23:05 +0800736 /* optional after last header block */
737 HKPS_OPT_DISCARD_PADDING,
Andy Greenecc2e722014-10-09 16:57:47 +0800738};
739
Andy Green2add6342014-10-12 08:38:16 +0800740enum http2_hpack_type {
741 HPKT_INDEXED_HDR_7,
742 HPKT_INDEXED_HDR_6_VALUE_INCR,
743 HPKT_LITERAL_HDR_VALUE_INCR,
744 HPKT_INDEXED_HDR_4_VALUE,
745 HPKT_LITERAL_HDR_VALUE,
746 HPKT_SIZE_5
747};
748
Andy Green200f3852014-10-18 12:23:05 +0800749struct hpack_dt_entry {
750 int token; /* additions that don't map to a token are ignored */
751 int arg_offset;
752 int arg_len;
753};
754
755struct hpack_dynamic_table {
756 struct hpack_dt_entry *entries;
757 char *args;
758 int pos;
759 int next;
760 int num_entries;
761 int args_length;
762};
763
Andy Green024eb6c2014-10-08 12:00:53 +0800764struct _lws_http2_related {
Andy Green40110e82015-12-14 08:52:03 +0800765 /*
Andy Green024eb6c2014-10-08 12:00:53 +0800766 * having this first lets us also re-use all HTTP union code
767 * and in turn, http_mode_related has allocated headers in right
768 * place so we can use the header apis on the wsi directly still
769 */
770 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
771
772 struct http2_settings my_settings;
773 struct http2_settings peer_settings;
Andy Green40110e82015-12-14 08:52:03 +0800774
Andy Green4b85c1d2015-12-04 11:08:32 +0800775 struct lws *parent_wsi;
776 struct lws *next_child_wsi;
Andy Green024eb6c2014-10-08 12:00:53 +0800777
Andy Green200f3852014-10-18 12:23:05 +0800778 struct hpack_dynamic_table *hpack_dyn_table;
Andy Greenaa775fd2015-12-26 08:56:58 +0800779 struct lws *stream_wsi;
780 unsigned char ping_payload[8];
781 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
Andy Green40110e82015-12-14 08:52:03 +0800782
Andy Green024eb6c2014-10-08 12:00:53 +0800783 unsigned int count;
Andy Green024eb6c2014-10-08 12:00:53 +0800784 unsigned int length;
785 unsigned int stream_id;
Andy Greenaa775fd2015-12-26 08:56:58 +0800786 enum http2_hpack_state hpack;
787 enum http2_hpack_type hpack_type;
788 unsigned int header_index;
789 unsigned int hpack_len;
790 unsigned int hpack_e_dep;
791 int tx_credit;
792 unsigned int my_stream_id;
793 unsigned int child_count;
794 int my_priority;
795
Andy Green91b05892014-10-17 08:38:44 +0800796 unsigned int END_STREAM:1;
797 unsigned int END_HEADERS:1;
Andy Green1cea5812014-10-19 07:36:20 +0800798 unsigned int send_END_STREAM:1;
Andy Green7df53c52014-10-22 15:37:28 +0800799 unsigned int GOING_AWAY;
800 unsigned int requested_POLLOUT:1;
Andy Green97ee57f2014-10-29 09:39:08 +0800801 unsigned int waiting_tx_credit:1;
Andy Greenecc2e722014-10-09 16:57:47 +0800802 unsigned int huff:1;
803 unsigned int value:1;
Andy Green40110e82015-12-14 08:52:03 +0800804
Andy Greenaa775fd2015-12-26 08:56:58 +0800805 unsigned short round_robin_POLLOUT;
806 unsigned short count_POLLOUT_children;
807 unsigned short hpack_pos;
808
809 unsigned char type;
810 unsigned char flags;
811 unsigned char frame_state;
812 unsigned char padding;
813 unsigned char hpack_m;
Andy Green024eb6c2014-10-08 12:00:53 +0800814 unsigned char initialized;
Andy Green024eb6c2014-10-08 12:00:53 +0800815};
816
Andy Green7df53c52014-10-22 15:37:28 +0800817#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
Andy Green024eb6c2014-10-08 12:00:53 +0800818
819#endif
820
Andy Green623a98d2013-01-21 11:04:23 +0800821struct _lws_header_related {
Andy Green024eb6c2014-10-08 12:00:53 +0800822 /* MUST be first in struct */
Andy Green16ab3182013-02-10 18:02:31 +0800823 struct allocated_headers *ah;
Andy Greenb1a9e502013-11-10 15:15:21 +0800824 enum uri_path_states ups;
825 enum uri_esc_states ues;
Andy Greende132b92015-12-25 13:48:20 +0800826 short lextable_pos;
827 unsigned short current_token_limit;
Andy Greenb1a9e502013-11-10 15:15:21 +0800828 char esc_stash;
Andy Green3ba035d2015-12-18 15:40:03 +0800829 char post_literal_equal;
Andy Greende132b92015-12-25 13:48:20 +0800830 unsigned char parser_state; /* enum lws_token_indexes */
Andy Green623a98d2013-01-21 11:04:23 +0800831};
832
833struct _lws_websocket_related {
Andy Green54495112013-02-06 21:10:16 +0900834 char *rx_user_buffer;
Andy Greende132b92015-12-25 13:48:20 +0800835 size_t rx_packet_length;
Andy Green2cd30742015-11-02 13:10:33 +0800836 unsigned int rx_user_buffer_head;
Andy Green54806b12015-12-17 17:03:59 +0800837 unsigned char mask_nonce[4];
Andy Greende132b92015-12-25 13:48:20 +0800838 unsigned char ping_payload_buf[128 - 4 + LWS_SEND_BUFFER_PRE_PADDING]; /* control opc == < 124 */
Andy Greende132b92015-12-25 13:48:20 +0800839 short close_reason; /* enum lws_close_status */
Andy Greenaa775fd2015-12-26 08:56:58 +0800840 unsigned char ping_payload_len;
Andy Green623a98d2013-01-21 11:04:23 +0800841 unsigned char frame_mask_index;
Andy Green623a98d2013-01-21 11:04:23 +0800842 unsigned char opcode;
Andy Green623a98d2013-01-21 11:04:23 +0800843 unsigned char rsv;
Andy Greende132b92015-12-25 13:48:20 +0800844
845 unsigned int final:1;
Andy Greend91d5e82013-02-10 16:00:47 +0800846 unsigned int frame_is_binary:1;
Andy Greend91d5e82013-02-10 16:00:47 +0800847 unsigned int all_zero_nonce:1;
Andy Greend91d5e82013-02-10 16:00:47 +0800848 unsigned int this_frame_masked:1;
Andy Green1f4267b2013-10-17 08:09:19 +0800849 unsigned int inside_frame:1; /* next write will be more of frame */
850 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
Andy Green40d5abc2015-04-17 20:29:58 +0800851 unsigned int payload_is_close:1; /* process as PONG, but it is close */
Andy Greenba38a7e2015-12-25 13:14:09 +0800852 unsigned int ping_pending_flag:1;
Andy Green623a98d2013-01-21 11:04:23 +0800853};
854
Andy Green4b85c1d2015-12-04 11:08:32 +0800855struct lws {
Andy Green623a98d2013-01-21 11:04:23 +0800856
Andy Greenaa775fd2015-12-26 08:56:58 +0800857 /* structs */
858 /* members with mutually exclusive lifetimes are unionized */
859
860 union u {
861 struct _lws_http_mode_related http;
862#ifdef LWS_USE_HTTP2
863 struct _lws_http2_related http2;
864#endif
865 struct _lws_header_related hdr;
866 struct _lws_websocket_related ws;
867 } u;
868
Andy Green623a98d2013-01-21 11:04:23 +0800869 /* lifetime members */
870
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800871#ifdef LWS_USE_LIBEV
Andy Green40110e82015-12-14 08:52:03 +0800872 struct lws_io_watcher w_read;
873 struct lws_io_watcher w_write;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800874#endif /* LWS_USE_LIBEV */
Andy Greende132b92015-12-25 13:48:20 +0800875 time_t pending_timeout_limit;
Andy Greenaa775fd2015-12-26 08:56:58 +0800876
877 /* pointers */
878
Andy Green40110e82015-12-14 08:52:03 +0800879 struct lws_context *context;
Andy Green4b85c1d2015-12-04 11:08:32 +0800880 const struct lws_protocols *protocol;
Andy Greende132b92015-12-25 13:48:20 +0800881 void *user_space;
882 /* rxflow handling */
883 unsigned char *rxflow_buffer;
884 /* truncated send handling */
885 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
Andy Green3182ece2013-01-20 17:08:31 +0800886#ifndef LWS_NO_EXTENSIONS
Andy Greend2ac22c2015-12-11 10:45:35 +0800887 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green6ee372f2012-04-09 15:09:01 +0800888 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green3182ece2013-01-20 17:08:31 +0800889#endif
Andy Greende132b92015-12-25 13:48:20 +0800890#ifdef LWS_OPENSSL_SUPPORT
891 SSL *ssl;
892 BIO *client_bio;
893 struct lws *pending_read_list_prev, *pending_read_list_next;
894#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800895#ifdef LWS_LATENCY
896 unsigned long action_start;
897 unsigned long latency_start;
898#endif
899 /* pointer / int */
Andy Green3b193862015-11-02 13:13:44 +0800900 lws_sockfd_type sock;
Andy Greende132b92015-12-25 13:48:20 +0800901
Andy Greenaa775fd2015-12-26 08:56:58 +0800902 /* ints */
Andy Greende132b92015-12-25 13:48:20 +0800903 enum lws_pending_protocol_send pps;
Andy Greendfb23042013-01-17 12:26:48 +0800904 int position_in_fds_table;
Andy Green024eb6c2014-10-08 12:00:53 +0800905 int rxflow_len;
906 int rxflow_pos;
Andy Green54806b12015-12-17 17:03:59 +0800907 unsigned int trunc_alloc_len; /* size of malloc */
908 unsigned int trunc_offset; /* where we are in terms of spilling */
909 unsigned int trunc_len; /* how much is buffered */
Andy Green2764eba2013-12-09 14:16:17 +0800910
Andy Greende132b92015-12-25 13:48:20 +0800911 unsigned int hdr_parsing_completed:1;
912 unsigned int user_space_externally_allocated:1;
913 unsigned int socket_is_permanently_unusable:1;
914 unsigned int rxflow_change_to:2;
915#ifndef LWS_NO_EXTENSIONS
916 unsigned int extension_data_pending:1;
917#endif
918#ifdef LWS_OPENSSL_SUPPORT
919 unsigned int use_ssl:2;
920 unsigned int upgraded:1;
921#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800922#ifdef _WIN32
923 unsigned int sock_send_blocking:1;
924#endif
Andy Greende132b92015-12-25 13:48:20 +0800925
Andy Greenaa775fd2015-12-26 08:56:58 +0800926 /* chars */
Andy Greende132b92015-12-25 13:48:20 +0800927#ifndef LWS_NO_EXTENSIONS
928 unsigned char count_active_extensions;
929#endif
930 unsigned char ietf_spec_revision;
931 char mode; /* enum connection_mode */
932 char state; /* enum lws_connection_states */
933 char lws_rx_parse_state; /* enum lws_rx_parse_state */
934 char rx_frame_type; /* enum lws_write_protocol */
935 char pending_timeout; /* enum pending_timeout */
Andy Green7c212cc2010-11-08 20:20:42 +0000936};
937
Andy Green3d67f512014-04-03 07:29:50 +0800938LWS_EXTERN int log_level;
939
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800940LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +0800941lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
Andy Green508946c2013-02-12 10:19:08 +0800942
Andy Green34f3dd22014-04-03 07:42:50 +0800943LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +0800944remove_wsi_socket_from_fds(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +0800945LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800946lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
Andy Green34f3dd22014-04-03 07:42:50 +0800947
Andy Greend636e352013-01-29 12:36:17 +0800948#ifndef LWS_LATENCY
Andy Greendc8a3a82015-12-06 09:15:27 +0800949static inline void
950lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
951 int ret, int completion) {
Andy Green40110e82015-12-14 08:52:03 +0800952 do {
953 (void)context; (void)wsi; (void)action; (void)ret;
954 (void)completion;
Andy Greendc8a3a82015-12-06 09:15:27 +0800955 } while (0);
956}
957static inline void
958lws_latency_pre(struct lws_context *context, struct lws *wsi) {
959 do { (void)context; (void)wsi; } while (0);
960}
Andy Greend636e352013-01-29 12:36:17 +0800961#else
962#define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
963extern void
Andy Greendc8a3a82015-12-06 09:15:27 +0800964lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
965 int ret, int completion);
Andy Greend636e352013-01-29 12:36:17 +0800966#endif
967
Andy Greendc8a3a82015-12-06 09:15:27 +0800968LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +0800969lws_set_protocol_write_pending(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +0800970 enum lws_pending_protocol_send pend);
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800971LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800972lws_client_rx_sm(struct lws *wsi, unsigned char c);
Andy Green4739e5c2011-01-22 12:51:57 +0000973
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800974LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +0800975lws_parse(struct lws *wsi, unsigned char c);
Andy Greenb45993c2010-12-18 15:13:50 +0000976
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800977LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +0800978lws_http_action(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +0800979
980LWS_EXTERN int
Andy Greendf736162011-01-18 15:39:02 +0000981lws_b64_selftest(void);
Andy Greenbfb051f2011-02-09 08:49:14 +0000982
Andy Green2cd30742015-11-02 13:10:33 +0800983#if defined(_WIN32) || defined(MBED_OPERATORS)
Andy Green4b85c1d2015-12-04 11:08:32 +0800984LWS_EXTERN struct lws *
Andy Green1fa76852015-12-14 11:17:16 +0800985wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
Andy Green0d338332011-02-12 11:57:43 +0000986
Andy Green40110e82015-12-14 08:52:03 +0800987LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800988insert_wsi(struct lws_context *context, struct lws *wsi);
Bud Davis229bfec2015-01-30 10:13:01 +0800989
990LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800991delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
Bud Davis229bfec2015-01-30 10:13:01 +0800992#else
Andy Green40110e82015-12-14 08:52:03 +0800993#define wsi_from_fd(A,B) A->lws_lookup[B]
Bud Davis229bfec2015-01-30 10:13:01 +0800994#define insert_wsi(A,B) A->lws_lookup[B->sock]=B
995#define delete_from_fd(A,B) A->lws_lookup[B]=0
996#endif
997
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800998LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +0800999insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
Darin Willitsc19456f2011-02-14 17:52:39 +00001000
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001001LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001002lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
Andy Greend44bf7f2011-03-06 10:29:38 +00001003
Andy Green95a7b5d2011-03-06 10:29:39 +00001004
Andy Green1c6e1422013-02-20 19:11:31 +08001005LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001006lws_service_timeout_check(struct lws *wsi, unsigned int sec);
Andy Greena41314f2011-05-23 10:00:03 +01001007
Andy Green4b85c1d2015-12-04 11:08:32 +08001008LWS_EXTERN struct lws *
Andy Green6b5de702015-12-15 21:15:58 +08001009lws_client_connect_2(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001010
Andy Green4b85c1d2015-12-04 11:08:32 +08001011LWS_EXTERN struct lws *
1012lws_create_new_server_wsi(struct lws_context *context);
Andy Greena41314f2011-05-23 10:00:03 +01001013
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001014LWS_EXTERN char *
Andy Green6b5de702015-12-15 21:15:58 +08001015lws_generate_client_handshake(struct lws *wsi, char *pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001016
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001017LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001018lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
Andy Green91b05892014-10-17 08:38:44 +08001019
Andy Greencdb9bf92014-04-12 10:07:02 +08001020/*
1021 * EXTENSIONS
1022 */
1023
Andy Green3182ece2013-01-20 17:08:31 +08001024#ifndef LWS_NO_EXTENSIONS
Andy Greencdb9bf92014-04-12 10:07:02 +08001025LWS_VISIBLE void
1026lws_context_init_extensions(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001027 struct lws_context *context);
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001028LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001029lws_any_extension_handled(struct lws *wsi,
Andy Green4b85c1d2015-12-04 11:08:32 +08001030 enum lws_extension_callback_reasons r,
Andy Green6ee372f2012-04-09 15:09:01 +08001031 void *v, size_t len);
Andy Greena41314f2011-05-23 10:00:03 +01001032
Andy Green2c24ec02014-04-02 19:45:42 +08001033LWS_EXTERN int
Andy Green54806b12015-12-17 17:03:59 +08001034lws_ext_cb_wsi_active_exts(struct lws *wsi, int reason,
Andy Greendc8a3a82015-12-06 09:15:27 +08001035 void *buf, int len);
Andy Green2c24ec02014-04-02 19:45:42 +08001036LWS_EXTERN int
Andy Green54806b12015-12-17 17:03:59 +08001037lws_ext_cb_all_exts(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001038 struct lws *wsi, int reason,
1039 void *arg, int len);
Andy Green2c24ec02014-04-02 19:45:42 +08001040#else
Andy Green6b5de702015-12-15 21:15:58 +08001041#define lws_any_extension_handled(_a, _b, _c, _d) (0)
Andy Green54806b12015-12-17 17:03:59 +08001042#define lws_ext_cb_wsi_active_exts(_a, _b, _c, _d) (0)
1043#define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
Andy Greenb49a9952014-04-03 10:11:04 +08001044#define lws_issue_raw_ext_access lws_issue_raw
Andy Greencdb9bf92014-04-12 10:07:02 +08001045#define lws_context_init_extensions(_a, _b)
Andy Green3182ece2013-01-20 17:08:31 +08001046#endif
Andy Greena41314f2011-05-23 10:00:03 +01001047
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001048LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001049lws_client_interpret_server_handshake(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001050
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001051LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001052lws_rx_sm(struct lws *wsi, unsigned char c);
Andy Greena41314f2011-05-23 10:00:03 +01001053
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001054LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001055lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
Andy Green09226502011-05-28 10:19:19 +01001056
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001057LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001058_lws_rx_flow_control(struct lws *wsi);
Andy Green706961d2013-01-17 16:50:35 +08001059
Andy Green44c11612014-11-08 11:18:47 +08001060LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001061lws_union_transition(struct lws *wsi, enum connection_mode mode);
Andy Green44c11612014-11-08 11:18:47 +08001062
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001063LWS_EXTERN int
Andy Green00c6d152015-12-17 07:54:44 +08001064user_callback_handle_rxflow(callback_function, struct lws *wsi,
1065 enum lws_callback_reasons reason, void *user,
1066 void *in, size_t len);
Andy Green024eb6c2014-10-08 12:00:53 +08001067#ifdef LWS_USE_HTTP2
Andy Green4b85c1d2015-12-04 11:08:32 +08001068LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1069struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
Andy Green024eb6c2014-10-08 12:00:53 +08001070LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001071lws_http2_interpret_settings_payload(struct http2_settings *settings,
1072 unsigned char *buf, int len);
Andy Green024eb6c2014-10-08 12:00:53 +08001073LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1074LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001075lws_http2_parser(struct lws *wsi, unsigned char c);
Andy Greendc8a3a82015-12-06 09:15:27 +08001076LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1077 struct lws *wsi);
1078LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1079 unsigned int sid, unsigned int len,
1080 unsigned char *buf);
Andy Green4b85c1d2015-12-04 11:08:32 +08001081LWS_EXTERN struct lws *
1082lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
Andy Green11c05bf2015-12-16 18:19:08 +08001083LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
Andy Green2add6342014-10-12 08:38:16 +08001084 unsigned char c);
Andy Green917f43a2014-10-12 14:31:47 +08001085LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001086lws_add_http2_header_by_name(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001087 const unsigned char *name,
1088 const unsigned char *value, int length,
1089 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001090LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001091lws_add_http2_header_by_token(struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001092 enum lws_token_indexes token,
Andy Greendc8a3a82015-12-06 09:15:27 +08001093 const unsigned char *value, int length,
1094 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001095LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001096lws_add_http2_header_status(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001097 unsigned int code, unsigned char **p,
Andy Green917f43a2014-10-12 14:31:47 +08001098 unsigned char *end);
Andy Green7df53c52014-10-22 15:37:28 +08001099LWS_EXTERN
Andy Green4b85c1d2015-12-04 11:08:32 +08001100void lws_http2_configure_if_upgraded(struct lws *wsi);
Andy Green7df53c52014-10-22 15:37:28 +08001101#else
1102#define lws_http2_configure_if_upgraded(x)
Andy Green024eb6c2014-10-08 12:00:53 +08001103#endif
Andy Green706961d2013-01-17 16:50:35 +08001104
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001105LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001106lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
Andy Greena690cd02013-02-09 12:25:31 +08001107
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001108LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001109lws_allocate_header_table(struct lws *wsi);
Andy Green16ab3182013-02-10 18:02:31 +08001110
Andrew Canaday37718812014-11-07 11:20:59 +08001111LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001112lws_free_header_table(struct lws *wsi);
Andrew Canaday37718812014-11-07 11:20:59 +08001113
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001114LWS_EXTERN char *
Andy Green4b85c1d2015-12-04 11:08:32 +08001115lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001116
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001117LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001118lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
Andy Greenb5b23192013-02-11 17:13:32 +08001119
Andy Green2af4d5b2013-02-18 16:30:10 +08001120LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001121lws_ensure_user_space(struct lws *wsi);
Andy Green2af4d5b2013-02-18 16:30:10 +08001122
Andy Green158e8042014-04-02 14:25:10 +08001123LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001124lws_change_pollfd(struct lws *wsi, int _and, int _or);
Andy Green91f19d82013-12-21 11:18:34 +08001125
Andy Greenb5b23192013-02-11 17:13:32 +08001126#ifndef LWS_NO_SERVER
Andy Greene38031a2014-04-03 08:24:29 +08001127int lws_context_init_server(struct lws_context_creation_info *info,
Andy Greenaa775fd2015-12-26 08:56:58 +08001128 struct lws_context *context);
Andy Greend7340c12014-04-10 14:08:10 +08001129LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001130handshake_0405(struct lws_context *context, struct lws *wsi);
1131LWS_EXTERN int
1132lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
Andy Greencdb9bf92014-04-12 10:07:02 +08001133LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001134lws_server_get_canonical_hostname(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001135 struct lws_context_creation_info *info);
Andy Greene38031a2014-04-03 08:24:29 +08001136#else
1137#define lws_context_init_server(_a, _b) (0)
Andy Green3ef579b2015-12-04 09:23:56 +08001138#define lws_interpret_incoming_packet(_a, _b, _c) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001139#define lws_server_get_canonical_hostname(_a, _b)
Andy Greenb5b23192013-02-11 17:13:32 +08001140#endif
1141
1142#ifndef LWS_NO_DAEMONIZE
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001143LWS_EXTERN int get_daemonize_pid();
Andy Greencdb9bf92014-04-12 10:07:02 +08001144#else
1145#define get_daemonize_pid() (0)
Andy Greenb5b23192013-02-11 17:13:32 +08001146#endif
1147
Andy Green2cd30742015-11-02 13:10:33 +08001148#if !defined(MBED_OPERATORS)
Andy Greendc8a3a82015-12-06 09:15:27 +08001149LWS_EXTERN int
1150interface_to_sa(struct lws_context *context, const char *ifname,
1151 struct sockaddr_in *addr, size_t addrlen);
Andy Green2cd30742015-11-02 13:10:33 +08001152#endif
Andy Greenb25b85f2014-04-03 23:34:09 +08001153LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1154
Andy Green1a308e42014-04-08 07:26:30 +01001155enum lws_ssl_capable_status {
1156 LWS_SSL_CAPABLE_ERROR = -1,
1157 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1158};
1159
Darin Willitsc19456f2011-02-14 17:52:39 +00001160#ifndef LWS_OPENSSL_SUPPORT
Andy Greencdb9bf92014-04-12 10:07:02 +08001161#define LWS_SSL_ENABLED(context) (0)
Andy Greenc57037a2014-04-03 10:17:00 +08001162#define lws_context_init_server_ssl(_a, _b) (0)
1163#define lws_ssl_destroy(_a)
Andy Green2eedea92014-04-03 14:33:48 +08001164#define lws_context_init_http2_ssl(_a)
Andy Green02138122014-04-06 06:26:35 +01001165#define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1166#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 +02001167#define lws_ssl_pending lws_ssl_pending_no_ssl
Andy Green6b5de702015-12-15 21:15:58 +08001168#define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001169#define lws_ssl_close(_a) (0)
1170#define lws_ssl_context_destroy(_a)
Andy Green6b5de702015-12-15 21:15:58 +08001171#define lws_ssl_remove_wsi_from_buffered_list(_a)
Andy Greenb5b23192013-02-11 17:13:32 +08001172#else
Andy Greencdb9bf92014-04-12 10:07:02 +08001173#define LWS_SSL_ENABLED(context) (context->use_ssl)
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001174LWS_EXTERN int openssl_websocket_private_data_index;
Andy Green02138122014-04-06 06:26:35 +01001175LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001176lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
Andy Green02138122014-04-06 06:26:35 +01001177LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001178lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
Andy Greencdb9bf92014-04-12 10:07:02 +08001179LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001180lws_ssl_pending(struct lws *wsi);
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001181LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001182lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
1183 lws_sockfd_type accept_fd,
Andy Greendc8a3a82015-12-06 09:15:27 +08001184 struct lws_pollfd *pollfd);
Andy Greencdb9bf92014-04-12 10:07:02 +08001185LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001186lws_ssl_close(struct lws *wsi);
Andy Greencdb9bf92014-04-12 10:07:02 +08001187LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001188lws_ssl_context_destroy(struct lws_context *context);
Andy Green52815602015-01-29 08:36:18 +08001189LWS_VISIBLE void
Andy Green6b5de702015-12-15 21:15:58 +08001190lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
Andy Greenc57037a2014-04-03 10:17:00 +08001191#ifndef LWS_NO_SERVER
1192LWS_EXTERN int
1193lws_context_init_server_ssl(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001194 struct lws_context *context);
Andy Greenc57037a2014-04-03 10:17:00 +08001195#else
1196#define lws_context_init_server_ssl(_a, _b) (0)
1197#endif
1198LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001199lws_ssl_destroy(struct lws_context *context);
Andy Green2eedea92014-04-03 14:33:48 +08001200
1201/* HTTP2-related */
1202
1203#ifdef LWS_USE_HTTP2
1204LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001205lws_context_init_http2_ssl(struct lws_context *context);
Andy Green2eedea92014-04-03 14:33:48 +08001206#else
1207#define lws_context_init_http2_ssl(_a)
1208#endif
Darin Willitsc19456f2011-02-14 17:52:39 +00001209#endif
Andy Greena654fc02014-04-03 07:16:40 +08001210
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001211LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001212lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001213
1214LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001215lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001216
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001217LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001218lws_ssl_pending_no_ssl(struct lws *wsi);
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001219
Andy Greena654fc02014-04-03 07:16:40 +08001220#ifndef LWS_NO_CLIENT
Andy Greendc8a3a82015-12-06 09:15:27 +08001221LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1222 struct lws *wsi,
1223 struct lws_pollfd *pollfd);
Andy Greenc57037a2014-04-03 10:17:00 +08001224#ifdef LWS_OPENSSL_SUPPORT
Andy Greendc8a3a82015-12-06 09:15:27 +08001225LWS_EXTERN int
1226lws_context_init_client_ssl(struct lws_context_creation_info *info,
Andy Green4b85c1d2015-12-04 11:08:32 +08001227 struct lws_context *context);
Andy Greene38031a2014-04-03 08:24:29 +08001228#else
Andy Greenc57037a2014-04-03 10:17:00 +08001229 #define lws_context_init_client_ssl(_a, _b) (0)
1230#endif
Andy Greendc8a3a82015-12-06 09:15:27 +08001231LWS_EXTERN int
1232lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1233LWS_EXTERN void
1234lws_decode_ssl_error(void);
Andy Greenc57037a2014-04-03 10:17:00 +08001235#else
1236#define lws_context_init_client_ssl(_a, _b) (0)
Andy Greenaad2eac2014-04-03 09:03:37 +08001237#define lws_handshake_client(_a, _b, _c) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001238#endif
1239#ifndef LWS_NO_SERVER
Andy Greendc8a3a82015-12-06 09:15:27 +08001240LWS_EXTERN int
1241lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1242 struct lws_pollfd *pollfd);
1243LWS_EXTERN int
1244_lws_rx_flow_control(struct lws *wsi);
1245LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001246lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
Andy Greenb3d21f12015-12-25 09:12:08 +08001247LWS_EXTERN int
1248_lws_server_listen_accept_flow_control(struct lws_context *context, int on);
Andy Greend99476b2014-04-03 08:40:05 +08001249#else
Andy Greenb49a9952014-04-03 10:11:04 +08001250#define lws_server_socket_service(_a, _b, _c) (0)
Andy Green62304762015-12-04 08:43:54 +08001251#define _lws_rx_flow_control(_a) (0)
Andy Green11c05bf2015-12-16 18:19:08 +08001252#define lws_handshake_server(_a, _b, _c) (0)
Andy Greenb3d21f12015-12-25 09:12:08 +08001253#define _lws_server_listen_accept_flow_control(a, b) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001254#endif
Andy Green40110e82015-12-14 08:52:03 +08001255
Andy Greendc8a3a82015-12-06 09:15:27 +08001256LWS_EXTERN int
1257lws_get_addresses(struct lws_context *context, void *ads, char *name,
1258 int name_len, char *rip, int rip_len);
Andy Greena654fc02014-04-03 07:16:40 +08001259
1260/*
Alejandro Merycdc97172014-12-04 23:15:27 +01001261 * custom allocator
1262 */
1263LWS_EXTERN void*
1264lws_realloc(void *ptr, size_t size);
1265
1266LWS_EXTERN void*
1267lws_zalloc(size_t size);
1268
1269#define lws_malloc(S) lws_realloc(NULL, S)
1270#define lws_free(P) lws_realloc(P, 0)
Andy Green54806b12015-12-17 17:03:59 +08001271#define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
Alejandro Merycdc97172014-12-04 23:15:27 +01001272
Andy Greendc8a3a82015-12-06 09:15:27 +08001273/* lws_plat_ */
Andy Greena654fc02014-04-03 07:16:40 +08001274LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001275lws_plat_delete_socket_from_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001276 struct lws *wsi, int m);
Andy Greena654fc02014-04-03 07:16:40 +08001277LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001278lws_plat_insert_socket_into_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001279 struct lws *wsi);
Andy Greena654fc02014-04-03 07:16:40 +08001280LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001281lws_plat_service_periodic(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001282
1283LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001284lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1285 struct lws_pollfd *pfd);
Andy Greena654fc02014-04-03 07:16:40 +08001286LWS_EXTERN int
1287lws_plat_context_early_init(void);
1288LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001289lws_plat_context_early_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001290LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001291lws_plat_context_late_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001292LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001293lws_poll_listen_fd(struct lws_pollfd *fd);
Andy Greena654fc02014-04-03 07:16:40 +08001294LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001295lws_plat_service(struct lws_context *context, int timeout_ms);
Andy Greena654fc02014-04-03 07:16:40 +08001296LWS_EXTERN int
Andy Green4386e362015-12-10 07:14:16 +08001297lws_plat_init(struct lws_context *context,
1298 struct lws_context_creation_info *info);
Andy Greena654fc02014-04-03 07:16:40 +08001299LWS_EXTERN void
1300lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1301LWS_EXTERN unsigned long long
1302time_in_microseconds(void);
1303LWS_EXTERN const char *
1304lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
Andy Green8c0d3c02015-11-02 20:34:12 +08001305
1306#ifdef __cplusplus
1307};
1308#endif