a9368bca4bb45fa8c4a9dcaf4044ef07c6c7bb9f
[profile/ivi/libwebsockets.git] / lib / private-libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2013 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 // System introspection configs.
23 #ifdef CMAKE_BUILD
24 #include "lws_config.h"
25 #else
26 #ifdef WIN32
27 #define inline __inline
28 #else
29 #include "config.h"
30 #endif
31 #endif
32
33 #if _MSC_VER > 1000 || defined(_WIN32)
34 #else
35 #include <unistd.h>
36 #include <strings.h>
37 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <signal.h>
45 #include <limits.h>
46 #ifdef  __MINGW64__
47 #else
48 #ifdef  __MINGW32__
49 #elif _MSC_VER > 1000 || defined(_WIN32)
50 #else
51 #include <netdb.h>
52 #endif
53 #endif
54 #include <stdarg.h>
55
56 #include <sys/stat.h>
57
58 #ifdef WIN32
59 #define LWS_NO_DAEMONIZE
60
61 #define compatible_close(fd) closesocket(fd);
62 #ifdef  __MINGW64__                                                             
63 #else                                                                           
64 #ifdef  __MINGW32__                                                             
65 #else
66 #include <time.h >
67 #endif
68 #endif
69 #include <winsock2.h>
70 #include <ws2ipdef.h>
71 #include <windows.h>
72 #else
73 #include <sys/types.h>
74 #include <sys/socket.h>
75 #ifndef LWS_NO_FORK
76 #ifdef HAVE_SYS_PRCTL_H
77 #include <sys/prctl.h>
78 #endif
79 #endif
80 #include <netinet/in.h>
81 #include <netinet/tcp.h>
82 #include <arpa/inet.h>
83
84 #include <poll.h>
85 #include <sys/mman.h>
86 #include <sys/time.h>
87
88 #define compatible_close(fd) close(fd);
89 #endif
90
91 #ifdef LWS_OPENSSL_SUPPORT
92 #ifdef USE_CYASSL
93 #include <cyassl/openssl/ssl.h>
94 #include <cyassl/error.h>
95 unsigned char *
96 SHA1(const unsigned char *d, size_t n, unsigned char *md);
97 #else
98 #include <openssl/ssl.h>
99 #include <openssl/evp.h>
100 #include <openssl/err.h>
101 #include <openssl/md5.h>
102 #include <openssl/sha.h>
103 #endif /* not USE_CYASSL */
104 #endif
105
106 #include "libwebsockets.h"
107
108 /*
109  * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
110  * but happily have something equivalent in the SO_NOSIGPIPE flag.
111  */
112 #ifdef __APPLE__
113 #define MSG_NOSIGNAL SO_NOSIGPIPE
114 #endif
115
116 #ifndef LWS_MAX_HEADER_NAME_LENGTH
117 #define LWS_MAX_HEADER_NAME_LENGTH 64
118 #endif
119 #ifndef LWS_MAX_HEADER_LEN
120 #define LWS_MAX_HEADER_LEN 4096
121 #endif
122 #ifndef LWS_INITIAL_HDR_ALLOC
123 #define LWS_INITIAL_HDR_ALLOC 256
124 #endif
125 #ifndef LWS_ADDITIONAL_HDR_ALLOC
126 #define LWS_ADDITIONAL_HDR_ALLOC 64
127 #endif
128 #ifndef LWS_MAX_PROTOCOLS
129 #define LWS_MAX_PROTOCOLS 5
130 #endif
131 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
132 #define LWS_MAX_EXTENSIONS_ACTIVE 3
133 #endif
134 #ifndef SPEC_LATEST_SUPPORTED
135 #define SPEC_LATEST_SUPPORTED 13
136 #endif
137 #ifndef AWAITING_TIMEOUT
138 #define AWAITING_TIMEOUT 5
139 #endif
140 #ifndef CIPHERS_LIST_STRING
141 #define CIPHERS_LIST_STRING "DEFAULT"
142 #endif
143 #ifndef LWS_SOMAXCONN
144 #define LWS_SOMAXCONN SOMAXCONN
145 #endif
146
147 #define MAX_WEBSOCKET_04_KEY_LEN 128
148 #define LWS_MAX_SOCKET_IO_BUF 4096
149
150 #ifndef SYSTEM_RANDOM_FILEPATH
151 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
152 #endif
153 #ifndef LWS_MAX_ZLIB_CONN_BUFFER
154 #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
155 #endif
156
157 /*
158  * if not in a connection storm, check for incoming
159  * connections this many normal connection services
160  */
161 #define LWS_LISTEN_SERVICE_MODULO 10
162
163 enum lws_websocket_opcodes_07 {
164         LWS_WS_OPCODE_07__CONTINUATION = 0,
165         LWS_WS_OPCODE_07__TEXT_FRAME = 1,
166         LWS_WS_OPCODE_07__BINARY_FRAME = 2,
167
168         LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
169
170         /* control extensions 8+ */
171
172         LWS_WS_OPCODE_07__CLOSE = 8,
173         LWS_WS_OPCODE_07__PING = 9,
174         LWS_WS_OPCODE_07__PONG = 0xa,
175 };
176
177
178 enum lws_connection_states {
179         WSI_STATE_HTTP,
180         WSI_STATE_HTTP_ISSUING_FILE,
181         WSI_STATE_HTTP_HEADERS,
182         WSI_STATE_DEAD_SOCKET,
183         WSI_STATE_ESTABLISHED,
184         WSI_STATE_CLIENT_UNCONNECTED,
185         WSI_STATE_RETURNED_CLOSE_ALREADY,
186         WSI_STATE_AWAITING_CLOSE_ACK,
187 };
188
189 enum lws_rx_parse_state {
190         LWS_RXPS_NEW,
191
192         LWS_RXPS_04_MASK_NONCE_1,
193         LWS_RXPS_04_MASK_NONCE_2,
194         LWS_RXPS_04_MASK_NONCE_3,
195
196         LWS_RXPS_04_FRAME_HDR_1,
197         LWS_RXPS_04_FRAME_HDR_LEN,
198         LWS_RXPS_04_FRAME_HDR_LEN16_2,
199         LWS_RXPS_04_FRAME_HDR_LEN16_1,
200         LWS_RXPS_04_FRAME_HDR_LEN64_8,
201         LWS_RXPS_04_FRAME_HDR_LEN64_7,
202         LWS_RXPS_04_FRAME_HDR_LEN64_6,
203         LWS_RXPS_04_FRAME_HDR_LEN64_5,
204         LWS_RXPS_04_FRAME_HDR_LEN64_4,
205         LWS_RXPS_04_FRAME_HDR_LEN64_3,
206         LWS_RXPS_04_FRAME_HDR_LEN64_2,
207         LWS_RXPS_04_FRAME_HDR_LEN64_1,
208
209         LWS_RXPS_07_COLLECT_FRAME_KEY_1,
210         LWS_RXPS_07_COLLECT_FRAME_KEY_2,
211         LWS_RXPS_07_COLLECT_FRAME_KEY_3,
212         LWS_RXPS_07_COLLECT_FRAME_KEY_4,
213
214         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
215 };
216
217
218 enum connection_mode {
219         LWS_CONNMODE_HTTP_SERVING,
220
221         LWS_CONNMODE_WS_SERVING,
222         LWS_CONNMODE_WS_CLIENT,
223
224         /* transient, ssl delay hiding */
225         LWS_CONNMODE_SSL_ACK_PENDING,
226
227         /* transient modes */
228         LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
229         LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
230         LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
231         LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
232         LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
233
234         /* special internal types */
235         LWS_CONNMODE_SERVER_LISTENER,
236 };
237
238 struct libwebsocket_protocols;
239 struct libwebsocket;
240
241 struct libwebsocket_context {
242         struct pollfd *fds;
243         struct libwebsocket **lws_lookup; /* fd to wsi */
244         int fds_count;
245         int max_fds;
246         int listen_port;
247         char http_proxy_address[256];
248         char canonical_hostname[1024];
249         unsigned int http_proxy_port;
250         unsigned int options;
251         unsigned long last_timeout_check_s;
252
253         /*
254          * usable by anything in the service code, but only if the scope
255          * does not last longer than the service action (since next service
256          * of any socket can likewise use it and overwrite)
257          */
258         unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
259
260         int started_with_parent;
261
262         int fd_random;
263         int listen_service_modulo;
264         int listen_service_count;
265         int listen_service_fd;
266         int listen_service_extraseen;
267
268         int ka_time;
269         int ka_probes;
270         int ka_interval;
271
272 #ifdef LWS_LATENCY
273         unsigned long worst_latency;
274         char worst_latency_info[256];
275 #endif
276
277 #ifdef LWS_OPENSSL_SUPPORT
278         int use_ssl;
279         SSL_CTX *ssl_ctx;
280         SSL_CTX *ssl_client_ctx;
281 #endif
282         struct libwebsocket_protocols *protocols;
283         int count_protocols;
284 #ifndef LWS_NO_EXTENSIONS
285         struct libwebsocket_extension *extensions;
286 #endif
287     void *user_space;
288 };
289
290
291 enum pending_timeout {
292         NO_PENDING_TIMEOUT = 0,
293         PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
294         PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
295         PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
296         PENDING_TIMEOUT_AWAITING_PING,
297         PENDING_TIMEOUT_CLOSE_ACK,
298         PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
299         PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
300         PENDING_TIMEOUT_SSL_ACCEPT,
301 };
302
303
304 /*
305  * This is totally opaque to code using the library.  It's exported as a
306  * forward-reference pointer-only declaration; the user can use the pointer with
307  * other APIs to get information out of it.
308  */
309
310 struct _lws_http_mode_related {
311         int fd;
312         unsigned long filepos;
313         unsigned long filelen;
314 };
315
316 struct _lws_header_related {
317         char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
318         unsigned char name_buffer_pos;
319         struct lws_tokens hdrs[WSI_TOKEN_COUNT];
320         int lextable_pos;
321         unsigned char parser_state; /* enum lws_token_indexes */
322         int current_alloc_len;
323 #ifndef LWS_NO_CLIENT
324         char initial_handshake_hash_base64[30];
325 #endif
326 };
327
328 struct _lws_websocket_related {
329         char *rx_user_buffer;
330         int rx_user_buffer_head;
331         unsigned char masking_key_04[20];
332         unsigned char frame_masking_nonce_04[4];
333         unsigned char frame_mask_04[20];
334         unsigned char frame_mask_index;
335         size_t rx_packet_length;
336         unsigned char opcode;
337         unsigned int final:1;
338         unsigned char rsv;
339         unsigned int frame_is_binary:1;
340         int pings_vs_pongs;
341         unsigned int all_zero_nonce:1;
342         enum lws_close_status close_reason;
343         unsigned char *rxflow_buffer;
344         int rxflow_len;
345         int rxflow_pos;
346         int rxflow_change_to;
347         unsigned int this_frame_masked:1;
348 };
349
350 struct libwebsocket {
351
352         /* lifetime members */
353
354         const struct libwebsocket_protocols *protocol;
355 #ifndef LWS_NO_EXTENSIONS
356         struct libwebsocket_extension *
357                                    active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
358         void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
359         unsigned char count_active_extensions;
360         unsigned int extension_data_pending:1;
361         struct libwebsocket *extension_handles;
362         struct libwebsocket *candidate_children_list;
363 #endif
364         unsigned char ietf_spec_revision;
365
366         char mode; /* enum connection_mode */
367         char state; /* enum lws_connection_states */
368         char lws_rx_parse_state; /* enum lws_rx_parse_state */
369         char rx_frame_type; /* enum libwebsocket_write_protocol */
370
371         char pending_timeout; /* enum pending_timeout */
372         unsigned long pending_timeout_limit;
373
374         int sock;
375         int position_in_fds_table;
376 #ifdef LWS_LATENCY
377         unsigned long action_start;
378         unsigned long latency_start;
379 #endif
380
381         void *user_space;
382
383         /* members with mutually exclusive lifetimes are unionized */
384
385         union u {
386                 struct _lws_http_mode_related http;
387                 struct _lws_header_related hdr;
388                 struct _lws_websocket_related ws;
389         } u;
390
391 #ifndef LWS_NO_CLIENT
392         char *c_path;
393         char *c_host;
394         char *c_origin;
395         char *c_protocol;
396         callback_function *c_callback;
397
398         char *c_address;
399         unsigned short c_port;
400 #endif
401
402 #ifdef LWS_OPENSSL_SUPPORT
403         SSL *ssl;
404         BIO *client_bio;
405         unsigned int use_ssl:2;
406 #endif  
407 };
408
409 #ifndef LWS_LATENCY
410 static inline void lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completion) { while (0); }
411 static inline void lws_latency_pre(struct libwebsocket_context *context, struct libwebsocket *wsi) { while (0); }
412 #else
413 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
414 extern void
415 lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completion);
416 #endif
417
418 extern int
419 libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
420
421 extern int
422 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
423
424 extern int
425 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
426                                                 unsigned char *buf, size_t len);
427
428 extern int
429 lws_b64_selftest(void);
430
431 extern struct libwebsocket *
432 wsi_from_fd(struct libwebsocket_context *context, int fd);
433
434 extern int
435 insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi);
436
437 extern void
438 libwebsocket_set_timeout(struct libwebsocket *wsi,
439                                          enum pending_timeout reason, int secs);
440
441 extern int
442 lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
443
444
445 extern void
446 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
447                                     struct libwebsocket *wsi, unsigned int sec);
448
449 extern struct libwebsocket *
450 __libwebsocket_client_connect_2(struct libwebsocket_context *context,
451         struct libwebsocket *wsi);
452
453 extern struct libwebsocket *
454 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
455
456 extern char *
457 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
458                 struct libwebsocket *wsi, char *pkt);
459
460 extern int
461 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
462                               struct libwebsocket *wsi, struct pollfd *pollfd);
463 #ifndef LWS_NO_EXTENSIONS
464 extern int
465 lws_any_extension_handled(struct libwebsocket_context *context,
466                           struct libwebsocket *wsi,
467                           enum libwebsocket_extension_callback_reasons r,
468                           void *v, size_t len);
469
470 extern void *
471 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
472                           struct libwebsocket_extension *ext);
473 #endif
474
475 extern int
476 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
477                 struct libwebsocket *wsi);
478
479 extern int
480 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
481
482 extern int
483 lws_issue_raw_ext_access(struct libwebsocket *wsi,
484                                                 unsigned char *buf, size_t len);
485
486 extern int
487 _libwebsocket_rx_flow_control(struct libwebsocket *wsi);
488
489 extern int
490 user_callback_handle_rxflow(callback_function, struct libwebsocket_context * context,
491                         struct libwebsocket *wsi,
492                          enum libwebsocket_callback_reasons reason, void *user,
493                                                           void *in, size_t len);
494
495 extern int
496 lws_set_socket_options(struct libwebsocket_context *context, int fd);
497
498 #ifndef LWS_OPENSSL_SUPPORT
499
500 unsigned char *
501 SHA1(const unsigned char *d, size_t n, unsigned char *md);
502
503 #endif