2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2015 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>
38 #ifdef LWS_HAVE_SYS_STAT_H
42 #if defined(WIN32) || defined(_WIN32)
47 #define _WIN32_WINNT WINVER
49 #define LWS_NO_DAEMONIZE
50 #define LWS_ERRNO WSAGetLastError()
51 #define LWS_EAGAIN WSAEWOULDBLOCK
52 #define LWS_EALREADY WSAEALREADY
53 #define LWS_EINPROGRESS WSAEINPROGRESS
54 #define LWS_EINTR WSAEINTR
55 #define LWS_EISCONN WSAEISCONN
56 #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
57 #define LWS_POLLHUP (FD_CLOSE)
58 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
59 #define LWS_POLLOUT (FD_WRITE)
60 #define MSG_NOSIGNAL 0
61 #define SHUT_RDWR SD_BOTH
62 #define SOL_TCP IPPROTO_TCP
64 #define compatible_close(fd) closesocket(fd)
65 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
66 #define lws_socket_is_valid(x) (!!x)
67 #define LWS_SOCK_INVALID 0
72 #ifdef LWS_HAVE_IN6ADDR_H
78 #define __func__ __FUNCTION__
82 #define vsnprintf _vsnprintf
85 #else /* not windows --> */
90 #include <sys/types.h>
91 #ifndef MBED_OPERATORS
97 #include <sys/socket.h>
98 #ifdef LWS_BUILTIN_GETIFADDRS
99 #include <getifaddrs.h>
103 #if defined (__ANDROID__)
106 #include <sys/syslog.h>
109 #include <sys/socket.h>
111 #include <netinet/in.h>
112 #include <netinet/tcp.h>
113 #include <arpa/inet.h>
117 #endif /* LWS_USE_LIBEV */
118 #include <sys/mman.h>
123 #ifdef LWS_HAVE_SYS_PRCTL_H
124 #include <sys/prctl.h>
128 #include <sys/time.h>
130 #define LWS_ERRNO errno
131 #define LWS_EAGAIN EAGAIN
132 #define LWS_EALREADY EALREADY
133 #define LWS_EINPROGRESS EINPROGRESS
134 #define LWS_EINTR EINTR
135 #define LWS_EISCONN EISCONN
136 #define LWS_EWOULDBLOCK EWOULDBLOCK
137 #define LWS_POLLHUP (POLLHUP|POLLERR)
138 #define LWS_POLLIN (POLLIN)
139 #define LWS_POLLOUT (POLLOUT)
140 #define compatible_close(fd) close(fd)
141 #define lws_set_blocking_send(wsi)
143 #ifdef MBED_OPERATORS
144 #define lws_socket_is_valid(x) ((x) != NULL)
145 #define LWS_SOCK_INVALID (NULL)
147 #define lws_socket_is_valid(x) (x >= 0)
148 #define LWS_SOCK_INVALID (-1)
152 #ifndef LWS_HAVE_BZERO
154 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
158 #ifndef LWS_HAVE_STRERROR
159 #define strerror(x) ""
162 #ifdef LWS_OPENSSL_SUPPORT
164 #ifdef USE_OLD_CYASSL
165 #include <cyassl/openssl/ssl.h>
166 #include <cyassl/error-ssl.h>
168 #include <wolfssl/openssl/ssl.h>
169 #include <wolfssl/error-ssl.h>
170 #endif /* not USE_OLD_CYASSL */
172 #include <openssl/ssl.h>
173 #include <openssl/evp.h>
174 #include <openssl/err.h>
175 #include <openssl/md5.h>
176 #include <openssl/sha.h>
177 #endif /* not USE_WOLFSSL */
180 #include "libwebsockets.h"
182 #if defined(MBED_OPERATORS)
183 #undef compatible_close
184 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
186 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
188 #ifndef LITTLE_ENDIAN
189 #define LITTLE_ENDIAN 1234
192 #define BYTE_ORDER LITTLE_ENDIAN
196 #if defined(WIN32) || defined(_WIN32)
199 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
201 #ifndef LITTLE_ENDIAN
202 #define LITTLE_ENDIAN 1234
205 #define BYTE_ORDER LITTLE_ENDIAN
208 typedef unsigned __int64 u_int64_t;
214 #define __P(protos) protos
216 #define __P(protos) ()
222 #include <sys/stat.h>
223 #include <sys/cdefs.h>
224 #include <sys/time.h>
226 #if defined(__APPLE__)
227 #include <machine/endian.h>
228 #elif defined(__FreeBSD__)
229 #include <sys/endian.h>
230 #elif defined(__linux__)
240 #define container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
244 #include <gulliver.h>
245 #if defined(__LITTLEENDIAN__)
246 #define BYTE_ORDER __LITTLEENDIAN__
247 #define LITTLE_ENDIAN __LITTLEENDIAN__
248 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
250 #if defined(__BIGENDIAN__)
251 #define BYTE_ORDER __BIGENDIAN__
252 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
253 #define BIG_ENDIAN __BIGENDIAN__
257 #if !defined(BYTE_ORDER)
258 # define BYTE_ORDER __BYTE_ORDER
260 #if !defined(LITTLE_ENDIAN)
261 # define LITTLE_ENDIAN __LITTLE_ENDIAN
263 #if !defined(BIG_ENDIAN)
264 # define BIG_ENDIAN __BIG_ENDIAN
270 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
271 * but happily have something equivalent in the SO_NOSIGPIPE flag.
274 #define MSG_NOSIGNAL SO_NOSIGPIPE
278 #ifndef FD_HASHTABLE_MODULUS
279 #define FD_HASHTABLE_MODULUS 32
283 #ifndef LWS_MAX_HEADER_LEN
284 #define LWS_MAX_HEADER_LEN 1024
286 #ifndef LWS_MAX_HEADER_POOL
287 #define LWS_MAX_HEADER_POOL 16
289 #ifndef LWS_MAX_PROTOCOLS
290 #define LWS_MAX_PROTOCOLS 5
292 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
293 #define LWS_MAX_EXTENSIONS_ACTIVE 2
295 #ifndef LWS_MAX_EXT_OFFERS
296 #define LWS_MAX_EXT_OFFERS 8
298 #ifndef SPEC_LATEST_SUPPORTED
299 #define SPEC_LATEST_SUPPORTED 13
301 #ifndef AWAITING_TIMEOUT
302 #define AWAITING_TIMEOUT 5
304 #ifndef CIPHERS_LIST_STRING
305 #define CIPHERS_LIST_STRING "DEFAULT"
307 #ifndef LWS_SOMAXCONN
308 #define LWS_SOMAXCONN SOMAXCONN
311 #define MAX_WEBSOCKET_04_KEY_LEN 128
312 #define LWS_MAX_SOCKET_IO_BUF 4096
314 #ifndef SYSTEM_RANDOM_FILEPATH
315 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
319 * if not in a connection storm, check for incoming
320 * connections this many normal connection services
322 #define LWS_lserv_mod 10
324 enum lws_websocket_opcodes_07 {
325 LWSWSOPC_CONTINUATION = 0,
326 LWSWSOPC_TEXT_FRAME = 1,
327 LWSWSOPC_BINARY_FRAME = 2,
329 LWSWSOPC_NOSPEC__MUX = 7,
331 /* control extensions 8+ */
339 enum lws_connection_states {
341 LWSS_HTTP_ISSUING_FILE,
346 LWSS_CLIENT_UNCONNECTED,
347 LWSS_RETURNED_CLOSE_ALREADY,
348 LWSS_AWAITING_CLOSE_ACK,
349 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
351 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
352 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
353 LWSS_HTTP2_ESTABLISHED,
361 enum http_connection_type {
362 HTTP_CONNECTION_CLOSE,
363 HTTP_CONNECTION_KEEP_ALIVE
366 enum lws_pending_protocol_send {
368 LWS_PPS_HTTP2_MY_SETTINGS,
369 LWS_PPS_HTTP2_ACK_SETTINGS,
373 enum lws_rx_parse_state {
380 LWS_RXPS_04_FRAME_HDR_1,
381 LWS_RXPS_04_FRAME_HDR_LEN,
382 LWS_RXPS_04_FRAME_HDR_LEN16_2,
383 LWS_RXPS_04_FRAME_HDR_LEN16_1,
384 LWS_RXPS_04_FRAME_HDR_LEN64_8,
385 LWS_RXPS_04_FRAME_HDR_LEN64_7,
386 LWS_RXPS_04_FRAME_HDR_LEN64_6,
387 LWS_RXPS_04_FRAME_HDR_LEN64_5,
388 LWS_RXPS_04_FRAME_HDR_LEN64_4,
389 LWS_RXPS_04_FRAME_HDR_LEN64_3,
390 LWS_RXPS_04_FRAME_HDR_LEN64_2,
391 LWS_RXPS_04_FRAME_HDR_LEN64_1,
393 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
394 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
395 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
396 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
398 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
402 enum connection_mode {
404 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
405 LWSCM_PRE_WS_SERVING_ACCEPT,
412 /* transient, ssl delay hiding */
413 LWSCM_SSL_ACK_PENDING,
415 /* transient modes */
416 LWSCM_WSCL_WAITING_CONNECT,
417 LWSCM_WSCL_WAITING_PROXY_REPLY,
418 LWSCM_WSCL_ISSUE_HANDSHAKE,
419 LWSCM_WSCL_ISSUE_HANDSHAKE2,
420 LWSCM_WSCL_WAITING_SSL,
421 LWSCM_WSCL_WAITING_SERVER_REPLY,
422 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
423 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
425 /* special internal types */
426 LWSCM_SERVER_LISTENER,
430 LWS_RXFLOW_ALLOW = (1 << 0),
431 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
434 /* this is not usable directly by user code any more, lws_close_reason() */
435 #define LWS_WRITE_CLOSE 4
437 struct lws_protocols;
441 struct lws_io_watcher {
442 struct ev_io watcher;
443 struct lws_context* context;
446 struct lws_signal_watcher {
447 struct ev_signal watcher;
448 struct lws_context* context;
450 #endif /* LWS_USE_LIBEV */
453 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
454 struct lws_fd_hashtable {
461 * This is totally opaque to code using the library. It's exported as a
462 * forward-reference pointer-only declaration; the user can use the pointer with
463 * other APIs to get information out of it.
466 struct lws_fragments {
467 unsigned short offset;
469 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
473 * these are assigned from a pool held in the context.
474 * Both client and server mode uses them for http header analysis
477 struct allocated_headers {
478 char *data; /* prepared by context init to point to dedicated storage */
480 * the randomly ordered fragments, indexed by frag_index and
481 * lws_fragments->nfrag for continuation.
483 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
485 * for each recognized token, frag_index says which frag[] his data
486 * starts in (0 means the token did not appear)
487 * the actual header data gets dumped as it comes in, into data[]
489 unsigned char frag_index[WSI_TOKEN_COUNT];
490 #ifndef LWS_NO_CLIENT
491 char initial_handshake_hash_base64[30];
492 unsigned short c_port;
496 unsigned char in_use;
501 * so we can have n connections being serviced simultaneously,
502 * these things need to be isolated per-thread.
505 struct lws_context_per_thread {
506 struct lws_pollfd *fds;
507 struct lws *rx_draining_ext_list;
508 struct lws *tx_draining_ext_list;
509 #ifdef LWS_OPENSSL_SUPPORT
510 struct lws *pending_read_list; /* linked list */
513 * usable by anything in the service code, but only if the scope
514 * does not last longer than the service action (since next service
515 * of any socket can likewise use it and overwrite)
517 unsigned char *serv_buf;
521 int dummy_pipe_fds[2];
527 * the rest is managed per-context, that includes
529 * - processwide single fd -> wsi lookup
530 * - contextwide headers pool
531 * - contextwide ssl context
532 * - contextwide proxy
536 time_t last_timeout_check_s;
537 struct lws_plat_file_ops fops;
538 struct lws_context_per_thread pt[LWS_MAX_SMP];
540 /* different implementation between unix and windows */
541 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
543 struct lws **lws_lookup; /* fd to wsi */
546 struct ev_loop* io_loop;
547 struct lws_io_watcher w_accept;
548 struct lws_signal_watcher w_sigint;
549 lws_ev_signal_cb* lws_ev_sigint_cb;
550 #endif /* LWS_USE_LIBEV */
552 const struct lws_token_limits *token_limits;
554 struct lws *timeout_list;
556 #ifndef LWS_NO_SERVER
557 struct lws *wsi_listening;
559 const struct lws_protocols *protocols;
560 void *http_header_data;
561 struct allocated_headers *ah_pool;
563 #ifdef LWS_OPENSSL_SUPPORT
565 SSL_CTX *ssl_client_ctx;
567 #ifndef LWS_NO_EXTENSIONS
568 const struct lws_extension *extensions;
571 char http_proxy_address[128];
572 char proxy_basic_auth_token[128];
573 char canonical_hostname[128];
575 unsigned long worst_latency;
576 char worst_latency_info[256];
579 lws_sockfd_type lserv_fd;
586 int started_with_parent;
592 unsigned int http_proxy_port;
593 unsigned int options;
594 unsigned int fd_limit_per_thread;
597 * set to the Thread ID that's doing the service loop just before entry
598 * to poll indicates service thread likely idling in poll()
599 * volatile because other threads may check it as part of processing
600 * for pollfd event change.
602 volatile int service_tid;
603 int service_tid_detected;
610 #ifdef LWS_OPENSSL_SUPPORT
612 int allow_non_ssl_on_ssl_port;
613 unsigned int user_supplied_ssl_ctx:1;
614 #define lws_ssl_anybody_has_buffered_read(w) \
615 (w->context->use_ssl && \
616 w->context->pt[(int)w->tsi].pending_read_list)
617 #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
619 c->pt[(int)t].pending_read_list)
621 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
622 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
625 short max_http_header_data;
626 short max_http_header_pool;
627 short ah_count_in_use;
630 unsigned int being_destroyed:1;
634 LWS_EV_READ = (1 << 0),
635 LWS_EV_WRITE = (1 << 1),
636 LWS_EV_START = (1 << 2),
637 LWS_EV_STOP = (1 << 3),
641 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
642 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
644 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
646 lws_libev_io(struct lws *wsi, int flags);
648 lws_libev_init_fd_table(struct lws_context *context);
650 lws_libev_run(const struct lws_context *context);
652 #define LWS_LIBEV_ENABLED(context) (0)
654 #define lws_feature_status_libev(_a) \
655 lwsl_notice("libev support not compiled in\n")
657 #define lws_feature_status_libev(_a)
659 #define lws_libev_accept(_a, _b) ((void) 0)
660 #define lws_libev_io(_a, _b) ((void) 0)
661 #define lws_libev_init_fd_table(_a) (0)
662 #define lws_libev_run(_a) ((void) 0)
666 #define LWS_IPV6_ENABLED(context) \
667 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
669 #define LWS_IPV6_ENABLED(context) (0)
672 enum uri_path_states {
675 URIPS_SEEN_SLASH_DOT,
676 URIPS_SEEN_SLASH_DOT_DOT,
679 enum uri_esc_states {
682 URIES_SEEN_PERCENT_H1,
685 /* notice that these union members:
691 * all have a pointer to allocated_headers struct as their first member.
693 * It means for allocated_headers access, the three union paths can all be
694 * used interchangeably to access the same data
697 struct _lws_http_mode_related {
698 /* MUST be first in struct */
699 struct allocated_headers *ah; /* mirroring _lws_header_related */
700 unsigned long filepos;
701 unsigned long filelen;
704 enum http_version request_version;
705 enum http_connection_type connection_type;
706 unsigned int content_length;
707 unsigned int content_remain;
712 enum lws_http2_settings {
713 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
714 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
715 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
716 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
717 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
718 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
720 LWS_HTTP2_SETTINGS__COUNT /* always last */
723 enum lws_http2_wellknown_frame_types {
724 LWS_HTTP2_FRAME_TYPE_DATA,
725 LWS_HTTP2_FRAME_TYPE_HEADERS,
726 LWS_HTTP2_FRAME_TYPE_PRIORITY,
727 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
728 LWS_HTTP2_FRAME_TYPE_SETTINGS,
729 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
730 LWS_HTTP2_FRAME_TYPE_PING,
731 LWS_HTTP2_FRAME_TYPE_GOAWAY,
732 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
733 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
735 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
738 enum lws_http2_flags {
739 LWS_HTTP2_FLAG_END_STREAM = 1,
740 LWS_HTTP2_FLAG_END_HEADERS = 4,
741 LWS_HTTP2_FLAG_PADDED = 8,
742 LWS_HTTP2_FLAG_PRIORITY = 0x20,
744 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
747 #define LWS_HTTP2_STREAM_ID_MASTER 0
748 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
749 #define LWS_HTTP2_SETTINGS_LENGTH 6
751 struct http2_settings {
752 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
755 enum http2_hpack_state {
757 /* optional before first header block */
759 HKPS_OPT_E_DEPENDENCY,
772 /* optional after last header block */
773 HKPS_OPT_DISCARD_PADDING,
776 enum http2_hpack_type {
778 HPKT_INDEXED_HDR_6_VALUE_INCR,
779 HPKT_LITERAL_HDR_VALUE_INCR,
780 HPKT_INDEXED_HDR_4_VALUE,
781 HPKT_LITERAL_HDR_VALUE,
785 struct hpack_dt_entry {
786 int token; /* additions that don't map to a token are ignored */
791 struct hpack_dynamic_table {
792 struct hpack_dt_entry *entries;
800 struct _lws_http2_related {
802 * having this first lets us also re-use all HTTP union code
803 * and in turn, http_mode_related has allocated headers in right
804 * place so we can use the header apis on the wsi directly still
806 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
808 struct http2_settings my_settings;
809 struct http2_settings peer_settings;
811 struct lws *parent_wsi;
812 struct lws *next_child_wsi;
814 struct hpack_dynamic_table *hpack_dyn_table;
815 struct lws *stream_wsi;
816 unsigned char ping_payload[8];
817 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
821 unsigned int stream_id;
822 enum http2_hpack_state hpack;
823 enum http2_hpack_type hpack_type;
824 unsigned int header_index;
825 unsigned int hpack_len;
826 unsigned int hpack_e_dep;
828 unsigned int my_stream_id;
829 unsigned int child_count;
832 unsigned int END_STREAM:1;
833 unsigned int END_HEADERS:1;
834 unsigned int send_END_STREAM:1;
835 unsigned int GOING_AWAY;
836 unsigned int requested_POLLOUT:1;
837 unsigned int waiting_tx_credit:1;
839 unsigned int value:1;
841 unsigned short round_robin_POLLOUT;
842 unsigned short count_POLLOUT_children;
843 unsigned short hpack_pos;
847 unsigned char frame_state;
848 unsigned char padding;
849 unsigned char hpack_m;
850 unsigned char initialized;
853 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
857 struct _lws_header_related {
858 /* MUST be first in struct */
859 struct allocated_headers *ah;
860 enum uri_path_states ups;
861 enum uri_esc_states ues;
863 unsigned short current_token_limit;
865 char post_literal_equal;
866 unsigned char parser_state; /* enum lws_token_indexes */
870 struct _lws_websocket_related {
872 unsigned int rx_ubuf_alloc;
873 struct lws *rx_draining_ext_list;
874 struct lws *tx_draining_ext_list;
875 size_t rx_packet_length;
876 unsigned int rx_ubuf_head;
877 unsigned char mask[4];
878 /* Also used for close content... control opcode == < 128 */
879 unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
881 unsigned char ping_payload_len;
882 unsigned char mask_idx;
883 unsigned char opcode;
885 unsigned char rsv_first_msg;
886 /* zero if no info, or length including 2-byte close code */
887 unsigned char close_in_ping_buffer_len;
889 unsigned char stashed_write_type;
890 unsigned char tx_draining_stashed_wp;
892 unsigned int final:1;
893 unsigned int frame_is_binary:1;
894 unsigned int all_zero_nonce:1;
895 unsigned int this_frame_masked:1;
896 unsigned int inside_frame:1; /* next write will be more of frame */
897 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
898 unsigned int payload_is_close:1; /* process as PONG, but it is close */
899 unsigned int ping_pending_flag:1;
900 unsigned int continuation_possible:1;
901 unsigned int owed_a_fin:1;
902 unsigned int check_utf8:1;
903 unsigned int defeat_check_utf8:1;
904 unsigned int pmce_compressed_message:1;
905 unsigned int stashed_write_pending:1;
906 unsigned int rx_draining_ext:1;
907 unsigned int tx_draining_ext:1;
913 /* members with mutually exclusive lifetimes are unionized */
916 struct _lws_http_mode_related http;
918 struct _lws_http2_related http2;
920 struct _lws_header_related hdr;
921 struct _lws_websocket_related ws;
924 /* lifetime members */
927 struct lws_io_watcher w_read;
928 struct lws_io_watcher w_write;
929 #endif /* LWS_USE_LIBEV */
930 time_t pending_timeout_limit;
934 struct lws_context *context;
935 const struct lws_protocols *protocol;
936 struct lws *timeout_list;
937 struct lws **timeout_list_prev;
939 /* rxflow handling */
940 unsigned char *rxflow_buffer;
941 /* truncated send handling */
942 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
943 #ifndef LWS_NO_EXTENSIONS
944 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
945 void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
947 #ifdef LWS_OPENSSL_SUPPORT
950 struct lws *pending_read_list_prev, *pending_read_list_next;
953 unsigned long action_start;
954 unsigned long latency_start;
957 lws_sockfd_type sock;
960 int position_in_fds_table;
963 unsigned int trunc_alloc_len; /* size of malloc */
964 unsigned int trunc_offset; /* where we are in terms of spilling */
965 unsigned int trunc_len; /* how much is buffered */
967 unsigned int hdr_parsing_completed:1;
968 unsigned int user_space_externally_allocated:1;
969 unsigned int socket_is_permanently_unusable:1;
970 unsigned int rxflow_change_to:2;
971 #ifndef LWS_NO_EXTENSIONS
972 unsigned int extension_data_pending:1;
974 #ifdef LWS_OPENSSL_SUPPORT
975 unsigned int use_ssl:2;
976 unsigned int upgraded:1;
979 unsigned int sock_send_blocking:1;
983 #ifndef LWS_NO_EXTENSIONS
984 unsigned char count_act_ext;
986 unsigned char ietf_spec_revision;
987 char mode; /* enum connection_mode */
988 char state; /* enum lws_connection_states */
989 char lws_rx_parse_state; /* enum lws_rx_parse_state */
990 char rx_frame_type; /* enum lws_write_protocol */
991 char pending_timeout; /* enum pending_timeout */
992 char pps; /* enum lws_pending_protocol_send */
993 char tsi; /* thread service index we belong to */
996 LWS_EXTERN int log_level;
999 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1002 remove_wsi_socket_from_fds(struct lws *wsi);
1004 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1008 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1009 int ret, int completion) {
1011 (void)context; (void)wsi; (void)action; (void)ret;
1016 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1017 do { (void)context; (void)wsi; } while (0);
1020 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1022 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1023 int ret, int completion);
1027 lws_set_protocol_write_pending(struct lws *wsi,
1028 enum lws_pending_protocol_send pend);
1030 lws_client_rx_sm(struct lws *wsi, unsigned char c);
1033 lws_parse(struct lws *wsi, unsigned char c);
1036 lws_http_action(struct lws *wsi);
1039 lws_b64_selftest(void);
1041 #if defined(_WIN32) || defined(MBED_OPERATORS)
1042 LWS_EXTERN struct lws *
1043 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
1046 insert_wsi(struct lws_context *context, struct lws *wsi);
1049 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
1051 #define wsi_from_fd(A,B) A->lws_lookup[B]
1052 #define insert_wsi(A,B) A->lws_lookup[B->sock]=B
1053 #define delete_from_fd(A,B) A->lws_lookup[B]=0
1057 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1060 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1064 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1066 LWS_EXTERN struct lws *
1067 lws_client_connect_2(struct lws *wsi);
1069 LWS_VISIBLE struct lws *
1070 lws_client_reset(struct lws *wsi, int ssl, const char *address, int port, const char *path, const char *host);
1073 LWS_EXTERN struct lws *
1074 lws_create_new_server_wsi(struct lws_context *context);
1077 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1080 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1086 #ifndef LWS_NO_EXTENSIONS
1088 lws_context_init_extensions(struct lws_context_creation_info *info,
1089 struct lws_context *context);
1091 lws_any_extension_handled(struct lws *wsi,
1092 enum lws_extension_callback_reasons r,
1093 void *v, size_t len);
1096 lws_ext_cb_active(struct lws *wsi, int reason,
1097 void *buf, int len);
1099 lws_ext_cb_all_exts(struct lws_context *context,
1100 struct lws *wsi, int reason,
1101 void *arg, int len);
1104 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1105 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1106 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1107 #define lws_issue_raw_ext_access lws_issue_raw
1108 #define lws_context_init_extensions(_a, _b)
1112 lws_client_interpret_server_handshake(struct lws *wsi);
1115 lws_rx_sm(struct lws *wsi, unsigned char c);
1118 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1121 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1124 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1125 enum lws_callback_reasons reason, void *user,
1126 void *in, size_t len);
1127 #ifdef LWS_USE_HTTP2
1128 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1129 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1131 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1132 unsigned char *buf, int len);
1133 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1135 lws_http2_parser(struct lws *wsi, unsigned char c);
1136 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1138 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1139 unsigned int sid, unsigned int len,
1140 unsigned char *buf);
1141 LWS_EXTERN struct lws *
1142 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1143 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1146 lws_add_http2_header_by_name(struct lws *wsi,
1147 const unsigned char *name,
1148 const unsigned char *value, int length,
1149 unsigned char **p, unsigned char *end);
1151 lws_add_http2_header_by_token(struct lws *wsi,
1152 enum lws_token_indexes token,
1153 const unsigned char *value, int length,
1154 unsigned char **p, unsigned char *end);
1156 lws_add_http2_header_status(struct lws *wsi,
1157 unsigned int code, unsigned char **p,
1158 unsigned char *end);
1160 void lws_http2_configure_if_upgraded(struct lws *wsi);
1162 #define lws_http2_configure_if_upgraded(x)
1166 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
1169 lws_allocate_header_table(struct lws *wsi);
1172 lws_free_header_table(struct lws *wsi);
1175 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1178 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1181 lws_ensure_user_space(struct lws *wsi);
1184 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1186 #ifndef LWS_NO_SERVER
1187 int lws_context_init_server(struct lws_context_creation_info *info,
1188 struct lws_context *context);
1190 handshake_0405(struct lws_context *context, struct lws *wsi);
1192 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
1194 lws_server_get_canonical_hostname(struct lws_context *context,
1195 struct lws_context_creation_info *info);
1197 #define lws_context_init_server(_a, _b) (0)
1198 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1199 #define lws_server_get_canonical_hostname(_a, _b)
1202 #ifndef LWS_NO_DAEMONIZE
1203 LWS_EXTERN int get_daemonize_pid();
1205 #define get_daemonize_pid() (0)
1208 #if !defined(MBED_OPERATORS)
1210 interface_to_sa(struct lws_context *context, const char *ifname,
1211 struct sockaddr_in *addr, size_t addrlen);
1213 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1215 enum lws_ssl_capable_status {
1216 LWS_SSL_CAPABLE_ERROR = -1,
1217 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1220 #ifndef LWS_OPENSSL_SUPPORT
1221 #define LWS_SSL_ENABLED(context) (0)
1222 #define lws_context_init_server_ssl(_a, _b) (0)
1223 #define lws_ssl_destroy(_a)
1224 #define lws_context_init_http2_ssl(_a)
1225 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1226 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1227 #define lws_ssl_pending lws_ssl_pending_no_ssl
1228 #define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
1229 #define lws_ssl_close(_a) (0)
1230 #define lws_ssl_context_destroy(_a)
1231 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1233 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1234 LWS_EXTERN int openssl_websocket_private_data_index;
1236 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1238 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1240 lws_ssl_pending(struct lws *wsi);
1242 lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
1243 lws_sockfd_type accept_fd,
1244 struct lws_pollfd *pollfd);
1246 lws_ssl_close(struct lws *wsi);
1248 lws_ssl_context_destroy(struct lws_context *context);
1250 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1251 #ifndef LWS_NO_SERVER
1253 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1254 struct lws_context *context);
1256 #define lws_context_init_server_ssl(_a, _b) (0)
1259 lws_ssl_destroy(struct lws_context *context);
1263 #ifdef LWS_USE_HTTP2
1265 lws_context_init_http2_ssl(struct lws_context *context);
1267 #define lws_context_init_http2_ssl(_a)
1272 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1275 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1278 lws_ssl_pending_no_ssl(struct lws *wsi);
1280 #ifndef LWS_NO_CLIENT
1281 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1283 struct lws_pollfd *pollfd);
1284 #ifdef LWS_OPENSSL_SUPPORT
1286 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1287 struct lws_context *context);
1289 #define lws_context_init_client_ssl(_a, _b) (0)
1292 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1294 lws_decode_ssl_error(void);
1296 #define lws_context_init_client_ssl(_a, _b) (0)
1297 #define lws_handshake_client(_a, _b, _c) (0)
1301 _lws_rx_flow_control(struct lws *wsi);
1303 #ifndef LWS_NO_SERVER
1305 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1306 struct lws_pollfd *pollfd);
1308 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1310 _lws_server_listen_accept_flow_control(struct lws_context *context, int on);
1312 #define lws_server_socket_service(_a, _b, _c) (0)
1313 #define lws_handshake_server(_a, _b, _c) (0)
1314 #define _lws_server_listen_accept_flow_control(a, b) (0)
1318 lws_get_addresses(struct lws_context *context, void *ads, char *name,
1319 int name_len, char *rip, int rip_len);
1325 lws_realloc(void *ptr, size_t size);
1328 lws_zalloc(size_t size);
1330 #define lws_malloc(S) lws_realloc(NULL, S)
1331 #define lws_free(P) lws_realloc(P, 0)
1332 #define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
1336 lws_plat_delete_socket_from_fds(struct lws_context *context,
1337 struct lws *wsi, int m);
1339 lws_plat_insert_socket_into_fds(struct lws_context *context,
1342 lws_plat_service_periodic(struct lws_context *context);
1345 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1346 struct lws_pollfd *pfd);
1348 lws_plat_context_early_init(void);
1350 lws_plat_context_early_destroy(struct lws_context *context);
1352 lws_plat_context_late_destroy(struct lws_context *context);
1354 lws_poll_listen_fd(struct lws_pollfd *fd);
1356 lws_plat_service(struct lws_context *context, int timeout_ms);
1357 LWS_EXTERN LWS_VISIBLE int
1358 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1360 lws_plat_init(struct lws_context *context,
1361 struct lws_context_creation_info *info);
1363 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1364 LWS_EXTERN unsigned long long
1365 time_in_microseconds(void);
1366 LWS_EXTERN const char *
1367 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1370 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);