client: protect against possible NULL deref path
[platform/upstream/libwebsockets.git] / lib / private-libwebsockets.h
index b443daf..ec3cafd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libwebsockets - small server side websockets and web server implementation
  *
- * Copyright (C) 2010 - 2015 Andy Green <andy@warmcat.com>
+ * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -34,6 +34,9 @@
 #include <limits.h>
 #include <stdarg.h>
 #include <assert.h>
+#if LWS_MAX_SMP > 1
+#include <pthread.h>
+#endif
 
 #ifdef LWS_HAVE_SYS_STAT_H
 #include <sys/stat.h>
@@ -60,6 +63,7 @@
 #define MSG_NOSIGNAL 0
 #define SHUT_RDWR SD_BOTH
 #define SOL_TCP IPPROTO_TCP
+#define SHUT_WR SD_SEND
 
 #define compatible_close(fd) closesocket(fd)
 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
 
 #ifdef _WIN32_WCE
 #define vsnprintf _vsnprintf
+#else
+#ifdef LWS_HAVE__VSNPRINTF
+#define vsnprintf _vsnprintf
+#endif
+#endif
+
+#ifdef LWS_HAVE__SNPRINTF
+#define lws_snprintf _snprintf
 #endif
 
 #else /* not windows --> */
 #endif
 #if defined (__ANDROID__)
 #include <syslog.h>
+#include <sys/resource.h>
 #else
 #include <sys/syslog.h>
 #endif
 #include <poll.h>
 #ifdef LWS_USE_LIBEV
 #include <ev.h>
-#endif /* LWS_USE_LIBEV */
+#endif
+#ifdef LWS_USE_LIBUV
+#include <uv.h>
+#endif
 #include <sys/mman.h>
 
 #endif /* MBED */
@@ -217,10 +233,15 @@ typedef unsigned __int64 u_int64_t;
 #endif
 #endif
 
+#include <stddef.h>
+
+#ifndef container_of
+#define container_of(P,T,M)    ((T *)((char *)(P) - offsetof(T, M)))
+#endif
+
 #else
 
 #include <sys/stat.h>
-#include <sys/cdefs.h>
 #include <sys/time.h>
 
 #if defined(__APPLE__)
@@ -280,23 +301,26 @@ extern "C" {
 #endif
 #endif
 
-#ifndef LWS_MAX_HEADER_LEN
-#define LWS_MAX_HEADER_LEN 1024
+#ifndef LWS_DEF_HEADER_LEN
+#define LWS_DEF_HEADER_LEN 1024
 #endif
-#ifndef LWS_MAX_HEADER_POOL
-#define LWS_MAX_HEADER_POOL 16
+#ifndef LWS_DEF_HEADER_POOL
+#define LWS_DEF_HEADER_POOL 16
 #endif
 #ifndef LWS_MAX_PROTOCOLS
 #define LWS_MAX_PROTOCOLS 5
 #endif
 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
-#define LWS_MAX_EXTENSIONS_ACTIVE 3
+#define LWS_MAX_EXTENSIONS_ACTIVE 2
+#endif
+#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
-#define AWAITING_TIMEOUT 5
+#define AWAITING_TIMEOUT 20
 #endif
 #ifndef CIPHERS_LIST_STRING
 #define CIPHERS_LIST_STRING "DEFAULT"
@@ -311,15 +335,6 @@ extern "C" {
 #ifndef SYSTEM_RANDOM_FILEPATH
 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
 #endif
-#ifndef LWS_MAX_ZLIB_CONN_BUFFER
-#define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
-#endif
-
-/*
- * if not in a connection storm, check for incoming
- * connections this many normal connection services
- */
-#define LWS_lserv_mod 10
 
 enum lws_websocket_opcodes_07 {
        LWSWSOPC_CONTINUATION = 0,
@@ -347,6 +362,7 @@ enum lws_connection_states {
        LWSS_RETURNED_CLOSE_ALREADY,
        LWSS_AWAITING_CLOSE_ACK,
        LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
+       LWSS_SHUTDOWN,
 
        LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
        LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
@@ -373,9 +389,9 @@ enum lws_pending_protocol_send {
 enum lws_rx_parse_state {
        LWS_RXPS_NEW,
 
-       LWS_RXPS_04_MASK_NONCE_1,
-       LWS_RXPS_04_MASK_NONCE_2,
-       LWS_RXPS_04_MASK_NONCE_3,
+       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,
@@ -411,6 +427,7 @@ enum connection_mode {
 
        /* transient, ssl delay hiding */
        LWSCM_SSL_ACK_PENDING,
+       LWSCM_SSL_INIT,
 
        /* transient modes */
        LWSCM_WSCL_WAITING_CONNECT,
@@ -437,17 +454,28 @@ enum {
 struct lws_protocols;
 struct lws;
 
-#ifdef LWS_USE_LIBEV
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
+
 struct lws_io_watcher {
-       struct ev_io watcher;
-       struct lws_context* context;
+#ifdef LWS_USE_LIBEV
+       ev_io ev_watcher;
+#endif
+#ifdef LWS_USE_LIBUV
+       uv_poll_t uv_watcher;
+#endif
+       struct lws_context *context;
 };
 
 struct lws_signal_watcher {
-       struct ev_signal watcher;
-       struct lws_context* context;
+#ifdef LWS_USE_LIBEV
+       ev_signal ev_watcher;
+#endif
+#ifdef LWS_USE_LIBUV
+       uv_signal_t uv_watcher;
+#endif
+       struct lws_context *context;
 };
-#endif /* LWS_USE_LIBEV */
+#endif
 
 #ifdef _WIN32
 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
@@ -475,18 +503,24 @@ struct lws_fragments {
  */
 
 struct allocated_headers {
+       struct lws *wsi; /* owner */
        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];
+       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];
+       unsigned char rx[2048];
+       unsigned int rxpos;
+       unsigned int rxlen;
+
 #ifndef LWS_NO_CLIENT
        char initial_handshake_hash_base64[30];
        unsigned short c_port;
@@ -497,49 +531,104 @@ struct allocated_headers {
        unsigned char nfrag;
 };
 
-struct lws_context {
-       time_t last_timeout_check_s;
-       struct lws_plat_file_ops fops;
+/*
+ * so we can have n connections being serviced simultaneously,
+ * these things need to be isolated per-thread.
+ */
+
+struct lws_context_per_thread {
+#if LWS_MAX_SMP > 1
+       pthread_mutex_t lock;
+#endif
+       struct lws_pollfd *fds;
+       struct lws *rx_draining_ext_list;
+       struct lws *tx_draining_ext_list;
+       struct lws *timeout_list;
+       struct lws_context *context;
+       void *http_header_data;
+       struct allocated_headers *ah_pool;
+       struct lws *ah_wait_list;
+       int ah_wait_list_length;
+#ifdef LWS_OPENSSL_SUPPORT
+       struct lws *pending_read_list; /* linked list */
+#endif
+#ifndef LWS_NO_SERVER
+       struct lws *wsi_listening;
+#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];
+       uv_timer_t uv_timeout_watcher;
+       uv_idle_t uv_idle;
+#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;
+#endif
+       lws_sockfd_type lserv_fd;
+
+       unsigned long count_conns;
+       /*
+        * 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;
 #ifdef _WIN32
        WSAEVENT *events;
+#else
+       int dummy_pipe_fds[2];
 #endif
-       struct lws_pollfd *fds;
+       unsigned int fds_count;
+
+       short ah_count_in_use;
+       unsigned char tid;
+};
+
+/*
+ * 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];
 #else
        struct lws **lws_lookup;  /* fd to wsi */
 #endif
-#ifdef LWS_USE_LIBEV
-       struct ev_loop* io_loop;
-       struct lws_io_watcher w_accept;
-       struct lws_signal_watcher w_sigint;
-       lws_ev_signal_cb* lws_ev_sigint_cb;
-#endif /* LWS_USE_LIBEV */
        const char *iface;
        const struct lws_token_limits *token_limits;
        void *user_space;
-#ifndef LWS_NO_SERVER
-       struct lws *wsi_listening;
-#endif
+
        const struct lws_protocols *protocols;
-       void *http_header_data;
-       struct allocated_headers *ah_pool;
+
 #ifdef LWS_OPENSSL_SUPPORT
        SSL_CTX *ssl_ctx;
        SSL_CTX *ssl_client_ctx;
-       struct lws *pending_read_list; /* linked list */
 #endif
 #ifndef LWS_NO_EXTENSIONS
        const struct lws_extension *extensions;
 #endif
-
-       /*
-        * 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[LWS_MAX_SOCKET_IO_BUF];
+#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];
@@ -548,22 +637,20 @@ struct lws_context {
        char worst_latency_info[256];
 #endif
 
-       lws_sockfd_type lserv_fd;
-
-       int fds_count;
        int max_fds;
        int listen_port;
-#ifdef LWS_USE_LIBEV
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
        int use_ev_sigint;
 #endif
        int started_with_parent;
 
        int fd_random;
        int lserv_mod;
-       int lserv_count;
-       int lserv_seen;
+       int count_wsi_allocated;
        unsigned int http_proxy_port;
        unsigned int options;
+       unsigned int fd_limit_per_thread;
+       unsigned int timeout_secs;
 
        /*
         * set to the Thread ID that's doing the service loop just before entry
@@ -573,9 +660,6 @@ struct lws_context {
         */
        volatile int service_tid;
        int service_tid_detected;
-#ifndef _WIN32
-       int dummy_pipe_fds[2];
-#endif
 
        int count_protocols;
        int ka_time;
@@ -586,27 +670,40 @@ struct lws_context {
        int use_ssl;
        int allow_non_ssl_on_ssl_port;
        unsigned int user_supplied_ssl_ctx:1;
-#define lws_ssl_anybody_has_buffered_read(ctx) \
-               (ctx->use_ssl && ctx->pending_read_list)
+#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)
+#define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
 #endif
 
        short max_http_header_data;
        short max_http_header_pool;
-       short ah_count_in_use;
+       short count_threads;
+
+       unsigned int being_destroyed:1;
+       unsigned int requested_kill:1;
 };
 
+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),
+
+       LWS_EV_PREPARE_DELETION = (1 << 31),
 };
 
-#ifdef LWS_USE_LIBEV
-#define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
-LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
+#if defined(LWS_USE_LIBEV)
 LWS_EXTERN void
 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
 LWS_EXTERN void
@@ -614,21 +711,55 @@ lws_libev_io(struct lws *wsi, int flags);
 LWS_EXTERN int
 lws_libev_init_fd_table(struct lws_context *context);
 LWS_EXTERN void
-lws_libev_run(const struct lws_context *context);
+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);
 #else
+#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)
 #define LWS_LIBEV_ENABLED(context) (0)
-#ifdef LWS_POSIX
+#if LWS_POSIX
 #define lws_feature_status_libev(_a) \
                        lwsl_notice("libev support not compiled in\n")
 #else
 #define lws_feature_status_libev(_a)
 #endif
-#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) ((void) 0)
 #endif
 
+#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
+
+
 #ifdef LWS_USE_IPV6
 #define LWS_IPV6_ENABLED(context) \
        (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
@@ -664,6 +795,8 @@ enum uri_esc_states {
 struct _lws_http_mode_related {
        /* MUST be first in struct */
        struct allocated_headers *ah; /* mirroring  _lws_header_related */
+       struct lws *ah_wait_list;
+       struct lws *new_wsi_list;
        unsigned long filepos;
        unsigned long filelen;
        lws_filefd_type fd;
@@ -795,7 +928,7 @@ struct _lws_http2_related {
        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;
@@ -824,6 +957,7 @@ struct _lws_http2_related {
 struct _lws_header_related {
        /* MUST be first in struct */
        struct allocated_headers *ah;
+       struct lws *ah_wait_list;
        enum uri_path_states ups;
        enum uri_esc_states ues;
        short lextable_pos;
@@ -831,23 +965,32 @@ struct _lws_header_related {
        char esc_stash;
        char post_literal_equal;
        unsigned char parser_state; /* enum lws_token_indexes */
+       char redirects;
 };
 
 struct _lws_websocket_related {
-       char *rx_user_buffer;
+       /* 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;
        size_t rx_packet_length;
-       unsigned int rx_user_buffer_head;
-       unsigned char mask_nonce[4];
+       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_SEND_BUFFER_PRE_PADDING];
+       unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
 
        unsigned char ping_payload_len;
-       unsigned char frame_mask_index;
+       unsigned char mask_idx;
        unsigned char opcode;
        unsigned char rsv;
+       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;
 
        unsigned int final:1;
        unsigned int frame_is_binary:1;
@@ -861,6 +1004,10 @@ struct _lws_websocket_related {
        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;
 };
 
 struct lws {
@@ -879,16 +1026,20 @@ struct lws {
 
        /* lifetime members */
 
-#ifdef LWS_USE_LIBEV
+#if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
        struct lws_io_watcher w_read;
+#endif
+#if defined(LWS_USE_LIBEV)
        struct lws_io_watcher w_write;
-#endif /* LWS_USE_LIBEV */
+#endif
        time_t pending_timeout_limit;
 
        /* pointers */
 
        struct lws_context *context;
        const struct lws_protocols *protocol;
+       struct lws *timeout_list;
+       struct lws **timeout_list_prev;
        void *user_space;
        /* rxflow handling */
        unsigned char *rxflow_buffer;
@@ -896,7 +1047,7 @@ struct lws {
        unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
 #ifndef LWS_NO_EXTENSIONS
        const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
-       void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
+       void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
 #endif
 #ifdef LWS_OPENSSL_SUPPORT
        SSL *ssl;
@@ -911,7 +1062,6 @@ struct lws {
        lws_sockfd_type sock;
 
        /* ints */
-       enum lws_pending_protocol_send pps;
        int position_in_fds_table;
        int rxflow_len;
        int rxflow_pos;
@@ -923,6 +1073,7 @@ struct lws {
        unsigned int user_space_externally_allocated:1;
        unsigned int socket_is_permanently_unusable:1;
        unsigned int rxflow_change_to:2;
+       unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
 #ifndef LWS_NO_EXTENSIONS
        unsigned int extension_data_pending:1;
 #endif
@@ -936,14 +1087,17 @@ struct lws {
 
        /* chars */
 #ifndef LWS_NO_EXTENSIONS
-       unsigned char count_active_extensions;
+       unsigned char count_act_ext;
 #endif
        unsigned char ietf_spec_revision;
        char mode; /* enum connection_mode */
        char state; /* enum lws_connection_states */
+       char state_pre_close;
        char lws_rx_parse_state; /* enum lws_rx_parse_state */
        char rx_frame_type; /* enum lws_write_protocol */
        char pending_timeout; /* enum pending_timeout */
+       char pps; /* enum lws_pending_protocol_send */
+       char tsi; /* thread service index we belong to */
 };
 
 LWS_EXTERN int log_level;
@@ -979,18 +1133,24 @@ lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
 LWS_EXTERN void
 lws_set_protocol_write_pending(struct lws *wsi,
                               enum lws_pending_protocol_send pend);
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_client_rx_sm(struct lws *wsi, unsigned char c);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_parse(struct lws *wsi, unsigned char c);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_http_action(struct lws *wsi);
 
 LWS_EXTERN int
 lws_b64_selftest(void);
 
+LWS_EXTERN int
+lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
+
+LWS_EXTERN int
+lws_service_flag_pending(struct lws_context *context, int tsi);
+
 #if defined(_WIN32) || defined(MBED_OPERATORS)
 LWS_EXTERN struct lws *
 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
@@ -1002,27 +1162,31 @@ LWS_EXTERN int
 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
 #else
 #define wsi_from_fd(A,B)  A->lws_lookup[B]
-#define insert_wsi(A,B)   A->lws_lookup[B->sock]=B
+#define insert_wsi(A,B)   assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
 #define delete_from_fd(A,B) A->lws_lookup[B]=0
 #endif
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
 
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
 
-LWS_EXTERN struct lws *
+LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
 lws_client_connect_2(struct lws *wsi);
 
-LWS_EXTERN struct lws *
+LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
+lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
+                const char *path, const char *host);
+
+LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
 lws_create_new_server_wsi(struct lws_context *context);
 
-LWS_EXTERN char *
+LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
 lws_generate_client_handshake(struct lws *wsi, char *pkt);
 
 LWS_EXTERN int
@@ -1037,39 +1201,40 @@ LWS_VISIBLE void
 lws_context_init_extensions(struct lws_context_creation_info *info,
                            struct lws_context *context);
 LWS_EXTERN int
-lws_any_extension_handled(struct lws *wsi,
-                         enum lws_extension_callback_reasons r,
+lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
                          void *v, size_t len);
 
 LWS_EXTERN int
-lws_ext_cb_wsi_active_exts(struct lws *wsi, int reason,
-                                void *buf, int len);
+lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
 LWS_EXTERN int
-lws_ext_cb_all_exts(struct lws_context *context,
-                                        struct lws *wsi, int reason,
-                                        void *arg, int len);
+lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
+                   void *arg, int len);
+
 #else
 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
-#define lws_ext_cb_wsi_active_exts(_a, _b, _c, _d) (0)
+#define lws_ext_cb_active(_a, _b, _c, _d) (0)
 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
 #define lws_issue_raw_ext_access lws_issue_raw
 #define lws_context_init_extensions(_a, _b)
 #endif
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_client_interpret_server_handshake(struct lws *wsi);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_rx_sm(struct lws *wsi, unsigned char c);
 
-LWS_EXTERN int
+LWS_EXTERN void
+lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
+
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
 
 LWS_EXTERN void
 lws_union_transition(struct lws *wsi, enum connection_mode mode);
 
-LWS_EXTERN int
-user_callback_handle_rxflow(callback_function, struct lws *wsi,
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
+user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
                            enum lws_callback_reasons reason, void *user,
                            void *in, size_t len);
 #ifdef LWS_USE_HTTP2
@@ -1113,16 +1278,19 @@ void lws_http2_configure_if_upgraded(struct lws *wsi);
 LWS_EXTERN int
 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
 
-LWS_EXTERN int
-lws_allocate_header_table(struct lws *wsi);
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
+lws_header_table_attach(struct lws *wsi);
 
 LWS_EXTERN int
-lws_free_header_table(struct lws *wsi);
+lws_header_table_detach(struct lws *wsi);
+
+LWS_EXTERN void
+lws_header_table_reset(struct lws *wsi);
 
-LWS_EXTERN char *
+LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
 
 LWS_EXTERN int
@@ -1136,8 +1304,8 @@ int lws_context_init_server(struct lws_context_creation_info *info,
                            struct lws_context *context);
 LWS_EXTERN int
 handshake_0405(struct lws_context *context, struct lws *wsi);
-LWS_EXTERN int
-lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
+lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
 LWS_EXTERN void
 lws_server_get_canonical_hostname(struct lws_context *context,
                                  struct lws_context_creation_info *info);
@@ -1154,7 +1322,7 @@ LWS_EXTERN int get_daemonize_pid();
 #endif
 
 #if !defined(MBED_OPERATORS)
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 interface_to_sa(struct lws_context *context, const char *ifname,
                struct sockaddr_in *addr, size_t addrlen);
 #endif
@@ -1173,23 +1341,21 @@ enum lws_ssl_capable_status {
 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
 #define lws_ssl_pending lws_ssl_pending_no_ssl
-#define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
+#define lws_server_socket_service_ssl(_b, _c) (0)
 #define lws_ssl_close(_a) (0)
 #define lws_ssl_context_destroy(_a)
 #define lws_ssl_remove_wsi_from_buffered_list(_a)
 #else
 #define LWS_SSL_ENABLED(context) (context->use_ssl)
 LWS_EXTERN int openssl_websocket_private_data_index;
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_ssl_pending(struct lws *wsi);
-LWS_EXTERN int
-lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
-                             lws_sockfd_type accept_fd,
-                             struct lws_pollfd *pollfd);
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
+lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
 LWS_EXTERN int
 lws_ssl_close(struct lws *wsi);
 LWS_EXTERN void
@@ -1216,13 +1382,44 @@ lws_context_init_http2_ssl(struct lws_context *context);
 #endif
 #endif
 
-LWS_EXTERN int
+#if LWS_MAX_SMP > 1
+static LWS_INLINE void
+lws_pt_mutex_init(struct lws_context_per_thread *pt)
+{
+       pthread_mutex_init(&pt->lock, NULL);
+}
+
+static LWS_INLINE void
+lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
+{
+       pthread_mutex_destroy(&pt->lock);
+}
+
+static LWS_INLINE void
+lws_pt_lock(struct lws_context_per_thread *pt)
+{
+       pthread_mutex_lock(&pt->lock);
+}
+
+static LWS_INLINE void
+lws_pt_unlock(struct lws_context_per_thread *pt)
+{
+       pthread_mutex_unlock(&pt->lock);
+}
+#else
+#define lws_pt_mutex_init(_a) (void)(_a)
+#define lws_pt_mutex_destroy(_a) (void)(_a)
+#define lws_pt_lock(_a) (void)(_a)
+#define lws_pt_unlock(_a) (void)(_a)
+#endif
+
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_ssl_pending_no_ssl(struct lws *wsi);
 
 #ifndef LWS_NO_CLIENT
@@ -1236,7 +1433,7 @@ lws_context_init_client_ssl(struct lws_context_creation_info *info,
 #else
        #define lws_context_init_client_ssl(_a, _b) (0)
 #endif
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
 LWS_EXTERN void
 lws_decode_ssl_error(void);
@@ -1248,6 +1445,9 @@ lws_decode_ssl_error(void);
 LWS_EXTERN int
 _lws_rx_flow_control(struct lws *wsi);
 
+LWS_EXTERN int
+_lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
+
 #ifndef LWS_NO_SERVER
 LWS_EXTERN int
 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
@@ -1255,7 +1455,7 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
 LWS_EXTERN int
 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
 LWS_EXTERN int
-_lws_server_listen_accept_flow_control(struct lws_context *context, int on);
+_lws_server_listen_accept_flow_control(struct lws *twsi, int on);
 #else
 #define lws_server_socket_service(_a, _b, _c) (0)
 #define lws_handshake_server(_a, _b, _c) (0)
@@ -1269,10 +1469,10 @@ lws_get_addresses(struct lws_context *context, void *ads, char *name,
 /*
  * custom allocator
  */
-LWS_EXTERN void*
+LWS_EXTERN void *
 lws_realloc(void *ptr, size_t size);
 
-LWS_EXTERN void*
+LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
 lws_zalloc(size_t size);
 
 #define lws_malloc(S)  lws_realloc(NULL, S)
@@ -1302,6 +1502,8 @@ LWS_EXTERN int
 lws_poll_listen_fd(struct lws_pollfd *fd);
 LWS_EXTERN int
 lws_plat_service(struct lws_context *context, int timeout_ms);
+LWS_EXTERN LWS_VISIBLE int
+lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
 LWS_EXTERN int
 lws_plat_init(struct lws_context *context,
              struct lws_context_creation_info *info);
@@ -1309,10 +1511,10 @@ LWS_EXTERN void
 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
 LWS_EXTERN unsigned long long
 time_in_microseconds(void);
-LWS_EXTERN const char *
+LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
 
-LWS_EXTERN int
+LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
 
 #ifdef __cplusplus