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