uridecoding disallow uriencoded equals in name part
[platform/upstream/libwebsockets.git] / lib / private-libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2015 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 #ifdef LWS_HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <stdarg.h>
36 #include <assert.h>
37
38 #ifdef LWS_HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41
42 #if defined(WIN32) || defined(_WIN32)
43 #if (WINVER < 0x0501)
44 #undef WINVER
45 #undef _WIN32_WINNT
46 #define WINVER 0x0501
47 #define _WIN32_WINNT WINVER
48 #endif
49 #define LWS_NO_DAEMONIZE
50 #define LWS_ERRNO WSAGetLastError()
51 #define LWS_EAGAIN WSAEWOULDBLOCK
52 #define LWS_EALREADY WSAEALREADY
53 #define LWS_EINPROGRESS WSAEINPROGRESS
54 #define LWS_EINTR WSAEINTR
55 #define LWS_EISCONN WSAEISCONN
56 #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
57 #define LWS_POLLHUP (FD_CLOSE)
58 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
59 #define LWS_POLLOUT (FD_WRITE)
60 #define MSG_NOSIGNAL 0
61 #define SHUT_RDWR SD_BOTH
62 #define SOL_TCP IPPROTO_TCP
63
64 #define compatible_close(fd) closesocket(fd)
65 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = TRUE
66 #define lws_socket_is_valid(x) (!!x)
67 #define LWS_SOCK_INVALID 0
68 #include <winsock2.h>
69 #include <ws2tcpip.h>
70 #include <windows.h>
71 #include <tchar.h>
72 #ifdef LWS_HAVE_IN6ADDR_H
73 #include <in6addr.h>
74 #endif
75 #include <mstcpip.h>
76
77 #ifndef __func__
78 #define __func__ __FUNCTION__
79 #endif
80
81 #ifdef _WIN32_WCE
82 #define vsnprintf _vsnprintf
83 #endif
84
85 #else /* not windows --> */
86
87 #include <fcntl.h>
88 #include <strings.h>
89 #include <unistd.h>
90 #include <sys/types.h>
91 #ifndef MBED_OPERATORS
92 #ifndef __cplusplus
93 #include <errno.h>
94 #endif
95 #include <netdb.h>
96 #include <signal.h>
97 #include <sys/socket.h>
98 #ifdef LWS_BUILTIN_GETIFADDRS
99  #include <getifaddrs.h>
100 #else
101  #include <ifaddrs.h>
102 #endif
103 #if defined (__ANDROID__)
104 #include <syslog.h>
105 #else
106 #include <sys/syslog.h>
107 #endif
108 #include <sys/un.h>
109 #include <sys/socket.h>
110 #include <netdb.h>
111 #include <netinet/in.h>
112 #include <netinet/tcp.h>
113 #include <arpa/inet.h>
114 #include <poll.h>
115 #ifdef LWS_USE_LIBEV
116 #include <ev.h>
117 #endif /* LWS_USE_LIBEV */
118 #include <sys/mman.h>
119
120 #endif /* MBED */
121
122 #ifndef LWS_NO_FORK
123 #ifdef LWS_HAVE_SYS_PRCTL_H
124 #include <sys/prctl.h>
125 #endif
126 #endif
127
128 #include <sys/time.h>
129
130 #define LWS_ERRNO errno
131 #define LWS_EAGAIN EAGAIN
132 #define LWS_EALREADY EALREADY
133 #define LWS_EINPROGRESS EINPROGRESS
134 #define LWS_EINTR EINTR
135 #define LWS_EISCONN EISCONN
136 #define LWS_EWOULDBLOCK EWOULDBLOCK
137 #define LWS_POLLHUP (POLLHUP|POLLERR)
138 #define LWS_POLLIN (POLLIN)
139 #define LWS_POLLOUT (POLLOUT)
140 #define compatible_close(fd) close(fd)
141 #define lws_set_blocking_send(wsi)
142
143 #ifdef MBED_OPERATORS
144 #define lws_socket_is_valid(x) ((x) != NULL)
145 #define LWS_SOCK_INVALID (NULL)
146 #else
147 #define lws_socket_is_valid(x) (x >= 0)
148 #define LWS_SOCK_INVALID (-1)
149 #endif
150 #endif
151
152 #ifndef LWS_HAVE_BZERO
153 #ifndef bzero
154 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
155 #endif
156 #endif
157
158 #ifndef LWS_HAVE_STRERROR
159 #define strerror(x) ""
160 #endif
161
162 #ifdef LWS_OPENSSL_SUPPORT
163 #ifdef USE_WOLFSSL
164 #ifdef USE_OLD_CYASSL
165 #include <cyassl/openssl/ssl.h>
166 #include <cyassl/error-ssl.h>
167 #else
168 #include <wolfssl/openssl/ssl.h>
169 #include <wolfssl/error-ssl.h>
170 #endif /* not USE_OLD_CYASSL */
171 #else
172 #include <openssl/ssl.h>
173 #include <openssl/evp.h>
174 #include <openssl/err.h>
175 #include <openssl/md5.h>
176 #include <openssl/sha.h>
177 #endif /* not USE_WOLFSSL */
178 #endif
179
180 #include "libwebsockets.h"
181
182 #if defined(MBED_OPERATORS)
183 #undef compatible_close
184 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
185 #ifndef BIG_ENDIAN
186 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
187 #endif
188 #ifndef LITTLE_ENDIAN
189 #define LITTLE_ENDIAN 1234
190 #endif
191 #ifndef BYTE_ORDER
192 #define BYTE_ORDER LITTLE_ENDIAN
193 #endif
194 #endif
195
196 #if defined(WIN32) || defined(_WIN32)
197
198 #ifndef BIG_ENDIAN
199 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
200 #endif
201 #ifndef LITTLE_ENDIAN
202 #define LITTLE_ENDIAN 1234
203 #endif
204 #ifndef BYTE_ORDER
205 #define BYTE_ORDER LITTLE_ENDIAN
206 #endif
207 #ifndef u_int64_t
208 typedef unsigned __int64 u_int64_t;
209 #endif
210
211 #undef __P
212 #ifndef __P
213 #if __STDC__
214 #define __P(protos) protos
215 #else
216 #define __P(protos) ()
217 #endif
218 #endif
219
220 #else
221
222 #include <sys/stat.h>
223 #include <sys/cdefs.h>
224 #include <sys/time.h>
225
226 #if defined(__APPLE__)
227 #include <machine/endian.h>
228 #elif defined(__FreeBSD__)
229 #include <sys/endian.h>
230 #elif defined(__linux__)
231 #include <endian.h>
232 #endif
233
234 #ifdef __cplusplus
235 extern "C" {
236 #endif
237 #include <stddef.h>
238
239 #ifndef container_of
240 #define container_of(P,T,M)     ((T *)((char *)(P) - offsetof(T, M)))
241 #endif
242
243 #if defined(__QNX__)
244         #include <gulliver.h>
245         #if defined(__LITTLEENDIAN__)
246                 #define BYTE_ORDER __LITTLEENDIAN__
247                 #define LITTLE_ENDIAN __LITTLEENDIAN__
248                 #define BIG_ENDIAN 4321  /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
249         #endif
250         #if defined(__BIGENDIAN__)
251                 #define BYTE_ORDER __BIGENDIAN__
252                 #define LITTLE_ENDIAN 1234  /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
253                 #define BIG_ENDIAN __BIGENDIAN__
254         #endif
255 #endif
256
257 #if !defined(BYTE_ORDER)
258 # define BYTE_ORDER __BYTE_ORDER
259 #endif
260 #if !defined(LITTLE_ENDIAN)
261 # define LITTLE_ENDIAN __LITTLE_ENDIAN
262 #endif
263 #if !defined(BIG_ENDIAN)
264 # define BIG_ENDIAN __BIG_ENDIAN
265 #endif
266
267 #endif
268
269 /*
270  * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
271  * but happily have something equivalent in the SO_NOSIGPIPE flag.
272  */
273 #ifdef __APPLE__
274 #define MSG_NOSIGNAL SO_NOSIGPIPE
275 #endif
276
277 #ifdef _WIN32
278 #ifndef FD_HASHTABLE_MODULUS
279 #define FD_HASHTABLE_MODULUS 32
280 #endif
281 #endif
282
283 #ifndef LWS_MAX_HEADER_LEN
284 #define LWS_MAX_HEADER_LEN 1024
285 #endif
286 #ifndef LWS_MAX_PROTOCOLS
287 #define LWS_MAX_PROTOCOLS 5
288 #endif
289 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
290 #define LWS_MAX_EXTENSIONS_ACTIVE 3
291 #endif
292 #ifndef SPEC_LATEST_SUPPORTED
293 #define SPEC_LATEST_SUPPORTED 13
294 #endif
295 #ifndef AWAITING_TIMEOUT
296 #define AWAITING_TIMEOUT 5
297 #endif
298 #ifndef CIPHERS_LIST_STRING
299 #define CIPHERS_LIST_STRING "DEFAULT"
300 #endif
301 #ifndef LWS_SOMAXCONN
302 #define LWS_SOMAXCONN SOMAXCONN
303 #endif
304
305 #define MAX_WEBSOCKET_04_KEY_LEN 128
306 #define LWS_MAX_SOCKET_IO_BUF 4096
307
308 #ifndef SYSTEM_RANDOM_FILEPATH
309 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
310 #endif
311 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
312 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
313 #endif
314
315 /*
316  * if not in a connection storm, check for incoming
317  * connections this many normal connection services
318  */
319 #define LWS_lserv_mod 10
320
321 enum lws_websocket_opcodes_07 {
322         LWSWSOPC_CONTINUATION = 0,
323         LWSWSOPC_TEXT_FRAME = 1,
324         LWSWSOPC_BINARY_FRAME = 2,
325
326         LWSWSOPC_NOSPEC__MUX = 7,
327
328         /* control extensions 8+ */
329
330         LWSWSOPC_CLOSE = 8,
331         LWSWSOPC_PING = 9,
332         LWSWSOPC_PONG = 0xa,
333 };
334
335
336 enum lws_connection_states {
337         LWSS_HTTP,
338         LWSS_HTTP_ISSUING_FILE,
339         LWSS_HTTP_HEADERS,
340         LWSS_HTTP_BODY,
341         LWSS_DEAD_SOCKET,
342         LWSS_ESTABLISHED,
343         LWSS_CLIENT_UNCONNECTED,
344         LWSS_RETURNED_CLOSE_ALREADY,
345         LWSS_AWAITING_CLOSE_ACK,
346         LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
347
348         LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
349         LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
350         LWSS_HTTP2_ESTABLISHED,
351 };
352
353 enum http_version {
354         HTTP_VERSION_1_0,
355         HTTP_VERSION_1_1,
356 };
357
358 enum http_connection_type {
359         HTTP_CONNECTION_CLOSE,
360         HTTP_CONNECTION_KEEP_ALIVE
361 };
362
363 enum lws_pending_protocol_send {
364         LWS_PPS_NONE,
365         LWS_PPS_HTTP2_MY_SETTINGS,
366         LWS_PPS_HTTP2_ACK_SETTINGS,
367         LWS_PPS_HTTP2_PONG,
368 };
369
370 enum lws_rx_parse_state {
371         LWS_RXPS_NEW,
372
373         LWS_RXPS_04_MASK_NONCE_1,
374         LWS_RXPS_04_MASK_NONCE_2,
375         LWS_RXPS_04_MASK_NONCE_3,
376
377         LWS_RXPS_04_FRAME_HDR_1,
378         LWS_RXPS_04_FRAME_HDR_LEN,
379         LWS_RXPS_04_FRAME_HDR_LEN16_2,
380         LWS_RXPS_04_FRAME_HDR_LEN16_1,
381         LWS_RXPS_04_FRAME_HDR_LEN64_8,
382         LWS_RXPS_04_FRAME_HDR_LEN64_7,
383         LWS_RXPS_04_FRAME_HDR_LEN64_6,
384         LWS_RXPS_04_FRAME_HDR_LEN64_5,
385         LWS_RXPS_04_FRAME_HDR_LEN64_4,
386         LWS_RXPS_04_FRAME_HDR_LEN64_3,
387         LWS_RXPS_04_FRAME_HDR_LEN64_2,
388         LWS_RXPS_04_FRAME_HDR_LEN64_1,
389
390         LWS_RXPS_07_COLLECT_FRAME_KEY_1,
391         LWS_RXPS_07_COLLECT_FRAME_KEY_2,
392         LWS_RXPS_07_COLLECT_FRAME_KEY_3,
393         LWS_RXPS_07_COLLECT_FRAME_KEY_4,
394
395         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
396 };
397
398
399 enum connection_mode {
400         LWSCM_HTTP_SERVING,
401         LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
402         LWSCM_PRE_WS_SERVING_ACCEPT,
403
404         LWSCM_WS_SERVING,
405         LWSCM_WS_CLIENT,
406
407         LWSCM_HTTP2_SERVING,
408
409         /* transient, ssl delay hiding */
410         LWSCM_SSL_ACK_PENDING,
411
412         /* transient modes */
413         LWSCM_WSCL_WAITING_CONNECT,
414         LWSCM_WSCL_WAITING_PROXY_REPLY,
415         LWSCM_WSCL_ISSUE_HANDSHAKE,
416         LWSCM_WSCL_ISSUE_HANDSHAKE2,
417         LWSCM_WSCL_WAITING_SSL,
418         LWSCM_WSCL_WAITING_SERVER_REPLY,
419         LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
420         LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
421
422         /* special internal types */
423         LWSCM_SERVER_LISTENER,
424 };
425
426 enum {
427         LWS_RXFLOW_ALLOW = (1 << 0),
428         LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
429 };
430
431 struct lws_protocols;
432 struct lws;
433
434 #ifdef LWS_USE_LIBEV
435 struct lws_io_watcher {
436         struct ev_io watcher;
437         struct lws_context* context;
438 };
439
440 struct lws_signal_watcher {
441         struct ev_signal watcher;
442         struct lws_context* context;
443 };
444 #endif /* LWS_USE_LIBEV */
445
446 #ifdef _WIN32
447 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
448 struct lws_fd_hashtable {
449         struct lws **wsi;
450         int length;
451 };
452 #endif
453
454 struct lws_context {
455 #ifdef _WIN32
456         WSAEVENT *events;
457 #endif
458         struct lws_pollfd *fds;
459 #ifdef _WIN32
460 /* different implementation between unix and windows */
461         struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
462 #else
463         struct lws **lws_lookup;  /* fd to wsi */
464 #endif
465         int fds_count;
466 #ifdef LWS_USE_LIBEV
467         struct ev_loop* io_loop;
468         struct lws_io_watcher w_accept;
469         struct lws_signal_watcher w_sigint;
470         lws_ev_signal_cb* lws_ev_sigint_cb;
471         int use_ev_sigint;
472 #endif /* LWS_USE_LIBEV */
473         int max_fds;
474         int listen_port;
475         const char *iface;
476         char http_proxy_address[128];
477         char canonical_hostname[128];
478         char proxy_basic_auth_token[128];
479         unsigned int http_proxy_port;
480         unsigned int options;
481         time_t last_timeout_check_s;
482
483         /*
484          * usable by anything in the service code, but only if the scope
485          * does not last longer than the service action (since next service
486          * of any socket can likewise use it and overwrite)
487          */
488         unsigned char serv_buf[LWS_MAX_SOCKET_IO_BUF];
489
490         int started_with_parent;
491
492         int fd_random;
493         int lserv_mod;
494         int lserv_count;
495         lws_sockfd_type lserv_fd;
496         int lserv_seen;
497
498         /*
499          * set to the Thread ID that's doing the service loop just before entry
500          * to poll indicates service thread likely idling in poll()
501          * volatile because other threads may check it as part of processing
502          * for pollfd event change.
503          */
504         volatile int service_tid;
505 #ifndef _WIN32
506         int dummy_pipe_fds[2];
507 #endif
508
509         int ka_time;
510         int ka_probes;
511         int ka_interval;
512
513 #ifdef LWS_LATENCY
514         unsigned long worst_latency;
515         char worst_latency_info[256];
516 #endif
517
518 #ifdef LWS_OPENSSL_SUPPORT
519         int use_ssl;
520         int allow_non_ssl_on_ssl_port;
521         unsigned int user_supplied_ssl_ctx:1;
522         SSL_CTX *ssl_ctx;
523         SSL_CTX *ssl_client_ctx;
524         struct lws *pending_read_list; /* linked list */
525 #define lws_ssl_anybody_has_buffered_read(ctx) (ctx->use_ssl && ctx->pending_read_list)
526 #else
527 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
528 #endif
529         const struct lws_protocols *protocols;
530         int count_protocols;
531 #ifndef LWS_NO_EXTENSIONS
532         const struct lws_extension *extensions;
533 #endif
534         const struct lws_token_limits *token_limits;
535         void *user_space;
536
537         struct lws_plat_file_ops fops;
538 };
539
540 enum {
541         LWS_EV_READ = (1 << 0),
542         LWS_EV_WRITE = (1 << 1),
543         LWS_EV_START = (1 << 2),
544         LWS_EV_STOP = (1 << 3),
545 };
546
547 #ifdef LWS_USE_LIBEV
548 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
549 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
550 LWS_EXTERN void
551 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
552 LWS_EXTERN void
553 lws_libev_io(struct lws *wsi, int flags);
554 LWS_EXTERN int
555 lws_libev_init_fd_table(struct lws_context *context);
556 LWS_EXTERN void
557 lws_libev_run(const struct lws_context *context);
558 #else
559 #define LWS_LIBEV_ENABLED(context) (0)
560 #ifdef LWS_POSIX
561 #define lws_feature_status_libev(_a) \
562                         lwsl_notice("libev support not compiled in\n")
563 #else
564 #define lws_feature_status_libev(_a)
565 #endif
566 #define lws_libev_accept(_a, _b) ((void) 0)
567 #define lws_libev_io(_a, _b) ((void) 0)
568 #define lws_libev_init_fd_table(_a) (0)
569 #define lws_libev_run(_a) ((void) 0)
570 #endif
571
572 #ifdef LWS_USE_IPV6
573 #define LWS_IPV6_ENABLED(context) \
574         (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
575 #else
576 #define LWS_IPV6_ENABLED(context) (0)
577 #endif
578
579 enum uri_path_states {
580         URIPS_IDLE,
581         URIPS_SEEN_SLASH,
582         URIPS_SEEN_SLASH_DOT,
583         URIPS_SEEN_SLASH_DOT_DOT,
584 };
585
586 enum uri_esc_states {
587         URIES_IDLE,
588         URIES_SEEN_PERCENT,
589         URIES_SEEN_PERCENT_H1,
590 };
591
592 /*
593  * This is totally opaque to code using the library.  It's exported as a
594  * forward-reference pointer-only declaration; the user can use the pointer with
595  * other APIs to get information out of it.
596  */
597
598 struct lws_fragments {
599         unsigned short offset;
600         unsigned short len;
601         unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
602 };
603
604 /* notice that these union members:
605  *
606  *  hdr
607  *  http
608  *  http2
609  *
610  * all have a pointer to allocated_headers struct as their first member.
611  *
612  * It means for allocated_headers access, the three union paths can all be
613  * used interchangeably to access the same data
614  */
615
616 struct allocated_headers {
617         unsigned char nfrag;
618         unsigned short pos;
619         /*
620          * for each recognized token, frag_index says which frag[] his data
621          * starts in (0 means the token did not appear)
622          * the actual header data gets dumped as it comes in, into data[]
623          */
624         unsigned char frag_index[WSI_TOKEN_COUNT];
625         /*
626          * the randomly ordered fragments, indexed by frag_index and
627          * lws_fragments->nfrag for continuation.
628          */
629         struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
630         char data[LWS_MAX_HEADER_LEN];
631
632 #ifndef LWS_NO_CLIENT
633         char initial_handshake_hash_base64[30];
634         unsigned short c_port;
635 #endif
636 };
637
638 struct _lws_http_mode_related {
639         /* MUST be first in struct */
640         struct allocated_headers *ah; /* mirroring  _lws_header_related */
641         lws_filefd_type fd;
642         unsigned long filepos;
643         unsigned long filelen;
644
645         enum http_version request_version;
646         enum http_connection_type connection_type;
647         unsigned int content_length;
648         unsigned int content_remain;
649 };
650
651 #ifdef LWS_USE_HTTP2
652
653 enum lws_http2_settings {
654         LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
655         LWS_HTTP2_SETTINGS__ENABLE_PUSH,
656         LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
657         LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
658         LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
659         LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
660
661         LWS_HTTP2_SETTINGS__COUNT /* always last */
662 };
663
664 enum lws_http2_wellknown_frame_types {
665         LWS_HTTP2_FRAME_TYPE_DATA,
666         LWS_HTTP2_FRAME_TYPE_HEADERS,
667         LWS_HTTP2_FRAME_TYPE_PRIORITY,
668         LWS_HTTP2_FRAME_TYPE_RST_STREAM,
669         LWS_HTTP2_FRAME_TYPE_SETTINGS,
670         LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
671         LWS_HTTP2_FRAME_TYPE_PING,
672         LWS_HTTP2_FRAME_TYPE_GOAWAY,
673         LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
674         LWS_HTTP2_FRAME_TYPE_CONTINUATION,
675
676         LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
677 };
678
679 enum lws_http2_flags {
680         LWS_HTTP2_FLAG_END_STREAM = 1,
681         LWS_HTTP2_FLAG_END_HEADERS = 4,
682         LWS_HTTP2_FLAG_PADDED = 8,
683         LWS_HTTP2_FLAG_PRIORITY = 0x20,
684
685         LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
686 };
687
688 #define LWS_HTTP2_STREAM_ID_MASTER 0
689 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
690 #define LWS_HTTP2_SETTINGS_LENGTH 6
691
692 struct http2_settings {
693         unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
694 };
695
696 enum http2_hpack_state {
697
698         /* optional before first header block */
699         HPKS_OPT_PADDING,
700         HKPS_OPT_E_DEPENDENCY,
701         HKPS_OPT_WEIGHT,
702
703         /* header block */
704         HPKS_TYPE,
705
706         HPKS_IDX_EXT,
707
708         HPKS_HLEN,
709         HPKS_HLEN_EXT,
710
711         HPKS_DATA,
712
713         /* optional after last header block */
714         HKPS_OPT_DISCARD_PADDING,
715 };
716
717 enum http2_hpack_type {
718         HPKT_INDEXED_HDR_7,
719         HPKT_INDEXED_HDR_6_VALUE_INCR,
720         HPKT_LITERAL_HDR_VALUE_INCR,
721         HPKT_INDEXED_HDR_4_VALUE,
722         HPKT_LITERAL_HDR_VALUE,
723         HPKT_SIZE_5
724 };
725
726 struct hpack_dt_entry {
727         int token; /* additions that don't map to a token are ignored */
728         int arg_offset;
729         int arg_len;
730 };
731
732 struct hpack_dynamic_table {
733         struct hpack_dt_entry *entries;
734         char *args;
735         int pos;
736         int next;
737         int num_entries;
738         int args_length;
739 };
740
741 struct _lws_http2_related {
742         /*
743          * having this first lets us also re-use all HTTP union code
744          * and in turn, http_mode_related has allocated headers in right
745          * place so we can use the header apis on the wsi directly still
746          */
747         struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
748
749         struct http2_settings my_settings;
750         struct http2_settings peer_settings;
751
752         struct lws *parent_wsi;
753         struct lws *next_child_wsi;
754
755         struct hpack_dynamic_table *hpack_dyn_table;
756
757         unsigned int count;
758
759         /* frame */
760         unsigned int length;
761         unsigned int stream_id;
762         struct lws *stream_wsi;
763         unsigned char type;
764         unsigned char flags;
765         unsigned char frame_state;
766         unsigned char padding;
767
768         unsigned char ping_payload[8];
769
770         unsigned short round_robin_POLLOUT;
771         unsigned short count_POLLOUT_children;
772
773         unsigned int END_STREAM:1;
774         unsigned int END_HEADERS:1;
775         unsigned int send_END_STREAM:1;
776         unsigned int GOING_AWAY;
777         unsigned int requested_POLLOUT:1;
778         unsigned int waiting_tx_credit:1;
779
780         /* hpack */
781         enum http2_hpack_state hpack;
782         enum http2_hpack_type hpack_type;
783         unsigned int header_index;
784         unsigned int hpack_len;
785         unsigned short hpack_pos;
786         unsigned char hpack_m;
787         unsigned int hpack_e_dep;
788         unsigned int huff:1;
789         unsigned int value:1;
790
791         /* negative credit is mandated by the spec */
792         int tx_credit;
793         unsigned int my_stream_id;
794         unsigned int child_count;
795         int my_priority;
796         unsigned char initialized;
797         unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
798 };
799
800 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
801
802 #endif
803
804 struct _lws_header_related {
805         /* MUST be first in struct */
806         struct allocated_headers *ah;
807         short lextable_pos;
808         unsigned short current_token_limit;
809         unsigned char parser_state; /* enum lws_token_indexes */
810         enum uri_path_states ups;
811         enum uri_esc_states ues;
812         char esc_stash;
813         char post_literal_equal;
814 };
815
816 struct _lws_websocket_related {
817         char *rx_user_buffer;
818         unsigned int rx_user_buffer_head;
819         unsigned char mask_nonce[4];
820         unsigned char frame_mask_index;
821         size_t rx_packet_length;
822         unsigned char opcode;
823         unsigned int final:1;
824         unsigned char rsv;
825         unsigned int frame_is_binary:1;
826         unsigned int all_zero_nonce:1;
827         short close_reason; /* enum lws_close_status */
828
829         unsigned int this_frame_masked:1;
830         unsigned int inside_frame:1; /* next write will be more of frame */
831         unsigned int clean_buffer:1; /* buffer not rewritten by extension */
832         unsigned int payload_is_close:1; /* process as PONG, but it is close */
833
834         unsigned char *ping_payload_buf; /* non-NULL if malloc'd */
835         unsigned int ping_payload_alloc; /* length malloc'd */
836         unsigned int ping_payload_len;
837         unsigned char ping_pending_flag;
838 };
839
840 struct lws {
841
842         /* lifetime members */
843
844 #ifdef LWS_USE_LIBEV
845         struct lws_io_watcher w_read;
846         struct lws_io_watcher w_write;
847 #endif /* LWS_USE_LIBEV */
848         struct lws_context *context;
849         const struct lws_protocols *protocol;
850 #ifndef LWS_NO_EXTENSIONS
851         const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
852         void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
853         unsigned char count_active_extensions;
854         unsigned int extension_data_pending:1;
855 #endif
856         unsigned char ietf_spec_revision;
857         enum lws_pending_protocol_send pps;
858
859         char mode; /* enum connection_mode */
860         char state; /* enum lws_connection_states */
861         char lws_rx_parse_state; /* enum lws_rx_parse_state */
862         char rx_frame_type; /* enum lws_write_protocol */
863
864         unsigned int hdr_parsing_completed:1;
865         unsigned int user_space_externally_allocated:1;
866         unsigned int socket_is_permanently_unusable:1;
867
868         char pending_timeout; /* enum pending_timeout */
869         time_t pending_timeout_limit;
870         lws_sockfd_type sock;
871         int position_in_fds_table;
872 #ifdef LWS_LATENCY
873         unsigned long action_start;
874         unsigned long latency_start;
875 #endif
876         /* rxflow handling */
877         unsigned char *rxflow_buffer;
878         int rxflow_len;
879         int rxflow_pos;
880         unsigned int rxflow_change_to:2;
881
882         /* truncated send handling */
883         unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
884         unsigned int trunc_alloc_len; /* size of malloc */
885         unsigned int trunc_offset; /* where we are in terms of spilling */
886         unsigned int trunc_len; /* how much is buffered */
887
888         void *user_space;
889
890         /* members with mutually exclusive lifetimes are unionized */
891
892         union u {
893                 struct _lws_http_mode_related http;
894 #ifdef LWS_USE_HTTP2
895                 struct _lws_http2_related http2;
896 #endif
897                 struct _lws_header_related hdr;
898                 struct _lws_websocket_related ws;
899         } u;
900
901 #ifdef LWS_OPENSSL_SUPPORT
902         SSL *ssl;
903         BIO *client_bio;
904         struct lws *pending_read_list_prev, *pending_read_list_next;
905         unsigned int use_ssl:2;
906         unsigned int upgraded:1;
907 #endif
908
909 #ifdef _WIN32
910         BOOL sock_send_blocking;
911 #endif
912 };
913
914 LWS_EXTERN int log_level;
915
916 LWS_EXTERN void
917 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
918
919 LWS_EXTERN int
920 remove_wsi_socket_from_fds(struct lws *wsi);
921 LWS_EXTERN int
922 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
923
924 #ifndef LWS_LATENCY
925 static inline void
926 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
927             int ret, int completion) {
928         do {
929                 (void)context; (void)wsi; (void)action; (void)ret;
930                 (void)completion;
931         } while (0);
932 }
933 static inline void
934 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
935         do { (void)context; (void)wsi; } while (0);
936 }
937 #else
938 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
939 extern void
940 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
941             int ret, int completion);
942 #endif
943
944 LWS_EXTERN void
945 lws_set_protocol_write_pending(struct lws *wsi,
946                                enum lws_pending_protocol_send pend);
947 LWS_EXTERN int
948 lws_client_rx_sm(struct lws *wsi, unsigned char c);
949
950 LWS_EXTERN int
951 lws_parse(struct lws *wsi, unsigned char c);
952
953 LWS_EXTERN int
954 lws_http_action(struct lws *wsi);
955
956 LWS_EXTERN int
957 lws_b64_selftest(void);
958
959 #if defined(_WIN32) || defined(MBED_OPERATORS)
960 LWS_EXTERN struct lws *
961 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
962
963 LWS_EXTERN int
964 insert_wsi(struct lws_context *context, struct lws *wsi);
965
966 LWS_EXTERN int
967 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
968 #else
969 #define wsi_from_fd(A,B)  A->lws_lookup[B]
970 #define insert_wsi(A,B)   A->lws_lookup[B->sock]=B
971 #define delete_from_fd(A,B) A->lws_lookup[B]=0
972 #endif
973
974 LWS_EXTERN int
975 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
976
977 LWS_EXTERN int
978 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
979
980
981 LWS_EXTERN int
982 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
983
984 LWS_EXTERN struct lws *
985 lws_client_connect_2(struct lws *wsi);
986
987 LWS_EXTERN struct lws *
988 lws_create_new_server_wsi(struct lws_context *context);
989
990 LWS_EXTERN char *
991 lws_generate_client_handshake(struct lws *wsi, char *pkt);
992
993 LWS_EXTERN int
994 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
995
996 /*
997  * EXTENSIONS
998  */
999
1000 #ifndef LWS_NO_EXTENSIONS
1001 LWS_VISIBLE void
1002 lws_context_init_extensions(struct lws_context_creation_info *info,
1003                             struct lws_context *context);
1004 LWS_EXTERN int
1005 lws_any_extension_handled(struct lws *wsi,
1006                           enum lws_extension_callback_reasons r,
1007                           void *v, size_t len);
1008
1009 LWS_EXTERN int
1010 lws_ext_cb_wsi_active_exts(struct lws *wsi, int reason,
1011                                  void *buf, int len);
1012 LWS_EXTERN int
1013 lws_ext_cb_all_exts(struct lws_context *context,
1014                                          struct lws *wsi, int reason,
1015                                          void *arg, int len);
1016 #else
1017 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1018 #define lws_ext_cb_wsi_active_exts(_a, _b, _c, _d) (0)
1019 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1020 #define lws_issue_raw_ext_access lws_issue_raw
1021 #define lws_context_init_extensions(_a, _b)
1022 #endif
1023
1024 LWS_EXTERN int
1025 lws_client_interpret_server_handshake(struct lws *wsi);
1026
1027 LWS_EXTERN int
1028 lws_rx_sm(struct lws *wsi, unsigned char c);
1029
1030 LWS_EXTERN int
1031 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1032
1033 LWS_EXTERN int
1034 _lws_rx_flow_control(struct lws *wsi);
1035
1036 LWS_EXTERN void
1037 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1038
1039 LWS_EXTERN int
1040 user_callback_handle_rxflow(callback_function, struct lws *wsi,
1041                             enum lws_callback_reasons reason, void *user,
1042                             void *in, size_t len);
1043 #ifdef LWS_USE_HTTP2
1044 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1045 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1046 LWS_EXTERN int
1047 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1048                                      unsigned char *buf, int len);
1049 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1050 LWS_EXTERN int
1051 lws_http2_parser(struct lws *wsi, unsigned char c);
1052 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1053                                      struct lws *wsi);
1054 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1055                                      unsigned int sid, unsigned int len,
1056                                      unsigned char *buf);
1057 LWS_EXTERN struct lws *
1058 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1059 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1060                                    unsigned char c);
1061 LWS_EXTERN int
1062 lws_add_http2_header_by_name(struct lws *wsi,
1063                              const unsigned char *name,
1064                              const unsigned char *value, int length,
1065                              unsigned char **p, unsigned char *end);
1066 LWS_EXTERN int
1067 lws_add_http2_header_by_token(struct lws *wsi,
1068                             enum lws_token_indexes token,
1069                             const unsigned char *value, int length,
1070                             unsigned char **p, unsigned char *end);
1071 LWS_EXTERN int
1072 lws_add_http2_header_status(struct lws *wsi,
1073                             unsigned int code, unsigned char **p,
1074                             unsigned char *end);
1075 LWS_EXTERN
1076 void lws_http2_configure_if_upgraded(struct lws *wsi);
1077 #else
1078 #define lws_http2_configure_if_upgraded(x)
1079 #endif
1080
1081 LWS_EXTERN int
1082 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
1083
1084 LWS_EXTERN int
1085 lws_allocate_header_table(struct lws *wsi);
1086
1087 LWS_EXTERN int
1088 lws_free_header_table(struct lws *wsi);
1089
1090 LWS_EXTERN char *
1091 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1092
1093 LWS_EXTERN int
1094 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1095
1096 LWS_EXTERN int
1097 lws_ensure_user_space(struct lws *wsi);
1098
1099 LWS_EXTERN int
1100 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1101
1102 #ifndef LWS_NO_SERVER
1103 int lws_context_init_server(struct lws_context_creation_info *info,
1104                             struct lws_context *context);
1105 LWS_EXTERN int
1106 handshake_0405(struct lws_context *context, struct lws *wsi);
1107 LWS_EXTERN int
1108 lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf, size_t len);
1109 LWS_EXTERN void
1110 lws_server_get_canonical_hostname(struct lws_context *context,
1111                                   struct lws_context_creation_info *info);
1112 #else
1113 #define lws_context_init_server(_a, _b) (0)
1114 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1115 #define lws_server_get_canonical_hostname(_a, _b)
1116 #endif
1117
1118 #ifndef LWS_NO_DAEMONIZE
1119 LWS_EXTERN int get_daemonize_pid();
1120 #else
1121 #define get_daemonize_pid() (0)
1122 #endif
1123
1124 #if !defined(MBED_OPERATORS)
1125 LWS_EXTERN int
1126 interface_to_sa(struct lws_context *context, const char *ifname,
1127                 struct sockaddr_in *addr, size_t addrlen);
1128 #endif
1129 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1130
1131 enum lws_ssl_capable_status {
1132         LWS_SSL_CAPABLE_ERROR = -1,
1133         LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1134 };
1135
1136 #ifndef LWS_OPENSSL_SUPPORT
1137 #define LWS_SSL_ENABLED(context) (0)
1138 #define lws_context_init_server_ssl(_a, _b) (0)
1139 #define lws_ssl_destroy(_a)
1140 #define lws_context_init_http2_ssl(_a)
1141 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1142 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1143 #define lws_ssl_pending lws_ssl_pending_no_ssl
1144 #define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
1145 #define lws_ssl_close(_a) (0)
1146 #define lws_ssl_context_destroy(_a)
1147 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1148 #else
1149 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1150 LWS_EXTERN int openssl_websocket_private_data_index;
1151 LWS_EXTERN int
1152 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1153 LWS_EXTERN int
1154 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1155 LWS_EXTERN int
1156 lws_ssl_pending(struct lws *wsi);
1157 LWS_EXTERN int
1158 lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
1159                               lws_sockfd_type accept_fd,
1160                               struct lws_pollfd *pollfd);
1161 LWS_EXTERN int
1162 lws_ssl_close(struct lws *wsi);
1163 LWS_EXTERN void
1164 lws_ssl_context_destroy(struct lws_context *context);
1165 LWS_VISIBLE void
1166 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1167 #ifndef LWS_NO_SERVER
1168 LWS_EXTERN int
1169 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1170                             struct lws_context *context);
1171 #else
1172 #define lws_context_init_server_ssl(_a, _b) (0)
1173 #endif
1174 LWS_EXTERN void
1175 lws_ssl_destroy(struct lws_context *context);
1176
1177 /* HTTP2-related */
1178
1179 #ifdef LWS_USE_HTTP2
1180 LWS_EXTERN void
1181 lws_context_init_http2_ssl(struct lws_context *context);
1182 #else
1183 #define lws_context_init_http2_ssl(_a)
1184 #endif
1185 #endif
1186
1187 LWS_EXTERN int
1188 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1189
1190 LWS_EXTERN int
1191 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1192
1193 LWS_EXTERN int
1194 lws_ssl_pending_no_ssl(struct lws *wsi);
1195
1196 #ifndef LWS_NO_CLIENT
1197 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1198                                          struct lws *wsi,
1199                                          struct lws_pollfd *pollfd);
1200 #ifdef LWS_OPENSSL_SUPPORT
1201 LWS_EXTERN int
1202 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1203                             struct lws_context *context);
1204 #else
1205         #define lws_context_init_client_ssl(_a, _b) (0)
1206 #endif
1207 LWS_EXTERN int
1208 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1209 LWS_EXTERN void
1210 lws_decode_ssl_error(void);
1211 #else
1212 #define lws_context_init_client_ssl(_a, _b) (0)
1213 #define lws_handshake_client(_a, _b, _c) (0)
1214 #endif
1215 #ifndef LWS_NO_SERVER
1216 LWS_EXTERN int
1217 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1218                           struct lws_pollfd *pollfd);
1219 LWS_EXTERN int
1220 _lws_rx_flow_control(struct lws *wsi);
1221 LWS_EXTERN int
1222 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1223 #else
1224 #define lws_server_socket_service(_a, _b, _c) (0)
1225 #define _lws_rx_flow_control(_a) (0)
1226 #define lws_handshake_server(_a, _b, _c) (0)
1227 #endif
1228
1229 LWS_EXTERN int
1230 lws_get_addresses(struct lws_context *context, void *ads, char *name,
1231                   int name_len, char *rip, int rip_len);
1232
1233 /*
1234  * custom allocator
1235  */
1236 LWS_EXTERN void*
1237 lws_realloc(void *ptr, size_t size);
1238
1239 LWS_EXTERN void*
1240 lws_zalloc(size_t size);
1241
1242 #define lws_malloc(S)   lws_realloc(NULL, S)
1243 #define lws_free(P)     lws_realloc(P, 0)
1244 #define lws_free_set_NULL(P)    do { lws_realloc(P, 0); (P) = NULL; } while(0)
1245
1246 /* lws_plat_ */
1247 LWS_EXTERN void
1248 lws_plat_delete_socket_from_fds(struct lws_context *context,
1249                                 struct lws *wsi, int m);
1250 LWS_EXTERN void
1251 lws_plat_insert_socket_into_fds(struct lws_context *context,
1252                                 struct lws *wsi);
1253 LWS_EXTERN void
1254 lws_plat_service_periodic(struct lws_context *context);
1255
1256 LWS_EXTERN int
1257 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1258                        struct lws_pollfd *pfd);
1259 LWS_EXTERN int
1260 lws_plat_context_early_init(void);
1261 LWS_EXTERN void
1262 lws_plat_context_early_destroy(struct lws_context *context);
1263 LWS_EXTERN void
1264 lws_plat_context_late_destroy(struct lws_context *context);
1265 LWS_EXTERN int
1266 lws_poll_listen_fd(struct lws_pollfd *fd);
1267 LWS_EXTERN int
1268 lws_plat_service(struct lws_context *context, int timeout_ms);
1269 LWS_EXTERN int
1270 lws_plat_init(struct lws_context *context,
1271               struct lws_context_creation_info *info);
1272 LWS_EXTERN void
1273 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1274 LWS_EXTERN unsigned long long
1275 time_in_microseconds(void);
1276 LWS_EXTERN const char *
1277 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1278
1279 #ifdef __cplusplus
1280 };
1281 #endif