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,
32 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <sys/prctl.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
44 #ifdef LWS_OPENSSL_SUPPORT
45 #include <openssl/ssl.h>
46 #include <openssl/evp.h>
47 #include <openssl/err.h>
50 #include <openssl/md5.h>
51 #include <openssl/sha.h>
52 #include "libwebsockets.h"
58 static inline void debug(const char *format, ...) {
60 va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
63 static inline void debug(const char *format, ...) { }
66 #ifdef LWS_OPENSSL_SUPPORT
67 extern SSL_CTX *ssl_ctx;
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 512
78 #define MAX_BROADCAST_PAYLOAD 1024
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
101 enum lws_token_indexes {
104 WSI_TOKEN_CONNECTION,
118 /* client receives these */
123 /* always last real token index*/
125 /* parser state additions */
128 WSI_TOKEN_SKIPPING_SAW_CR,
132 enum lws_rx_parse_state {
136 LWS_RXPS_PULLING_76_LENGTH,
137 LWS_RXPS_EAT_UNTIL_76_FF,
139 LWS_RXPS_04_MASK_NONCE_1,
140 LWS_RXPS_04_MASK_NONCE_2,
141 LWS_RXPS_04_MASK_NONCE_3,
143 LWS_RXPS_04_FRAME_HDR_1,
144 LWS_RXPS_04_FRAME_HDR_LEN,
145 LWS_RXPS_04_FRAME_HDR_LEN16_2,
146 LWS_RXPS_04_FRAME_HDR_LEN16_1,
147 LWS_RXPS_04_FRAME_HDR_LEN64_8,
148 LWS_RXPS_04_FRAME_HDR_LEN64_7,
149 LWS_RXPS_04_FRAME_HDR_LEN64_6,
150 LWS_RXPS_04_FRAME_HDR_LEN64_5,
151 LWS_RXPS_04_FRAME_HDR_LEN64_4,
152 LWS_RXPS_04_FRAME_HDR_LEN64_3,
153 LWS_RXPS_04_FRAME_HDR_LEN64_2,
154 LWS_RXPS_04_FRAME_HDR_LEN64_1,
156 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
165 struct libwebsocket_protocols;
167 struct libwebsocket_context {
168 struct libwebsocket *wsi[MAX_CLIENTS + 1];
169 struct pollfd fds[MAX_CLIENTS + 1];
172 #ifdef LWS_OPENSSL_SUPPORT
175 struct libwebsocket_protocols *protocols;
181 * This is totally opaque to code using the library. It's exported as a
182 * forward-reference pointer-only declaration; the user can use the pointer with
183 * other APIs to get information out of it.
186 struct libwebsocket {
187 const struct libwebsocket_protocols *protocol;
189 enum lws_connection_states state;
191 char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
193 int current_alloc_len;
194 enum lws_token_indexes parser_state;
195 struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
196 int ietf_spec_revision;
197 char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
198 LWS_SEND_BUFFER_POST_PADDING];
199 int rx_user_buffer_head;
203 enum lws_rx_parse_state lws_rx_parse_state;
205 /* 04 protocol specific */
207 unsigned char masking_key_04[20];
208 unsigned char frame_masking_nonce_04[4];
209 unsigned char frame_mask_04[20];
210 unsigned char frame_mask_index;
211 size_t rx_packet_length;
212 unsigned char opcode;
218 char initial_handshake_hash_base64[30];
221 #ifdef LWS_OPENSSL_SUPPORT
229 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
232 libwebsocket_close_and_free_session(struct libwebsocket *wsi);
235 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
238 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
239 unsigned char *buf, size_t len);
242 libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len);
245 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
248 lws_b64_decode_string(const char *in, char *out, int out_size);
251 lws_b64_selftest(void);