lib/server.c: fix ipv6 support
[platform/upstream/libwebsockets.git] / lib / server-handshake.c
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 #include "private-libwebsockets.h"
23
24 #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
25 #ifndef LWS_NO_EXTENSIONS
26 LWS_VISIBLE int
27 lws_extension_server_handshake(struct lws *wsi, char **p)
28 {
29         struct lws_context *context = wsi->context;
30         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
31         const struct lws_extension *ext;
32         char ext_name[128];
33         int ext_count = 0;
34         int more = 1;
35         char ignore;
36         int n, m;
37         char *c;
38
39         /*
40          * Figure out which extensions the client has that we want to
41          * enable on this connection, and give him back the list
42          */
43         if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
44                 return 0;
45
46         /*
47          * break down the list of client extensions
48          * and go through them
49          */
50
51         if (lws_hdr_copy(wsi, (char *)pt->serv_buf, LWS_MAX_SOCKET_IO_BUF,
52                          WSI_TOKEN_EXTENSIONS) < 0)
53                 return 1;
54
55         c = (char *)pt->serv_buf;
56         lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
57         wsi->count_act_ext = 0;
58         n = 0;
59         ignore = 0;
60         while (more) {
61
62                 if (*c && (*c != ',' && *c != '\t')) {
63                         if (*c == ';')
64                                 ignore = 1;
65                         if (ignore || *c == ' ') {
66                                 c++;
67                                 continue;
68                         }
69                         ext_name[n] = *c++;
70                         if (n < sizeof(ext_name) - 1)
71                                 n++;
72                         continue;
73                 }
74                 ext_name[n] = '\0';
75
76                 ignore = 0;
77                 if (!*c)
78                         more = 0;
79                 else {
80                         c++;
81                         if (!n)
82                                 continue;
83                 }
84
85                 /* check a client's extension against our support */
86
87                 ext = wsi->vhost->extensions;
88
89                 while (ext && ext->callback) {
90
91                         if (strcmp(ext_name, ext->name)) {
92                                 ext++;
93                                 continue;
94                         }
95 #if 0
96                         m = ext->callback(lws_get_context(wsi), ext, wsi,
97                                           LWS_EXT_CB_ARGS_VALIDATE,
98                                           NULL, start + n, 0);
99                         if (m) {
100                                 ext++;
101                                 continue;
102                         }
103 #endif
104                         /*
105                          * oh, we do support this one he asked for... but let's
106                          * ask user code if it's OK to apply it on this
107                          * particular connection + protocol
108                          */
109                         m = wsi->vhost->protocols[0].callback(wsi,
110                                 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
111                                 wsi->user_space, ext_name, 0);
112
113                         /*
114                          * zero return from callback means go ahead and allow
115                          * the extension, it's what we get if the callback is
116                          * unhandled
117                          */
118
119                         if (m) {
120                                 ext++;
121                                 continue;
122                         }
123
124                         /* apply it */
125
126                         ext_count++;
127
128                         /* instantiate the extension on this conn */
129
130                         wsi->active_extensions[wsi->count_act_ext] = ext;
131
132                         /* allow him to construct his context */
133
134                         if (ext->callback(lws_get_context(wsi), ext, wsi,
135                                       LWS_EXT_CB_CONSTRUCT,
136                                       (void *)&wsi->act_ext_user[wsi->count_act_ext],
137                                       NULL, 0)) {
138                                 lwsl_notice("ext %s failed construction\n", ext_name);
139                                 ext_count--;
140                                 ext++;
141                                 continue;
142                         }
143
144                         if (ext_count > 1)
145                                 *(*p)++ = ',';
146                         else
147                                 LWS_CPYAPP(*p, "\x0d\x0aSec-WebSocket-Extensions: ");
148                         *p += sprintf(*p, "%s", ext_name);
149
150                         wsi->count_act_ext++;
151                         lwsl_parser("count_act_ext <- %d\n", wsi->count_act_ext);
152
153                         ext++;
154                 }
155
156                 n = 0;
157         }
158
159         return 0;
160 }
161 #endif
162 int
163 handshake_0405(struct lws_context *context, struct lws *wsi)
164 {
165         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
166         unsigned char hash[20];
167         int n, accept_len;
168         char *response;
169         char *p;
170
171         if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
172             !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
173                 lwsl_parser("handshake_04 missing pieces\n");
174                 /* completed header processing, but missing some bits */
175                 goto bail;
176         }
177
178         if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
179                 lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
180                 goto bail;
181         }
182
183         /*
184          * since key length is restricted above (currently 128), cannot
185          * overflow
186          */
187         n = sprintf((char *)pt->serv_buf,
188                     "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
189                     lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
190
191         lws_SHA1(pt->serv_buf, n, hash);
192
193         accept_len = lws_b64_encode_string((char *)hash, 20, (char *)pt->serv_buf,
194                                            LWS_MAX_SOCKET_IO_BUF);
195         if (accept_len < 0) {
196                 lwsl_warn("Base64 encoded hash too long\n");
197                 goto bail;
198         }
199
200         /* allocate the per-connection user memory (if any) */
201         if (lws_ensure_user_space(wsi))
202                 goto bail;
203
204         /* create the response packet */
205
206         /* make a buffer big enough for everything */
207
208         response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
209         p = response;
210         LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
211                       "Upgrade: WebSocket\x0d\x0a"
212                       "Connection: Upgrade\x0d\x0a"
213                       "Sec-WebSocket-Accept: ");
214         strcpy(p, (char *)pt->serv_buf);
215         p += accept_len;
216
217         if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
218                 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
219                 n = lws_hdr_copy(wsi, p, 128, WSI_TOKEN_PROTOCOL);
220                 if (n < 0)
221                         goto bail;
222                 p += n;
223         }
224
225 #ifndef LWS_NO_EXTENSIONS
226         /*
227          * Figure out which extensions the client has that we want to
228          * enable on this connection, and give him back the list
229          */
230         if (lws_extension_server_handshake(wsi, &p))
231                 goto bail;
232 #endif
233
234         //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
235
236         /* end of response packet */
237
238         LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
239
240         if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
241                                        response, p - response)) {
242
243                 /* okay send the handshake response accepting the connection */
244
245                 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
246 #ifdef DEBUG
247                 fwrite(response, 1,  p - response, stderr);
248 #endif
249                 n = lws_write(wsi, (unsigned char *)response,
250                               p - response, LWS_WRITE_HTTP_HEADERS);
251                 if (n != (p - response)) {
252                         lwsl_debug("handshake_0405: ERROR writing to socket\n");
253                         goto bail;
254                 }
255
256         }
257
258         /* alright clean up and set ourselves into established state */
259
260         wsi->state = LWSS_ESTABLISHED;
261         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
262
263         /* notify user code that we're ready to roll */
264
265         if (wsi->protocol->callback)
266                 if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
267                                             wsi->user_space,
268 #ifdef LWS_OPENSSL_SUPPORT
269                                             wsi->ssl,
270 #else
271                                             NULL,
272 #endif
273                                             0))
274                         goto bail;
275
276         return 0;
277
278
279 bail:
280         /* caller will free up his parsing allocations */
281         return -1;
282 }
283