blob: f475a378628f20c0ddfbdaeeda16e8fc159f8988 [file] [log] [blame]
Andy Green7c212cc2010-11-08 20:20:42 +00001/*
2 * libwebsockets - small server side websockets and web server implementation
Andy Greene77ddd82010-11-13 10:03:47 +00003 *
Andy Green8c1f6022016-01-26 20:56:56 +08004 * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
Andy Green7c212cc2010-11-08 20:20:42 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
Joakim Soderberg91de9332013-02-06 15:30:33 +090021
Joakim Soderberg4c531232013-02-06 15:26:58 +090022#include "lws_config.h"
Roger A. Light7a474b42015-06-26 11:40:54 +020023#include "lws_config_private.h"
Andy Greene40aa9b2014-04-02 21:02:54 +080024
Andy Green6a8099b2016-02-21 21:25:48 +080025
26#if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK)
27#define _GNU_SOURCE
28#endif
29
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020030#ifdef LWS_HAVE_SYS_TYPES_H
Andy Greene40aa9b2014-04-02 21:02:54 +080031#include <sys/types.h>
Joakim Soderberg91de9332013-02-06 15:30:33 +090032#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090033
Andy Green7c212cc2010-11-08 20:20:42 +000034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Patrick Ganstererb13eed42014-03-30 10:19:23 +020037#include <time.h>
Andy Green7c212cc2010-11-08 20:20:42 +000038#include <ctype.h>
Peter Young26757a72013-01-17 10:08:16 +080039#include <limits.h>
Peter Hinz56885f32011-03-02 22:03:47 +000040#include <stdarg.h>
Andy Green7acf76c2016-07-23 14:18:25 +080041#if defined(LWS_WITH_ESP8266)
42#include <user_interface.h>
43#define assert(n)
44
45/* rom-provided stdc functions for free, ensure use these instead of libc ones */
46
47int ets_vsprintf(char *str, const char *format, va_list argptr);
48int ets_vsnprintf(char *buffer, size_t sizeOfBuffer, const char *format, va_list argptr);
49int ets_snprintf(char *str, size_t size, const char *format, ...);
50int ets_sprintf(char *str, const char *format, ...);
51int os_printf_plus(const char *format, ...);
52#undef malloc
53#undef realloc
54#undef free
55void *pvPortMalloc(size_t s, const char *f, int line);
56#define malloc(s) pvPortMalloc(s, "", 0)
57void *pvPortRealloc(void *p, size_t s, const char *f, int line);
58#define realloc(p, s) pvPortRealloc(p, s, "", 0)
59void vPortFree(void *p, const char *f, int line);
60#define free(p) vPortFree(p, "", 0)
61#undef memcpy
62void *ets_memcpy(void *dest, const void *src, size_t n);
63#define memcpy ets_memcpy
64void *ets_memset(void *dest, int v, size_t n);
65#define memset ets_memset
66char *ets_strcpy(char *dest, const char *src);
67#define strcpy ets_strcpy
68char *ets_strncpy(char *dest, const char *src, size_t n);
69#define strncpy ets_strncpy
70char *ets_strstr(const char *haystack, const char *needle);
71#define strstr ets_strstr
72int ets_strcmp(const char *s1, const char *s2);
73int ets_strncmp(const char *s1, const char *s2, size_t n);
74#define strcmp ets_strcmp
75#define strncmp ets_strncmp
76size_t ets_strlen(const char *s);
77#define strlen ets_strlen
78void *ets_memmove(void *dest, const void *src, size_t n);
79#define memmove ets_memmove
80char *ets_strchr(const char *s, int c);
81#define strchr_ets_strchr
82#undef _DEBUG
83#include <osapi.h>
84
85#else
86#define STORE_IN_ROM
Andy Green112f9802015-12-04 07:22:44 +080087#include <assert.h>
Andy Green7acf76c2016-07-23 14:18:25 +080088#endif
Andy Green8c1f6022016-01-26 20:56:56 +080089#if LWS_MAX_SMP > 1
90#include <pthread.h>
91#endif
Peter Hinz56885f32011-03-02 22:03:47 +000092
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +020093#ifdef LWS_HAVE_SYS_STAT_H
Peter Hinz56885f32011-03-02 22:03:47 +000094#include <sys/stat.h>
Patrick Gansterere5720a32014-02-28 00:57:19 +010095#endif
Peter Hinz56885f32011-03-02 22:03:47 +000096
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010097#if defined(WIN32) || defined(_WIN32)
Stephan Eberleb820e2c2015-10-23 08:10:55 +020098#if (WINVER < 0x0501)
99#undef WINVER
100#undef _WIN32_WINNT
101#define WINVER 0x0501
102#define _WIN32_WINNT WINVER
103#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +0900104#define LWS_NO_DAEMONIZE
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100105#define LWS_ERRNO WSAGetLastError()
106#define LWS_EAGAIN WSAEWOULDBLOCK
107#define LWS_EALREADY WSAEALREADY
108#define LWS_EINPROGRESS WSAEINPROGRESS
109#define LWS_EINTR WSAEINTR
110#define LWS_EISCONN WSAEISCONN
111#define LWS_EWOULDBLOCK WSAEWOULDBLOCK
Patrick Gansterer73882e42014-03-29 08:25:58 +0100112#define MSG_NOSIGNAL 0
113#define SHUT_RDWR SD_BOTH
114#define SOL_TCP IPPROTO_TCP
Andy Green8c1f6022016-01-26 20:56:56 +0800115#define SHUT_WR SD_SEND
Joakim Soderberg4c531232013-02-06 15:26:58 +0900116
Andy Green158e8042014-04-02 14:25:10 +0800117#define compatible_close(fd) closesocket(fd)
Andy Greenaa775fd2015-12-26 08:56:58 +0800118#define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
Andy Greenc53f7ca2015-11-14 07:35:27 +0800119#define lws_socket_is_valid(x) (!!x)
Andy Green40110e82015-12-14 08:52:03 +0800120#define LWS_SOCK_INVALID 0
Peter Hinz56885f32011-03-02 22:03:47 +0000121#include <winsock2.h>
Andy Greeneee0d8a2015-12-17 15:15:12 +0800122#include <ws2tcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +0000123#include <windows.h>
Joakim Soderbergd2f5b192014-04-07 11:28:08 +0200124#include <tchar.h>
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200125#ifdef LWS_HAVE_IN6ADDR_H
Andy Green0f58db32014-04-12 11:10:35 +0800126#include <in6addr.h>
127#endif
Joakim Soderbergd2f5b192014-04-07 11:28:08 +0200128#include <mstcpip.h>
Andy Greend6be6772016-05-14 06:49:29 +0800129#include <io.h>
Joakim Soderbergd2f5b192014-04-07 11:28:08 +0200130
131#ifndef __func__
132#define __func__ __FUNCTION__
133#endif
134
OndraCoa709cbb2016-05-05 13:56:07 +0200135#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
Patrick Gansterer6bb4b622014-04-15 18:39:26 +0200136#define vsnprintf _vsnprintf
Andy Green7d22c292016-03-04 10:53:51 +0800137#else
138#ifdef LWS_HAVE__VSNPRINTF
139#define vsnprintf _vsnprintf
140#endif
141#endif
142
143#ifdef LWS_HAVE__SNPRINTF
Andy Greena4967002016-09-15 02:22:57 +0800144#define lws_snprintf _snprintf
Patrick Gansterer6bb4b622014-04-15 18:39:26 +0200145#endif
146
Andy Green158e8042014-04-02 14:25:10 +0800147#else /* not windows --> */
Andy Green8c0d3c02015-11-02 20:34:12 +0800148
Patrick Ganstererb13eed42014-03-30 10:19:23 +0200149#include <fcntl.h>
Patrick Ganstererb13eed42014-03-30 10:19:23 +0200150#include <strings.h>
151#include <unistd.h>
Andy Greene77ddd82010-11-13 10:03:47 +0000152#include <sys/types.h>
Andy Green5f2a8152015-11-02 08:21:08 +0800153#ifndef MBED_OPERATORS
Andy Green8c0d3c02015-11-02 20:34:12 +0800154#ifndef __cplusplus
155#include <errno.h>
156#endif
Andy Green5f2a8152015-11-02 08:21:08 +0800157#include <netdb.h>
158#include <signal.h>
Andy Green7acf76c2016-07-23 14:18:25 +0800159#ifdef LWS_WITH_ESP8266
160#include <sockets.h>
161#define vsnprintf ets_vsnprintf
162#define snprintf ets_snprintf
163#define sprintf ets_sprintf
164#else
Andy Green7c212cc2010-11-08 20:20:42 +0000165#include <sys/socket.h>
Andy Green7acf76c2016-07-23 14:18:25 +0800166#endif
Andy Green1e5a9ad2016-03-20 11:59:53 +0800167#ifdef LWS_WITH_HTTP_PROXY
168#include <hubbub/hubbub.h>
169#include <hubbub/parser.h>
170#endif
Andy Green7acf76c2016-07-23 14:18:25 +0800171#if defined(LWS_BUILTIN_GETIFADDRS)
Andy Greene40aa9b2014-04-02 21:02:54 +0800172 #include <getifaddrs.h>
173#else
Andy Green7acf76c2016-07-23 14:18:25 +0800174 #if !defined(LWS_WITH_ESP8266)
Andy Greene40aa9b2014-04-02 21:02:54 +0800175 #include <ifaddrs.h>
Andy Green7acf76c2016-07-23 14:18:25 +0800176 #endif
Andy Greene40aa9b2014-04-02 21:02:54 +0800177#endif
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800178#if defined (__ANDROID__)
179#include <syslog.h>
Alexander Bruines119bdaa2016-04-23 11:10:18 +0200180#include <sys/resource.h>
Fredrik Skogman316960b2016-09-09 06:49:44 +0800181#elif defined (__sun)
182#include <syslog.h>
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800183#else
Andy Green7acf76c2016-07-23 14:18:25 +0800184#if !defined(LWS_WITH_ESP8266)
Andy Greene40aa9b2014-04-02 21:02:54 +0800185#include <sys/syslog.h>
Dnyanesh Gate759e50c2014-09-26 05:39:05 +0800186#endif
Andy Green7acf76c2016-07-23 14:18:25 +0800187#endif
Andy Greene40aa9b2014-04-02 21:02:54 +0800188#include <netdb.h>
Andy Green7acf76c2016-07-23 14:18:25 +0800189#if !defined(LWS_WITH_ESP8266)
190#include <sys/mman.h>
191#include <sys/un.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000192#include <netinet/in.h>
Andy Green6c939552011-03-08 08:56:57 +0000193#include <netinet/tcp.h>
Andy Greenb45993c2010-12-18 15:13:50 +0000194#include <arpa/inet.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000195#include <poll.h>
Andy Green7acf76c2016-07-23 14:18:25 +0800196#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800197#ifdef LWS_USE_LIBEV
Andy Green86ed65f2016-02-14 09:27:41 +0800198#include <ev.h>
199#endif
200#ifdef LWS_USE_LIBUV
Alex Hultmana43c2ac2016-02-01 08:31:54 +0800201#include <uv.h>
Andy Green86ed65f2016-02-14 09:27:41 +0800202#endif
Andy Green5f2a8152015-11-02 08:21:08 +0800203
204#endif /* MBED */
205
206#ifndef LWS_NO_FORK
207#ifdef LWS_HAVE_SYS_PRCTL_H
208#include <sys/prctl.h>
209#endif
210#endif
211
Andy Green038d5822011-02-14 20:58:26 +0000212#include <sys/time.h>
Andy Green7c212cc2010-11-08 20:20:42 +0000213
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100214#define LWS_ERRNO errno
215#define LWS_EAGAIN EAGAIN
216#define LWS_EALREADY EALREADY
217#define LWS_EINPROGRESS EINPROGRESS
218#define LWS_EINTR EINTR
219#define LWS_EISCONN EISCONN
220#define LWS_EWOULDBLOCK EWOULDBLOCK
Andy Green2b304a92016-07-11 07:28:23 +0800221
Andy Green158e8042014-04-02 14:25:10 +0800222#define lws_set_blocking_send(wsi)
Andy Green2cd30742015-11-02 13:10:33 +0800223
Andy Green7acf76c2016-07-23 14:18:25 +0800224#if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
Andy Green2cd30742015-11-02 13:10:33 +0800225#define lws_socket_is_valid(x) ((x) != NULL)
226#define LWS_SOCK_INVALID (NULL)
Andy Green7acf76c2016-07-23 14:18:25 +0800227struct lws;
228const char *
229lws_plat_get_peer_simple(struct lws *wsi, char *name, int namelen);
Andy Green2cd30742015-11-02 13:10:33 +0800230#else
Andy Greenc53f7ca2015-11-14 07:35:27 +0800231#define lws_socket_is_valid(x) (x >= 0)
232#define LWS_SOCK_INVALID (-1)
Peter Hinz56885f32011-03-02 22:03:47 +0000233#endif
Andy Green2cd30742015-11-02 13:10:33 +0800234#endif
Peter Hinz56885f32011-03-02 22:03:47 +0000235
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200236#ifndef LWS_HAVE_BZERO
Andy Greene5ea1f92014-11-18 18:25:24 +0800237#ifndef bzero
Patrick Gansterer4a837272014-02-28 13:17:49 +0100238#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
239#endif
Andy Greene5ea1f92014-11-18 18:25:24 +0800240#endif
Patrick Gansterer4a837272014-02-28 13:17:49 +0100241
=?UTF-8?q?Joakim=20S=C3=B6derberg?=cefab312015-06-24 16:46:02 +0200242#ifndef LWS_HAVE_STRERROR
Patrick Gansterer9d614912014-02-28 00:59:53 +0100243#define strerror(x) ""
244#endif
245
Andy Green7c212cc2010-11-08 20:20:42 +0000246#ifdef LWS_OPENSSL_SUPPORT
Andy Green1a3f1772016-03-28 19:58:02 +0800247
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200248#ifdef USE_WOLFSSL
ABruines80a70682015-08-09 22:56:32 +0200249#ifdef USE_OLD_CYASSL
250#include <cyassl/openssl/ssl.h>
251#include <cyassl/error-ssl.h>
252#else
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200253#include <wolfssl/openssl/ssl.h>
254#include <wolfssl/error-ssl.h>
ABruines80a70682015-08-09 22:56:32 +0200255#endif /* not USE_OLD_CYASSL */
Andy Green23c5f2e2013-02-06 15:43:00 +0900256#else
Andy Green1a3f1772016-03-28 19:58:02 +0800257#if defined(LWS_USE_POLARSSL)
258#include <polarssl/ssl.h>
259#include <polarssl/error.h>
260#include <polarssl/md5.h>
261#include <polarssl/sha1.h>
262#include <polarssl/ecdh.h>
Andy Green451cee52016-04-17 11:28:43 +0800263#define SSL_ERROR_WANT_READ POLARSSL_ERR_NET_WANT_READ
264#define SSL_ERROR_WANT_WRITE POLARSSL_ERR_NET_WANT_WRITE
265#define OPENSSL_VERSION_NUMBER 0x10002000L
Andy Green1a3f1772016-03-28 19:58:02 +0800266#else
267#if defined(LWS_USE_MBEDTLS)
268#include <mbedtls/ssl.h>
269#include <mbedtls/error.h>
270#include <mbedtls/md5.h>
271#include <mbedtls/sha1.h>
272#include <mbedtls/ecdh.h>
273#else
Andy Green7c212cc2010-11-08 20:20:42 +0000274#include <openssl/ssl.h>
275#include <openssl/evp.h>
276#include <openssl/err.h>
Andy Green70dfebd2010-12-20 09:35:03 +0000277#include <openssl/md5.h>
Andy Greene2522172011-01-18 17:14:03 +0000278#include <openssl/sha.h>
Andy Green1a3f1772016-03-28 19:58:02 +0800279#ifdef LWS_HAVE_OPENSSL_ECDH_H
280#include <openssl/ecdh.h>
281#endif
Andy Greene7bf0aa2016-06-28 19:50:40 +0800282#include <openssl/x509v3.h>
Andy Green1a3f1772016-03-28 19:58:02 +0800283#endif /* not USE_MBEDTLS */
284#endif /* not USE_POLARSSL */
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200285#endif /* not USE_WOLFSSL */
Darin Willitsdb9ba422011-02-14 20:56:24 +0000286#endif
287
Andy Green7c212cc2010-11-08 20:20:42 +0000288#include "libwebsockets.h"
Andy Green7acf76c2016-07-23 14:18:25 +0800289#if defined(WIN32) || defined(_WIN32)
290#else
291static inline int compatible_close(int fd) { return close(fd); }
292#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000293
Andy Green8c0d3c02015-11-02 20:34:12 +0800294#if defined(MBED_OPERATORS)
295#undef compatible_close
296#define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
Andy Green11f27342015-11-08 12:10:26 +0800297#ifndef BIG_ENDIAN
298#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
299#endif
300#ifndef LITTLE_ENDIAN
301#define LITTLE_ENDIAN 1234
302#endif
303#ifndef BYTE_ORDER
304#define BYTE_ORDER LITTLE_ENDIAN
305#endif
Andy Green8c0d3c02015-11-02 20:34:12 +0800306#endif
307
Andy Green7acf76c2016-07-23 14:18:25 +0800308#if defined(LWS_WITH_ESP8266)
309#undef compatible_close
310#define compatible_close(fd) { fd->state=ESPCONN_CLOSE; espconn_delete(fd); }
311lws_sockfd_type
312esp8266_create_tcp_stream_socket(void);
313void
314esp8266_tcp_stream_bind(lws_sockfd_type fd, int port, struct lws *wsi);
315#ifndef BIG_ENDIAN
316#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
317#endif
318#ifndef LITTLE_ENDIAN
319#define LITTLE_ENDIAN 1234
320#endif
321#ifndef BYTE_ORDER
322#define BYTE_ORDER LITTLE_ENDIAN
323#endif
324#endif
325
326
Andy Green158e8042014-04-02 14:25:10 +0800327#if defined(WIN32) || defined(_WIN32)
328
329#ifndef BIG_ENDIAN
330#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
331#endif
332#ifndef LITTLE_ENDIAN
333#define LITTLE_ENDIAN 1234
334#endif
335#ifndef BYTE_ORDER
336#define BYTE_ORDER LITTLE_ENDIAN
337#endif
Andy Green11f27342015-11-08 12:10:26 +0800338#ifndef u_int64_t
Andy Green158e8042014-04-02 14:25:10 +0800339typedef unsigned __int64 u_int64_t;
Andy Green11f27342015-11-08 12:10:26 +0800340#endif
Andy Green158e8042014-04-02 14:25:10 +0800341
342#undef __P
343#ifndef __P
344#if __STDC__
345#define __P(protos) protos
346#else
347#define __P(protos) ()
348#endif
349#endif
350
351#else
352
353#include <sys/stat.h>
Andy Green158e8042014-04-02 14:25:10 +0800354#include <sys/time.h>
355
356#if defined(__APPLE__)
357#include <machine/endian.h>
358#elif defined(__FreeBSD__)
359#include <sys/endian.h>
360#elif defined(__linux__)
361#include <endian.h>
362#endif
363
Andy Green8c0d3c02015-11-02 20:34:12 +0800364#ifdef __cplusplus
365extern "C" {
366#endif
Alejandro Meryead8afe2014-12-07 03:36:11 +0100367
emironova49d0842014-09-16 14:05:13 +0400368#if defined(__QNX__)
369 #include <gulliver.h>
370 #if defined(__LITTLEENDIAN__)
371 #define BYTE_ORDER __LITTLEENDIAN__
372 #define LITTLE_ENDIAN __LITTLEENDIAN__
373 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
374 #endif
375 #if defined(__BIGENDIAN__)
376 #define BYTE_ORDER __BIGENDIAN__
377 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
378 #define BIG_ENDIAN __BIGENDIAN__
379 #endif
380#endif
381
Fredrik Skogman316960b2016-09-09 06:49:44 +0800382#if defined(__sun) && defined(__GNUC__)
383# define BYTE_ORDER __BYTE_ORDER__
384# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
385# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
386#endif
387
Andy Green158e8042014-04-02 14:25:10 +0800388#if !defined(BYTE_ORDER)
389# define BYTE_ORDER __BYTE_ORDER
390#endif
391#if !defined(LITTLE_ENDIAN)
392# define LITTLE_ENDIAN __LITTLE_ENDIAN
393#endif
394#if !defined(BIG_ENDIAN)
395# define BIG_ENDIAN __BIG_ENDIAN
396#endif
397
398#endif
399
Darin Willitsc19456f2011-02-14 17:52:39 +0000400/*
401 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
402 * but happily have something equivalent in the SO_NOSIGPIPE flag.
403 */
404#ifdef __APPLE__
Andy Green6ee372f2012-04-09 15:09:01 +0800405#define MSG_NOSIGNAL SO_NOSIGPIPE
Darin Willitsc19456f2011-02-14 17:52:39 +0000406#endif
407
Fredrik Skogman316960b2016-09-09 06:49:44 +0800408/*
409 * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
410 * POSIX 2008.
411 */
412#ifdef __sun
413#define MSG_NOSIGNAL 0
414#endif
415
Bud Davis229bfec2015-01-30 10:13:01 +0800416#ifdef _WIN32
417#ifndef FD_HASHTABLE_MODULUS
418#define FD_HASHTABLE_MODULUS 32
419#endif
420#endif
421
Andy Green8c1f6022016-01-26 20:56:56 +0800422#ifndef LWS_DEF_HEADER_LEN
Andy Green5ab523e2016-07-11 07:48:53 +0800423#define LWS_DEF_HEADER_LEN 4096
Andy Greenc0d6b632013-01-12 23:42:17 +0800424#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800425#ifndef LWS_DEF_HEADER_POOL
Andy Green5ab523e2016-07-11 07:48:53 +0800426#define LWS_DEF_HEADER_POOL 4
Andy Green3df58002015-12-25 12:44:12 +0800427#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800428#ifndef LWS_MAX_PROTOCOLS
Andy Greend91d5e82013-02-10 16:00:47 +0800429#define LWS_MAX_PROTOCOLS 5
Andy Greenc0d6b632013-01-12 23:42:17 +0800430#endif
431#ifndef LWS_MAX_EXTENSIONS_ACTIVE
Andy Greend738f842016-01-19 04:32:14 +0800432#define LWS_MAX_EXTENSIONS_ACTIVE 2
Andy Greenc0d6b632013-01-12 23:42:17 +0800433#endif
Andy Green67112662016-01-11 11:34:01 +0800434#ifndef LWS_MAX_EXT_OFFERS
435#define LWS_MAX_EXT_OFFERS 8
436#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800437#ifndef SPEC_LATEST_SUPPORTED
Andy Greend85cb202011-09-25 09:32:54 +0100438#define SPEC_LATEST_SUPPORTED 13
Andy Greenc0d6b632013-01-12 23:42:17 +0800439#endif
440#ifndef AWAITING_TIMEOUT
Andy Green8c1f6022016-01-26 20:56:56 +0800441#define AWAITING_TIMEOUT 20
Andy Greenc0d6b632013-01-12 23:42:17 +0800442#endif
443#ifndef CIPHERS_LIST_STRING
David Galeanof177f2a2013-01-10 10:15:19 +0800444#define CIPHERS_LIST_STRING "DEFAULT"
Andy Greenc0d6b632013-01-12 23:42:17 +0800445#endif
Andy Greena824d182013-01-15 20:52:29 +0800446#ifndef LWS_SOMAXCONN
447#define LWS_SOMAXCONN SOMAXCONN
448#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000449
Andy Greene2522172011-01-18 17:14:03 +0000450#define MAX_WEBSOCKET_04_KEY_LEN 128
Andy Greenc0d6b632013-01-12 23:42:17 +0800451
452#ifndef SYSTEM_RANDOM_FILEPATH
Andy Green4739e5c2011-01-22 12:51:57 +0000453#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
Andy Greenc0d6b632013-01-12 23:42:17 +0800454#endif
Andy Greenc0d6b632013-01-12 23:42:17 +0800455
Andy Green5fd55cd2011-04-23 10:54:53 +0100456enum lws_websocket_opcodes_07 {
Andy Green54806b12015-12-17 17:03:59 +0800457 LWSWSOPC_CONTINUATION = 0,
458 LWSWSOPC_TEXT_FRAME = 1,
459 LWSWSOPC_BINARY_FRAME = 2,
Andy Greena41314f2011-05-23 10:00:03 +0100460
Andy Green54806b12015-12-17 17:03:59 +0800461 LWSWSOPC_NOSPEC__MUX = 7,
Andy Greena41314f2011-05-23 10:00:03 +0100462
463 /* control extensions 8+ */
464
Andy Green54806b12015-12-17 17:03:59 +0800465 LWSWSOPC_CLOSE = 8,
466 LWSWSOPC_PING = 9,
467 LWSWSOPC_PONG = 0xa,
Andy Green5fd55cd2011-04-23 10:54:53 +0100468};
469
Andy Greena41314f2011-05-23 10:00:03 +0100470
Andy Green7c212cc2010-11-08 20:20:42 +0000471enum lws_connection_states {
Andy Green54806b12015-12-17 17:03:59 +0800472 LWSS_HTTP,
473 LWSS_HTTP_ISSUING_FILE,
474 LWSS_HTTP_HEADERS,
475 LWSS_HTTP_BODY,
476 LWSS_DEAD_SOCKET,
477 LWSS_ESTABLISHED,
Andy Greena661ee52016-02-29 13:18:30 +0800478 LWSS_CLIENT_HTTP_ESTABLISHED,
Andy Green54806b12015-12-17 17:03:59 +0800479 LWSS_CLIENT_UNCONNECTED,
480 LWSS_RETURNED_CLOSE_ALREADY,
481 LWSS_AWAITING_CLOSE_ACK,
482 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
Andy Green8c1f6022016-01-26 20:56:56 +0800483 LWSS_SHUTDOWN,
Andy Green40110e82015-12-14 08:52:03 +0800484
Andy Green54806b12015-12-17 17:03:59 +0800485 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
486 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
487 LWSS_HTTP2_ESTABLISHED,
Andy Green6a8099b2016-02-21 21:25:48 +0800488
Andy Green2d8d35a2016-02-29 14:19:16 +0800489 LWSS_CGI,
Andy Green7c212cc2010-11-08 20:20:42 +0000490};
491
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400492enum http_version {
493 HTTP_VERSION_1_0,
494 HTTP_VERSION_1_1,
Andy Green22d6f392016-04-10 09:33:54 +0800495 HTTP_VERSION_2
Andrew Canadayafe26cf2014-07-13 01:07:36 -0400496};
497
498enum http_connection_type {
499 HTTP_CONNECTION_CLOSE,
500 HTTP_CONNECTION_KEEP_ALIVE
501};
502
Andy Green024eb6c2014-10-08 12:00:53 +0800503enum lws_pending_protocol_send {
504 LWS_PPS_NONE,
505 LWS_PPS_HTTP2_MY_SETTINGS,
506 LWS_PPS_HTTP2_ACK_SETTINGS,
Andy Greenbbbf07a2014-10-27 16:46:44 +0800507 LWS_PPS_HTTP2_PONG,
Andy Green024eb6c2014-10-08 12:00:53 +0800508};
509
Andy Green7c212cc2010-11-08 20:20:42 +0000510enum lws_rx_parse_state {
511 LWS_RXPS_NEW,
Andy Greene77ddd82010-11-13 10:03:47 +0000512
Andy Green67112662016-01-11 11:34:01 +0800513 LWS_RXPS_04_mask_1,
514 LWS_RXPS_04_mask_2,
515 LWS_RXPS_04_mask_3,
Andy Green3e5eb782011-01-18 18:14:26 +0000516
517 LWS_RXPS_04_FRAME_HDR_1,
Andy Green38e57bb2011-01-19 12:20:27 +0000518 LWS_RXPS_04_FRAME_HDR_LEN,
519 LWS_RXPS_04_FRAME_HDR_LEN16_2,
520 LWS_RXPS_04_FRAME_HDR_LEN16_1,
521 LWS_RXPS_04_FRAME_HDR_LEN64_8,
522 LWS_RXPS_04_FRAME_HDR_LEN64_7,
523 LWS_RXPS_04_FRAME_HDR_LEN64_6,
524 LWS_RXPS_04_FRAME_HDR_LEN64_5,
525 LWS_RXPS_04_FRAME_HDR_LEN64_4,
526 LWS_RXPS_04_FRAME_HDR_LEN64_3,
527 LWS_RXPS_04_FRAME_HDR_LEN64_2,
528 LWS_RXPS_04_FRAME_HDR_LEN64_1,
Andy Green3e5eb782011-01-18 18:14:26 +0000529
Andy Green283d0a22011-04-24 05:46:23 +0100530 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
531 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
532 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
533 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
534
Andy Green7c212cc2010-11-08 20:20:42 +0000535 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
536};
537
Andy Green95fff472016-08-08 21:54:30 +0800538#define LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP 32
Andy Green7c212cc2010-11-08 20:20:42 +0000539
Andy Green0d338332011-02-12 11:57:43 +0000540enum connection_mode {
Andy Green54806b12015-12-17 17:03:59 +0800541 LWSCM_HTTP_SERVING,
542 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
543 LWSCM_PRE_WS_SERVING_ACCEPT,
Andy Greend280b6e2013-01-15 13:40:23 +0800544
Andy Green54806b12015-12-17 17:03:59 +0800545 LWSCM_WS_SERVING,
546 LWSCM_WS_CLIENT,
Andy Green40110e82015-12-14 08:52:03 +0800547
Andy Green54806b12015-12-17 17:03:59 +0800548 LWSCM_HTTP2_SERVING,
Andy Green0d338332011-02-12 11:57:43 +0000549
Andy Greene2160712013-01-28 12:19:10 +0800550 /* transient, ssl delay hiding */
Andy Green54806b12015-12-17 17:03:59 +0800551 LWSCM_SSL_ACK_PENDING,
Andy Green8c1f6022016-01-26 20:56:56 +0800552 LWSCM_SSL_INIT,
Andy Greene2160712013-01-28 12:19:10 +0800553
Andy Green95fff472016-08-08 21:54:30 +0800554 /* special internal types */
555 LWSCM_SERVER_LISTENER,
556 LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
557
558 /* HTTP Client related */
559 LWSCM_HTTP_CLIENT = LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP,
560 LWSCM_HTTP_CLIENT_ACCEPTED, /* actual HTTP service going on */
Andy Green54806b12015-12-17 17:03:59 +0800561 LWSCM_WSCL_WAITING_CONNECT,
562 LWSCM_WSCL_WAITING_PROXY_REPLY,
563 LWSCM_WSCL_ISSUE_HANDSHAKE,
564 LWSCM_WSCL_ISSUE_HANDSHAKE2,
Andy Green95fff472016-08-08 21:54:30 +0800565 LWSCM_WSCL_ISSUE_HTTP_BODY,
Andy Green54806b12015-12-17 17:03:59 +0800566 LWSCM_WSCL_WAITING_SSL,
567 LWSCM_WSCL_WAITING_SERVER_REPLY,
568 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
569 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
Andy Greenbe93fef2011-02-14 20:25:43 +0000570
Andy Green95fff472016-08-08 21:54:30 +0800571 /****** add new things just above ---^ ******/
572
573
Andy Green0d338332011-02-12 11:57:43 +0000574};
575
Andy Greenca0a1292013-03-16 11:24:23 +0800576enum {
577 LWS_RXFLOW_ALLOW = (1 << 0),
578 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
579};
580
Andy Green1fb95e82015-12-26 17:20:34 +0800581/* this is not usable directly by user code any more, lws_close_reason() */
582#define LWS_WRITE_CLOSE 4
583
Andy Green4b85c1d2015-12-04 11:08:32 +0800584struct lws_protocols;
585struct lws;
Andy Greene92cd172011-01-19 13:11:55 +0000586
Andy Green86ed65f2016-02-14 09:27:41 +0800587#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
588
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800589struct lws_io_watcher {
Andy Green86ed65f2016-02-14 09:27:41 +0800590#ifdef LWS_USE_LIBEV
591 ev_io ev_watcher;
592#endif
593#ifdef LWS_USE_LIBUV
594 uv_poll_t uv_watcher;
595#endif
596 struct lws_context *context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800597};
598
599struct lws_signal_watcher {
Andy Green86ed65f2016-02-14 09:27:41 +0800600#ifdef LWS_USE_LIBEV
601 ev_signal ev_watcher;
602#endif
603#ifdef LWS_USE_LIBUV
604 uv_signal_t uv_watcher;
605#endif
606 struct lws_context *context;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800607};
Andy Green86ed65f2016-02-14 09:27:41 +0800608#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800609
Bud Davis229bfec2015-01-30 10:13:01 +0800610#ifdef _WIN32
611#define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
Andy Green3ef579b2015-12-04 09:23:56 +0800612struct lws_fd_hashtable {
Andy Green4b85c1d2015-12-04 11:08:32 +0800613 struct lws **wsi;
Bud Davis229bfec2015-01-30 10:13:01 +0800614 int length;
615};
616#endif
617
Andy Green3df58002015-12-25 12:44:12 +0800618/*
619 * This is totally opaque to code using the library. It's exported as a
620 * forward-reference pointer-only declaration; the user can use the pointer with
621 * other APIs to get information out of it.
622 */
623
624struct lws_fragments {
Andy Green48895662016-06-02 12:32:38 +0800625 unsigned int offset;
Andy Green3df58002015-12-25 12:44:12 +0800626 unsigned short len;
627 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
628};
629
630/*
631 * these are assigned from a pool held in the context.
632 * Both client and server mode uses them for http header analysis
633 */
634
635struct allocated_headers {
Andy Green2c218e72016-02-15 12:37:04 +0800636 struct lws *wsi; /* owner */
Andy Greende132b92015-12-25 13:48:20 +0800637 char *data; /* prepared by context init to point to dedicated storage */
638 /*
639 * the randomly ordered fragments, indexed by frag_index and
640 * lws_fragments->nfrag for continuation.
641 */
642 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
Andy Green8c1f6022016-01-26 20:56:56 +0800643 time_t assigned;
Andy Green3df58002015-12-25 12:44:12 +0800644 /*
645 * for each recognized token, frag_index says which frag[] his data
646 * starts in (0 means the token did not appear)
647 * the actual header data gets dumped as it comes in, into data[]
648 */
649 unsigned char frag_index[WSI_TOKEN_COUNT];
Andy Green2c218e72016-02-15 12:37:04 +0800650 unsigned char rx[2048];
651 unsigned int rxpos;
652 unsigned int rxlen;
Andy Green48895662016-06-02 12:32:38 +0800653 unsigned int pos;
Andy Green2c218e72016-02-15 12:37:04 +0800654
Andy Green3df58002015-12-25 12:44:12 +0800655#ifndef LWS_NO_CLIENT
656 char initial_handshake_hash_base64[30];
Andy Green3df58002015-12-25 12:44:12 +0800657#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800658
Andy Greenaa775fd2015-12-26 08:56:58 +0800659 unsigned char in_use;
660 unsigned char nfrag;
Andy Green3df58002015-12-25 12:44:12 +0800661};
662
Andy Greend3a55052016-01-19 03:34:24 +0800663/*
664 * so we can have n connections being serviced simultaneously,
665 * these things need to be isolated per-thread.
666 */
667
668struct lws_context_per_thread {
Andy Green8c1f6022016-01-26 20:56:56 +0800669#if LWS_MAX_SMP > 1
670 pthread_mutex_t lock;
671#endif
Andy Greend3a55052016-01-19 03:34:24 +0800672 struct lws_pollfd *fds;
Andy Green7acf76c2016-07-23 14:18:25 +0800673#if defined(LWS_WITH_ESP8266)
674 struct lws **lws_vs_fds_index;
675#endif
Andy Greend3a55052016-01-19 03:34:24 +0800676 struct lws *rx_draining_ext_list;
677 struct lws *tx_draining_ext_list;
Andy Green8c1f6022016-01-26 20:56:56 +0800678 struct lws *timeout_list;
Patrick Ganstererfa9ebb32016-08-14 14:04:56 +0200679#ifdef LWS_USE_LIBUV
Andy Green38a1cbb2016-02-27 11:03:27 +0800680 struct lws_context *context;
Patrick Ganstererfa9ebb32016-08-14 14:04:56 +0200681#endif
Andy Green6a8099b2016-02-21 21:25:48 +0800682#ifdef LWS_WITH_CGI
683 struct lws_cgi *cgi_list;
684#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800685 void *http_header_data;
686 struct allocated_headers *ah_pool;
687 struct lws *ah_wait_list;
688 int ah_wait_list_length;
Andy Greend3a55052016-01-19 03:34:24 +0800689#ifdef LWS_OPENSSL_SUPPORT
690 struct lws *pending_read_list; /* linked list */
691#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800692#ifndef LWS_NO_SERVER
693 struct lws *wsi_listening;
694#endif
Andy Green86ed65f2016-02-14 09:27:41 +0800695#if defined(LWS_USE_LIBEV)
696 struct ev_loop *io_loop_ev;
697#endif
698#if defined(LWS_USE_LIBUV)
699 uv_loop_t *io_loop_uv;
700 uv_signal_t signals[8];
Andy Green38a1cbb2016-02-27 11:03:27 +0800701 uv_timer_t uv_timeout_watcher;
Andy Green09998e32016-04-06 09:23:16 +0800702 uv_idle_t uv_idle;
Andy Green86ed65f2016-02-14 09:27:41 +0800703#endif
704#if defined(LWS_USE_LIBEV)
705 struct lws_io_watcher w_accept;
706#endif
707#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
708 struct lws_signal_watcher w_sigint;
709 unsigned char ev_loop_foreign:1;
710#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800711
712 unsigned long count_conns;
Andy Greend3a55052016-01-19 03:34:24 +0800713 /*
714 * usable by anything in the service code, but only if the scope
715 * does not last longer than the service action (since next service
716 * of any socket can likewise use it and overwrite)
717 */
718 unsigned char *serv_buf;
719#ifdef _WIN32
720 WSAEVENT *events;
721#else
Andy Green7acf76c2016-07-23 14:18:25 +0800722 lws_sockfd_type dummy_pipe_fds[2];
Andy Greend3a55052016-01-19 03:34:24 +0800723#endif
Andy Green8c1f6022016-01-26 20:56:56 +0800724 unsigned int fds_count;
725
726 short ah_count_in_use;
Andy Green38a1cbb2016-02-27 11:03:27 +0800727 unsigned char tid;
Andy Greenbbf93692016-08-07 08:33:08 +0800728 unsigned char lock_depth;
Andy Greend3a55052016-01-19 03:34:24 +0800729};
730
Andy Greend526c502016-03-28 10:10:43 +0800731/*
732 * virtual host -related context information
733 * vhostwide SSL context
734 * vhostwide proxy
735 *
736 * heirarchy:
737 *
738 * context -> vhost -> wsi
739 *
740 * incoming connection non-SSL vhost binding:
741 *
742 * listen socket -> wsi -> select vhost after first headers
743 *
744 * incoming connection SSL vhost binding:
745 *
746 * SSL SNI -> wsi -> bind after SSL negotiation
747 */
748
749struct lws_vhost {
Andy Green7acf76c2016-07-23 14:18:25 +0800750#if !defined(LWS_WITH_ESP8266)
Andy Greend526c502016-03-28 10:10:43 +0800751 char http_proxy_address[128];
752 char proxy_basic_auth_token[128];
Andy Green7acf76c2016-07-23 14:18:25 +0800753#endif
754#if defined(LWS_WITH_ESP8266)
755 /* listen sockets need a place to hang their hat */
756 esp_tcp tcp;
757#endif
Andy Greend526c502016-03-28 10:10:43 +0800758 struct lws_context *context;
759 struct lws_vhost *vhost_next;
Andy Green4664f712016-05-02 04:59:54 +0800760 const struct lws_http_mount *mount_list;
Andy Greend526c502016-03-28 10:10:43 +0800761 struct lws *lserv_wsi;
762 const char *name;
763 const char *iface;
764 const struct lws_protocols *protocols;
Andy Green02077052016-04-06 16:15:40 +0800765 void **protocol_vh_privs;
Andy Green952fcde2016-05-02 06:01:59 +0800766 const struct lws_protocol_vhost_options *pvo;
Andy Greene35d91a2016-08-27 17:07:06 +0800767 const struct lws_protocol_vhost_options *headers;
Andy Green4714cf02016-04-16 08:40:35 +0800768 struct lws **same_vh_protocol_list;
Andy Greend526c502016-03-28 10:10:43 +0800769#ifdef LWS_OPENSSL_SUPPORT
770 SSL_CTX *ssl_ctx;
771 SSL_CTX *ssl_client_ctx;
772#endif
773#ifndef LWS_NO_EXTENSIONS
774 const struct lws_extension *extensions;
775#endif
Andy Greenc25b2902016-05-04 15:59:27 +0800776 unsigned long long rx, tx;
777 unsigned long conn, trans, ws_upgrades, http2_upgrades;
Andy Greend526c502016-03-28 10:10:43 +0800778
779 int listen_port;
780 unsigned int http_proxy_port;
Andy Greencc3c6fb2016-04-14 15:09:01 +0800781 unsigned int options;
Andy Greend526c502016-03-28 10:10:43 +0800782 int count_protocols;
783 int ka_time;
784 int ka_probes;
785 int ka_interval;
Andy Greenb46e4a82016-04-12 16:26:03 +0800786 int keepalive_timeout;
Andy Green2f0bc932016-04-15 12:00:23 +0800787#ifdef LWS_WITH_ACCESS_LOG
788 int log_fd;
789#endif
Andy Greend526c502016-03-28 10:10:43 +0800790
791#ifdef LWS_OPENSSL_SUPPORT
792 int use_ssl;
793 int allow_non_ssl_on_ssl_port;
794 unsigned int user_supplied_ssl_ctx:1;
795#endif
Andy Greenf32d2502016-07-15 13:41:38 +0800796
Andy Greenf6585282016-05-06 14:24:59 +0800797 unsigned char default_protocol_index;
Andy Greend526c502016-03-28 10:10:43 +0800798};
799
Andy Greend3a55052016-01-19 03:34:24 +0800800/*
801 * the rest is managed per-context, that includes
802 *
803 * - processwide single fd -> wsi lookup
804 * - contextwide headers pool
Andy Greend3a55052016-01-19 03:34:24 +0800805 */
806
Andy Green4b85c1d2015-12-04 11:08:32 +0800807struct lws_context {
Andy Greenaa775fd2015-12-26 08:56:58 +0800808 time_t last_timeout_check_s;
Andy Greenf32d2502016-07-15 13:41:38 +0800809 time_t last_ws_ping_pong_check_s;
Andy Green98061402016-04-15 14:01:29 +0800810 time_t time_up;
Andy Greenaa775fd2015-12-26 08:56:58 +0800811 struct lws_plat_file_ops fops;
Andy Greend3a55052016-01-19 03:34:24 +0800812 struct lws_context_per_thread pt[LWS_MAX_SMP];
Bud Davis229bfec2015-01-30 10:13:01 +0800813#ifdef _WIN32
814/* different implementation between unix and windows */
Andy Green3ef579b2015-12-04 09:23:56 +0800815 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
Bud Davis229bfec2015-01-30 10:13:01 +0800816#else
Andy Green7acf76c2016-07-23 14:18:25 +0800817#if defined(LWS_WITH_ESP8266)
818 struct espconn **connpool; /* .reverse points to the wsi */
819 void *rxd;
820 int rxd_len;
821 os_timer_t to_timer;
822#else
Andy Green4b85c1d2015-12-04 11:08:32 +0800823 struct lws **lws_lookup; /* fd to wsi */
Bud Davis229bfec2015-01-30 10:13:01 +0800824#endif
Andy Green7acf76c2016-07-23 14:18:25 +0800825#endif
Andy Greend526c502016-03-28 10:10:43 +0800826 struct lws_vhost *vhost_list;
Andy Green02077052016-04-06 16:15:40 +0800827 struct lws_plugin *plugin_list;
Andy Greenaa775fd2015-12-26 08:56:58 +0800828 const struct lws_token_limits *token_limits;
829 void *user_space;
Andy Greenb21c20b2016-04-15 13:33:52 +0800830 const char *server_string;
Andy Greend738f842016-01-19 04:32:14 +0800831
Andy Green86ed65f2016-02-14 09:27:41 +0800832#if defined(LWS_USE_LIBEV)
833 lws_ev_signal_cb_t * lws_ev_sigint_cb;
834#endif
835#if defined(LWS_USE_LIBUV)
Denis Osvaldf107e4b2016-03-22 14:04:15 +0100836 uv_signal_cb lws_uv_sigint_cb;
Andy Green86ed65f2016-02-14 09:27:41 +0800837#endif
Andy Greenaa775fd2015-12-26 08:56:58 +0800838 char canonical_hostname[128];
839#ifdef LWS_LATENCY
840 unsigned long worst_latency;
841 char worst_latency_info[256];
842#endif
Andy Greenb8b247d2013-01-22 07:20:08 +0800843
Andy Greenaa775fd2015-12-26 08:56:58 +0800844 int max_fds;
Andy Green86ed65f2016-02-14 09:27:41 +0800845#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
Andy Greenaa775fd2015-12-26 08:56:58 +0800846 int use_ev_sigint;
847#endif
Andy Green24cba922013-01-19 13:56:10 +0800848 int started_with_parent;
Andy Greend526c502016-03-28 10:10:43 +0800849 int uid, gid;
Andy Green24cba922013-01-19 13:56:10 +0800850
Andy Green44eee682011-02-10 09:32:24 +0000851 int fd_random;
Andy Greend526c502016-03-28 10:10:43 +0800852#ifdef LWS_OPENSSL_SUPPORT
853#define lws_ssl_anybody_has_buffered_read(w) \
854 (w->vhost->use_ssl && \
855 w->context->pt[(int)w->tsi].pending_read_list)
856#define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
857 (/*c->use_ssl && */ \
858 c->pt[(int)t].pending_read_list)
859#else
860#define lws_ssl_anybody_has_buffered_read(ctx) (0)
861#define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
862#endif
Andy Green86ed65f2016-02-14 09:27:41 +0800863 int count_wsi_allocated;
Andy Green748a2212016-04-20 06:10:56 +0800864 int count_cgi_spawned;
Andy Greenaa775fd2015-12-26 08:56:58 +0800865 unsigned int options;
Andy Greend3a55052016-01-19 03:34:24 +0800866 unsigned int fd_limit_per_thread;
Andy Green200a6a22016-02-15 20:36:02 +0800867 unsigned int timeout_secs;
Andy Greene7c1c752016-05-19 12:34:35 +0800868 unsigned int pt_serv_buf_size;
Andy Green48895662016-06-02 12:32:38 +0800869 int max_http_header_data;
Andy Green6ee372f2012-04-09 15:09:01 +0800870
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100871 /*
872 * set to the Thread ID that's doing the service loop just before entry
873 * to poll indicates service thread likely idling in poll()
874 * volatile because other threads may check it as part of processing
875 * for pollfd event change.
876 */
877 volatile int service_tid;
Andy Greenc35b36b2015-12-24 13:00:54 +0800878 int service_tid_detected;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +0100879
Andy Green3df58002015-12-25 12:44:12 +0800880 short max_http_header_pool;
Andy Greend3a55052016-01-19 03:34:24 +0800881 short count_threads;
Andy Green02077052016-04-06 16:15:40 +0800882 short plugin_protocol_count;
883 short plugin_extension_count;
Andy Greenb21c20b2016-04-15 13:33:52 +0800884 short server_string_len;
Andy Greenf32d2502016-07-15 13:41:38 +0800885 unsigned short ws_ping_pong_interval;
Andy Green9a9d5ea2016-01-18 11:49:41 +0800886
887 unsigned int being_destroyed:1;
Andy Green86ed65f2016-02-14 09:27:41 +0800888 unsigned int requested_kill:1;
Andy Green02077052016-04-06 16:15:40 +0800889 unsigned int protocol_init_done:1;
Andy Greenb45993c2010-12-18 15:13:50 +0000890};
891
Andy Greend526c502016-03-28 10:10:43 +0800892#define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
893#define lws_get_vh_protocol(vh, x) vh->protocols[x]
894
Andy Green86ed65f2016-02-14 09:27:41 +0800895LWS_EXTERN void
896lws_close_free_wsi_final(struct lws *wsi);
897LWS_EXTERN void
898lws_libuv_closehandle(struct lws *wsi);
899
Andy Green0a183542016-04-09 07:22:40 +0800900LWS_VISIBLE LWS_EXTERN int
Andy Greencae57ad2016-05-02 10:03:25 +0800901lws_plat_plugins_init(struct lws_context * context, const char * const *d);
Andy Green0a183542016-04-09 07:22:40 +0800902
903LWS_VISIBLE LWS_EXTERN int
904lws_plat_plugins_destroy(struct lws_context * context);
905
Andy Greenf32d2502016-07-15 13:41:38 +0800906LWS_EXTERN void
907lws_restart_ws_ping_pong_timer(struct lws *wsi);
908
Andy Green7acf76c2016-07-23 14:18:25 +0800909struct lws *
910lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
911
912
Andy Greena717df22014-04-11 13:14:37 +0800913enum {
914 LWS_EV_READ = (1 << 0),
915 LWS_EV_WRITE = (1 << 1),
916 LWS_EV_START = (1 << 2),
917 LWS_EV_STOP = (1 << 3),
Andy Green86ed65f2016-02-14 09:27:41 +0800918
919 LWS_EV_PREPARE_DELETION = (1 << 31),
Andy Greena717df22014-04-11 13:14:37 +0800920};
921
Andy Green86ed65f2016-02-14 09:27:41 +0800922#if defined(LWS_USE_LIBEV)
Andy Greena717df22014-04-11 13:14:37 +0800923LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800924lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
Andy Greena717df22014-04-11 13:14:37 +0800925LWS_EXTERN void
Andy Green11c05bf2015-12-16 18:19:08 +0800926lws_libev_io(struct lws *wsi, int flags);
Andy Greena717df22014-04-11 13:14:37 +0800927LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +0800928lws_libev_init_fd_table(struct lws_context *context);
Andy Greena717df22014-04-11 13:14:37 +0800929LWS_EXTERN void
Andy Green86ed65f2016-02-14 09:27:41 +0800930lws_libev_destroyloop(struct lws_context *context, int tsi);
931LWS_EXTERN void
932lws_libev_run(const struct lws_context *context, int tsi);
Andy Greenc6fd3602016-03-23 09:22:11 +0800933#define LWS_LIBEV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV)
Andy Green86ed65f2016-02-14 09:27:41 +0800934LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800935#else
Andy Green86ed65f2016-02-14 09:27:41 +0800936#define lws_libev_accept(_a, _b) ((void) 0)
937#define lws_libev_io(_a, _b) ((void) 0)
938#define lws_libev_init_fd_table(_a) (0)
939#define lws_libev_run(_a, _b) ((void) 0)
940#define lws_libev_destroyloop(_a, _b) ((void) 0)
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800941#define LWS_LIBEV_ENABLED(context) (0)
Andy Green86ed65f2016-02-14 09:27:41 +0800942#if LWS_POSIX
Andy Greena717df22014-04-11 13:14:37 +0800943#define lws_feature_status_libev(_a) \
944 lwsl_notice("libev support not compiled in\n")
Andy Green8c0d3c02015-11-02 20:34:12 +0800945#else
946#define lws_feature_status_libev(_a)
947#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800948#endif
949
Andy Green86ed65f2016-02-14 09:27:41 +0800950#if defined(LWS_USE_LIBUV)
951LWS_EXTERN void
952lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
953LWS_EXTERN void
954lws_libuv_io(struct lws *wsi, int flags);
955LWS_EXTERN int
956lws_libuv_init_fd_table(struct lws_context *context);
957LWS_EXTERN void
958lws_libuv_run(const struct lws_context *context, int tsi);
959LWS_EXTERN void
960lws_libuv_destroyloop(struct lws_context *context, int tsi);
Andy Greenc6fd3602016-03-23 09:22:11 +0800961#define LWS_LIBUV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV)
Andy Green86ed65f2016-02-14 09:27:41 +0800962LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
963#else
964#define lws_libuv_accept(_a, _b) ((void) 0)
965#define lws_libuv_io(_a, _b) ((void) 0)
966#define lws_libuv_init_fd_table(_a) (0)
967#define lws_libuv_run(_a, _b) ((void) 0)
968#define lws_libuv_destroyloop(_a, _b) ((void) 0)
969#define LWS_LIBUV_ENABLED(context) (0)
970#if LWS_POSIX
971#define lws_feature_status_libuv(_a) \
972 lwsl_notice("libuv support not compiled in\n")
973#else
974#define lws_feature_status_libuv(_a)
975#endif
976#endif
977
978
Andy Green055f2972014-03-24 16:09:25 +0800979#ifdef LWS_USE_IPV6
Andy Green2dc7dde2016-06-03 21:19:40 +0800980#define LWS_IPV6_ENABLED(vh) \
Denis Osvaldc16c6c82016-06-03 17:40:12 +0200981 (!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
Andy Green2dc7dde2016-06-03 21:19:40 +0800982 !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
James Devine3f13ea22014-03-24 16:09:25 +0800983#else
984#define LWS_IPV6_ENABLED(context) (0)
985#endif
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800986
Yeonjun Lim3c6a8c12016-03-30 22:47:02 -0700987#ifdef LWS_USE_UNIX_SOCK
Andy Green144594d2016-04-14 12:11:51 +0800988#define LWS_UNIX_SOCK_ENABLED(vhost) \
989 (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
Yeonjun Lim3c6a8c12016-03-30 22:47:02 -0700990#else
Andy Green144594d2016-04-14 12:11:51 +0800991#define LWS_UNIX_SOCK_ENABLED(vhost) (0)
Yeonjun Lim3c6a8c12016-03-30 22:47:02 -0700992#endif
Andy Greenb1a9e502013-11-10 15:15:21 +0800993enum uri_path_states {
994 URIPS_IDLE,
995 URIPS_SEEN_SLASH,
996 URIPS_SEEN_SLASH_DOT,
997 URIPS_SEEN_SLASH_DOT_DOT,
998};
999
1000enum uri_esc_states {
1001 URIES_IDLE,
1002 URIES_SEEN_PERCENT,
1003 URIES_SEEN_PERCENT_H1,
1004};
Andy Greenf3d3b402011-02-09 07:16:34 +00001005
Andy Green024eb6c2014-10-08 12:00:53 +08001006/* notice that these union members:
Andy Green40110e82015-12-14 08:52:03 +08001007 *
Andy Green024eb6c2014-10-08 12:00:53 +08001008 * hdr
1009 * http
1010 * http2
Andy Green40110e82015-12-14 08:52:03 +08001011 *
Andy Green024eb6c2014-10-08 12:00:53 +08001012 * all have a pointer to allocated_headers struct as their first member.
Andy Green40110e82015-12-14 08:52:03 +08001013 *
Andy Green024eb6c2014-10-08 12:00:53 +08001014 * It means for allocated_headers access, the three union paths can all be
Peter Pentchevbb085da2015-12-03 15:55:11 +02001015 * used interchangeably to access the same data
Andy Green024eb6c2014-10-08 12:00:53 +08001016 */
1017
Andy Greena661ee52016-02-29 13:18:30 +08001018
1019#ifndef LWS_NO_CLIENT
1020struct client_info_stash {
1021 char address[256];
1022 char path[1024];
1023 char host[256];
1024 char origin[256];
1025 char protocol[256];
1026 char method[16];
1027};
1028#endif
1029
Andy Green2d8d35a2016-02-29 14:19:16 +08001030struct _lws_header_related {
1031 /* MUST be first in struct */
1032 struct allocated_headers *ah;
1033 struct lws *ah_wait_list;
1034 unsigned char *preamble_rx;
1035#ifndef LWS_NO_CLIENT
1036 struct client_info_stash *stash;
1037#endif
1038 unsigned int preamble_rx_len;
1039 enum uri_path_states ups;
1040 enum uri_esc_states ues;
1041 short lextable_pos;
Andy Green48895662016-06-02 12:32:38 +08001042 unsigned int current_token_limit;
Andy Green2d8d35a2016-02-29 14:19:16 +08001043#ifndef LWS_NO_CLIENT
1044 unsigned short c_port;
1045#endif
1046 char esc_stash;
1047 char post_literal_equal;
1048 unsigned char parser_state; /* enum lws_token_indexes */
1049 char redirects;
1050};
1051
Andy Green84fd9492013-11-09 11:40:32 +08001052struct _lws_http_mode_related {
Andy Green024eb6c2014-10-08 12:00:53 +08001053 /* MUST be first in struct */
Andy Green84fd9492013-11-09 11:40:32 +08001054 struct allocated_headers *ah; /* mirroring _lws_header_related */
Andy Green8c1f6022016-01-26 20:56:56 +08001055 struct lws *ah_wait_list;
Andy Green2d8d35a2016-02-29 14:19:16 +08001056 unsigned char *preamble_rx;
1057#ifndef LWS_NO_CLIENT
1058 struct client_info_stash *stash;
1059#endif
1060 unsigned int preamble_rx_len;
Andy Green8c1f6022016-01-26 20:56:56 +08001061 struct lws *new_wsi_list;
Andy Green84fd9492013-11-09 11:40:32 +08001062 unsigned long filepos;
1063 unsigned long filelen;
Andy Greenaa775fd2015-12-26 08:56:58 +08001064 lws_filefd_type fd;
kapejodce64fb02013-11-19 13:38:16 +01001065
Andrew Canadayafe26cf2014-07-13 01:07:36 -04001066 enum http_version request_version;
1067 enum http_connection_type connection_type;
Andy Green2cd30742015-11-02 13:10:33 +08001068 unsigned int content_length;
1069 unsigned int content_remain;
Andy Green84fd9492013-11-09 11:40:32 +08001070};
1071
Andy Green024eb6c2014-10-08 12:00:53 +08001072#ifdef LWS_USE_HTTP2
1073
1074enum lws_http2_settings {
1075 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
1076 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
1077 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
1078 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
1079 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
1080 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
Andy Green40110e82015-12-14 08:52:03 +08001081
Andy Green024eb6c2014-10-08 12:00:53 +08001082 LWS_HTTP2_SETTINGS__COUNT /* always last */
Andy Greena54f2322014-09-30 09:43:14 +08001083};
1084
Andy Green024eb6c2014-10-08 12:00:53 +08001085enum lws_http2_wellknown_frame_types {
1086 LWS_HTTP2_FRAME_TYPE_DATA,
1087 LWS_HTTP2_FRAME_TYPE_HEADERS,
1088 LWS_HTTP2_FRAME_TYPE_PRIORITY,
1089 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
1090 LWS_HTTP2_FRAME_TYPE_SETTINGS,
1091 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
1092 LWS_HTTP2_FRAME_TYPE_PING,
1093 LWS_HTTP2_FRAME_TYPE_GOAWAY,
1094 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
1095 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
Andy Green40110e82015-12-14 08:52:03 +08001096
Andy Green024eb6c2014-10-08 12:00:53 +08001097 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
1098};
1099
Andy Green91b05892014-10-17 08:38:44 +08001100enum lws_http2_flags {
1101 LWS_HTTP2_FLAG_END_STREAM = 1,
1102 LWS_HTTP2_FLAG_END_HEADERS = 4,
1103 LWS_HTTP2_FLAG_PADDED = 8,
1104 LWS_HTTP2_FLAG_PRIORITY = 0x20,
1105
1106 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
1107};
1108
Andy Green024eb6c2014-10-08 12:00:53 +08001109#define LWS_HTTP2_STREAM_ID_MASTER 0
1110#define LWS_HTTP2_FRAME_HEADER_LENGTH 9
1111#define LWS_HTTP2_SETTINGS_LENGTH 6
1112
1113struct http2_settings {
1114 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
1115};
1116
Andy Greenecc2e722014-10-09 16:57:47 +08001117enum http2_hpack_state {
Andy Green40110e82015-12-14 08:52:03 +08001118
Andy Green200f3852014-10-18 12:23:05 +08001119 /* optional before first header block */
1120 HPKS_OPT_PADDING,
1121 HKPS_OPT_E_DEPENDENCY,
1122 HKPS_OPT_WEIGHT,
Andy Green40110e82015-12-14 08:52:03 +08001123
Andy Green200f3852014-10-18 12:23:05 +08001124 /* header block */
Andy Greenecc2e722014-10-09 16:57:47 +08001125 HPKS_TYPE,
Andy Green40110e82015-12-14 08:52:03 +08001126
Andy Green2add6342014-10-12 08:38:16 +08001127 HPKS_IDX_EXT,
Andy Green40110e82015-12-14 08:52:03 +08001128
Andy Greenecc2e722014-10-09 16:57:47 +08001129 HPKS_HLEN,
1130 HPKS_HLEN_EXT,
1131
1132 HPKS_DATA,
Andy Green40110e82015-12-14 08:52:03 +08001133
Andy Green200f3852014-10-18 12:23:05 +08001134 /* optional after last header block */
1135 HKPS_OPT_DISCARD_PADDING,
Andy Greenecc2e722014-10-09 16:57:47 +08001136};
1137
Andy Green2add6342014-10-12 08:38:16 +08001138enum http2_hpack_type {
1139 HPKT_INDEXED_HDR_7,
1140 HPKT_INDEXED_HDR_6_VALUE_INCR,
1141 HPKT_LITERAL_HDR_VALUE_INCR,
1142 HPKT_INDEXED_HDR_4_VALUE,
1143 HPKT_LITERAL_HDR_VALUE,
1144 HPKT_SIZE_5
1145};
1146
Andy Green200f3852014-10-18 12:23:05 +08001147struct hpack_dt_entry {
1148 int token; /* additions that don't map to a token are ignored */
1149 int arg_offset;
1150 int arg_len;
1151};
1152
1153struct hpack_dynamic_table {
1154 struct hpack_dt_entry *entries;
1155 char *args;
1156 int pos;
1157 int next;
1158 int num_entries;
1159 int args_length;
1160};
1161
Andy Green024eb6c2014-10-08 12:00:53 +08001162struct _lws_http2_related {
Andy Green40110e82015-12-14 08:52:03 +08001163 /*
Andy Green024eb6c2014-10-08 12:00:53 +08001164 * having this first lets us also re-use all HTTP union code
1165 * and in turn, http_mode_related has allocated headers in right
1166 * place so we can use the header apis on the wsi directly still
1167 */
1168 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
1169
1170 struct http2_settings my_settings;
1171 struct http2_settings peer_settings;
Andy Green40110e82015-12-14 08:52:03 +08001172
Andy Green4b85c1d2015-12-04 11:08:32 +08001173 struct lws *parent_wsi;
1174 struct lws *next_child_wsi;
Andy Green024eb6c2014-10-08 12:00:53 +08001175
Andy Green200f3852014-10-18 12:23:05 +08001176 struct hpack_dynamic_table *hpack_dyn_table;
Andy Greenaa775fd2015-12-26 08:56:58 +08001177 struct lws *stream_wsi;
1178 unsigned char ping_payload[8];
1179 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
Andy Green40110e82015-12-14 08:52:03 +08001180
Andy Green024eb6c2014-10-08 12:00:53 +08001181 unsigned int count;
Andy Green024eb6c2014-10-08 12:00:53 +08001182 unsigned int length;
1183 unsigned int stream_id;
Andy Greenaa775fd2015-12-26 08:56:58 +08001184 enum http2_hpack_state hpack;
1185 enum http2_hpack_type hpack_type;
1186 unsigned int header_index;
1187 unsigned int hpack_len;
1188 unsigned int hpack_e_dep;
1189 int tx_credit;
1190 unsigned int my_stream_id;
1191 unsigned int child_count;
1192 int my_priority;
Andy Green67112662016-01-11 11:34:01 +08001193
Andy Green91b05892014-10-17 08:38:44 +08001194 unsigned int END_STREAM:1;
1195 unsigned int END_HEADERS:1;
Andy Green1cea5812014-10-19 07:36:20 +08001196 unsigned int send_END_STREAM:1;
Andy Green7df53c52014-10-22 15:37:28 +08001197 unsigned int GOING_AWAY;
1198 unsigned int requested_POLLOUT:1;
Andy Green97ee57f2014-10-29 09:39:08 +08001199 unsigned int waiting_tx_credit:1;
Andy Greenecc2e722014-10-09 16:57:47 +08001200 unsigned int huff:1;
1201 unsigned int value:1;
Andy Green40110e82015-12-14 08:52:03 +08001202
Andy Greenaa775fd2015-12-26 08:56:58 +08001203 unsigned short round_robin_POLLOUT;
1204 unsigned short count_POLLOUT_children;
1205 unsigned short hpack_pos;
1206
1207 unsigned char type;
1208 unsigned char flags;
1209 unsigned char frame_state;
1210 unsigned char padding;
1211 unsigned char hpack_m;
Andy Green024eb6c2014-10-08 12:00:53 +08001212 unsigned char initialized;
Andy Green024eb6c2014-10-08 12:00:53 +08001213};
1214
Andy Green7df53c52014-10-22 15:37:28 +08001215#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
Andy Green024eb6c2014-10-08 12:00:53 +08001216
1217#endif
1218
Andy Green623a98d2013-01-21 11:04:23 +08001219struct _lws_websocket_related {
Andy Green2c218e72016-02-15 12:37:04 +08001220 /* cheapest way to deal with ah overlap with ws union transition */
Andy Green26d42492016-02-24 12:40:21 +08001221 struct _lws_header_related hdr;
Andy Green67112662016-01-11 11:34:01 +08001222 char *rx_ubuf;
Andy Green4019aab2016-01-30 11:43:10 +08001223 unsigned int rx_ubuf_alloc;
Andy Green67112662016-01-11 11:34:01 +08001224 struct lws *rx_draining_ext_list;
1225 struct lws *tx_draining_ext_list;
Andy Greenf32d2502016-07-15 13:41:38 +08001226 time_t time_next_ping_check;
Andy Greende132b92015-12-25 13:48:20 +08001227 size_t rx_packet_length;
Andy Green67112662016-01-11 11:34:01 +08001228 unsigned int rx_ubuf_head;
1229 unsigned char mask[4];
Andy Green1fb95e82015-12-26 17:20:34 +08001230 /* Also used for close content... control opcode == < 128 */
Andy Green67112662016-01-11 11:34:01 +08001231 unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
Andy Green1fb95e82015-12-26 17:20:34 +08001232
Andy Greenaa775fd2015-12-26 08:56:58 +08001233 unsigned char ping_payload_len;
Andy Green67112662016-01-11 11:34:01 +08001234 unsigned char mask_idx;
Andy Green623a98d2013-01-21 11:04:23 +08001235 unsigned char opcode;
Andy Green623a98d2013-01-21 11:04:23 +08001236 unsigned char rsv;
Andy Green67112662016-01-11 11:34:01 +08001237 unsigned char rsv_first_msg;
Andy Green1fb95e82015-12-26 17:20:34 +08001238 /* zero if no info, or length including 2-byte close code */
1239 unsigned char close_in_ping_buffer_len;
Andy Green0c7b38b2015-12-29 09:46:03 +08001240 unsigned char utf8;
Andy Green67112662016-01-11 11:34:01 +08001241 unsigned char stashed_write_type;
1242 unsigned char tx_draining_stashed_wp;
Andy Greende132b92015-12-25 13:48:20 +08001243
1244 unsigned int final:1;
Andy Greend91d5e82013-02-10 16:00:47 +08001245 unsigned int frame_is_binary:1;
Andy Greend91d5e82013-02-10 16:00:47 +08001246 unsigned int all_zero_nonce:1;
Andy Greend91d5e82013-02-10 16:00:47 +08001247 unsigned int this_frame_masked:1;
Andy Green1f4267b2013-10-17 08:09:19 +08001248 unsigned int inside_frame:1; /* next write will be more of frame */
1249 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
Andy Green40d5abc2015-04-17 20:29:58 +08001250 unsigned int payload_is_close:1; /* process as PONG, but it is close */
Andy Greenba38a7e2015-12-25 13:14:09 +08001251 unsigned int ping_pending_flag:1;
Andy Green977734e2015-12-28 16:51:08 +08001252 unsigned int continuation_possible:1;
Andy Green91d624e2015-12-28 17:05:40 +08001253 unsigned int owed_a_fin:1;
Andy Green9b81d3c2015-12-29 12:28:48 +08001254 unsigned int check_utf8:1;
1255 unsigned int defeat_check_utf8:1;
Andy Green67112662016-01-11 11:34:01 +08001256 unsigned int pmce_compressed_message:1;
1257 unsigned int stashed_write_pending:1;
1258 unsigned int rx_draining_ext:1;
1259 unsigned int tx_draining_ext:1;
Andy Greenf32d2502016-07-15 13:41:38 +08001260 unsigned int send_check_ping:1;
Andy Green623a98d2013-01-21 11:04:23 +08001261};
1262
Andy Green6a8099b2016-02-21 21:25:48 +08001263#ifdef LWS_WITH_CGI
1264
1265/* wsi who is master of the cgi points to an lws_cgi */
1266
1267struct lws_cgi {
1268 struct lws_cgi *cgi_list;
1269 struct lws *stdwsi[3]; /* points to the associated stdin/out/err wsis */
1270 struct lws *wsi; /* owner */
Andy Greenf5efa742016-04-13 11:42:53 +08001271 unsigned long content_length;
1272 unsigned long content_length_seen;
Andy Green6a8099b2016-02-21 21:25:48 +08001273 int pipe_fds[3][2];
1274 int pid;
1275
1276 unsigned int being_closed:1;
1277};
Andy Green5c8906e2016-03-13 16:44:19 +08001278#endif
Andy Greenc3c2d6d2016-03-09 07:41:59 +08001279
Andy Green5c8906e2016-03-13 16:44:19 +08001280signed char char_to_hex(const char c);
1281
1282#ifndef LWS_NO_CLIENT
1283enum lws_chunk_parser {
1284 ELCP_HEX,
1285 ELCP_CR,
1286 ELCP_CONTENT,
1287 ELCP_POST_CR,
1288 ELCP_POST_LF,
1289};
Andy Green6a8099b2016-02-21 21:25:48 +08001290#endif
1291
Andy Green1e5a9ad2016-03-20 11:59:53 +08001292struct lws_rewrite;
1293
Andy Green2f0bc932016-04-15 12:00:23 +08001294#ifdef LWS_WITH_ACCESS_LOG
1295struct lws_access_log {
1296 char *header_log;
1297 char *user_agent;
1298 unsigned long sent;
1299 int response;
1300};
1301#endif
1302
Andy Green4b85c1d2015-12-04 11:08:32 +08001303struct lws {
Andy Green623a98d2013-01-21 11:04:23 +08001304
Andy Greenaa775fd2015-12-26 08:56:58 +08001305 /* structs */
1306 /* members with mutually exclusive lifetimes are unionized */
1307
1308 union u {
1309 struct _lws_http_mode_related http;
1310#ifdef LWS_USE_HTTP2
1311 struct _lws_http2_related http2;
1312#endif
1313 struct _lws_header_related hdr;
1314 struct _lws_websocket_related ws;
1315 } u;
1316
Andy Green623a98d2013-01-21 11:04:23 +08001317 /* lifetime members */
1318
Andy Green86ed65f2016-02-14 09:27:41 +08001319#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
Andy Green40110e82015-12-14 08:52:03 +08001320 struct lws_io_watcher w_read;
Andy Green86ed65f2016-02-14 09:27:41 +08001321#endif
1322#if defined(LWS_USE_LIBEV)
Andy Green40110e82015-12-14 08:52:03 +08001323 struct lws_io_watcher w_write;
Andy Green86ed65f2016-02-14 09:27:41 +08001324#endif
Andy Greende132b92015-12-25 13:48:20 +08001325 time_t pending_timeout_limit;
Andy Greenaa775fd2015-12-26 08:56:58 +08001326
1327 /* pointers */
1328
Andy Green40110e82015-12-14 08:52:03 +08001329 struct lws_context *context;
Andy Greend526c502016-03-28 10:10:43 +08001330 struct lws_vhost *vhost;
Andy Green494418a2016-03-02 09:17:22 +08001331 struct lws *parent; /* points to parent, if any */
1332 struct lws *child_list; /* points to first child */
1333 struct lws *sibling_list; /* subsequent children at same level */
Andy Green6a8099b2016-02-21 21:25:48 +08001334#ifdef LWS_WITH_CGI
1335 struct lws_cgi *cgi; /* wsi being cgi master have one of these */
Andy Green6a8099b2016-02-21 21:25:48 +08001336#endif
Andy Green4b85c1d2015-12-04 11:08:32 +08001337 const struct lws_protocols *protocol;
Andy Green4714cf02016-04-16 08:40:35 +08001338 struct lws **same_vh_protocol_prev, *same_vh_protocol_next;
Andy Greend738f842016-01-19 04:32:14 +08001339 struct lws *timeout_list;
1340 struct lws **timeout_list_prev;
Andy Green2f0bc932016-04-15 12:00:23 +08001341#ifdef LWS_WITH_ACCESS_LOG
1342 struct lws_access_log access_log;
1343#endif
Andy Greende132b92015-12-25 13:48:20 +08001344 void *user_space;
1345 /* rxflow handling */
1346 unsigned char *rxflow_buffer;
1347 /* truncated send handling */
1348 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
Andy Green7acf76c2016-07-23 14:18:25 +08001349
1350#if defined (LWS_WITH_ESP8266)
1351 void *premature_rx;
1352 unsigned short prem_rx_size, prem_rx_pos;
1353#endif
1354
Andy Green3182ece2013-01-20 17:08:31 +08001355#ifndef LWS_NO_EXTENSIONS
Andy Greend2ac22c2015-12-11 10:45:35 +08001356 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green67112662016-01-11 11:34:01 +08001357 void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
Andy Green3182ece2013-01-20 17:08:31 +08001358#endif
Andy Greende132b92015-12-25 13:48:20 +08001359#ifdef LWS_OPENSSL_SUPPORT
1360 SSL *ssl;
Andy Green1a3f1772016-03-28 19:58:02 +08001361#if !defined(LWS_USE_POLARSSL) && !defined(LWS_USE_MBEDTLS)
Andy Greende132b92015-12-25 13:48:20 +08001362 BIO *client_bio;
Andy Green1a3f1772016-03-28 19:58:02 +08001363#endif
Andy Greende132b92015-12-25 13:48:20 +08001364 struct lws *pending_read_list_prev, *pending_read_list_next;
1365#endif
Andy Green1a138852016-03-20 11:55:25 +08001366#ifdef LWS_WITH_HTTP_PROXY
Andy Green1e5a9ad2016-03-20 11:59:53 +08001367 struct lws_rewrite *rw;
1368#endif
Andy Greenaa775fd2015-12-26 08:56:58 +08001369#ifdef LWS_LATENCY
1370 unsigned long action_start;
1371 unsigned long latency_start;
1372#endif
1373 /* pointer / int */
Andy Green3b193862015-11-02 13:13:44 +08001374 lws_sockfd_type sock;
Andy Greende132b92015-12-25 13:48:20 +08001375
Andy Greenaa775fd2015-12-26 08:56:58 +08001376 /* ints */
Andy Greendfb23042013-01-17 12:26:48 +08001377 int position_in_fds_table;
Andy Green024eb6c2014-10-08 12:00:53 +08001378 int rxflow_len;
1379 int rxflow_pos;
Andy Green54806b12015-12-17 17:03:59 +08001380 unsigned int trunc_alloc_len; /* size of malloc */
1381 unsigned int trunc_offset; /* where we are in terms of spilling */
1382 unsigned int trunc_len; /* how much is buffered */
Andy Green5c8906e2016-03-13 16:44:19 +08001383#ifndef LWS_NO_CLIENT
1384 int chunk_remaining;
1385#endif
Andy Green42e8b182016-04-22 08:53:49 +08001386 unsigned int cache_secs;
Andy Green2764eba2013-12-09 14:16:17 +08001387
Andy Greende132b92015-12-25 13:48:20 +08001388 unsigned int hdr_parsing_completed:1;
Andy Green22d6f392016-04-10 09:33:54 +08001389 unsigned int http2_substream:1;
Andy Greend526c502016-03-28 10:10:43 +08001390 unsigned int listener:1;
Andy Greende132b92015-12-25 13:48:20 +08001391 unsigned int user_space_externally_allocated:1;
1392 unsigned int socket_is_permanently_unusable:1;
1393 unsigned int rxflow_change_to:2;
Andy Green83af28a2016-02-28 10:55:31 +08001394 unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
Andy Green98061402016-04-15 14:01:29 +08001395 unsigned int conn_stat_done:1;
Andy Green42e8b182016-04-22 08:53:49 +08001396 unsigned int cache_reuse:1;
1397 unsigned int cache_revalidate:1;
1398 unsigned int cache_intermediaries:1;
Andy Green2f216282016-04-23 09:26:11 +08001399 unsigned int favoured_pollin:1;
Andy Green7a2fc442016-05-19 15:28:31 +08001400 unsigned int sending_chunked:1;
Andy Green81c221e2016-07-01 08:54:39 +08001401 unsigned int already_did_cce:1;
Andy Green723b3f12016-09-06 15:36:51 +08001402 unsigned int told_user_closed:1;
Andy Green7acf76c2016-07-23 14:18:25 +08001403#if defined(LWS_WITH_ESP8266)
1404 unsigned int pending_send_completion:3;
1405 unsigned int close_is_pending_send_completion:1;
1406#endif
Andy Green2f0bc932016-04-15 12:00:23 +08001407#ifdef LWS_WITH_ACCESS_LOG
1408 unsigned int access_log_pending:1;
1409#endif
Andy Greena661ee52016-02-29 13:18:30 +08001410#ifndef LWS_NO_CLIENT
1411 unsigned int do_ws:1; /* whether we are doing http or ws flow */
Andy Green5c8906e2016-03-13 16:44:19 +08001412 unsigned int chunked:1; /* if the clientside connection is chunked */
Andy Green1e5a9ad2016-03-20 11:59:53 +08001413 unsigned int client_rx_avail:1;
Andy Green95fff472016-08-08 21:54:30 +08001414 unsigned int client_http_body_pending:1;
Andy Green1a138852016-03-20 11:55:25 +08001415#endif
1416#ifdef LWS_WITH_HTTP_PROXY
Andy Green1e5a9ad2016-03-20 11:59:53 +08001417 unsigned int perform_rewrite:1;
Andy Greena661ee52016-02-29 13:18:30 +08001418#endif
Andy Greende132b92015-12-25 13:48:20 +08001419#ifndef LWS_NO_EXTENSIONS
1420 unsigned int extension_data_pending:1;
1421#endif
1422#ifdef LWS_OPENSSL_SUPPORT
Andy Green675c3492016-07-07 08:14:26 +08001423 unsigned int use_ssl:3;
Andy Greende132b92015-12-25 13:48:20 +08001424 unsigned int upgraded:1;
1425#endif
Andy Greenaa775fd2015-12-26 08:56:58 +08001426#ifdef _WIN32
1427 unsigned int sock_send_blocking:1;
1428#endif
Andy Green0f9904f2016-03-17 15:26:49 +08001429#ifdef LWS_OPENSSL_SUPPORT
1430 unsigned int redirect_to_https:1;
1431#endif
Andy Greende132b92015-12-25 13:48:20 +08001432
Andy Greenaa775fd2015-12-26 08:56:58 +08001433 /* chars */
Andy Greende132b92015-12-25 13:48:20 +08001434#ifndef LWS_NO_EXTENSIONS
Andy Green67112662016-01-11 11:34:01 +08001435 unsigned char count_act_ext;
Andy Greende132b92015-12-25 13:48:20 +08001436#endif
1437 unsigned char ietf_spec_revision;
1438 char mode; /* enum connection_mode */
1439 char state; /* enum lws_connection_states */
Andy Green8c1f6022016-01-26 20:56:56 +08001440 char state_pre_close;
Andy Greende132b92015-12-25 13:48:20 +08001441 char lws_rx_parse_state; /* enum lws_rx_parse_state */
1442 char rx_frame_type; /* enum lws_write_protocol */
1443 char pending_timeout; /* enum pending_timeout */
Andy Greend738f842016-01-19 04:32:14 +08001444 char pps; /* enum lws_pending_protocol_send */
Andy Greend3a55052016-01-19 03:34:24 +08001445 char tsi; /* thread service index we belong to */
Andy Green7a2fc442016-05-19 15:28:31 +08001446 char protocol_interpret_idx;
Andy Green6a8099b2016-02-21 21:25:48 +08001447#ifdef LWS_WITH_CGI
1448 char cgi_channel; /* which of stdin/out/err */
Andy Greenc3c2d6d2016-03-09 07:41:59 +08001449 char hdr_state;
Andy Green6a8099b2016-02-21 21:25:48 +08001450#endif
Andy Green5c8906e2016-03-13 16:44:19 +08001451#ifndef LWS_NO_CLIENT
1452 char chunk_parser; /* enum lws_chunk_parser */
1453#endif
Andy Green0a4da2c2016-05-10 10:16:52 +08001454#if defined(LWS_WITH_CGI) || !defined(LWS_NO_CLIENT)
1455 char reason_bf; /* internal writeable callback reason bitfield */
1456#endif
Andy Green7c212cc2010-11-08 20:20:42 +00001457};
1458
Andy Green3d67f512014-04-03 07:29:50 +08001459LWS_EXTERN int log_level;
1460
Andy Greenc7939442016-03-12 08:18:58 +08001461LWS_EXTERN int
Andy Green144594d2016-04-14 12:11:51 +08001462lws_socket_bind(struct lws_vhost *vhost, int sockfd, int port,
Andy Greenc7939442016-03-12 08:18:58 +08001463 const char *iface);
1464
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001465LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +08001466lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
Andy Green508946c2013-02-12 10:19:08 +08001467
Andy Green34f3dd22014-04-03 07:42:50 +08001468LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001469remove_wsi_socket_from_fds(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +08001470LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001471lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
Andy Green34f3dd22014-04-03 07:42:50 +08001472
Andy Greend636e352013-01-29 12:36:17 +08001473#ifndef LWS_LATENCY
Andy Greendc8a3a82015-12-06 09:15:27 +08001474static inline void
1475lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1476 int ret, int completion) {
Andy Green40110e82015-12-14 08:52:03 +08001477 do {
1478 (void)context; (void)wsi; (void)action; (void)ret;
1479 (void)completion;
Andy Greendc8a3a82015-12-06 09:15:27 +08001480 } while (0);
1481}
1482static inline void
1483lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1484 do { (void)context; (void)wsi; } while (0);
1485}
Andy Greend636e352013-01-29 12:36:17 +08001486#else
1487#define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1488extern void
Andy Greendc8a3a82015-12-06 09:15:27 +08001489lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1490 int ret, int completion);
Andy Greend636e352013-01-29 12:36:17 +08001491#endif
1492
Andy Greendc8a3a82015-12-06 09:15:27 +08001493LWS_EXTERN void
Andy Green6b5de702015-12-15 21:15:58 +08001494lws_set_protocol_write_pending(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001495 enum lws_pending_protocol_send pend);
Andy Greene99a83c2016-01-20 16:56:06 +08001496LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001497lws_client_rx_sm(struct lws *wsi, unsigned char c);
Andy Green4739e5c2011-01-22 12:51:57 +00001498
Andy Greene99a83c2016-01-20 16:56:06 +08001499LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001500lws_parse(struct lws *wsi, unsigned char c);
Andy Greenb45993c2010-12-18 15:13:50 +00001501
Andy Greene99a83c2016-01-20 16:56:06 +08001502LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001503lws_http_action(struct lws *wsi);
Andy Green024eb6c2014-10-08 12:00:53 +08001504
1505LWS_EXTERN int
Andy Greendf736162011-01-18 15:39:02 +00001506lws_b64_selftest(void);
Andy Greenbfb051f2011-02-09 08:49:14 +00001507
Andy Green2c218e72016-02-15 12:37:04 +08001508LWS_EXTERN int
1509lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
1510
1511LWS_EXTERN int
1512lws_service_flag_pending(struct lws_context *context, int tsi);
1513
Andy Green7acf76c2016-07-23 14:18:25 +08001514#if defined(_WIN32) || defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
Andy Green4b85c1d2015-12-04 11:08:32 +08001515LWS_EXTERN struct lws *
Andy Green1fa76852015-12-14 11:17:16 +08001516wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
Andy Green0d338332011-02-12 11:57:43 +00001517
Andy Green40110e82015-12-14 08:52:03 +08001518LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001519insert_wsi(struct lws_context *context, struct lws *wsi);
Bud Davis229bfec2015-01-30 10:13:01 +08001520
1521LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001522delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
Bud Davis229bfec2015-01-30 10:13:01 +08001523#else
Andy Green40110e82015-12-14 08:52:03 +08001524#define wsi_from_fd(A,B) A->lws_lookup[B]
Andy Green8c1f6022016-01-26 20:56:56 +08001525#define insert_wsi(A,B) assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
Bud Davis229bfec2015-01-30 10:13:01 +08001526#define delete_from_fd(A,B) A->lws_lookup[B]=0
1527#endif
1528
Andy Greene99a83c2016-01-20 16:56:06 +08001529LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001530insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
Darin Willitsc19456f2011-02-14 17:52:39 +00001531
Andy Greene99a83c2016-01-20 16:56:06 +08001532LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001533lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
Andy Greend44bf7f2011-03-06 10:29:38 +00001534
Andy Green95a7b5d2011-03-06 10:29:39 +00001535
Andy Greene99a83c2016-01-20 16:56:06 +08001536LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001537lws_service_timeout_check(struct lws *wsi, unsigned int sec);
Andy Greena41314f2011-05-23 10:00:03 +01001538
Andy Greene99a83c2016-01-20 16:56:06 +08001539LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001540lws_client_connect_2(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001541
Andy Greene99a83c2016-01-20 16:56:06 +08001542LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1543lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
1544 const char *path, const char *host);
Andy Green809d69a2016-01-14 11:37:56 +08001545
Andy Greene99a83c2016-01-20 16:56:06 +08001546LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
Andy Greend526c502016-03-28 10:10:43 +08001547lws_create_new_server_wsi(struct lws_vhost *vhost);
Andy Greena41314f2011-05-23 10:00:03 +01001548
Andy Greene99a83c2016-01-20 16:56:06 +08001549LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001550lws_generate_client_handshake(struct lws *wsi, char *pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001551
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001552LWS_EXTERN int
Andy Green6b5de702015-12-15 21:15:58 +08001553lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
Andy Green91b05892014-10-17 08:38:44 +08001554
Andy Green2d8d35a2016-02-29 14:19:16 +08001555LWS_EXTERN struct lws *
1556lws_client_connect_via_info2(struct lws *wsi);
1557
Andy Greencdb9bf92014-04-12 10:07:02 +08001558/*
1559 * EXTENSIONS
1560 */
1561
Andy Green3182ece2013-01-20 17:08:31 +08001562#ifndef LWS_NO_EXTENSIONS
Andy Greencdb9bf92014-04-12 10:07:02 +08001563LWS_VISIBLE void
1564lws_context_init_extensions(struct lws_context_creation_info *info,
Andy Greendc8a3a82015-12-06 09:15:27 +08001565 struct lws_context *context);
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001566LWS_EXTERN int
Andy Greene99a83c2016-01-20 16:56:06 +08001567lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
Andy Green6ee372f2012-04-09 15:09:01 +08001568 void *v, size_t len);
Andy Greena41314f2011-05-23 10:00:03 +01001569
Andy Green2c24ec02014-04-02 19:45:42 +08001570LWS_EXTERN int
Andy Greene99a83c2016-01-20 16:56:06 +08001571lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
Andy Green2c24ec02014-04-02 19:45:42 +08001572LWS_EXTERN int
Andy Greene99a83c2016-01-20 16:56:06 +08001573lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1574 void *arg, int len);
Andy Green67112662016-01-11 11:34:01 +08001575
Andy Green2c24ec02014-04-02 19:45:42 +08001576#else
Andy Green6b5de702015-12-15 21:15:58 +08001577#define lws_any_extension_handled(_a, _b, _c, _d) (0)
Andy Green67112662016-01-11 11:34:01 +08001578#define lws_ext_cb_active(_a, _b, _c, _d) (0)
Andy Green54806b12015-12-17 17:03:59 +08001579#define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
Andy Greenb49a9952014-04-03 10:11:04 +08001580#define lws_issue_raw_ext_access lws_issue_raw
Andy Greencdb9bf92014-04-12 10:07:02 +08001581#define lws_context_init_extensions(_a, _b)
Andy Green3182ece2013-01-20 17:08:31 +08001582#endif
Andy Greena41314f2011-05-23 10:00:03 +01001583
Andy Greene99a83c2016-01-20 16:56:06 +08001584LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001585lws_client_interpret_server_handshake(struct lws *wsi);
Andy Greena41314f2011-05-23 10:00:03 +01001586
Andy Greene99a83c2016-01-20 16:56:06 +08001587LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001588lws_rx_sm(struct lws *wsi, unsigned char c);
Andy Greena41314f2011-05-23 10:00:03 +01001589
Andy Greenc15714f2016-09-10 04:43:07 +08001590LWS_EXTERN int
Alex Hultman599cad92016-03-17 09:34:15 +08001591lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
1592
Andy Greene99a83c2016-01-20 16:56:06 +08001593LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001594lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
Andy Green09226502011-05-28 10:19:19 +01001595
Andy Green44c11612014-11-08 11:18:47 +08001596LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001597lws_union_transition(struct lws *wsi, enum connection_mode mode);
Andy Green44c11612014-11-08 11:18:47 +08001598
Andy Greene99a83c2016-01-20 16:56:06 +08001599LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Denis Osvald034e5142015-12-21 18:06:38 +01001600user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
Andy Green00c6d152015-12-17 07:54:44 +08001601 enum lws_callback_reasons reason, void *user,
1602 void *in, size_t len);
Andy Green024eb6c2014-10-08 12:00:53 +08001603#ifdef LWS_USE_HTTP2
Andy Green4b85c1d2015-12-04 11:08:32 +08001604LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1605struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
Andy Green024eb6c2014-10-08 12:00:53 +08001606LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001607lws_http2_interpret_settings_payload(struct http2_settings *settings,
1608 unsigned char *buf, int len);
Andy Green024eb6c2014-10-08 12:00:53 +08001609LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1610LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001611lws_http2_parser(struct lws *wsi, unsigned char c);
Andy Greendc8a3a82015-12-06 09:15:27 +08001612LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1613 struct lws *wsi);
1614LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1615 unsigned int sid, unsigned int len,
1616 unsigned char *buf);
Andy Green4b85c1d2015-12-04 11:08:32 +08001617LWS_EXTERN struct lws *
1618lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
Andy Green11c05bf2015-12-16 18:19:08 +08001619LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
Andy Green2add6342014-10-12 08:38:16 +08001620 unsigned char c);
Andy Green917f43a2014-10-12 14:31:47 +08001621LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001622lws_add_http2_header_by_name(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001623 const unsigned char *name,
1624 const unsigned char *value, int length,
1625 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001626LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001627lws_add_http2_header_by_token(struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001628 enum lws_token_indexes token,
Andy Greendc8a3a82015-12-06 09:15:27 +08001629 const unsigned char *value, int length,
1630 unsigned char **p, unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001631LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001632lws_add_http2_header_status(struct lws *wsi,
Andy Greendc8a3a82015-12-06 09:15:27 +08001633 unsigned int code, unsigned char **p,
Andy Green917f43a2014-10-12 14:31:47 +08001634 unsigned char *end);
Andy Green7df53c52014-10-22 15:37:28 +08001635LWS_EXTERN
Andy Green4b85c1d2015-12-04 11:08:32 +08001636void lws_http2_configure_if_upgraded(struct lws *wsi);
Andy Green7df53c52014-10-22 15:37:28 +08001637#else
1638#define lws_http2_configure_if_upgraded(x)
Andy Green024eb6c2014-10-08 12:00:53 +08001639#endif
Andy Green706961d2013-01-17 16:50:35 +08001640
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001641LWS_EXTERN int
Andy Greend526c502016-03-28 10:10:43 +08001642lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd);
Andy Greena690cd02013-02-09 12:25:31 +08001643
Andy Piper6ff571f2016-06-28 19:01:20 +08001644LWS_EXTERN int
1645lws_plat_check_connection_error(struct lws *wsi);
1646
Andy Greene99a83c2016-01-20 16:56:06 +08001647LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greene3d141d2016-02-27 11:42:22 +08001648lws_header_table_attach(struct lws *wsi, int autoservice);
Andy Green16ab3182013-02-10 18:02:31 +08001649
Andrew Canaday37718812014-11-07 11:20:59 +08001650LWS_EXTERN int
Andy Greene3d141d2016-02-27 11:42:22 +08001651lws_header_table_detach(struct lws *wsi, int autoservice);
Andrew Canaday37718812014-11-07 11:20:59 +08001652
Andy Green4019aab2016-01-30 11:43:10 +08001653LWS_EXTERN void
Andy Greene3d141d2016-02-27 11:42:22 +08001654lws_header_table_reset(struct lws *wsi, int autoservice);
Andy Green4019aab2016-01-30 11:43:10 +08001655
Andy Greene99a83c2016-01-20 16:56:06 +08001656LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001657lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001658
Andy Greene99a83c2016-01-20 16:56:06 +08001659LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green11c05bf2015-12-16 18:19:08 +08001660lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
Andy Greenb5b23192013-02-11 17:13:32 +08001661
Andy Green16146cd2016-04-23 07:53:46 +08001662LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001663lws_ensure_user_space(struct lws *wsi);
Andy Green2af4d5b2013-02-18 16:30:10 +08001664
Andy Green158e8042014-04-02 14:25:10 +08001665LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001666lws_change_pollfd(struct lws *wsi, int _and, int _or);
Andy Green91f19d82013-12-21 11:18:34 +08001667
Andy Greenb5b23192013-02-11 17:13:32 +08001668#ifndef LWS_NO_SERVER
Andy Greene38031a2014-04-03 08:24:29 +08001669int lws_context_init_server(struct lws_context_creation_info *info,
Andy Greend526c502016-03-28 10:10:43 +08001670 struct lws_vhost *vhost);
1671LWS_EXTERN struct lws_vhost *
1672lws_select_vhost(struct lws_context *context, int port, const char *servername);
Andy Greend7340c12014-04-10 14:08:10 +08001673LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001674handshake_0405(struct lws_context *context, struct lws *wsi);
Andy Greene99a83c2016-01-20 16:56:06 +08001675LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green67112662016-01-11 11:34:01 +08001676lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
Andy Greencdb9bf92014-04-12 10:07:02 +08001677LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001678lws_server_get_canonical_hostname(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001679 struct lws_context_creation_info *info);
Andy Greene38031a2014-04-03 08:24:29 +08001680#else
1681#define lws_context_init_server(_a, _b) (0)
Andy Green3ef579b2015-12-04 09:23:56 +08001682#define lws_interpret_incoming_packet(_a, _b, _c) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001683#define lws_server_get_canonical_hostname(_a, _b)
Andy Greenb5b23192013-02-11 17:13:32 +08001684#endif
1685
1686#ifndef LWS_NO_DAEMONIZE
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001687LWS_EXTERN int get_daemonize_pid();
Andy Greencdb9bf92014-04-12 10:07:02 +08001688#else
1689#define get_daemonize_pid() (0)
Andy Greenb5b23192013-02-11 17:13:32 +08001690#endif
1691
Andy Green7acf76c2016-07-23 14:18:25 +08001692#if !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
Andy Greene99a83c2016-01-20 16:56:06 +08001693LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green2dc7dde2016-06-03 21:19:40 +08001694interface_to_sa(struct lws_vhost *vh, const char *ifname,
Andy Greendc8a3a82015-12-06 09:15:27 +08001695 struct sockaddr_in *addr, size_t addrlen);
Andy Green2cd30742015-11-02 13:10:33 +08001696#endif
Andy Greenb25b85f2014-04-03 23:34:09 +08001697LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1698
Andy Green1a308e42014-04-08 07:26:30 +01001699enum lws_ssl_capable_status {
1700 LWS_SSL_CAPABLE_ERROR = -1,
1701 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1702};
1703
Darin Willitsc19456f2011-02-14 17:52:39 +00001704#ifndef LWS_OPENSSL_SUPPORT
Andy Greencdb9bf92014-04-12 10:07:02 +08001705#define LWS_SSL_ENABLED(context) (0)
Andy Greenc57037a2014-04-03 10:17:00 +08001706#define lws_context_init_server_ssl(_a, _b) (0)
1707#define lws_ssl_destroy(_a)
Andy Green2eedea92014-04-03 14:33:48 +08001708#define lws_context_init_http2_ssl(_a)
Andy Green02138122014-04-06 06:26:35 +01001709#define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1710#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 +02001711#define lws_ssl_pending lws_ssl_pending_no_ssl
Andy Green8c1f6022016-01-26 20:56:56 +08001712#define lws_server_socket_service_ssl(_b, _c) (0)
Andy Greencdb9bf92014-04-12 10:07:02 +08001713#define lws_ssl_close(_a) (0)
1714#define lws_ssl_context_destroy(_a)
Andy Greend526c502016-03-28 10:10:43 +08001715#define lws_ssl_SSL_CTX_destroy(_a)
Andy Green6b5de702015-12-15 21:15:58 +08001716#define lws_ssl_remove_wsi_from_buffered_list(_a)
Andy Greend526c502016-03-28 10:10:43 +08001717#define lws_context_init_ssl_library(_a)
Andy Greenb5b23192013-02-11 17:13:32 +08001718#else
Andy Greencdb9bf92014-04-12 10:07:02 +08001719#define LWS_SSL_ENABLED(context) (context->use_ssl)
Joakim Soderbergf272cb02013-02-13 09:29:26 +08001720LWS_EXTERN int openssl_websocket_private_data_index;
Andy Greene99a83c2016-01-20 16:56:06 +08001721LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001722lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
Andy Greene99a83c2016-01-20 16:56:06 +08001723LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001724lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
Andy Greene99a83c2016-01-20 16:56:06 +08001725LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001726lws_ssl_pending(struct lws *wsi);
Andy Greend526c502016-03-28 10:10:43 +08001727LWS_EXTERN int
1728lws_context_init_ssl_library(struct lws_context_creation_info *info);
Andy Greene99a83c2016-01-20 16:56:06 +08001729LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green8c1f6022016-01-26 20:56:56 +08001730lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
Andy Greencdb9bf92014-04-12 10:07:02 +08001731LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001732lws_ssl_close(struct lws *wsi);
Andy Greencdb9bf92014-04-12 10:07:02 +08001733LWS_EXTERN void
Andy Greend526c502016-03-28 10:10:43 +08001734lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost);
1735LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001736lws_ssl_context_destroy(struct lws_context *context);
Andy Green52815602015-01-29 08:36:18 +08001737LWS_VISIBLE void
Andy Green6b5de702015-12-15 21:15:58 +08001738lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
Andy Greeneefb13a2016-03-28 12:43:55 +08001739LWS_EXTERN int
1740lws_ssl_client_bio_create(struct lws *wsi);
1741LWS_EXTERN int
1742lws_ssl_client_connect1(struct lws *wsi);
1743LWS_EXTERN int
1744lws_ssl_client_connect2(struct lws *wsi);
Andy Greenf1fd8822016-05-03 07:26:10 +08001745LWS_EXTERN void
1746lws_ssl_elaborate_error(void);
Andy Greenc57037a2014-04-03 10:17:00 +08001747#ifndef LWS_NO_SERVER
1748LWS_EXTERN int
1749lws_context_init_server_ssl(struct lws_context_creation_info *info,
Andy Greend526c502016-03-28 10:10:43 +08001750 struct lws_vhost *vhost);
Andy Greenc57037a2014-04-03 10:17:00 +08001751#else
1752#define lws_context_init_server_ssl(_a, _b) (0)
1753#endif
1754LWS_EXTERN void
Andy Greend526c502016-03-28 10:10:43 +08001755lws_ssl_destroy(struct lws_vhost *vhost);
Andy Green2eedea92014-04-03 14:33:48 +08001756
1757/* HTTP2-related */
1758
1759#ifdef LWS_USE_HTTP2
1760LWS_EXTERN void
Andy Green22d6f392016-04-10 09:33:54 +08001761lws_context_init_http2_ssl(struct lws_vhost *vhost);
Andy Green2eedea92014-04-03 14:33:48 +08001762#else
1763#define lws_context_init_http2_ssl(_a)
1764#endif
Darin Willitsc19456f2011-02-14 17:52:39 +00001765#endif
Andy Greena654fc02014-04-03 07:16:40 +08001766
Andy Green8c1f6022016-01-26 20:56:56 +08001767#if LWS_MAX_SMP > 1
1768static LWS_INLINE void
1769lws_pt_mutex_init(struct lws_context_per_thread *pt)
1770{
1771 pthread_mutex_init(&pt->lock, NULL);
1772}
Sterling Jensenecaed5e2016-05-12 20:22:35 -05001773
1774static LWS_INLINE void
1775lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
1776{
1777 pthread_mutex_destroy(&pt->lock);
1778}
1779
Andy Green8c1f6022016-01-26 20:56:56 +08001780static LWS_INLINE void
1781lws_pt_lock(struct lws_context_per_thread *pt)
1782{
Andy Greenbbf93692016-08-07 08:33:08 +08001783 if (!pt->lock_depth++)
1784 pthread_mutex_lock(&pt->lock);
Andy Green8c1f6022016-01-26 20:56:56 +08001785}
1786
1787static LWS_INLINE void
1788lws_pt_unlock(struct lws_context_per_thread *pt)
1789{
Andy Greenbbf93692016-08-07 08:33:08 +08001790 if (!(--pt->lock_depth))
1791 pthread_mutex_unlock(&pt->lock);
Andy Green8c1f6022016-01-26 20:56:56 +08001792}
1793#else
1794#define lws_pt_mutex_init(_a) (void)(_a)
Sterling Jensenecaed5e2016-05-12 20:22:35 -05001795#define lws_pt_mutex_destroy(_a) (void)(_a)
Andy Green8c1f6022016-01-26 20:56:56 +08001796#define lws_pt_lock(_a) (void)(_a)
1797#define lws_pt_unlock(_a) (void)(_a)
1798#endif
1799
Andy Greene99a83c2016-01-20 16:56:06 +08001800LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green6b5de702015-12-15 21:15:58 +08001801lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001802
Andy Greene99a83c2016-01-20 16:56:06 +08001803LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001804lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
Patrick Gansterera6b019a2014-04-15 18:40:31 +02001805
Andy Greene99a83c2016-01-20 16:56:06 +08001806LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green4b85c1d2015-12-04 11:08:32 +08001807lws_ssl_pending_no_ssl(struct lws *wsi);
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +02001808
Andy Green1e5a9ad2016-03-20 11:59:53 +08001809#ifdef LWS_WITH_HTTP_PROXY
1810struct lws_rewrite {
1811 hubbub_parser *parser;
1812 hubbub_parser_optparams params;
1813 const char *from, *to;
1814 int from_len, to_len;
1815 unsigned char *p, *end;
1816 struct lws *wsi;
1817};
1818static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
1819{
1820 if (s->len != len)
1821 return 1;
1822
1823 return strncmp((const char *)s->ptr, p, len);
1824}
1825typedef hubbub_error (*hubbub_callback_t)(const hubbub_token *token, void *pw);
Andy Green1e5a9ad2016-03-20 11:59:53 +08001826LWS_EXTERN struct lws_rewrite *
1827lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from, const char *to);
1828LWS_EXTERN void
1829lws_rewrite_destroy(struct lws_rewrite *r);
1830LWS_EXTERN int
1831lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
1832#endif
1833
1834#ifndef LWS_NO_CLIENT
Andy Green1a138852016-03-20 11:55:25 +08001835LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1836 struct lws *wsi,
1837 struct lws_pollfd *pollfd);
1838LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1839lws_http_transaction_completed_client(struct lws *wsi);
Andy Greenc57037a2014-04-03 10:17:00 +08001840#ifdef LWS_OPENSSL_SUPPORT
Andy Greendc8a3a82015-12-06 09:15:27 +08001841LWS_EXTERN int
1842lws_context_init_client_ssl(struct lws_context_creation_info *info,
Andy Greend526c502016-03-28 10:10:43 +08001843 struct lws_vhost *vhost);
Andy Greene38031a2014-04-03 08:24:29 +08001844#else
Andy Greenc57037a2014-04-03 10:17:00 +08001845 #define lws_context_init_client_ssl(_a, _b) (0)
1846#endif
Andy Greene99a83c2016-01-20 16:56:06 +08001847LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Greendc8a3a82015-12-06 09:15:27 +08001848lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1849LWS_EXTERN void
1850lws_decode_ssl_error(void);
Andy Greenc57037a2014-04-03 10:17:00 +08001851#else
1852#define lws_context_init_client_ssl(_a, _b) (0)
Andy Greenaad2eac2014-04-03 09:03:37 +08001853#define lws_handshake_client(_a, _b, _c) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001854#endif
Andy Green44e0b082015-12-28 14:24:49 +08001855
1856LWS_EXTERN int
1857_lws_rx_flow_control(struct lws *wsi);
1858
Andy Green8c1f6022016-01-26 20:56:56 +08001859LWS_EXTERN int
1860_lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1861
Andy Greena654fc02014-04-03 07:16:40 +08001862#ifndef LWS_NO_SERVER
Andy Greendc8a3a82015-12-06 09:15:27 +08001863LWS_EXTERN int
1864lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1865 struct lws_pollfd *pollfd);
1866LWS_EXTERN int
Andy Green11c05bf2015-12-16 18:19:08 +08001867lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
Andy Greenb3d21f12015-12-25 09:12:08 +08001868LWS_EXTERN int
Andy Green8c1f6022016-01-26 20:56:56 +08001869_lws_server_listen_accept_flow_control(struct lws *twsi, int on);
Andy Greend99476b2014-04-03 08:40:05 +08001870#else
Andy Greenb49a9952014-04-03 10:11:04 +08001871#define lws_server_socket_service(_a, _b, _c) (0)
Andy Green11c05bf2015-12-16 18:19:08 +08001872#define lws_handshake_server(_a, _b, _c) (0)
Andy Greenb3d21f12015-12-25 09:12:08 +08001873#define _lws_server_listen_accept_flow_control(a, b) (0)
Andy Greena654fc02014-04-03 07:16:40 +08001874#endif
Andy Green40110e82015-12-14 08:52:03 +08001875
Andy Green2f0bc932016-04-15 12:00:23 +08001876#ifdef LWS_WITH_ACCESS_LOG
1877LWS_EXTERN int
1878lws_access_log(struct lws *wsi);
1879#else
1880#define lws_access_log(_a)
1881#endif
1882
Andy Green6a8099b2016-02-21 21:25:48 +08001883LWS_EXTERN int
1884lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1885
Andy Green02077052016-04-06 16:15:40 +08001886int
1887lws_protocol_init(struct lws_context *context);
1888
Andy Green7f92ee82016-06-18 09:00:04 +08001889int
1890lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p);
1891
Andy Green722cc4a2016-06-26 06:29:20 +08001892const struct lws_http_mount *
1893lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
1894
Andy Greena654fc02014-04-03 07:16:40 +08001895/*
Alejandro Merycdc97172014-12-04 23:15:27 +01001896 * custom allocator
1897 */
Andy Greene99a83c2016-01-20 16:56:06 +08001898LWS_EXTERN void *
Alejandro Merycdc97172014-12-04 23:15:27 +01001899lws_realloc(void *ptr, size_t size);
1900
Andy Greene99a83c2016-01-20 16:56:06 +08001901LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
Alejandro Merycdc97172014-12-04 23:15:27 +01001902lws_zalloc(size_t size);
1903
1904#define lws_malloc(S) lws_realloc(NULL, S)
1905#define lws_free(P) lws_realloc(P, 0)
Andy Green54806b12015-12-17 17:03:59 +08001906#define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
Alejandro Merycdc97172014-12-04 23:15:27 +01001907
Andy Greendc8a3a82015-12-06 09:15:27 +08001908/* lws_plat_ */
Andy Greena654fc02014-04-03 07:16:40 +08001909LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001910lws_plat_delete_socket_from_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001911 struct lws *wsi, int m);
Andy Greena654fc02014-04-03 07:16:40 +08001912LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001913lws_plat_insert_socket_into_fds(struct lws_context *context,
Andy Greendc8a3a82015-12-06 09:15:27 +08001914 struct lws *wsi);
Andy Greena654fc02014-04-03 07:16:40 +08001915LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001916lws_plat_service_periodic(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001917
1918LWS_EXTERN int
Andy Greendc8a3a82015-12-06 09:15:27 +08001919lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1920 struct lws_pollfd *pfd);
Andy Greena654fc02014-04-03 07:16:40 +08001921LWS_EXTERN int
1922lws_plat_context_early_init(void);
1923LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001924lws_plat_context_early_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001925LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001926lws_plat_context_late_destroy(struct lws_context *context);
Andy Greena654fc02014-04-03 07:16:40 +08001927LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001928lws_poll_listen_fd(struct lws_pollfd *fd);
Andy Greena654fc02014-04-03 07:16:40 +08001929LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001930lws_plat_service(struct lws_context *context, int timeout_ms);
Andy Greend3a55052016-01-19 03:34:24 +08001931LWS_EXTERN LWS_VISIBLE int
1932lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
Andy Greena654fc02014-04-03 07:16:40 +08001933LWS_EXTERN int
Andy Green4386e362015-12-10 07:14:16 +08001934lws_plat_init(struct lws_context *context,
1935 struct lws_context_creation_info *info);
Andy Greena654fc02014-04-03 07:16:40 +08001936LWS_EXTERN void
1937lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1938LWS_EXTERN unsigned long long
1939time_in_microseconds(void);
Andy Greene99a83c2016-01-20 16:56:06 +08001940LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
Andy Greena654fc02014-04-03 07:16:40 +08001941lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
Andy Green8c0d3c02015-11-02 20:34:12 +08001942
Andy Greene99a83c2016-01-20 16:56:06 +08001943LWS_EXTERN int LWS_WARN_UNUSED_RESULT
Andy Green86c1ef12015-12-30 11:43:36 +08001944lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
1945
Andy Green8c0d3c02015-11-02 20:34:12 +08001946#ifdef __cplusplus
1947};
1948#endif