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