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,
45 #include <sys/types.h>
46 #include <sys/socket.h>
48 #include <sys/prctl.h>
50 #include <netinet/in.h>
51 #include <netinet/tcp.h>
52 #include <arpa/inet.h>
60 #ifdef LWS_OPENSSL_SUPPORT
61 #include <openssl/ssl.h>
62 #include <openssl/evp.h>
63 #include <openssl/err.h>
64 #include <openssl/md5.h>
65 #include <openssl/sha.h>
69 #include "libwebsockets.h"
81 void debug(const char *format, ...)
84 va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
92 void debug(const char *format, ...)
99 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
100 * but happily have something equivalent in the SO_NOSIGPIPE flag.
103 #define MSG_NOSIGNAL SO_NOSIGPIPE
107 #define FD_HASHTABLE_MODULUS 32
108 #define MAX_CLIENTS 100
109 #define LWS_MAX_HEADER_NAME_LENGTH 64
110 #define LWS_MAX_HEADER_LEN 4096
111 #define LWS_INITIAL_HDR_ALLOC 256
112 #define LWS_ADDITIONAL_HDR_ALLOC 64
113 #define MAX_USER_RX_BUFFER 4096
114 #define MAX_BROADCAST_PAYLOAD 2048
115 #define LWS_MAX_PROTOCOLS 10
116 #define LWS_MAX_EXTENSIONS_ACTIVE 10
117 #define SPEC_LATEST_SUPPORTED 7
119 #define MAX_WEBSOCKET_04_KEY_LEN 128
120 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
122 enum lws_websocket_opcodes_04 {
123 LWS_WS_OPCODE_04__CONTINUATION = 0,
124 LWS_WS_OPCODE_04__CLOSE = 1,
125 LWS_WS_OPCODE_04__PING = 2,
126 LWS_WS_OPCODE_04__PONG = 3,
127 LWS_WS_OPCODE_04__TEXT_FRAME = 4,
128 LWS_WS_OPCODE_04__BINARY_FRAME = 5,
130 LWS_WS_OPCODE_04__RESERVED_6 = 6,
131 LWS_WS_OPCODE_04__RESERVED_7 = 7,
132 LWS_WS_OPCODE_04__RESERVED_8 = 8,
133 LWS_WS_OPCODE_04__RESERVED_9 = 9,
134 LWS_WS_OPCODE_04__RESERVED_A = 0xa,
135 LWS_WS_OPCODE_04__RESERVED_B = 0xb,
136 LWS_WS_OPCODE_04__RESERVED_C = 0xc,
137 LWS_WS_OPCODE_04__RESERVED_D = 0xd,
138 LWS_WS_OPCODE_04__RESERVED_E = 0xe,
139 LWS_WS_OPCODE_04__RESERVED_F = 0xf,
142 enum lws_websocket_opcodes_07 {
143 LWS_WS_OPCODE_07__CONTINUATION = 0,
144 LWS_WS_OPCODE_07__TEXT_FRAME = 1,
145 LWS_WS_OPCODE_07__BINARY_FRAME = 2,
147 LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
149 /* control extensions 8+ */
151 LWS_WS_OPCODE_07__CLOSE = 8,
152 LWS_WS_OPCODE_07__PING = 9,
153 LWS_WS_OPCODE_07__PONG = 0xa,
157 enum lws_connection_states {
159 WSI_STATE_HTTP_HEADERS,
160 WSI_STATE_DEAD_SOCKET,
161 WSI_STATE_ESTABLISHED,
162 WSI_STATE_CLIENT_UNCONNECTED,
163 WSI_STATE_RETURNED_CLOSE_ALREADY,
164 WSI_STATE_AWAITING_CLOSE_ACK,
167 enum lws_rx_parse_state {
171 LWS_RXPS_PULLING_76_LENGTH,
172 LWS_RXPS_EAT_UNTIL_76_FF,
174 LWS_RXPS_04_MASK_NONCE_1,
175 LWS_RXPS_04_MASK_NONCE_2,
176 LWS_RXPS_04_MASK_NONCE_3,
178 LWS_RXPS_04_FRAME_HDR_1,
179 LWS_RXPS_04_FRAME_HDR_LEN,
180 LWS_RXPS_04_FRAME_HDR_LEN16_2,
181 LWS_RXPS_04_FRAME_HDR_LEN16_1,
182 LWS_RXPS_04_FRAME_HDR_LEN64_8,
183 LWS_RXPS_04_FRAME_HDR_LEN64_7,
184 LWS_RXPS_04_FRAME_HDR_LEN64_6,
185 LWS_RXPS_04_FRAME_HDR_LEN64_5,
186 LWS_RXPS_04_FRAME_HDR_LEN64_4,
187 LWS_RXPS_04_FRAME_HDR_LEN64_3,
188 LWS_RXPS_04_FRAME_HDR_LEN64_2,
189 LWS_RXPS_04_FRAME_HDR_LEN64_1,
191 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
192 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
193 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
194 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
196 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
200 enum connection_mode {
201 LWS_CONNMODE_WS_SERVING,
202 LWS_CONNMODE_WS_CLIENT,
204 /* transient modes */
205 LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
206 LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
207 LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
208 LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
209 LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
211 /* special internal types */
212 LWS_CONNMODE_SERVER_LISTENER,
213 LWS_CONNMODE_BROADCAST_PROXY_LISTENER,
214 LWS_CONNMODE_BROADCAST_PROXY
218 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
220 struct libwebsocket_fd_hashtable {
221 struct libwebsocket *wsi[MAX_CLIENTS + 1];
225 struct libwebsocket_protocols;
227 struct libwebsocket_context {
228 struct libwebsocket_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
229 struct pollfd fds[MAX_CLIENTS * FD_HASHTABLE_MODULUS + 1];
232 char http_proxy_address[256];
233 char canonical_hostname[1024];
234 unsigned int http_proxy_port;
235 unsigned int options;
236 unsigned long last_timeout_check_s;
240 #ifdef LWS_OPENSSL_SUPPORT
243 SSL_CTX *ssl_client_ctx;
245 struct libwebsocket_protocols *protocols;
247 struct libwebsocket_extension *extensions;
251 enum pending_timeout {
252 NO_PENDING_TIMEOUT = 0,
253 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
254 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
255 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
256 PENDING_TIMEOUT_AWAITING_PING,
257 PENDING_TIMEOUT_CLOSE_ACK,
258 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
263 * This is totally opaque to code using the library. It's exported as a
264 * forward-reference pointer-only declaration; the user can use the pointer with
265 * other APIs to get information out of it.
268 struct libwebsocket {
269 const struct libwebsocket_protocols *protocol;
270 struct libwebsocket_extension *
271 active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
272 void * active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
273 int count_active_extensions;
275 enum lws_connection_states state;
277 char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
279 int current_alloc_len;
280 enum lws_token_indexes parser_state;
281 struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
282 int ietf_spec_revision;
283 char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
284 LWS_SEND_BUFFER_POST_PADDING];
285 int rx_user_buffer_head;
286 enum libwebsocket_write_protocol rx_frame_type;
287 int protocol_index_for_broadcast_proxy;
288 enum pending_timeout pending_timeout;
289 unsigned long pending_timeout_limit;
293 enum lws_rx_parse_state lws_rx_parse_state;
294 char extension_data_pending;
295 struct libwebsocket *candidate_children_list;
296 struct libwebsocket *extension_handles;
298 /* 04 protocol specific */
301 unsigned char masking_key_04[20];
302 unsigned char frame_masking_nonce_04[4];
303 unsigned char frame_mask_04[20];
304 unsigned char frame_mask_index;
305 size_t rx_packet_length;
306 unsigned char opcode;
310 unsigned char (*xor_mask)(struct libwebsocket *, unsigned char);
313 enum lws_close_status close_reason;
316 char this_frame_masked;
319 char initial_handshake_hash_base64[30];
320 enum connection_mode mode;
330 #ifdef LWS_OPENSSL_SUPPORT
340 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
343 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
346 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
347 unsigned char *buf, size_t len);
350 libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi,
351 unsigned char * buf, size_t len);
354 lws_b64_selftest(void);
357 xor_no_mask(struct libwebsocket *wsi, unsigned char c);
360 xor_mask_04(struct libwebsocket *wsi, unsigned char c);
363 xor_mask_05(struct libwebsocket *wsi, unsigned char c);
365 extern struct libwebsocket *
366 wsi_from_fd(struct libwebsocket_context *context, int fd);
369 insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi);
372 delete_from_fd(struct libwebsocket_context *context, int fd);
375 libwebsocket_set_timeout(struct libwebsocket *wsi,
376 enum pending_timeout reason, int secs);
379 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
383 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
384 struct libwebsocket *wsi, unsigned int sec);
386 extern struct libwebsocket * __libwebsocket_client_connect_2(
387 struct libwebsocket_context *context,
388 struct libwebsocket *wsi);
390 extern struct libwebsocket *
391 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
394 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
395 struct libwebsocket *wsi, char *pkt);
398 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
399 struct libwebsocket *wsi, struct pollfd *pollfd);
402 lws_any_extension_handled(struct libwebsocket_context *context,
403 struct libwebsocket *wsi,
404 enum libwebsocket_extension_callback_reasons r,
405 void *v, size_t len);
408 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
409 struct libwebsocket_extension * ext);
412 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
413 struct libwebsocket *wsi);
416 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
419 lws_issue_raw_ext_access(struct libwebsocket *wsi,
420 unsigned char *buf, size_t len);
422 #ifndef LWS_OPENSSL_SUPPORT
425 SHA1(const unsigned char *d, size_t n, unsigned char *md);
428 MD5(const unsigned char *input, int ilen, unsigned char *output);