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)
58 #if defined(WIN32) || defined(_WIN32)
59 #define LWS_NO_DAEMONIZE
61 #define EWOULDBLOCK EAGAIN
64 #define EALREADY WSAEALREADY
67 #define EINPROGRESS WSAEINPROGRESS
70 #define EISCONN WSAEISCONN
73 #define compatible_close(fd) closesocket(fd);
85 #include <sys/types.h>
86 #include <sys/socket.h>
88 #ifdef HAVE_SYS_PRCTL_H
89 #include <sys/prctl.h>
92 #include <netinet/in.h>
93 #include <netinet/tcp.h>
94 #include <arpa/inet.h>
100 #define compatible_close(fd) close(fd);
103 #ifdef LWS_OPENSSL_SUPPORT
105 #include <cyassl/openssl/ssl.h>
106 #include <cyassl/error.h>
108 SHA1(const unsigned char *d, size_t n, unsigned char *md);
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 */
118 #include "libwebsockets.h"
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.
125 #define MSG_NOSIGNAL SO_NOSIGPIPE
128 #ifndef LWS_MAX_HEADER_LEN
129 #define LWS_MAX_HEADER_LEN 1024
131 #ifndef LWS_MAX_PROTOCOLS
132 #define LWS_MAX_PROTOCOLS 5
134 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
135 #define LWS_MAX_EXTENSIONS_ACTIVE 3
137 #ifndef SPEC_LATEST_SUPPORTED
138 #define SPEC_LATEST_SUPPORTED 13
140 #ifndef AWAITING_TIMEOUT
141 #define AWAITING_TIMEOUT 5
143 #ifndef CIPHERS_LIST_STRING
144 #define CIPHERS_LIST_STRING "DEFAULT"
146 #ifndef LWS_SOMAXCONN
147 #define LWS_SOMAXCONN SOMAXCONN
150 #define MAX_WEBSOCKET_04_KEY_LEN 128
151 #define LWS_MAX_SOCKET_IO_BUF 4096
153 #ifndef SYSTEM_RANDOM_FILEPATH
154 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
156 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
157 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
161 * if not in a connection storm, check for incoming
162 * connections this many normal connection services
164 #define LWS_LISTEN_SERVICE_MODULO 10
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,
171 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
173 /* control extensions 8+ */
175 LWS_WS_OPCODE_07__CLOSE = 8,
176 LWS_WS_OPCODE_07__PING = 9,
177 LWS_WS_OPCODE_07__PONG = 0xa,
181 enum lws_connection_states {
183 WSI_STATE_HTTP_ISSUING_FILE,
184 WSI_STATE_HTTP_HEADERS,
185 WSI_STATE_DEAD_SOCKET,
186 WSI_STATE_ESTABLISHED,
187 WSI_STATE_CLIENT_UNCONNECTED,
188 WSI_STATE_RETURNED_CLOSE_ALREADY,
189 WSI_STATE_AWAITING_CLOSE_ACK,
192 enum lws_rx_parse_state {
195 LWS_RXPS_04_MASK_NONCE_1,
196 LWS_RXPS_04_MASK_NONCE_2,
197 LWS_RXPS_04_MASK_NONCE_3,
199 LWS_RXPS_04_FRAME_HDR_1,
200 LWS_RXPS_04_FRAME_HDR_LEN,
201 LWS_RXPS_04_FRAME_HDR_LEN16_2,
202 LWS_RXPS_04_FRAME_HDR_LEN16_1,
203 LWS_RXPS_04_FRAME_HDR_LEN64_8,
204 LWS_RXPS_04_FRAME_HDR_LEN64_7,
205 LWS_RXPS_04_FRAME_HDR_LEN64_6,
206 LWS_RXPS_04_FRAME_HDR_LEN64_5,
207 LWS_RXPS_04_FRAME_HDR_LEN64_4,
208 LWS_RXPS_04_FRAME_HDR_LEN64_3,
209 LWS_RXPS_04_FRAME_HDR_LEN64_2,
210 LWS_RXPS_04_FRAME_HDR_LEN64_1,
212 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
213 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
214 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
215 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
217 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
221 enum connection_mode {
222 LWS_CONNMODE_HTTP_SERVING,
223 LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
224 LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
226 LWS_CONNMODE_WS_SERVING,
227 LWS_CONNMODE_WS_CLIENT,
229 /* transient, ssl delay hiding */
230 LWS_CONNMODE_SSL_ACK_PENDING,
232 /* transient modes */
233 LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
234 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
235 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
236 LWS_CONNMODE_WS_CLIENT_WAITING_SSL,
237 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
238 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
239 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
241 /* special internal types */
242 LWS_CONNMODE_SERVER_LISTENER,
246 LWS_RXFLOW_ALLOW = (1 << 0),
247 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
250 struct libwebsocket_protocols;
253 struct libwebsocket_context {
255 struct libwebsocket **lws_lookup; /* fd to wsi */
259 char http_proxy_address[128];
260 char canonical_hostname[128];
261 unsigned int http_proxy_port;
262 unsigned int options;
263 unsigned long last_timeout_check_s;
266 * usable by anything in the service code, but only if the scope
267 * does not last longer than the service action (since next service
268 * of any socket can likewise use it and overwrite)
270 unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
272 int started_with_parent;
275 int listen_service_modulo;
276 int listen_service_count;
277 int listen_service_fd;
278 int listen_service_extraseen;
285 unsigned long worst_latency;
286 char worst_latency_info[256];
289 #ifdef LWS_OPENSSL_SUPPORT
292 SSL_CTX *ssl_client_ctx;
294 struct libwebsocket_protocols *protocols;
296 #ifndef LWS_NO_EXTENSIONS
297 struct libwebsocket_extension *extensions;
304 * This is totally opaque to code using the library. It's exported as a
305 * forward-reference pointer-only declaration; the user can use the pointer with
306 * other APIs to get information out of it.
309 struct _lws_http_mode_related {
311 unsigned long filepos;
312 unsigned long filelen;
315 struct lws_fragments {
316 unsigned short offset;
318 unsigned char next_frag_index;
321 struct allocated_headers {
322 unsigned short next_frag_index;
324 unsigned char frag_index[WSI_TOKEN_COUNT];
325 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
326 char data[LWS_MAX_HEADER_LEN];
327 #ifndef LWS_NO_CLIENT
328 char initial_handshake_hash_base64[30];
329 unsigned short c_port;
333 struct _lws_header_related {
334 struct allocated_headers *ah;
336 unsigned char parser_state; /* enum lws_token_indexes */
339 struct _lws_websocket_related {
340 char *rx_user_buffer;
341 int rx_user_buffer_head;
342 unsigned char frame_masking_nonce_04[4];
343 unsigned char frame_mask_index;
344 size_t rx_packet_length;
345 unsigned char opcode;
346 unsigned int final:1;
348 unsigned int frame_is_binary:1;
349 unsigned int all_zero_nonce:1;
350 short close_reason; /* enum lws_close_status */
351 unsigned char *rxflow_buffer;
354 unsigned int rxflow_change_to:2;
355 unsigned int this_frame_masked:1;
356 unsigned int inside_frame:1; /* next write will be more of frame */
357 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
358 /* truncated send handling */
359 unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */
360 unsigned int truncated_send_offset; /* where we are in terms of spilling */
361 unsigned int truncated_send_len; /* how much is buffered */
364 struct libwebsocket {
366 /* lifetime members */
368 const struct libwebsocket_protocols *protocol;
369 #ifndef LWS_NO_EXTENSIONS
370 struct libwebsocket_extension *
371 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
372 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
373 unsigned char count_active_extensions;
374 unsigned int extension_data_pending:1;
376 unsigned char ietf_spec_revision;
378 char mode; /* enum connection_mode */
379 char state; /* enum lws_connection_states */
380 char lws_rx_parse_state; /* enum lws_rx_parse_state */
381 char rx_frame_type; /* enum libwebsocket_write_protocol */
383 unsigned int hdr_parsing_completed:1;
385 char pending_timeout; /* enum pending_timeout */
386 unsigned long pending_timeout_limit;
389 int position_in_fds_table;
391 unsigned long action_start;
392 unsigned long latency_start;
397 /* members with mutually exclusive lifetimes are unionized */
400 struct _lws_http_mode_related http;
401 struct _lws_header_related hdr;
402 struct _lws_websocket_related ws;
405 #ifdef LWS_OPENSSL_SUPPORT
408 unsigned int use_ssl:2;
413 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
414 struct libwebsocket *wsi, enum lws_close_status);
417 static inline void lws_latency(struct libwebsocket_context *context,
418 struct libwebsocket *wsi, const char *action,
419 int ret, int completion) { while (0); }
420 static inline void lws_latency_pre(struct libwebsocket_context *context,
421 struct libwebsocket *wsi) { while (0); }
423 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
425 lws_latency(struct libwebsocket_context *context,
426 struct libwebsocket *wsi, const char *action,
427 int ret, int completion);
431 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
434 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
437 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
438 unsigned char *buf, size_t len);
441 lws_b64_selftest(void);
443 LWS_EXTERN struct libwebsocket *
444 wsi_from_fd(struct libwebsocket_context *context, int fd);
447 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
448 struct libwebsocket *wsi);
451 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
455 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
456 struct libwebsocket *wsi, unsigned int sec);
458 LWS_EXTERN struct libwebsocket *
459 libwebsocket_client_connect_2(struct libwebsocket_context *context,
460 struct libwebsocket *wsi);
462 LWS_EXTERN struct libwebsocket *
463 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
466 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
467 struct libwebsocket *wsi, char *pkt);
470 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
471 struct libwebsocket *wsi, struct pollfd *pollfd);
472 #ifndef LWS_NO_EXTENSIONS
474 lws_any_extension_handled(struct libwebsocket_context *context,
475 struct libwebsocket *wsi,
476 enum libwebsocket_extension_callback_reasons r,
477 void *v, size_t len);
480 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
481 struct libwebsocket_extension *ext);
485 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
486 struct libwebsocket *wsi);
489 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
492 lws_issue_raw_ext_access(struct libwebsocket *wsi,
493 unsigned char *buf, size_t len);
496 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
499 user_callback_handle_rxflow(callback_function,
500 struct libwebsocket_context *context,
501 struct libwebsocket *wsi,
502 enum libwebsocket_callback_reasons reason, void *user,
503 void *in, size_t len);
506 lws_set_socket_options(struct libwebsocket_context *context, int fd);
509 lws_allocate_header_table(struct libwebsocket *wsi);
512 lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
515 lws_hdr_simple_create(struct libwebsocket *wsi,
516 enum lws_token_indexes h, const char *s);
519 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
521 #ifndef LWS_NO_SERVER
522 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
523 struct libwebsocket *wsi);
526 #ifndef LWS_NO_DAEMONIZE
527 LWS_EXTERN int get_daemonize_pid();
530 extern int interface_to_sa(const char *ifname,
531 struct sockaddr_in *addr, size_t addrlen);
533 #ifndef LWS_OPENSSL_SUPPORT
536 SHA1(const unsigned char *d, size_t n, unsigned char *md);
540 LWS_EXTERN int openssl_websocket_private_data_index;