a16425231ff9f8eeedbba43b9e9aca3f1f722456
[profile/ivi/libwebsockets.git] / lib / private-libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <netdb.h>
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/socket.h>
36 #ifndef LWS_NO_FORK
37 #include <sys/prctl.h>
38 #endif
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41
42 #include <poll.h>
43 #include <sys/mman.h>
44
45 #ifdef LWS_OPENSSL_SUPPORT
46 #include <openssl/ssl.h>
47 #include <openssl/evp.h>
48 #include <openssl/err.h>
49 #endif
50
51 #include <openssl/md5.h>
52 #include <openssl/sha.h>
53 #include "libwebsockets.h"
54
55 #if 0
56 #define DEBUG
57 #endif
58
59 #ifdef DEBUG
60 static inline void debug(const char *format, ...)
61 {
62         va_list ap;
63         va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
64 }
65 #else
66 static inline void debug(const char *format, ...)
67 {
68 }
69 #endif
70
71
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
80
81 #define MAX_WEBSOCKET_04_KEY_LEN 128
82 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
83
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,
91 };
92
93 enum lws_connection_states {
94         WSI_STATE_HTTP,
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
100 };
101
102 enum lws_token_indexes {
103         WSI_TOKEN_GET_URI,
104         WSI_TOKEN_HOST,
105         WSI_TOKEN_CONNECTION,
106         WSI_TOKEN_KEY1,
107         WSI_TOKEN_KEY2,
108         WSI_TOKEN_PROTOCOL,
109         WSI_TOKEN_UPGRADE,
110         WSI_TOKEN_ORIGIN,
111         WSI_TOKEN_DRAFT,
112         WSI_TOKEN_CHALLENGE,
113
114         /* new for 04 */
115         WSI_TOKEN_KEY,
116         WSI_TOKEN_VERSION,
117         WSI_TOKEN_SWORIGIN,
118
119         /* new for 05 */
120         WSI_TOKEN_EXTENSIONS,
121
122         /* client receives these */
123         WSI_TOKEN_ACCEPT,
124         WSI_TOKEN_NONCE,
125         WSI_TOKEN_HTTP,
126
127         /* always last real token index*/
128         WSI_TOKEN_COUNT,
129         /* parser state additions */
130         WSI_TOKEN_NAME_PART,
131         WSI_TOKEN_SKIPPING,
132         WSI_TOKEN_SKIPPING_SAW_CR,
133         WSI_PARSING_COMPLETE
134 };
135
136 enum lws_rx_parse_state {
137         LWS_RXPS_NEW,
138
139         LWS_RXPS_SEEN_76_FF,
140         LWS_RXPS_PULLING_76_LENGTH,
141         LWS_RXPS_EAT_UNTIL_76_FF,
142
143         LWS_RXPS_04_MASK_NONCE_1,
144         LWS_RXPS_04_MASK_NONCE_2,
145         LWS_RXPS_04_MASK_NONCE_3,
146
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,
159
160         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
161 };
162
163
164 struct lws_tokens {
165         char *token;
166         int token_len;
167 };
168
169 struct libwebsocket_protocols;
170
171 struct libwebsocket_context {
172         struct libwebsocket *wsi[MAX_CLIENTS + 1];
173         struct pollfd fds[MAX_CLIENTS + 1];
174         int fds_count;
175         int listen_port;
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
181         int use_ssl;
182         SSL_CTX *ssl_ctx;
183         SSL_CTX *ssl_client_ctx;
184 #endif
185         struct libwebsocket_protocols *protocols;
186         int count_protocols;
187 };
188
189 enum connection_mode {
190         LWS_CONNMODE_WS_SERVING,
191         LWS_CONNMODE_WS_CLIENT,
192 };
193
194
195 /*
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.
199  */
200
201 struct libwebsocket {
202         const struct libwebsocket_protocols *protocol;
203
204         enum lws_connection_states state;
205
206         char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
207         int name_buffer_pos;
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;
215
216         int sock;
217
218         enum lws_rx_parse_state lws_rx_parse_state;
219
220         /* 04 protocol specific */
221
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;
228         unsigned char final;
229
230         int pings_vs_pongs;
231         unsigned char (*xor_mask)(struct libwebsocket *, unsigned char);
232         char all_zero_nonce;
233
234         /* client support */
235         char initial_handshake_hash_base64[30];
236         enum connection_mode mode;
237
238 #ifdef LWS_OPENSSL_SUPPORT
239         SSL *ssl;
240         BIO *client_bio;
241 #endif
242
243         void *user_space;
244 };
245
246 extern int
247 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
248
249 extern void
250 libwebsocket_close_and_free_session(struct libwebsocket *wsi);
251
252 extern int
253 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
254
255 extern int
256 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
257                                                 unsigned char *buf, size_t len);
258
259 extern int
260 libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len);
261
262 extern int
263 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
264
265 extern int
266 lws_b64_decode_string(const char *in, char *out, int out_size);
267
268 extern int
269 lws_b64_selftest(void);
270
271 extern unsigned char
272 xor_no_mask(struct libwebsocket *wsi, unsigned char c);
273
274 extern unsigned char
275 xor_mask_04(struct libwebsocket *wsi, unsigned char c);
276
277 extern unsigned char
278 xor_mask_05(struct libwebsocket *wsi, unsigned char c);