2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 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,
43 #define compatible_close(fd) closesocket(fd);
55 #include <sys/types.h>
56 #include <sys/socket.h>
58 #ifdef HAVE_SYS_PRCTL_H
59 #include <sys/prctl.h>
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
64 #include <arpa/inet.h>
70 #define compatible_close(fd) close(fd);
73 #ifdef LWS_OPENSSL_SUPPORT
74 #include <openssl/ssl.h>
75 #include <openssl/evp.h>
76 #include <openssl/err.h>
77 #include <openssl/md5.h>
78 #include <openssl/sha.h>
81 /* some Linux toolchains don't have this from the above, some do */
83 #include <linux/limits.h>
86 #include "libwebsockets.h"
88 extern void _lws_log(int filter, const char *format, ...);
90 /* warn and log are always compiled in */
91 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
92 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
95 * weaker logging can be deselected at configure time using disable_debug
96 * that gets rid of the overhead of checking while keeping _warn and _err
100 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
101 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
102 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
103 #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
104 #define lwsl_ext(...) _lws_log(LLL_HEADER, __VA_ARGS__)
105 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
107 #define lwsl_info(...)
108 #define lwsl_debug(...)
109 #define lwsl_parser(...)
110 #define lwsl_header(...)
111 #define lwsl_ext(...)
112 #define lwsl_client(...)
116 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
117 * but happily have something equivalent in the SO_NOSIGPIPE flag.
120 #define MSG_NOSIGNAL SO_NOSIGPIPE
124 #ifndef FD_HASHTABLE_MODULUS
125 #define FD_HASHTABLE_MODULUS 32
128 #define MAX_CLIENTS 100
130 #ifndef LWS_MAX_HEADER_NAME_LENGTH
131 #define LWS_MAX_HEADER_NAME_LENGTH 64
133 #ifndef LWS_MAX_HEADER_LEN
134 #define LWS_MAX_HEADER_LEN 4096
136 #ifndef LWS_INITIAL_HDR_ALLOC
137 #define LWS_INITIAL_HDR_ALLOC 256
139 #ifndef LWS_ADDITIONAL_HDR_ALLOC
140 #define LWS_ADDITIONAL_HDR_ALLOC 64
142 #ifndef MAX_USER_RX_BUFFER
143 #define MAX_USER_RX_BUFFER 4096
145 #ifndef MAX_BROADCAST_PAYLOAD
146 #define MAX_BROADCAST_PAYLOAD 4096
148 #ifndef LWS_MAX_PROTOCOLS
149 #define LWS_MAX_PROTOCOLS 10
151 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
152 #define LWS_MAX_EXTENSIONS_ACTIVE 10
154 #ifndef SPEC_LATEST_SUPPORTED
155 #define SPEC_LATEST_SUPPORTED 13
157 #ifndef AWAITING_TIMEOUT
158 #define AWAITING_TIMEOUT 5
160 #ifndef CIPHERS_LIST_STRING
161 #define CIPHERS_LIST_STRING "DEFAULT"
163 #ifndef LWS_SOMAXCONN
164 #define LWS_SOMAXCONN SOMAXCONN
167 #define MAX_WEBSOCKET_04_KEY_LEN 128
169 #ifndef SYSTEM_RANDOM_FILEPATH
170 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
172 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
173 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
177 enum lws_websocket_opcodes_04 {
178 LWS_WS_OPCODE_04__CONTINUATION = 0,
179 LWS_WS_OPCODE_04__CLOSE = 1,
180 LWS_WS_OPCODE_04__PING = 2,
181 LWS_WS_OPCODE_04__PONG = 3,
182 LWS_WS_OPCODE_04__TEXT_FRAME = 4,
183 LWS_WS_OPCODE_04__BINARY_FRAME = 5,
185 LWS_WS_OPCODE_04__RESERVED_6 = 6,
186 LWS_WS_OPCODE_04__RESERVED_7 = 7,
187 LWS_WS_OPCODE_04__RESERVED_8 = 8,
188 LWS_WS_OPCODE_04__RESERVED_9 = 9,
189 LWS_WS_OPCODE_04__RESERVED_A = 0xa,
190 LWS_WS_OPCODE_04__RESERVED_B = 0xb,
191 LWS_WS_OPCODE_04__RESERVED_C = 0xc,
192 LWS_WS_OPCODE_04__RESERVED_D = 0xd,
193 LWS_WS_OPCODE_04__RESERVED_E = 0xe,
194 LWS_WS_OPCODE_04__RESERVED_F = 0xf,
197 enum lws_websocket_opcodes_07 {
198 LWS_WS_OPCODE_07__CONTINUATION = 0,
199 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
200 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
202 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
204 /* control extensions 8+ */
206 LWS_WS_OPCODE_07__CLOSE = 8,
207 LWS_WS_OPCODE_07__PING = 9,
208 LWS_WS_OPCODE_07__PONG = 0xa,
212 enum lws_connection_states {
214 WSI_STATE_HTTP_ISSUING_FILE,
215 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 {
227 LWS_RXPS_PULLING_76_LENGTH,
228 LWS_RXPS_EAT_UNTIL_76_FF,
230 LWS_RXPS_04_MASK_NONCE_1,
231 LWS_RXPS_04_MASK_NONCE_2,
232 LWS_RXPS_04_MASK_NONCE_3,
234 LWS_RXPS_04_FRAME_HDR_1,
235 LWS_RXPS_04_FRAME_HDR_LEN,
236 LWS_RXPS_04_FRAME_HDR_LEN16_2,
237 LWS_RXPS_04_FRAME_HDR_LEN16_1,
238 LWS_RXPS_04_FRAME_HDR_LEN64_8,
239 LWS_RXPS_04_FRAME_HDR_LEN64_7,
240 LWS_RXPS_04_FRAME_HDR_LEN64_6,
241 LWS_RXPS_04_FRAME_HDR_LEN64_5,
242 LWS_RXPS_04_FRAME_HDR_LEN64_4,
243 LWS_RXPS_04_FRAME_HDR_LEN64_3,
244 LWS_RXPS_04_FRAME_HDR_LEN64_2,
245 LWS_RXPS_04_FRAME_HDR_LEN64_1,
247 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
248 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
249 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
250 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
252 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
256 enum connection_mode {
257 LWS_CONNMODE_HTTP_SERVING,
259 LWS_CONNMODE_WS_SERVING,
260 LWS_CONNMODE_WS_CLIENT,
262 /* transient modes */
263 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
264 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
265 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
266 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
267 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
269 /* special internal types */
270 LWS_CONNMODE_SERVER_LISTENER,
271 LWS_CONNMODE_BROADCAST_PROXY_LISTENER,
272 LWS_CONNMODE_BROADCAST_PROXY
276 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
278 struct libwebsocket_fd_hashtable {
279 struct libwebsocket *wsi[MAX_CLIENTS + 1];
283 struct libwebsocket_protocols;
285 struct libwebsocket_context {
286 struct libwebsocket_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
287 struct pollfd fds[MAX_CLIENTS * FD_HASHTABLE_MODULUS + 1];
290 char http_proxy_address[256];
291 char canonical_hostname[1024];
292 unsigned int http_proxy_port;
293 unsigned int options;
294 unsigned long last_timeout_check_s;
298 #ifdef LWS_OPENSSL_SUPPORT
301 SSL_CTX *ssl_client_ctx;
303 struct libwebsocket_protocols *protocols;
305 struct libwebsocket_extension *extensions;
311 enum pending_timeout {
312 NO_PENDING_TIMEOUT = 0,
313 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
314 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
315 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
316 PENDING_TIMEOUT_AWAITING_PING,
317 PENDING_TIMEOUT_CLOSE_ACK,
318 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
319 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
324 * This is totally opaque to code using the library. It's exported as a
325 * forward-reference pointer-only declaration; the user can use the pointer with
326 * other APIs to get information out of it.
329 struct libwebsocket {
330 const struct libwebsocket_protocols *protocol;
331 struct libwebsocket_extension *
332 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
333 void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
334 int count_active_extensions;
336 enum lws_connection_states state;
338 char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
340 int current_alloc_len;
341 enum lws_token_indexes parser_state;
342 struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
343 int ietf_spec_revision;
344 char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
345 LWS_SEND_BUFFER_POST_PADDING];
346 int rx_user_buffer_head;
347 enum libwebsocket_write_protocol rx_frame_type;
348 int protocol_index_for_broadcast_proxy;
349 enum pending_timeout pending_timeout;
350 unsigned long pending_timeout_limit;
354 enum lws_rx_parse_state lws_rx_parse_state;
355 char extension_data_pending;
356 struct libwebsocket *candidate_children_list;
357 struct libwebsocket *extension_handles;
359 /* 04 protocol specific */
362 unsigned char masking_key_04[20];
363 unsigned char frame_masking_nonce_04[4];
364 unsigned char frame_mask_04[20];
365 unsigned char frame_mask_index;
366 size_t rx_packet_length;
367 unsigned char opcode;
372 unsigned char (*xor_mask)(struct libwebsocket *, unsigned char);
375 enum lws_close_status close_reason;
378 char this_frame_masked;
381 char initial_handshake_hash_base64[30];
382 enum connection_mode mode;
387 callback_function *c_callback;
393 #ifdef LWS_OPENSSL_SUPPORT
400 char filepath[PATH_MAX];
401 unsigned long filepos;
402 unsigned long filelen;
408 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
411 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
414 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
415 unsigned char *buf, size_t len);
418 libwebsocket_read(struct libwebsocket_context *context,
419 struct libwebsocket *wsi,
420 unsigned char *buf, size_t len);
423 lws_b64_selftest(void);
426 xor_no_mask(struct libwebsocket *wsi, unsigned char c);
429 xor_mask_04(struct libwebsocket *wsi, unsigned char c);
432 xor_mask_05(struct libwebsocket *wsi, unsigned char c);
434 extern struct libwebsocket *
435 wsi_from_fd(struct libwebsocket_context *context, int fd);
438 insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi);
441 delete_from_fd(struct libwebsocket_context *context, int fd);
444 libwebsocket_set_timeout(struct libwebsocket *wsi,
445 enum pending_timeout reason, int secs);
448 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
452 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
453 struct libwebsocket *wsi, unsigned int sec);
455 extern struct libwebsocket *
456 __libwebsocket_client_connect_2(struct libwebsocket_context *context,
457 struct libwebsocket *wsi);
459 extern struct libwebsocket *
460 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
463 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
464 struct libwebsocket *wsi, char *pkt);
467 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
468 struct libwebsocket *wsi, struct pollfd *pollfd);
471 lws_any_extension_handled(struct libwebsocket_context *context,
472 struct libwebsocket *wsi,
473 enum libwebsocket_extension_callback_reasons r,
474 void *v, size_t len);
477 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
478 struct libwebsocket_extension *ext);
481 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
482 struct libwebsocket *wsi);
485 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
488 lws_issue_raw_ext_access(struct libwebsocket *wsi,
489 unsigned char *buf, size_t len);
491 #ifndef LWS_OPENSSL_SUPPORT
494 SHA1(const unsigned char *d, size_t n, unsigned char *md);
497 MD5(const unsigned char *input, int ilen, unsigned char *output);