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