public api remove context from user callback API BREAK
[platform/upstream/libwebsockets.git] / lib / private-libwebsockets.h
old mode 100755 (executable)
new mode 100644 (file)
index 3ec5d02..a30fd58
@@ -1,7 +1,7 @@
 /*
  * libwebsockets - small server side websockets and web server implementation
  *
- * Copyright (C) 2010 - 2013 Andy Green <andy@warmcat.com>
+ * Copyright (C) 2010 - 2015 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
  *  MA  02110-1301  USA
  */
 
-/* System introspection configs */
-#ifdef CMAKE_BUILD
 #include "lws_config.h"
-#else
-#if defined(WIN32) || defined(_WIN32)
-#define inline __inline
-#else /* not WIN32 */
-#include "config.h"
-
-#endif /* not WIN32 */
-#endif /* not CMAKE */
+#include "lws_config_private.h"
 
-#ifdef HAVE_SYS_TYPES_H
+#ifdef LWS_HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
 
 #include <ctype.h>
 #include <limits.h>
 #include <stdarg.h>
+#include <assert.h>
 
-#ifdef HAVE_SYS_STAT_H
+#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 SOL_TCP IPPROTO_TCP
 
 #define compatible_close(fd) closesocket(fd)
-#define compatible_file_close(fd) CloseHandle(fd)
-#define compatible_file_seek_cur(fd, offset) SetFilePointer(fd, offset, NULL, FILE_CURRENT)
-#define compatible_file_read(amount, fd, buf, len) {\
-       DWORD _amount; \
-       if (!ReadFile(fd, buf, len, &_amount, NULL)) \
-               amount = -1; \
-       else \
-               amount = _amount; \
-       }
 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = TRUE
+#define lws_socket_is_valid(x) (!!x)
+#define LWS_SOCK_INVALID 0
 #include <winsock2.h>
 #include <windows.h>
 #include <tchar.h>
-#ifdef HAVE_IN6ADDR_H
+#ifdef LWS_HAVE_IN6ADDR_H
 #include <in6addr.h>
 #endif
 #include <mstcpip.h>
 #define vsnprintf _vsnprintf
 #endif
 
-#define LWS_INVALID_FILE INVALID_HANDLE_VALUE
 #else /* not windows --> */
-#include <errno.h>
+
 #include <fcntl.h>
-#include <netdb.h>
-#include <signal.h>
 #include <strings.h>
 #include <unistd.h>
 #include <sys/types.h>
+#ifndef MBED_OPERATORS
+#ifndef __cplusplus
+#include <errno.h>
+#endif
+#include <netdb.h>
+#include <signal.h>
 #include <sys/socket.h>
 #ifdef LWS_BUILTIN_GETIFADDRS
  #include <getifaddrs.h>
 #include <sys/un.h>
 #include <sys/socket.h>
 #include <netdb.h>
-#ifndef LWS_NO_FORK
-#ifdef HAVE_SYS_PRCTL_H
-#include <sys/prctl.h>
-#endif
-#endif
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 #ifdef LWS_USE_LIBEV
 #include <ev.h>
 #endif /* LWS_USE_LIBEV */
-
 #include <sys/mman.h>
+
+#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_EINTR EINTR
 #define LWS_EISCONN EISCONN
 #define LWS_EWOULDBLOCK EWOULDBLOCK
-#define LWS_INVALID_FILE -1
 #define LWS_POLLHUP (POLLHUP|POLLERR)
 #define LWS_POLLIN (POLLIN)
 #define LWS_POLLOUT (POLLOUT)
 #define compatible_close(fd) close(fd)
-#define compatible_file_close(fd) close(fd)
-#define compatible_file_seek_cur(fd, offset) lseek(fd, offset, SEEK_CUR)
-#define compatible_file_read(amount, fd, buf, len) \
-               amount = read(fd, buf, len);
 #define lws_set_blocking_send(wsi)
+
+#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
 #endif
 
-#ifndef HAVE_BZERO
+#ifndef LWS_HAVE_BZERO
+#ifndef bzero
 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
 #endif
+#endif
 
-#ifndef HAVE_STRERROR
+#ifndef LWS_HAVE_STRERROR
 #define strerror(x) ""
 #endif
 
 #ifdef LWS_OPENSSL_SUPPORT
-#ifdef USE_CYASSL
+#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
 #include <openssl/ssl.h>
 #include <openssl/evp.h>
 #include <openssl/err.h>
 #include <openssl/md5.h>
 #include <openssl/sha.h>
-#endif /* not USE_CYASSL */
+#endif /* not USE_WOLFSSL */
 #endif
 
 #include "libwebsockets.h"
 
+#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
+#endif
+
 #if defined(WIN32) || defined(_WIN32)
 
 #ifndef BIG_ENDIAN
 #ifndef BYTE_ORDER
 #define BYTE_ORDER LITTLE_ENDIAN
 #endif
+#ifndef u_int64_t
 typedef unsigned __int64 u_int64_t;
+#endif
 
 #undef __P
 #ifndef __P
@@ -207,6 +230,15 @@ typedef unsigned __int64 u_int64_t;
 #include <endian.h>
 #endif
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <stddef.h>
+
+#ifndef container_of
+#define container_of(P,T,M)    ((T *)((char *)(P) - offsetof(T, M)))
+#endif
+
 #if defined(__QNX__)
        #include <gulliver.h>
        #if defined(__LITTLEENDIAN__)
@@ -241,6 +273,12 @@ typedef unsigned __int64 u_int64_t;
 #define MSG_NOSIGNAL SO_NOSIGPIPE
 #endif
 
+#ifdef _WIN32
+#ifndef FD_HASHTABLE_MODULUS
+#define FD_HASHTABLE_MODULUS 32
+#endif
+#endif
+
 #ifndef LWS_MAX_HEADER_LEN
 #define LWS_MAX_HEADER_LEN 1024
 #endif
@@ -305,7 +343,7 @@ enum lws_connection_states {
        WSI_STATE_RETURNED_CLOSE_ALREADY,
        WSI_STATE_AWAITING_CLOSE_ACK,
        WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE,
-       
+
        WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE,
        WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS,
        WSI_STATE_HTTP2_ESTABLISHED,
@@ -325,6 +363,7 @@ enum lws_pending_protocol_send {
        LWS_PPS_NONE,
        LWS_PPS_HTTP2_MY_SETTINGS,
        LWS_PPS_HTTP2_ACK_SETTINGS,
+       LWS_PPS_HTTP2_PONG,
 };
 
 enum lws_rx_parse_state {
@@ -363,7 +402,7 @@ enum connection_mode {
 
        LWS_CONNMODE_WS_SERVING,
        LWS_CONNMODE_WS_CLIENT,
-       
+
        LWS_CONNMODE_HTTP2_SERVING,
 
        /* transient, ssl delay hiding */
@@ -388,38 +427,54 @@ enum {
        LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
 };
 
-struct libwebsocket_protocols;
-struct libwebsocket;
+struct lws_protocols;
+struct lws;
 
 #ifdef LWS_USE_LIBEV
 struct lws_io_watcher {
        struct ev_io watcher;
-       struct libwebsocket_context* context;
+       struct lws_context* context;
 };
 
 struct lws_signal_watcher {
        struct ev_signal watcher;
-       struct libwebsocket_context* context;
+       struct lws_context* context;
 };
 #endif /* LWS_USE_LIBEV */
 
-struct libwebsocket_context {
+#ifdef _WIN32
+#define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
+struct lws_fd_hashtable {
+       struct lws **wsi;
+       int length;
+};
+#endif
+
+struct lws_context {
 #ifdef _WIN32
        WSAEVENT *events;
 #endif
-       struct libwebsocket_pollfd *fds;
-       struct libwebsocket **lws_lookup; /* fd to wsi */
+       struct lws_pollfd *fds;
+#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
        int fds_count;
 #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;
+       int use_ev_sigint;
 #endif /* LWS_USE_LIBEV */
        int max_fds;
        int listen_port;
        const char *iface;
        char http_proxy_address[128];
        char canonical_hostname[128];
+       char proxy_basic_auth_token[128];
        unsigned int http_proxy_port;
        unsigned int options;
        time_t last_timeout_check_s;
@@ -436,7 +491,7 @@ struct libwebsocket_context {
        int fd_random;
        int listen_service_modulo;
        int listen_service_count;
-       int listen_service_fd;
+       lws_sockfd_type listen_service_fd;
        int listen_service_extraseen;
 
        /*
@@ -462,16 +517,23 @@ struct libwebsocket_context {
 #ifdef LWS_OPENSSL_SUPPORT
        int use_ssl;
        int allow_non_ssl_on_ssl_port;
+       unsigned int user_supplied_ssl_ctx:1;
        SSL_CTX *ssl_ctx;
        SSL_CTX *ssl_client_ctx;
+       struct lws *pending_read_list; /* linked list */
+#define lws_ssl_anybody_has_buffered_read(ctx) (ctx->use_ssl && ctx->pending_read_list)
+#else
+#define lws_ssl_anybody_has_buffered_read(ctx) (0)
 #endif
-       struct libwebsocket_protocols *protocols;
+       const struct lws_protocols *protocols;
        int count_protocols;
 #ifndef LWS_NO_EXTENSIONS
-       struct libwebsocket_extension *extensions;
+       const struct lws_extension *extensions;
 #endif
-    struct lws_token_limits *token_limits;
+       const struct lws_token_limits *token_limits;
        void *user_space;
+
+       struct lws_plat_file_ops fops;
 };
 
 enum {
@@ -485,27 +547,30 @@ enum {
 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
 LWS_EXTERN void
-lws_libev_accept(struct libwebsocket_context *context,
-                struct libwebsocket *new_wsi, int accept_fd);
+lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
 LWS_EXTERN void
-lws_libev_io(struct libwebsocket_context *context,
-                               struct libwebsocket *wsi, int flags);
+lws_libev_io(struct lws *wsi, int flags);
 LWS_EXTERN int
-lws_libev_init_fd_table(struct libwebsocket_context *context);
+lws_libev_init_fd_table(struct lws_context *context);
 LWS_EXTERN void
-lws_libev_run(struct libwebsocket_context *context);
+lws_libev_run(const struct lws_context *context);
 #else
 #define LWS_LIBEV_ENABLED(context) (0)
+#ifdef LWS_POSIX
 #define lws_feature_status_libev(_a) \
                        lwsl_notice("libev support not compiled in\n")
-#define lws_libev_accept(_a, _b, _c) ((void) 0)
-#define lws_libev_io(_a, _b, _c) ((void) 0)
+#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
 
 #ifdef LWS_USE_IPV6
-#define LWS_IPV6_ENABLED(context) (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
+#define LWS_IPV6_ENABLED(context) \
+       (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
 #else
 #define LWS_IPV6_ENABLED(context) (0)
 #endif
@@ -515,7 +580,6 @@ enum uri_path_states {
        URIPS_SEEN_SLASH,
        URIPS_SEEN_SLASH_DOT,
        URIPS_SEEN_SLASH_DOT_DOT,
-       URIPS_ARGUMENTS,
 };
 
 enum uri_esc_states {
@@ -533,27 +597,37 @@ enum uri_esc_states {
 struct lws_fragments {
        unsigned short offset;
        unsigned short len;
-       unsigned char next_frag_index;
+       unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
 };
 
 /* 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
- * used interchangably to access the same data
+ * used interchangeably to access the same data
  */
 
 struct allocated_headers {
-       unsigned short next_frag_index;
+       unsigned char nfrag;
        unsigned short pos;
+       /*
+        * 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];
+       /*
+        * the randomly ordered fragments, indexed by frag_index and
+        * lws_fragments->nfrag for continuation.
+        */
        struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
        char data[LWS_MAX_HEADER_LEN];
+
 #ifndef LWS_NO_CLIENT
        char initial_handshake_hash_base64[30];
        unsigned short c_port;
@@ -563,18 +637,14 @@ struct allocated_headers {
 struct _lws_http_mode_related {
        /* MUST be first in struct */
        struct allocated_headers *ah; /* mirroring  _lws_header_related */
-#if defined(WIN32) || defined(_WIN32)
-       HANDLE fd;
-#else
-       int fd;
-#endif
+       lws_filefd_type fd;
        unsigned long filepos;
        unsigned long filelen;
 
        enum http_version request_version;
        enum http_connection_type connection_type;
-       int content_length;
-       int content_remain;
+       unsigned int content_length;
+       unsigned int content_remain;
 };
 
 #ifdef LWS_USE_HTTP2
@@ -586,7 +656,7 @@ enum lws_http2_settings {
        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 */
 };
 
@@ -601,10 +671,19 @@ enum lws_http2_wellknown_frame_types {
        LWS_HTTP2_FRAME_TYPE_GOAWAY,
        LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
        LWS_HTTP2_FRAME_TYPE_CONTINUATION,
-       
+
        LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
 };
 
+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
@@ -613,8 +692,53 @@ 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
@@ -623,21 +747,48 @@ struct _lws_http2_related {
 
        struct http2_settings my_settings;
        struct http2_settings peer_settings;
-       
-       struct libwebsocket *parent_wsi;
-       struct libwebsocket *next_child_wsi;
+
+       struct lws *parent_wsi;
+       struct lws *next_child_wsi;
+
+       struct hpack_dynamic_table *hpack_dyn_table;
 
        unsigned int count;
-       
+
        /* frame */
        unsigned int length;
        unsigned int stream_id;
-       struct libwebsocket *stream_wsi;
+       struct lws *stream_wsi;
        unsigned char type;
        unsigned char flags;
        unsigned char frame_state;
-
-       unsigned int tx_credit;
+       unsigned char padding;
+
+       unsigned char ping_payload[8];
+
+       unsigned short round_robin_POLLOUT;
+       unsigned short count_POLLOUT_children;
+
+       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;
+
+       /* hpack */
+       enum http2_hpack_state hpack;
+       enum http2_hpack_type hpack_type;
+       unsigned int header_index;
+       unsigned int hpack_len;
+       unsigned short hpack_pos;
+       unsigned char hpack_m;
+       unsigned int hpack_e_dep;
+       unsigned int huff:1;
+       unsigned int value:1;
+
+       /* negative credit is mandated by the spec */
+       int tx_credit;
        unsigned int my_stream_id;
        unsigned int child_count;
        int my_priority;
@@ -645,7 +796,7 @@ struct _lws_http2_related {
        unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
 };
 
-#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->parent_wsi)
+#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
 
 #endif
 
@@ -662,7 +813,7 @@ struct _lws_header_related {
 
 struct _lws_websocket_related {
        char *rx_user_buffer;
-       int rx_user_buffer_head;
+       unsigned int rx_user_buffer_head;
        unsigned char frame_masking_nonce_04[4];
        unsigned char frame_mask_index;
        size_t rx_packet_length;
@@ -676,24 +827,26 @@ struct _lws_websocket_related {
        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 char *ping_payload_buf; /* non-NULL if malloc'd */
        unsigned int ping_payload_alloc; /* length malloc'd */
-       unsigned int ping_payload_len; /* nonzero if PONG pending */
+       unsigned int ping_payload_len;
+       unsigned char ping_pending_flag;
 };
 
-struct libwebsocket {
+struct lws {
 
        /* lifetime members */
 
 #ifdef LWS_USE_LIBEV
-    struct lws_io_watcher w_read;
-    struct lws_io_watcher w_write;
+       struct lws_io_watcher w_read;
+       struct lws_io_watcher w_write;
 #endif /* LWS_USE_LIBEV */
-       const struct libwebsocket_protocols *protocol;
+       struct lws_context *context;
+       const struct lws_protocols *protocol;
 #ifndef LWS_NO_EXTENSIONS
-       struct libwebsocket_extension *
-                                  active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
+       const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
        void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
        unsigned char count_active_extensions;
        unsigned int extension_data_pending:1;
@@ -704,15 +857,15 @@ struct libwebsocket {
        char mode; /* enum connection_mode */
        char state; /* enum lws_connection_states */
        char lws_rx_parse_state; /* enum lws_rx_parse_state */
-       char rx_frame_type; /* enum libwebsocket_write_protocol */
+       char rx_frame_type; /* enum lws_write_protocol */
 
        unsigned int hdr_parsing_completed:1;
        unsigned int user_space_externally_allocated:1;
+       unsigned int socket_is_permanently_unusable:1;
 
        char pending_timeout; /* enum pending_timeout */
        time_t pending_timeout_limit;
-
-       int sock;
+       lws_sockfd_type sock;
        int position_in_fds_table;
 #ifdef LWS_LATENCY
        unsigned long action_start;
@@ -746,7 +899,9 @@ struct libwebsocket {
 #ifdef LWS_OPENSSL_SUPPORT
        SSL *ssl;
        BIO *client_bio;
+       struct lws *pending_read_list_prev, *pending_read_list_next;
        unsigned int use_ssl:2;
+       unsigned int upgraded:1;
 #endif
 
 #ifdef _WIN32
@@ -757,74 +912,85 @@ struct libwebsocket {
 LWS_EXTERN int log_level;
 
 LWS_EXTERN void
-libwebsocket_close_and_free_session(struct libwebsocket_context *context,
-                              struct libwebsocket *wsi, enum lws_close_status);
+lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
 
 LWS_EXTERN int
-remove_wsi_socket_from_fds(struct libwebsocket_context *context,
-                                                     struct libwebsocket *wsi);
+remove_wsi_socket_from_fds(struct lws *wsi);
 LWS_EXTERN int
-lws_rxflow_cache(struct libwebsocket *wsi, unsigned char *buf, int n, int len);
+lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
 
 #ifndef LWS_LATENCY
-static inline void lws_latency(struct libwebsocket_context *context,
-               struct libwebsocket *wsi, const char *action,
-                                        int ret, int completion) { while (0); }
-static inline void lws_latency_pre(struct libwebsocket_context *context,
-                                       struct libwebsocket *wsi) { while (0); }
+static inline void
+lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
+           int ret, int completion) {
+       do {
+               (void)context; (void)wsi; (void)action; (void)ret;
+               (void)completion;
+       } while (0);
+}
+static inline void
+lws_latency_pre(struct lws_context *context, struct lws *wsi) {
+       do { (void)context; (void)wsi; } while (0);
+}
 #else
 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
 extern void
-lws_latency(struct libwebsocket_context *context,
-                       struct libwebsocket *wsi, const char *action,
-                                                      int ret, int completion);
+lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
+           int ret, int completion);
 #endif
 
-LWS_EXTERN void lws_set_protocol_write_pending(struct libwebsocket_context *context,
-                                   struct libwebsocket *wsi,
-                                   enum lws_pending_protocol_send pend);
+LWS_EXTERN void
+lws_set_protocol_write_pending(struct lws *wsi,
+                              enum lws_pending_protocol_send pend);
 LWS_EXTERN int
-libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
+lws_client_rx_sm(struct lws *wsi, unsigned char c);
 
 LWS_EXTERN int
-libwebsocket_parse(struct libwebsocket_context *context,
-               struct libwebsocket *wsi, unsigned char c);
+lws_parse(struct lws *wsi, unsigned char c);
 
 LWS_EXTERN int
-lws_http_action(struct libwebsocket_context *context, struct libwebsocket *wsi);
+lws_http_action(struct lws *wsi);
 
 LWS_EXTERN int
 lws_b64_selftest(void);
 
-LWS_EXTERN struct libwebsocket *
-wsi_from_fd(struct libwebsocket_context *context, int fd);
+#if defined(_WIN32) || defined(MBED_OPERATORS)
+LWS_EXTERN struct lws *
+wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
+
+LWS_EXTERN int
+insert_wsi(struct lws_context *context, struct lws *wsi);
+
+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 delete_from_fd(A,B) A->lws_lookup[B]=0
+#endif
 
 LWS_EXTERN int
-insert_wsi_socket_into_fds(struct libwebsocket_context *context,
-                                                     struct libwebsocket *wsi);
+insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
 
 LWS_EXTERN int
-lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
+lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
 
 
 LWS_EXTERN int
-libwebsocket_service_timeout_check(struct libwebsocket_context *context,
-                                   struct libwebsocket *wsi, unsigned int sec);
+lws_service_timeout_check(struct lws *wsi, unsigned int sec);
 
-LWS_EXTERN struct libwebsocket *
-libwebsocket_client_connect_2(struct libwebsocket_context *context,
-       struct libwebsocket *wsi);
+LWS_EXTERN struct lws *
+lws_client_connect_2(struct lws *wsi);
 
-LWS_EXTERN struct libwebsocket *
-libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
+LWS_EXTERN struct lws *
+lws_create_new_server_wsi(struct lws_context *context);
 
 LWS_EXTERN char *
-libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
-               struct libwebsocket *wsi, char *pkt);
+lws_generate_client_handshake(struct lws *wsi, char *pkt);
 
 LWS_EXTERN int
-lws_handle_POLLOUT_event(struct libwebsocket_context *context,
-                             struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd);
+lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
+
 /*
  * EXTENSIONS
  */
@@ -832,22 +998,21 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
 #ifndef LWS_NO_EXTENSIONS
 LWS_VISIBLE void
 lws_context_init_extensions(struct lws_context_creation_info *info,
-                                   struct libwebsocket_context *context);
+                           struct lws_context *context);
 LWS_EXTERN int
-lws_any_extension_handled(struct libwebsocket_context *context,
-                         struct libwebsocket *wsi,
-                         enum libwebsocket_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_callback_for_each_active(struct libwebsocket *wsi, int reason,
-                                                   void *buf, int len);
+lws_ext_callback_for_each_active(struct lws *wsi, int reason,
+                                void *buf, int len);
 LWS_EXTERN int
-lws_ext_callback_for_each_extension_type(
-               struct libwebsocket_context *context, struct libwebsocket *wsi,
-                       int reason, void *arg, int len);
+lws_ext_callback_for_each_extension_type(struct lws_context *context,
+                                        struct lws *wsi, int reason,
+                                        void *arg, int len);
 #else
-#define lws_any_extension_handled(_a, _b, _c, _d, _e) (0)
+#define lws_any_extension_handled(_a, _b, _c, _d) (0)
 #define lws_ext_callback_for_each_active(_a, _b, _c, _d) (0)
 #define lws_ext_callback_for_each_extension_type(_a, _b, _c, _d, _e) (0)
 #define lws_issue_raw_ext_access lws_issue_raw
@@ -855,71 +1020,96 @@ lws_ext_callback_for_each_extension_type(
 #endif
 
 LWS_EXTERN int
-lws_client_interpret_server_handshake(struct libwebsocket_context *context,
-               struct libwebsocket *wsi);
+lws_client_interpret_server_handshake(struct lws *wsi);
 
 LWS_EXTERN int
-libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
+lws_rx_sm(struct lws *wsi, unsigned char c);
 
 LWS_EXTERN int
-lws_issue_raw_ext_access(struct libwebsocket *wsi,
-                                               unsigned char *buf, size_t len);
+lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
 
 LWS_EXTERN int
-_libwebsocket_rx_flow_control(struct libwebsocket *wsi);
+_lws_rx_flow_control(struct lws *wsi);
+
+LWS_EXTERN void
+lws_union_transition(struct lws *wsi, enum connection_mode mode);
 
 LWS_EXTERN int
-user_callback_handle_rxflow(callback_function,
-               struct libwebsocket_context *context,
-                       struct libwebsocket *wsi,
-                        enum libwebsocket_callback_reasons reason, void *user,
-                                                         void *in, size_t len);
+user_callback_handle_rxflow(callback_function, struct lws *wsi,
+                           enum lws_callback_reasons reason, void *user,
+                           void *in, size_t len);
 #ifdef LWS_USE_HTTP2
+LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
+struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
 LWS_EXTERN int
-lws_http2_interpret_settings_payload(struct http2_settings *settings, unsigned char *buf, int len);
+lws_http2_interpret_settings_payload(struct http2_settings *settings,
+                                    unsigned char *buf, int len);
 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
 LWS_EXTERN int
-lws_http2_parser(struct libwebsocket_context *context,
-                    struct libwebsocket *wsi, unsigned char c);
-LWS_EXTERN int lws_http2_do_pps_send(struct libwebsocket_context *context, struct libwebsocket *wsi);
-LWS_EXTERN int lws_http2_frame_write(struct libwebsocket *wsi, int type, int flags, unsigned int sid, unsigned int len, unsigned char *buf);
-LWS_EXTERN struct libwebsocket *
-lws_http2_wsi_from_id(struct libwebsocket *wsi, unsigned int sid);
+lws_http2_parser(struct lws *wsi, unsigned char c);
+LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
+                                    struct lws *wsi);
+LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
+                                    unsigned int sid, unsigned int len,
+                                    unsigned char *buf);
+LWS_EXTERN struct lws *
+lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
+LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
+                                  unsigned char c);
+LWS_EXTERN int
+lws_add_http2_header_by_name(struct lws *wsi,
+                            const unsigned char *name,
+                            const unsigned char *value, int length,
+                            unsigned char **p, unsigned char *end);
+LWS_EXTERN int
+lws_add_http2_header_by_token(struct lws *wsi,
+                           enum lws_token_indexes token,
+                           const unsigned char *value, int length,
+                           unsigned char **p, unsigned char *end);
+LWS_EXTERN int
+lws_add_http2_header_status(struct lws *wsi,
+                           unsigned int code, unsigned char **p,
+                           unsigned char *end);
+LWS_EXTERN
+void lws_http2_configure_if_upgraded(struct lws *wsi);
+#else
+#define lws_http2_configure_if_upgraded(x)
 #endif
 
 LWS_EXTERN int
-lws_plat_set_socket_options(struct libwebsocket_context *context, int fd);
+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_allocate_header_table(struct libwebsocket *wsi);
+lws_free_header_table(struct lws *wsi);
 
 LWS_EXTERN char *
-lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
+lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
 
 LWS_EXTERN int
-lws_hdr_simple_create(struct libwebsocket *wsi,
-                               enum lws_token_indexes h, const char *s);
+lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
 
 LWS_EXTERN int
-libwebsocket_ensure_user_space(struct libwebsocket *wsi);
+lws_ensure_user_space(struct lws *wsi);
 
 LWS_EXTERN int
-lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or);
+lws_change_pollfd(struct lws *wsi, int _and, int _or);
 
 #ifndef LWS_NO_SERVER
 int lws_context_init_server(struct lws_context_creation_info *info,
-                           struct libwebsocket_context *context);
-LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
-                                                     struct libwebsocket *wsi);
+                           struct lws_context *context);
+LWS_EXTERN int
+handshake_0405(struct lws_context *context, struct lws *wsi);
 LWS_EXTERN int
-libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
-                                               unsigned char *buf, size_t len);
+lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
 LWS_EXTERN void
-lws_server_get_canonical_hostname(struct libwebsocket_context *context,
-                               struct lws_context_creation_info *info);
+lws_server_get_canonical_hostname(struct lws_context *context,
+                                 struct lws_context_creation_info *info);
 #else
 #define lws_context_init_server(_a, _b) (0)
-#define libwebsocket_interpret_incoming_packet(_a, _b, _c) (0)
+#define lws_interpret_incoming_packet(_a, _b, _c) (0)
 #define lws_server_get_canonical_hostname(_a, _b)
 #endif
 
@@ -929,16 +1119,12 @@ LWS_EXTERN int get_daemonize_pid();
 #define get_daemonize_pid() (0)
 #endif
 
-LWS_EXTERN int interface_to_sa(struct libwebsocket_context *context,
-               const char *ifname, struct sockaddr_in *addr, size_t addrlen);
-
-LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
-
-#ifdef _WIN32
-LWS_EXTERN HANDLE lws_plat_open_file(const char* filename, unsigned long* filelen);
-#else
-LWS_EXTERN int lws_plat_open_file(const char* filename, unsigned long* filelen);
+#if !defined(MBED_OPERATORS)
+LWS_EXTERN int
+interface_to_sa(struct lws_context *context, const char *ifname,
+               struct sockaddr_in *addr, size_t addrlen);
 #endif
+LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
 
 enum lws_ssl_capable_status {
        LWS_SSL_CAPABLE_ERROR = -1,
@@ -950,115 +1136,144 @@ enum lws_ssl_capable_status {
 #define lws_context_init_server_ssl(_a, _b) (0)
 #define lws_ssl_destroy(_a)
 #define lws_context_init_http2_ssl(_a)
-#define lws_ssl_pending(_a) (0)
 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
-#define lws_server_socket_service_ssl(_a, _b, _c, _d, _e) (0)
+#define lws_ssl_pending lws_ssl_pending_no_ssl
+#define lws_server_socket_service_ssl(_a, _b, _c, _d) (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 lws_ssl_pending(struct libwebsocket *wsi);
 LWS_EXTERN int openssl_websocket_private_data_index;
 LWS_EXTERN int
-lws_ssl_capable_read(struct libwebsocket *wsi, unsigned char *buf, int len);
-
+lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
+LWS_EXTERN int
+lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
 LWS_EXTERN int
-lws_ssl_capable_write(struct libwebsocket *wsi, unsigned char *buf, int len);
+lws_ssl_pending(struct lws *wsi);
 LWS_EXTERN int
-lws_server_socket_service_ssl(struct libwebsocket_context *context,
-               struct libwebsocket **wsi, struct libwebsocket *new_wsi,
-               int accept_fd, struct libwebsocket_pollfd *pollfd);
+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_ssl_close(struct libwebsocket *wsi);
+lws_ssl_close(struct lws *wsi);
 LWS_EXTERN void
-lws_ssl_context_destroy(struct libwebsocket_context *context);
+lws_ssl_context_destroy(struct lws_context *context);
+LWS_VISIBLE void
+lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
 #ifndef LWS_NO_SERVER
 LWS_EXTERN int
 lws_context_init_server_ssl(struct lws_context_creation_info *info,
-                    struct libwebsocket_context *context);
+                           struct lws_context *context);
 #else
 #define lws_context_init_server_ssl(_a, _b) (0)
 #endif
 LWS_EXTERN void
-lws_ssl_destroy(struct libwebsocket_context *context);
+lws_ssl_destroy(struct lws_context *context);
 
 /* HTTP2-related */
 
 #ifdef LWS_USE_HTTP2
 LWS_EXTERN void
-lws_context_init_http2_ssl(struct libwebsocket_context *context);
+lws_context_init_http2_ssl(struct lws_context *context);
 #else
 #define lws_context_init_http2_ssl(_a)
 #endif
 #endif
 
 LWS_EXTERN int
-lws_ssl_capable_read_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int len);
+lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
+
+LWS_EXTERN int
+lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
 
 LWS_EXTERN int
-lws_ssl_capable_write_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int len);
+lws_ssl_pending_no_ssl(struct lws *wsi);
 
 #ifndef LWS_NO_CLIENT
-       LWS_EXTERN int lws_client_socket_service(
-               struct libwebsocket_context *context,
-               struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd);
+LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
+                                        struct lws *wsi,
+                                        struct lws_pollfd *pollfd);
 #ifdef LWS_OPENSSL_SUPPORT
-       LWS_EXTERN int lws_context_init_client_ssl(struct lws_context_creation_info *info,
-                           struct libwebsocket_context *context);
+LWS_EXTERN int
+lws_context_init_client_ssl(struct lws_context_creation_info *info,
+                           struct lws_context *context);
 #else
        #define lws_context_init_client_ssl(_a, _b) (0)
 #endif
-       LWS_EXTERN int lws_handshake_client(struct libwebsocket *wsi, unsigned char **buf, size_t len);
-       LWS_EXTERN void
-       libwebsockets_decode_ssl_error(void);
+LWS_EXTERN int
+lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
+LWS_EXTERN void
+lws_decode_ssl_error(void);
 #else
 #define lws_context_init_client_ssl(_a, _b) (0)
 #define lws_handshake_client(_a, _b, _c) (0)
 #endif
 #ifndef LWS_NO_SERVER
-       LWS_EXTERN int lws_server_socket_service(
-               struct libwebsocket_context *context,
-               struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd);
-       LWS_EXTERN int _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
-       LWS_EXTERN int lws_handshake_server(struct libwebsocket_context *context,
-                    struct libwebsocket *wsi, unsigned char **buf, size_t len);
+LWS_EXTERN int
+lws_server_socket_service(struct lws_context *context, struct lws *wsi,
+                         struct lws_pollfd *pollfd);
+LWS_EXTERN int
+_lws_rx_flow_control(struct lws *wsi);
+LWS_EXTERN int
+lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
 #else
 #define lws_server_socket_service(_a, _b, _c) (0)
-#define _libwebsocket_rx_flow_control(_a) (0)
-#define lws_handshake_server(_a, _b, _c, _d) (0)
+#define _lws_rx_flow_control(_a) (0)
+#define lws_handshake_server(_a, _b, _c) (0)
 #endif
 
+LWS_EXTERN int
+lws_get_addresses(struct lws_context *context, void *ads, char *name,
+                 int name_len, char *rip, int rip_len);
+
 /*
- * lws_plat_
+ * custom allocator
  */
+LWS_EXTERN void*
+lws_realloc(void *ptr, size_t size);
+
+LWS_EXTERN void*
+lws_zalloc(size_t size);
+
+#define lws_malloc(S)  lws_realloc(NULL, S)
+#define lws_free(P)    lws_realloc(P, 0)
+#define lws_free2(P)   do { lws_realloc(P, 0); (P) = NULL; } while(0)
+
+/* lws_plat_ */
 LWS_EXTERN void
-lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
-                                              struct libwebsocket *wsi, int m);
+lws_plat_delete_socket_from_fds(struct lws_context *context,
+                               struct lws *wsi, int m);
 LWS_EXTERN void
-lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
-                                                     struct libwebsocket *wsi);
+lws_plat_insert_socket_into_fds(struct lws_context *context,
+                               struct lws *wsi);
 LWS_EXTERN void
-lws_plat_service_periodic(struct libwebsocket_context *context);
+lws_plat_service_periodic(struct lws_context *context);
 
 LWS_EXTERN int
-lws_plat_change_pollfd(struct libwebsocket_context *context,
-                    struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd);
+lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
+                      struct lws_pollfd *pfd);
 LWS_EXTERN int
 lws_plat_context_early_init(void);
 LWS_EXTERN void
-lws_plat_context_early_destroy(struct libwebsocket_context *context);
+lws_plat_context_early_destroy(struct lws_context *context);
 LWS_EXTERN void
-lws_plat_context_late_destroy(struct libwebsocket_context *context);
+lws_plat_context_late_destroy(struct lws_context *context);
 LWS_EXTERN int
-lws_poll_listen_fd(struct libwebsocket_pollfd *fd);
+lws_poll_listen_fd(struct lws_pollfd *fd);
 LWS_EXTERN int
-lws_plat_service(struct libwebsocket_context *context, int timeout_ms);
+lws_plat_service(struct lws_context *context, int timeout_ms);
 LWS_EXTERN int
-lws_plat_init_fd_tables(struct libwebsocket_context *context);
+lws_plat_init(struct lws_context *context,
+             struct lws_context_creation_info *info);
 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_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
+
+#ifdef __cplusplus
+};
+#endif