windows Use WSAWaitForMultipleEvents() instead of poll() on Windows
[platform/upstream/libwebsockets.git] / lib / private-libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2013 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 /* System introspection configs */
23 #ifdef CMAKE_BUILD
24 #include "lws_config.h"
25 #else
26 #if defined(WIN32) || defined(_WIN32)
27 #define inline __inline
28 #else
29 #include "config.h"
30 #endif
31 #endif
32
33 #if _MSC_VER > 1000 || defined(_WIN32)
34 #else
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <unistd.h>
39 #include <strings.h>
40 #include <time.h>
41
42 #endif
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <limits.h>
48 #ifdef __MINGW64__
49 #else
50 #ifdef __MINGW32__
51 #elif _MSC_VER > 1000 || defined(_WIN32)
52 #else
53 #include <netdb.h>
54 #endif
55 #endif
56 #include <stdarg.h>
57
58 #ifdef HAVE_SYS_STAT_H
59 #include <sys/stat.h>
60 #endif
61
62 #if defined(WIN32) || defined(_WIN32)
63 #define LWS_NO_DAEMONIZE
64 #define LWS_ERRNO WSAGetLastError()
65 #define LWS_EAGAIN WSAEWOULDBLOCK
66 #define LWS_EALREADY WSAEALREADY
67 #define LWS_EINPROGRESS WSAEINPROGRESS
68 #define LWS_EINTR WSAEINTR
69 #define LWS_EISCONN WSAEISCONN
70 #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
71 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
72 #define LWS_POLLOUT (FD_WRITE)
73
74 #define compatible_close(fd) closesocket(fd);
75 #ifdef __MINGW64__
76 #else
77 #ifdef __MINGW32__
78 #else
79 #include <time.h >
80 #endif
81 #endif
82 #include <winsock2.h>
83 #include <windows.h>
84 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
85 #else
86 #include <sys/types.h>
87 #include <sys/socket.h>
88 #ifndef LWS_NO_FORK
89 #ifdef HAVE_SYS_PRCTL_H
90 #include <sys/prctl.h>
91 #endif
92 #endif
93 #include <netinet/in.h>
94 #include <netinet/tcp.h>
95 #include <arpa/inet.h>
96 #include <poll.h>
97 #ifdef LWS_USE_LIBEV
98 #include <ev.h>
99 #endif /* LWS_USE_LIBEV */
100
101 #include <sys/mman.h>
102 #include <sys/time.h>
103
104 #define LWS_ERRNO errno
105 #define LWS_EAGAIN EAGAIN
106 #define LWS_EALREADY EALREADY
107 #define LWS_EINPROGRESS EINPROGRESS
108 #define LWS_EINTR EINTR
109 #define LWS_EISCONN EISCONN
110 #define LWS_EWOULDBLOCK EWOULDBLOCK
111 #define LWS_INVALID_FILE -1
112 #define compatible_close(fd) close(fd);
113 #endif
114
115 #ifndef HAVE_BZERO
116 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
117 #endif
118
119 #ifndef HAVE_STRERROR
120 #define strerror(x) ""
121 #endif
122
123 #ifdef LWS_OPENSSL_SUPPORT
124 #ifdef USE_CYASSL
125 #include <cyassl/openssl/ssl.h>
126 #include <cyassl/error.h>
127 unsigned char *
128 SHA1(const unsigned char *d, size_t n, unsigned char *md);
129 #else
130 #include <openssl/ssl.h>
131 #include <openssl/evp.h>
132 #include <openssl/err.h>
133 #include <openssl/md5.h>
134 #include <openssl/sha.h>
135 #endif /* not USE_CYASSL */
136 #endif
137
138 #include "libwebsockets.h"
139
140 /*
141  * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
142  * but happily have something equivalent in the SO_NOSIGPIPE flag.
143  */
144 #ifdef __APPLE__
145 #define MSG_NOSIGNAL SO_NOSIGPIPE
146 #endif
147
148 #ifndef LWS_MAX_HEADER_LEN
149 #define LWS_MAX_HEADER_LEN 1024
150 #endif
151 #ifndef LWS_MAX_PROTOCOLS
152 #define LWS_MAX_PROTOCOLS 5
153 #endif
154 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
155 #define LWS_MAX_EXTENSIONS_ACTIVE 3
156 #endif
157 #ifndef SPEC_LATEST_SUPPORTED
158 #define SPEC_LATEST_SUPPORTED 13
159 #endif
160 #ifndef AWAITING_TIMEOUT
161 #define AWAITING_TIMEOUT 5
162 #endif
163 #ifndef CIPHERS_LIST_STRING
164 #define CIPHERS_LIST_STRING "DEFAULT"
165 #endif
166 #ifndef LWS_SOMAXCONN
167 #define LWS_SOMAXCONN SOMAXCONN
168 #endif
169
170 #define MAX_WEBSOCKET_04_KEY_LEN 128
171 #define LWS_MAX_SOCKET_IO_BUF 4096
172
173 #ifndef SYSTEM_RANDOM_FILEPATH
174 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
175 #endif
176 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
177 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
178 #endif
179
180 /*
181  * if not in a connection storm, check for incoming
182  * connections this many normal connection services
183  */
184 #define LWS_LISTEN_SERVICE_MODULO 10
185
186 enum lws_websocket_opcodes_07 {
187         LWS_WS_OPCODE_07__CONTINUATION = 0,
188         LWS_WS_OPCODE_07__TEXT_FRAME = 1,
189         LWS_WS_OPCODE_07__BINARY_FRAME = 2,
190
191         LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
192
193         /* control extensions 8+ */
194
195         LWS_WS_OPCODE_07__CLOSE = 8,
196         LWS_WS_OPCODE_07__PING = 9,
197         LWS_WS_OPCODE_07__PONG = 0xa,
198 };
199
200
201 enum lws_connection_states {
202         WSI_STATE_HTTP,
203         WSI_STATE_HTTP_ISSUING_FILE,
204         WSI_STATE_HTTP_HEADERS,
205         WSI_STATE_HTTP_BODY,
206         WSI_STATE_DEAD_SOCKET,
207         WSI_STATE_ESTABLISHED,
208         WSI_STATE_CLIENT_UNCONNECTED,
209         WSI_STATE_RETURNED_CLOSE_ALREADY,
210         WSI_STATE_AWAITING_CLOSE_ACK,
211 };
212
213 enum lws_rx_parse_state {
214         LWS_RXPS_NEW,
215
216         LWS_RXPS_04_MASK_NONCE_1,
217         LWS_RXPS_04_MASK_NONCE_2,
218         LWS_RXPS_04_MASK_NONCE_3,
219
220         LWS_RXPS_04_FRAME_HDR_1,
221         LWS_RXPS_04_FRAME_HDR_LEN,
222         LWS_RXPS_04_FRAME_HDR_LEN16_2,
223         LWS_RXPS_04_FRAME_HDR_LEN16_1,
224         LWS_RXPS_04_FRAME_HDR_LEN64_8,
225         LWS_RXPS_04_FRAME_HDR_LEN64_7,
226         LWS_RXPS_04_FRAME_HDR_LEN64_6,
227         LWS_RXPS_04_FRAME_HDR_LEN64_5,
228         LWS_RXPS_04_FRAME_HDR_LEN64_4,
229         LWS_RXPS_04_FRAME_HDR_LEN64_3,
230         LWS_RXPS_04_FRAME_HDR_LEN64_2,
231         LWS_RXPS_04_FRAME_HDR_LEN64_1,
232
233         LWS_RXPS_07_COLLECT_FRAME_KEY_1,
234         LWS_RXPS_07_COLLECT_FRAME_KEY_2,
235         LWS_RXPS_07_COLLECT_FRAME_KEY_3,
236         LWS_RXPS_07_COLLECT_FRAME_KEY_4,
237
238         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
239 };
240
241
242 enum connection_mode {
243         LWS_CONNMODE_HTTP_SERVING,
244         LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
245         LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
246
247         LWS_CONNMODE_WS_SERVING,
248         LWS_CONNMODE_WS_CLIENT,
249
250         /* transient, ssl delay hiding */
251         LWS_CONNMODE_SSL_ACK_PENDING,
252
253         /* transient modes */
254         LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
255         LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
256         LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
257         LWS_CONNMODE_WS_CLIENT_WAITING_SSL,
258         LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
259         LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
260         LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
261
262         /* special internal types */
263         LWS_CONNMODE_SERVER_LISTENER,
264 };
265
266 enum {
267         LWS_RXFLOW_ALLOW = (1 << 0),
268         LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
269 };
270
271 struct libwebsocket_protocols;
272 struct libwebsocket;
273
274 #ifdef LWS_USE_LIBEV
275 struct lws_io_watcher {
276         struct ev_io watcher;
277         struct libwebsocket_context* context;
278 };
279
280 struct lws_signal_watcher {
281         struct ev_signal watcher;
282         struct libwebsocket_context* context;
283 };
284 #endif /* LWS_USE_LIBEV */
285
286 struct libwebsocket_context {
287 #ifdef _WIN32
288         WSAEVENT *events;
289 #endif
290         struct pollfd *fds;
291         struct libwebsocket **lws_lookup; /* fd to wsi */
292         int fds_count;
293 #ifdef LWS_USE_LIBEV
294         struct ev_loop* io_loop;
295         struct lws_io_watcher w_accept;
296         struct lws_signal_watcher w_sigint;
297 #endif /* LWS_USE_LIBEV */
298         int max_fds;
299         int listen_port;
300         const char *iface;
301         char http_proxy_address[128];
302         char canonical_hostname[128];
303         unsigned int http_proxy_port;
304         unsigned int options;
305         time_t last_timeout_check_s;
306
307         /*
308          * usable by anything in the service code, but only if the scope
309          * does not last longer than the service action (since next service
310          * of any socket can likewise use it and overwrite)
311          */
312         unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
313
314         int started_with_parent;
315
316         int fd_random;
317         int listen_service_modulo;
318         int listen_service_count;
319         int listen_service_fd;
320         int listen_service_extraseen;
321
322         /*
323          * set to the Thread ID that's doing the service loop just before entry
324          * to poll indicates service thread likely idling in poll()
325          * volatile because other threads may check it as part of processing
326          * for pollfd event change.
327          */
328         volatile int service_tid;
329 #ifndef _WIN32
330         int dummy_pipe_fds[2];
331 #endif
332
333         int ka_time;
334         int ka_probes;
335         int ka_interval;
336
337 #ifdef LWS_LATENCY
338         unsigned long worst_latency;
339         char worst_latency_info[256];
340 #endif
341
342 #ifdef LWS_OPENSSL_SUPPORT
343         int use_ssl;
344         int allow_non_ssl_on_ssl_port;
345         SSL_CTX *ssl_ctx;
346         SSL_CTX *ssl_client_ctx;
347 #endif
348         struct libwebsocket_protocols *protocols;
349         int count_protocols;
350 #ifndef LWS_NO_EXTENSIONS
351         struct libwebsocket_extension *extensions;
352 #endif
353         void *user_space;
354 };
355
356 #ifdef LWS_USE_LIBEV
357 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
358 #else
359 #define LWS_LIBEV_ENABLED(context) (0)
360 #endif
361
362 #ifdef LWS_USE_IPV6
363 #define LWS_IPV6_ENABLED(context) (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
364 #else
365 #define LWS_IPV6_ENABLED(context) (0)
366 #endif
367
368 enum uri_path_states {
369         URIPS_IDLE,
370         URIPS_SEEN_SLASH,
371         URIPS_SEEN_SLASH_DOT,
372         URIPS_SEEN_SLASH_DOT_DOT,
373         URIPS_ARGUMENTS,
374 };
375
376 enum uri_esc_states {
377         URIES_IDLE,
378         URIES_SEEN_PERCENT,
379         URIES_SEEN_PERCENT_H1,
380 };
381
382 /*
383  * This is totally opaque to code using the library.  It's exported as a
384  * forward-reference pointer-only declaration; the user can use the pointer with
385  * other APIs to get information out of it.
386  */
387
388 struct lws_fragments {
389         unsigned short offset;
390         unsigned short len;
391         unsigned char next_frag_index;
392 };
393
394 struct allocated_headers {
395         unsigned short next_frag_index;
396         unsigned short pos;
397         unsigned char frag_index[WSI_TOKEN_COUNT];
398         struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
399         char data[LWS_MAX_HEADER_LEN];
400 #ifndef LWS_NO_CLIENT
401         char initial_handshake_hash_base64[30];
402         unsigned short c_port;
403 #endif
404 };
405
406 struct _lws_http_mode_related {
407         struct allocated_headers *ah; /* mirroring  _lws_header_related */
408 #if defined(WIN32) || defined(_WIN32)
409         HANDLE fd;
410 #else
411         int fd;
412 #endif
413         unsigned long filepos;
414         unsigned long filelen;
415
416         int content_length;
417         int content_length_seen;
418         int body_index;
419         unsigned char *post_buffer;
420 };
421
422 struct _lws_header_related {
423         struct allocated_headers *ah;
424         short lextable_pos;
425         unsigned char parser_state; /* enum lws_token_indexes */
426         enum uri_path_states ups;
427         enum uri_esc_states ues;
428         char esc_stash;
429 };
430
431 struct _lws_websocket_related {
432         char *rx_user_buffer;
433         int rx_user_buffer_head;
434         unsigned char frame_masking_nonce_04[4];
435         unsigned char frame_mask_index;
436         size_t rx_packet_length;
437         unsigned char opcode;
438         unsigned int final:1;
439         unsigned char rsv;
440         unsigned int frame_is_binary:1;
441         unsigned int all_zero_nonce:1;
442         short close_reason; /* enum lws_close_status */
443         unsigned char *rxflow_buffer;
444         int rxflow_len;
445         int rxflow_pos;
446         unsigned int rxflow_change_to:2;
447         unsigned int this_frame_masked:1;
448         unsigned int inside_frame:1; /* next write will be more of frame */
449         unsigned int clean_buffer:1; /* buffer not rewritten by extension */
450 };
451
452 struct libwebsocket {
453
454         /* lifetime members */
455
456 #ifdef LWS_USE_LIBEV
457     struct lws_io_watcher w_read;
458     struct lws_io_watcher w_write;
459 #endif /* LWS_USE_LIBEV */
460         const struct libwebsocket_protocols *protocol;
461 #ifndef LWS_NO_EXTENSIONS
462         struct libwebsocket_extension *
463                                    active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
464         void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
465         unsigned char count_active_extensions;
466         unsigned int extension_data_pending:1;
467 #endif
468         unsigned char ietf_spec_revision;
469
470         char mode; /* enum connection_mode */
471         char state; /* enum lws_connection_states */
472         char lws_rx_parse_state; /* enum lws_rx_parse_state */
473         char rx_frame_type; /* enum libwebsocket_write_protocol */
474
475         unsigned int hdr_parsing_completed:1;
476
477         char pending_timeout; /* enum pending_timeout */
478         time_t pending_timeout_limit;
479
480         int sock;
481         int position_in_fds_table;
482 #ifdef LWS_LATENCY
483         unsigned long action_start;
484         unsigned long latency_start;
485 #endif
486
487         /* truncated send handling */
488         unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */
489         unsigned int truncated_send_allocation; /* size of malloc */
490         unsigned int truncated_send_offset; /* where we are in terms of spilling */
491         unsigned int truncated_send_len; /* how much is buffered */
492
493         void *user_space;
494
495         /* members with mutually exclusive lifetimes are unionized */
496
497         union u {
498                 struct _lws_http_mode_related http;
499                 struct _lws_header_related hdr;
500                 struct _lws_websocket_related ws;
501         } u;
502
503 #ifdef LWS_OPENSSL_SUPPORT
504         SSL *ssl;
505         BIO *client_bio;
506         unsigned int use_ssl:2;
507 #endif
508
509 #ifdef _WIN32
510         BOOL sock_send_blocking;
511 #endif
512 };
513
514 LWS_EXTERN void
515 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
516                                struct libwebsocket *wsi, enum lws_close_status);
517
518 #ifndef LWS_LATENCY
519 static inline void lws_latency(struct libwebsocket_context *context,
520                 struct libwebsocket *wsi, const char *action,
521                                          int ret, int completion) { while (0); }
522 static inline void lws_latency_pre(struct libwebsocket_context *context,
523                                         struct libwebsocket *wsi) { while (0); }
524 #else
525 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
526 extern void
527 lws_latency(struct libwebsocket_context *context,
528                         struct libwebsocket *wsi, const char *action,
529                                                        int ret, int completion);
530 #endif
531
532 LWS_EXTERN int
533 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
534
535 LWS_EXTERN int
536 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
537
538 LWS_EXTERN int
539 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
540                                                 unsigned char *buf, size_t len);
541
542 LWS_EXTERN int
543 lws_b64_selftest(void);
544
545 LWS_EXTERN struct libwebsocket *
546 wsi_from_fd(struct libwebsocket_context *context, int fd);
547
548 LWS_EXTERN int
549 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
550                                                       struct libwebsocket *wsi);
551
552 LWS_EXTERN int
553 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
554
555
556 LWS_EXTERN int
557 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
558                                     struct libwebsocket *wsi, unsigned int sec);
559
560 LWS_EXTERN struct libwebsocket *
561 libwebsocket_client_connect_2(struct libwebsocket_context *context,
562         struct libwebsocket *wsi);
563
564 LWS_EXTERN struct libwebsocket *
565 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
566
567 LWS_EXTERN char *
568 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
569                 struct libwebsocket *wsi, char *pkt);
570
571 LWS_EXTERN int
572 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
573                               struct libwebsocket *wsi, struct pollfd *pollfd);
574 #ifndef LWS_NO_EXTENSIONS
575 LWS_EXTERN int
576 lws_any_extension_handled(struct libwebsocket_context *context,
577                           struct libwebsocket *wsi,
578                           enum libwebsocket_extension_callback_reasons r,
579                           void *v, size_t len);
580
581 LWS_EXTERN void *
582 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
583                           struct libwebsocket_extension *ext);
584 #endif
585
586 LWS_EXTERN int
587 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
588                 struct libwebsocket *wsi);
589
590 LWS_EXTERN int
591 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
592
593 LWS_EXTERN int
594 lws_issue_raw_ext_access(struct libwebsocket *wsi,
595                                                 unsigned char *buf, size_t len);
596
597 LWS_EXTERN int
598 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
599
600 LWS_EXTERN int
601 user_callback_handle_rxflow(callback_function,
602                 struct libwebsocket_context *context,
603                         struct libwebsocket *wsi,
604                          enum libwebsocket_callback_reasons reason, void *user,
605                                                           void *in, size_t len);
606
607 LWS_EXTERN int
608 lws_set_socket_options(struct libwebsocket_context *context, int fd);
609
610 LWS_EXTERN int
611 lws_allocate_header_table(struct libwebsocket *wsi);
612
613 LWS_EXTERN char *
614 lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
615
616 LWS_EXTERN int
617 lws_hdr_simple_create(struct libwebsocket *wsi,
618                                 enum lws_token_indexes h, const char *s);
619
620 LWS_EXTERN int
621 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
622
623 LWS_EXTERN void
624 lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or);
625
626 #ifndef LWS_NO_SERVER
627 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
628                                                       struct libwebsocket *wsi);
629 #endif
630
631 #ifndef LWS_NO_DAEMONIZE
632 LWS_EXTERN int get_daemonize_pid();
633 #endif
634
635 extern int interface_to_sa(struct libwebsocket_context *context,
636                 const char *ifname, struct sockaddr_in *addr, size_t addrlen);
637
638 #ifndef LWS_OPENSSL_SUPPORT
639
640 unsigned char *
641 SHA1(const unsigned char *d, size_t n, unsigned char *md);
642
643 #else
644
645 LWS_EXTERN int openssl_websocket_private_data_index;
646
647 #endif