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
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_PROTOCOLS
287 #define LWS_MAX_PROTOCOLS 5
289 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
290 #define LWS_MAX_EXTENSIONS_ACTIVE 3
292 #ifndef SPEC_LATEST_SUPPORTED
293 #define SPEC_LATEST_SUPPORTED 13
295 #ifndef AWAITING_TIMEOUT
296 #define AWAITING_TIMEOUT 5
298 #ifndef CIPHERS_LIST_STRING
299 #define CIPHERS_LIST_STRING "DEFAULT"
301 #ifndef LWS_SOMAXCONN
302 #define LWS_SOMAXCONN SOMAXCONN
305 #define MAX_WEBSOCKET_04_KEY_LEN 128
306 #define LWS_MAX_SOCKET_IO_BUF 4096
308 #ifndef SYSTEM_RANDOM_FILEPATH
309 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
311 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
312 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
316 * if not in a connection storm, check for incoming
317 * connections this many normal connection services
319 #define LWS_lserv_mod 10
321 enum lws_websocket_opcodes_07 {
322 LWSWSOPC_CONTINUATION = 0,
323 LWSWSOPC_TEXT_FRAME = 1,
324 LWSWSOPC_BINARY_FRAME = 2,
326 LWSWSOPC_NOSPEC__MUX = 7,
328 /* control extensions 8+ */
336 enum lws_connection_states {
338 LWSS_HTTP_ISSUING_FILE,
343 LWSS_CLIENT_UNCONNECTED,
344 LWSS_RETURNED_CLOSE_ALREADY,
345 LWSS_AWAITING_CLOSE_ACK,
346 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
348 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
349 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
350 LWSS_HTTP2_ESTABLISHED,
358 enum http_connection_type {
359 HTTP_CONNECTION_CLOSE,
360 HTTP_CONNECTION_KEEP_ALIVE
363 enum lws_pending_protocol_send {
365 LWS_PPS_HTTP2_MY_SETTINGS,
366 LWS_PPS_HTTP2_ACK_SETTINGS,
370 enum lws_rx_parse_state {
373 LWS_RXPS_04_MASK_NONCE_1,
374 LWS_RXPS_04_MASK_NONCE_2,
375 LWS_RXPS_04_MASK_NONCE_3,
377 LWS_RXPS_04_FRAME_HDR_1,
378 LWS_RXPS_04_FRAME_HDR_LEN,
379 LWS_RXPS_04_FRAME_HDR_LEN16_2,
380 LWS_RXPS_04_FRAME_HDR_LEN16_1,
381 LWS_RXPS_04_FRAME_HDR_LEN64_8,
382 LWS_RXPS_04_FRAME_HDR_LEN64_7,
383 LWS_RXPS_04_FRAME_HDR_LEN64_6,
384 LWS_RXPS_04_FRAME_HDR_LEN64_5,
385 LWS_RXPS_04_FRAME_HDR_LEN64_4,
386 LWS_RXPS_04_FRAME_HDR_LEN64_3,
387 LWS_RXPS_04_FRAME_HDR_LEN64_2,
388 LWS_RXPS_04_FRAME_HDR_LEN64_1,
390 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
391 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
392 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
393 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
395 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
399 enum connection_mode {
401 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
402 LWSCM_PRE_WS_SERVING_ACCEPT,
409 /* transient, ssl delay hiding */
410 LWSCM_SSL_ACK_PENDING,
412 /* transient modes */
413 LWSCM_WSCL_WAITING_CONNECT,
414 LWSCM_WSCL_WAITING_PROXY_REPLY,
415 LWSCM_WSCL_ISSUE_HANDSHAKE,
416 LWSCM_WSCL_ISSUE_HANDSHAKE2,
417 LWSCM_WSCL_WAITING_SSL,
418 LWSCM_WSCL_WAITING_SERVER_REPLY,
419 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
420 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
422 /* special internal types */
423 LWSCM_SERVER_LISTENER,
427 LWS_RXFLOW_ALLOW = (1 << 0),
428 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
431 struct lws_protocols;
435 struct lws_io_watcher {
436 struct ev_io watcher;
437 struct lws_context* context;
440 struct lws_signal_watcher {
441 struct ev_signal watcher;
442 struct lws_context* context;
444 #endif /* LWS_USE_LIBEV */
447 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
448 struct lws_fd_hashtable {
458 struct lws_pollfd *fds;
460 /* different implementation between unix and windows */
461 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
463 struct lws **lws_lookup; /* fd to wsi */
467 struct ev_loop* io_loop;
468 struct lws_io_watcher w_accept;
469 struct lws_signal_watcher w_sigint;
470 lws_ev_signal_cb* lws_ev_sigint_cb;
472 #endif /* LWS_USE_LIBEV */
476 char http_proxy_address[128];
477 char canonical_hostname[128];
478 char proxy_basic_auth_token[128];
479 unsigned int http_proxy_port;
480 unsigned int options;
481 time_t last_timeout_check_s;
484 * usable by anything in the service code, but only if the scope
485 * does not last longer than the service action (since next service
486 * of any socket can likewise use it and overwrite)
488 unsigned char serv_buf[LWS_MAX_SOCKET_IO_BUF];
490 int started_with_parent;
495 lws_sockfd_type lserv_fd;
499 * set to the Thread ID that's doing the service loop just before entry
500 * to poll indicates service thread likely idling in poll()
501 * volatile because other threads may check it as part of processing
502 * for pollfd event change.
504 volatile int service_tid;
506 int dummy_pipe_fds[2];
514 unsigned long worst_latency;
515 char worst_latency_info[256];
518 #ifdef LWS_OPENSSL_SUPPORT
520 int allow_non_ssl_on_ssl_port;
521 unsigned int user_supplied_ssl_ctx:1;
523 SSL_CTX *ssl_client_ctx;
524 struct lws *pending_read_list; /* linked list */
525 #define lws_ssl_anybody_has_buffered_read(ctx) (ctx->use_ssl && ctx->pending_read_list)
527 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
529 const struct lws_protocols *protocols;
531 #ifndef LWS_NO_EXTENSIONS
532 const struct lws_extension *extensions;
534 const struct lws_token_limits *token_limits;
537 struct lws_plat_file_ops fops;
541 LWS_EV_READ = (1 << 0),
542 LWS_EV_WRITE = (1 << 1),
543 LWS_EV_START = (1 << 2),
544 LWS_EV_STOP = (1 << 3),
548 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
549 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
551 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
553 lws_libev_io(struct lws *wsi, int flags);
555 lws_libev_init_fd_table(struct lws_context *context);
557 lws_libev_run(const struct lws_context *context);
559 #define LWS_LIBEV_ENABLED(context) (0)
561 #define lws_feature_status_libev(_a) \
562 lwsl_notice("libev support not compiled in\n")
564 #define lws_feature_status_libev(_a)
566 #define lws_libev_accept(_a, _b) ((void) 0)
567 #define lws_libev_io(_a, _b) ((void) 0)
568 #define lws_libev_init_fd_table(_a) (0)
569 #define lws_libev_run(_a) ((void) 0)
573 #define LWS_IPV6_ENABLED(context) \
574 (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
576 #define LWS_IPV6_ENABLED(context) (0)
579 enum uri_path_states {
582 URIPS_SEEN_SLASH_DOT,
583 URIPS_SEEN_SLASH_DOT_DOT,
586 enum uri_esc_states {
589 URIES_SEEN_PERCENT_H1,
593 * This is totally opaque to code using the library. It's exported as a
594 * forward-reference pointer-only declaration; the user can use the pointer with
595 * other APIs to get information out of it.
598 struct lws_fragments {
599 unsigned short offset;
601 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
604 /* notice that these union members:
610 * all have a pointer to allocated_headers struct as their first member.
612 * It means for allocated_headers access, the three union paths can all be
613 * used interchangeably to access the same data
616 struct allocated_headers {
620 * for each recognized token, frag_index says which frag[] his data
621 * starts in (0 means the token did not appear)
622 * the actual header data gets dumped as it comes in, into data[]
624 unsigned char frag_index[WSI_TOKEN_COUNT];
626 * the randomly ordered fragments, indexed by frag_index and
627 * lws_fragments->nfrag for continuation.
629 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
630 char data[LWS_MAX_HEADER_LEN];
632 #ifndef LWS_NO_CLIENT
633 char initial_handshake_hash_base64[30];
634 unsigned short c_port;
638 struct _lws_http_mode_related {
639 /* MUST be first in struct */
640 struct allocated_headers *ah; /* mirroring _lws_header_related */
642 unsigned long filepos;
643 unsigned long filelen;
645 enum http_version request_version;
646 enum http_connection_type connection_type;
647 unsigned int content_length;
648 unsigned int content_remain;
653 enum lws_http2_settings {
654 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
655 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
656 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
657 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
658 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
659 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
661 LWS_HTTP2_SETTINGS__COUNT /* always last */
664 enum lws_http2_wellknown_frame_types {
665 LWS_HTTP2_FRAME_TYPE_DATA,
666 LWS_HTTP2_FRAME_TYPE_HEADERS,
667 LWS_HTTP2_FRAME_TYPE_PRIORITY,
668 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
669 LWS_HTTP2_FRAME_TYPE_SETTINGS,
670 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
671 LWS_HTTP2_FRAME_TYPE_PING,
672 LWS_HTTP2_FRAME_TYPE_GOAWAY,
673 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
674 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
676 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
679 enum lws_http2_flags {
680 LWS_HTTP2_FLAG_END_STREAM = 1,
681 LWS_HTTP2_FLAG_END_HEADERS = 4,
682 LWS_HTTP2_FLAG_PADDED = 8,
683 LWS_HTTP2_FLAG_PRIORITY = 0x20,
685 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
688 #define LWS_HTTP2_STREAM_ID_MASTER 0
689 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
690 #define LWS_HTTP2_SETTINGS_LENGTH 6
692 struct http2_settings {
693 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
696 enum http2_hpack_state {
698 /* optional before first header block */
700 HKPS_OPT_E_DEPENDENCY,
713 /* optional after last header block */
714 HKPS_OPT_DISCARD_PADDING,
717 enum http2_hpack_type {
719 HPKT_INDEXED_HDR_6_VALUE_INCR,
720 HPKT_LITERAL_HDR_VALUE_INCR,
721 HPKT_INDEXED_HDR_4_VALUE,
722 HPKT_LITERAL_HDR_VALUE,
726 struct hpack_dt_entry {
727 int token; /* additions that don't map to a token are ignored */
732 struct hpack_dynamic_table {
733 struct hpack_dt_entry *entries;
741 struct _lws_http2_related {
743 * having this first lets us also re-use all HTTP union code
744 * and in turn, http_mode_related has allocated headers in right
745 * place so we can use the header apis on the wsi directly still
747 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
749 struct http2_settings my_settings;
750 struct http2_settings peer_settings;
752 struct lws *parent_wsi;
753 struct lws *next_child_wsi;
755 struct hpack_dynamic_table *hpack_dyn_table;
761 unsigned int stream_id;
762 struct lws *stream_wsi;
765 unsigned char frame_state;
766 unsigned char padding;
768 unsigned char ping_payload[8];
770 unsigned short round_robin_POLLOUT;
771 unsigned short count_POLLOUT_children;
773 unsigned int END_STREAM:1;
774 unsigned int END_HEADERS:1;
775 unsigned int send_END_STREAM:1;
776 unsigned int GOING_AWAY;
777 unsigned int requested_POLLOUT:1;
778 unsigned int waiting_tx_credit:1;
781 enum http2_hpack_state hpack;
782 enum http2_hpack_type hpack_type;
783 unsigned int header_index;
784 unsigned int hpack_len;
785 unsigned short hpack_pos;
786 unsigned char hpack_m;
787 unsigned int hpack_e_dep;
789 unsigned int value:1;
791 /* negative credit is mandated by the spec */
793 unsigned int my_stream_id;
794 unsigned int child_count;
796 unsigned char initialized;
797 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
800 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
804 struct _lws_header_related {
805 /* MUST be first in struct */
806 struct allocated_headers *ah;
808 unsigned short current_token_limit;
809 unsigned char parser_state; /* enum lws_token_indexes */
810 enum uri_path_states ups;
811 enum uri_esc_states ues;
813 char post_literal_equal;
816 struct _lws_websocket_related {
817 char *rx_user_buffer;
818 unsigned int rx_user_buffer_head;
819 unsigned char mask_nonce[4];
820 unsigned char frame_mask_index;
821 size_t rx_packet_length;
822 unsigned char opcode;
823 unsigned int final:1;
825 unsigned int frame_is_binary:1;
826 unsigned int all_zero_nonce:1;
827 short close_reason; /* enum lws_close_status */
829 unsigned int this_frame_masked:1;
830 unsigned int inside_frame:1; /* next write will be more of frame */
831 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
832 unsigned int payload_is_close:1; /* process as PONG, but it is close */
834 unsigned char *ping_payload_buf; /* non-NULL if malloc'd */
835 unsigned int ping_payload_alloc; /* length malloc'd */
836 unsigned int ping_payload_len;
837 unsigned char ping_pending_flag;
842 /* lifetime members */
845 struct lws_io_watcher w_read;
846 struct lws_io_watcher w_write;
847 #endif /* LWS_USE_LIBEV */
848 struct lws_context *context;
849 const struct lws_protocols *protocol;
850 #ifndef LWS_NO_EXTENSIONS
851 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
852 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
853 unsigned char count_active_extensions;
854 unsigned int extension_data_pending:1;
856 unsigned char ietf_spec_revision;
857 enum lws_pending_protocol_send pps;
859 char mode; /* enum connection_mode */
860 char state; /* enum lws_connection_states */
861 char lws_rx_parse_state; /* enum lws_rx_parse_state */
862 char rx_frame_type; /* enum lws_write_protocol */
864 unsigned int hdr_parsing_completed:1;
865 unsigned int user_space_externally_allocated:1;
866 unsigned int socket_is_permanently_unusable:1;
868 char pending_timeout; /* enum pending_timeout */
869 time_t pending_timeout_limit;
870 lws_sockfd_type sock;
871 int position_in_fds_table;
873 unsigned long action_start;
874 unsigned long latency_start;
876 /* rxflow handling */
877 unsigned char *rxflow_buffer;
880 unsigned int rxflow_change_to:2;
882 /* truncated send handling */
883 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
884 unsigned int trunc_alloc_len; /* size of malloc */
885 unsigned int trunc_offset; /* where we are in terms of spilling */
886 unsigned int trunc_len; /* how much is buffered */
890 /* members with mutually exclusive lifetimes are unionized */
893 struct _lws_http_mode_related http;
895 struct _lws_http2_related http2;
897 struct _lws_header_related hdr;
898 struct _lws_websocket_related ws;
901 #ifdef LWS_OPENSSL_SUPPORT
904 struct lws *pending_read_list_prev, *pending_read_list_next;
905 unsigned int use_ssl:2;
906 unsigned int upgraded:1;
910 BOOL sock_send_blocking;
914 LWS_EXTERN int log_level;
917 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
920 remove_wsi_socket_from_fds(struct lws *wsi);
922 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
926 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
927 int ret, int completion) {
929 (void)context; (void)wsi; (void)action; (void)ret;
934 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
935 do { (void)context; (void)wsi; } while (0);
938 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
940 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
941 int ret, int completion);
945 lws_set_protocol_write_pending(struct lws *wsi,
946 enum lws_pending_protocol_send pend);
948 lws_client_rx_sm(struct lws *wsi, unsigned char c);
951 lws_parse(struct lws *wsi, unsigned char c);
954 lws_http_action(struct lws *wsi);
957 lws_b64_selftest(void);
959 #if defined(_WIN32) || defined(MBED_OPERATORS)
960 LWS_EXTERN struct lws *
961 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
964 insert_wsi(struct lws_context *context, struct lws *wsi);
967 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
969 #define wsi_from_fd(A,B) A->lws_lookup[B]
970 #define insert_wsi(A,B) A->lws_lookup[B->sock]=B
971 #define delete_from_fd(A,B) A->lws_lookup[B]=0
975 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
978 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
982 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
984 LWS_EXTERN struct lws *
985 lws_client_connect_2(struct lws *wsi);
987 LWS_EXTERN struct lws *
988 lws_create_new_server_wsi(struct lws_context *context);
991 lws_generate_client_handshake(struct lws *wsi, char *pkt);
994 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1000 #ifndef LWS_NO_EXTENSIONS
1002 lws_context_init_extensions(struct lws_context_creation_info *info,
1003 struct lws_context *context);
1005 lws_any_extension_handled(struct lws *wsi,
1006 enum lws_extension_callback_reasons r,
1007 void *v, size_t len);
1010 lws_ext_cb_wsi_active_exts(struct lws *wsi, int reason,
1011 void *buf, int len);
1013 lws_ext_cb_all_exts(struct lws_context *context,
1014 struct lws *wsi, int reason,
1015 void *arg, int len);
1017 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1018 #define lws_ext_cb_wsi_active_exts(_a, _b, _c, _d) (0)
1019 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1020 #define lws_issue_raw_ext_access lws_issue_raw
1021 #define lws_context_init_extensions(_a, _b)
1025 lws_client_interpret_server_handshake(struct lws *wsi);
1028 lws_rx_sm(struct lws *wsi, unsigned char c);
1031 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1034 _lws_rx_flow_control(struct lws *wsi);
1037 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1040 user_callback_handle_rxflow(callback_function, struct lws *wsi,
1041 enum lws_callback_reasons reason, void *user,
1042 void *in, size_t len);
1043 #ifdef LWS_USE_HTTP2
1044 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1045 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1047 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1048 unsigned char *buf, int len);
1049 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1051 lws_http2_parser(struct lws *wsi, unsigned char c);
1052 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1054 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1055 unsigned int sid, unsigned int len,
1056 unsigned char *buf);
1057 LWS_EXTERN struct lws *
1058 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1059 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1062 lws_add_http2_header_by_name(struct lws *wsi,
1063 const unsigned char *name,
1064 const unsigned char *value, int length,
1065 unsigned char **p, unsigned char *end);
1067 lws_add_http2_header_by_token(struct lws *wsi,
1068 enum lws_token_indexes token,
1069 const unsigned char *value, int length,
1070 unsigned char **p, unsigned char *end);
1072 lws_add_http2_header_status(struct lws *wsi,
1073 unsigned int code, unsigned char **p,
1074 unsigned char *end);
1076 void lws_http2_configure_if_upgraded(struct lws *wsi);
1078 #define lws_http2_configure_if_upgraded(x)
1082 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
1085 lws_allocate_header_table(struct lws *wsi);
1088 lws_free_header_table(struct lws *wsi);
1091 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1094 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1097 lws_ensure_user_space(struct lws *wsi);
1100 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1102 #ifndef LWS_NO_SERVER
1103 int lws_context_init_server(struct lws_context_creation_info *info,
1104 struct lws_context *context);
1106 handshake_0405(struct lws_context *context, struct lws *wsi);
1108 lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
1110 lws_server_get_canonical_hostname(struct lws_context *context,
1111 struct lws_context_creation_info *info);
1113 #define lws_context_init_server(_a, _b) (0)
1114 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1115 #define lws_server_get_canonical_hostname(_a, _b)
1118 #ifndef LWS_NO_DAEMONIZE
1119 LWS_EXTERN int get_daemonize_pid();
1121 #define get_daemonize_pid() (0)
1124 #if !defined(MBED_OPERATORS)
1126 interface_to_sa(struct lws_context *context, const char *ifname,
1127 struct sockaddr_in *addr, size_t addrlen);
1129 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1131 enum lws_ssl_capable_status {
1132 LWS_SSL_CAPABLE_ERROR = -1,
1133 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1136 #ifndef LWS_OPENSSL_SUPPORT
1137 #define LWS_SSL_ENABLED(context) (0)
1138 #define lws_context_init_server_ssl(_a, _b) (0)
1139 #define lws_ssl_destroy(_a)
1140 #define lws_context_init_http2_ssl(_a)
1141 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1142 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1143 #define lws_ssl_pending lws_ssl_pending_no_ssl
1144 #define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
1145 #define lws_ssl_close(_a) (0)
1146 #define lws_ssl_context_destroy(_a)
1147 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1149 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1150 LWS_EXTERN int openssl_websocket_private_data_index;
1152 lws_ssl_capable_read(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 **wsi, struct lws *new_wsi,
1159 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 *wsi);
1167 #ifndef LWS_NO_SERVER
1169 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1170 struct lws_context *context);
1172 #define lws_context_init_server_ssl(_a, _b) (0)
1175 lws_ssl_destroy(struct lws_context *context);
1179 #ifdef LWS_USE_HTTP2
1181 lws_context_init_http2_ssl(struct lws_context *context);
1183 #define lws_context_init_http2_ssl(_a)
1188 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1191 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1194 lws_ssl_pending_no_ssl(struct lws *wsi);
1196 #ifndef LWS_NO_CLIENT
1197 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1199 struct lws_pollfd *pollfd);
1200 #ifdef LWS_OPENSSL_SUPPORT
1202 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1203 struct lws_context *context);
1205 #define lws_context_init_client_ssl(_a, _b) (0)
1208 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1210 lws_decode_ssl_error(void);
1212 #define lws_context_init_client_ssl(_a, _b) (0)
1213 #define lws_handshake_client(_a, _b, _c) (0)
1215 #ifndef LWS_NO_SERVER
1217 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1218 struct lws_pollfd *pollfd);
1220 _lws_rx_flow_control(struct lws *wsi);
1222 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1224 #define lws_server_socket_service(_a, _b, _c) (0)
1225 #define _lws_rx_flow_control(_a) (0)
1226 #define lws_handshake_server(_a, _b, _c) (0)
1230 lws_get_addresses(struct lws_context *context, void *ads, char *name,
1231 int name_len, char *rip, int rip_len);
1237 lws_realloc(void *ptr, size_t size);
1240 lws_zalloc(size_t size);
1242 #define lws_malloc(S) lws_realloc(NULL, S)
1243 #define lws_free(P) lws_realloc(P, 0)
1244 #define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
1248 lws_plat_delete_socket_from_fds(struct lws_context *context,
1249 struct lws *wsi, int m);
1251 lws_plat_insert_socket_into_fds(struct lws_context *context,
1254 lws_plat_service_periodic(struct lws_context *context);
1257 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1258 struct lws_pollfd *pfd);
1260 lws_plat_context_early_init(void);
1262 lws_plat_context_early_destroy(struct lws_context *context);
1264 lws_plat_context_late_destroy(struct lws_context *context);
1266 lws_poll_listen_fd(struct lws_pollfd *fd);
1268 lws_plat_service(struct lws_context *context, int timeout_ms);
1270 lws_plat_init(struct lws_context *context,
1271 struct lws_context_creation_info *info);
1273 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1274 LWS_EXTERN unsigned long long
1275 time_in_microseconds(void);
1276 LWS_EXTERN const char *
1277 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);