2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "lws_config.h"
23 #include "lws_config_private.h"
25 #ifdef LWS_HAVE_SYS_TYPES_H
26 #include <sys/types.h>
41 #ifdef LWS_HAVE_SYS_STAT_H
45 #if defined(WIN32) || defined(_WIN32)
50 #define _WIN32_WINNT WINVER
52 #define LWS_NO_DAEMONIZE
53 #define LWS_ERRNO WSAGetLastError()
54 #define LWS_EAGAIN WSAEWOULDBLOCK
55 #define LWS_EALREADY WSAEALREADY
56 #define LWS_EINPROGRESS WSAEINPROGRESS
57 #define LWS_EINTR WSAEINTR
58 #define LWS_EISCONN WSAEISCONN
59 #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
60 #define LWS_POLLHUP (FD_CLOSE)
61 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
62 #define LWS_POLLOUT (FD_WRITE)
63 #define MSG_NOSIGNAL 0
64 #define SHUT_RDWR SD_BOTH
65 #define SOL_TCP IPPROTO_TCP
66 #define SHUT_WR SD_SEND
68 #define compatible_close(fd) closesocket(fd)
69 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
70 #define lws_socket_is_valid(x) (!!x)
71 #define LWS_SOCK_INVALID 0
76 #ifdef LWS_HAVE_IN6ADDR_H
82 #define __func__ __FUNCTION__
86 #define vsnprintf _vsnprintf
89 #else /* not windows --> */
94 #include <sys/types.h>
95 #ifndef MBED_OPERATORS
101 #include <sys/socket.h>
102 #ifdef LWS_BUILTIN_GETIFADDRS
103 #include <getifaddrs.h>
107 #if defined (__ANDROID__)
110 #include <sys/syslog.h>
113 #include <sys/socket.h>
115 #include <netinet/in.h>
116 #include <netinet/tcp.h>
117 #include <arpa/inet.h>
125 #include <sys/mman.h>
130 #ifdef LWS_HAVE_SYS_PRCTL_H
131 #include <sys/prctl.h>
135 #include <sys/time.h>
137 #define LWS_ERRNO errno
138 #define LWS_EAGAIN EAGAIN
139 #define LWS_EALREADY EALREADY
140 #define LWS_EINPROGRESS EINPROGRESS
141 #define LWS_EINTR EINTR
142 #define LWS_EISCONN EISCONN
143 #define LWS_EWOULDBLOCK EWOULDBLOCK
144 #define LWS_POLLHUP (POLLHUP|POLLERR)
145 #define LWS_POLLIN (POLLIN)
146 #define LWS_POLLOUT (POLLOUT)
147 #define compatible_close(fd) close(fd)
148 #define lws_set_blocking_send(wsi)
150 #ifdef MBED_OPERATORS
151 #define lws_socket_is_valid(x) ((x) != NULL)
152 #define LWS_SOCK_INVALID (NULL)
154 #define lws_socket_is_valid(x) (x >= 0)
155 #define LWS_SOCK_INVALID (-1)
159 #ifndef LWS_HAVE_BZERO
161 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
165 #ifndef LWS_HAVE_STRERROR
166 #define strerror(x) ""
169 #ifdef LWS_OPENSSL_SUPPORT
171 #ifdef USE_OLD_CYASSL
172 #include <cyassl/openssl/ssl.h>
173 #include <cyassl/error-ssl.h>
175 #include <wolfssl/openssl/ssl.h>
176 #include <wolfssl/error-ssl.h>
177 #endif /* not USE_OLD_CYASSL */
179 #include <openssl/ssl.h>
180 #include <openssl/evp.h>
181 #include <openssl/err.h>
182 #include <openssl/md5.h>
183 #include <openssl/sha.h>
184 #endif /* not USE_WOLFSSL */
187 #include "libwebsockets.h"
189 #if defined(MBED_OPERATORS)
190 #undef compatible_close
191 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
193 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
195 #ifndef LITTLE_ENDIAN
196 #define LITTLE_ENDIAN 1234
199 #define BYTE_ORDER LITTLE_ENDIAN
203 #if defined(WIN32) || defined(_WIN32)
206 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
208 #ifndef LITTLE_ENDIAN
209 #define LITTLE_ENDIAN 1234
212 #define BYTE_ORDER LITTLE_ENDIAN
215 typedef unsigned __int64 u_int64_t;
221 #define __P(protos) protos
223 #define __P(protos) ()
229 #include <sys/stat.h>
230 #include <sys/cdefs.h>
231 #include <sys/time.h>
233 #if defined(__APPLE__)
234 #include <machine/endian.h>
235 #elif defined(__FreeBSD__)
236 #include <sys/endian.h>
237 #elif defined(__linux__)
247 #define container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
251 #include <gulliver.h>
252 #if defined(__LITTLEENDIAN__)
253 #define BYTE_ORDER __LITTLEENDIAN__
254 #define LITTLE_ENDIAN __LITTLEENDIAN__
255 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
257 #if defined(__BIGENDIAN__)
258 #define BYTE_ORDER __BIGENDIAN__
259 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
260 #define BIG_ENDIAN __BIGENDIAN__
264 #if !defined(BYTE_ORDER)
265 # define BYTE_ORDER __BYTE_ORDER
267 #if !defined(LITTLE_ENDIAN)
268 # define LITTLE_ENDIAN __LITTLE_ENDIAN
270 #if !defined(BIG_ENDIAN)
271 # define BIG_ENDIAN __BIG_ENDIAN
277 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
278 * but happily have something equivalent in the SO_NOSIGPIPE flag.
281 #define MSG_NOSIGNAL SO_NOSIGPIPE
285 #ifndef FD_HASHTABLE_MODULUS
286 #define FD_HASHTABLE_MODULUS 32
290 #ifndef LWS_DEF_HEADER_LEN
291 #define LWS_DEF_HEADER_LEN 1024
293 #ifndef LWS_DEF_HEADER_POOL
294 #define LWS_DEF_HEADER_POOL 16
296 #ifndef LWS_MAX_PROTOCOLS
297 #define LWS_MAX_PROTOCOLS 5
299 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
300 #define LWS_MAX_EXTENSIONS_ACTIVE 2
302 #ifndef LWS_MAX_EXT_OFFERS
303 #define LWS_MAX_EXT_OFFERS 8
305 #ifndef SPEC_LATEST_SUPPORTED
306 #define SPEC_LATEST_SUPPORTED 13
308 #ifndef AWAITING_TIMEOUT
309 #define AWAITING_TIMEOUT 20
311 #ifndef CIPHERS_LIST_STRING
312 #define CIPHERS_LIST_STRING "DEFAULT"
314 #ifndef LWS_SOMAXCONN
315 #define LWS_SOMAXCONN SOMAXCONN
318 #define MAX_WEBSOCKET_04_KEY_LEN 128
319 #define LWS_MAX_SOCKET_IO_BUF 4096
321 #ifndef SYSTEM_RANDOM_FILEPATH
322 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
325 enum lws_websocket_opcodes_07 {
326 LWSWSOPC_CONTINUATION = 0,
327 LWSWSOPC_TEXT_FRAME = 1,
328 LWSWSOPC_BINARY_FRAME = 2,
330 LWSWSOPC_NOSPEC__MUX = 7,
332 /* control extensions 8+ */
340 enum lws_connection_states {
342 LWSS_HTTP_ISSUING_FILE,
347 LWSS_CLIENT_UNCONNECTED,
348 LWSS_RETURNED_CLOSE_ALREADY,
349 LWSS_AWAITING_CLOSE_ACK,
350 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
353 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
354 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
355 LWSS_HTTP2_ESTABLISHED,
363 enum http_connection_type {
364 HTTP_CONNECTION_CLOSE,
365 HTTP_CONNECTION_KEEP_ALIVE
368 enum lws_pending_protocol_send {
370 LWS_PPS_HTTP2_MY_SETTINGS,
371 LWS_PPS_HTTP2_ACK_SETTINGS,
375 enum lws_rx_parse_state {
382 LWS_RXPS_04_FRAME_HDR_1,
383 LWS_RXPS_04_FRAME_HDR_LEN,
384 LWS_RXPS_04_FRAME_HDR_LEN16_2,
385 LWS_RXPS_04_FRAME_HDR_LEN16_1,
386 LWS_RXPS_04_FRAME_HDR_LEN64_8,
387 LWS_RXPS_04_FRAME_HDR_LEN64_7,
388 LWS_RXPS_04_FRAME_HDR_LEN64_6,
389 LWS_RXPS_04_FRAME_HDR_LEN64_5,
390 LWS_RXPS_04_FRAME_HDR_LEN64_4,
391 LWS_RXPS_04_FRAME_HDR_LEN64_3,
392 LWS_RXPS_04_FRAME_HDR_LEN64_2,
393 LWS_RXPS_04_FRAME_HDR_LEN64_1,
395 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
396 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
397 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
398 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
400 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
404 enum connection_mode {
406 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
407 LWSCM_PRE_WS_SERVING_ACCEPT,
414 /* transient, ssl delay hiding */
415 LWSCM_SSL_ACK_PENDING,
418 /* transient modes */
419 LWSCM_WSCL_WAITING_CONNECT,
420 LWSCM_WSCL_WAITING_PROXY_REPLY,
421 LWSCM_WSCL_ISSUE_HANDSHAKE,
422 LWSCM_WSCL_ISSUE_HANDSHAKE2,
423 LWSCM_WSCL_WAITING_SSL,
424 LWSCM_WSCL_WAITING_SERVER_REPLY,
425 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
426 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
428 /* special internal types */
429 LWSCM_SERVER_LISTENER,
433 LWS_RXFLOW_ALLOW = (1 << 0),
434 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
437 /* this is not usable directly by user code any more, lws_close_reason() */
438 #define LWS_WRITE_CLOSE 4
440 struct lws_protocols;
443 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
445 struct lws_io_watcher {
450 uv_poll_t uv_watcher;
452 struct lws_context *context;
455 struct lws_signal_watcher {
457 ev_signal ev_watcher;
460 uv_signal_t uv_watcher;
462 struct lws_context *context;
467 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
468 struct lws_fd_hashtable {
475 * This is totally opaque to code using the library. It's exported as a
476 * forward-reference pointer-only declaration; the user can use the pointer with
477 * other APIs to get information out of it.
480 struct lws_fragments {
481 unsigned short offset;
483 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
487 * these are assigned from a pool held in the context.
488 * Both client and server mode uses them for http header analysis
491 struct allocated_headers {
492 struct lws *wsi; /* owner */
493 char *data; /* prepared by context init to point to dedicated storage */
495 * the randomly ordered fragments, indexed by frag_index and
496 * lws_fragments->nfrag for continuation.
498 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
501 * for each recognized token, frag_index says which frag[] his data
502 * starts in (0 means the token did not appear)
503 * the actual header data gets dumped as it comes in, into data[]
505 unsigned char frag_index[WSI_TOKEN_COUNT];
506 unsigned char rx[2048];
510 #ifndef LWS_NO_CLIENT
511 char initial_handshake_hash_base64[30];
512 unsigned short c_port;
516 unsigned char in_use;
521 * so we can have n connections being serviced simultaneously,
522 * these things need to be isolated per-thread.
525 struct lws_context_per_thread {
527 pthread_mutex_t lock;
529 struct lws_pollfd *fds;
530 struct lws *rx_draining_ext_list;
531 struct lws *tx_draining_ext_list;
532 struct lws *timeout_list;
533 void *http_header_data;
534 struct allocated_headers *ah_pool;
535 struct lws *ah_wait_list;
536 int ah_wait_list_length;
537 #ifdef LWS_OPENSSL_SUPPORT
538 struct lws *pending_read_list; /* linked list */
540 #ifndef LWS_NO_SERVER
541 struct lws *wsi_listening;
543 #if defined(LWS_USE_LIBEV)
544 struct ev_loop *io_loop_ev;
546 #if defined(LWS_USE_LIBUV)
547 uv_loop_t *io_loop_uv;
548 uv_signal_t signals[8];
550 #if defined(LWS_USE_LIBEV)
551 struct lws_io_watcher w_accept;
553 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
554 struct lws_signal_watcher w_sigint;
555 unsigned char ev_loop_foreign:1;
557 lws_sockfd_type lserv_fd;
559 unsigned long count_conns;
561 * usable by anything in the service code, but only if the scope
562 * does not last longer than the service action (since next service
563 * of any socket can likewise use it and overwrite)
565 unsigned char *serv_buf;
569 int dummy_pipe_fds[2];
571 unsigned int fds_count;
573 short ah_count_in_use;
577 * the rest is managed per-context, that includes
579 * - processwide single fd -> wsi lookup
580 * - contextwide headers pool
581 * - contextwide ssl context
582 * - contextwide proxy
586 time_t last_timeout_check_s;
587 struct lws_plat_file_ops fops;
588 struct lws_context_per_thread pt[LWS_MAX_SMP];
590 /* different implementation between unix and windows */
591 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
593 struct lws **lws_lookup; /* fd to wsi */
596 const struct lws_token_limits *token_limits;
599 const struct lws_protocols *protocols;
601 #ifdef LWS_OPENSSL_SUPPORT
603 SSL_CTX *ssl_client_ctx;
605 #ifndef LWS_NO_EXTENSIONS
606 const struct lws_extension *extensions;
608 #if defined(LWS_USE_LIBEV)
609 lws_ev_signal_cb_t * lws_ev_sigint_cb;
611 #if defined(LWS_USE_LIBUV)
612 lws_uv_signal_cb_t * lws_uv_sigint_cb;
614 char http_proxy_address[128];
615 char proxy_basic_auth_token[128];
616 char canonical_hostname[128];
618 unsigned long worst_latency;
619 char worst_latency_info[256];
624 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
627 int started_with_parent;
631 int count_wsi_allocated;
632 unsigned int http_proxy_port;
633 unsigned int options;
634 unsigned int fd_limit_per_thread;
635 unsigned int timeout_secs;
638 * set to the Thread ID that's doing the service loop just before entry
639 * to poll indicates service thread likely idling in poll()
640 * volatile because other threads may check it as part of processing
641 * for pollfd event change.
643 volatile int service_tid;
644 int service_tid_detected;
651 #ifdef LWS_OPENSSL_SUPPORT
653 int allow_non_ssl_on_ssl_port;
654 unsigned int user_supplied_ssl_ctx:1;
655 #define lws_ssl_anybody_has_buffered_read(w) \
656 (w->context->use_ssl && \
657 w->context->pt[(int)w->tsi].pending_read_list)
658 #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
660 c->pt[(int)t].pending_read_list)
662 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
663 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
666 short max_http_header_data;
667 short max_http_header_pool;
670 unsigned int being_destroyed:1;
671 unsigned int requested_kill:1;
675 lws_close_free_wsi_final(struct lws *wsi);
677 lws_libuv_closehandle(struct lws *wsi);
680 LWS_EV_READ = (1 << 0),
681 LWS_EV_WRITE = (1 << 1),
682 LWS_EV_START = (1 << 2),
683 LWS_EV_STOP = (1 << 3),
685 LWS_EV_PREPARE_DELETION = (1 << 31),
688 #if defined(LWS_USE_LIBEV)
690 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
692 lws_libev_io(struct lws *wsi, int flags);
694 lws_libev_init_fd_table(struct lws_context *context);
696 lws_libev_destroyloop(struct lws_context *context, int tsi);
698 lws_libev_run(const struct lws_context *context, int tsi);
699 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
700 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
702 #define lws_libev_accept(_a, _b) ((void) 0)
703 #define lws_libev_io(_a, _b) ((void) 0)
704 #define lws_libev_init_fd_table(_a) (0)
705 #define lws_libev_run(_a, _b) ((void) 0)
706 #define lws_libev_destroyloop(_a, _b) ((void) 0)
707 #define LWS_LIBEV_ENABLED(context) (0)
709 #define lws_feature_status_libev(_a) \
710 lwsl_notice("libev support not compiled in\n")
712 #define lws_feature_status_libev(_a)
716 #if defined(LWS_USE_LIBUV)
718 lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
720 lws_libuv_io(struct lws *wsi, int flags);
722 lws_libuv_init_fd_table(struct lws_context *context);
724 lws_libuv_run(const struct lws_context *context, int tsi);
726 lws_libuv_destroyloop(struct lws_context *context, int tsi);
727 #define LWS_LIBUV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBUV)
728 LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
730 #define lws_libuv_accept(_a, _b) ((void) 0)
731 #define lws_libuv_io(_a, _b) ((void) 0)
732 #define lws_libuv_init_fd_table(_a) (0)
733 #define lws_libuv_run(_a, _b) ((void) 0)
734 #define lws_libuv_destroyloop(_a, _b) ((void) 0)
735 #define LWS_LIBUV_ENABLED(context) (0)
737 #define lws_feature_status_libuv(_a) \
738 lwsl_notice("libuv support not compiled in\n")
740 #define lws_feature_status_libuv(_a)
746 #define LWS_IPV6_ENABLED(context) \
747 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
749 #define LWS_IPV6_ENABLED(context) (0)
752 enum uri_path_states {
755 URIPS_SEEN_SLASH_DOT,
756 URIPS_SEEN_SLASH_DOT_DOT,
759 enum uri_esc_states {
762 URIES_SEEN_PERCENT_H1,
765 /* notice that these union members:
771 * all have a pointer to allocated_headers struct as their first member.
773 * It means for allocated_headers access, the three union paths can all be
774 * used interchangeably to access the same data
777 struct _lws_http_mode_related {
778 /* MUST be first in struct */
779 struct allocated_headers *ah; /* mirroring _lws_header_related */
780 struct lws *ah_wait_list;
781 struct lws *new_wsi_list;
782 unsigned long filepos;
783 unsigned long filelen;
786 enum http_version request_version;
787 enum http_connection_type connection_type;
788 unsigned int content_length;
789 unsigned int content_remain;
794 enum lws_http2_settings {
795 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
796 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
797 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
798 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
799 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
800 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
802 LWS_HTTP2_SETTINGS__COUNT /* always last */
805 enum lws_http2_wellknown_frame_types {
806 LWS_HTTP2_FRAME_TYPE_DATA,
807 LWS_HTTP2_FRAME_TYPE_HEADERS,
808 LWS_HTTP2_FRAME_TYPE_PRIORITY,
809 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
810 LWS_HTTP2_FRAME_TYPE_SETTINGS,
811 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
812 LWS_HTTP2_FRAME_TYPE_PING,
813 LWS_HTTP2_FRAME_TYPE_GOAWAY,
814 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
815 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
817 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
820 enum lws_http2_flags {
821 LWS_HTTP2_FLAG_END_STREAM = 1,
822 LWS_HTTP2_FLAG_END_HEADERS = 4,
823 LWS_HTTP2_FLAG_PADDED = 8,
824 LWS_HTTP2_FLAG_PRIORITY = 0x20,
826 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
829 #define LWS_HTTP2_STREAM_ID_MASTER 0
830 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
831 #define LWS_HTTP2_SETTINGS_LENGTH 6
833 struct http2_settings {
834 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
837 enum http2_hpack_state {
839 /* optional before first header block */
841 HKPS_OPT_E_DEPENDENCY,
854 /* optional after last header block */
855 HKPS_OPT_DISCARD_PADDING,
858 enum http2_hpack_type {
860 HPKT_INDEXED_HDR_6_VALUE_INCR,
861 HPKT_LITERAL_HDR_VALUE_INCR,
862 HPKT_INDEXED_HDR_4_VALUE,
863 HPKT_LITERAL_HDR_VALUE,
867 struct hpack_dt_entry {
868 int token; /* additions that don't map to a token are ignored */
873 struct hpack_dynamic_table {
874 struct hpack_dt_entry *entries;
882 struct _lws_http2_related {
884 * having this first lets us also re-use all HTTP union code
885 * and in turn, http_mode_related has allocated headers in right
886 * place so we can use the header apis on the wsi directly still
888 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
890 struct http2_settings my_settings;
891 struct http2_settings peer_settings;
893 struct lws *parent_wsi;
894 struct lws *next_child_wsi;
896 struct hpack_dynamic_table *hpack_dyn_table;
897 struct lws *stream_wsi;
898 unsigned char ping_payload[8];
899 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
903 unsigned int stream_id;
904 enum http2_hpack_state hpack;
905 enum http2_hpack_type hpack_type;
906 unsigned int header_index;
907 unsigned int hpack_len;
908 unsigned int hpack_e_dep;
910 unsigned int my_stream_id;
911 unsigned int child_count;
914 unsigned int END_STREAM:1;
915 unsigned int END_HEADERS:1;
916 unsigned int send_END_STREAM:1;
917 unsigned int GOING_AWAY;
918 unsigned int requested_POLLOUT:1;
919 unsigned int waiting_tx_credit:1;
921 unsigned int value:1;
923 unsigned short round_robin_POLLOUT;
924 unsigned short count_POLLOUT_children;
925 unsigned short hpack_pos;
929 unsigned char frame_state;
930 unsigned char padding;
931 unsigned char hpack_m;
932 unsigned char initialized;
935 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
939 struct _lws_header_related {
940 /* MUST be first in struct */
941 struct allocated_headers *ah;
942 struct lws *ah_wait_list;
943 enum uri_path_states ups;
944 enum uri_esc_states ues;
946 unsigned short current_token_limit;
948 char post_literal_equal;
949 unsigned char parser_state; /* enum lws_token_indexes */
951 char more_rx_waiting;
954 struct _lws_websocket_related {
955 /* cheapest way to deal with ah overlap with ws union transition */
956 struct _lws_header_related hdr;
958 unsigned int rx_ubuf_alloc;
959 struct lws *rx_draining_ext_list;
960 struct lws *tx_draining_ext_list;
961 size_t rx_packet_length;
962 unsigned int rx_ubuf_head;
963 unsigned char mask[4];
964 /* Also used for close content... control opcode == < 128 */
965 unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
967 unsigned char ping_payload_len;
968 unsigned char mask_idx;
969 unsigned char opcode;
971 unsigned char rsv_first_msg;
972 /* zero if no info, or length including 2-byte close code */
973 unsigned char close_in_ping_buffer_len;
975 unsigned char stashed_write_type;
976 unsigned char tx_draining_stashed_wp;
978 unsigned int final:1;
979 unsigned int frame_is_binary:1;
980 unsigned int all_zero_nonce:1;
981 unsigned int this_frame_masked:1;
982 unsigned int inside_frame:1; /* next write will be more of frame */
983 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
984 unsigned int payload_is_close:1; /* process as PONG, but it is close */
985 unsigned int ping_pending_flag:1;
986 unsigned int continuation_possible:1;
987 unsigned int owed_a_fin:1;
988 unsigned int check_utf8:1;
989 unsigned int defeat_check_utf8:1;
990 unsigned int pmce_compressed_message:1;
991 unsigned int stashed_write_pending:1;
992 unsigned int rx_draining_ext:1;
993 unsigned int tx_draining_ext:1;
999 /* members with mutually exclusive lifetimes are unionized */
1002 struct _lws_http_mode_related http;
1003 #ifdef LWS_USE_HTTP2
1004 struct _lws_http2_related http2;
1006 struct _lws_header_related hdr;
1007 struct _lws_websocket_related ws;
1010 /* lifetime members */
1012 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
1013 struct lws_io_watcher w_read;
1015 #if defined(LWS_USE_LIBEV)
1016 struct lws_io_watcher w_write;
1018 time_t pending_timeout_limit;
1022 struct lws_context *context;
1023 const struct lws_protocols *protocol;
1024 struct lws *timeout_list;
1025 struct lws **timeout_list_prev;
1027 /* rxflow handling */
1028 unsigned char *rxflow_buffer;
1029 /* truncated send handling */
1030 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
1031 #ifndef LWS_NO_EXTENSIONS
1032 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
1033 void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
1035 #ifdef LWS_OPENSSL_SUPPORT
1038 struct lws *pending_read_list_prev, *pending_read_list_next;
1041 unsigned long action_start;
1042 unsigned long latency_start;
1045 lws_sockfd_type sock;
1048 int position_in_fds_table;
1051 unsigned int trunc_alloc_len; /* size of malloc */
1052 unsigned int trunc_offset; /* where we are in terms of spilling */
1053 unsigned int trunc_len; /* how much is buffered */
1055 unsigned int hdr_parsing_completed:1;
1056 unsigned int user_space_externally_allocated:1;
1057 unsigned int socket_is_permanently_unusable:1;
1058 unsigned int rxflow_change_to:2;
1059 #ifndef LWS_NO_EXTENSIONS
1060 unsigned int extension_data_pending:1;
1062 #ifdef LWS_OPENSSL_SUPPORT
1063 unsigned int use_ssl:2;
1064 unsigned int upgraded:1;
1067 unsigned int sock_send_blocking:1;
1071 #ifndef LWS_NO_EXTENSIONS
1072 unsigned char count_act_ext;
1074 unsigned char ietf_spec_revision;
1075 char mode; /* enum connection_mode */
1076 char state; /* enum lws_connection_states */
1077 char state_pre_close;
1078 char lws_rx_parse_state; /* enum lws_rx_parse_state */
1079 char rx_frame_type; /* enum lws_write_protocol */
1080 char pending_timeout; /* enum pending_timeout */
1081 char pps; /* enum lws_pending_protocol_send */
1082 char tsi; /* thread service index we belong to */
1085 LWS_EXTERN int log_level;
1088 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1091 remove_wsi_socket_from_fds(struct lws *wsi);
1093 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1097 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1098 int ret, int completion) {
1100 (void)context; (void)wsi; (void)action; (void)ret;
1105 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1106 do { (void)context; (void)wsi; } while (0);
1109 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1111 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1112 int ret, int completion);
1116 lws_set_protocol_write_pending(struct lws *wsi,
1117 enum lws_pending_protocol_send pend);
1118 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1119 lws_client_rx_sm(struct lws *wsi, unsigned char c);
1121 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1122 lws_parse(struct lws *wsi, unsigned char c);
1124 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1125 lws_http_action(struct lws *wsi);
1128 lws_b64_selftest(void);
1131 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
1134 lws_service_flag_pending(struct lws_context *context, int tsi);
1136 #if defined(_WIN32) || defined(MBED_OPERATORS)
1137 LWS_EXTERN struct lws *
1138 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
1141 insert_wsi(struct lws_context *context, struct lws *wsi);
1144 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
1146 #define wsi_from_fd(A,B) A->lws_lookup[B]
1147 #define insert_wsi(A,B) assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
1148 #define delete_from_fd(A,B) A->lws_lookup[B]=0
1151 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1152 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1154 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1155 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1158 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1159 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1161 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1162 lws_client_connect_2(struct lws *wsi);
1164 LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1165 lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
1166 const char *path, const char *host);
1168 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1169 lws_create_new_server_wsi(struct lws_context *context);
1171 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1172 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1175 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1181 #ifndef LWS_NO_EXTENSIONS
1183 lws_context_init_extensions(struct lws_context_creation_info *info,
1184 struct lws_context *context);
1186 lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
1187 void *v, size_t len);
1190 lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
1192 lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1193 void *arg, int len);
1196 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1197 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1198 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1199 #define lws_issue_raw_ext_access lws_issue_raw
1200 #define lws_context_init_extensions(_a, _b)
1203 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1204 lws_client_interpret_server_handshake(struct lws *wsi);
1206 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1207 lws_rx_sm(struct lws *wsi, unsigned char c);
1209 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1210 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1213 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1215 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1216 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1217 enum lws_callback_reasons reason, void *user,
1218 void *in, size_t len);
1219 #ifdef LWS_USE_HTTP2
1220 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1221 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1223 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1224 unsigned char *buf, int len);
1225 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1227 lws_http2_parser(struct lws *wsi, unsigned char c);
1228 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1230 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1231 unsigned int sid, unsigned int len,
1232 unsigned char *buf);
1233 LWS_EXTERN struct lws *
1234 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1235 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1238 lws_add_http2_header_by_name(struct lws *wsi,
1239 const unsigned char *name,
1240 const unsigned char *value, int length,
1241 unsigned char **p, unsigned char *end);
1243 lws_add_http2_header_by_token(struct lws *wsi,
1244 enum lws_token_indexes token,
1245 const unsigned char *value, int length,
1246 unsigned char **p, unsigned char *end);
1248 lws_add_http2_header_status(struct lws *wsi,
1249 unsigned int code, unsigned char **p,
1250 unsigned char *end);
1252 void lws_http2_configure_if_upgraded(struct lws *wsi);
1254 #define lws_http2_configure_if_upgraded(x)
1258 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
1260 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1261 lws_header_table_attach(struct lws *wsi);
1264 lws_header_table_detach(struct lws *wsi);
1267 lws_header_table_reset(struct lws *wsi);
1269 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1270 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1272 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1273 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1276 lws_ensure_user_space(struct lws *wsi);
1279 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1281 #ifndef LWS_NO_SERVER
1282 int lws_context_init_server(struct lws_context_creation_info *info,
1283 struct lws_context *context);
1285 handshake_0405(struct lws_context *context, struct lws *wsi);
1286 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1287 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
1289 lws_server_get_canonical_hostname(struct lws_context *context,
1290 struct lws_context_creation_info *info);
1292 #define lws_context_init_server(_a, _b) (0)
1293 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1294 #define lws_server_get_canonical_hostname(_a, _b)
1297 #ifndef LWS_NO_DAEMONIZE
1298 LWS_EXTERN int get_daemonize_pid();
1300 #define get_daemonize_pid() (0)
1303 #if !defined(MBED_OPERATORS)
1304 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1305 interface_to_sa(struct lws_context *context, const char *ifname,
1306 struct sockaddr_in *addr, size_t addrlen);
1308 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1310 enum lws_ssl_capable_status {
1311 LWS_SSL_CAPABLE_ERROR = -1,
1312 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1315 #ifndef LWS_OPENSSL_SUPPORT
1316 #define LWS_SSL_ENABLED(context) (0)
1317 #define lws_context_init_server_ssl(_a, _b) (0)
1318 #define lws_ssl_destroy(_a)
1319 #define lws_context_init_http2_ssl(_a)
1320 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1321 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1322 #define lws_ssl_pending lws_ssl_pending_no_ssl
1323 #define lws_server_socket_service_ssl(_b, _c) (0)
1324 #define lws_ssl_close(_a) (0)
1325 #define lws_ssl_context_destroy(_a)
1326 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1328 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1329 LWS_EXTERN int openssl_websocket_private_data_index;
1330 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1331 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1332 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1333 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1334 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1335 lws_ssl_pending(struct lws *wsi);
1336 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1337 lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
1339 lws_ssl_close(struct lws *wsi);
1341 lws_ssl_context_destroy(struct lws_context *context);
1343 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1344 #ifndef LWS_NO_SERVER
1346 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1347 struct lws_context *context);
1349 #define lws_context_init_server_ssl(_a, _b) (0)
1352 lws_ssl_destroy(struct lws_context *context);
1356 #ifdef LWS_USE_HTTP2
1358 lws_context_init_http2_ssl(struct lws_context *context);
1360 #define lws_context_init_http2_ssl(_a)
1365 static LWS_INLINE void
1366 lws_pt_mutex_init(struct lws_context_per_thread *pt)
1368 pthread_mutex_init(&pt->lock, NULL);
1370 static LWS_INLINE void
1371 lws_pt_lock(struct lws_context_per_thread *pt)
1373 pthread_mutex_lock(&pt->lock);
1376 static LWS_INLINE void
1377 lws_pt_unlock(struct lws_context_per_thread *pt)
1379 pthread_mutex_unlock(&pt->lock);
1382 #define lws_pt_mutex_init(_a) (void)(_a)
1383 #define lws_pt_lock(_a) (void)(_a)
1384 #define lws_pt_unlock(_a) (void)(_a)
1387 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1388 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1390 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1391 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1393 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1394 lws_ssl_pending_no_ssl(struct lws *wsi);
1396 #ifndef LWS_NO_CLIENT
1397 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1399 struct lws_pollfd *pollfd);
1400 #ifdef LWS_OPENSSL_SUPPORT
1402 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1403 struct lws_context *context);
1405 #define lws_context_init_client_ssl(_a, _b) (0)
1407 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1408 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1410 lws_decode_ssl_error(void);
1412 #define lws_context_init_client_ssl(_a, _b) (0)
1413 #define lws_handshake_client(_a, _b, _c) (0)
1417 _lws_rx_flow_control(struct lws *wsi);
1420 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1422 #ifndef LWS_NO_SERVER
1424 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1425 struct lws_pollfd *pollfd);
1427 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1429 _lws_server_listen_accept_flow_control(struct lws *twsi, int on);
1431 #define lws_server_socket_service(_a, _b, _c) (0)
1432 #define lws_handshake_server(_a, _b, _c) (0)
1433 #define _lws_server_listen_accept_flow_control(a, b) (0)
1437 lws_get_addresses(struct lws_context *context, void *ads, char *name,
1438 int name_len, char *rip, int rip_len);
1444 lws_realloc(void *ptr, size_t size);
1446 LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
1447 lws_zalloc(size_t size);
1449 #define lws_malloc(S) lws_realloc(NULL, S)
1450 #define lws_free(P) lws_realloc(P, 0)
1451 #define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
1455 lws_plat_delete_socket_from_fds(struct lws_context *context,
1456 struct lws *wsi, int m);
1458 lws_plat_insert_socket_into_fds(struct lws_context *context,
1461 lws_plat_service_periodic(struct lws_context *context);
1464 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1465 struct lws_pollfd *pfd);
1467 lws_plat_context_early_init(void);
1469 lws_plat_context_early_destroy(struct lws_context *context);
1471 lws_plat_context_late_destroy(struct lws_context *context);
1473 lws_poll_listen_fd(struct lws_pollfd *fd);
1475 lws_plat_service(struct lws_context *context, int timeout_ms);
1476 LWS_EXTERN LWS_VISIBLE int
1477 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1479 lws_plat_init(struct lws_context *context,
1480 struct lws_context_creation_info *info);
1482 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1483 LWS_EXTERN unsigned long long
1484 time_in_microseconds(void);
1485 LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
1486 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1488 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1489 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);