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