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