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,
33 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <sys/prctl.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #ifdef LWS_OPENSSL_SUPPORT
46 #include <openssl/ssl.h>
47 #include <openssl/evp.h>
48 #include <openssl/err.h>
51 #include <openssl/md5.h>
52 #include <openssl/sha.h>
53 #include "libwebsockets.h"
60 static inline void debug(const char *format, ...)
63 va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
66 static inline void debug(const char *format, ...)
72 #define MAX_CLIENTS 100
73 #define LWS_MAX_HEADER_NAME_LENGTH 64
74 #define LWS_MAX_HEADER_LEN 4096
75 #define LWS_INITIAL_HDR_ALLOC 256
76 #define LWS_ADDITIONAL_HDR_ALLOC 64
77 #define MAX_USER_RX_BUFFER 4096
78 #define MAX_BROADCAST_PAYLOAD 2048
79 #define LWS_MAX_PROTOCOLS 10
81 #define MAX_WEBSOCKET_04_KEY_LEN 128
82 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
84 enum lws_websocket_opcodes_04 {
85 LWS_WS_OPCODE_04__CONTINUATION = 0,
86 LWS_WS_OPCODE_04__CLOSE = 1,
87 LWS_WS_OPCODE_04__PING = 2,
88 LWS_WS_OPCODE_04__PONG = 3,
89 LWS_WS_OPCODE_04__TEXT_FRAME = 4,
90 LWS_WS_OPCODE_04__BINARY_FRAME = 5,
93 enum lws_connection_states {
95 WSI_STATE_HTTP_HEADERS,
96 WSI_STATE_DEAD_SOCKET,
97 WSI_STATE_ESTABLISHED,
98 WSI_STATE_CLIENT_UNCONNECTED,
99 WSI_STATE_RETURNED_CLOSE_ALREADY
102 enum lws_token_indexes {
105 WSI_TOKEN_CONNECTION,
120 WSI_TOKEN_EXTENSIONS,
122 /* client receives these */
127 /* always last real token index*/
129 /* parser state additions */
132 WSI_TOKEN_SKIPPING_SAW_CR,
136 enum lws_rx_parse_state {
140 LWS_RXPS_PULLING_76_LENGTH,
141 LWS_RXPS_EAT_UNTIL_76_FF,
143 LWS_RXPS_04_MASK_NONCE_1,
144 LWS_RXPS_04_MASK_NONCE_2,
145 LWS_RXPS_04_MASK_NONCE_3,
147 LWS_RXPS_04_FRAME_HDR_1,
148 LWS_RXPS_04_FRAME_HDR_LEN,
149 LWS_RXPS_04_FRAME_HDR_LEN16_2,
150 LWS_RXPS_04_FRAME_HDR_LEN16_1,
151 LWS_RXPS_04_FRAME_HDR_LEN64_8,
152 LWS_RXPS_04_FRAME_HDR_LEN64_7,
153 LWS_RXPS_04_FRAME_HDR_LEN64_6,
154 LWS_RXPS_04_FRAME_HDR_LEN64_5,
155 LWS_RXPS_04_FRAME_HDR_LEN64_4,
156 LWS_RXPS_04_FRAME_HDR_LEN64_3,
157 LWS_RXPS_04_FRAME_HDR_LEN64_2,
158 LWS_RXPS_04_FRAME_HDR_LEN64_1,
160 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
169 struct libwebsocket_protocols;
171 struct libwebsocket_context {
172 struct libwebsocket *wsi[MAX_CLIENTS + 1];
173 struct pollfd fds[MAX_CLIENTS + 1];
176 char http_proxy_address[256];
177 char canonical_hostname[1024];
178 unsigned int http_proxy_port;
179 unsigned int options;
180 #ifdef LWS_OPENSSL_SUPPORT
183 SSL_CTX *ssl_client_ctx;
185 struct libwebsocket_protocols *protocols;
189 enum connection_mode {
190 LWS_CONNMODE_WS_SERVING,
191 LWS_CONNMODE_WS_CLIENT,
196 * This is totally opaque to code using the library. It's exported as a
197 * forward-reference pointer-only declaration; the user can use the pointer with
198 * other APIs to get information out of it.
201 struct libwebsocket {
202 const struct libwebsocket_protocols *protocol;
204 enum lws_connection_states state;
206 char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
208 int current_alloc_len;
209 enum lws_token_indexes parser_state;
210 struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
211 int ietf_spec_revision;
212 char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
213 LWS_SEND_BUFFER_POST_PADDING];
214 int rx_user_buffer_head;
218 enum lws_rx_parse_state lws_rx_parse_state;
220 /* 04 protocol specific */
222 unsigned char masking_key_04[20];
223 unsigned char frame_masking_nonce_04[4];
224 unsigned char frame_mask_04[20];
225 unsigned char frame_mask_index;
226 size_t rx_packet_length;
227 unsigned char opcode;
231 unsigned char (*xor_mask)(struct libwebsocket *, unsigned char);
235 char initial_handshake_hash_base64[30];
236 enum connection_mode mode;
238 #ifdef LWS_OPENSSL_SUPPORT
247 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
250 libwebsocket_close_and_free_session(struct libwebsocket *wsi);
253 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
256 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
257 unsigned char *buf, size_t len);
260 libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len);
263 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
266 lws_b64_decode_string(const char *in, char *out, int out_size);
269 lws_b64_selftest(void);
272 xor_no_mask(struct libwebsocket *wsi, unsigned char c);
275 xor_mask_04(struct libwebsocket *wsi, unsigned char c);
278 xor_mask_05(struct libwebsocket *wsi, unsigned char c);