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