multithreaded service
[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 = 1
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_HEADER_POOL
287 #define LWS_MAX_HEADER_POOL 16
288 #endif
289 #ifndef LWS_MAX_PROTOCOLS
290 #define LWS_MAX_PROTOCOLS 5
291 #endif
292 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
293 #define LWS_MAX_EXTENSIONS_ACTIVE 2
294 #endif
295 #ifndef LWS_MAX_EXT_OFFERS
296 #define LWS_MAX_EXT_OFFERS 8
297 #endif
298 #ifndef SPEC_LATEST_SUPPORTED
299 #define SPEC_LATEST_SUPPORTED 13
300 #endif
301 #ifndef AWAITING_TIMEOUT
302 #define AWAITING_TIMEOUT 5
303 #endif
304 #ifndef CIPHERS_LIST_STRING
305 #define CIPHERS_LIST_STRING "DEFAULT"
306 #endif
307 #ifndef LWS_SOMAXCONN
308 #define LWS_SOMAXCONN SOMAXCONN
309 #endif
310
311 #define MAX_WEBSOCKET_04_KEY_LEN 128
312 #define LWS_MAX_SOCKET_IO_BUF 4096
313
314 #ifndef SYSTEM_RANDOM_FILEPATH
315 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
316 #endif
317
318 /*
319  * if not in a connection storm, check for incoming
320  * connections this many normal connection services
321  */
322 #define LWS_lserv_mod 10
323
324 enum lws_websocket_opcodes_07 {
325         LWSWSOPC_CONTINUATION = 0,
326         LWSWSOPC_TEXT_FRAME = 1,
327         LWSWSOPC_BINARY_FRAME = 2,
328
329         LWSWSOPC_NOSPEC__MUX = 7,
330
331         /* control extensions 8+ */
332
333         LWSWSOPC_CLOSE = 8,
334         LWSWSOPC_PING = 9,
335         LWSWSOPC_PONG = 0xa,
336 };
337
338
339 enum lws_connection_states {
340         LWSS_HTTP,
341         LWSS_HTTP_ISSUING_FILE,
342         LWSS_HTTP_HEADERS,
343         LWSS_HTTP_BODY,
344         LWSS_DEAD_SOCKET,
345         LWSS_ESTABLISHED,
346         LWSS_CLIENT_UNCONNECTED,
347         LWSS_RETURNED_CLOSE_ALREADY,
348         LWSS_AWAITING_CLOSE_ACK,
349         LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
350
351         LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
352         LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
353         LWSS_HTTP2_ESTABLISHED,
354 };
355
356 enum http_version {
357         HTTP_VERSION_1_0,
358         HTTP_VERSION_1_1,
359 };
360
361 enum http_connection_type {
362         HTTP_CONNECTION_CLOSE,
363         HTTP_CONNECTION_KEEP_ALIVE
364 };
365
366 enum lws_pending_protocol_send {
367         LWS_PPS_NONE,
368         LWS_PPS_HTTP2_MY_SETTINGS,
369         LWS_PPS_HTTP2_ACK_SETTINGS,
370         LWS_PPS_HTTP2_PONG,
371 };
372
373 enum lws_rx_parse_state {
374         LWS_RXPS_NEW,
375
376         LWS_RXPS_04_mask_1,
377         LWS_RXPS_04_mask_2,
378         LWS_RXPS_04_mask_3,
379
380         LWS_RXPS_04_FRAME_HDR_1,
381         LWS_RXPS_04_FRAME_HDR_LEN,
382         LWS_RXPS_04_FRAME_HDR_LEN16_2,
383         LWS_RXPS_04_FRAME_HDR_LEN16_1,
384         LWS_RXPS_04_FRAME_HDR_LEN64_8,
385         LWS_RXPS_04_FRAME_HDR_LEN64_7,
386         LWS_RXPS_04_FRAME_HDR_LEN64_6,
387         LWS_RXPS_04_FRAME_HDR_LEN64_5,
388         LWS_RXPS_04_FRAME_HDR_LEN64_4,
389         LWS_RXPS_04_FRAME_HDR_LEN64_3,
390         LWS_RXPS_04_FRAME_HDR_LEN64_2,
391         LWS_RXPS_04_FRAME_HDR_LEN64_1,
392
393         LWS_RXPS_07_COLLECT_FRAME_KEY_1,
394         LWS_RXPS_07_COLLECT_FRAME_KEY_2,
395         LWS_RXPS_07_COLLECT_FRAME_KEY_3,
396         LWS_RXPS_07_COLLECT_FRAME_KEY_4,
397
398         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
399 };
400
401
402 enum connection_mode {
403         LWSCM_HTTP_SERVING,
404         LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
405         LWSCM_PRE_WS_SERVING_ACCEPT,
406
407         LWSCM_WS_SERVING,
408         LWSCM_WS_CLIENT,
409
410         LWSCM_HTTP2_SERVING,
411
412         /* transient, ssl delay hiding */
413         LWSCM_SSL_ACK_PENDING,
414
415         /* transient modes */
416         LWSCM_WSCL_WAITING_CONNECT,
417         LWSCM_WSCL_WAITING_PROXY_REPLY,
418         LWSCM_WSCL_ISSUE_HANDSHAKE,
419         LWSCM_WSCL_ISSUE_HANDSHAKE2,
420         LWSCM_WSCL_WAITING_SSL,
421         LWSCM_WSCL_WAITING_SERVER_REPLY,
422         LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
423         LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
424
425         /* special internal types */
426         LWSCM_SERVER_LISTENER,
427 };
428
429 enum {
430         LWS_RXFLOW_ALLOW = (1 << 0),
431         LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
432 };
433
434 /* this is not usable directly by user code any more, lws_close_reason() */
435 #define LWS_WRITE_CLOSE 4
436
437 struct lws_protocols;
438 struct lws;
439
440 #ifdef LWS_USE_LIBEV
441 struct lws_io_watcher {
442         struct ev_io watcher;
443         struct lws_context* context;
444 };
445
446 struct lws_signal_watcher {
447         struct ev_signal watcher;
448         struct lws_context* context;
449 };
450 #endif /* LWS_USE_LIBEV */
451
452 #ifdef _WIN32
453 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
454 struct lws_fd_hashtable {
455         struct lws **wsi;
456         int length;
457 };
458 #endif
459
460 /*
461  * This is totally opaque to code using the library.  It's exported as a
462  * forward-reference pointer-only declaration; the user can use the pointer with
463  * other APIs to get information out of it.
464  */
465
466 struct lws_fragments {
467         unsigned short offset;
468         unsigned short len;
469         unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
470 };
471
472 /*
473  * these are assigned from a pool held in the context.
474  * Both client and server mode uses them for http header analysis
475  */
476
477 struct allocated_headers {
478         char *data; /* prepared by context init to point to dedicated storage */
479         /*
480          * the randomly ordered fragments, indexed by frag_index and
481          * lws_fragments->nfrag for continuation.
482          */
483         struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
484         /*
485          * for each recognized token, frag_index says which frag[] his data
486          * starts in (0 means the token did not appear)
487          * the actual header data gets dumped as it comes in, into data[]
488          */
489         unsigned char frag_index[WSI_TOKEN_COUNT];
490 #ifndef LWS_NO_CLIENT
491         char initial_handshake_hash_base64[30];
492         unsigned short c_port;
493 #endif
494
495         unsigned short pos;
496         unsigned char in_use;
497         unsigned char nfrag;
498 };
499
500 /*
501  * so we can have n connections being serviced simultaneously,
502  * these things need to be isolated per-thread.
503  */
504
505 struct lws_context_per_thread {
506         struct lws_pollfd *fds;
507         struct lws *rx_draining_ext_list;
508         struct lws *tx_draining_ext_list;
509 #ifdef LWS_OPENSSL_SUPPORT
510         struct lws *pending_read_list; /* linked list */
511 #endif
512         /*
513          * usable by anything in the service code, but only if the scope
514          * does not last longer than the service action (since next service
515          * of any socket can likewise use it and overwrite)
516          */
517         unsigned char *serv_buf;
518 #ifdef _WIN32
519         WSAEVENT *events;
520 #else
521         int dummy_pipe_fds[2];
522 #endif
523         int fds_count;
524 };
525
526 /*
527  * the rest is managed per-context, that includes
528  *
529  *  - processwide single fd -> wsi lookup
530  *  - contextwide headers pool
531  *  - contextwide ssl context
532  *  - contextwide proxy
533  */
534
535 struct lws_context {
536         time_t last_timeout_check_s;
537         struct lws_plat_file_ops fops;
538         struct lws_context_per_thread pt[LWS_MAX_SMP];
539 #ifdef _WIN32
540 /* different implementation between unix and windows */
541         struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
542 #else
543         struct lws **lws_lookup;  /* fd to wsi */
544 #endif
545 #ifdef LWS_USE_LIBEV
546         struct ev_loop* io_loop;
547         struct lws_io_watcher w_accept;
548         struct lws_signal_watcher w_sigint;
549         lws_ev_signal_cb* lws_ev_sigint_cb;
550 #endif /* LWS_USE_LIBEV */
551         const char *iface;
552         const struct lws_token_limits *token_limits;
553         void *user_space;
554         struct lws *timeout_list;
555
556 #ifndef LWS_NO_SERVER
557         struct lws *wsi_listening;
558 #endif
559         const struct lws_protocols *protocols;
560         void *http_header_data;
561         struct allocated_headers *ah_pool;
562
563 #ifdef LWS_OPENSSL_SUPPORT
564         SSL_CTX *ssl_ctx;
565         SSL_CTX *ssl_client_ctx;
566 #endif
567 #ifndef LWS_NO_EXTENSIONS
568         const struct lws_extension *extensions;
569 #endif
570
571         char http_proxy_address[128];
572         char proxy_basic_auth_token[128];
573         char canonical_hostname[128];
574 #ifdef LWS_LATENCY
575         unsigned long worst_latency;
576         char worst_latency_info[256];
577 #endif
578
579         lws_sockfd_type lserv_fd;
580
581         int max_fds;
582         int listen_port;
583 #ifdef LWS_USE_LIBEV
584         int use_ev_sigint;
585 #endif
586         int started_with_parent;
587
588         int fd_random;
589         int lserv_mod;
590         int lserv_count;
591         int lserv_seen;
592         unsigned int http_proxy_port;
593         unsigned int options;
594         unsigned int fd_limit_per_thread;
595
596         /*
597          * set to the Thread ID that's doing the service loop just before entry
598          * to poll indicates service thread likely idling in poll()
599          * volatile because other threads may check it as part of processing
600          * for pollfd event change.
601          */
602         volatile int service_tid;
603         int service_tid_detected;
604
605         int count_protocols;
606         int ka_time;
607         int ka_probes;
608         int ka_interval;
609
610 #ifdef LWS_OPENSSL_SUPPORT
611         int use_ssl;
612         int allow_non_ssl_on_ssl_port;
613         unsigned int user_supplied_ssl_ctx:1;
614 #define lws_ssl_anybody_has_buffered_read(w) \
615                 (w->context->use_ssl && \
616                  w->context->pt[(int)w->tsi].pending_read_list)
617 #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
618                 (c->use_ssl && \
619                  c->pt[(int)t].pending_read_list)
620 #else
621 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
622 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
623 #endif
624
625         short max_http_header_data;
626         short max_http_header_pool;
627         short ah_count_in_use;
628         short count_threads;
629
630         unsigned int being_destroyed:1;
631 };
632
633 enum {
634         LWS_EV_READ = (1 << 0),
635         LWS_EV_WRITE = (1 << 1),
636         LWS_EV_START = (1 << 2),
637         LWS_EV_STOP = (1 << 3),
638 };
639
640 #ifdef LWS_USE_LIBEV
641 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
642 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
643 LWS_EXTERN void
644 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
645 LWS_EXTERN void
646 lws_libev_io(struct lws *wsi, int flags);
647 LWS_EXTERN int
648 lws_libev_init_fd_table(struct lws_context *context);
649 LWS_EXTERN void
650 lws_libev_run(const struct lws_context *context);
651 #else
652 #define LWS_LIBEV_ENABLED(context) (0)
653 #ifdef LWS_POSIX
654 #define lws_feature_status_libev(_a) \
655                         lwsl_notice("libev support not compiled in\n")
656 #else
657 #define lws_feature_status_libev(_a)
658 #endif
659 #define lws_libev_accept(_a, _b) ((void) 0)
660 #define lws_libev_io(_a, _b) ((void) 0)
661 #define lws_libev_init_fd_table(_a) (0)
662 #define lws_libev_run(_a) ((void) 0)
663 #endif
664
665 #ifdef LWS_USE_IPV6
666 #define LWS_IPV6_ENABLED(context) \
667         (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
668 #else
669 #define LWS_IPV6_ENABLED(context) (0)
670 #endif
671
672 enum uri_path_states {
673         URIPS_IDLE,
674         URIPS_SEEN_SLASH,
675         URIPS_SEEN_SLASH_DOT,
676         URIPS_SEEN_SLASH_DOT_DOT,
677 };
678
679 enum uri_esc_states {
680         URIES_IDLE,
681         URIES_SEEN_PERCENT,
682         URIES_SEEN_PERCENT_H1,
683 };
684
685 /* notice that these union members:
686  *
687  *  hdr
688  *  http
689  *  http2
690  *
691  * all have a pointer to allocated_headers struct as their first member.
692  *
693  * It means for allocated_headers access, the three union paths can all be
694  * used interchangeably to access the same data
695  */
696
697 struct _lws_http_mode_related {
698         /* MUST be first in struct */
699         struct allocated_headers *ah; /* mirroring  _lws_header_related */
700         unsigned long filepos;
701         unsigned long filelen;
702         lws_filefd_type fd;
703
704         enum http_version request_version;
705         enum http_connection_type connection_type;
706         unsigned int content_length;
707         unsigned int content_remain;
708 };
709
710 #ifdef LWS_USE_HTTP2
711
712 enum lws_http2_settings {
713         LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
714         LWS_HTTP2_SETTINGS__ENABLE_PUSH,
715         LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
716         LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
717         LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
718         LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
719
720         LWS_HTTP2_SETTINGS__COUNT /* always last */
721 };
722
723 enum lws_http2_wellknown_frame_types {
724         LWS_HTTP2_FRAME_TYPE_DATA,
725         LWS_HTTP2_FRAME_TYPE_HEADERS,
726         LWS_HTTP2_FRAME_TYPE_PRIORITY,
727         LWS_HTTP2_FRAME_TYPE_RST_STREAM,
728         LWS_HTTP2_FRAME_TYPE_SETTINGS,
729         LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
730         LWS_HTTP2_FRAME_TYPE_PING,
731         LWS_HTTP2_FRAME_TYPE_GOAWAY,
732         LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
733         LWS_HTTP2_FRAME_TYPE_CONTINUATION,
734
735         LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
736 };
737
738 enum lws_http2_flags {
739         LWS_HTTP2_FLAG_END_STREAM = 1,
740         LWS_HTTP2_FLAG_END_HEADERS = 4,
741         LWS_HTTP2_FLAG_PADDED = 8,
742         LWS_HTTP2_FLAG_PRIORITY = 0x20,
743
744         LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
745 };
746
747 #define LWS_HTTP2_STREAM_ID_MASTER 0
748 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
749 #define LWS_HTTP2_SETTINGS_LENGTH 6
750
751 struct http2_settings {
752         unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
753 };
754
755 enum http2_hpack_state {
756
757         /* optional before first header block */
758         HPKS_OPT_PADDING,
759         HKPS_OPT_E_DEPENDENCY,
760         HKPS_OPT_WEIGHT,
761
762         /* header block */
763         HPKS_TYPE,
764
765         HPKS_IDX_EXT,
766
767         HPKS_HLEN,
768         HPKS_HLEN_EXT,
769
770         HPKS_DATA,
771
772         /* optional after last header block */
773         HKPS_OPT_DISCARD_PADDING,
774 };
775
776 enum http2_hpack_type {
777         HPKT_INDEXED_HDR_7,
778         HPKT_INDEXED_HDR_6_VALUE_INCR,
779         HPKT_LITERAL_HDR_VALUE_INCR,
780         HPKT_INDEXED_HDR_4_VALUE,
781         HPKT_LITERAL_HDR_VALUE,
782         HPKT_SIZE_5
783 };
784
785 struct hpack_dt_entry {
786         int token; /* additions that don't map to a token are ignored */
787         int arg_offset;
788         int arg_len;
789 };
790
791 struct hpack_dynamic_table {
792         struct hpack_dt_entry *entries;
793         char *args;
794         int pos;
795         int next;
796         int num_entries;
797         int args_length;
798 };
799
800 struct _lws_http2_related {
801         /*
802          * having this first lets us also re-use all HTTP union code
803          * and in turn, http_mode_related has allocated headers in right
804          * place so we can use the header apis on the wsi directly still
805          */
806         struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
807
808         struct http2_settings my_settings;
809         struct http2_settings peer_settings;
810
811         struct lws *parent_wsi;
812         struct lws *next_child_wsi;
813
814         struct hpack_dynamic_table *hpack_dyn_table;
815         struct lws *stream_wsi;
816         unsigned char ping_payload[8];
817         unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
818
819         unsigned int count;
820         unsigned int length;
821         unsigned int stream_id;
822         enum http2_hpack_state hpack;
823         enum http2_hpack_type hpack_type;
824         unsigned int header_index;
825         unsigned int hpack_len;
826         unsigned int hpack_e_dep;
827         int tx_credit;
828         unsigned int my_stream_id;
829         unsigned int child_count;
830         int my_priority;
831
832         unsigned int END_STREAM:1;
833         unsigned int END_HEADERS:1;
834         unsigned int send_END_STREAM:1;
835         unsigned int GOING_AWAY;
836         unsigned int requested_POLLOUT:1;
837         unsigned int waiting_tx_credit:1;
838         unsigned int huff:1;
839         unsigned int value:1;
840
841         unsigned short round_robin_POLLOUT;
842         unsigned short count_POLLOUT_children;
843         unsigned short hpack_pos;
844
845         unsigned char type;
846         unsigned char flags;
847         unsigned char frame_state;
848         unsigned char padding;
849         unsigned char hpack_m;
850         unsigned char initialized;
851 };
852
853 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
854
855 #endif
856
857 struct _lws_header_related {
858         /* MUST be first in struct */
859         struct allocated_headers *ah;
860         enum uri_path_states ups;
861         enum uri_esc_states ues;
862         short lextable_pos;
863         unsigned short current_token_limit;
864         char esc_stash;
865         char post_literal_equal;
866         unsigned char parser_state; /* enum lws_token_indexes */
867         char redirects;
868 };
869
870 struct _lws_websocket_related {
871         char *rx_ubuf;
872        unsigned int rx_ubuf_alloc;
873         struct lws *rx_draining_ext_list;
874         struct lws *tx_draining_ext_list;
875         size_t rx_packet_length;
876         unsigned int rx_ubuf_head;
877         unsigned char mask[4];
878         /* Also used for close content... control opcode == < 128 */
879         unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
880
881         unsigned char ping_payload_len;
882         unsigned char mask_idx;
883         unsigned char opcode;
884         unsigned char rsv;
885         unsigned char rsv_first_msg;
886         /* zero if no info, or length including 2-byte close code */
887         unsigned char close_in_ping_buffer_len;
888         unsigned char utf8;
889         unsigned char stashed_write_type;
890         unsigned char tx_draining_stashed_wp;
891
892         unsigned int final:1;
893         unsigned int frame_is_binary:1;
894         unsigned int all_zero_nonce:1;
895         unsigned int this_frame_masked:1;
896         unsigned int inside_frame:1; /* next write will be more of frame */
897         unsigned int clean_buffer:1; /* buffer not rewritten by extension */
898         unsigned int payload_is_close:1; /* process as PONG, but it is close */
899         unsigned int ping_pending_flag:1;
900         unsigned int continuation_possible:1;
901         unsigned int owed_a_fin:1;
902         unsigned int check_utf8:1;
903         unsigned int defeat_check_utf8:1;
904         unsigned int pmce_compressed_message:1;
905         unsigned int stashed_write_pending:1;
906         unsigned int rx_draining_ext:1;
907         unsigned int tx_draining_ext:1;
908 };
909
910 struct lws {
911
912         /* structs */
913         /* members with mutually exclusive lifetimes are unionized */
914
915         union u {
916                 struct _lws_http_mode_related http;
917 #ifdef LWS_USE_HTTP2
918                 struct _lws_http2_related http2;
919 #endif
920                 struct _lws_header_related hdr;
921                 struct _lws_websocket_related ws;
922         } u;
923
924         /* lifetime members */
925
926 #ifdef LWS_USE_LIBEV
927         struct lws_io_watcher w_read;
928         struct lws_io_watcher w_write;
929 #endif /* LWS_USE_LIBEV */
930         time_t pending_timeout_limit;
931
932         /* pointers */
933
934         struct lws_context *context;
935         const struct lws_protocols *protocol;
936         struct lws *timeout_list;
937         struct lws **timeout_list_prev;
938         void *user_space;
939         /* rxflow handling */
940         unsigned char *rxflow_buffer;
941         /* truncated send handling */
942         unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
943 #ifndef LWS_NO_EXTENSIONS
944         const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
945         void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
946 #endif
947 #ifdef LWS_OPENSSL_SUPPORT
948         SSL *ssl;
949         BIO *client_bio;
950         struct lws *pending_read_list_prev, *pending_read_list_next;
951 #endif
952 #ifdef LWS_LATENCY
953         unsigned long action_start;
954         unsigned long latency_start;
955 #endif
956         /* pointer / int */
957         lws_sockfd_type sock;
958
959         /* ints */
960         int position_in_fds_table;
961         int rxflow_len;
962         int rxflow_pos;
963         unsigned int trunc_alloc_len; /* size of malloc */
964         unsigned int trunc_offset; /* where we are in terms of spilling */
965         unsigned int trunc_len; /* how much is buffered */
966
967         unsigned int hdr_parsing_completed:1;
968         unsigned int user_space_externally_allocated:1;
969         unsigned int socket_is_permanently_unusable:1;
970         unsigned int rxflow_change_to:2;
971 #ifndef LWS_NO_EXTENSIONS
972         unsigned int extension_data_pending:1;
973 #endif
974 #ifdef LWS_OPENSSL_SUPPORT
975         unsigned int use_ssl:2;
976         unsigned int upgraded:1;
977 #endif
978 #ifdef _WIN32
979         unsigned int sock_send_blocking:1;
980 #endif
981
982         /* chars */
983 #ifndef LWS_NO_EXTENSIONS
984         unsigned char count_act_ext;
985 #endif
986         unsigned char ietf_spec_revision;
987         char mode; /* enum connection_mode */
988         char state; /* enum lws_connection_states */
989         char lws_rx_parse_state; /* enum lws_rx_parse_state */
990         char rx_frame_type; /* enum lws_write_protocol */
991         char pending_timeout; /* enum pending_timeout */
992         char pps; /* enum lws_pending_protocol_send */
993         char tsi; /* thread service index we belong to */
994 };
995
996 LWS_EXTERN int log_level;
997
998 LWS_EXTERN void
999 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1000
1001 LWS_EXTERN int
1002 remove_wsi_socket_from_fds(struct lws *wsi);
1003 LWS_EXTERN int
1004 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1005
1006 #ifndef LWS_LATENCY
1007 static inline void
1008 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1009             int ret, int completion) {
1010         do {
1011                 (void)context; (void)wsi; (void)action; (void)ret;
1012                 (void)completion;
1013         } while (0);
1014 }
1015 static inline void
1016 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1017         do { (void)context; (void)wsi; } while (0);
1018 }
1019 #else
1020 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1021 extern void
1022 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1023             int ret, int completion);
1024 #endif
1025
1026 LWS_EXTERN void
1027 lws_set_protocol_write_pending(struct lws *wsi,
1028                                enum lws_pending_protocol_send pend);
1029 LWS_EXTERN int
1030 lws_client_rx_sm(struct lws *wsi, unsigned char c);
1031
1032 LWS_EXTERN int
1033 lws_parse(struct lws *wsi, unsigned char c);
1034
1035 LWS_EXTERN int
1036 lws_http_action(struct lws *wsi);
1037
1038 LWS_EXTERN int
1039 lws_b64_selftest(void);
1040
1041 #if defined(_WIN32) || defined(MBED_OPERATORS)
1042 LWS_EXTERN struct lws *
1043 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
1044
1045 LWS_EXTERN int
1046 insert_wsi(struct lws_context *context, struct lws *wsi);
1047
1048 LWS_EXTERN int
1049 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
1050 #else
1051 #define wsi_from_fd(A,B)  A->lws_lookup[B]
1052 #define insert_wsi(A,B)   A->lws_lookup[B->sock]=B
1053 #define delete_from_fd(A,B) A->lws_lookup[B]=0
1054 #endif
1055
1056 LWS_EXTERN int
1057 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1058
1059 LWS_EXTERN int
1060 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1061
1062
1063 LWS_EXTERN int
1064 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1065
1066 LWS_EXTERN struct lws *
1067 lws_client_connect_2(struct lws *wsi);
1068
1069 LWS_VISIBLE struct lws *
1070 lws_client_reset(struct lws *wsi, int ssl, const char *address, int port, const char *path, const char *host);
1071
1072
1073 LWS_EXTERN struct lws *
1074 lws_create_new_server_wsi(struct lws_context *context);
1075
1076 LWS_EXTERN char *
1077 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1078
1079 LWS_EXTERN int
1080 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1081
1082 /*
1083  * EXTENSIONS
1084  */
1085
1086 #ifndef LWS_NO_EXTENSIONS
1087 LWS_VISIBLE void
1088 lws_context_init_extensions(struct lws_context_creation_info *info,
1089                             struct lws_context *context);
1090 LWS_EXTERN int
1091 lws_any_extension_handled(struct lws *wsi,
1092                           enum lws_extension_callback_reasons r,
1093                           void *v, size_t len);
1094
1095 LWS_EXTERN int
1096 lws_ext_cb_active(struct lws *wsi, int reason,
1097                                  void *buf, int len);
1098 LWS_EXTERN int
1099 lws_ext_cb_all_exts(struct lws_context *context,
1100                                          struct lws *wsi, int reason,
1101                                          void *arg, int len);
1102
1103 #else
1104 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1105 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1106 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1107 #define lws_issue_raw_ext_access lws_issue_raw
1108 #define lws_context_init_extensions(_a, _b)
1109 #endif
1110
1111 LWS_EXTERN int
1112 lws_client_interpret_server_handshake(struct lws *wsi);
1113
1114 LWS_EXTERN int
1115 lws_rx_sm(struct lws *wsi, unsigned char c);
1116
1117 LWS_EXTERN int
1118 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1119
1120 LWS_EXTERN void
1121 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1122
1123 LWS_EXTERN int
1124 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1125                             enum lws_callback_reasons reason, void *user,
1126                             void *in, size_t len);
1127 #ifdef LWS_USE_HTTP2
1128 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1129 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1130 LWS_EXTERN int
1131 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1132                                      unsigned char *buf, int len);
1133 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1134 LWS_EXTERN int
1135 lws_http2_parser(struct lws *wsi, unsigned char c);
1136 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1137                                      struct lws *wsi);
1138 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1139                                      unsigned int sid, unsigned int len,
1140                                      unsigned char *buf);
1141 LWS_EXTERN struct lws *
1142 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1143 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1144                                    unsigned char c);
1145 LWS_EXTERN int
1146 lws_add_http2_header_by_name(struct lws *wsi,
1147                              const unsigned char *name,
1148                              const unsigned char *value, int length,
1149                              unsigned char **p, unsigned char *end);
1150 LWS_EXTERN int
1151 lws_add_http2_header_by_token(struct lws *wsi,
1152                             enum lws_token_indexes token,
1153                             const unsigned char *value, int length,
1154                             unsigned char **p, unsigned char *end);
1155 LWS_EXTERN int
1156 lws_add_http2_header_status(struct lws *wsi,
1157                             unsigned int code, unsigned char **p,
1158                             unsigned char *end);
1159 LWS_EXTERN
1160 void lws_http2_configure_if_upgraded(struct lws *wsi);
1161 #else
1162 #define lws_http2_configure_if_upgraded(x)
1163 #endif
1164
1165 LWS_EXTERN int
1166 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd);
1167
1168 LWS_EXTERN int
1169 lws_allocate_header_table(struct lws *wsi);
1170
1171 LWS_EXTERN int
1172 lws_free_header_table(struct lws *wsi);
1173
1174 LWS_EXTERN char *
1175 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1176
1177 LWS_EXTERN int
1178 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1179
1180 LWS_EXTERN int
1181 lws_ensure_user_space(struct lws *wsi);
1182
1183 LWS_EXTERN int
1184 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1185
1186 #ifndef LWS_NO_SERVER
1187 int lws_context_init_server(struct lws_context_creation_info *info,
1188                             struct lws_context *context);
1189 LWS_EXTERN int
1190 handshake_0405(struct lws_context *context, struct lws *wsi);
1191 LWS_EXTERN int
1192 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
1193 LWS_EXTERN void
1194 lws_server_get_canonical_hostname(struct lws_context *context,
1195                                   struct lws_context_creation_info *info);
1196 #else
1197 #define lws_context_init_server(_a, _b) (0)
1198 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1199 #define lws_server_get_canonical_hostname(_a, _b)
1200 #endif
1201
1202 #ifndef LWS_NO_DAEMONIZE
1203 LWS_EXTERN int get_daemonize_pid();
1204 #else
1205 #define get_daemonize_pid() (0)
1206 #endif
1207
1208 #if !defined(MBED_OPERATORS)
1209 LWS_EXTERN int
1210 interface_to_sa(struct lws_context *context, const char *ifname,
1211                 struct sockaddr_in *addr, size_t addrlen);
1212 #endif
1213 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1214
1215 enum lws_ssl_capable_status {
1216         LWS_SSL_CAPABLE_ERROR = -1,
1217         LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1218 };
1219
1220 #ifndef LWS_OPENSSL_SUPPORT
1221 #define LWS_SSL_ENABLED(context) (0)
1222 #define lws_context_init_server_ssl(_a, _b) (0)
1223 #define lws_ssl_destroy(_a)
1224 #define lws_context_init_http2_ssl(_a)
1225 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1226 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1227 #define lws_ssl_pending lws_ssl_pending_no_ssl
1228 #define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
1229 #define lws_ssl_close(_a) (0)
1230 #define lws_ssl_context_destroy(_a)
1231 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1232 #else
1233 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1234 LWS_EXTERN int openssl_websocket_private_data_index;
1235 LWS_EXTERN int
1236 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1237 LWS_EXTERN int
1238 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1239 LWS_EXTERN int
1240 lws_ssl_pending(struct lws *wsi);
1241 LWS_EXTERN int
1242 lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
1243                               lws_sockfd_type accept_fd,
1244                               struct lws_pollfd *pollfd);
1245 LWS_EXTERN int
1246 lws_ssl_close(struct lws *wsi);
1247 LWS_EXTERN void
1248 lws_ssl_context_destroy(struct lws_context *context);
1249 LWS_VISIBLE void
1250 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1251 #ifndef LWS_NO_SERVER
1252 LWS_EXTERN int
1253 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1254                             struct lws_context *context);
1255 #else
1256 #define lws_context_init_server_ssl(_a, _b) (0)
1257 #endif
1258 LWS_EXTERN void
1259 lws_ssl_destroy(struct lws_context *context);
1260
1261 /* HTTP2-related */
1262
1263 #ifdef LWS_USE_HTTP2
1264 LWS_EXTERN void
1265 lws_context_init_http2_ssl(struct lws_context *context);
1266 #else
1267 #define lws_context_init_http2_ssl(_a)
1268 #endif
1269 #endif
1270
1271 LWS_EXTERN int
1272 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1273
1274 LWS_EXTERN int
1275 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1276
1277 LWS_EXTERN int
1278 lws_ssl_pending_no_ssl(struct lws *wsi);
1279
1280 #ifndef LWS_NO_CLIENT
1281 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1282                                          struct lws *wsi,
1283                                          struct lws_pollfd *pollfd);
1284 #ifdef LWS_OPENSSL_SUPPORT
1285 LWS_EXTERN int
1286 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1287                             struct lws_context *context);
1288 #else
1289         #define lws_context_init_client_ssl(_a, _b) (0)
1290 #endif
1291 LWS_EXTERN int
1292 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1293 LWS_EXTERN void
1294 lws_decode_ssl_error(void);
1295 #else
1296 #define lws_context_init_client_ssl(_a, _b) (0)
1297 #define lws_handshake_client(_a, _b, _c) (0)
1298 #endif
1299
1300 LWS_EXTERN int
1301 _lws_rx_flow_control(struct lws *wsi);
1302
1303 #ifndef LWS_NO_SERVER
1304 LWS_EXTERN int
1305 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1306                           struct lws_pollfd *pollfd);
1307 LWS_EXTERN int
1308 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1309 LWS_EXTERN int
1310 _lws_server_listen_accept_flow_control(struct lws_context *context, int on);
1311 #else
1312 #define lws_server_socket_service(_a, _b, _c) (0)
1313 #define lws_handshake_server(_a, _b, _c) (0)
1314 #define _lws_server_listen_accept_flow_control(a, b) (0)
1315 #endif
1316
1317 LWS_EXTERN int
1318 lws_get_addresses(struct lws_context *context, void *ads, char *name,
1319                   int name_len, char *rip, int rip_len);
1320
1321 /*
1322  * custom allocator
1323  */
1324 LWS_EXTERN void*
1325 lws_realloc(void *ptr, size_t size);
1326
1327 LWS_EXTERN void*
1328 lws_zalloc(size_t size);
1329
1330 #define lws_malloc(S)   lws_realloc(NULL, S)
1331 #define lws_free(P)     lws_realloc(P, 0)
1332 #define lws_free_set_NULL(P)    do { lws_realloc(P, 0); (P) = NULL; } while(0)
1333
1334 /* lws_plat_ */
1335 LWS_EXTERN void
1336 lws_plat_delete_socket_from_fds(struct lws_context *context,
1337                                 struct lws *wsi, int m);
1338 LWS_EXTERN void
1339 lws_plat_insert_socket_into_fds(struct lws_context *context,
1340                                 struct lws *wsi);
1341 LWS_EXTERN void
1342 lws_plat_service_periodic(struct lws_context *context);
1343
1344 LWS_EXTERN int
1345 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1346                        struct lws_pollfd *pfd);
1347 LWS_EXTERN int
1348 lws_plat_context_early_init(void);
1349 LWS_EXTERN void
1350 lws_plat_context_early_destroy(struct lws_context *context);
1351 LWS_EXTERN void
1352 lws_plat_context_late_destroy(struct lws_context *context);
1353 LWS_EXTERN int
1354 lws_poll_listen_fd(struct lws_pollfd *fd);
1355 LWS_EXTERN int
1356 lws_plat_service(struct lws_context *context, int timeout_ms);
1357 LWS_EXTERN LWS_VISIBLE int
1358 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1359 LWS_EXTERN int
1360 lws_plat_init(struct lws_context *context,
1361               struct lws_context_creation_info *info);
1362 LWS_EXTERN void
1363 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1364 LWS_EXTERN unsigned long long
1365 time_in_microseconds(void);
1366 LWS_EXTERN const char *
1367 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1368
1369 LWS_EXTERN int
1370 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
1371
1372 #ifdef __cplusplus
1373 };
1374 #endif