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