Skip to content
Snippets Groups Projects
private-libwebsockets.h 37.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Andy Green's avatar
    Andy Green committed
    /*
     * libwebsockets - small server side websockets and web server implementation
    
    Andy Green's avatar
    Andy Green committed
     *
    
    Andy Green's avatar
    Andy Green committed
     * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
    
    Andy Green's avatar
    Andy Green committed
     *
     *  This library is free software; you can redistribute it and/or
     *  modify it under the terms of the GNU Lesser General Public
     *  License as published by the Free Software Foundation:
     *  version 2.1 of the License.
     *
     *  This library is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     *  Lesser General Public License for more details.
     *
     *  You should have received a copy of the GNU Lesser General Public
     *  License along with this library; if not, write to the Free Software
     *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     *  MA  02110-1301  USA
     */
    
    #include "lws_config.h"
    
    #include "lws_config_private.h"
    
    #ifdef LWS_HAVE_SYS_TYPES_H
    
    #include <sys/types.h>
    
    Joakim Söderberg's avatar
    Joakim Söderberg committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <time.h>
    
    Andy Green's avatar
    Andy Green committed
    #include <ctype.h>
    
    Peter Young's avatar
    Peter Young committed
    #include <limits.h>
    
    #include <stdarg.h>
    
    #include <assert.h>
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_MAX_SMP > 1
    #include <pthread.h>
    #endif
    
    #ifdef LWS_HAVE_SYS_STAT_H
    
    #include <sys/stat.h>
    
    #endif
    
    #if defined(WIN32) || defined(_WIN32)
    
    #if (WINVER < 0x0501)
    #undef WINVER
    #undef _WIN32_WINNT
    #define WINVER 0x0501
    #define _WIN32_WINNT WINVER
    #endif
    
    #define LWS_NO_DAEMONIZE
    
    #define LWS_ERRNO WSAGetLastError()
    #define LWS_EAGAIN WSAEWOULDBLOCK
    #define LWS_EALREADY WSAEALREADY
    #define LWS_EINPROGRESS WSAEINPROGRESS
    #define LWS_EINTR WSAEINTR
    #define LWS_EISCONN WSAEISCONN
    #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
    
    #define LWS_POLLHUP (FD_CLOSE)
    
    #define LWS_POLLIN (FD_READ | FD_ACCEPT)
    #define LWS_POLLOUT (FD_WRITE)
    
    Patrick Gansterer's avatar
    Patrick Gansterer committed
    #define MSG_NOSIGNAL 0
    #define SHUT_RDWR SD_BOTH
    #define SOL_TCP IPPROTO_TCP
    
    Andy Green's avatar
    Andy Green committed
    #define SHUT_WR SD_SEND
    
    #define compatible_close(fd) closesocket(fd)
    
    #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
    
    #define lws_socket_is_valid(x) (!!x)
    
    #define LWS_SOCK_INVALID 0
    
    #include <winsock2.h>
    
    #include <ws2tcpip.h>
    
    #include <windows.h>
    
    Joakim Söderberg's avatar
    Joakim Söderberg committed
    #include <tchar.h>
    
    #ifdef LWS_HAVE_IN6ADDR_H
    
    #include <in6addr.h>
    #endif
    
    Joakim Söderberg's avatar
    Joakim Söderberg committed
    #include <mstcpip.h>
    
    #ifndef __func__
    #define __func__ __FUNCTION__
    #endif
    
    
    #ifdef _WIN32_WCE
    #define vsnprintf _vsnprintf
    #endif
    
    
    #else /* not windows --> */
    
    Andy Green's avatar
    Andy Green committed
    
    
    #include <fcntl.h>
    #include <strings.h>
    #include <unistd.h>
    
    #include <sys/types.h>
    
    Andy Green's avatar
    Andy Green committed
    #ifndef MBED_OPERATORS
    
    Andy Green's avatar
    Andy Green committed
    #ifndef __cplusplus
    #include <errno.h>
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #include <netdb.h>
    #include <signal.h>
    
    Andy Green's avatar
    Andy Green committed
    #include <sys/socket.h>
    
    #ifdef LWS_BUILTIN_GETIFADDRS
     #include <getifaddrs.h>
    #else
     #include <ifaddrs.h>
    #endif
    
    Dnyanesh Gate's avatar
    Dnyanesh Gate committed
    #if defined (__ANDROID__)
    #include <syslog.h>
    #else
    
    #include <sys/syslog.h>
    
    Dnyanesh Gate's avatar
    Dnyanesh Gate committed
    #endif
    
    #include <sys/un.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    Andy Green's avatar
    Andy Green committed
    #include <netinet/in.h>
    
    Andy Green's avatar
    Andy Green committed
    #include <netinet/tcp.h>
    
    #include <arpa/inet.h>
    
    Andy Green's avatar
    Andy Green committed
    #include <poll.h>
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    #ifdef LWS_USE_LIBEV
    
    Andy Green's avatar
    Andy Green committed
    #include <ev.h>
    #endif
    #ifdef LWS_USE_LIBUV
    
    #include <uv.h>
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #include <sys/mman.h>
    
    Andy Green's avatar
    Andy Green committed
    
    #endif /* MBED */
    
    #ifndef LWS_NO_FORK
    #ifdef LWS_HAVE_SYS_PRCTL_H
    #include <sys/prctl.h>
    #endif
    #endif
    
    
    #include <sys/time.h>
    
    #define LWS_ERRNO errno
    #define LWS_EAGAIN EAGAIN
    #define LWS_EALREADY EALREADY
    #define LWS_EINPROGRESS EINPROGRESS
    #define LWS_EINTR EINTR
    #define LWS_EISCONN EISCONN
    #define LWS_EWOULDBLOCK EWOULDBLOCK
    
    #define LWS_POLLHUP (POLLHUP|POLLERR)
    #define LWS_POLLIN (POLLIN)
    #define LWS_POLLOUT (POLLOUT)
    
    #define compatible_close(fd) close(fd)
    #define lws_set_blocking_send(wsi)
    
    Andy Green's avatar
    Andy Green committed
    
    #ifdef MBED_OPERATORS
    #define lws_socket_is_valid(x) ((x) != NULL)
    #define LWS_SOCK_INVALID (NULL)
    #else
    
    #define lws_socket_is_valid(x) (x >= 0)
    #define LWS_SOCK_INVALID (-1)
    
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    #ifndef LWS_HAVE_BZERO
    
    #ifndef bzero
    
    #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
    #endif
    
    #ifndef LWS_HAVE_STRERROR
    
    Andy Green's avatar
    Andy Green committed
    #ifdef LWS_OPENSSL_SUPPORT
    
    #ifdef USE_WOLFSSL
    
    #ifdef USE_OLD_CYASSL
    #include <cyassl/openssl/ssl.h>
    #include <cyassl/error-ssl.h>
    #else
    
    #include <wolfssl/openssl/ssl.h>
    #include <wolfssl/error-ssl.h>
    
    #endif /* not USE_OLD_CYASSL */
    
    #else
    
    Andy Green's avatar
    Andy Green committed
    #include <openssl/ssl.h>
    #include <openssl/evp.h>
    #include <openssl/err.h>
    
    #include <openssl/md5.h>
    
    #include <openssl/sha.h>
    
    #endif /* not USE_WOLFSSL */
    
    Andy Green's avatar
    Andy Green committed
    #include "libwebsockets.h"
    
    
    Andy Green's avatar
    Andy Green committed
    #if defined(MBED_OPERATORS)
    #undef compatible_close
    #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
    
    #ifndef BIG_ENDIAN
    #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
    #endif
    #ifndef LITTLE_ENDIAN
    #define LITTLE_ENDIAN 1234
    #endif
    #ifndef BYTE_ORDER
    #define BYTE_ORDER LITTLE_ENDIAN
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    
    #if defined(WIN32) || defined(_WIN32)
    
    #ifndef BIG_ENDIAN
    #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
    #endif
    #ifndef LITTLE_ENDIAN
    #define LITTLE_ENDIAN 1234
    #endif
    #ifndef BYTE_ORDER
    #define BYTE_ORDER LITTLE_ENDIAN
    #endif
    
    typedef unsigned __int64 u_int64_t;
    
    
    #undef __P
    #ifndef __P
    #if __STDC__
    #define __P(protos) protos
    #else
    #define __P(protos) ()
    #endif
    #endif
    
    #else
    
    #include <sys/stat.h>
    #include <sys/cdefs.h>
    #include <sys/time.h>
    
    #if defined(__APPLE__)
    #include <machine/endian.h>
    #elif defined(__FreeBSD__)
    #include <sys/endian.h>
    #elif defined(__linux__)
    #include <endian.h>
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    Alejandro Mery's avatar
    Alejandro Mery committed
    #include <stddef.h>
    
    #ifndef container_of
    #define container_of(P,T,M)	((T *)((char *)(P) - offsetof(T, M)))
    #endif
    
    emironov's avatar
    emironov committed
    #if defined(__QNX__)
    	#include <gulliver.h>
    	#if defined(__LITTLEENDIAN__)
    		#define BYTE_ORDER __LITTLEENDIAN__
    		#define LITTLE_ENDIAN __LITTLEENDIAN__
    		#define BIG_ENDIAN 4321  /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
    	#endif
    	#if defined(__BIGENDIAN__)
    		#define BYTE_ORDER __BIGENDIAN__
    		#define LITTLE_ENDIAN 1234  /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
    		#define BIG_ENDIAN __BIGENDIAN__
    	#endif
    #endif
    
    
    #if !defined(BYTE_ORDER)
    # define BYTE_ORDER __BYTE_ORDER
    #endif
    #if !defined(LITTLE_ENDIAN)
    # define LITTLE_ENDIAN __LITTLE_ENDIAN
    #endif
    #if !defined(BIG_ENDIAN)
    # define BIG_ENDIAN __BIG_ENDIAN
    #endif
    
    #endif
    
    
    /*
     * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
     * but happily have something equivalent in the SO_NOSIGPIPE flag.
     */
    #ifdef __APPLE__
    
    Andy Green's avatar
    Andy Green committed
    #define MSG_NOSIGNAL SO_NOSIGPIPE
    
    #ifdef _WIN32
    #ifndef FD_HASHTABLE_MODULUS
    #define FD_HASHTABLE_MODULUS 32
    #endif
    #endif
    
    
    Andy Green's avatar
    Andy Green committed
    #ifndef LWS_DEF_HEADER_LEN
    #define LWS_DEF_HEADER_LEN 1024
    
    Andy Green's avatar
    Andy Green committed
    #ifndef LWS_DEF_HEADER_POOL
    #define LWS_DEF_HEADER_POOL 16
    
    #ifndef LWS_MAX_PROTOCOLS
    
    #define LWS_MAX_PROTOCOLS 5
    
    #endif
    #ifndef LWS_MAX_EXTENSIONS_ACTIVE
    
    #define LWS_MAX_EXTENSIONS_ACTIVE 2
    
    #ifndef LWS_MAX_EXT_OFFERS
    #define LWS_MAX_EXT_OFFERS 8
    #endif
    
    #ifndef SPEC_LATEST_SUPPORTED
    
    #define SPEC_LATEST_SUPPORTED 13
    
    #endif
    #ifndef AWAITING_TIMEOUT
    
    Andy Green's avatar
    Andy Green committed
    #define AWAITING_TIMEOUT 20
    
    #endif
    #ifndef CIPHERS_LIST_STRING
    
    #define CIPHERS_LIST_STRING "DEFAULT"
    
    #ifndef LWS_SOMAXCONN
    #define LWS_SOMAXCONN SOMAXCONN
    #endif
    
    #define MAX_WEBSOCKET_04_KEY_LEN 128
    
    #define LWS_MAX_SOCKET_IO_BUF 4096
    
    
    #ifndef SYSTEM_RANDOM_FILEPATH
    
    #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
    
    Andy Green's avatar
    Andy Green committed
    enum lws_websocket_opcodes_07 {
    
    Andy Green's avatar
    Andy Green committed
    	LWSWSOPC_CONTINUATION = 0,
    	LWSWSOPC_TEXT_FRAME = 1,
    	LWSWSOPC_BINARY_FRAME = 2,
    
    Andy Green's avatar
    Andy Green committed
    	LWSWSOPC_NOSPEC__MUX = 7,
    
    
    	/* control extensions 8+ */
    
    
    Andy Green's avatar
    Andy Green committed
    	LWSWSOPC_CLOSE = 8,
    	LWSWSOPC_PING = 9,
    	LWSWSOPC_PONG = 0xa,
    
    Andy Green's avatar
    Andy Green committed
    enum lws_connection_states {
    
    Andy Green's avatar
    Andy Green committed
    	LWSS_HTTP,
    	LWSS_HTTP_ISSUING_FILE,
    	LWSS_HTTP_HEADERS,
    	LWSS_HTTP_BODY,
    	LWSS_DEAD_SOCKET,
    	LWSS_ESTABLISHED,
    	LWSS_CLIENT_UNCONNECTED,
    	LWSS_RETURNED_CLOSE_ALREADY,
    	LWSS_AWAITING_CLOSE_ACK,
    	LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
    
    Andy Green's avatar
    Andy Green committed
    	LWSS_SHUTDOWN,
    
    Andy Green's avatar
    Andy Green committed
    
    	LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
    	LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
    	LWSS_HTTP2_ESTABLISHED,
    
    enum http_version {
    	HTTP_VERSION_1_0,
    	HTTP_VERSION_1_1,
    };
    
    enum http_connection_type {
    	HTTP_CONNECTION_CLOSE,
    	HTTP_CONNECTION_KEEP_ALIVE
    };
    
    
    enum lws_pending_protocol_send {
    	LWS_PPS_NONE,
    	LWS_PPS_HTTP2_MY_SETTINGS,
    	LWS_PPS_HTTP2_ACK_SETTINGS,
    
    	LWS_PPS_HTTP2_PONG,
    
    Andy Green's avatar
    Andy Green committed
    enum lws_rx_parse_state {
    	LWS_RXPS_NEW,
    
    	LWS_RXPS_04_mask_1,
    	LWS_RXPS_04_mask_2,
    	LWS_RXPS_04_mask_3,
    
    
    	LWS_RXPS_04_FRAME_HDR_1,
    
    	LWS_RXPS_04_FRAME_HDR_LEN,
    	LWS_RXPS_04_FRAME_HDR_LEN16_2,
    	LWS_RXPS_04_FRAME_HDR_LEN16_1,
    	LWS_RXPS_04_FRAME_HDR_LEN64_8,
    	LWS_RXPS_04_FRAME_HDR_LEN64_7,
    	LWS_RXPS_04_FRAME_HDR_LEN64_6,
    	LWS_RXPS_04_FRAME_HDR_LEN64_5,
    	LWS_RXPS_04_FRAME_HDR_LEN64_4,
    	LWS_RXPS_04_FRAME_HDR_LEN64_3,
    	LWS_RXPS_04_FRAME_HDR_LEN64_2,
    	LWS_RXPS_04_FRAME_HDR_LEN64_1,
    
    Andy Green's avatar
    Andy Green committed
    	LWS_RXPS_07_COLLECT_FRAME_KEY_1,
    	LWS_RXPS_07_COLLECT_FRAME_KEY_2,
    	LWS_RXPS_07_COLLECT_FRAME_KEY_3,
    	LWS_RXPS_07_COLLECT_FRAME_KEY_4,
    
    
    Andy Green's avatar
    Andy Green committed
    	LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
    };
    
    
    
    enum connection_mode {
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_HTTP_SERVING,
    	LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
    	LWSCM_PRE_WS_SERVING_ACCEPT,
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_WS_SERVING,
    	LWSCM_WS_CLIENT,
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_HTTP2_SERVING,
    
    Andy Green's avatar
    Andy Green committed
    	/* transient, ssl delay hiding */
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_SSL_ACK_PENDING,
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_SSL_INIT,
    
    	/* transient modes */
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_WSCL_WAITING_CONNECT,
    	LWSCM_WSCL_WAITING_PROXY_REPLY,
    	LWSCM_WSCL_ISSUE_HANDSHAKE,
    	LWSCM_WSCL_ISSUE_HANDSHAKE2,
    	LWSCM_WSCL_WAITING_SSL,
    	LWSCM_WSCL_WAITING_SERVER_REPLY,
    	LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
    	LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
    
    	/* special internal types */
    
    Andy Green's avatar
    Andy Green committed
    	LWSCM_SERVER_LISTENER,
    
    enum {
    	LWS_RXFLOW_ALLOW = (1 << 0),
    	LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
    };
    
    
    /* this is not usable directly by user code any more, lws_close_reason() */
    #define LWS_WRITE_CLOSE 4
    
    
    struct lws_protocols;
    struct lws;
    
    Andy Green's avatar
    Andy Green committed
    #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
    
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    struct lws_io_watcher {
    
    Andy Green's avatar
    Andy Green committed
    #ifdef LWS_USE_LIBEV
    	ev_io ev_watcher;
    #endif
    #ifdef LWS_USE_LIBUV
    	uv_poll_t uv_watcher;
    #endif
    	struct lws_context *context;
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    };
    
    struct lws_signal_watcher {
    
    Andy Green's avatar
    Andy Green committed
    #ifdef LWS_USE_LIBEV
    	ev_signal ev_watcher;
    #endif
    #ifdef LWS_USE_LIBUV
    	uv_signal_t uv_watcher;
    #endif
    	struct lws_context *context;
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    };
    
    Andy Green's avatar
    Andy Green committed
    #endif
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    
    
    #ifdef _WIN32
    #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
    
    struct lws_fd_hashtable {
    
    /*
     * This is totally opaque to code using the library.  It's exported as a
     * forward-reference pointer-only declaration; the user can use the pointer with
     * other APIs to get information out of it.
     */
    
    struct lws_fragments {
    	unsigned short offset;
    	unsigned short len;
    	unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
    };
    
    /*
     * these are assigned from a pool held in the context.
     * Both client and server mode uses them for http header analysis
     */
    
    struct allocated_headers {
    
    Andy Green's avatar
    Andy Green committed
    	struct lws *wsi; /* owner */
    
    Andy Green's avatar
    Andy Green committed
    	char *data; /* prepared by context init to point to dedicated storage */
    	/*
    	 * the randomly ordered fragments, indexed by frag_index and
    	 * lws_fragments->nfrag for continuation.
    	 */
    	struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
    
    Andy Green's avatar
    Andy Green committed
    	time_t assigned;
    
    	/*
    	 * for each recognized token, frag_index says which frag[] his data
    	 * starts in (0 means the token did not appear)
    	 * the actual header data gets dumped as it comes in, into data[]
    	 */
    	unsigned char frag_index[WSI_TOKEN_COUNT];
    
    Andy Green's avatar
    Andy Green committed
    	unsigned char rx[2048];
    	unsigned int rxpos;
    	unsigned int rxlen;
    
    
    #ifndef LWS_NO_CLIENT
    	char initial_handshake_hash_base64[30];
    	unsigned short c_port;
    #endif
    
    
    	unsigned short pos;
    	unsigned char in_use;
    	unsigned char nfrag;
    
    Andy Green's avatar
    Andy Green committed
    /*
     * so we can have n connections being serviced simultaneously,
     * these things need to be isolated per-thread.
     */
    
    struct lws_context_per_thread {
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_MAX_SMP > 1
    	pthread_mutex_t lock;
    #endif
    
    Andy Green's avatar
    Andy Green committed
    	struct lws_pollfd *fds;
    	struct lws *rx_draining_ext_list;
    	struct lws *tx_draining_ext_list;
    
    Andy Green's avatar
    Andy Green committed
    	struct lws *timeout_list;
    	void *http_header_data;
    	struct allocated_headers *ah_pool;
    	struct lws *ah_wait_list;
    	int ah_wait_list_length;
    
    Andy Green's avatar
    Andy Green committed
    #ifdef LWS_OPENSSL_SUPPORT
    	struct lws *pending_read_list; /* linked list */
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #ifndef LWS_NO_SERVER
    	struct lws *wsi_listening;
    
    Andy Green's avatar
    Andy Green committed
    #endif
    #if defined(LWS_USE_LIBEV)
    	struct ev_loop *io_loop_ev;
    #endif
    #if defined(LWS_USE_LIBUV)
    	uv_loop_t *io_loop_uv;
    	uv_signal_t signals[8];
    #endif
    #if defined(LWS_USE_LIBEV)
    	struct lws_io_watcher w_accept;
    #endif
    #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
    	struct lws_signal_watcher w_sigint;
    	unsigned char ev_loop_foreign:1;
    
    Andy Green's avatar
    Andy Green committed
    #endif
    	lws_sockfd_type lserv_fd;
    
    	unsigned long count_conns;
    
    Andy Green's avatar
    Andy Green committed
    	/*
    	 * usable by anything in the service code, but only if the scope
    	 * does not last longer than the service action (since next service
    	 * of any socket can likewise use it and overwrite)
    	 */
    	unsigned char *serv_buf;
    
    Andy Green's avatar
    Andy Green committed
    #else
    	int dummy_pipe_fds[2];
    
    Andy Green's avatar
    Andy Green committed
    	unsigned int fds_count;
    
    	short ah_count_in_use;
    
    Andy Green's avatar
    Andy Green committed
    };
    
    /*
     * the rest is managed per-context, that includes
     *
     *  - processwide single fd -> wsi lookup
     *  - contextwide headers pool
     *  - contextwide ssl context
     *  - contextwide proxy
     */
    
    struct lws_context {
    	time_t last_timeout_check_s;
    	struct lws_plat_file_ops fops;
    	struct lws_context_per_thread pt[LWS_MAX_SMP];
    
    #ifdef _WIN32
    /* different implementation between unix and windows */
    
    	struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
    
    	struct lws **lws_lookup;  /* fd to wsi */
    
    	const char *iface;
    
    	const struct lws_token_limits *token_limits;
    	void *user_space;
    
    	const struct lws_protocols *protocols;
    
    #ifdef LWS_OPENSSL_SUPPORT
    	SSL_CTX *ssl_ctx;
    	SSL_CTX *ssl_client_ctx;
    #endif
    #ifndef LWS_NO_EXTENSIONS
    	const struct lws_extension *extensions;
    #endif
    
    Andy Green's avatar
    Andy Green committed
    #if defined(LWS_USE_LIBEV)
    	lws_ev_signal_cb_t * lws_ev_sigint_cb;
    #endif
    #if defined(LWS_USE_LIBUV)
    	lws_uv_signal_cb_t * lws_uv_sigint_cb;
    #endif
    
    	char http_proxy_address[128];
    	char proxy_basic_auth_token[128];
    	char canonical_hostname[128];
    #ifdef LWS_LATENCY
    	unsigned long worst_latency;
    	char worst_latency_info[256];
    #endif
    
    	int max_fds;
    	int listen_port;
    
    Andy Green's avatar
    Andy Green committed
    #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
    
    	int use_ev_sigint;
    #endif
    
    Andy Green's avatar
    Andy Green committed
    	int started_with_parent;
    
    
    	int fd_random;
    
    Andy Green's avatar
    Andy Green committed
    	int lserv_mod;
    
    Andy Green's avatar
    Andy Green committed
    	int count_wsi_allocated;
    
    	unsigned int http_proxy_port;
    	unsigned int options;
    
    Andy Green's avatar
    Andy Green committed
    	unsigned int fd_limit_per_thread;
    
    Andy Green's avatar
    Andy Green committed
    	unsigned int timeout_secs;
    
    Andy Green's avatar
    Andy Green committed
    
    
    	/*
    	 * set to the Thread ID that's doing the service loop just before entry
    	 * to poll indicates service thread likely idling in poll()
    	 * volatile because other threads may check it as part of processing
    	 * for pollfd event change.
    	 */
    	volatile int service_tid;
    
    	int count_protocols;
    
    	int ka_time;
    	int ka_probes;
    	int ka_interval;
    
    
    #ifdef LWS_OPENSSL_SUPPORT
    	int use_ssl;
    
    	int allow_non_ssl_on_ssl_port;
    
    	unsigned int user_supplied_ssl_ctx:1;
    
    Andy Green's avatar
    Andy Green committed
    #define lws_ssl_anybody_has_buffered_read(w) \
    		(w->context->use_ssl && \
    		 w->context->pt[(int)w->tsi].pending_read_list)
    #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
    		(c->use_ssl && \
    		 c->pt[(int)t].pending_read_list)
    
    #else
    #define lws_ssl_anybody_has_buffered_read(ctx) (0)
    
    Andy Green's avatar
    Andy Green committed
    #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
    
    	short max_http_header_data;
    	short max_http_header_pool;
    
    Andy Green's avatar
    Andy Green committed
    	short count_threads;
    
    
    	unsigned int being_destroyed:1;
    
    Andy Green's avatar
    Andy Green committed
    	unsigned int requested_kill:1;
    
    Andy Green's avatar
    Andy Green committed
    LWS_EXTERN void
    lws_close_free_wsi_final(struct lws *wsi);
    LWS_EXTERN void
    lws_libuv_closehandle(struct lws *wsi);
    
    
    enum {
    	LWS_EV_READ = (1 << 0),
    	LWS_EV_WRITE = (1 << 1),
    	LWS_EV_START = (1 << 2),
    	LWS_EV_STOP = (1 << 3),
    
    Andy Green's avatar
    Andy Green committed
    
    	LWS_EV_PREPARE_DELETION = (1 << 31),
    
    Andy Green's avatar
    Andy Green committed
    #if defined(LWS_USE_LIBEV)
    
    LWS_EXTERN void
    
    lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
    
    LWS_EXTERN void
    
    lws_libev_io(struct lws *wsi, int flags);
    
    LWS_EXTERN int
    
    lws_libev_init_fd_table(struct lws_context *context);
    
    LWS_EXTERN void
    
    Andy Green's avatar
    Andy Green committed
    lws_libev_destroyloop(struct lws_context *context, int tsi);
    LWS_EXTERN void
    lws_libev_run(const struct lws_context *context, int tsi);
    #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
    LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    #else
    
    Andy Green's avatar
    Andy Green committed
    #define lws_libev_accept(_a, _b) ((void) 0)
    #define lws_libev_io(_a, _b) ((void) 0)
    #define lws_libev_init_fd_table(_a) (0)
    #define lws_libev_run(_a, _b) ((void) 0)
    #define lws_libev_destroyloop(_a, _b) ((void) 0)
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    #define LWS_LIBEV_ENABLED(context) (0)
    
    Andy Green's avatar
    Andy Green committed
    #if LWS_POSIX
    
    #define lws_feature_status_libev(_a) \
    			lwsl_notice("libev support not compiled in\n")
    
    Andy Green's avatar
    Andy Green committed
    #else
    #define lws_feature_status_libev(_a)
    #endif
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    #endif
    
    
    Andy Green's avatar
    Andy Green committed
    #if defined(LWS_USE_LIBUV)
    LWS_EXTERN void
    lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
    LWS_EXTERN void
    lws_libuv_io(struct lws *wsi, int flags);
    LWS_EXTERN int
    lws_libuv_init_fd_table(struct lws_context *context);
    LWS_EXTERN void
    lws_libuv_run(const struct lws_context *context, int tsi);
    LWS_EXTERN void
    lws_libuv_destroyloop(struct lws_context *context, int tsi);
    #define LWS_LIBUV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBUV)
    LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
    #else
    #define lws_libuv_accept(_a, _b) ((void) 0)
    #define lws_libuv_io(_a, _b) ((void) 0)
    #define lws_libuv_init_fd_table(_a) (0)
    #define lws_libuv_run(_a, _b) ((void) 0)
    #define lws_libuv_destroyloop(_a, _b) ((void) 0)
    #define LWS_LIBUV_ENABLED(context) (0)
    #if LWS_POSIX
    #define lws_feature_status_libuv(_a) \
    			lwsl_notice("libuv support not compiled in\n")
    #else
    #define lws_feature_status_libuv(_a)
    #endif
    #endif
    
    
    
    Andy Green's avatar
    Andy Green committed
    #ifdef LWS_USE_IPV6
    
    Andy Green's avatar
    Andy Green committed
    #define LWS_IPV6_ENABLED(context) \
    	(!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
    
    James Devine's avatar
    James Devine committed
    #else
    #define LWS_IPV6_ENABLED(context) (0)
    #endif
    
    Andrew Canaday's avatar
    Andrew Canaday committed
    
    
    enum uri_path_states {
    	URIPS_IDLE,
    	URIPS_SEEN_SLASH,
    	URIPS_SEEN_SLASH_DOT,
    	URIPS_SEEN_SLASH_DOT_DOT,
    };
    
    enum uri_esc_states {
    	URIES_IDLE,
    	URIES_SEEN_PERCENT,
    	URIES_SEEN_PERCENT_H1,
    };
    
    /* notice that these union members:
    
     *  hdr
     *  http
     *  http2
    
     * all have a pointer to allocated_headers struct as their first member.
    
     * It means for allocated_headers access, the three union paths can all be
    
    Peter Pentchev's avatar
    Peter Pentchev committed
     * used interchangeably to access the same data
    
    struct _lws_http_mode_related {
    
    	/* MUST be first in struct */
    
    	struct allocated_headers *ah; /* mirroring  _lws_header_related */
    
    Andy Green's avatar
    Andy Green committed
    	struct lws *ah_wait_list;
    	struct lws *new_wsi_list;
    
    	unsigned long filepos;
    	unsigned long filelen;
    
    	lws_filefd_type fd;
    
    kapejod's avatar
    kapejod committed
    
    
    	enum http_version request_version;
    	enum http_connection_type connection_type;
    
    Andy Green's avatar
    Andy Green committed
    	unsigned int content_length;
    	unsigned int content_remain;
    
    #ifdef LWS_USE_HTTP2
    
    enum lws_http2_settings {
    	LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
    	LWS_HTTP2_SETTINGS__ENABLE_PUSH,
    	LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
    	LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
    	LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
    	LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
    
    	LWS_HTTP2_SETTINGS__COUNT /* always last */
    };
    
    enum lws_http2_wellknown_frame_types {
    	LWS_HTTP2_FRAME_TYPE_DATA,
    	LWS_HTTP2_FRAME_TYPE_HEADERS,
    	LWS_HTTP2_FRAME_TYPE_PRIORITY,
    	LWS_HTTP2_FRAME_TYPE_RST_STREAM,
    	LWS_HTTP2_FRAME_TYPE_SETTINGS,
    	LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
    	LWS_HTTP2_FRAME_TYPE_PING,
    	LWS_HTTP2_FRAME_TYPE_GOAWAY,
    	LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
    	LWS_HTTP2_FRAME_TYPE_CONTINUATION,
    
    	LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
    };
    
    
    Andy Green's avatar
    Andy Green committed
    enum lws_http2_flags {
    	LWS_HTTP2_FLAG_END_STREAM = 1,
    	LWS_HTTP2_FLAG_END_HEADERS = 4,
    	LWS_HTTP2_FLAG_PADDED = 8,
    	LWS_HTTP2_FLAG_PRIORITY = 0x20,
    
    	LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
    };
    
    
    #define LWS_HTTP2_STREAM_ID_MASTER 0
    #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
    #define LWS_HTTP2_SETTINGS_LENGTH 6
    
    struct http2_settings {
    	unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
    };
    
    
    enum http2_hpack_state {
    
    	/* optional before first header block */
    	HPKS_OPT_PADDING,
    	HKPS_OPT_E_DEPENDENCY,
    	HKPS_OPT_WEIGHT,
    
    	/* header block */
    
    	HPKS_TYPE,
    
    	HPKS_IDX_EXT,
    
    	HPKS_HLEN,
    	HPKS_HLEN_EXT,
    
    	HPKS_DATA,
    
    	/* optional after last header block */
    	HKPS_OPT_DISCARD_PADDING,
    
    enum http2_hpack_type {
    	HPKT_INDEXED_HDR_7,
    	HPKT_INDEXED_HDR_6_VALUE_INCR,
    	HPKT_LITERAL_HDR_VALUE_INCR,
    	HPKT_INDEXED_HDR_4_VALUE,
    	HPKT_LITERAL_HDR_VALUE,
    	HPKT_SIZE_5
    };
    
    
    struct hpack_dt_entry {
    	int token; /* additions that don't map to a token are ignored */
    	int arg_offset;
    	int arg_len;
    };
    
    struct hpack_dynamic_table {
    	struct hpack_dt_entry *entries;
    	char *args;
    	int pos;
    	int next;
    	int num_entries;
    	int args_length;
    };
    
    
    struct _lws_http2_related {
    
    	 * having this first lets us also re-use all HTTP union code
    	 * and in turn, http_mode_related has allocated headers in right
    	 * place so we can use the header apis on the wsi directly still
    	 */
    	struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
    
    	struct http2_settings my_settings;
    	struct http2_settings peer_settings;
    
    	struct lws *parent_wsi;
    	struct lws *next_child_wsi;
    
    	struct hpack_dynamic_table *hpack_dyn_table;
    
    	struct lws *stream_wsi;
    	unsigned char ping_payload[8];
    	unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
    
    	unsigned int count;
    	unsigned int length;
    	unsigned int stream_id;
    
    	enum http2_hpack_state hpack;
    
    	enum http2_hpack_type hpack_type;
    
    	unsigned int header_index;
    	unsigned int hpack_len;
    
    	unsigned int hpack_e_dep;
    
    Andy Green's avatar
    Andy Green committed
    	int tx_credit;
    
    	unsigned int my_stream_id;
    	unsigned int child_count;
    	int my_priority;
    
    	unsigned int END_STREAM:1;
    	unsigned int END_HEADERS:1;
    	unsigned int send_END_STREAM:1;
    	unsigned int GOING_AWAY;
    	unsigned int requested_POLLOUT:1;
    	unsigned int waiting_tx_credit:1;
    	unsigned int huff:1;
    	unsigned int value:1;
    
    	unsigned short round_robin_POLLOUT;
    	unsigned short count_POLLOUT_children;
    	unsigned short hpack_pos;
    
    	unsigned char type;
    	unsigned char flags;
    	unsigned char frame_state;
    	unsigned char padding;
    	unsigned char hpack_m;
    
    	unsigned char initialized;
    
    Andy Green's avatar
    Andy Green committed
    #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
    
    struct _lws_header_related {
    
    	/* MUST be first in struct */
    
    	struct allocated_headers *ah;
    
    Andy Green's avatar
    Andy Green committed
    	struct lws *ah_wait_list;
    
    	enum uri_path_states ups;
    	enum uri_esc_states ues;
    
    Andy Green's avatar
    Andy Green committed
    	short lextable_pos;
    	unsigned short current_token_limit;
    
    	char post_literal_equal;
    
    Andy Green's avatar
    Andy Green committed
    	unsigned char parser_state; /* enum lws_token_indexes */
    
    Andy Green's avatar
    Andy Green committed
    	char redirects;
    
    	char more_rx_waiting;
    
    };
    
    struct _lws_websocket_related {
    
    Andy Green's avatar
    Andy Green committed
    	/* cheapest way to deal with ah overlap with ws union transition */
    
    	struct _lws_header_related hdr;
    
    	char *rx_ubuf;
    
    	unsigned int rx_ubuf_alloc;
    
    	struct lws *rx_draining_ext_list;
    	struct lws *tx_draining_ext_list;
    
    Andy Green's avatar
    Andy Green committed
    	size_t rx_packet_length;
    
    	unsigned int rx_ubuf_head;
    	unsigned char mask[4];
    
    	/* Also used for close content... control opcode == < 128 */
    
    	unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
    
    	unsigned char ping_payload_len;
    
    	unsigned char mask_idx;
    
    	unsigned char opcode;
    
    	unsigned char rsv_first_msg;
    
    	/* zero if no info, or length including 2-byte close code */
    	unsigned char close_in_ping_buffer_len;
    
    	unsigned char utf8;
    
    	unsigned char stashed_write_type;
    	unsigned char tx_draining_stashed_wp;
    
    Andy Green's avatar
    Andy Green committed
    
    	unsigned int final:1;
    
    	unsigned int frame_is_binary:1;
    	unsigned int all_zero_nonce:1;
    	unsigned int this_frame_masked:1;
    
    	unsigned int inside_frame:1; /* next write will be more of frame */
    	unsigned int clean_buffer:1; /* buffer not rewritten by extension */
    
    	unsigned int payload_is_close:1; /* process as PONG, but it is close */
    
    	unsigned int ping_pending_flag:1;
    
    	unsigned int continuation_possible:1;
    
    	unsigned int owed_a_fin:1;
    
    	unsigned int check_utf8:1;
    	unsigned int defeat_check_utf8:1;
    
    	unsigned int pmce_compressed_message:1;
    	unsigned int stashed_write_pending:1;
    	unsigned int rx_draining_ext:1;
    	unsigned int tx_draining_ext:1;
    
    	/* structs */
    	/* members with mutually exclusive lifetimes are unionized */