fork-sever-process-and-introduce-broadcast-api.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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <signal.h>
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39 #include <poll.h>
40 #include <sys/mman.h>
41
42 #ifdef LWS_OPENSSL_SUPPORT
43 #include <openssl/ssl.h>
44 #include <openssl/evp.h>
45 #include <openssl/err.h>
46 #endif
47
48 #include "libwebsockets.h"
49
50 /* #define DEBUG  */
51
52
53 #ifdef DEBUG
54 #define debug(format, args...)  \
55       fprintf(stderr, format , ## args)
56 #else
57 #define debug(format, args...)
58 #endif
59
60 #ifdef LWS_OPENSSL_SUPPORT
61 extern SSL_CTX *ssl_ctx;
62 extern int use_ssl;
63 #endif
64
65
66 #define MAX_CLIENTS 100
67 #define LWS_MAX_HEADER_NAME_LENGTH 64
68 #define LWS_MAX_HEADER_LEN 4096
69 #define LWS_INITIAL_HDR_ALLOC 256
70 #define LWS_ADDITIONAL_HDR_ALLOC 64
71 #define MAX_USER_RX_BUFFER 512
72 #define MAX_BROADCAST_PAYLOAD 1024
73 #define LWS_MAX_PROTOCOLS 10
74
75 enum lws_connection_states {
76         WSI_STATE_HTTP,
77         WSI_STATE_HTTP_HEADERS,
78         WSI_STATE_DEAD_SOCKET,
79         WSI_STATE_ESTABLISHED
80 };
81
82 enum lws_token_indexes {
83         WSI_TOKEN_GET_URI,
84         WSI_TOKEN_HOST,
85         WSI_TOKEN_CONNECTION,
86         WSI_TOKEN_KEY1,
87         WSI_TOKEN_KEY2,
88         WSI_TOKEN_PROTOCOL,
89         WSI_TOKEN_UPGRADE,
90         WSI_TOKEN_ORIGIN,
91         WSI_TOKEN_DRAFT,
92         WSI_TOKEN_CHALLENGE,
93
94         /* always last real token index*/
95         WSI_TOKEN_COUNT,
96         /* parser state additions */
97         WSI_TOKEN_NAME_PART,
98         WSI_TOKEN_SKIPPING,
99         WSI_TOKEN_SKIPPING_SAW_CR,
100         WSI_PARSING_COMPLETE
101 };
102
103 enum lws_rx_parse_state {
104         LWS_RXPS_NEW,
105
106         LWS_RXPS_SEEN_76_FF,
107         LWS_RXPS_PULLING_76_LENGTH,
108         LWS_RXPS_EAT_UNTIL_76_FF,
109
110         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
111 };
112
113
114 struct lws_tokens {
115         char *token;
116         int token_len;
117 };
118
119 struct libwebsocket_context {
120         struct libwebsocket *wsi[MAX_CLIENTS + 1];
121         struct pollfd fds[MAX_CLIENTS + 1];
122         int fds_count;
123 #ifdef LWS_OPENSSL_SUPPORT
124         int use_ssl;
125 #endif
126         int count_protocols;
127 };
128
129
130 /*
131  * This is totally opaque to code using the library.  It's exported as a
132  * forward-reference pointer-only declaration; the user can use the pointer with
133  * other APIs to get information out of it.
134  */
135
136 struct libwebsocket {
137         const struct libwebsocket_protocols *protocol;
138
139         enum lws_connection_states state;
140
141         char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
142         int name_buffer_pos;
143         int current_alloc_len;
144         enum lws_token_indexes parser_state;
145         struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
146         int ietf_spec_revision;
147         char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
148                                                   LWS_SEND_BUFFER_POST_PADDING];
149         int rx_user_buffer_head;
150
151         int sock;
152
153         enum lws_rx_parse_state lws_rx_parse_state;
154         size_t rx_packet_length;
155
156 #ifdef LWS_OPENSSL_SUPPORT
157         SSL *ssl;
158 #endif
159
160         void *user_space;
161 };
162
163 extern void
164 libwebsocket_close_and_free_session(struct libwebsocket *wsi);
165
166 extern void
167 libwebsockets_md5(const unsigned char *input, int ilen, unsigned char *output);
168
169 extern int
170 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
171
172 extern int
173 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
174                                                 unsigned char *buf, size_t len);