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 = TRUE
66 #define lws_socket_is_valid(x) (!!x)
67 #define LWS_SOCK_INVALID 0
71 #ifdef LWS_HAVE_IN6ADDR_H
77 #define __func__ __FUNCTION__
81 #define vsnprintf _vsnprintf
84 #else /* not windows --> */
89 #include <sys/types.h>
90 #ifndef MBED_OPERATORS
96 #include <sys/socket.h>
97 #ifdef LWS_BUILTIN_GETIFADDRS
98 #include <getifaddrs.h>
102 #if defined (__ANDROID__)
105 #include <sys/syslog.h>
108 #include <sys/socket.h>
110 #include <netinet/in.h>
111 #include <netinet/tcp.h>
112 #include <arpa/inet.h>
116 #endif /* LWS_USE_LIBEV */
117 #include <sys/mman.h>
122 #ifdef LWS_HAVE_SYS_PRCTL_H
123 #include <sys/prctl.h>
127 #include <sys/time.h>
129 #define LWS_ERRNO errno
130 #define LWS_EAGAIN EAGAIN
131 #define LWS_EALREADY EALREADY
132 #define LWS_EINPROGRESS EINPROGRESS
133 #define LWS_EINTR EINTR
134 #define LWS_EISCONN EISCONN
135 #define LWS_EWOULDBLOCK EWOULDBLOCK
136 #define LWS_POLLHUP (POLLHUP|POLLERR)
137 #define LWS_POLLIN (POLLIN)
138 #define LWS_POLLOUT (POLLOUT)
139 #define compatible_close(fd) close(fd)
140 #define lws_set_blocking_send(wsi)
142 #ifdef MBED_OPERATORS
143 #define lws_socket_is_valid(x) ((x) != NULL)
144 #define LWS_SOCK_INVALID (NULL)
146 #define lws_socket_is_valid(x) (x >= 0)
147 #define LWS_SOCK_INVALID (-1)
151 #ifndef LWS_HAVE_BZERO
153 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
157 #ifndef LWS_HAVE_STRERROR
158 #define strerror(x) ""
161 #ifdef LWS_OPENSSL_SUPPORT
163 #ifdef USE_OLD_CYASSL
164 #include <cyassl/openssl/ssl.h>
165 #include <cyassl/error-ssl.h>
167 #include <wolfssl/openssl/ssl.h>
168 #include <wolfssl/error-ssl.h>
169 #endif /* not USE_OLD_CYASSL */
171 #include <openssl/ssl.h>
172 #include <openssl/evp.h>
173 #include <openssl/err.h>
174 #include <openssl/md5.h>
175 #include <openssl/sha.h>
176 #endif /* not USE_WOLFSSL */
179 #include "libwebsockets.h"
181 #if defined(MBED_OPERATORS)
182 #undef compatible_close
183 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
185 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
187 #ifndef LITTLE_ENDIAN
188 #define LITTLE_ENDIAN 1234
191 #define BYTE_ORDER LITTLE_ENDIAN
195 #if defined(WIN32) || defined(_WIN32)
198 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
200 #ifndef LITTLE_ENDIAN
201 #define LITTLE_ENDIAN 1234
204 #define BYTE_ORDER LITTLE_ENDIAN
207 typedef unsigned __int64 u_int64_t;
213 #define __P(protos) protos
215 #define __P(protos) ()
221 #include <sys/stat.h>
222 #include <sys/cdefs.h>
223 #include <sys/time.h>
225 #if defined(__APPLE__)
226 #include <machine/endian.h>
227 #elif defined(__FreeBSD__)
228 #include <sys/endian.h>
229 #elif defined(__linux__)
239 #define container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
243 #include <gulliver.h>
244 #if defined(__LITTLEENDIAN__)
245 #define BYTE_ORDER __LITTLEENDIAN__
246 #define LITTLE_ENDIAN __LITTLEENDIAN__
247 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
249 #if defined(__BIGENDIAN__)
250 #define BYTE_ORDER __BIGENDIAN__
251 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
252 #define BIG_ENDIAN __BIGENDIAN__
256 #if !defined(BYTE_ORDER)
257 # define BYTE_ORDER __BYTE_ORDER
259 #if !defined(LITTLE_ENDIAN)
260 # define LITTLE_ENDIAN __LITTLE_ENDIAN
262 #if !defined(BIG_ENDIAN)
263 # define BIG_ENDIAN __BIG_ENDIAN
269 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
270 * but happily have something equivalent in the SO_NOSIGPIPE flag.
273 #define MSG_NOSIGNAL SO_NOSIGPIPE
277 #ifndef FD_HASHTABLE_MODULUS
278 #define FD_HASHTABLE_MODULUS 32
282 #ifndef LWS_MAX_HEADER_LEN
283 #define LWS_MAX_HEADER_LEN 1024
285 #ifndef LWS_MAX_PROTOCOLS
286 #define LWS_MAX_PROTOCOLS 5
288 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
289 #define LWS_MAX_EXTENSIONS_ACTIVE 3
291 #ifndef SPEC_LATEST_SUPPORTED
292 #define SPEC_LATEST_SUPPORTED 13
294 #ifndef AWAITING_TIMEOUT
295 #define AWAITING_TIMEOUT 5
297 #ifndef CIPHERS_LIST_STRING
298 #define CIPHERS_LIST_STRING "DEFAULT"
300 #ifndef LWS_SOMAXCONN
301 #define LWS_SOMAXCONN SOMAXCONN
304 #define MAX_WEBSOCKET_04_KEY_LEN 128
305 #define LWS_MAX_SOCKET_IO_BUF 4096
307 #ifndef SYSTEM_RANDOM_FILEPATH
308 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
310 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
311 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
315 * if not in a connection storm, check for incoming
316 * connections this many normal connection services
318 #define LWS_LISTEN_SERVICE_MODULO 10
320 enum lws_websocket_opcodes_07 {
321 LWS_WS_OPCODE_07__CONTINUATION = 0,
322 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
323 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
325 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
327 /* control extensions 8+ */
329 LWS_WS_OPCODE_07__CLOSE = 8,
330 LWS_WS_OPCODE_07__PING = 9,
331 LWS_WS_OPCODE_07__PONG = 0xa,
335 enum lws_connection_states {
337 WSI_STATE_HTTP_ISSUING_FILE,
338 WSI_STATE_HTTP_HEADERS,
340 WSI_STATE_DEAD_SOCKET,
341 WSI_STATE_ESTABLISHED,
342 WSI_STATE_CLIENT_UNCONNECTED,
343 WSI_STATE_RETURNED_CLOSE_ALREADY,
344 WSI_STATE_AWAITING_CLOSE_ACK,
345 WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE,
347 WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE,
348 WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS,
349 WSI_STATE_HTTP2_ESTABLISHED,
357 enum http_connection_type {
358 HTTP_CONNECTION_CLOSE,
359 HTTP_CONNECTION_KEEP_ALIVE
362 enum lws_pending_protocol_send {
364 LWS_PPS_HTTP2_MY_SETTINGS,
365 LWS_PPS_HTTP2_ACK_SETTINGS,
369 enum lws_rx_parse_state {
372 LWS_RXPS_04_MASK_NONCE_1,
373 LWS_RXPS_04_MASK_NONCE_2,
374 LWS_RXPS_04_MASK_NONCE_3,
376 LWS_RXPS_04_FRAME_HDR_1,
377 LWS_RXPS_04_FRAME_HDR_LEN,
378 LWS_RXPS_04_FRAME_HDR_LEN16_2,
379 LWS_RXPS_04_FRAME_HDR_LEN16_1,
380 LWS_RXPS_04_FRAME_HDR_LEN64_8,
381 LWS_RXPS_04_FRAME_HDR_LEN64_7,
382 LWS_RXPS_04_FRAME_HDR_LEN64_6,
383 LWS_RXPS_04_FRAME_HDR_LEN64_5,
384 LWS_RXPS_04_FRAME_HDR_LEN64_4,
385 LWS_RXPS_04_FRAME_HDR_LEN64_3,
386 LWS_RXPS_04_FRAME_HDR_LEN64_2,
387 LWS_RXPS_04_FRAME_HDR_LEN64_1,
389 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
390 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
391 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
392 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
394 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
398 enum connection_mode {
399 LWS_CONNMODE_HTTP_SERVING,
400 LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
401 LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
403 LWS_CONNMODE_WS_SERVING,
404 LWS_CONNMODE_WS_CLIENT,
406 LWS_CONNMODE_HTTP2_SERVING,
408 /* transient, ssl delay hiding */
409 LWS_CONNMODE_SSL_ACK_PENDING,
411 /* transient modes */
412 LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
413 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
414 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
415 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2,
416 LWS_CONNMODE_WS_CLIENT_WAITING_SSL,
417 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
418 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
419 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
421 /* special internal types */
422 LWS_CONNMODE_SERVER_LISTENER,
426 LWS_RXFLOW_ALLOW = (1 << 0),
427 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
430 struct lws_protocols;
434 struct lws_io_watcher {
435 struct ev_io watcher;
436 struct lws_context* context;
439 struct lws_signal_watcher {
440 struct ev_signal watcher;
441 struct lws_context* context;
443 #endif /* LWS_USE_LIBEV */
446 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
447 struct lws_fd_hashtable {
457 struct lws_pollfd *fds;
459 /* different implementation between unix and windows */
460 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
462 struct lws **lws_lookup; /* fd to wsi */
466 struct ev_loop* io_loop;
467 struct lws_io_watcher w_accept;
468 struct lws_signal_watcher w_sigint;
469 lws_ev_signal_cb* lws_ev_sigint_cb;
471 #endif /* LWS_USE_LIBEV */
475 char http_proxy_address[128];
476 char canonical_hostname[128];
477 char proxy_basic_auth_token[128];
478 unsigned int http_proxy_port;
479 unsigned int options;
480 time_t last_timeout_check_s;
483 * usable by anything in the service code, but only if the scope
484 * does not last longer than the service action (since next service
485 * of any socket can likewise use it and overwrite)
487 unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
489 int started_with_parent;
492 int listen_service_modulo;
493 int listen_service_count;
494 lws_sockfd_type listen_service_fd;
495 int listen_service_extraseen;
498 * set to the Thread ID that's doing the service loop just before entry
499 * to poll indicates service thread likely idling in poll()
500 * volatile because other threads may check it as part of processing
501 * for pollfd event change.
503 volatile int service_tid;
505 int dummy_pipe_fds[2];
513 unsigned long worst_latency;
514 char worst_latency_info[256];
517 #ifdef LWS_OPENSSL_SUPPORT
519 int allow_non_ssl_on_ssl_port;
520 unsigned int user_supplied_ssl_ctx:1;
522 SSL_CTX *ssl_client_ctx;
523 struct lws *pending_read_list; /* linked list */
524 #define lws_ssl_anybody_has_buffered_read(ctx) (ctx->use_ssl && ctx->pending_read_list)
526 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
528 const struct lws_protocols *protocols;
530 #ifndef LWS_NO_EXTENSIONS
531 const struct lws_extension *extensions;
533 const struct lws_token_limits *token_limits;
536 struct lws_plat_file_ops fops;
540 LWS_EV_READ = (1 << 0),
541 LWS_EV_WRITE = (1 << 1),
542 LWS_EV_START = (1 << 2),
543 LWS_EV_STOP = (1 << 3),
547 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
548 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
550 lws_libev_accept(struct lws_context *context,
551 struct lws *new_wsi, lws_sockfd_type accept_fd);
553 lws_libev_io(const struct lws_context *context,
554 struct lws *wsi, int flags);
556 lws_libev_init_fd_table(struct lws_context *context);
558 lws_libev_run(const struct lws_context *context);
560 #define LWS_LIBEV_ENABLED(context) (0)
562 #define lws_feature_status_libev(_a) \
563 lwsl_notice("libev support not compiled in\n")
565 #define lws_feature_status_libev(_a)
567 #define lws_libev_accept(_a, _b, _c) ((void) 0)
568 #define lws_libev_io(_a, _b, _c) ((void) 0)
569 #define lws_libev_init_fd_table(_a) (0)
570 #define lws_libev_run(_a) ((void) 0)
574 #define LWS_IPV6_ENABLED(context) \
575 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
577 #define LWS_IPV6_ENABLED(context) (0)
580 enum uri_path_states {
583 URIPS_SEEN_SLASH_DOT,
584 URIPS_SEEN_SLASH_DOT_DOT,
588 enum uri_esc_states {
591 URIES_SEEN_PERCENT_H1,
595 * This is totally opaque to code using the library. It's exported as a
596 * forward-reference pointer-only declaration; the user can use the pointer with
597 * other APIs to get information out of it.
600 struct lws_fragments {
601 unsigned short offset;
603 unsigned char next_frag_index;
606 /* notice that these union members:
612 * all have a pointer to allocated_headers struct as their first member.
614 * It means for allocated_headers access, the three union paths can all be
615 * used interchangeably to access the same data
618 struct allocated_headers {
619 unsigned char next_frag_index;
621 unsigned char frag_index[WSI_TOKEN_COUNT];
622 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
623 char data[LWS_MAX_HEADER_LEN];
624 #ifndef LWS_NO_CLIENT
625 char initial_handshake_hash_base64[30];
626 unsigned short c_port;
630 struct _lws_http_mode_related {
631 /* MUST be first in struct */
632 struct allocated_headers *ah; /* mirroring _lws_header_related */
634 unsigned long filepos;
635 unsigned long filelen;
637 enum http_version request_version;
638 enum http_connection_type connection_type;
639 unsigned int content_length;
640 unsigned int content_remain;
645 enum lws_http2_settings {
646 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
647 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
648 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
649 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
650 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
651 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
653 LWS_HTTP2_SETTINGS__COUNT /* always last */
656 enum lws_http2_wellknown_frame_types {
657 LWS_HTTP2_FRAME_TYPE_DATA,
658 LWS_HTTP2_FRAME_TYPE_HEADERS,
659 LWS_HTTP2_FRAME_TYPE_PRIORITY,
660 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
661 LWS_HTTP2_FRAME_TYPE_SETTINGS,
662 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
663 LWS_HTTP2_FRAME_TYPE_PING,
664 LWS_HTTP2_FRAME_TYPE_GOAWAY,
665 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
666 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
668 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
671 enum lws_http2_flags {
672 LWS_HTTP2_FLAG_END_STREAM = 1,
673 LWS_HTTP2_FLAG_END_HEADERS = 4,
674 LWS_HTTP2_FLAG_PADDED = 8,
675 LWS_HTTP2_FLAG_PRIORITY = 0x20,
677 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
680 #define LWS_HTTP2_STREAM_ID_MASTER 0
681 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
682 #define LWS_HTTP2_SETTINGS_LENGTH 6
684 struct http2_settings {
685 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
688 enum http2_hpack_state {
690 /* optional before first header block */
692 HKPS_OPT_E_DEPENDENCY,
705 /* optional after last header block */
706 HKPS_OPT_DISCARD_PADDING,
709 enum http2_hpack_type {
711 HPKT_INDEXED_HDR_6_VALUE_INCR,
712 HPKT_LITERAL_HDR_VALUE_INCR,
713 HPKT_INDEXED_HDR_4_VALUE,
714 HPKT_LITERAL_HDR_VALUE,
718 struct hpack_dt_entry {
719 int token; /* additions that don't map to a token are ignored */
724 struct hpack_dynamic_table {
725 struct hpack_dt_entry *entries;
733 struct _lws_http2_related {
735 * having this first lets us also re-use all HTTP union code
736 * and in turn, http_mode_related has allocated headers in right
737 * place so we can use the header apis on the wsi directly still
739 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
741 struct http2_settings my_settings;
742 struct http2_settings peer_settings;
744 struct lws *parent_wsi;
745 struct lws *next_child_wsi;
747 struct hpack_dynamic_table *hpack_dyn_table;
753 unsigned int stream_id;
754 struct lws *stream_wsi;
757 unsigned char frame_state;
758 unsigned char padding;
760 unsigned char ping_payload[8];
762 unsigned short round_robin_POLLOUT;
763 unsigned short count_POLLOUT_children;
765 unsigned int END_STREAM:1;
766 unsigned int END_HEADERS:1;
767 unsigned int send_END_STREAM:1;
768 unsigned int GOING_AWAY;
769 unsigned int requested_POLLOUT:1;
770 unsigned int waiting_tx_credit:1;
773 enum http2_hpack_state hpack;
774 enum http2_hpack_type hpack_type;
775 unsigned int header_index;
776 unsigned int hpack_len;
777 unsigned short hpack_pos;
778 unsigned char hpack_m;
779 unsigned int hpack_e_dep;
781 unsigned int value:1;
783 /* negative credit is mandated by the spec */
785 unsigned int my_stream_id;
786 unsigned int child_count;
788 unsigned char initialized;
789 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
792 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
796 struct _lws_header_related {
797 /* MUST be first in struct */
798 struct allocated_headers *ah;
800 unsigned short current_token_limit;
801 unsigned char parser_state; /* enum lws_token_indexes */
802 enum uri_path_states ups;
803 enum uri_esc_states ues;
807 struct _lws_websocket_related {
808 char *rx_user_buffer;
809 unsigned int rx_user_buffer_head;
810 unsigned char frame_masking_nonce_04[4];
811 unsigned char frame_mask_index;
812 size_t rx_packet_length;
813 unsigned char opcode;
814 unsigned int final:1;
816 unsigned int frame_is_binary:1;
817 unsigned int all_zero_nonce:1;
818 short close_reason; /* enum lws_close_status */
820 unsigned int this_frame_masked:1;
821 unsigned int inside_frame:1; /* next write will be more of frame */
822 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
823 unsigned int payload_is_close:1; /* process as PONG, but it is close */
825 unsigned char *ping_payload_buf; /* non-NULL if malloc'd */
826 unsigned int ping_payload_alloc; /* length malloc'd */
827 unsigned int ping_payload_len;
828 unsigned char ping_pending_flag;
833 /* lifetime members */
836 struct lws_io_watcher w_read;
837 struct lws_io_watcher w_write;
838 #endif /* LWS_USE_LIBEV */
839 struct lws_context *context;
840 const struct lws_protocols *protocol;
841 #ifndef LWS_NO_EXTENSIONS
842 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
843 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
844 unsigned char count_active_extensions;
845 unsigned int extension_data_pending:1;
847 unsigned char ietf_spec_revision;
848 enum lws_pending_protocol_send pps;
850 char mode; /* enum connection_mode */
851 char state; /* enum lws_connection_states */
852 char lws_rx_parse_state; /* enum lws_rx_parse_state */
853 char rx_frame_type; /* enum lws_write_protocol */
855 unsigned int hdr_parsing_completed:1;
856 unsigned int user_space_externally_allocated:1;
857 unsigned int socket_is_permanently_unusable:1;
859 char pending_timeout; /* enum pending_timeout */
860 time_t pending_timeout_limit;
861 lws_sockfd_type sock;
862 int position_in_fds_table;
864 unsigned long action_start;
865 unsigned long latency_start;
867 /* rxflow handling */
868 unsigned char *rxflow_buffer;
871 unsigned int rxflow_change_to:2;
873 /* truncated send handling */
874 unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */
875 unsigned int truncated_send_allocation; /* size of malloc */
876 unsigned int truncated_send_offset; /* where we are in terms of spilling */
877 unsigned int truncated_send_len; /* how much is buffered */
881 /* members with mutually exclusive lifetimes are unionized */
884 struct _lws_http_mode_related http;
886 struct _lws_http2_related http2;
888 struct _lws_header_related hdr;
889 struct _lws_websocket_related ws;
892 #ifdef LWS_OPENSSL_SUPPORT
895 struct lws *pending_read_list_prev, *pending_read_list_next;
896 unsigned int use_ssl:2;
897 unsigned int upgraded:1;
901 BOOL sock_send_blocking;
905 LWS_EXTERN int log_level;
908 lws_close_and_free_session(struct lws_context *context,
909 struct lws *wsi, enum lws_close_status);
912 remove_wsi_socket_from_fds(struct lws_context *context, struct lws *wsi);
914 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
918 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
919 int ret, int completion) {
921 (void)context; (void)wsi; (void)action; (void)ret;
926 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
927 do { (void)context; (void)wsi; } while (0);
930 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
932 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
933 int ret, int completion);
937 lws_set_protocol_write_pending(struct lws_context *context, struct lws *wsi,
938 enum lws_pending_protocol_send pend);
940 lws_client_rx_sm(struct lws *wsi, unsigned char c);
943 lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c);
946 lws_http_action(struct lws_context *context, struct lws *wsi);
949 lws_b64_selftest(void);
951 #if defined(_WIN32) || defined(MBED_OPERATORS)
952 LWS_EXTERN struct lws *
953 wsi_from_fd(struct lws_context *context, lws_sockfd_type fd);
956 insert_wsi(struct lws_context *context, struct lws *wsi);
959 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
961 #define wsi_from_fd(A,B) A->lws_lookup[B]
962 #define insert_wsi(A,B) A->lws_lookup[B->sock]=B
963 #define delete_from_fd(A,B) A->lws_lookup[B]=0
967 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
970 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
974 lws_service_timeout_check(struct lws_context *context,
975 struct lws *wsi, unsigned int sec);
977 LWS_EXTERN struct lws *
978 lws_client_connect_2(struct lws_context *context, struct lws *wsi);
980 LWS_EXTERN struct lws *
981 lws_create_new_server_wsi(struct lws_context *context);
984 lws_generate_client_handshake(struct lws_context *context, struct lws *wsi,
988 lws_handle_POLLOUT_event(struct lws_context *context, struct lws *wsi,
989 struct lws_pollfd *pollfd);
995 #ifndef LWS_NO_EXTENSIONS
997 lws_context_init_extensions(struct lws_context_creation_info *info,
998 struct lws_context *context);
1000 lws_any_extension_handled(struct lws_context *context, struct lws *wsi,
1001 enum lws_extension_callback_reasons r,
1002 void *v, size_t len);
1005 lws_ext_callback_for_each_active(struct lws *wsi, int reason,
1006 void *buf, int len);
1008 lws_ext_callback_for_each_extension_type(struct lws_context *context,
1009 struct lws *wsi, int reason,
1010 void *arg, int len);
1012 #define lws_any_extension_handled(_a, _b, _c, _d, _e) (0)
1013 #define lws_ext_callback_for_each_active(_a, _b, _c, _d) (0)
1014 #define lws_ext_callback_for_each_extension_type(_a, _b, _c, _d, _e) (0)
1015 #define lws_issue_raw_ext_access lws_issue_raw
1016 #define lws_context_init_extensions(_a, _b)
1020 lws_client_interpret_server_handshake(struct lws_context *context,
1024 lws_rx_sm(struct lws *wsi, unsigned char c);
1027 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1030 _lws_rx_flow_control(struct lws *wsi);
1033 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1036 user_callback_handle_rxflow(callback_function, struct lws_context *context,
1037 struct lws *wsi, enum lws_callback_reasons reason,
1038 void *user, void *in, size_t len);
1039 #ifdef LWS_USE_HTTP2
1040 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1041 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1043 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1044 unsigned char *buf, int len);
1045 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1047 lws_http2_parser(struct lws_context *context,
1048 struct lws *wsi, unsigned char c);
1049 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1051 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1052 unsigned int sid, unsigned int len,
1053 unsigned char *buf);
1054 LWS_EXTERN struct lws *
1055 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1056 LWS_EXTERN int lws_hpack_interpret(struct lws_context *context,
1060 lws_add_http2_header_by_name(struct lws_context *context, struct lws *wsi,
1061 const unsigned char *name,
1062 const unsigned char *value, int length,
1063 unsigned char **p, unsigned char *end);
1065 lws_add_http2_header_by_token(struct lws_context *context, struct lws *wsi,
1066 enum lws_token_indexes token,
1067 const unsigned char *value, int length,
1068 unsigned char **p, unsigned char *end);
1070 lws_add_http2_header_status(struct lws_context *context, struct lws *wsi,
1071 unsigned int code, unsigned char **p,
1072 unsigned char *end);
1074 void lws_http2_configure_if_upgraded(struct lws *wsi);
1076 #define lws_http2_configure_if_upgraded(x)
1080 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
1083 lws_allocate_header_table(struct lws *wsi);
1086 lws_free_header_table(struct lws *wsi);
1089 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1092 lws_hdr_simple_create(struct lws *wsi,
1093 enum lws_token_indexes h, const char *s);
1096 lws_ensure_user_space(struct lws *wsi);
1099 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1101 #ifndef LWS_NO_SERVER
1102 int lws_context_init_server(struct lws_context_creation_info *info,
1103 struct lws_context *context);
1105 handshake_0405(struct lws_context *context, struct lws *wsi);
1107 lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
1109 lws_server_get_canonical_hostname(struct lws_context *context,
1110 struct lws_context_creation_info *info);
1112 #define lws_context_init_server(_a, _b) (0)
1113 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1114 #define lws_server_get_canonical_hostname(_a, _b)
1117 #ifndef LWS_NO_DAEMONIZE
1118 LWS_EXTERN int get_daemonize_pid();
1120 #define get_daemonize_pid() (0)
1123 #if !defined(MBED_OPERATORS)
1125 interface_to_sa(struct lws_context *context, const char *ifname,
1126 struct sockaddr_in *addr, size_t addrlen);
1128 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1130 enum lws_ssl_capable_status {
1131 LWS_SSL_CAPABLE_ERROR = -1,
1132 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1135 #ifndef LWS_OPENSSL_SUPPORT
1136 #define LWS_SSL_ENABLED(context) (0)
1137 #define lws_context_init_server_ssl(_a, _b) (0)
1138 #define lws_ssl_destroy(_a)
1139 #define lws_context_init_http2_ssl(_a)
1140 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1141 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1142 #define lws_ssl_pending lws_ssl_pending_no_ssl
1143 #define lws_server_socket_service_ssl(_a, _b, _c, _d, _e) (0)
1144 #define lws_ssl_close(_a) (0)
1145 #define lws_ssl_context_destroy(_a)
1146 #define lws_ssl_remove_wsi_from_buffered_list(_a, _b)
1148 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1149 LWS_EXTERN int openssl_websocket_private_data_index;
1151 lws_ssl_capable_read(struct lws_context *context,
1152 struct lws *wsi, unsigned char *buf, int len);
1154 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1156 lws_ssl_pending(struct lws *wsi);
1158 lws_server_socket_service_ssl(struct lws_context *context, struct lws **wsi,
1159 struct lws *new_wsi, lws_sockfd_type accept_fd,
1160 struct lws_pollfd *pollfd);
1162 lws_ssl_close(struct lws *wsi);
1164 lws_ssl_context_destroy(struct lws_context *context);
1166 lws_ssl_remove_wsi_from_buffered_list(struct lws_context *context,
1168 #ifndef LWS_NO_SERVER
1170 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1171 struct lws_context *context);
1173 #define lws_context_init_server_ssl(_a, _b) (0)
1176 lws_ssl_destroy(struct lws_context *context);
1180 #ifdef LWS_USE_HTTP2
1182 lws_context_init_http2_ssl(struct lws_context *context);
1184 #define lws_context_init_http2_ssl(_a)
1189 lws_ssl_capable_read_no_ssl(struct lws_context *context,
1190 struct lws *wsi, unsigned char *buf, int len);
1193 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1196 lws_ssl_pending_no_ssl(struct lws *wsi);
1198 #ifndef LWS_NO_CLIENT
1199 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1201 struct lws_pollfd *pollfd);
1202 #ifdef LWS_OPENSSL_SUPPORT
1204 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1205 struct lws_context *context);
1207 #define lws_context_init_client_ssl(_a, _b) (0)
1210 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1212 lws_decode_ssl_error(void);
1214 #define lws_context_init_client_ssl(_a, _b) (0)
1215 #define lws_handshake_client(_a, _b, _c) (0)
1217 #ifndef LWS_NO_SERVER
1219 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1220 struct lws_pollfd *pollfd);
1222 _lws_rx_flow_control(struct lws *wsi);
1224 lws_handshake_server(struct lws_context *context,
1225 struct lws *wsi, unsigned char **buf, size_t len);
1227 #define lws_server_socket_service(_a, _b, _c) (0)
1228 #define _lws_rx_flow_control(_a) (0)
1229 #define lws_handshake_server(_a, _b, _c, _d) (0)
1233 lws_get_addresses(struct lws_context *context, void *ads, char *name,
1234 int name_len, char *rip, int rip_len);
1240 lws_realloc(void *ptr, size_t size);
1243 lws_zalloc(size_t size);
1245 #define lws_malloc(S) lws_realloc(NULL, S)
1246 #define lws_free(P) lws_realloc(P, 0)
1247 #define lws_free2(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
1251 lws_plat_delete_socket_from_fds(struct lws_context *context,
1252 struct lws *wsi, int m);
1254 lws_plat_insert_socket_into_fds(struct lws_context *context,
1257 lws_plat_service_periodic(struct lws_context *context);
1260 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1261 struct lws_pollfd *pfd);
1263 lws_plat_context_early_init(void);
1265 lws_plat_context_early_destroy(struct lws_context *context);
1267 lws_plat_context_late_destroy(struct lws_context *context);
1269 lws_poll_listen_fd(struct lws_pollfd *fd);
1271 lws_plat_service(struct lws_context *context, int timeout_ms);
1273 lws_plat_init(struct lws_context *context,
1274 struct lws_context_creation_info *info);
1276 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1277 LWS_EXTERN unsigned long long
1278 time_in_microseconds(void);
1279 LWS_EXTERN const char *
1280 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);