client fixups after esp8266
[platform/upstream/libwebsockets.git] / lib / private-libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include "lws_config.h"
23 #include "lws_config_private.h"
24
25
26 #if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK)
27 #define  _GNU_SOURCE
28 #endif
29
30 #ifdef LWS_HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <stdarg.h>
41 #if defined(LWS_WITH_ESP8266)
42 #include <user_interface.h>
43 #define assert(n)
44
45 /* rom-provided stdc functions for free, ensure use these instead of libc ones */
46
47 int ets_vsprintf(char *str, const char *format, va_list argptr);
48 int ets_vsnprintf(char *buffer, size_t sizeOfBuffer,  const char *format, va_list argptr);
49 int ets_snprintf(char *str, size_t size, const char *format, ...);
50 int ets_sprintf(char *str, const char *format, ...);
51 int os_printf_plus(const char *format, ...);
52 #undef malloc
53 #undef realloc
54 #undef free
55 void *pvPortMalloc(size_t s, const char *f, int line);
56 #define malloc(s) pvPortMalloc(s, "", 0)
57 void *pvPortRealloc(void *p, size_t s, const char *f, int line);
58 #define realloc(p, s) pvPortRealloc(p, s, "", 0)
59 void vPortFree(void *p, const char *f, int line);
60 #define free(p) vPortFree(p, "", 0)
61 #undef memcpy
62 void *ets_memcpy(void *dest, const void *src, size_t n);
63 #define memcpy ets_memcpy
64 void *ets_memset(void *dest, int v, size_t n);
65 #define memset ets_memset
66 char *ets_strcpy(char *dest, const char *src);
67 #define strcpy ets_strcpy
68 char *ets_strncpy(char *dest, const char *src, size_t n);
69 #define strncpy ets_strncpy
70 char *ets_strstr(const char *haystack, const char *needle);
71 #define strstr ets_strstr
72 int ets_strcmp(const char *s1, const char *s2);
73 int ets_strncmp(const char *s1, const char *s2, size_t n);
74 #define strcmp ets_strcmp
75 #define strncmp ets_strncmp
76 size_t ets_strlen(const char *s);
77 #define strlen ets_strlen
78 void *ets_memmove(void *dest, const void *src, size_t n);
79 #define memmove ets_memmove
80 char *ets_strchr(const char *s, int c);
81 #define strchr_ets_strchr
82 #undef _DEBUG
83 #include <osapi.h>
84
85 #else
86 #define STORE_IN_ROM
87 #include <assert.h>
88 #endif
89 #if LWS_MAX_SMP > 1
90 #include <pthread.h>
91 #endif
92
93 #ifdef LWS_HAVE_SYS_STAT_H
94 #include <sys/stat.h>
95 #endif
96
97 #if defined(WIN32) || defined(_WIN32)
98 #if (WINVER < 0x0501)
99 #undef WINVER
100 #undef _WIN32_WINNT
101 #define WINVER 0x0501
102 #define _WIN32_WINNT WINVER
103 #endif
104 #define LWS_NO_DAEMONIZE
105 #define LWS_ERRNO WSAGetLastError()
106 #define LWS_EAGAIN WSAEWOULDBLOCK
107 #define LWS_EALREADY WSAEALREADY
108 #define LWS_EINPROGRESS WSAEINPROGRESS
109 #define LWS_EINTR WSAEINTR
110 #define LWS_EISCONN WSAEISCONN
111 #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
112 #define MSG_NOSIGNAL 0
113 #define SHUT_RDWR SD_BOTH
114 #define SOL_TCP IPPROTO_TCP
115 #define SHUT_WR SD_SEND
116
117 #define compatible_close(fd) closesocket(fd)
118 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
119 #define lws_socket_is_valid(x) (!!x)
120 #define LWS_SOCK_INVALID 0
121 #include <winsock2.h>
122 #include <ws2tcpip.h>
123 #include <windows.h>
124 #include <tchar.h>
125 #ifdef LWS_HAVE_IN6ADDR_H
126 #include <in6addr.h>
127 #endif
128 #include <mstcpip.h>
129 #include <io.h>
130
131 #ifndef __func__
132 #define __func__ __FUNCTION__
133 #endif
134
135 #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
136 #define vsnprintf _vsnprintf
137 #else
138 #ifdef LWS_HAVE__VSNPRINTF
139 #define vsnprintf _vsnprintf
140 #endif
141 #endif
142
143 #ifdef LWS_HAVE__SNPRINTF
144 #define snprintf _snprintf
145 #endif
146
147 #else /* not windows --> */
148
149 #include <fcntl.h>
150 #include <strings.h>
151 #include <unistd.h>
152 #include <sys/types.h>
153 #ifndef MBED_OPERATORS
154 #ifndef __cplusplus
155 #include <errno.h>
156 #endif
157 #include <netdb.h>
158 #include <signal.h>
159 #ifdef LWS_WITH_ESP8266
160 #include <sockets.h>
161 #define vsnprintf ets_vsnprintf
162 #define snprintf ets_snprintf
163 #define sprintf ets_sprintf
164 #else
165 #include <sys/socket.h>
166 #endif
167 #ifdef LWS_WITH_HTTP_PROXY
168 #include <hubbub/hubbub.h>
169 #include <hubbub/parser.h>
170 #endif
171 #if defined(LWS_BUILTIN_GETIFADDRS)
172  #include <getifaddrs.h>
173 #else
174  #if !defined(LWS_WITH_ESP8266)
175  #include <ifaddrs.h>
176  #endif
177 #endif
178 #if defined (__ANDROID__)
179 #include <syslog.h>
180 #include <sys/resource.h>
181 #else
182 #if !defined(LWS_WITH_ESP8266)
183 #include <sys/syslog.h>
184 #endif
185 #endif
186 #include <netdb.h>
187 #if !defined(LWS_WITH_ESP8266)
188 #include <sys/mman.h>
189 #include <sys/un.h>
190 #include <netinet/in.h>
191 #include <netinet/tcp.h>
192 #include <arpa/inet.h>
193 #include <poll.h>
194 #endif
195 #ifdef LWS_USE_LIBEV
196 #include <ev.h>
197 #endif
198 #ifdef LWS_USE_LIBUV
199 #include <uv.h>
200 #endif
201
202 #endif /* MBED */
203
204 #ifndef LWS_NO_FORK
205 #ifdef LWS_HAVE_SYS_PRCTL_H
206 #include <sys/prctl.h>
207 #endif
208 #endif
209
210 #include <sys/time.h>
211
212 #define LWS_ERRNO errno
213 #define LWS_EAGAIN EAGAIN
214 #define LWS_EALREADY EALREADY
215 #define LWS_EINPROGRESS EINPROGRESS
216 #define LWS_EINTR EINTR
217 #define LWS_EISCONN EISCONN
218 #define LWS_EWOULDBLOCK EWOULDBLOCK
219
220 #define lws_set_blocking_send(wsi)
221
222 #if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
223 #define lws_socket_is_valid(x) ((x) != NULL)
224 #define LWS_SOCK_INVALID (NULL)
225 struct lws;
226 const char *
227 lws_plat_get_peer_simple(struct lws *wsi, char *name, int namelen);
228 #else
229 #define lws_socket_is_valid(x) (x >= 0)
230 #define LWS_SOCK_INVALID (-1)
231 #endif
232 #endif
233
234 #ifndef LWS_HAVE_BZERO
235 #ifndef bzero
236 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
237 #endif
238 #endif
239
240 #ifndef LWS_HAVE_STRERROR
241 #define strerror(x) ""
242 #endif
243
244 #ifdef LWS_OPENSSL_SUPPORT
245
246 #ifdef USE_WOLFSSL
247 #ifdef USE_OLD_CYASSL
248 #include <cyassl/openssl/ssl.h>
249 #include <cyassl/error-ssl.h>
250 #else
251 #include <wolfssl/openssl/ssl.h>
252 #include <wolfssl/error-ssl.h>
253 #endif /* not USE_OLD_CYASSL */
254 #else
255 #if defined(LWS_USE_POLARSSL)
256 #include <polarssl/ssl.h>
257 #include <polarssl/error.h>
258 #include <polarssl/md5.h>
259 #include <polarssl/sha1.h>
260 #include <polarssl/ecdh.h>
261 #define SSL_ERROR_WANT_READ POLARSSL_ERR_NET_WANT_READ
262 #define SSL_ERROR_WANT_WRITE POLARSSL_ERR_NET_WANT_WRITE
263 #define OPENSSL_VERSION_NUMBER  0x10002000L
264 #else
265 #if defined(LWS_USE_MBEDTLS)
266 #include <mbedtls/ssl.h>
267 #include <mbedtls/error.h>
268 #include <mbedtls/md5.h>
269 #include <mbedtls/sha1.h>
270 #include <mbedtls/ecdh.h>
271 #else
272 #include <openssl/ssl.h>
273 #include <openssl/evp.h>
274 #include <openssl/err.h>
275 #include <openssl/md5.h>
276 #include <openssl/sha.h>
277 #ifdef LWS_HAVE_OPENSSL_ECDH_H
278 #include <openssl/ecdh.h>
279 #endif
280 #include <openssl/x509v3.h>
281 #endif /* not USE_MBEDTLS */
282 #endif /* not USE_POLARSSL */
283 #endif /* not USE_WOLFSSL */
284 #endif
285
286 #include "libwebsockets.h"
287 #if defined(WIN32) || defined(_WIN32)
288 #else
289 static inline int compatible_close(int fd) { return close(fd); }
290 #endif
291
292 #if defined(MBED_OPERATORS)
293 #undef compatible_close
294 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
295 #ifndef BIG_ENDIAN
296 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
297 #endif
298 #ifndef LITTLE_ENDIAN
299 #define LITTLE_ENDIAN 1234
300 #endif
301 #ifndef BYTE_ORDER
302 #define BYTE_ORDER LITTLE_ENDIAN
303 #endif
304 #endif
305
306 #if defined(LWS_WITH_ESP8266)
307 #undef compatible_close
308 #define compatible_close(fd) { fd->state=ESPCONN_CLOSE; espconn_delete(fd); }
309 lws_sockfd_type
310 esp8266_create_tcp_stream_socket(void);
311 void
312 esp8266_tcp_stream_bind(lws_sockfd_type fd, int port, struct lws *wsi);
313 #ifndef BIG_ENDIAN
314 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
315 #endif
316 #ifndef LITTLE_ENDIAN
317 #define LITTLE_ENDIAN 1234
318 #endif
319 #ifndef BYTE_ORDER
320 #define BYTE_ORDER LITTLE_ENDIAN
321 #endif
322 #endif
323
324
325 #if defined(WIN32) || defined(_WIN32)
326
327 #ifndef BIG_ENDIAN
328 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
329 #endif
330 #ifndef LITTLE_ENDIAN
331 #define LITTLE_ENDIAN 1234
332 #endif
333 #ifndef BYTE_ORDER
334 #define BYTE_ORDER LITTLE_ENDIAN
335 #endif
336 #ifndef u_int64_t
337 typedef unsigned __int64 u_int64_t;
338 #endif
339
340 #undef __P
341 #ifndef __P
342 #if __STDC__
343 #define __P(protos) protos
344 #else
345 #define __P(protos) ()
346 #endif
347 #endif
348
349 #else
350
351 #include <sys/stat.h>
352 #include <sys/time.h>
353
354 #if defined(__APPLE__)
355 #include <machine/endian.h>
356 #elif defined(__FreeBSD__)
357 #include <sys/endian.h>
358 #elif defined(__linux__)
359 #include <endian.h>
360 #endif
361
362 #ifdef __cplusplus
363 extern "C" {
364 #endif
365
366 #if defined(__QNX__)
367         #include <gulliver.h>
368         #if defined(__LITTLEENDIAN__)
369                 #define BYTE_ORDER __LITTLEENDIAN__
370                 #define LITTLE_ENDIAN __LITTLEENDIAN__
371                 #define BIG_ENDIAN 4321  /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
372         #endif
373         #if defined(__BIGENDIAN__)
374                 #define BYTE_ORDER __BIGENDIAN__
375                 #define LITTLE_ENDIAN 1234  /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
376                 #define BIG_ENDIAN __BIGENDIAN__
377         #endif
378 #endif
379
380 #if !defined(BYTE_ORDER)
381 # define BYTE_ORDER __BYTE_ORDER
382 #endif
383 #if !defined(LITTLE_ENDIAN)
384 # define LITTLE_ENDIAN __LITTLE_ENDIAN
385 #endif
386 #if !defined(BIG_ENDIAN)
387 # define BIG_ENDIAN __BIG_ENDIAN
388 #endif
389
390 #endif
391
392 /*
393  * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
394  * but happily have something equivalent in the SO_NOSIGPIPE flag.
395  */
396 #ifdef __APPLE__
397 #define MSG_NOSIGNAL SO_NOSIGPIPE
398 #endif
399
400 #ifdef _WIN32
401 #ifndef FD_HASHTABLE_MODULUS
402 #define FD_HASHTABLE_MODULUS 32
403 #endif
404 #endif
405
406 #ifndef LWS_DEF_HEADER_LEN
407 #define LWS_DEF_HEADER_LEN 4096
408 #endif
409 #ifndef LWS_DEF_HEADER_POOL
410 #define LWS_DEF_HEADER_POOL 4
411 #endif
412 #ifndef LWS_MAX_PROTOCOLS
413 #define LWS_MAX_PROTOCOLS 5
414 #endif
415 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
416 #define LWS_MAX_EXTENSIONS_ACTIVE 2
417 #endif
418 #ifndef LWS_MAX_EXT_OFFERS
419 #define LWS_MAX_EXT_OFFERS 8
420 #endif
421 #ifndef SPEC_LATEST_SUPPORTED
422 #define SPEC_LATEST_SUPPORTED 13
423 #endif
424 #ifndef AWAITING_TIMEOUT
425 #define AWAITING_TIMEOUT 20
426 #endif
427 #ifndef CIPHERS_LIST_STRING
428 #define CIPHERS_LIST_STRING "DEFAULT"
429 #endif
430 #ifndef LWS_SOMAXCONN
431 #define LWS_SOMAXCONN SOMAXCONN
432 #endif
433
434 #define MAX_WEBSOCKET_04_KEY_LEN 128
435
436 #ifndef SYSTEM_RANDOM_FILEPATH
437 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
438 #endif
439
440 enum lws_websocket_opcodes_07 {
441         LWSWSOPC_CONTINUATION = 0,
442         LWSWSOPC_TEXT_FRAME = 1,
443         LWSWSOPC_BINARY_FRAME = 2,
444
445         LWSWSOPC_NOSPEC__MUX = 7,
446
447         /* control extensions 8+ */
448
449         LWSWSOPC_CLOSE = 8,
450         LWSWSOPC_PING = 9,
451         LWSWSOPC_PONG = 0xa,
452 };
453
454
455 enum lws_connection_states {
456         LWSS_HTTP,
457         LWSS_HTTP_ISSUING_FILE,
458         LWSS_HTTP_HEADERS,
459         LWSS_HTTP_BODY,
460         LWSS_DEAD_SOCKET,
461         LWSS_ESTABLISHED,
462         LWSS_CLIENT_HTTP_ESTABLISHED,
463         LWSS_CLIENT_UNCONNECTED,
464         LWSS_RETURNED_CLOSE_ALREADY,
465         LWSS_AWAITING_CLOSE_ACK,
466         LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
467         LWSS_SHUTDOWN,
468
469         LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
470         LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
471         LWSS_HTTP2_ESTABLISHED,
472
473         LWSS_CGI,
474 };
475
476 enum http_version {
477         HTTP_VERSION_1_0,
478         HTTP_VERSION_1_1,
479         HTTP_VERSION_2
480 };
481
482 enum http_connection_type {
483         HTTP_CONNECTION_CLOSE,
484         HTTP_CONNECTION_KEEP_ALIVE
485 };
486
487 enum lws_pending_protocol_send {
488         LWS_PPS_NONE,
489         LWS_PPS_HTTP2_MY_SETTINGS,
490         LWS_PPS_HTTP2_ACK_SETTINGS,
491         LWS_PPS_HTTP2_PONG,
492 };
493
494 enum lws_rx_parse_state {
495         LWS_RXPS_NEW,
496
497         LWS_RXPS_04_mask_1,
498         LWS_RXPS_04_mask_2,
499         LWS_RXPS_04_mask_3,
500
501         LWS_RXPS_04_FRAME_HDR_1,
502         LWS_RXPS_04_FRAME_HDR_LEN,
503         LWS_RXPS_04_FRAME_HDR_LEN16_2,
504         LWS_RXPS_04_FRAME_HDR_LEN16_1,
505         LWS_RXPS_04_FRAME_HDR_LEN64_8,
506         LWS_RXPS_04_FRAME_HDR_LEN64_7,
507         LWS_RXPS_04_FRAME_HDR_LEN64_6,
508         LWS_RXPS_04_FRAME_HDR_LEN64_5,
509         LWS_RXPS_04_FRAME_HDR_LEN64_4,
510         LWS_RXPS_04_FRAME_HDR_LEN64_3,
511         LWS_RXPS_04_FRAME_HDR_LEN64_2,
512         LWS_RXPS_04_FRAME_HDR_LEN64_1,
513
514         LWS_RXPS_07_COLLECT_FRAME_KEY_1,
515         LWS_RXPS_07_COLLECT_FRAME_KEY_2,
516         LWS_RXPS_07_COLLECT_FRAME_KEY_3,
517         LWS_RXPS_07_COLLECT_FRAME_KEY_4,
518
519         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
520 };
521
522
523 enum connection_mode {
524         LWSCM_HTTP_SERVING,
525         LWSCM_HTTP_CLIENT, /* we are client to someone else's server */
526         LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
527         LWSCM_HTTP_CLIENT_ACCEPTED, /* actual HTTP service going on */
528         LWSCM_PRE_WS_SERVING_ACCEPT,
529
530         LWSCM_WS_SERVING,
531         LWSCM_WS_CLIENT,
532
533         LWSCM_HTTP2_SERVING,
534
535         /* transient, ssl delay hiding */
536         LWSCM_SSL_ACK_PENDING,
537         LWSCM_SSL_INIT,
538
539         /* transient modes */
540         LWSCM_WSCL_WAITING_CONNECT,
541         LWSCM_WSCL_WAITING_PROXY_REPLY,
542         LWSCM_WSCL_ISSUE_HANDSHAKE,
543         LWSCM_WSCL_ISSUE_HANDSHAKE2,
544         LWSCM_WSCL_WAITING_SSL,
545         LWSCM_WSCL_WAITING_SERVER_REPLY,
546         LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
547         LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
548
549         /* special internal types */
550         LWSCM_SERVER_LISTENER,
551         LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
552 };
553
554 enum {
555         LWS_RXFLOW_ALLOW = (1 << 0),
556         LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
557 };
558
559 /* this is not usable directly by user code any more, lws_close_reason() */
560 #define LWS_WRITE_CLOSE 4
561
562 struct lws_protocols;
563 struct lws;
564
565 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
566
567 struct lws_io_watcher {
568 #ifdef LWS_USE_LIBEV
569         ev_io ev_watcher;
570 #endif
571 #ifdef LWS_USE_LIBUV
572         uv_poll_t uv_watcher;
573 #endif
574         struct lws_context *context;
575 };
576
577 struct lws_signal_watcher {
578 #ifdef LWS_USE_LIBEV
579         ev_signal ev_watcher;
580 #endif
581 #ifdef LWS_USE_LIBUV
582         uv_signal_t uv_watcher;
583 #endif
584         struct lws_context *context;
585 };
586 #endif
587
588 #ifdef _WIN32
589 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
590 struct lws_fd_hashtable {
591         struct lws **wsi;
592         int length;
593 };
594 #endif
595
596 /*
597  * This is totally opaque to code using the library.  It's exported as a
598  * forward-reference pointer-only declaration; the user can use the pointer with
599  * other APIs to get information out of it.
600  */
601
602 struct lws_fragments {
603         unsigned int offset;
604         unsigned short len;
605         unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
606 };
607
608 /*
609  * these are assigned from a pool held in the context.
610  * Both client and server mode uses them for http header analysis
611  */
612
613 struct allocated_headers {
614         struct lws *wsi; /* owner */
615         char *data; /* prepared by context init to point to dedicated storage */
616         /*
617          * the randomly ordered fragments, indexed by frag_index and
618          * lws_fragments->nfrag for continuation.
619          */
620         struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
621         time_t assigned;
622         /*
623          * for each recognized token, frag_index says which frag[] his data
624          * starts in (0 means the token did not appear)
625          * the actual header data gets dumped as it comes in, into data[]
626          */
627         unsigned char frag_index[WSI_TOKEN_COUNT];
628         unsigned char rx[2048];
629         unsigned int rxpos;
630         unsigned int rxlen;
631         unsigned int pos;
632
633 #ifndef LWS_NO_CLIENT
634         char initial_handshake_hash_base64[30];
635 #endif
636
637         unsigned char in_use;
638         unsigned char nfrag;
639 };
640
641 /*
642  * so we can have n connections being serviced simultaneously,
643  * these things need to be isolated per-thread.
644  */
645
646 struct lws_context_per_thread {
647 #if LWS_MAX_SMP > 1
648         pthread_mutex_t lock;
649 #endif
650         struct lws_pollfd *fds;
651 #if defined(LWS_WITH_ESP8266)
652         struct lws **lws_vs_fds_index;
653 #endif
654         struct lws *rx_draining_ext_list;
655         struct lws *tx_draining_ext_list;
656         struct lws *timeout_list;
657         struct lws_context *context;
658 #ifdef LWS_WITH_CGI
659         struct lws_cgi *cgi_list;
660 #endif
661         void *http_header_data;
662         struct allocated_headers *ah_pool;
663         struct lws *ah_wait_list;
664         int ah_wait_list_length;
665 #ifdef LWS_OPENSSL_SUPPORT
666         struct lws *pending_read_list; /* linked list */
667 #endif
668 #ifndef LWS_NO_SERVER
669         struct lws *wsi_listening;
670 #endif
671 #if defined(LWS_USE_LIBEV)
672         struct ev_loop *io_loop_ev;
673 #endif
674 #if defined(LWS_USE_LIBUV)
675         uv_loop_t *io_loop_uv;
676         uv_signal_t signals[8];
677         uv_timer_t uv_timeout_watcher;
678         uv_idle_t uv_idle;
679 #endif
680 #if defined(LWS_USE_LIBEV)
681         struct lws_io_watcher w_accept;
682 #endif
683 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
684         struct lws_signal_watcher w_sigint;
685         unsigned char ev_loop_foreign:1;
686 #endif
687
688         unsigned long count_conns;
689         /*
690          * usable by anything in the service code, but only if the scope
691          * does not last longer than the service action (since next service
692          * of any socket can likewise use it and overwrite)
693          */
694         unsigned char *serv_buf;
695 #ifdef _WIN32
696         WSAEVENT *events;
697 #else
698         lws_sockfd_type dummy_pipe_fds[2];
699 #endif
700         unsigned int fds_count;
701
702         short ah_count_in_use;
703         unsigned char tid;
704         unsigned char lock_depth;
705 };
706
707 /*
708  * virtual host -related context information
709  *   vhostwide SSL context
710  *   vhostwide proxy
711  *
712  * heirarchy:
713  *
714  * context -> vhost -> wsi
715  *
716  * incoming connection non-SSL vhost binding:
717  *
718  *    listen socket -> wsi -> select vhost after first headers
719  *
720  * incoming connection SSL vhost binding:
721  *
722  *    SSL SNI -> wsi -> bind after SSL negotiation
723  */
724
725 struct lws_vhost {
726 #if !defined(LWS_WITH_ESP8266)
727         char http_proxy_address[128];
728         char proxy_basic_auth_token[128];
729 #endif
730 #if defined(LWS_WITH_ESP8266)
731         /* listen sockets need a place to hang their hat */
732         esp_tcp tcp;
733 #endif
734         struct lws_context *context;
735         struct lws_vhost *vhost_next;
736         const struct lws_http_mount *mount_list;
737         struct lws *lserv_wsi;
738         const char *name;
739         const char *iface;
740         const struct lws_protocols *protocols;
741         void **protocol_vh_privs;
742         const struct lws_protocol_vhost_options *pvo;
743         struct lws **same_vh_protocol_list;
744 #ifdef LWS_OPENSSL_SUPPORT
745         SSL_CTX *ssl_ctx;
746         SSL_CTX *ssl_client_ctx;
747 #endif
748 #ifndef LWS_NO_EXTENSIONS
749         const struct lws_extension *extensions;
750 #endif
751         unsigned long long rx, tx;
752         unsigned long conn, trans, ws_upgrades, http2_upgrades;
753
754         int listen_port;
755         unsigned int http_proxy_port;
756         unsigned int options;
757         int count_protocols;
758         int ka_time;
759         int ka_probes;
760         int ka_interval;
761         int keepalive_timeout;
762 #ifdef LWS_WITH_ACCESS_LOG
763         int log_fd;
764 #endif
765
766 #ifdef LWS_OPENSSL_SUPPORT
767         int use_ssl;
768         int allow_non_ssl_on_ssl_port;
769         unsigned int user_supplied_ssl_ctx:1;
770 #endif
771
772         unsigned char default_protocol_index;
773 };
774
775 /*
776  * the rest is managed per-context, that includes
777  *
778  *  - processwide single fd -> wsi lookup
779  *  - contextwide headers pool
780  */
781
782 struct lws_context {
783         time_t last_timeout_check_s;
784         time_t last_ws_ping_pong_check_s;
785         time_t time_up;
786         struct lws_plat_file_ops fops;
787         struct lws_context_per_thread pt[LWS_MAX_SMP];
788 #ifdef _WIN32
789 /* different implementation between unix and windows */
790         struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
791 #else
792 #if defined(LWS_WITH_ESP8266)
793         struct espconn **connpool; /* .reverse points to the wsi */
794         void *rxd;
795         int rxd_len;
796         os_timer_t to_timer;
797 #else
798         struct lws **lws_lookup;  /* fd to wsi */
799 #endif
800 #endif
801         struct lws_vhost *vhost_list;
802         struct lws_plugin *plugin_list;
803         const struct lws_token_limits *token_limits;
804         void *user_space;
805         const char *server_string;
806
807 #if defined(LWS_USE_LIBEV)
808         lws_ev_signal_cb_t * lws_ev_sigint_cb;
809 #endif
810 #if defined(LWS_USE_LIBUV)
811         uv_signal_cb lws_uv_sigint_cb;
812 #endif
813         char canonical_hostname[128];
814 #ifdef LWS_LATENCY
815         unsigned long worst_latency;
816         char worst_latency_info[256];
817 #endif
818
819         int max_fds;
820 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
821         int use_ev_sigint;
822 #endif
823         int started_with_parent;
824         int uid, gid;
825
826         int fd_random;
827 #ifdef LWS_OPENSSL_SUPPORT
828 #define lws_ssl_anybody_has_buffered_read(w) \
829                 (w->vhost->use_ssl && \
830                  w->context->pt[(int)w->tsi].pending_read_list)
831 #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
832                 (/*c->use_ssl && */ \
833                  c->pt[(int)t].pending_read_list)
834 #else
835 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
836 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
837 #endif
838         int count_wsi_allocated;
839         int count_cgi_spawned;
840         unsigned int options;
841         unsigned int fd_limit_per_thread;
842         unsigned int timeout_secs;
843         unsigned int pt_serv_buf_size;
844         int max_http_header_data;
845
846         /*
847          * set to the Thread ID that's doing the service loop just before entry
848          * to poll indicates service thread likely idling in poll()
849          * volatile because other threads may check it as part of processing
850          * for pollfd event change.
851          */
852         volatile int service_tid;
853         int service_tid_detected;
854
855         short max_http_header_pool;
856         short count_threads;
857         short plugin_protocol_count;
858         short plugin_extension_count;
859         short server_string_len;
860         unsigned short ws_ping_pong_interval;
861
862         unsigned int being_destroyed:1;
863         unsigned int requested_kill:1;
864         unsigned int protocol_init_done:1;
865 };
866
867 #define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
868 #define lws_get_vh_protocol(vh, x) vh->protocols[x]
869
870 LWS_EXTERN void
871 lws_close_free_wsi_final(struct lws *wsi);
872 LWS_EXTERN void
873 lws_libuv_closehandle(struct lws *wsi);
874
875 LWS_VISIBLE LWS_EXTERN int
876 lws_plat_plugins_init(struct lws_context * context, const char * const *d);
877
878 LWS_VISIBLE LWS_EXTERN int
879 lws_plat_plugins_destroy(struct lws_context * context);
880
881 LWS_EXTERN void
882 lws_restart_ws_ping_pong_timer(struct lws *wsi);
883
884 struct lws *
885 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
886
887
888 enum {
889         LWS_EV_READ = (1 << 0),
890         LWS_EV_WRITE = (1 << 1),
891         LWS_EV_START = (1 << 2),
892         LWS_EV_STOP = (1 << 3),
893
894         LWS_EV_PREPARE_DELETION = (1 << 31),
895 };
896
897 #if defined(LWS_USE_LIBEV)
898 LWS_EXTERN void
899 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
900 LWS_EXTERN void
901 lws_libev_io(struct lws *wsi, int flags);
902 LWS_EXTERN int
903 lws_libev_init_fd_table(struct lws_context *context);
904 LWS_EXTERN void
905 lws_libev_destroyloop(struct lws_context *context, int tsi);
906 LWS_EXTERN void
907 lws_libev_run(const struct lws_context *context, int tsi);
908 #define LWS_LIBEV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV)
909 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
910 #else
911 #define lws_libev_accept(_a, _b) ((void) 0)
912 #define lws_libev_io(_a, _b) ((void) 0)
913 #define lws_libev_init_fd_table(_a) (0)
914 #define lws_libev_run(_a, _b) ((void) 0)
915 #define lws_libev_destroyloop(_a, _b) ((void) 0)
916 #define LWS_LIBEV_ENABLED(context) (0)
917 #if LWS_POSIX
918 #define lws_feature_status_libev(_a) \
919                         lwsl_notice("libev support not compiled in\n")
920 #else
921 #define lws_feature_status_libev(_a)
922 #endif
923 #endif
924
925 #if defined(LWS_USE_LIBUV)
926 LWS_EXTERN void
927 lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
928 LWS_EXTERN void
929 lws_libuv_io(struct lws *wsi, int flags);
930 LWS_EXTERN int
931 lws_libuv_init_fd_table(struct lws_context *context);
932 LWS_EXTERN void
933 lws_libuv_run(const struct lws_context *context, int tsi);
934 LWS_EXTERN void
935 lws_libuv_destroyloop(struct lws_context *context, int tsi);
936 #define LWS_LIBUV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV)
937 LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
938 #else
939 #define lws_libuv_accept(_a, _b) ((void) 0)
940 #define lws_libuv_io(_a, _b) ((void) 0)
941 #define lws_libuv_init_fd_table(_a) (0)
942 #define lws_libuv_run(_a, _b) ((void) 0)
943 #define lws_libuv_destroyloop(_a, _b) ((void) 0)
944 #define LWS_LIBUV_ENABLED(context) (0)
945 #if LWS_POSIX
946 #define lws_feature_status_libuv(_a) \
947                         lwsl_notice("libuv support not compiled in\n")
948 #else
949 #define lws_feature_status_libuv(_a)
950 #endif
951 #endif
952
953
954 #ifdef LWS_USE_IPV6
955 #define LWS_IPV6_ENABLED(vh) \
956         (!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
957          !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
958 #else
959 #define LWS_IPV6_ENABLED(context) (0)
960 #endif
961
962 #ifdef LWS_USE_UNIX_SOCK
963 #define LWS_UNIX_SOCK_ENABLED(vhost) \
964         (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
965 #else
966 #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
967 #endif
968 enum uri_path_states {
969         URIPS_IDLE,
970         URIPS_SEEN_SLASH,
971         URIPS_SEEN_SLASH_DOT,
972         URIPS_SEEN_SLASH_DOT_DOT,
973 };
974
975 enum uri_esc_states {
976         URIES_IDLE,
977         URIES_SEEN_PERCENT,
978         URIES_SEEN_PERCENT_H1,
979 };
980
981 /* notice that these union members:
982  *
983  *  hdr
984  *  http
985  *  http2
986  *
987  * all have a pointer to allocated_headers struct as their first member.
988  *
989  * It means for allocated_headers access, the three union paths can all be
990  * used interchangeably to access the same data
991  */
992
993
994 #ifndef LWS_NO_CLIENT
995 struct client_info_stash {
996         char address[256];
997         char path[1024];
998         char host[256];
999         char origin[256];
1000         char protocol[256];
1001         char method[16];
1002 };
1003 #endif
1004
1005 struct _lws_header_related {
1006         /* MUST be first in struct */
1007         struct allocated_headers *ah;
1008         struct lws *ah_wait_list;
1009         unsigned char *preamble_rx;
1010 #ifndef LWS_NO_CLIENT
1011         struct client_info_stash *stash;
1012 #endif
1013         unsigned int preamble_rx_len;
1014         enum uri_path_states ups;
1015         enum uri_esc_states ues;
1016         short lextable_pos;
1017         unsigned int current_token_limit;
1018 #ifndef LWS_NO_CLIENT
1019         unsigned short c_port;
1020 #endif
1021         char esc_stash;
1022         char post_literal_equal;
1023         unsigned char parser_state; /* enum lws_token_indexes */
1024         char redirects;
1025 };
1026
1027 struct _lws_http_mode_related {
1028         /* MUST be first in struct */
1029         struct allocated_headers *ah; /* mirroring  _lws_header_related */
1030         struct lws *ah_wait_list;
1031         unsigned char *preamble_rx;
1032 #ifndef LWS_NO_CLIENT
1033         struct client_info_stash *stash;
1034 #endif
1035         unsigned int preamble_rx_len;
1036         struct lws *new_wsi_list;
1037         unsigned long filepos;
1038         unsigned long filelen;
1039         lws_filefd_type fd;
1040
1041         enum http_version request_version;
1042         enum http_connection_type connection_type;
1043         unsigned int content_length;
1044         unsigned int content_remain;
1045 };
1046
1047 #ifdef LWS_USE_HTTP2
1048
1049 enum lws_http2_settings {
1050         LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
1051         LWS_HTTP2_SETTINGS__ENABLE_PUSH,
1052         LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
1053         LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
1054         LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
1055         LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
1056
1057         LWS_HTTP2_SETTINGS__COUNT /* always last */
1058 };
1059
1060 enum lws_http2_wellknown_frame_types {
1061         LWS_HTTP2_FRAME_TYPE_DATA,
1062         LWS_HTTP2_FRAME_TYPE_HEADERS,
1063         LWS_HTTP2_FRAME_TYPE_PRIORITY,
1064         LWS_HTTP2_FRAME_TYPE_RST_STREAM,
1065         LWS_HTTP2_FRAME_TYPE_SETTINGS,
1066         LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
1067         LWS_HTTP2_FRAME_TYPE_PING,
1068         LWS_HTTP2_FRAME_TYPE_GOAWAY,
1069         LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
1070         LWS_HTTP2_FRAME_TYPE_CONTINUATION,
1071
1072         LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
1073 };
1074
1075 enum lws_http2_flags {
1076         LWS_HTTP2_FLAG_END_STREAM = 1,
1077         LWS_HTTP2_FLAG_END_HEADERS = 4,
1078         LWS_HTTP2_FLAG_PADDED = 8,
1079         LWS_HTTP2_FLAG_PRIORITY = 0x20,
1080
1081         LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
1082 };
1083
1084 #define LWS_HTTP2_STREAM_ID_MASTER 0
1085 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
1086 #define LWS_HTTP2_SETTINGS_LENGTH 6
1087
1088 struct http2_settings {
1089         unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
1090 };
1091
1092 enum http2_hpack_state {
1093
1094         /* optional before first header block */
1095         HPKS_OPT_PADDING,
1096         HKPS_OPT_E_DEPENDENCY,
1097         HKPS_OPT_WEIGHT,
1098
1099         /* header block */
1100         HPKS_TYPE,
1101
1102         HPKS_IDX_EXT,
1103
1104         HPKS_HLEN,
1105         HPKS_HLEN_EXT,
1106
1107         HPKS_DATA,
1108
1109         /* optional after last header block */
1110         HKPS_OPT_DISCARD_PADDING,
1111 };
1112
1113 enum http2_hpack_type {
1114         HPKT_INDEXED_HDR_7,
1115         HPKT_INDEXED_HDR_6_VALUE_INCR,
1116         HPKT_LITERAL_HDR_VALUE_INCR,
1117         HPKT_INDEXED_HDR_4_VALUE,
1118         HPKT_LITERAL_HDR_VALUE,
1119         HPKT_SIZE_5
1120 };
1121
1122 struct hpack_dt_entry {
1123         int token; /* additions that don't map to a token are ignored */
1124         int arg_offset;
1125         int arg_len;
1126 };
1127
1128 struct hpack_dynamic_table {
1129         struct hpack_dt_entry *entries;
1130         char *args;
1131         int pos;
1132         int next;
1133         int num_entries;
1134         int args_length;
1135 };
1136
1137 struct _lws_http2_related {
1138         /*
1139          * having this first lets us also re-use all HTTP union code
1140          * and in turn, http_mode_related has allocated headers in right
1141          * place so we can use the header apis on the wsi directly still
1142          */
1143         struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
1144
1145         struct http2_settings my_settings;
1146         struct http2_settings peer_settings;
1147
1148         struct lws *parent_wsi;
1149         struct lws *next_child_wsi;
1150
1151         struct hpack_dynamic_table *hpack_dyn_table;
1152         struct lws *stream_wsi;
1153         unsigned char ping_payload[8];
1154         unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
1155
1156         unsigned int count;
1157         unsigned int length;
1158         unsigned int stream_id;
1159         enum http2_hpack_state hpack;
1160         enum http2_hpack_type hpack_type;
1161         unsigned int header_index;
1162         unsigned int hpack_len;
1163         unsigned int hpack_e_dep;
1164         int tx_credit;
1165         unsigned int my_stream_id;
1166         unsigned int child_count;
1167         int my_priority;
1168
1169         unsigned int END_STREAM:1;
1170         unsigned int END_HEADERS:1;
1171         unsigned int send_END_STREAM:1;
1172         unsigned int GOING_AWAY;
1173         unsigned int requested_POLLOUT:1;
1174         unsigned int waiting_tx_credit:1;
1175         unsigned int huff:1;
1176         unsigned int value:1;
1177
1178         unsigned short round_robin_POLLOUT;
1179         unsigned short count_POLLOUT_children;
1180         unsigned short hpack_pos;
1181
1182         unsigned char type;
1183         unsigned char flags;
1184         unsigned char frame_state;
1185         unsigned char padding;
1186         unsigned char hpack_m;
1187         unsigned char initialized;
1188 };
1189
1190 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
1191
1192 #endif
1193
1194 struct _lws_websocket_related {
1195         /* cheapest way to deal with ah overlap with ws union transition */
1196         struct _lws_header_related hdr;
1197         char *rx_ubuf;
1198         unsigned int rx_ubuf_alloc;
1199         struct lws *rx_draining_ext_list;
1200         struct lws *tx_draining_ext_list;
1201         time_t time_next_ping_check;
1202         size_t rx_packet_length;
1203         unsigned int rx_ubuf_head;
1204         unsigned char mask[4];
1205         /* Also used for close content... control opcode == < 128 */
1206         unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
1207
1208         unsigned char ping_payload_len;
1209         unsigned char mask_idx;
1210         unsigned char opcode;
1211         unsigned char rsv;
1212         unsigned char rsv_first_msg;
1213         /* zero if no info, or length including 2-byte close code */
1214         unsigned char close_in_ping_buffer_len;
1215         unsigned char utf8;
1216         unsigned char stashed_write_type;
1217         unsigned char tx_draining_stashed_wp;
1218
1219         unsigned int final:1;
1220         unsigned int frame_is_binary:1;
1221         unsigned int all_zero_nonce:1;
1222         unsigned int this_frame_masked:1;
1223         unsigned int inside_frame:1; /* next write will be more of frame */
1224         unsigned int clean_buffer:1; /* buffer not rewritten by extension */
1225         unsigned int payload_is_close:1; /* process as PONG, but it is close */
1226         unsigned int ping_pending_flag:1;
1227         unsigned int continuation_possible:1;
1228         unsigned int owed_a_fin:1;
1229         unsigned int check_utf8:1;
1230         unsigned int defeat_check_utf8:1;
1231         unsigned int pmce_compressed_message:1;
1232         unsigned int stashed_write_pending:1;
1233         unsigned int rx_draining_ext:1;
1234         unsigned int tx_draining_ext:1;
1235         unsigned int send_check_ping:1;
1236 };
1237
1238 #ifdef LWS_WITH_CGI
1239
1240 /* wsi who is master of the cgi points to an lws_cgi */
1241
1242 struct lws_cgi {
1243         struct lws_cgi *cgi_list;
1244         struct lws *stdwsi[3]; /* points to the associated stdin/out/err wsis */
1245         struct lws *wsi; /* owner */
1246         unsigned long content_length;
1247         unsigned long content_length_seen;
1248         int pipe_fds[3][2];
1249         int pid;
1250
1251         unsigned int being_closed:1;
1252 };
1253 #endif
1254
1255 signed char char_to_hex(const char c);
1256
1257 #ifndef LWS_NO_CLIENT
1258 enum lws_chunk_parser {
1259         ELCP_HEX,
1260         ELCP_CR,
1261         ELCP_CONTENT,
1262         ELCP_POST_CR,
1263         ELCP_POST_LF,
1264 };
1265 #endif
1266
1267 struct lws_rewrite;
1268
1269 #ifdef LWS_WITH_ACCESS_LOG
1270 struct lws_access_log {
1271         char *header_log;
1272         char *user_agent;
1273         unsigned long sent;
1274         int response;
1275 };
1276 #endif
1277
1278 struct lws {
1279
1280         /* structs */
1281         /* members with mutually exclusive lifetimes are unionized */
1282
1283         union u {
1284                 struct _lws_http_mode_related http;
1285 #ifdef LWS_USE_HTTP2
1286                 struct _lws_http2_related http2;
1287 #endif
1288                 struct _lws_header_related hdr;
1289                 struct _lws_websocket_related ws;
1290         } u;
1291
1292         /* lifetime members */
1293
1294 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
1295         struct lws_io_watcher w_read;
1296 #endif
1297 #if defined(LWS_USE_LIBEV)
1298         struct lws_io_watcher w_write;
1299 #endif
1300         time_t pending_timeout_limit;
1301
1302         /* pointers */
1303
1304         struct lws_context *context;
1305         struct lws_vhost *vhost;
1306         struct lws *parent; /* points to parent, if any */
1307         struct lws *child_list; /* points to first child */
1308         struct lws *sibling_list; /* subsequent children at same level */
1309 #ifdef LWS_WITH_CGI
1310         struct lws_cgi *cgi; /* wsi being cgi master have one of these */
1311 #endif
1312         const struct lws_protocols *protocol;
1313         struct lws **same_vh_protocol_prev, *same_vh_protocol_next;
1314         struct lws *timeout_list;
1315         struct lws **timeout_list_prev;
1316 #ifdef LWS_WITH_ACCESS_LOG
1317         struct lws_access_log access_log;
1318 #endif
1319         void *user_space;
1320         /* rxflow handling */
1321         unsigned char *rxflow_buffer;
1322         /* truncated send handling */
1323         unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
1324
1325 #if defined (LWS_WITH_ESP8266)
1326         void *premature_rx;
1327         unsigned short prem_rx_size, prem_rx_pos;
1328 #endif
1329
1330 #ifndef LWS_NO_EXTENSIONS
1331         const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
1332         void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
1333 #endif
1334 #ifdef LWS_OPENSSL_SUPPORT
1335         SSL *ssl;
1336 #if !defined(LWS_USE_POLARSSL) && !defined(LWS_USE_MBEDTLS)
1337         BIO *client_bio;
1338 #endif
1339         struct lws *pending_read_list_prev, *pending_read_list_next;
1340 #endif
1341 #ifdef LWS_WITH_HTTP_PROXY
1342         struct lws_rewrite *rw;
1343 #endif
1344 #ifdef LWS_LATENCY
1345         unsigned long action_start;
1346         unsigned long latency_start;
1347 #endif
1348         /* pointer / int */
1349         lws_sockfd_type sock;
1350
1351         /* ints */
1352         int position_in_fds_table;
1353         int rxflow_len;
1354         int rxflow_pos;
1355         unsigned int trunc_alloc_len; /* size of malloc */
1356         unsigned int trunc_offset; /* where we are in terms of spilling */
1357         unsigned int trunc_len; /* how much is buffered */
1358 #ifndef LWS_NO_CLIENT
1359         int chunk_remaining;
1360 #endif
1361         unsigned int cache_secs;
1362
1363         unsigned int hdr_parsing_completed:1;
1364         unsigned int http2_substream:1;
1365         unsigned int listener:1;
1366         unsigned int user_space_externally_allocated:1;
1367         unsigned int socket_is_permanently_unusable:1;
1368         unsigned int rxflow_change_to:2;
1369         unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
1370         unsigned int conn_stat_done:1;
1371         unsigned int cache_reuse:1;
1372         unsigned int cache_revalidate:1;
1373         unsigned int cache_intermediaries:1;
1374         unsigned int favoured_pollin:1;
1375         unsigned int sending_chunked:1;
1376         unsigned int already_did_cce:1;
1377 #if defined(LWS_WITH_ESP8266)
1378         unsigned int pending_send_completion:3;
1379         unsigned int close_is_pending_send_completion:1;
1380 #endif
1381 #ifdef LWS_WITH_ACCESS_LOG
1382         unsigned int access_log_pending:1;
1383 #endif
1384 #ifndef LWS_NO_CLIENT
1385         unsigned int do_ws:1; /* whether we are doing http or ws flow */
1386         unsigned int chunked:1; /* if the clientside connection is chunked */
1387         unsigned int client_rx_avail:1;
1388 #endif
1389 #ifdef LWS_WITH_HTTP_PROXY
1390         unsigned int perform_rewrite:1;
1391 #endif
1392 #ifndef LWS_NO_EXTENSIONS
1393         unsigned int extension_data_pending:1;
1394 #endif
1395 #ifdef LWS_OPENSSL_SUPPORT
1396         unsigned int use_ssl:3;
1397         unsigned int upgraded:1;
1398 #endif
1399 #ifdef _WIN32
1400         unsigned int sock_send_blocking:1;
1401 #endif
1402 #ifdef LWS_OPENSSL_SUPPORT
1403         unsigned int redirect_to_https:1;
1404 #endif
1405
1406         /* chars */
1407 #ifndef LWS_NO_EXTENSIONS
1408         unsigned char count_act_ext;
1409 #endif
1410         unsigned char ietf_spec_revision;
1411         char mode; /* enum connection_mode */
1412         char state; /* enum lws_connection_states */
1413         char state_pre_close;
1414         char lws_rx_parse_state; /* enum lws_rx_parse_state */
1415         char rx_frame_type; /* enum lws_write_protocol */
1416         char pending_timeout; /* enum pending_timeout */
1417         char pps; /* enum lws_pending_protocol_send */
1418         char tsi; /* thread service index we belong to */
1419         char protocol_interpret_idx;
1420 #ifdef LWS_WITH_CGI
1421         char cgi_channel; /* which of stdin/out/err */
1422         char hdr_state;
1423 #endif
1424 #ifndef LWS_NO_CLIENT
1425         char chunk_parser; /* enum lws_chunk_parser */
1426 #endif
1427 #if defined(LWS_WITH_CGI) || !defined(LWS_NO_CLIENT)
1428         char reason_bf; /* internal writeable callback reason bitfield */
1429 #endif
1430 };
1431
1432 LWS_EXTERN int log_level;
1433
1434 LWS_EXTERN int
1435 lws_socket_bind(struct lws_vhost *vhost, int sockfd, int port,
1436                 const char *iface);
1437
1438 LWS_EXTERN void
1439 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1440
1441 LWS_EXTERN int
1442 remove_wsi_socket_from_fds(struct lws *wsi);
1443 LWS_EXTERN int
1444 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1445
1446 #ifndef LWS_LATENCY
1447 static inline void
1448 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1449             int ret, int completion) {
1450         do {
1451                 (void)context; (void)wsi; (void)action; (void)ret;
1452                 (void)completion;
1453         } while (0);
1454 }
1455 static inline void
1456 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1457         do { (void)context; (void)wsi; } while (0);
1458 }
1459 #else
1460 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1461 extern void
1462 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1463             int ret, int completion);
1464 #endif
1465
1466 LWS_EXTERN void
1467 lws_set_protocol_write_pending(struct lws *wsi,
1468                                enum lws_pending_protocol_send pend);
1469 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1470 lws_client_rx_sm(struct lws *wsi, unsigned char c);
1471
1472 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1473 lws_parse(struct lws *wsi, unsigned char c);
1474
1475 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1476 lws_http_action(struct lws *wsi);
1477
1478 LWS_EXTERN int
1479 lws_b64_selftest(void);
1480
1481 LWS_EXTERN int
1482 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
1483
1484 LWS_EXTERN int
1485 lws_service_flag_pending(struct lws_context *context, int tsi);
1486
1487 #if defined(_WIN32) || defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
1488 LWS_EXTERN struct lws *
1489 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
1490
1491 LWS_EXTERN int
1492 insert_wsi(struct lws_context *context, struct lws *wsi);
1493
1494 LWS_EXTERN int
1495 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
1496 #else
1497 #define wsi_from_fd(A,B)  A->lws_lookup[B]
1498 #define insert_wsi(A,B)   assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
1499 #define delete_from_fd(A,B) A->lws_lookup[B]=0
1500 #endif
1501
1502 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1503 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1504
1505 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1506 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1507
1508
1509 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1510 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1511
1512 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1513 lws_client_connect_2(struct lws *wsi);
1514
1515 LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1516 lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
1517                  const char *path, const char *host);
1518
1519 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1520 lws_create_new_server_wsi(struct lws_vhost *vhost);
1521
1522 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1523 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1524
1525 LWS_EXTERN int
1526 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1527
1528 LWS_EXTERN struct lws *
1529 lws_client_connect_via_info2(struct lws *wsi);
1530
1531 /*
1532  * EXTENSIONS
1533  */
1534
1535 #ifndef LWS_NO_EXTENSIONS
1536 LWS_VISIBLE void
1537 lws_context_init_extensions(struct lws_context_creation_info *info,
1538                             struct lws_context *context);
1539 LWS_EXTERN int
1540 lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
1541                           void *v, size_t len);
1542
1543 LWS_EXTERN int
1544 lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
1545 LWS_EXTERN int
1546 lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1547                     void *arg, int len);
1548
1549 #else
1550 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1551 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1552 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1553 #define lws_issue_raw_ext_access lws_issue_raw
1554 #define lws_context_init_extensions(_a, _b)
1555 #endif
1556
1557 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1558 lws_client_interpret_server_handshake(struct lws *wsi);
1559
1560 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1561 lws_rx_sm(struct lws *wsi, unsigned char c);
1562
1563 LWS_EXTERN void
1564 lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
1565
1566 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1567 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1568
1569 LWS_EXTERN void
1570 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1571
1572 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1573 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1574                             enum lws_callback_reasons reason, void *user,
1575                             void *in, size_t len);
1576 #ifdef LWS_USE_HTTP2
1577 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1578 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1579 LWS_EXTERN int
1580 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1581                                      unsigned char *buf, int len);
1582 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1583 LWS_EXTERN int
1584 lws_http2_parser(struct lws *wsi, unsigned char c);
1585 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1586                                      struct lws *wsi);
1587 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1588                                      unsigned int sid, unsigned int len,
1589                                      unsigned char *buf);
1590 LWS_EXTERN struct lws *
1591 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1592 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1593                                    unsigned char c);
1594 LWS_EXTERN int
1595 lws_add_http2_header_by_name(struct lws *wsi,
1596                              const unsigned char *name,
1597                              const unsigned char *value, int length,
1598                              unsigned char **p, unsigned char *end);
1599 LWS_EXTERN int
1600 lws_add_http2_header_by_token(struct lws *wsi,
1601                             enum lws_token_indexes token,
1602                             const unsigned char *value, int length,
1603                             unsigned char **p, unsigned char *end);
1604 LWS_EXTERN int
1605 lws_add_http2_header_status(struct lws *wsi,
1606                             unsigned int code, unsigned char **p,
1607                             unsigned char *end);
1608 LWS_EXTERN
1609 void lws_http2_configure_if_upgraded(struct lws *wsi);
1610 #else
1611 #define lws_http2_configure_if_upgraded(x)
1612 #endif
1613
1614 LWS_EXTERN int
1615 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd);
1616
1617 LWS_EXTERN int
1618 lws_plat_check_connection_error(struct lws *wsi);
1619
1620 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1621 lws_header_table_attach(struct lws *wsi, int autoservice);
1622
1623 LWS_EXTERN int
1624 lws_header_table_detach(struct lws *wsi, int autoservice);
1625
1626 LWS_EXTERN void
1627 lws_header_table_reset(struct lws *wsi, int autoservice);
1628
1629 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1630 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1631
1632 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1633 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1634
1635 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1636 lws_ensure_user_space(struct lws *wsi);
1637
1638 LWS_EXTERN int
1639 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1640
1641 #ifndef LWS_NO_SERVER
1642 int lws_context_init_server(struct lws_context_creation_info *info,
1643                             struct lws_vhost *vhost);
1644 LWS_EXTERN struct lws_vhost *
1645 lws_select_vhost(struct lws_context *context, int port, const char *servername);
1646 LWS_EXTERN int
1647 handshake_0405(struct lws_context *context, struct lws *wsi);
1648 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1649 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
1650 LWS_EXTERN void
1651 lws_server_get_canonical_hostname(struct lws_context *context,
1652                                   struct lws_context_creation_info *info);
1653 #else
1654 #define lws_context_init_server(_a, _b) (0)
1655 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1656 #define lws_server_get_canonical_hostname(_a, _b)
1657 #endif
1658
1659 #ifndef LWS_NO_DAEMONIZE
1660 LWS_EXTERN int get_daemonize_pid();
1661 #else
1662 #define get_daemonize_pid() (0)
1663 #endif
1664
1665 #if !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
1666 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1667 interface_to_sa(struct lws_vhost *vh, const char *ifname,
1668                 struct sockaddr_in *addr, size_t addrlen);
1669 #endif
1670 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1671
1672 enum lws_ssl_capable_status {
1673         LWS_SSL_CAPABLE_ERROR = -1,
1674         LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1675 };
1676
1677 #ifndef LWS_OPENSSL_SUPPORT
1678 #define LWS_SSL_ENABLED(context) (0)
1679 #define lws_context_init_server_ssl(_a, _b) (0)
1680 #define lws_ssl_destroy(_a)
1681 #define lws_context_init_http2_ssl(_a)
1682 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1683 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1684 #define lws_ssl_pending lws_ssl_pending_no_ssl
1685 #define lws_server_socket_service_ssl(_b, _c) (0)
1686 #define lws_ssl_close(_a) (0)
1687 #define lws_ssl_context_destroy(_a)
1688 #define lws_ssl_SSL_CTX_destroy(_a)
1689 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1690 #define lws_context_init_ssl_library(_a)
1691 #else
1692 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1693 LWS_EXTERN int openssl_websocket_private_data_index;
1694 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1695 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1696 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1697 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1698 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1699 lws_ssl_pending(struct lws *wsi);
1700 LWS_EXTERN int
1701 lws_context_init_ssl_library(struct lws_context_creation_info *info);
1702 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1703 lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
1704 LWS_EXTERN int
1705 lws_ssl_close(struct lws *wsi);
1706 LWS_EXTERN void
1707 lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost);
1708 LWS_EXTERN void
1709 lws_ssl_context_destroy(struct lws_context *context);
1710 LWS_VISIBLE void
1711 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1712 LWS_EXTERN int
1713 lws_ssl_client_bio_create(struct lws *wsi);
1714 LWS_EXTERN int
1715 lws_ssl_client_connect1(struct lws *wsi);
1716 LWS_EXTERN int
1717 lws_ssl_client_connect2(struct lws *wsi);
1718 LWS_EXTERN void
1719 lws_ssl_elaborate_error(void);
1720 #ifndef LWS_NO_SERVER
1721 LWS_EXTERN int
1722 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1723                             struct lws_vhost *vhost);
1724 #else
1725 #define lws_context_init_server_ssl(_a, _b) (0)
1726 #endif
1727 LWS_EXTERN void
1728 lws_ssl_destroy(struct lws_vhost *vhost);
1729
1730 /* HTTP2-related */
1731
1732 #ifdef LWS_USE_HTTP2
1733 LWS_EXTERN void
1734 lws_context_init_http2_ssl(struct lws_vhost *vhost);
1735 #else
1736 #define lws_context_init_http2_ssl(_a)
1737 #endif
1738 #endif
1739
1740 #if LWS_MAX_SMP > 1
1741 static LWS_INLINE void
1742 lws_pt_mutex_init(struct lws_context_per_thread *pt)
1743 {
1744         pthread_mutex_init(&pt->lock, NULL);
1745 }
1746
1747 static LWS_INLINE void
1748 lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
1749 {
1750         pthread_mutex_destroy(&pt->lock);
1751 }
1752
1753 static LWS_INLINE void
1754 lws_pt_lock(struct lws_context_per_thread *pt)
1755 {
1756         if (!pt->lock_depth++)
1757                 pthread_mutex_lock(&pt->lock);
1758 }
1759
1760 static LWS_INLINE void
1761 lws_pt_unlock(struct lws_context_per_thread *pt)
1762 {
1763         if (!(--pt->lock_depth))
1764                 pthread_mutex_unlock(&pt->lock);
1765 }
1766 #else
1767 #define lws_pt_mutex_init(_a) (void)(_a)
1768 #define lws_pt_mutex_destroy(_a) (void)(_a)
1769 #define lws_pt_lock(_a) (void)(_a)
1770 #define lws_pt_unlock(_a) (void)(_a)
1771 #endif
1772
1773 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1774 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1775
1776 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1777 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1778
1779 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1780 lws_ssl_pending_no_ssl(struct lws *wsi);
1781
1782 #ifdef LWS_WITH_HTTP_PROXY
1783 struct lws_rewrite {
1784         hubbub_parser *parser;
1785         hubbub_parser_optparams params;
1786         const char *from, *to;
1787         int from_len, to_len;
1788         unsigned char *p, *end;
1789         struct lws *wsi;
1790 };
1791 static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
1792 {
1793         if (s->len != len)
1794                 return 1;
1795
1796         return strncmp((const char *)s->ptr, p, len);
1797 }
1798 typedef hubbub_error (*hubbub_callback_t)(const hubbub_token *token, void *pw);
1799 LWS_EXTERN struct lws_rewrite *
1800 lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from, const char *to);
1801 LWS_EXTERN void
1802 lws_rewrite_destroy(struct lws_rewrite *r);
1803 LWS_EXTERN int
1804 lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
1805 #endif
1806
1807 #ifndef LWS_NO_CLIENT
1808 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1809                                          struct lws *wsi,
1810                                          struct lws_pollfd *pollfd);
1811 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1812 lws_http_transaction_completed_client(struct lws *wsi);
1813 #ifdef LWS_OPENSSL_SUPPORT
1814 LWS_EXTERN int
1815 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1816                             struct lws_vhost *vhost);
1817 #else
1818         #define lws_context_init_client_ssl(_a, _b) (0)
1819 #endif
1820 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1821 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1822 LWS_EXTERN void
1823 lws_decode_ssl_error(void);
1824 #else
1825 #define lws_context_init_client_ssl(_a, _b) (0)
1826 #define lws_handshake_client(_a, _b, _c) (0)
1827 #endif
1828
1829 LWS_EXTERN int
1830 _lws_rx_flow_control(struct lws *wsi);
1831
1832 LWS_EXTERN int
1833 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1834
1835 #ifndef LWS_NO_SERVER
1836 LWS_EXTERN int
1837 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1838                           struct lws_pollfd *pollfd);
1839 LWS_EXTERN int
1840 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1841 LWS_EXTERN int
1842 _lws_server_listen_accept_flow_control(struct lws *twsi, int on);
1843 #else
1844 #define lws_server_socket_service(_a, _b, _c) (0)
1845 #define lws_handshake_server(_a, _b, _c) (0)
1846 #define _lws_server_listen_accept_flow_control(a, b) (0)
1847 #endif
1848
1849 #ifdef LWS_WITH_ACCESS_LOG
1850 LWS_EXTERN int
1851 lws_access_log(struct lws *wsi);
1852 #else
1853 #define lws_access_log(_a)
1854 #endif
1855
1856 LWS_EXTERN int
1857 lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1858
1859 int
1860 lws_protocol_init(struct lws_context *context);
1861
1862 int
1863 lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p);
1864
1865 const struct lws_http_mount *
1866 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
1867
1868 /*
1869  * custom allocator
1870  */
1871 LWS_EXTERN void *
1872 lws_realloc(void *ptr, size_t size);
1873
1874 LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
1875 lws_zalloc(size_t size);
1876
1877 #define lws_malloc(S)   lws_realloc(NULL, S)
1878 #define lws_free(P)     lws_realloc(P, 0)
1879 #define lws_free_set_NULL(P)    do { lws_realloc(P, 0); (P) = NULL; } while(0)
1880
1881 /* lws_plat_ */
1882 LWS_EXTERN void
1883 lws_plat_delete_socket_from_fds(struct lws_context *context,
1884                                 struct lws *wsi, int m);
1885 LWS_EXTERN void
1886 lws_plat_insert_socket_into_fds(struct lws_context *context,
1887                                 struct lws *wsi);
1888 LWS_EXTERN void
1889 lws_plat_service_periodic(struct lws_context *context);
1890
1891 LWS_EXTERN int
1892 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1893                        struct lws_pollfd *pfd);
1894 LWS_EXTERN int
1895 lws_plat_context_early_init(void);
1896 LWS_EXTERN void
1897 lws_plat_context_early_destroy(struct lws_context *context);
1898 LWS_EXTERN void
1899 lws_plat_context_late_destroy(struct lws_context *context);
1900 LWS_EXTERN int
1901 lws_poll_listen_fd(struct lws_pollfd *fd);
1902 LWS_EXTERN int
1903 lws_plat_service(struct lws_context *context, int timeout_ms);
1904 LWS_EXTERN LWS_VISIBLE int
1905 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1906 LWS_EXTERN int
1907 lws_plat_init(struct lws_context *context,
1908               struct lws_context_creation_info *info);
1909 LWS_EXTERN void
1910 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1911 LWS_EXTERN unsigned long long
1912 time_in_microseconds(void);
1913 LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
1914 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1915
1916 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1917 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
1918
1919 #ifdef __cplusplus
1920 };
1921 #endif