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