2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2013 Andy Green <andy@warmcat.com>
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.
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.
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,
22 /* System introspection configs */
24 #include "lws_config.h"
26 #if defined(WIN32) || defined(_WIN32)
27 #define inline __inline
33 #if _MSC_VER > 1000 || defined(_WIN32)
51 #elif _MSC_VER > 1000 || defined(_WIN32)
58 #ifdef HAVE_SYS_STAT_H
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 #define MSG_NOSIGNAL 0
74 #define SHUT_RDWR SD_BOTH
75 #define SOL_TCP IPPROTO_TCP
77 #define compatible_close(fd) closesocket(fd);
87 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
89 #if defined(__MINGW32__) || defined(__MINGW64__) || _WIN32_WINNT < 0x0600
96 #include <sys/types.h>
97 #include <sys/socket.h>
99 #ifdef HAVE_SYS_PRCTL_H
100 #include <sys/prctl.h>
103 #include <netinet/in.h>
104 #include <netinet/tcp.h>
105 #include <arpa/inet.h>
109 #endif /* LWS_USE_LIBEV */
111 #include <sys/mman.h>
112 #include <sys/time.h>
114 #define LWS_ERRNO errno
115 #define LWS_EAGAIN EAGAIN
116 #define LWS_EALREADY EALREADY
117 #define LWS_EINPROGRESS EINPROGRESS
118 #define LWS_EINTR EINTR
119 #define LWS_EISCONN EISCONN
120 #define LWS_EWOULDBLOCK EWOULDBLOCK
121 #define LWS_INVALID_FILE -1
122 #define compatible_close(fd) close(fd);
126 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
129 #ifndef HAVE_STRERROR
130 #define strerror(x) ""
133 #ifdef LWS_OPENSSL_SUPPORT
135 #include <cyassl/openssl/ssl.h>
136 #include <cyassl/error.h>
138 SHA1(const unsigned char *d, size_t n, unsigned char *md);
140 #include <openssl/ssl.h>
141 #include <openssl/evp.h>
142 #include <openssl/err.h>
143 #include <openssl/md5.h>
144 #include <openssl/sha.h>
145 #endif /* not USE_CYASSL */
148 #include "libwebsockets.h"
151 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
152 * but happily have something equivalent in the SO_NOSIGPIPE flag.
155 #define MSG_NOSIGNAL SO_NOSIGPIPE
158 #ifndef LWS_MAX_HEADER_LEN
159 #define LWS_MAX_HEADER_LEN 1024
161 #ifndef LWS_MAX_PROTOCOLS
162 #define LWS_MAX_PROTOCOLS 5
164 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
165 #define LWS_MAX_EXTENSIONS_ACTIVE 3
167 #ifndef SPEC_LATEST_SUPPORTED
168 #define SPEC_LATEST_SUPPORTED 13
170 #ifndef AWAITING_TIMEOUT
171 #define AWAITING_TIMEOUT 5
173 #ifndef CIPHERS_LIST_STRING
174 #define CIPHERS_LIST_STRING "DEFAULT"
176 #ifndef LWS_SOMAXCONN
177 #define LWS_SOMAXCONN SOMAXCONN
180 #define MAX_WEBSOCKET_04_KEY_LEN 128
181 #define LWS_MAX_SOCKET_IO_BUF 4096
183 #ifndef SYSTEM_RANDOM_FILEPATH
184 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
186 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
187 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
191 * if not in a connection storm, check for incoming
192 * connections this many normal connection services
194 #define LWS_LISTEN_SERVICE_MODULO 10
196 enum lws_websocket_opcodes_07 {
197 LWS_WS_OPCODE_07__CONTINUATION = 0,
198 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
199 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
201 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
203 /* control extensions 8+ */
205 LWS_WS_OPCODE_07__CLOSE = 8,
206 LWS_WS_OPCODE_07__PING = 9,
207 LWS_WS_OPCODE_07__PONG = 0xa,
211 enum lws_connection_states {
213 WSI_STATE_HTTP_ISSUING_FILE,
214 WSI_STATE_HTTP_HEADERS,
216 WSI_STATE_DEAD_SOCKET,
217 WSI_STATE_ESTABLISHED,
218 WSI_STATE_CLIENT_UNCONNECTED,
219 WSI_STATE_RETURNED_CLOSE_ALREADY,
220 WSI_STATE_AWAITING_CLOSE_ACK,
223 enum lws_rx_parse_state {
226 LWS_RXPS_04_MASK_NONCE_1,
227 LWS_RXPS_04_MASK_NONCE_2,
228 LWS_RXPS_04_MASK_NONCE_3,
230 LWS_RXPS_04_FRAME_HDR_1,
231 LWS_RXPS_04_FRAME_HDR_LEN,
232 LWS_RXPS_04_FRAME_HDR_LEN16_2,
233 LWS_RXPS_04_FRAME_HDR_LEN16_1,
234 LWS_RXPS_04_FRAME_HDR_LEN64_8,
235 LWS_RXPS_04_FRAME_HDR_LEN64_7,
236 LWS_RXPS_04_FRAME_HDR_LEN64_6,
237 LWS_RXPS_04_FRAME_HDR_LEN64_5,
238 LWS_RXPS_04_FRAME_HDR_LEN64_4,
239 LWS_RXPS_04_FRAME_HDR_LEN64_3,
240 LWS_RXPS_04_FRAME_HDR_LEN64_2,
241 LWS_RXPS_04_FRAME_HDR_LEN64_1,
243 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
244 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
245 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
246 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
248 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
252 enum connection_mode {
253 LWS_CONNMODE_HTTP_SERVING,
254 LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
255 LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
257 LWS_CONNMODE_WS_SERVING,
258 LWS_CONNMODE_WS_CLIENT,
260 /* transient, ssl delay hiding */
261 LWS_CONNMODE_SSL_ACK_PENDING,
263 /* transient modes */
264 LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
265 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
266 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
267 LWS_CONNMODE_WS_CLIENT_WAITING_SSL,
268 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
269 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
270 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
272 /* special internal types */
273 LWS_CONNMODE_SERVER_LISTENER,
277 LWS_RXFLOW_ALLOW = (1 << 0),
278 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
281 struct libwebsocket_protocols;
285 struct lws_io_watcher {
286 struct ev_io watcher;
287 struct libwebsocket_context* context;
290 struct lws_signal_watcher {
291 struct ev_signal watcher;
292 struct libwebsocket_context* context;
294 #endif /* LWS_USE_LIBEV */
296 struct libwebsocket_context {
300 struct libwebsocket_pollfd *fds;
301 struct libwebsocket **lws_lookup; /* fd to wsi */
304 struct ev_loop* io_loop;
305 struct lws_io_watcher w_accept;
306 struct lws_signal_watcher w_sigint;
307 #endif /* LWS_USE_LIBEV */
311 char http_proxy_address[128];
312 char canonical_hostname[128];
313 unsigned int http_proxy_port;
314 unsigned int options;
315 time_t last_timeout_check_s;
318 * usable by anything in the service code, but only if the scope
319 * does not last longer than the service action (since next service
320 * of any socket can likewise use it and overwrite)
322 unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
324 int started_with_parent;
327 int listen_service_modulo;
328 int listen_service_count;
329 int listen_service_fd;
330 int listen_service_extraseen;
333 * set to the Thread ID that's doing the service loop just before entry
334 * to poll indicates service thread likely idling in poll()
335 * volatile because other threads may check it as part of processing
336 * for pollfd event change.
338 volatile int service_tid;
340 int dummy_pipe_fds[2];
348 unsigned long worst_latency;
349 char worst_latency_info[256];
352 #ifdef LWS_OPENSSL_SUPPORT
354 int allow_non_ssl_on_ssl_port;
356 SSL_CTX *ssl_client_ctx;
358 struct libwebsocket_protocols *protocols;
360 #ifndef LWS_NO_EXTENSIONS
361 struct libwebsocket_extension *extensions;
367 #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
369 #define LWS_LIBEV_ENABLED(context) (0)
373 #define LWS_IPV6_ENABLED(context) (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6))
375 #define LWS_IPV6_ENABLED(context) (0)
378 enum uri_path_states {
381 URIPS_SEEN_SLASH_DOT,
382 URIPS_SEEN_SLASH_DOT_DOT,
386 enum uri_esc_states {
389 URIES_SEEN_PERCENT_H1,
393 * This is totally opaque to code using the library. It's exported as a
394 * forward-reference pointer-only declaration; the user can use the pointer with
395 * other APIs to get information out of it.
398 struct lws_fragments {
399 unsigned short offset;
401 unsigned char next_frag_index;
404 struct allocated_headers {
405 unsigned short next_frag_index;
407 unsigned char frag_index[WSI_TOKEN_COUNT];
408 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
409 char data[LWS_MAX_HEADER_LEN];
410 #ifndef LWS_NO_CLIENT
411 char initial_handshake_hash_base64[30];
412 unsigned short c_port;
416 struct _lws_http_mode_related {
417 struct allocated_headers *ah; /* mirroring _lws_header_related */
418 #if defined(WIN32) || defined(_WIN32)
423 unsigned long filepos;
424 unsigned long filelen;
427 int content_length_seen;
429 unsigned char *post_buffer;
432 struct _lws_header_related {
433 struct allocated_headers *ah;
435 unsigned char parser_state; /* enum lws_token_indexes */
436 enum uri_path_states ups;
437 enum uri_esc_states ues;
441 struct _lws_websocket_related {
442 char *rx_user_buffer;
443 int rx_user_buffer_head;
444 unsigned char frame_masking_nonce_04[4];
445 unsigned char frame_mask_index;
446 size_t rx_packet_length;
447 unsigned char opcode;
448 unsigned int final:1;
450 unsigned int frame_is_binary:1;
451 unsigned int all_zero_nonce:1;
452 short close_reason; /* enum lws_close_status */
453 unsigned char *rxflow_buffer;
456 unsigned int rxflow_change_to:2;
457 unsigned int this_frame_masked:1;
458 unsigned int inside_frame:1; /* next write will be more of frame */
459 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
462 struct libwebsocket {
464 /* lifetime members */
467 struct lws_io_watcher w_read;
468 struct lws_io_watcher w_write;
469 #endif /* LWS_USE_LIBEV */
470 const struct libwebsocket_protocols *protocol;
471 #ifndef LWS_NO_EXTENSIONS
472 struct libwebsocket_extension *
473 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
474 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
475 unsigned char count_active_extensions;
476 unsigned int extension_data_pending:1;
478 unsigned char ietf_spec_revision;
480 char mode; /* enum connection_mode */
481 char state; /* enum lws_connection_states */
482 char lws_rx_parse_state; /* enum lws_rx_parse_state */
483 char rx_frame_type; /* enum libwebsocket_write_protocol */
485 unsigned int hdr_parsing_completed:1;
487 char pending_timeout; /* enum pending_timeout */
488 time_t pending_timeout_limit;
491 int position_in_fds_table;
493 unsigned long action_start;
494 unsigned long latency_start;
497 /* truncated send handling */
498 unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */
499 unsigned int truncated_send_allocation; /* size of malloc */
500 unsigned int truncated_send_offset; /* where we are in terms of spilling */
501 unsigned int truncated_send_len; /* how much is buffered */
505 /* members with mutually exclusive lifetimes are unionized */
508 struct _lws_http_mode_related http;
509 struct _lws_header_related hdr;
510 struct _lws_websocket_related ws;
513 #ifdef LWS_OPENSSL_SUPPORT
516 unsigned int use_ssl:2;
520 BOOL sock_send_blocking;
525 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
526 struct libwebsocket *wsi, enum lws_close_status);
529 static inline void lws_latency(struct libwebsocket_context *context,
530 struct libwebsocket *wsi, const char *action,
531 int ret, int completion) { while (0); }
532 static inline void lws_latency_pre(struct libwebsocket_context *context,
533 struct libwebsocket *wsi) { while (0); }
535 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
537 lws_latency(struct libwebsocket_context *context,
538 struct libwebsocket *wsi, const char *action,
539 int ret, int completion);
543 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
546 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
549 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
550 unsigned char *buf, size_t len);
553 lws_b64_selftest(void);
555 LWS_EXTERN struct libwebsocket *
556 wsi_from_fd(struct libwebsocket_context *context, int fd);
559 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
560 struct libwebsocket *wsi);
563 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
567 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
568 struct libwebsocket *wsi, unsigned int sec);
570 LWS_EXTERN struct libwebsocket *
571 libwebsocket_client_connect_2(struct libwebsocket_context *context,
572 struct libwebsocket *wsi);
574 LWS_EXTERN struct libwebsocket *
575 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
578 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
579 struct libwebsocket *wsi, char *pkt);
582 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
583 struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd);
584 #ifndef LWS_NO_EXTENSIONS
586 lws_any_extension_handled(struct libwebsocket_context *context,
587 struct libwebsocket *wsi,
588 enum libwebsocket_extension_callback_reasons r,
589 void *v, size_t len);
592 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
593 struct libwebsocket_extension *ext);
597 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
598 struct libwebsocket *wsi);
601 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
604 lws_issue_raw_ext_access(struct libwebsocket *wsi,
605 unsigned char *buf, size_t len);
608 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
611 user_callback_handle_rxflow(callback_function,
612 struct libwebsocket_context *context,
613 struct libwebsocket *wsi,
614 enum libwebsocket_callback_reasons reason, void *user,
615 void *in, size_t len);
618 lws_set_socket_options(struct libwebsocket_context *context, int fd);
621 lws_allocate_header_table(struct libwebsocket *wsi);
624 lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
627 lws_hdr_simple_create(struct libwebsocket *wsi,
628 enum lws_token_indexes h, const char *s);
631 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
634 lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or);
636 #ifndef LWS_NO_SERVER
637 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
638 struct libwebsocket *wsi);
641 #ifndef LWS_NO_DAEMONIZE
642 LWS_EXTERN int get_daemonize_pid();
645 extern int interface_to_sa(struct libwebsocket_context *context,
646 const char *ifname, struct sockaddr_in *addr, size_t addrlen);
648 #ifndef LWS_OPENSSL_SUPPORT
651 SHA1(const unsigned char *d, size_t n, unsigned char *md);
655 LWS_EXTERN int openssl_websocket_private_data_index;