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 compatible_close(fd) closesocket(fd);
73 #include <sys/types.h>
74 #include <sys/socket.h>
76 #ifdef HAVE_SYS_PRCTL_H
77 #include <sys/prctl.h>
80 #include <netinet/in.h>
81 #include <netinet/tcp.h>
82 #include <arpa/inet.h>
88 #define compatible_close(fd) close(fd);
91 #ifdef LWS_OPENSSL_SUPPORT
93 #include <cyassl/openssl/ssl.h>
94 #include <cyassl/error.h>
96 SHA1(const unsigned char *d, size_t n, unsigned char *md);
98 #include <openssl/ssl.h>
99 #include <openssl/evp.h>
100 #include <openssl/err.h>
101 #include <openssl/md5.h>
102 #include <openssl/sha.h>
103 #endif /* not USE_CYASSL */
106 #include "libwebsockets.h"
109 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
110 * but happily have something equivalent in the SO_NOSIGPIPE flag.
113 #define MSG_NOSIGNAL SO_NOSIGPIPE
116 #ifndef LWS_MAX_HEADER_LEN
117 #define LWS_MAX_HEADER_LEN 1024
119 #ifndef LWS_MAX_PROTOCOLS
120 #define LWS_MAX_PROTOCOLS 5
122 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
123 #define LWS_MAX_EXTENSIONS_ACTIVE 3
125 #ifndef SPEC_LATEST_SUPPORTED
126 #define SPEC_LATEST_SUPPORTED 13
128 #ifndef AWAITING_TIMEOUT
129 #define AWAITING_TIMEOUT 5
131 #ifndef CIPHERS_LIST_STRING
132 #define CIPHERS_LIST_STRING "DEFAULT"
134 #ifndef LWS_SOMAXCONN
135 #define LWS_SOMAXCONN SOMAXCONN
138 #define MAX_WEBSOCKET_04_KEY_LEN 128
139 #define LWS_MAX_SOCKET_IO_BUF 4096
141 #ifndef SYSTEM_RANDOM_FILEPATH
142 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
144 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
145 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
149 * if not in a connection storm, check for incoming
150 * connections this many normal connection services
152 #define LWS_LISTEN_SERVICE_MODULO 10
154 enum lws_websocket_opcodes_07 {
155 LWS_WS_OPCODE_07__CONTINUATION = 0,
156 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
157 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
159 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
161 /* control extensions 8+ */
163 LWS_WS_OPCODE_07__CLOSE = 8,
164 LWS_WS_OPCODE_07__PING = 9,
165 LWS_WS_OPCODE_07__PONG = 0xa,
169 enum lws_connection_states {
171 WSI_STATE_HTTP_ISSUING_FILE,
172 WSI_STATE_HTTP_HEADERS,
173 WSI_STATE_DEAD_SOCKET,
174 WSI_STATE_ESTABLISHED,
175 WSI_STATE_CLIENT_UNCONNECTED,
176 WSI_STATE_RETURNED_CLOSE_ALREADY,
177 WSI_STATE_AWAITING_CLOSE_ACK,
180 enum lws_rx_parse_state {
183 LWS_RXPS_04_MASK_NONCE_1,
184 LWS_RXPS_04_MASK_NONCE_2,
185 LWS_RXPS_04_MASK_NONCE_3,
187 LWS_RXPS_04_FRAME_HDR_1,
188 LWS_RXPS_04_FRAME_HDR_LEN,
189 LWS_RXPS_04_FRAME_HDR_LEN16_2,
190 LWS_RXPS_04_FRAME_HDR_LEN16_1,
191 LWS_RXPS_04_FRAME_HDR_LEN64_8,
192 LWS_RXPS_04_FRAME_HDR_LEN64_7,
193 LWS_RXPS_04_FRAME_HDR_LEN64_6,
194 LWS_RXPS_04_FRAME_HDR_LEN64_5,
195 LWS_RXPS_04_FRAME_HDR_LEN64_4,
196 LWS_RXPS_04_FRAME_HDR_LEN64_3,
197 LWS_RXPS_04_FRAME_HDR_LEN64_2,
198 LWS_RXPS_04_FRAME_HDR_LEN64_1,
200 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
201 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
202 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
203 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
205 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
209 enum connection_mode {
210 LWS_CONNMODE_HTTP_SERVING,
212 LWS_CONNMODE_WS_SERVING,
213 LWS_CONNMODE_WS_CLIENT,
215 /* transient, ssl delay hiding */
216 LWS_CONNMODE_SSL_ACK_PENDING,
218 /* transient modes */
219 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
220 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
221 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
222 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
223 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
225 /* special internal types */
226 LWS_CONNMODE_SERVER_LISTENER,
229 struct libwebsocket_protocols;
232 struct libwebsocket_context {
234 struct libwebsocket **lws_lookup; /* fd to wsi */
238 char http_proxy_address[128];
239 char canonical_hostname[128];
240 unsigned int http_proxy_port;
241 unsigned int options;
242 unsigned long last_timeout_check_s;
245 * usable by anything in the service code, but only if the scope
246 * does not last longer than the service action (since next service
247 * of any socket can likewise use it and overwrite)
249 unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
251 int started_with_parent;
254 int listen_service_modulo;
255 int listen_service_count;
256 int listen_service_fd;
257 int listen_service_extraseen;
264 unsigned long worst_latency;
265 char worst_latency_info[256];
268 #ifdef LWS_OPENSSL_SUPPORT
271 SSL_CTX *ssl_client_ctx;
273 struct libwebsocket_protocols *protocols;
275 #ifndef LWS_NO_EXTENSIONS
276 struct libwebsocket_extension *extensions;
282 enum pending_timeout {
283 NO_PENDING_TIMEOUT = 0,
284 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
285 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
286 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
287 PENDING_TIMEOUT_AWAITING_PING,
288 PENDING_TIMEOUT_CLOSE_ACK,
289 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
290 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
291 PENDING_TIMEOUT_SSL_ACCEPT,
296 * This is totally opaque to code using the library. It's exported as a
297 * forward-reference pointer-only declaration; the user can use the pointer with
298 * other APIs to get information out of it.
301 struct _lws_http_mode_related {
303 unsigned long filepos;
304 unsigned long filelen;
307 struct lws_fragments {
308 unsigned short offset;
310 unsigned char next_frag_index;
313 struct allocated_headers {
314 unsigned short next_frag_index;
316 unsigned char frag_index[WSI_TOKEN_COUNT];
317 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
318 char data[LWS_MAX_HEADER_LEN];
319 #ifndef LWS_NO_CLIENT
320 char initial_handshake_hash_base64[30];
321 unsigned short c_port;
325 struct _lws_header_related {
326 struct allocated_headers *ah;
328 unsigned char parser_state; /* enum lws_token_indexes */
331 struct _lws_websocket_related {
332 char *rx_user_buffer;
333 int rx_user_buffer_head;
334 unsigned char frame_masking_nonce_04[4];
335 unsigned char frame_mask_index;
336 size_t rx_packet_length;
337 unsigned char opcode;
338 unsigned int final:1;
340 unsigned int frame_is_binary:1;
341 unsigned int all_zero_nonce:1;
342 short close_reason; /* enum lws_close_status */
343 unsigned char *rxflow_buffer;
346 unsigned int rxflow_change_to:2;
347 unsigned int this_frame_masked:1;
350 struct libwebsocket {
352 /* lifetime members */
354 const struct libwebsocket_protocols *protocol;
355 #ifndef LWS_NO_EXTENSIONS
356 struct libwebsocket_extension *
357 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
358 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
359 unsigned char count_active_extensions;
360 unsigned int extension_data_pending:1;
362 unsigned char ietf_spec_revision;
364 char mode; /* enum connection_mode */
365 char state; /* enum lws_connection_states */
366 char lws_rx_parse_state; /* enum lws_rx_parse_state */
367 char rx_frame_type; /* enum libwebsocket_write_protocol */
369 unsigned int hdr_parsing_completed:1;
371 char pending_timeout; /* enum pending_timeout */
372 unsigned long pending_timeout_limit;
375 int position_in_fds_table;
377 unsigned long action_start;
378 unsigned long latency_start;
383 /* members with mutually exclusive lifetimes are unionized */
386 struct _lws_http_mode_related http;
387 struct _lws_header_related hdr;
388 struct _lws_websocket_related ws;
391 #ifdef LWS_OPENSSL_SUPPORT
394 unsigned int use_ssl:2;
399 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
400 struct libwebsocket *wsi, enum lws_close_status);
403 static inline void lws_latency(struct libwebsocket_context *context,
404 struct libwebsocket *wsi, const char *action,
405 int ret, int completion) { while (0); }
406 static inline void lws_latency_pre(struct libwebsocket_context *context,
407 struct libwebsocket *wsi) { while (0); }
409 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
411 lws_latency(struct libwebsocket_context *context,
412 struct libwebsocket *wsi, const char *action,
413 int ret, int completion);
417 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
420 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
423 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
424 unsigned char *buf, size_t len);
427 lws_b64_selftest(void);
429 LWS_EXTERN struct libwebsocket *
430 wsi_from_fd(struct libwebsocket_context *context, int fd);
433 insert_wsi_socket_into_fds(struct libwebsocket_context *context,
434 struct libwebsocket *wsi);
437 libwebsocket_set_timeout(struct libwebsocket *wsi,
438 enum pending_timeout reason, int secs);
441 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
445 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
446 struct libwebsocket *wsi, unsigned int sec);
448 LWS_EXTERN struct libwebsocket *
449 __libwebsocket_client_connect_2(struct libwebsocket_context *context,
450 struct libwebsocket *wsi);
452 LWS_EXTERN struct libwebsocket *
453 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
456 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
457 struct libwebsocket *wsi, char *pkt);
460 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
461 struct libwebsocket *wsi, struct pollfd *pollfd);
462 #ifndef LWS_NO_EXTENSIONS
464 lws_any_extension_handled(struct libwebsocket_context *context,
465 struct libwebsocket *wsi,
466 enum libwebsocket_extension_callback_reasons r,
467 void *v, size_t len);
470 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
471 struct libwebsocket_extension *ext);
475 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
476 struct libwebsocket *wsi);
479 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
482 lws_issue_raw_ext_access(struct libwebsocket *wsi,
483 unsigned char *buf, size_t len);
486 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
489 user_callback_handle_rxflow(callback_function,
490 struct libwebsocket_context *context,
491 struct libwebsocket *wsi,
492 enum libwebsocket_callback_reasons reason, void *user,
493 void *in, size_t len);
496 lws_set_socket_options(struct libwebsocket_context *context, int fd);
499 lws_allocate_header_table(struct libwebsocket *wsi);
502 lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
505 lws_hdr_simple_create(struct libwebsocket *wsi,
506 enum lws_token_indexes h, const char *s);
509 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
511 #ifndef LWS_NO_SERVER
512 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
513 struct libwebsocket *wsi);
516 #ifndef LWS_NO_DAEMONIZE
517 LWS_EXTERN int get_daemonize_pid();
520 extern int interface_to_sa(const char *ifname,
521 struct sockaddr_in *addr, size_t addrlen);
523 #ifndef LWS_OPENSSL_SUPPORT
526 SHA1(const unsigned char *d, size_t n, unsigned char *md);
530 LWS_EXTERN int openssl_websocket_private_data_index;