introduce-client-support.patch
[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
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/socket.h>
35 #ifndef LWS_NO_FORK
36 #include <sys/prctl.h>
37 #endif
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40
41 #include <poll.h>
42 #include <sys/mman.h>
43
44 #ifdef LWS_OPENSSL_SUPPORT
45 #include <openssl/ssl.h>
46 #include <openssl/evp.h>
47 #include <openssl/err.h>
48 #endif
49
50 #include <openssl/md5.h>
51 #include <openssl/sha.h>
52 #include "libwebsockets.h"
53
54 //#define DEBUG 
55
56
57 #ifdef DEBUG
58 static inline void debug(const char *format, ...) {
59         va_list ap;
60         va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
61 }
62 #else
63 static inline void debug(const char *format, ...) { }
64 #endif
65
66 #ifdef LWS_OPENSSL_SUPPORT
67 extern SSL_CTX *ssl_ctx;
68 extern int use_ssl;
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 512
78 #define MAX_BROADCAST_PAYLOAD 1024
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 };
100
101 enum lws_token_indexes {
102         WSI_TOKEN_GET_URI,
103         WSI_TOKEN_HOST,
104         WSI_TOKEN_CONNECTION,
105         WSI_TOKEN_KEY1,
106         WSI_TOKEN_KEY2,
107         WSI_TOKEN_PROTOCOL,
108         WSI_TOKEN_UPGRADE,
109         WSI_TOKEN_ORIGIN,
110         WSI_TOKEN_DRAFT,
111         WSI_TOKEN_CHALLENGE,
112
113         /* new for 04 */
114         WSI_TOKEN_KEY,
115         WSI_TOKEN_VERSION,
116         WSI_TOKEN_SWORIGIN,
117
118         /* client receives these */
119         WSI_TOKEN_ACCEPT,
120         WSI_TOKEN_NONCE,
121         WSI_TOKEN_HTTP,
122
123         /* always last real token index*/
124         WSI_TOKEN_COUNT,
125         /* parser state additions */
126         WSI_TOKEN_NAME_PART,
127         WSI_TOKEN_SKIPPING,
128         WSI_TOKEN_SKIPPING_SAW_CR,
129         WSI_PARSING_COMPLETE
130 };
131
132 enum lws_rx_parse_state {
133         LWS_RXPS_NEW,
134
135         LWS_RXPS_SEEN_76_FF,
136         LWS_RXPS_PULLING_76_LENGTH,
137         LWS_RXPS_EAT_UNTIL_76_FF,
138
139         LWS_RXPS_04_MASK_NONCE_1,
140         LWS_RXPS_04_MASK_NONCE_2,
141         LWS_RXPS_04_MASK_NONCE_3,
142
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,
155
156         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
157 };
158
159
160 struct lws_tokens {
161         char *token;
162         int token_len;
163 };
164
165 struct libwebsocket_protocols;
166
167 struct libwebsocket_context {
168         struct libwebsocket *wsi[MAX_CLIENTS + 1];
169         struct pollfd fds[MAX_CLIENTS + 1];
170         int fds_count;
171         int listen_port;
172 #ifdef LWS_OPENSSL_SUPPORT
173         int use_ssl;
174 #endif
175         struct libwebsocket_protocols *protocols;
176         int count_protocols;
177 };
178
179
180 /*
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.
184  */
185
186 struct libwebsocket {
187         const struct libwebsocket_protocols *protocol;
188
189         enum lws_connection_states state;
190
191         char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
192         int name_buffer_pos;
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;
200
201         int sock;
202
203         enum lws_rx_parse_state lws_rx_parse_state;
204
205         /* 04 protocol specific */
206
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;
213         unsigned char final;
214
215         int pings_vs_pongs;
216
217         /* client support */
218         char initial_handshake_hash_base64[30];
219         int client_mode;
220
221 #ifdef LWS_OPENSSL_SUPPORT
222         SSL *ssl;
223 #endif
224
225         void *user_space;
226 };
227
228 extern int
229 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
230
231 extern void
232 libwebsocket_close_and_free_session(struct libwebsocket *wsi);
233
234 extern int
235 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
236
237 extern int
238 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
239                                                 unsigned char *buf, size_t len);
240
241 extern int
242 libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len);
243
244 extern int
245 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
246
247 extern int
248 lws_b64_decode_string(const char *in, char *out, int out_size);
249
250 extern int
251 lws_b64_selftest(void);