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"
27 #define inline __inline
33 #if _MSC_VER > 1000 || defined(_WIN32)
49 #elif _MSC_VER > 1000 || defined(_WIN32)
59 #define LWS_NO_DAEMONIZE
61 #define EWOULDBLOCK EAGAIN
64 #define compatible_close(fd) closesocket(fd);
76 #include <sys/types.h>
77 #include <sys/socket.h>
79 #ifdef HAVE_SYS_PRCTL_H
80 #include <sys/prctl.h>
83 #include <netinet/in.h>
84 #include <netinet/tcp.h>
85 #include <arpa/inet.h>
91 #define compatible_close(fd) close(fd);
94 #ifdef LWS_OPENSSL_SUPPORT
96 #include <cyassl/openssl/ssl.h>
97 #include <cyassl/error.h>
99 SHA1(const unsigned char *d, size_t n, unsigned char *md);
101 #include <openssl/ssl.h>
102 #include <openssl/evp.h>
103 #include <openssl/err.h>
104 #include <openssl/md5.h>
105 #include <openssl/sha.h>
106 #endif /* not USE_CYASSL */
109 #include "libwebsockets.h"
112 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
113 * but happily have something equivalent in the SO_NOSIGPIPE flag.
116 #define MSG_NOSIGNAL SO_NOSIGPIPE
119 #ifndef LWS_MAX_HEADER_LEN
120 #define LWS_MAX_HEADER_LEN 1024
122 #ifndef LWS_MAX_PROTOCOLS
123 #define LWS_MAX_PROTOCOLS 5
125 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
126 #define LWS_MAX_EXTENSIONS_ACTIVE 3
128 #ifndef SPEC_LATEST_SUPPORTED
129 #define SPEC_LATEST_SUPPORTED 13
131 #ifndef AWAITING_TIMEOUT
132 #define AWAITING_TIMEOUT 5
134 #ifndef CIPHERS_LIST_STRING
135 #define CIPHERS_LIST_STRING "DEFAULT"
137 #ifndef LWS_SOMAXCONN
138 #define LWS_SOMAXCONN SOMAXCONN
141 #define MAX_WEBSOCKET_04_KEY_LEN 128
142 #define LWS_MAX_SOCKET_IO_BUF 4096
144 #ifndef SYSTEM_RANDOM_FILEPATH
145 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
147 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
148 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
152 * if not in a connection storm, check for incoming
153 * connections this many normal connection services
155 #define LWS_LISTEN_SERVICE_MODULO 10
157 enum lws_websocket_opcodes_07 {
158 LWS_WS_OPCODE_07__CONTINUATION = 0,
159 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
160 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
162 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
164 /* control extensions 8+ */
166 LWS_WS_OPCODE_07__CLOSE = 8,
167 LWS_WS_OPCODE_07__PING = 9,
168 LWS_WS_OPCODE_07__PONG = 0xa,
172 enum lws_connection_states {
174 WSI_STATE_HTTP_ISSUING_FILE,
175 WSI_STATE_HTTP_HEADERS,
176 WSI_STATE_DEAD_SOCKET,
177 WSI_STATE_ESTABLISHED,
178 WSI_STATE_CLIENT_UNCONNECTED,
179 WSI_STATE_RETURNED_CLOSE_ALREADY,
180 WSI_STATE_AWAITING_CLOSE_ACK,
183 enum lws_rx_parse_state {
186 LWS_RXPS_04_MASK_NONCE_1,
187 LWS_RXPS_04_MASK_NONCE_2,
188 LWS_RXPS_04_MASK_NONCE_3,
190 LWS_RXPS_04_FRAME_HDR_1,
191 LWS_RXPS_04_FRAME_HDR_LEN,
192 LWS_RXPS_04_FRAME_HDR_LEN16_2,
193 LWS_RXPS_04_FRAME_HDR_LEN16_1,
194 LWS_RXPS_04_FRAME_HDR_LEN64_8,
195 LWS_RXPS_04_FRAME_HDR_LEN64_7,
196 LWS_RXPS_04_FRAME_HDR_LEN64_6,
197 LWS_RXPS_04_FRAME_HDR_LEN64_5,
198 LWS_RXPS_04_FRAME_HDR_LEN64_4,
199 LWS_RXPS_04_FRAME_HDR_LEN64_3,
200 LWS_RXPS_04_FRAME_HDR_LEN64_2,
201 LWS_RXPS_04_FRAME_HDR_LEN64_1,
203 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
204 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
205 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
206 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
208 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
212 enum connection_mode {
213 LWS_CONNMODE_HTTP_SERVING,
214 LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
215 LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
217 LWS_CONNMODE_WS_SERVING,
218 LWS_CONNMODE_WS_CLIENT,
220 /* transient, ssl delay hiding */
221 LWS_CONNMODE_SSL_ACK_PENDING,
223 /* transient modes */
224 LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
225 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
226 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
227 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
228 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
229 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
231 /* special internal types */
232 LWS_CONNMODE_SERVER_LISTENER,
236 LWS_RXFLOW_ALLOW = (1 << 0),
237 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
240 struct libwebsocket_protocols;
243 struct libwebsocket_context {
245 struct libwebsocket **lws_lookup; /* fd to wsi */
249 char http_proxy_address[128];
250 char canonical_hostname[128];
251 unsigned int http_proxy_port;
252 unsigned int options;
253 unsigned long last_timeout_check_s;
256 * usable by anything in the service code, but only if the scope
257 * does not last longer than the service action (since next service
258 * of any socket can likewise use it and overwrite)
260 unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
262 int started_with_parent;
265 int listen_service_modulo;
266 int listen_service_count;
267 int listen_service_fd;
268 int listen_service_extraseen;
275 unsigned long worst_latency;
276 char worst_latency_info[256];
279 #ifdef LWS_OPENSSL_SUPPORT
282 SSL_CTX *ssl_client_ctx;
284 struct libwebsocket_protocols *protocols;
286 #ifndef LWS_NO_EXTENSIONS
287 struct libwebsocket_extension *extensions;
294 * This is totally opaque to code using the library. It's exported as a
295 * forward-reference pointer-only declaration; the user can use the pointer with
296 * other APIs to get information out of it.
299 struct _lws_http_mode_related {
301 unsigned long filepos;
302 unsigned long filelen;
305 struct lws_fragments {
306 unsigned short offset;
308 unsigned char next_frag_index;
311 struct allocated_headers {
312 unsigned short next_frag_index;
314 unsigned char frag_index[WSI_TOKEN_COUNT];
315 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
316 char data[LWS_MAX_HEADER_LEN];
317 #ifndef LWS_NO_CLIENT
318 char initial_handshake_hash_base64[30];
319 unsigned short c_port;
323 struct _lws_header_related {
324 struct allocated_headers *ah;
326 unsigned char parser_state; /* enum lws_token_indexes */
329 struct _lws_websocket_related {
330 char *rx_user_buffer;
331 int rx_user_buffer_head;
332 unsigned char frame_masking_nonce_04[4];
333 unsigned char frame_mask_index;
334 size_t rx_packet_length;
335 unsigned char opcode;
336 unsigned int final:1;
338 unsigned int frame_is_binary:1;
339 unsigned int all_zero_nonce:1;
340 short close_reason; /* enum lws_close_status */
341 unsigned char *rxflow_buffer;
344 unsigned int rxflow_change_to:2;
345 unsigned int this_frame_masked:1;
348 struct libwebsocket {
350 /* lifetime members */
352 const struct libwebsocket_protocols *protocol;
353 #ifndef LWS_NO_EXTENSIONS
354 struct libwebsocket_extension *
355 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
356 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
357 unsigned char count_active_extensions;
358 unsigned int extension_data_pending:1;
360 unsigned char ietf_spec_revision;
362 char mode; /* enum connection_mode */
363 char state; /* enum lws_connection_states */
364 char lws_rx_parse_state; /* enum lws_rx_parse_state */
365 char rx_frame_type; /* enum libwebsocket_write_protocol */
367 unsigned int hdr_parsing_completed:1;
369 char pending_timeout; /* enum pending_timeout */
370 unsigned long pending_timeout_limit;
373 int position_in_fds_table;
375 unsigned long action_start;
376 unsigned long latency_start;
381 /* members with mutually exclusive lifetimes are unionized */
384 struct _lws_http_mode_related http;
385 struct _lws_header_related hdr;
386 struct _lws_websocket_related ws;
389 #ifdef LWS_OPENSSL_SUPPORT
392 unsigned int use_ssl:2;
397 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
398 struct libwebsocket *wsi, enum lws_close_status);
401 static inline void lws_latency(struct libwebsocket_context *context,
402 struct libwebsocket *wsi, const char *action,
403 int ret, int completion) { while (0); }
404 static inline void lws_latency_pre(struct libwebsocket_context *context,
405 struct libwebsocket *wsi) { while (0); }
407 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
409 lws_latency(struct libwebsocket_context *context,
410 struct libwebsocket *wsi, const char *action,
411 int ret, int completion);
415 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
418 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
421 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
422 unsigned char *buf, size_t len);
425 lws_b64_selftest(void);
427 LWS_EXTERN struct libwebsocket *
428 wsi_from_fd(struct libwebsocket_context *context, int fd);
431 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
432 struct libwebsocket *wsi);
435 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
439 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
440 struct libwebsocket *wsi, unsigned int sec);
442 LWS_EXTERN struct libwebsocket *
443 __libwebsocket_client_connect_2(struct libwebsocket_context *context,
444 struct libwebsocket *wsi);
446 LWS_EXTERN struct libwebsocket *
447 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
450 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
451 struct libwebsocket *wsi, char *pkt);
454 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
455 struct libwebsocket *wsi, struct pollfd *pollfd);
456 #ifndef LWS_NO_EXTENSIONS
458 lws_any_extension_handled(struct libwebsocket_context *context,
459 struct libwebsocket *wsi,
460 enum libwebsocket_extension_callback_reasons r,
461 void *v, size_t len);
464 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
465 struct libwebsocket_extension *ext);
469 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
470 struct libwebsocket *wsi);
473 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
476 lws_issue_raw_ext_access(struct libwebsocket *wsi,
477 unsigned char *buf, size_t len);
480 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
483 user_callback_handle_rxflow(callback_function,
484 struct libwebsocket_context *context,
485 struct libwebsocket *wsi,
486 enum libwebsocket_callback_reasons reason, void *user,
487 void *in, size_t len);
490 lws_set_socket_options(struct libwebsocket_context *context, int fd);
493 lws_allocate_header_table(struct libwebsocket *wsi);
496 lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
499 lws_hdr_simple_create(struct libwebsocket *wsi,
500 enum lws_token_indexes h, const char *s);
503 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
505 #ifndef LWS_NO_SERVER
506 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
507 struct libwebsocket *wsi);
510 #ifndef LWS_NO_DAEMONIZE
511 LWS_EXTERN int get_daemonize_pid();
514 extern int interface_to_sa(const char *ifname,
515 struct sockaddr_in *addr, size_t addrlen);
517 #ifndef LWS_OPENSSL_SUPPORT
520 SHA1(const unsigned char *d, size_t n, unsigned char *md);
524 LWS_EXTERN int openssl_websocket_private_data_index;