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)
49 #elif _MSC_VER > 1000 || defined(_WIN32)
56 #ifdef HAVE_SYS_STAT_H
60 #if defined(WIN32) || defined(_WIN32)
61 #define LWS_NO_DAEMONIZE
63 #define EWOULDBLOCK EAGAIN
66 #define EALREADY WSAEALREADY
69 #define EINPROGRESS WSAEINPROGRESS
72 #define EISCONN WSAEISCONN
75 #define compatible_close(fd) closesocket(fd);
85 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
87 #include <sys/types.h>
88 #include <sys/socket.h>
90 #ifdef HAVE_SYS_PRCTL_H
91 #include <sys/prctl.h>
94 #include <netinet/in.h>
95 #include <netinet/tcp.h>
96 #include <arpa/inet.h>
101 #include <sys/mman.h>
102 #include <sys/time.h>
104 #define LWS_INVALID_FILE -1
105 #define compatible_close(fd) close(fd);
108 #ifndef HAVE_STRERROR
109 #define strerror(x) ""
112 #ifdef LWS_OPENSSL_SUPPORT
114 #include <cyassl/openssl/ssl.h>
115 #include <cyassl/error.h>
117 SHA1(const unsigned char *d, size_t n, unsigned char *md);
119 #include <openssl/ssl.h>
120 #include <openssl/evp.h>
121 #include <openssl/err.h>
122 #include <openssl/md5.h>
123 #include <openssl/sha.h>
124 #endif /* not USE_CYASSL */
127 #include "libwebsockets.h"
130 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
131 * but happily have something equivalent in the SO_NOSIGPIPE flag.
134 #define MSG_NOSIGNAL SO_NOSIGPIPE
137 #ifndef LWS_MAX_HEADER_LEN
138 #define LWS_MAX_HEADER_LEN 1024
140 #ifndef LWS_MAX_PROTOCOLS
141 #define LWS_MAX_PROTOCOLS 5
143 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
144 #define LWS_MAX_EXTENSIONS_ACTIVE 3
146 #ifndef SPEC_LATEST_SUPPORTED
147 #define SPEC_LATEST_SUPPORTED 13
149 #ifndef AWAITING_TIMEOUT
150 #define AWAITING_TIMEOUT 5
152 #ifndef CIPHERS_LIST_STRING
153 #define CIPHERS_LIST_STRING "DEFAULT"
155 #ifndef LWS_SOMAXCONN
156 #define LWS_SOMAXCONN SOMAXCONN
159 #define MAX_WEBSOCKET_04_KEY_LEN 128
160 #define LWS_MAX_SOCKET_IO_BUF 4096
162 #ifndef SYSTEM_RANDOM_FILEPATH
163 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
165 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
166 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
170 * if not in a connection storm, check for incoming
171 * connections this many normal connection services
173 #define LWS_LISTEN_SERVICE_MODULO 10
175 enum lws_websocket_opcodes_07 {
176 LWS_WS_OPCODE_07__CONTINUATION = 0,
177 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
178 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
180 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
182 /* control extensions 8+ */
184 LWS_WS_OPCODE_07__CLOSE = 8,
185 LWS_WS_OPCODE_07__PING = 9,
186 LWS_WS_OPCODE_07__PONG = 0xa,
190 enum lws_connection_states {
192 WSI_STATE_HTTP_ISSUING_FILE,
193 WSI_STATE_HTTP_HEADERS,
195 WSI_STATE_DEAD_SOCKET,
196 WSI_STATE_ESTABLISHED,
197 WSI_STATE_CLIENT_UNCONNECTED,
198 WSI_STATE_RETURNED_CLOSE_ALREADY,
199 WSI_STATE_AWAITING_CLOSE_ACK,
202 enum lws_rx_parse_state {
205 LWS_RXPS_04_MASK_NONCE_1,
206 LWS_RXPS_04_MASK_NONCE_2,
207 LWS_RXPS_04_MASK_NONCE_3,
209 LWS_RXPS_04_FRAME_HDR_1,
210 LWS_RXPS_04_FRAME_HDR_LEN,
211 LWS_RXPS_04_FRAME_HDR_LEN16_2,
212 LWS_RXPS_04_FRAME_HDR_LEN16_1,
213 LWS_RXPS_04_FRAME_HDR_LEN64_8,
214 LWS_RXPS_04_FRAME_HDR_LEN64_7,
215 LWS_RXPS_04_FRAME_HDR_LEN64_6,
216 LWS_RXPS_04_FRAME_HDR_LEN64_5,
217 LWS_RXPS_04_FRAME_HDR_LEN64_4,
218 LWS_RXPS_04_FRAME_HDR_LEN64_3,
219 LWS_RXPS_04_FRAME_HDR_LEN64_2,
220 LWS_RXPS_04_FRAME_HDR_LEN64_1,
222 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
223 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
224 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
225 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
227 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
231 enum connection_mode {
232 LWS_CONNMODE_HTTP_SERVING,
233 LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
234 LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
236 LWS_CONNMODE_WS_SERVING,
237 LWS_CONNMODE_WS_CLIENT,
239 /* transient, ssl delay hiding */
240 LWS_CONNMODE_SSL_ACK_PENDING,
242 /* transient modes */
243 LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
244 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
245 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
246 LWS_CONNMODE_WS_CLIENT_WAITING_SSL,
247 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
248 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
249 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
251 /* special internal types */
252 LWS_CONNMODE_SERVER_LISTENER,
256 LWS_RXFLOW_ALLOW = (1 << 0),
257 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
260 struct libwebsocket_protocols;
263 struct libwebsocket_context {
265 struct libwebsocket **lws_lookup; /* fd to wsi */
270 char http_proxy_address[128];
271 char canonical_hostname[128];
272 unsigned int http_proxy_port;
273 unsigned int options;
274 time_t last_timeout_check_s;
277 * usable by anything in the service code, but only if the scope
278 * does not last longer than the service action (since next service
279 * of any socket can likewise use it and overwrite)
281 unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
283 int started_with_parent;
286 int listen_service_modulo;
287 int listen_service_count;
288 int listen_service_fd;
289 int listen_service_extraseen;
296 unsigned long worst_latency;
297 char worst_latency_info[256];
300 #ifdef LWS_OPENSSL_SUPPORT
302 int allow_non_ssl_on_ssl_port;
304 SSL_CTX *ssl_client_ctx;
306 struct libwebsocket_protocols *protocols;
308 #ifndef LWS_NO_EXTENSIONS
309 struct libwebsocket_extension *extensions;
314 enum uri_path_states {
317 URIPS_SEEN_SLASH_DOT,
318 URIPS_SEEN_SLASH_DOT_DOT,
322 enum uri_esc_states {
325 URIES_SEEN_PERCENT_H1,
329 * This is totally opaque to code using the library. It's exported as a
330 * forward-reference pointer-only declaration; the user can use the pointer with
331 * other APIs to get information out of it.
334 struct lws_fragments {
335 unsigned short offset;
337 unsigned char next_frag_index;
340 struct allocated_headers {
341 unsigned short next_frag_index;
343 unsigned char frag_index[WSI_TOKEN_COUNT];
344 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
345 char data[LWS_MAX_HEADER_LEN];
346 #ifndef LWS_NO_CLIENT
347 char initial_handshake_hash_base64[30];
348 unsigned short c_port;
352 struct _lws_http_mode_related {
353 struct allocated_headers *ah; /* mirroring _lws_header_related */
354 #if defined(WIN32) || defined(_WIN32)
359 unsigned long filepos;
360 unsigned long filelen;
363 int content_length_seen;
365 unsigned char *post_buffer;
368 struct _lws_header_related {
369 struct allocated_headers *ah;
371 unsigned char parser_state; /* enum lws_token_indexes */
372 enum uri_path_states ups;
373 enum uri_esc_states ues;
377 struct _lws_websocket_related {
378 char *rx_user_buffer;
379 int rx_user_buffer_head;
380 unsigned char frame_masking_nonce_04[4];
381 unsigned char frame_mask_index;
382 size_t rx_packet_length;
383 unsigned char opcode;
384 unsigned int final:1;
386 unsigned int frame_is_binary:1;
387 unsigned int all_zero_nonce:1;
388 short close_reason; /* enum lws_close_status */
389 unsigned char *rxflow_buffer;
392 unsigned int rxflow_change_to:2;
393 unsigned int this_frame_masked:1;
394 unsigned int inside_frame:1; /* next write will be more of frame */
395 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
398 struct libwebsocket {
400 /* lifetime members */
402 const struct libwebsocket_protocols *protocol;
403 #ifndef LWS_NO_EXTENSIONS
404 struct libwebsocket_extension *
405 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
406 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
407 unsigned char count_active_extensions;
408 unsigned int extension_data_pending:1;
410 unsigned char ietf_spec_revision;
412 char mode; /* enum connection_mode */
413 char state; /* enum lws_connection_states */
414 char lws_rx_parse_state; /* enum lws_rx_parse_state */
415 char rx_frame_type; /* enum libwebsocket_write_protocol */
417 unsigned int hdr_parsing_completed:1;
419 char pending_timeout; /* enum pending_timeout */
420 time_t pending_timeout_limit;
423 int position_in_fds_table;
425 unsigned long action_start;
426 unsigned long latency_start;
429 /* truncated send handling */
430 unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */
431 unsigned int truncated_send_offset; /* where we are in terms of spilling */
432 unsigned int truncated_send_len; /* how much is buffered */
436 /* members with mutually exclusive lifetimes are unionized */
439 struct _lws_http_mode_related http;
440 struct _lws_header_related hdr;
441 struct _lws_websocket_related ws;
444 #ifdef LWS_OPENSSL_SUPPORT
447 unsigned int use_ssl:2;
452 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
453 struct libwebsocket *wsi, enum lws_close_status);
456 static inline void lws_latency(struct libwebsocket_context *context,
457 struct libwebsocket *wsi, const char *action,
458 int ret, int completion) { while (0); }
459 static inline void lws_latency_pre(struct libwebsocket_context *context,
460 struct libwebsocket *wsi) { while (0); }
462 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
464 lws_latency(struct libwebsocket_context *context,
465 struct libwebsocket *wsi, const char *action,
466 int ret, int completion);
470 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
473 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
476 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
477 unsigned char *buf, size_t len);
480 lws_b64_selftest(void);
482 LWS_EXTERN struct libwebsocket *
483 wsi_from_fd(struct libwebsocket_context *context, int fd);
486 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
487 struct libwebsocket *wsi);
490 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
494 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
495 struct libwebsocket *wsi, unsigned int sec);
497 LWS_EXTERN struct libwebsocket *
498 libwebsocket_client_connect_2(struct libwebsocket_context *context,
499 struct libwebsocket *wsi);
501 LWS_EXTERN struct libwebsocket *
502 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
505 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
506 struct libwebsocket *wsi, char *pkt);
509 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
510 struct libwebsocket *wsi, struct pollfd *pollfd);
511 #ifndef LWS_NO_EXTENSIONS
513 lws_any_extension_handled(struct libwebsocket_context *context,
514 struct libwebsocket *wsi,
515 enum libwebsocket_extension_callback_reasons r,
516 void *v, size_t len);
519 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
520 struct libwebsocket_extension *ext);
524 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
525 struct libwebsocket *wsi);
528 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
531 lws_issue_raw_ext_access(struct libwebsocket *wsi,
532 unsigned char *buf, size_t len);
535 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
538 user_callback_handle_rxflow(callback_function,
539 struct libwebsocket_context *context,
540 struct libwebsocket *wsi,
541 enum libwebsocket_callback_reasons reason, void *user,
542 void *in, size_t len);
545 lws_set_socket_options(struct libwebsocket_context *context, int fd);
548 lws_allocate_header_table(struct libwebsocket *wsi);
551 lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
554 lws_hdr_simple_create(struct libwebsocket *wsi,
555 enum lws_token_indexes h, const char *s);
558 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
561 lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or);
563 #ifndef LWS_NO_SERVER
564 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
565 struct libwebsocket *wsi);
568 #ifndef LWS_NO_DAEMONIZE
569 LWS_EXTERN int get_daemonize_pid();
572 extern int interface_to_sa(const char *ifname,
573 struct sockaddr_in *addr, size_t addrlen);
575 #ifndef LWS_OPENSSL_SUPPORT
578 SHA1(const unsigned char *d, size_t n, unsigned char *md);
582 LWS_EXTERN int openssl_websocket_private_data_index;