blob: d099a695ec9d23396183b3bee4d0a816755fe733 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * 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
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Peter Hinz56885f32011-03-02 22:03:47 +000027#else
28#include <ifaddrs.h>
Andy Green7627af52011-03-09 15:13:52 +000029#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080030#include <sys/socket.h>
31#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000032#endif
Andy Green2e24da02011-03-05 16:12:04 +000033
34#ifdef LWS_OPENSSL_SUPPORT
35int openssl_websocket_private_data_index;
36#endif
37
Andy Greenaa6fc442012-04-12 13:26:49 +080038#ifdef __MINGW32__
39#include "../win32port/win32helpers/websock-w32.c"
40#else
41#ifdef __MINGW64__
42#include "../win32port/win32helpers/websock-w32.c"
43#endif
44#endif
45
Andy Greenbe93fef2011-02-14 20:25:43 +000046/*
47 * In-place str to lower case
48 */
49
50static void
51strtolower(char *s)
52{
53 while (*s) {
54 *s = tolower(*s);
55 s++;
56 }
57}
58
Andy Green0d338332011-02-12 11:57:43 +000059/* file descriptor hash management */
60
61struct libwebsocket *
Peter Hinz56885f32011-03-02 22:03:47 +000062wsi_from_fd(struct libwebsocket_context *context, int fd)
Andy Green0d338332011-02-12 11:57:43 +000063{
64 int h = LWS_FD_HASH(fd);
65 int n = 0;
66
Peter Hinz56885f32011-03-02 22:03:47 +000067 for (n = 0; n < context->fd_hashtable[h].length; n++)
68 if (context->fd_hashtable[h].wsi[n]->sock == fd)
69 return context->fd_hashtable[h].wsi[n];
Andy Green0d338332011-02-12 11:57:43 +000070
71 return NULL;
72}
73
74int
Peter Hinz56885f32011-03-02 22:03:47 +000075insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000076{
77 int h = LWS_FD_HASH(wsi->sock);
78
Peter Hinz56885f32011-03-02 22:03:47 +000079 if (context->fd_hashtable[h].length == MAX_CLIENTS - 1) {
Andy Green0d338332011-02-12 11:57:43 +000080 fprintf(stderr, "hash table overflow\n");
81 return 1;
82 }
83
Peter Hinz56885f32011-03-02 22:03:47 +000084 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
Andy Green0d338332011-02-12 11:57:43 +000085
86 return 0;
87}
88
89int
Peter Hinz56885f32011-03-02 22:03:47 +000090delete_from_fd(struct libwebsocket_context *context, int fd)
Andy Green0d338332011-02-12 11:57:43 +000091{
92 int h = LWS_FD_HASH(fd);
93 int n = 0;
94
Peter Hinz56885f32011-03-02 22:03:47 +000095 for (n = 0; n < context->fd_hashtable[h].length; n++)
96 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
97 while (n < context->fd_hashtable[h].length) {
98 context->fd_hashtable[h].wsi[n] =
99 context->fd_hashtable[h].wsi[n + 1];
Andy Green0d338332011-02-12 11:57:43 +0000100 n++;
101 }
Peter Hinz56885f32011-03-02 22:03:47 +0000102 context->fd_hashtable[h].length--;
Andy Green0d338332011-02-12 11:57:43 +0000103
104 return 0;
105 }
106
107 fprintf(stderr, "Failed to find fd %d requested for "
108 "delete in hashtable\n", fd);
109 return 1;
110}
111
Andy Green1f9bf522011-02-14 21:14:37 +0000112#ifdef LWS_OPENSSL_SUPPORT
113static void
114libwebsockets_decode_ssl_error(void)
115{
116 char buf[256];
117 u_long err;
118
119 while ((err = ERR_get_error()) != 0) {
120 ERR_error_string_n(err, buf, sizeof(buf));
121 fprintf(stderr, "*** %s\n", buf);
122 }
123}
124#endif
Andy Green0d338332011-02-12 11:57:43 +0000125
Andy Green32375b72011-02-19 08:32:53 +0000126
127static int
Andy Green6ee372f2012-04-09 15:09:01 +0800128interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
Andy Green32375b72011-02-19 08:32:53 +0000129{
130 int rc = -1;
Peter Hinz56885f32011-03-02 22:03:47 +0000131#ifdef WIN32
Andy Green6ee372f2012-04-09 15:09:01 +0800132 /* TODO */
Peter Hinz56885f32011-03-02 22:03:47 +0000133#else
Andy Green32375b72011-02-19 08:32:53 +0000134 struct ifaddrs *ifr;
135 struct ifaddrs *ifc;
136 struct sockaddr_in *sin;
137
138 getifaddrs(&ifr);
139 for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
140 if (strcmp(ifc->ifa_name, ifname))
141 continue;
142 if (ifc->ifa_addr == NULL)
143 continue;
144 sin = (struct sockaddr_in *)ifc->ifa_addr;
145 if (sin->sin_family != AF_INET)
146 continue;
147 memcpy(addr, sin, addrlen);
Andy Green6ee372f2012-04-09 15:09:01 +0800148 rc = 0;
Andy Green32375b72011-02-19 08:32:53 +0000149 }
150
151 freeifaddrs(ifr);
Peter Hinz56885f32011-03-02 22:03:47 +0000152#endif
Andy Green32375b72011-02-19 08:32:53 +0000153 return rc;
154}
155
Andy Green8f037e42010-12-19 22:13:26 +0000156void
Peter Hinz56885f32011-03-02 22:03:47 +0000157libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000158 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000159{
Andy Greenb45993c2010-12-18 15:13:50 +0000160 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000161 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000162 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
163 LWS_SEND_BUFFER_POST_PADDING];
Andy Greenc44159f2011-03-07 07:08:18 +0000164 int ret;
165 int m;
166 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100167 struct libwebsocket_extension *ext;
Andy Greenb45993c2010-12-18 15:13:50 +0000168
Andy Green4b6fbe12011-02-14 08:03:48 +0000169 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000170 return;
171
Andy Green62c54d22011-02-14 09:14:25 +0000172 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000173
Andy Green62c54d22011-02-14 09:14:25 +0000174 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000175 return;
176
Andy Greenda527df2011-03-07 07:08:12 +0000177 wsi->close_reason = reason;
178
179 /*
Andy Green68b45042011-05-25 21:41:57 +0100180 * are his extensions okay with him closing? Eg he might be a mux
181 * parent and just his ch1 aspect is closing?
182 */
183
184
185 for (n = 0; n < wsi->count_active_extensions; n++) {
186 if (!wsi->active_extensions[n]->callback)
187 continue;
188
189 m = wsi->active_extensions[n]->callback(context,
190 wsi->active_extensions[n], wsi,
191 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
192 wsi->active_extensions_user[n], NULL, 0);
193
194 /*
195 * if somebody vetoed actually closing him at this time....
196 * up to the extension to track the attempted close, let's
197 * just bail
198 */
199
200 if (m) {
Andy Greencc012472011-11-07 19:53:23 +0800201 debug("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100202 return;
203 }
204 }
205
206
207
208 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000209 * flush any tx pending from extensions, since we may send close packet
210 * if there are problems with send, just nuke the connection
211 */
212
213 ret = 1;
214 while (ret == 1) {
215
216 /* default to nobody has more to spill */
217
218 ret = 0;
219 eff_buf.token = NULL;
220 eff_buf.token_len = 0;
221
222 /* show every extension the new incoming data */
223
224 for (n = 0; n < wsi->count_active_extensions; n++) {
225 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000226 wsi->protocol->owning_server,
227 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000228 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
229 wsi->active_extensions_user[n], &eff_buf, 0);
230 if (m < 0) {
231 fprintf(stderr, "Extension reports "
232 "fatal error\n");
233 goto just_kill_connection;
234 }
235 if (m)
236 /*
237 * at least one extension told us he has more
238 * to spill, so we will go around again after
239 */
240 ret = 1;
241 }
242
243 /* assuming they left us something to send, send it */
244
245 if (eff_buf.token_len)
246 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
247 eff_buf.token_len))
248 goto just_kill_connection;
249 }
250
251 /*
Andy Greenda527df2011-03-07 07:08:12 +0000252 * signal we are closing, libsocket_write will
253 * add any necessary version-specific stuff. If the write fails,
254 * no worries we are closing anyway. If we didn't initiate this
255 * close, then our state has been changed to
256 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
257 *
258 * Likewise if it's a second call to close this connection after we
259 * sent the close indication to the peer already, we are in state
260 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
261 */
262
263 if (old_state == WSI_STATE_ESTABLISHED &&
264 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100265
Andy Greencc012472011-11-07 19:53:23 +0800266 debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100267
Andy Greenda527df2011-03-07 07:08:12 +0000268 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
269 0, LWS_WRITE_CLOSE);
270 if (!n) {
271 /*
272 * we have sent a nice protocol level indication we
273 * now wish to close, we should not send anything more
274 */
275
276 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
277
278 /* and we should wait for a reply for a bit */
279
280 libwebsocket_set_timeout(wsi,
David Galeanoc9f1ff82013-01-09 18:01:23 +0800281 PENDING_TIMEOUT_CLOSE_ACK, AWAITING_TIMEOUT);
Andy Greenda527df2011-03-07 07:08:12 +0000282
Andy Greencc012472011-11-07 19:53:23 +0800283 debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000284
285 return;
286 }
287
288 /* else, the send failed and we should just hang up */
289 }
290
Andy Greenc44159f2011-03-07 07:08:18 +0000291just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100292
Andy Greencc012472011-11-07 19:53:23 +0800293 debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100294
Andy Greenda527df2011-03-07 07:08:12 +0000295 /*
296 * we won't be servicing or receiving anything further from this guy
297 * remove this fd from wsi mapping hashtable
298 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000299
Andy Greena41314f2011-05-23 10:00:03 +0100300 if (wsi->sock)
301 delete_from_fd(context, wsi->sock);
Andy Green4b6fbe12011-02-14 08:03:48 +0000302
303 /* delete it from the internal poll list if still present */
304
Peter Hinz56885f32011-03-02 22:03:47 +0000305 for (n = 0; n < context->fds_count; n++) {
306 if (context->fds[n].fd != wsi->sock)
Andy Green4b6fbe12011-02-14 08:03:48 +0000307 continue;
Peter Hinz56885f32011-03-02 22:03:47 +0000308 while (n < context->fds_count - 1) {
309 context->fds[n] = context->fds[n + 1];
Andy Green4b6fbe12011-02-14 08:03:48 +0000310 n++;
311 }
Peter Hinz56885f32011-03-02 22:03:47 +0000312 context->fds_count--;
Andy Green4b6fbe12011-02-14 08:03:48 +0000313 /* we only have to deal with one */
Peter Hinz56885f32011-03-02 22:03:47 +0000314 n = context->fds_count;
Andy Green4b6fbe12011-02-14 08:03:48 +0000315 }
316
317 /* remove also from external POLL support via protocol 0 */
Andy Greena41314f2011-05-23 10:00:03 +0100318 if (wsi->sock)
319 context->protocols[0].callback(context, wsi,
Andy Green4b6fbe12011-02-14 08:03:48 +0000320 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
321
Andy Green251f6fa2010-11-03 11:13:06 +0000322 wsi->state = WSI_STATE_DEAD_SOCKET;
323
Andy Green4b6fbe12011-02-14 08:03:48 +0000324 /* tell the user it's all over for this guy */
325
Andy Greend4302732011-02-28 07:45:29 +0000326 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800327 ((old_state == WSI_STATE_ESTABLISHED) ||
328 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
329 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Greencc012472011-11-07 19:53:23 +0800330 debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000331 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000332 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800333 } else
Andy Green6ee372f2012-04-09 15:09:01 +0800334 debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000335
Andy Greenef660a92011-03-06 10:29:38 +0000336 /* deallocate any active extension contexts */
337
338 for (n = 0; n < wsi->count_active_extensions; n++) {
339 if (!wsi->active_extensions[n]->callback)
340 continue;
341
Andy Green46c2ea02011-03-22 09:04:01 +0000342 wsi->active_extensions[n]->callback(context,
343 wsi->active_extensions[n], wsi,
344 LWS_EXT_CALLBACK_DESTROY,
345 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000346
347 free(wsi->active_extensions_user[n]);
348 }
349
Andy Greena41314f2011-05-23 10:00:03 +0100350 /*
351 * inform all extensions in case they tracked this guy out of band
352 * even though not active on him specifically
353 */
354
355 ext = context->extensions;
356 while (ext && ext->callback) {
357 ext->callback(context, ext, wsi,
358 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
359 NULL, NULL, 0);
360 ext++;
361 }
362
Andy Greenef660a92011-03-06 10:29:38 +0000363 /* free up his parsing allocations */
Andy Green4b6fbe12011-02-14 08:03:48 +0000364
Andy Green251f6fa2010-11-03 11:13:06 +0000365 for (n = 0; n < WSI_TOKEN_COUNT; n++)
366 if (wsi->utf8_token[n].token)
367 free(wsi->utf8_token[n].token);
368
Andy Greena41314f2011-05-23 10:00:03 +0100369 if (wsi->c_address)
370 free(wsi->c_address);
371
Andy Green0ca6a172010-12-19 20:50:01 +0000372/* fprintf(stderr, "closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000373
Andy Green3faa9c72010-11-08 17:03:03 +0000374#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000375 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000376 n = SSL_get_fd(wsi->ssl);
377 SSL_shutdown(wsi->ssl);
Peter Hinz56885f32011-03-02 22:03:47 +0000378#ifdef WIN32
379 closesocket(n);
380#else
Andy Green3faa9c72010-11-08 17:03:03 +0000381 close(n);
Peter Hinz56885f32011-03-02 22:03:47 +0000382#endif
Andy Green3faa9c72010-11-08 17:03:03 +0000383 SSL_free(wsi->ssl);
384 } else {
385#endif
386 shutdown(wsi->sock, SHUT_RDWR);
Peter Hinz56885f32011-03-02 22:03:47 +0000387#ifdef WIN32
Andy Green66a16f32011-05-24 22:07:45 +0100388 if (wsi->sock)
389 closesocket(wsi->sock);
Peter Hinz56885f32011-03-02 22:03:47 +0000390#else
Andy Green66a16f32011-05-24 22:07:45 +0100391 if (wsi->sock)
392 close(wsi->sock);
Peter Hinz56885f32011-03-02 22:03:47 +0000393#endif
Andy Green3faa9c72010-11-08 17:03:03 +0000394#ifdef LWS_OPENSSL_SUPPORT
395 }
396#endif
David Brooks2c60d952012-04-20 12:19:01 +0800397 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000398 free(wsi->user_space);
399
Andy Green251f6fa2010-11-03 11:13:06 +0000400 free(wsi);
401}
402
Andy Green07034092011-02-13 08:37:12 +0000403/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000404 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800405 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000406 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000407 * @fd: Connection socket descriptor
408 */
409
410void
Peter Hinz56885f32011-03-02 22:03:47 +0000411libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000412{
Peter Hinz56885f32011-03-02 22:03:47 +0000413 struct libwebsocket *wsi = wsi_from_fd(context, fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000414
415 if (wsi == NULL)
416 return;
417
Peter Hinz56885f32011-03-02 22:03:47 +0000418 libwebsocket_close_and_free_session(context, wsi,
Andy Green6da560c2011-02-26 11:06:27 +0000419 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenf7ee5492011-02-13 09:04:21 +0000420}
421
422
423/**
Andy Green07034092011-02-13 08:37:12 +0000424 * libwebsockets_get_peer_addresses() - Get client address information
425 * @fd: Connection socket descriptor
426 * @name: Buffer to take client address name
427 * @name_len: Length of client address name buffer
428 * @rip: Buffer to take client address IP qotted quad
429 * @rip_len: Length of client address IP buffer
430 *
431 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800432 * the client connected with socket descriptor @fd. Names may be
433 * truncated if there is not enough room. If either cannot be
434 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000435 */
436
437void
438libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
439 char *rip, int rip_len)
440{
441 unsigned int len;
442 struct sockaddr_in sin;
443 struct hostent *host;
444 struct hostent *host1;
445 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000446 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000447 int n;
David Galeanocb193682013-01-09 15:29:00 +0800448#ifdef AF_LOCAL
449 struct sockaddr_un *un;
450#endif
Andy Green07034092011-02-13 08:37:12 +0000451
452 rip[0] = '\0';
453 name[0] = '\0';
454
455 len = sizeof sin;
456 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
457 perror("getpeername");
458 return;
459 }
Andy Green6ee372f2012-04-09 15:09:01 +0800460
Andy Green07034092011-02-13 08:37:12 +0000461 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
462 AF_INET);
463 if (host == NULL) {
464 perror("gethostbyaddr");
465 return;
466 }
467
468 strncpy(name, host->h_name, name_len);
469 name[name_len - 1] = '\0';
470
471 host1 = gethostbyname(host->h_name);
472 if (host1 == NULL)
473 return;
Andy Greenf92def72011-03-09 15:02:20 +0000474 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000475 n = 0;
476 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000477 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000478 if (p == NULL)
479 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000480 if ((host1->h_addrtype != AF_INET)
481#ifdef AF_LOCAL
482 && (host1->h_addrtype != AF_LOCAL)
483#endif
484 )
Andy Green07034092011-02-13 08:37:12 +0000485 continue;
486
Andy Green7627af52011-03-09 15:13:52 +0000487 if (host1->h_addrtype == AF_INET)
488 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000489#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000490 else {
491 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800492 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000493 ip[sizeof(ip) - 1] = '\0';
494 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000495#endif
Andy Green07034092011-02-13 08:37:12 +0000496 p = NULL;
497 strncpy(rip, ip, rip_len);
498 rip[rip_len - 1] = '\0';
499 }
500}
Andy Green9f990342011-02-12 11:57:45 +0000501
Peter Hinz56885f32011-03-02 22:03:47 +0000502int libwebsockets_get_random(struct libwebsocket_context *context,
503 void *buf, int len)
504{
505 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800506 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000507
508#ifdef WIN32
509 for (n = 0; n < len; n++)
510 p[n] = (unsigned char)rand();
511#else
512 n = read(context->fd_random, p, len);
513#endif
514
515 return n;
516}
517
Andy Green2836c642011-03-07 20:47:41 +0000518unsigned char *
519libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md)
520{
521 return SHA1(d, n, md);
522}
523
Andy Greeneeaacb32011-03-01 20:44:24 +0000524void libwebsockets_00_spaceout(char *key, int spaces, int seed)
525{
526 char *p;
Andy Green6ee372f2012-04-09 15:09:01 +0800527
Andy Greeneeaacb32011-03-01 20:44:24 +0000528 key++;
529 while (spaces--) {
530 if (*key && (seed & 1))
531 key++;
532 seed >>= 1;
Andy Green6ee372f2012-04-09 15:09:01 +0800533
Andy Greeneeaacb32011-03-01 20:44:24 +0000534 p = key + strlen(key);
535 while (p >= key) {
536 p[1] = p[0];
537 p--;
538 }
539 *key++ = ' ';
540 }
541}
542
543void libwebsockets_00_spam(char *key, int count, int seed)
544{
545 char *p;
546
547 key++;
548 while (count--) {
Andy Green6ee372f2012-04-09 15:09:01 +0800549
Andy Greeneeaacb32011-03-01 20:44:24 +0000550 if (*key && (seed & 1))
551 key++;
552 seed >>= 1;
553
554 p = key + strlen(key);
555 while (p >= key) {
556 p[1] = p[0];
557 p--;
558 }
559 *key++ = 0x21 + ((seed & 0xffff) % 15);
560 /* 4 would use it up too fast.. not like it matters */
561 seed >>= 1;
562 }
563}
564
Andy Green95a7b5d2011-03-06 10:29:39 +0000565int lws_send_pipe_choked(struct libwebsocket *wsi)
566{
567 struct pollfd fds;
568
569 fds.fd = wsi->sock;
570 fds.events = POLLOUT;
571 fds.revents = 0;
572
573 if (poll(&fds, 1, 0) != 1)
574 return 1;
575
576 if ((fds.revents & POLLOUT) == 0)
577 return 1;
578
579 /* okay to send another packet without blocking */
580
581 return 0;
582}
583
Andy Greena41314f2011-05-23 10:00:03 +0100584int
Andy Green3b84c002011-03-06 13:14:42 +0000585lws_handle_POLLOUT_event(struct libwebsocket_context *context,
586 struct libwebsocket *wsi, struct pollfd *pollfd)
587{
588 struct lws_tokens eff_buf;
589 int n;
590 int ret;
591 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100592 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000593
Andy Greena41314f2011-05-23 10:00:03 +0100594 for (n = 0; n < wsi->count_active_extensions; n++) {
595 if (!wsi->active_extensions[n]->callback)
596 continue;
597
598 m = wsi->active_extensions[n]->callback(context,
599 wsi->active_extensions[n], wsi,
600 LWS_EXT_CALLBACK_IS_WRITEABLE,
601 wsi->active_extensions_user[n], NULL, 0);
602 if (m > handled)
603 handled = m;
604 }
605
606 if (handled == 1)
607 goto notify_action;
608
609 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000610 goto user_service;
611
612 /*
613 * check in on the active extensions, see if they
614 * had pending stuff to spill... they need to get the
615 * first look-in otherwise sequence will be disordered
616 *
617 * NULL, zero-length eff_buf means just spill pending
618 */
619
620 ret = 1;
621 while (ret == 1) {
622
623 /* default to nobody has more to spill */
624
625 ret = 0;
626 eff_buf.token = NULL;
627 eff_buf.token_len = 0;
628
629 /* give every extension a chance to spill */
630
631 for (n = 0; n < wsi->count_active_extensions; n++) {
632 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000633 wsi->protocol->owning_server,
634 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000635 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
636 wsi->active_extensions_user[n], &eff_buf, 0);
637 if (m < 0) {
Andy Green6ee372f2012-04-09 15:09:01 +0800638 fprintf(stderr, "ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000639 return -1;
640 }
641 if (m)
642 /*
643 * at least one extension told us he has more
644 * to spill, so we will go around again after
645 */
646 ret = 1;
647 }
648
649 /* assuming they gave us something to send, send it */
650
651 if (eff_buf.token_len) {
652 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
653 eff_buf.token_len))
654 return -1;
655 } else
656 continue;
657
658 /* no extension has more to spill */
659
660 if (!ret)
661 continue;
662
663 /*
664 * There's more to spill from an extension, but we just sent
665 * something... did that leave the pipe choked?
666 */
667
668 if (!lws_send_pipe_choked(wsi))
669 /* no we could add more */
670 continue;
671
Andy Greencc012472011-11-07 19:53:23 +0800672 debug("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000673
674 /*
675 * Yes, he's choked. Leave the POLLOUT masked on so we will
676 * come back here when he is unchoked. Don't call the user
677 * callback to enforce ordering of spilling, he'll get called
678 * when we come back here and there's nothing more to spill.
679 */
680
681 return 0;
682 }
683
684 wsi->extension_data_pending = 0;
685
686user_service:
687 /* one shot */
688
Andy Greena41314f2011-05-23 10:00:03 +0100689 if (pollfd) {
690 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000691
Andy Greena41314f2011-05-23 10:00:03 +0100692 /* external POLL support via protocol 0 */
693 context->protocols[0].callback(context, wsi,
694 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
695 (void *)(long)wsi->sock, NULL, POLLOUT);
696 }
697
698notify_action:
Andy Green3b84c002011-03-06 13:14:42 +0000699
Andy Green9e4c2b62011-03-07 20:47:39 +0000700 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
701 n = LWS_CALLBACK_CLIENT_WRITEABLE;
702 else
703 n = LWS_CALLBACK_SERVER_WRITEABLE;
704
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800705 wsi->protocol->callback(context, wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000706
707 return 0;
708}
709
710
711
Andy Greena41314f2011-05-23 10:00:03 +0100712void
713libwebsocket_service_timeout_check(struct libwebsocket_context *context,
714 struct libwebsocket *wsi, unsigned int sec)
715{
716 int n;
717
718 /*
719 * if extensions want in on it (eg, we are a mux parent)
720 * give them a chance to service child timeouts
721 */
722
723 for (n = 0; n < wsi->count_active_extensions; n++)
724 wsi->active_extensions[n]->callback(
725 context, wsi->active_extensions[n],
726 wsi, LWS_EXT_CALLBACK_1HZ,
727 wsi->active_extensions_user[n], NULL, sec);
728
729 if (!wsi->pending_timeout)
730 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800731
Andy Greena41314f2011-05-23 10:00:03 +0100732 /*
733 * if we went beyond the allowed time, kill the
734 * connection
735 */
736
737 if (sec > wsi->pending_timeout_limit) {
Andy Greencc012472011-11-07 19:53:23 +0800738 debug("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100739 libwebsocket_close_and_free_session(context,
740 wsi, LWS_CLOSE_STATUS_NOSTATUS);
741 }
742}
743
744struct libwebsocket *
745libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
746{
747 struct libwebsocket *new_wsi;
748 int n;
749
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800750 new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Greena41314f2011-05-23 10:00:03 +0100751 if (new_wsi == NULL) {
752 fprintf(stderr, "Out of memory for new connection\n");
753 return NULL;
754 }
755
Andy Green6ee372f2012-04-09 15:09:01 +0800756 memset(new_wsi, 0, sizeof(struct libwebsocket));
Andy Greena41314f2011-05-23 10:00:03 +0100757 new_wsi->count_active_extensions = 0;
758 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
759
760 /* intialize the instance struct */
761
762 new_wsi->state = WSI_STATE_HTTP;
763 new_wsi->name_buffer_pos = 0;
764 new_wsi->mode = LWS_CONNMODE_WS_SERVING;
765
766 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
767 new_wsi->utf8_token[n].token = NULL;
768 new_wsi->utf8_token[n].token_len = 0;
769 }
770
771 /*
772 * these can only be set once the protocol is known
773 * we set an unestablished connection's protocol pointer
774 * to the start of the supported list, so it can look
775 * for matching ones during the handshake
776 */
777 new_wsi->protocol = context->protocols;
778 new_wsi->user_space = NULL;
779
780 /*
781 * Default protocol is 76 / 00
782 * After 76, there's a header specified to inform which
783 * draft the client wants, when that's seen we modify
784 * the individual connection's spec revision accordingly
785 */
786 new_wsi->ietf_spec_revision = 0;
787
788 return new_wsi;
789}
790
791char *
792libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
793 struct libwebsocket *wsi, char *pkt)
794{
795 char hash[20];
796 char *p = pkt;
797 int n;
798 struct libwebsocket_extension *ext;
Andy Green09226502011-05-28 10:19:19 +0100799 struct libwebsocket_extension *ext1;
Andy Greena41314f2011-05-23 10:00:03 +0100800 int ext_count = 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800801 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
802 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
Andy Greena41314f2011-05-23 10:00:03 +0100803 static const char magic_websocket_guid[] =
804 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
805
806 /*
807 * create the random key
808 */
809
810 n = libwebsockets_get_random(context, hash, 16);
811 if (n != 16) {
812 fprintf(stderr, "Unable to read from random dev %s\n",
813 SYSTEM_RANDOM_FILEPATH);
814 free(wsi->c_path);
815 free(wsi->c_host);
816 if (wsi->c_origin)
817 free(wsi->c_origin);
818 if (wsi->c_protocol)
819 free(wsi->c_protocol);
820 libwebsocket_close_and_free_session(context, wsi,
821 LWS_CLOSE_STATUS_NOSTATUS);
822 return NULL;
823 }
824
825 lws_b64_encode_string(hash, 16, wsi->key_b64,
826 sizeof wsi->key_b64);
827
828 /*
829 * 00 example client handshake
830 *
831 * GET /socket.io/websocket HTTP/1.1
832 * Upgrade: WebSocket
833 * Connection: Upgrade
834 * Host: 127.0.0.1:9999
835 * Origin: http://127.0.0.1
836 * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^
837 * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
838 * Cookie: socketio=websocket
839 *
840 * (Á®Ä0¶†≥
841 *
842 * 04 example client handshake
843 *
844 * GET /chat HTTP/1.1
845 * Host: server.example.com
846 * Upgrade: websocket
847 * Connection: Upgrade
848 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
849 * Sec-WebSocket-Origin: http://example.com
850 * Sec-WebSocket-Protocol: chat, superchat
851 * Sec-WebSocket-Version: 4
852 */
853
854 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
855
David Galeano4fbc40c2013-01-10 10:26:05 +0800856 p += sprintf(p, "Pragma: no-cache\x0d\x0a"
857 "Cache-Control: no-cache\x0d\x0a");
858
Andy Greena41314f2011-05-23 10:00:03 +0100859 if (wsi->ietf_spec_revision == 0) {
860 unsigned char spaces_1, spaces_2;
861 unsigned int max_1, max_2;
862 unsigned int num_1, num_2;
863 unsigned long product_1, product_2;
864 char key_1[40];
865 char key_2[40];
866 unsigned int seed;
867 unsigned int count;
868 char challenge[16];
869
Andy Green6ee372f2012-04-09 15:09:01 +0800870 libwebsockets_get_random(context, &spaces_1, sizeof(char));
871 libwebsockets_get_random(context, &spaces_2, sizeof(char));
Andy Greena41314f2011-05-23 10:00:03 +0100872
873 spaces_1 = (spaces_1 % 12) + 1;
874 spaces_2 = (spaces_2 % 12) + 1;
875
876 max_1 = 4294967295 / spaces_1;
877 max_2 = 4294967295 / spaces_2;
878
879 libwebsockets_get_random(context, &num_1, sizeof(int));
880 libwebsockets_get_random(context, &num_2, sizeof(int));
881
882 num_1 = (num_1 % max_1);
883 num_2 = (num_2 % max_2);
884
885 challenge[0] = num_1 >> 24;
886 challenge[1] = num_1 >> 16;
887 challenge[2] = num_1 >> 8;
888 challenge[3] = num_1;
889 challenge[4] = num_2 >> 24;
890 challenge[5] = num_2 >> 16;
891 challenge[6] = num_2 >> 8;
892 challenge[7] = num_2;
893
894 product_1 = num_1 * spaces_1;
895 product_2 = num_2 * spaces_2;
896
897 sprintf(key_1, "%lu", product_1);
898 sprintf(key_2, "%lu", product_2);
899
900 libwebsockets_get_random(context, &seed, sizeof(int));
901 libwebsockets_get_random(context, &count, sizeof(int));
902
903 libwebsockets_00_spam(key_1, (count % 12) + 1, seed);
904
905 libwebsockets_get_random(context, &seed, sizeof(int));
906 libwebsockets_get_random(context, &count, sizeof(int));
907
908 libwebsockets_00_spam(key_2, (count % 12) + 1, seed);
909
910 libwebsockets_get_random(context, &seed, sizeof(int));
911
912 libwebsockets_00_spaceout(key_1, spaces_1, seed);
913 libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16);
914
915 p += sprintf(p, "Upgrade: WebSocket\x0d\x0a"
916 "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
917 wsi->c_host);
918 if (wsi->c_origin)
Andy Green6ee372f2012-04-09 15:09:01 +0800919 p += sprintf(p, "Origin: %s\x0d\x0a", wsi->c_origin);
Andy Greena41314f2011-05-23 10:00:03 +0100920
921 if (wsi->c_protocol)
922 p += sprintf(p, "Sec-WebSocket-Protocol: %s"
923 "\x0d\x0a", wsi->c_protocol);
924
Andy Green6ee372f2012-04-09 15:09:01 +0800925 p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", key_1);
926 p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", key_2);
Andy Greena41314f2011-05-23 10:00:03 +0100927
928 /* give userland a chance to append, eg, cookies */
929
930 context->protocols[0].callback(context, wsi,
931 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
932 NULL, &p, (pkt + sizeof(pkt)) - p - 12);
933
934 p += sprintf(p, "\x0d\x0a");
935
936 if (libwebsockets_get_random(context, p, 8) != 8)
937 return NULL;
938 memcpy(&challenge[8], p, 8);
939 p += 8;
940
941 /* precompute what we want to see from the server */
942
943 MD5((unsigned char *)challenge, 16,
944 (unsigned char *)wsi->initial_handshake_hash_base64);
945
946 goto issue_hdr;
947 }
948
949 p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
David Galeano4fbc40c2013-01-10 10:26:05 +0800950 p += sprintf(p, "Upgrade: websocket\x0d\x0a"
951 "Connection: Upgrade\x0d\x0a"
952 "Sec-WebSocket-Key: ");
Andy Greena41314f2011-05-23 10:00:03 +0100953 strcpy(p, wsi->key_b64);
954 p += strlen(wsi->key_b64);
955 p += sprintf(p, "\x0d\x0a");
David Galeanoaa0bc862013-01-09 15:31:46 +0800956 if (wsi->c_origin) {
957 if (wsi->ietf_spec_revision == 13) {
958 p += sprintf(p, "Origin: %s\x0d\x0a",
959 wsi->c_origin);
960 }
961 else {
David Galeanocb193682013-01-09 15:29:00 +0800962 p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
Andy Greena41314f2011-05-23 10:00:03 +0100963 wsi->c_origin);
David Galeanoaa0bc862013-01-09 15:31:46 +0800964 }
965 }
Andy Greena41314f2011-05-23 10:00:03 +0100966 if (wsi->c_protocol)
967 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
968 wsi->c_protocol);
969
970 /* tell the server what extensions we could support */
971
972 p += sprintf(p, "Sec-WebSocket-Extensions: ");
973
Andy Green6ee372f2012-04-09 15:09:01 +0800974 ext = context->extensions;
Andy Greena41314f2011-05-23 10:00:03 +0100975 while (ext && ext->callback) {
976
977 n = 0;
Andy Green09226502011-05-28 10:19:19 +0100978 ext1 = context->extensions;
Andy Green09226502011-05-28 10:19:19 +0100979
Andy Green6ee372f2012-04-09 15:09:01 +0800980 while (ext1 && ext1->callback) {
Andy Green09226502011-05-28 10:19:19 +0100981 n |= ext1->callback(context, ext1, wsi,
982 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
983 NULL, (char *)ext->name, 0);
984
985 ext1++;
986 }
987
Andy Green6ee372f2012-04-09 15:09:01 +0800988 if (n) { /* an extension vetos us */
Andy Greencc012472011-11-07 19:53:23 +0800989 debug("ext %s vetoed\n", (char *)ext->name);
Andy Green09226502011-05-28 10:19:19 +0100990 ext++;
991 continue;
992 }
993
Andy Greena41314f2011-05-23 10:00:03 +0100994 n = context->protocols[0].callback(context, wsi,
995 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
996 wsi->user_space, (char *)ext->name, 0);
997
998 /*
999 * zero return from callback means
1000 * go ahead and allow the extension,
1001 * it's what we get if the callback is
1002 * unhandled
1003 */
1004
1005 if (n) {
1006 ext++;
1007 continue;
1008 }
1009
1010 /* apply it */
1011
1012 if (ext_count)
1013 *p++ = ',';
1014 p += sprintf(p, "%s", ext->name);
1015 ext_count++;
1016
1017 ext++;
1018 }
1019
1020 p += sprintf(p, "\x0d\x0a");
1021
1022 if (wsi->ietf_spec_revision)
1023 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
1024 wsi->ietf_spec_revision);
1025
1026 /* give userland a chance to append, eg, cookies */
1027
1028 context->protocols[0].callback(context, wsi,
1029 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
1030 NULL, &p, (pkt + sizeof(pkt)) - p - 12);
1031
1032 p += sprintf(p, "\x0d\x0a");
1033
1034 /* prepare the expected server accept response */
1035
1036 strcpy((char *)buf, wsi->key_b64);
1037 strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
1038
1039 SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
1040
1041 lws_b64_encode_string(hash, 20,
1042 wsi->initial_handshake_hash_base64,
1043 sizeof wsi->initial_handshake_hash_base64);
1044
1045issue_hdr:
1046
Andy Green6ee372f2012-04-09 15:09:01 +08001047#if 0
1048 puts(pkt);
1049#endif
Andy Green09226502011-05-28 10:19:19 +01001050
Andy Greena41314f2011-05-23 10:00:03 +01001051 /* done with these now */
1052
1053 free(wsi->c_path);
1054 free(wsi->c_host);
1055 if (wsi->c_origin)
1056 free(wsi->c_origin);
1057
1058 return p;
1059}
1060
1061int
1062lws_client_interpret_server_handshake(struct libwebsocket_context *context,
1063 struct libwebsocket *wsi)
1064{
Andy Green6ee372f2012-04-09 15:09:01 +08001065 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
1066 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
Andy Greena41314f2011-05-23 10:00:03 +01001067 char pkt[1024];
1068 char *p = &pkt[0];
1069 const char *pc;
1070 const char *c;
1071 int more = 1;
1072 int okay = 0;
1073 char ext_name[128];
1074 struct libwebsocket_extension *ext;
1075 void *v;
Andy Greenc15cb382011-06-26 10:27:28 +01001076 int len = 0;
Andy Greena41314f2011-05-23 10:00:03 +01001077 int n;
1078 static const char magic_websocket_04_masking_guid[] =
1079 "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
1080
1081 /*
1082 * 00 / 76 -->
1083 *
1084 * HTTP/1.1 101 WebSocket Protocol Handshake
1085 * Upgrade: WebSocket
1086 * Connection: Upgrade
1087 * Sec-WebSocket-Origin: http://127.0.0.1
1088 * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket
1089 *
1090 * xxxxxxxxxxxxxxxx
1091 */
1092
1093 if (wsi->ietf_spec_revision == 0) {
1094 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
1095 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
1096 !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
1097 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
1098 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
1099 wsi->c_protocol != NULL)) {
Andy Greencc012472011-11-07 19:53:23 +08001100 debug("libwebsocket_client_handshake "
Andy Greena41314f2011-05-23 10:00:03 +01001101 "missing required header(s)\n");
1102 pkt[len] = '\0';
Andy Greencc012472011-11-07 19:53:23 +08001103 debug("%s", pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001104 goto bail3;
1105 }
1106
1107 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
Andy Green6ee372f2012-04-09 15:09:01 +08001108 if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
Andy Greena41314f2011-05-23 10:00:03 +01001109 fprintf(stderr, "libwebsocket_client_handshake "
1110 "server sent bad HTTP response '%s'\n",
1111 wsi->utf8_token[WSI_TOKEN_HTTP].token);
1112 goto bail3;
1113 }
1114
Andy Green6ee372f2012-04-09 15:09:01 +08001115 if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) {
Andy Greena41314f2011-05-23 10:00:03 +01001116 fprintf(stderr, "libwebsocket_client_handshake "
1117 "challenge reply too short %d\n",
1118 wsi->utf8_token[
1119 WSI_TOKEN_CHALLENGE].token_len);
1120 pkt[len] = '\0';
Andy Greencc012472011-11-07 19:53:23 +08001121 debug("%s", pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001122 goto bail3;
1123
1124 }
1125
1126 goto select_protocol;
1127 }
1128
1129 /*
1130 * well, what the server sent looked reasonable for syntax.
1131 * Now let's confirm it sent all the necessary headers
1132 */
1133#if 0
Andy Green6ee372f2012-04-09 15:09:01 +08001134 fprintf(stderr, "WSI_TOKEN_HTTP: %d\n",
1135 wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
1136 fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n",
1137 wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
1138 fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n",
1139 wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
1140 fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n",
1141 wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
1142 fprintf(stderr, "WSI_TOKEN_NONCE: %d\n",
1143 wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
1144 fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n",
1145 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
Andy Greena41314f2011-05-23 10:00:03 +01001146#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001147 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
1148 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
1149 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
1150 !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
1151 (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
Andy Greena41314f2011-05-23 10:00:03 +01001152 wsi->ietf_spec_revision == 4) ||
Andy Green6ee372f2012-04-09 15:09:01 +08001153 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
1154 wsi->c_protocol != NULL)) {
1155 debug("libwebsocket_client_handshake "
Andy Greena41314f2011-05-23 10:00:03 +01001156 "missing required header(s)\n");
1157 pkt[len] = '\0';
Andy Greencc012472011-11-07 19:53:23 +08001158 debug("%s", pkt);
Andy Greena41314f2011-05-23 10:00:03 +01001159 goto bail3;
1160 }
1161
1162 /*
1163 * Everything seems to be there, now take a closer look at what
1164 * is in each header
1165 */
1166
1167 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
Artem Egorkined515ddd2011-11-23 10:46:24 +02001168 if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
Andy Greena41314f2011-05-23 10:00:03 +01001169 fprintf(stderr, "libwebsocket_client_handshake "
1170 "server sent bad HTTP response '%s'\n",
1171 wsi->utf8_token[WSI_TOKEN_HTTP].token);
1172 goto bail3;
1173 }
1174
1175 strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
1176 if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
1177 "websocket")) {
1178 fprintf(stderr, "libwebsocket_client_handshake server "
1179 "sent bad Upgrade header '%s'\n",
1180 wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
1181 goto bail3;
1182 }
1183
1184 strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
1185 if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
1186 "upgrade")) {
1187 fprintf(stderr, "libwebsocket_client_handshake server "
1188 "sent bad Connection hdr '%s'\n",
1189 wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
1190 goto bail3;
1191 }
1192
1193select_protocol:
1194 pc = wsi->c_protocol;
1195 if (pc == NULL)
Andy Green6ee372f2012-04-09 15:09:01 +08001196 fprintf(stderr, "lws_client_interpret_server_handshake: "
1197 "NULL c_protocol\n");
Andy Greena41314f2011-05-23 10:00:03 +01001198 else
Andy Green6ee372f2012-04-09 15:09:01 +08001199 debug("lws_client_interpret_server_handshake: "
1200 "cPprotocol='%s'\n", pc);
Andy Greena41314f2011-05-23 10:00:03 +01001201
1202 /*
1203 * confirm the protocol the server wants to talk was in the list
1204 * of protocols we offered
1205 */
1206
1207 if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
1208
Andy Green6ee372f2012-04-09 15:09:01 +08001209 fprintf(stderr, "lws_client_interpret_server_handshake "
1210 "WSI_TOKEN_PROTOCOL is null\n");
Andy Greena41314f2011-05-23 10:00:03 +01001211 /*
1212 * no protocol name to work from,
1213 * default to first protocol
1214 */
1215 wsi->protocol = &context->protocols[0];
David Brooks2c60d952012-04-20 12:19:01 +08001216 wsi->c_callback = wsi->protocol->callback;
Andy Greena41314f2011-05-23 10:00:03 +01001217 free(wsi->c_protocol);
1218
David Galeano4c38f142013-01-09 19:49:50 +08001219 goto check_extensions;
Andy Greena41314f2011-05-23 10:00:03 +01001220 }
1221
1222 while (*pc && !okay) {
Andy Green6ee372f2012-04-09 15:09:01 +08001223 if ((!strncmp(pc, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
1224 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
1225 (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
1226 pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
Andy Greena41314f2011-05-23 10:00:03 +01001227 okay = 1;
1228 continue;
1229 }
1230 while (*pc && *pc != ',')
1231 pc++;
1232 while (*pc && *pc != ' ')
1233 pc++;
1234 }
1235
1236 /* done with him now */
1237
1238 if (wsi->c_protocol)
1239 free(wsi->c_protocol);
1240
Andy Greena41314f2011-05-23 10:00:03 +01001241 if (!okay) {
1242 fprintf(stderr, "libwebsocket_client_handshake server "
1243 "sent bad protocol '%s'\n",
1244 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
1245 goto bail2;
1246 }
1247
1248 /*
1249 * identify the selected protocol struct and set it
1250 */
1251 n = 0;
1252 wsi->protocol = NULL;
David Brooks2c60d952012-04-20 12:19:01 +08001253 while (context->protocols[n].callback && !wsi->protocol) { /* Stop after finding first one?? */
Andy Greena41314f2011-05-23 10:00:03 +01001254 if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
David Brooks2c60d952012-04-20 12:19:01 +08001255 context->protocols[n].name) == 0) {
Andy Greena41314f2011-05-23 10:00:03 +01001256 wsi->protocol = &context->protocols[n];
David Brooks2c60d952012-04-20 12:19:01 +08001257 wsi->c_callback = wsi->protocol->callback;
1258 }
Andy Greena41314f2011-05-23 10:00:03 +01001259 n++;
1260 }
1261
1262 if (wsi->protocol == NULL) {
1263 fprintf(stderr, "libwebsocket_client_handshake server "
1264 "requested protocol '%s', which we "
1265 "said we supported but we don't!\n",
1266 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
1267 goto bail2;
1268 }
1269
1270
David Galeano4c38f142013-01-09 19:49:50 +08001271check_extensions:
1272
Andy Greena41314f2011-05-23 10:00:03 +01001273 /* instantiate the accepted extensions */
1274
1275 if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
Andy Green6ee372f2012-04-09 15:09:01 +08001276 debug("no client extenstions allowed by server\n");
Andy Greena41314f2011-05-23 10:00:03 +01001277 goto check_accept;
1278 }
1279
1280 /*
1281 * break down the list of server accepted extensions
1282 * and go through matching them or identifying bogons
1283 */
1284
1285 c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
1286 n = 0;
1287 while (more) {
1288
1289 if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
1290 ext_name[n] = *c++;
1291 if (n < sizeof(ext_name) - 1)
1292 n++;
1293 continue;
1294 }
1295 ext_name[n] = '\0';
1296 if (!*c)
1297 more = 0;
1298 else {
1299 c++;
1300 if (!n)
1301 continue;
1302 }
1303
1304 /* check we actually support it */
1305
Andy Greencc012472011-11-07 19:53:23 +08001306 debug("checking client ext %s\n", ext_name);
Andy Greena41314f2011-05-23 10:00:03 +01001307
1308 n = 0;
1309 ext = wsi->protocol->owning_server->extensions;
1310 while (ext && ext->callback) {
1311
1312 if (strcmp(ext_name, ext->name)) {
1313 ext++;
1314 continue;
1315 }
1316
1317 n = 1;
1318
Andy Greencc012472011-11-07 19:53:23 +08001319 debug("instantiating client ext %s\n", ext_name);
Andy Greena41314f2011-05-23 10:00:03 +01001320
1321 /* instantiate the extension on this conn */
1322
1323 wsi->active_extensions_user[
1324 wsi->count_active_extensions] =
1325 malloc(ext->per_session_data_size);
Andy Greenf6652412011-05-25 20:46:18 +01001326 memset(wsi->active_extensions_user[
1327 wsi->count_active_extensions], 0,
1328 ext->per_session_data_size);
Andy Greena41314f2011-05-23 10:00:03 +01001329 wsi->active_extensions[
1330 wsi->count_active_extensions] = ext;
1331
1332 /* allow him to construct his context */
1333
1334 ext->callback(wsi->protocol->owning_server,
1335 ext, wsi,
1336 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
1337 wsi->active_extensions_user[
1338 wsi->count_active_extensions],
1339 NULL, 0);
1340
1341 wsi->count_active_extensions++;
1342
1343 ext++;
1344 }
1345
1346 if (n == 0) {
1347 fprintf(stderr, "Server said we should use"
1348 "an unknown extension '%s'!\n", ext_name);
1349 goto bail2;
1350 }
1351
1352 n = 0;
1353 }
1354
1355
1356check_accept:
1357
1358 if (wsi->ietf_spec_revision == 0) {
1359
1360 if (memcmp(wsi->initial_handshake_hash_base64,
1361 wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
1362 fprintf(stderr, "libwebsocket_client_handshake "
1363 "failed 00 challenge compare\n");
1364 pkt[len] = '\0';
1365 fprintf(stderr, "%s", pkt);
1366 goto bail2;
1367 }
1368
1369 goto accept_ok;
1370 }
1371
1372 /*
1373 * Confirm his accept token is the one we precomputed
1374 */
1375
1376 if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
1377 wsi->initial_handshake_hash_base64)) {
1378 fprintf(stderr, "libwebsocket_client_handshake server "
1379 "sent bad ACCEPT '%s' vs computed '%s'\n",
1380 wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
1381 wsi->initial_handshake_hash_base64);
1382 goto bail2;
1383 }
1384
1385 if (wsi->ietf_spec_revision == 4) {
1386 /*
1387 * Calculate the 04 masking key to use when
1388 * sending data to server
1389 */
1390
1391 strcpy((char *)buf, wsi->key_b64);
1392 p = (char *)buf + strlen(wsi->key_b64);
1393 strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
1394 p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
1395 strcpy(p, magic_websocket_04_masking_guid);
1396 SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
1397 }
Andy Green6ee372f2012-04-09 15:09:01 +08001398accept_ok:
Andy Greena41314f2011-05-23 10:00:03 +01001399
1400 /* allocate the per-connection user memory (if any) */
Andy Green6ee372f2012-04-09 15:09:01 +08001401 if (wsi->protocol->per_session_data_size &&
1402 !libwebsocket_ensure_user_space(wsi))
1403 goto bail2;
Andy Greena41314f2011-05-23 10:00:03 +01001404
1405 /* clear his proxy connection timeout */
1406
1407 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1408
1409 /* mark him as being alive */
1410
1411 wsi->state = WSI_STATE_ESTABLISHED;
1412 wsi->mode = LWS_CONNMODE_WS_CLIENT;
1413
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001414 _debug("handshake OK for protocol %s\n", wsi->protocol->name);
Andy Greena41314f2011-05-23 10:00:03 +01001415
1416 /* call him back to inform him he is up */
1417
1418 wsi->protocol->callback(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001419 LWS_CALLBACK_CLIENT_ESTABLISHED,
1420 wsi->user_space, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001421
1422 /*
1423 * inform all extensions, not just active ones since they
1424 * already know
1425 */
1426
1427 ext = context->extensions;
1428
1429 while (ext && ext->callback) {
1430 v = NULL;
1431 for (n = 0; n < wsi->count_active_extensions; n++)
1432 if (wsi->active_extensions[n] == ext)
1433 v = wsi->active_extensions_user[n];
1434
1435 ext->callback(context, ext, wsi,
1436 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
1437 ext++;
1438 }
1439
1440 return 0;
1441
1442bail3:
1443 if (wsi->c_protocol)
1444 free(wsi->c_protocol);
1445
1446bail2:
David Brooks80a44972012-04-20 12:18:47 +08001447 if (wsi->c_callback) wsi->c_callback(context, wsi,
1448 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
1449 wsi->user_space,
1450 NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001451 libwebsocket_close_and_free_session(context, wsi,
David Brooks80a44972012-04-20 12:18:47 +08001452 LWS_CLOSE_STATUS_NOSTATUS); // But this should be LWS_CLOSE_STATUS_PROTOCOL_ERR
1453
Andy Greena41314f2011-05-23 10:00:03 +01001454 return 1;
1455}
1456
1457
1458
Andy Green9f990342011-02-12 11:57:45 +00001459/**
1460 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +00001461 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +00001462 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +08001463 * happened.
Andy Green9f990342011-02-12 11:57:45 +00001464 *
1465 * This function closes any active connections and then frees the
1466 * context. After calling this, any further use of the context is
1467 * undefined.
1468 */
1469
1470int
Peter Hinz56885f32011-03-02 22:03:47 +00001471libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +00001472 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +00001473{
Andy Green6ee372f2012-04-09 15:09:01 +08001474 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
1475 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
Andy Greena71eafc2011-02-14 17:59:43 +00001476 struct libwebsocket *wsi;
Andy Green0d338332011-02-12 11:57:43 +00001477 struct libwebsocket *new_wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00001478 int n;
Andy Green0d338332011-02-12 11:57:43 +00001479 int m;
Tobias Maiere8c9b562012-04-05 11:57:12 +02001480 ssize_t len;
Andy Green0d338332011-02-12 11:57:43 +00001481 int accept_fd;
1482 unsigned int clilen;
1483 struct sockaddr_in cli_addr;
Andy Greena71eafc2011-02-14 17:59:43 +00001484 struct timeval tv;
Andy Greenbe93fef2011-02-14 20:25:43 +00001485 char pkt[1024];
1486 char *p = &pkt[0];
Andy Green2366b1c2011-03-06 13:15:31 +00001487 int more = 1;
Andy Green98a717c2011-03-06 13:14:15 +00001488 struct lws_tokens eff_buf;
Andy Green6c939552011-03-08 08:56:57 +00001489 int opt = 1;
Yonathan Yusim3ae39ff2012-04-09 06:42:39 +08001490 char c;
Andy Greenc6517fa2011-03-06 13:15:29 +00001491
Andy Greenbe93fef2011-02-14 20:25:43 +00001492#ifdef LWS_OPENSSL_SUPPORT
1493 char ssl_err_buf[512];
1494#endif
Andy Greena71eafc2011-02-14 17:59:43 +00001495 /*
1496 * you can call us with pollfd = NULL to just allow the once-per-second
1497 * global timeout checks; if less than a second since the last check
1498 * it returns immediately then.
1499 */
1500
1501 gettimeofday(&tv, NULL);
1502
Peter Hinz56885f32011-03-02 22:03:47 +00001503 if (context->last_timeout_check_s != tv.tv_sec) {
1504 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +00001505
1506 /* global timeout check once per second */
1507
Peter Hinz56885f32011-03-02 22:03:47 +00001508 for (n = 0; n < context->fds_count; n++) {
1509 wsi = wsi_from_fd(context, context->fds[n].fd);
Andy Greena71eafc2011-02-14 17:59:43 +00001510
Andy Greena41314f2011-05-23 10:00:03 +01001511 libwebsocket_service_timeout_check(context, wsi,
1512 tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +00001513 }
1514 }
1515
1516 /* just here for timeout management? */
1517
1518 if (pollfd == NULL)
1519 return 0;
1520
1521 /* no, here to service a socket descriptor */
1522
Peter Hinz56885f32011-03-02 22:03:47 +00001523 wsi = wsi_from_fd(context, pollfd->fd);
Andy Greenb45993c2010-12-18 15:13:50 +00001524
Andy Green0d338332011-02-12 11:57:43 +00001525 if (wsi == NULL)
Andy Greenfa3f4052012-10-07 20:40:35 +08001526 return 0;
Andy Green8f037e42010-12-19 22:13:26 +00001527
Andy Green0d338332011-02-12 11:57:43 +00001528 switch (wsi->mode) {
1529 case LWS_CONNMODE_SERVER_LISTENER:
1530
1531 /* pollin means a client has connected to us then */
1532
David Galeanob88e0962013-01-10 09:54:10 +08001533 if (!(pollfd->revents & POLLIN))
Andy Green0d338332011-02-12 11:57:43 +00001534 break;
1535
David Galeanof7009352011-09-26 12:09:54 +01001536 if (context->fds_count >= MAX_CLIENTS) {
1537 fprintf(stderr, "too busy to accept new client\n");
1538 break;
1539 }
1540
Andy Green0d338332011-02-12 11:57:43 +00001541 /* listen socket got an unencrypted connection... */
1542
1543 clilen = sizeof(cli_addr);
1544 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
1545 &clilen);
1546 if (accept_fd < 0) {
David Galeano7c8d9892013-01-10 10:42:45 +08001547 debug("ERROR on accept: %d\n", strerror(errno));
Andy Green3928f612012-07-20 12:58:38 +08001548 return -1;
Andy Green0d338332011-02-12 11:57:43 +00001549 }
1550
Andy Green6c939552011-03-08 08:56:57 +00001551 /* Disable Nagle */
1552 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001553 setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY,
1554 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001555
Andy Green07034092011-02-13 08:37:12 +00001556 /*
1557 * look at who we connected to and give user code a chance
1558 * to reject based on client IP. There's no protocol selected
1559 * yet so we issue this to protocols[0]
1560 */
1561
Peter Hinz56885f32011-03-02 22:03:47 +00001562 if ((context->protocols[0].callback)(context, wsi,
Andy Green07034092011-02-13 08:37:12 +00001563 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
Andy Green6ee372f2012-04-09 15:09:01 +08001564 (void *)(long)accept_fd, NULL, 0)) {
Andy Greencc012472011-11-07 19:53:23 +08001565 debug("Callback denied network connection\n");
Peter Hinz56885f32011-03-02 22:03:47 +00001566#ifdef WIN32
1567 closesocket(accept_fd);
1568#else
Andy Green07034092011-02-13 08:37:12 +00001569 close(accept_fd);
Peter Hinz56885f32011-03-02 22:03:47 +00001570#endif
Andy Green07034092011-02-13 08:37:12 +00001571 break;
1572 }
1573
Andy Green0d338332011-02-12 11:57:43 +00001574 /* accepting connection to main listener */
1575
Andy Greena41314f2011-05-23 10:00:03 +01001576 new_wsi = libwebsocket_create_new_server_wsi(context);
David Galeanoed3c8402013-01-10 10:45:24 +08001577 if (new_wsi == NULL) {
1578#ifdef WIN32
1579 closesocket(accept_fd);
1580#else
1581 close(accept_fd);
1582#endif
Andy Green0d338332011-02-12 11:57:43 +00001583 break;
David Galeanoed3c8402013-01-10 10:45:24 +08001584 }
Andy Green0d338332011-02-12 11:57:43 +00001585
Andy Green0d338332011-02-12 11:57:43 +00001586 new_wsi->sock = accept_fd;
Andy Greena41314f2011-05-23 10:00:03 +01001587
Andy Green0d338332011-02-12 11:57:43 +00001588
1589#ifdef LWS_OPENSSL_SUPPORT
1590 new_wsi->ssl = NULL;
Andy Green0d338332011-02-12 11:57:43 +00001591
Peter Hinz56885f32011-03-02 22:03:47 +00001592 if (context->use_ssl) {
Andy Green0d338332011-02-12 11:57:43 +00001593
Peter Hinz56885f32011-03-02 22:03:47 +00001594 new_wsi->ssl = SSL_new(context->ssl_ctx);
Andy Green0d338332011-02-12 11:57:43 +00001595 if (new_wsi->ssl == NULL) {
1596 fprintf(stderr, "SSL_new failed: %s\n",
1597 ERR_error_string(SSL_get_error(
1598 new_wsi->ssl, 0), NULL));
Andy Green1f9bf522011-02-14 21:14:37 +00001599 libwebsockets_decode_ssl_error();
Andy Green0d338332011-02-12 11:57:43 +00001600 free(new_wsi);
David Galeanoed3c8402013-01-10 10:45:24 +08001601#ifdef WIN32
1602 closesocket(accept_fd);
1603#else
1604 close(accept_fd);
1605#endif
Andy Green0d338332011-02-12 11:57:43 +00001606 break;
1607 }
1608
1609 SSL_set_fd(new_wsi->ssl, accept_fd);
1610
1611 n = SSL_accept(new_wsi->ssl);
1612 if (n != 1) {
1613 /*
1614 * browsers seem to probe with various
1615 * ssl params which fail then retry
1616 * and succeed
1617 */
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001618 _debug("SSL_accept failed skt %u: %s\n",
Andy Green0d338332011-02-12 11:57:43 +00001619 pollfd->fd,
1620 ERR_error_string(SSL_get_error(
1621 new_wsi->ssl, n), NULL));
1622 SSL_free(
1623 new_wsi->ssl);
1624 free(new_wsi);
David Galeanoed3c8402013-01-10 10:45:24 +08001625#ifdef WIN32
1626 closesocket(accept_fd);
1627#else
1628 close(accept_fd);
1629#endif
Andy Green0d338332011-02-12 11:57:43 +00001630 break;
1631 }
Andy Green6ee372f2012-04-09 15:09:01 +08001632
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001633 _debug("accepted new SSL conn "
Andy Green0d338332011-02-12 11:57:43 +00001634 "port %u on fd=%d SSL ver %s\n",
1635 ntohs(cli_addr.sin_port), accept_fd,
1636 SSL_get_version(new_wsi->ssl));
1637
1638 } else
1639#endif
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001640 _debug("accepted new conn port %u on fd=%d\n",
Andy Green0d338332011-02-12 11:57:43 +00001641 ntohs(cli_addr.sin_port), accept_fd);
1642
Peter Hinz56885f32011-03-02 22:03:47 +00001643 insert_wsi(context, new_wsi);
Andy Green0d338332011-02-12 11:57:43 +00001644
Andy Green0d338332011-02-12 11:57:43 +00001645 /*
1646 * make sure NO events are seen yet on this new socket
1647 * (otherwise we inherit old fds[client].revents from
1648 * previous socket there and die mysteriously! )
1649 */
Peter Hinz56885f32011-03-02 22:03:47 +00001650 context->fds[context->fds_count].revents = 0;
Andy Green0d338332011-02-12 11:57:43 +00001651
Peter Hinz56885f32011-03-02 22:03:47 +00001652 context->fds[context->fds_count].events = POLLIN;
1653 context->fds[context->fds_count++].fd = accept_fd;
Andy Green0d338332011-02-12 11:57:43 +00001654
Andy Green3221f922011-02-12 13:14:11 +00001655 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001656 context->protocols[0].callback(context, new_wsi,
Andy Green3221f922011-02-12 13:14:11 +00001657 LWS_CALLBACK_ADD_POLL_FD,
1658 (void *)(long)accept_fd, NULL, POLLIN);
1659
Andy Green0d338332011-02-12 11:57:43 +00001660 break;
1661
1662 case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
1663
1664 /* as we are listening, POLLIN means accept() is needed */
Andy Green6ee372f2012-04-09 15:09:01 +08001665
David Galeanob88e0962013-01-10 09:54:10 +08001666 if (!(pollfd->revents & POLLIN))
Andy Green0d338332011-02-12 11:57:43 +00001667 break;
1668
1669 /* listen socket got an unencrypted connection... */
1670
1671 clilen = sizeof(cli_addr);
1672 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
1673 &clilen);
1674 if (accept_fd < 0) {
Andy Green3928f612012-07-20 12:58:38 +08001675 debug("ERROR on accept\n");
1676 return -1;
Andy Green0d338332011-02-12 11:57:43 +00001677 }
1678
Peter Hinz56885f32011-03-02 22:03:47 +00001679 if (context->fds_count >= MAX_CLIENTS) {
Andy Green3221f922011-02-12 13:14:11 +00001680 fprintf(stderr, "too busy to accept new broadcast "
1681 "proxy client\n");
Peter Hinz56885f32011-03-02 22:03:47 +00001682#ifdef WIN32
1683 closesocket(accept_fd);
1684#else
Andy Green0d338332011-02-12 11:57:43 +00001685 close(accept_fd);
Peter Hinz56885f32011-03-02 22:03:47 +00001686#endif
Andy Green0d338332011-02-12 11:57:43 +00001687 break;
1688 }
1689
1690 /* create a dummy wsi for the connection and add it */
1691
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001692 new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
1693 memset(new_wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001694 new_wsi->sock = accept_fd;
1695 new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY;
1696 new_wsi->state = WSI_STATE_ESTABLISHED;
Andy Greend6e09112011-03-05 16:12:15 +00001697 new_wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00001698 /* note which protocol we are proxying */
1699 new_wsi->protocol_index_for_broadcast_proxy =
1700 wsi->protocol_index_for_broadcast_proxy;
Peter Hinz56885f32011-03-02 22:03:47 +00001701 insert_wsi(context, new_wsi);
Andy Green0d338332011-02-12 11:57:43 +00001702
1703 /* add connected socket to internal poll array */
1704
Peter Hinz56885f32011-03-02 22:03:47 +00001705 context->fds[context->fds_count].revents = 0;
1706 context->fds[context->fds_count].events = POLLIN;
1707 context->fds[context->fds_count++].fd = accept_fd;
Andy Green0d338332011-02-12 11:57:43 +00001708
Andy Green3221f922011-02-12 13:14:11 +00001709 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001710 context->protocols[0].callback(context, new_wsi,
Andy Green3221f922011-02-12 13:14:11 +00001711 LWS_CALLBACK_ADD_POLL_FD,
1712 (void *)(long)accept_fd, NULL, POLLIN);
1713
Andy Green0d338332011-02-12 11:57:43 +00001714 break;
1715
1716 case LWS_CONNMODE_BROADCAST_PROXY:
Andy Green8f037e42010-12-19 22:13:26 +00001717
Andy Greenb45993c2010-12-18 15:13:50 +00001718 /* handle session socket closed */
Andy Green8f037e42010-12-19 22:13:26 +00001719
Andy Green0d338332011-02-12 11:57:43 +00001720 if (pollfd->revents & (POLLERR | POLLHUP)) {
Andy Green8f037e42010-12-19 22:13:26 +00001721
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001722 _debug("Session Socket %p (fd=%d) dead\n",
Timothy J Fontaineb86d64e2011-02-14 17:55:27 +00001723 (void *)wsi, pollfd->fd);
Andy Greenb45993c2010-12-18 15:13:50 +00001724
Peter Hinz56885f32011-03-02 22:03:47 +00001725 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00001726 LWS_CLOSE_STATUS_NORMAL);
Andy Green4b6fbe12011-02-14 08:03:48 +00001727 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +00001728 }
Andy Green8f037e42010-12-19 22:13:26 +00001729
Andy Green3b84c002011-03-06 13:14:42 +00001730 /*
1731 * either extension code with stuff to spill, or the user code,
1732 * requested a callback when it was OK to write
1733 */
Andy Green90c7cbc2011-01-27 06:26:52 +00001734
Andy Green3b84c002011-03-06 13:14:42 +00001735 if (pollfd->revents & POLLOUT)
Andy Green6ee372f2012-04-09 15:09:01 +08001736 if (lws_handle_POLLOUT_event(context, wsi,
1737 pollfd) < 0) {
1738 libwebsocket_close_and_free_session(
1739 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green3b84c002011-03-06 13:14:42 +00001740 return 1;
1741 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001742
Andy Greenb45993c2010-12-18 15:13:50 +00001743 /* any incoming data ready? */
1744
Andy Green0d338332011-02-12 11:57:43 +00001745 if (!(pollfd->revents & POLLIN))
1746 break;
Andy Greenb45993c2010-12-18 15:13:50 +00001747
Andy Green0d338332011-02-12 11:57:43 +00001748 /* get the issued broadcast payload from the socket */
Andy Greenb45993c2010-12-18 15:13:50 +00001749
Andy Green0d338332011-02-12 11:57:43 +00001750 len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING,
1751 MAX_BROADCAST_PAYLOAD);
1752 if (len < 0) {
1753 fprintf(stderr, "Error reading broadcast payload\n");
Andy Green4b6fbe12011-02-14 08:03:48 +00001754 break;
Andy Green0d338332011-02-12 11:57:43 +00001755 }
Andy Greenb45993c2010-12-18 15:13:50 +00001756
Andy Green0d338332011-02-12 11:57:43 +00001757 /* broadcast it to all guys with this protocol index */
Andy Green8f037e42010-12-19 22:13:26 +00001758
Andy Green0d338332011-02-12 11:57:43 +00001759 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
Andy Green8f037e42010-12-19 22:13:26 +00001760
Peter Hinz56885f32011-03-02 22:03:47 +00001761 for (m = 0; m < context->fd_hashtable[n].length; m++) {
Andy Greenb45993c2010-12-18 15:13:50 +00001762
Peter Hinz56885f32011-03-02 22:03:47 +00001763 new_wsi = context->fd_hashtable[n].wsi[m];
Andy Greenb45993c2010-12-18 15:13:50 +00001764
Andy Green0d338332011-02-12 11:57:43 +00001765 /* only to clients we are serving to */
Andy Greenb45993c2010-12-18 15:13:50 +00001766
Andy Green0d338332011-02-12 11:57:43 +00001767 if (new_wsi->mode != LWS_CONNMODE_WS_SERVING)
Andy Greenb45993c2010-12-18 15:13:50 +00001768 continue;
1769
1770 /*
1771 * never broadcast to non-established
1772 * connection
1773 */
1774
Andy Green0d338332011-02-12 11:57:43 +00001775 if (new_wsi->state != WSI_STATE_ESTABLISHED)
Andy Green4739e5c2011-01-22 12:51:57 +00001776 continue;
1777
Andy Greenb45993c2010-12-18 15:13:50 +00001778 /*
1779 * only broadcast to connections using
1780 * the requested protocol
1781 */
1782
Andy Green0d338332011-02-12 11:57:43 +00001783 if (new_wsi->protocol->protocol_index !=
1784 wsi->protocol_index_for_broadcast_proxy)
Andy Greenb45993c2010-12-18 15:13:50 +00001785 continue;
1786
Andy Green8f037e42010-12-19 22:13:26 +00001787 /* broadcast it to this connection */
1788
Peter Hinz56885f32011-03-02 22:03:47 +00001789 new_wsi->protocol->callback(context, new_wsi,
Andy Green8f037e42010-12-19 22:13:26 +00001790 LWS_CALLBACK_BROADCAST,
Andy Green0d338332011-02-12 11:57:43 +00001791 new_wsi->user_space,
Andy Green0ca6a172010-12-19 20:50:01 +00001792 buf + LWS_SEND_BUFFER_PRE_PADDING, len);
Andy Greenb45993c2010-12-18 15:13:50 +00001793 }
Andy Green0d338332011-02-12 11:57:43 +00001794 }
1795 break;
Andy Greenb45993c2010-12-18 15:13:50 +00001796
Andy Greenbe93fef2011-02-14 20:25:43 +00001797 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
1798
1799 /* handle proxy hung up on us */
1800
1801 if (pollfd->revents & (POLLERR | POLLHUP)) {
1802
1803 fprintf(stderr, "Proxy connection %p (fd=%d) dead\n",
1804 (void *)wsi, pollfd->fd);
1805
Peter Hinz56885f32011-03-02 22:03:47 +00001806 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00001807 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +00001808 return 1;
1809 }
1810
Andy Green72c34322011-04-16 10:46:21 +01001811 n = recv(wsi->sock, pkt, sizeof pkt, 0);
Andy Greenbe93fef2011-02-14 20:25:43 +00001812 if (n < 0) {
Peter Hinz56885f32011-03-02 22:03:47 +00001813 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00001814 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +00001815 fprintf(stderr, "ERROR reading from proxy socket\n");
1816 return 1;
1817 }
1818
1819 pkt[13] = '\0';
1820 if (strcmp(pkt, "HTTP/1.0 200 ") != 0) {
Peter Hinz56885f32011-03-02 22:03:47 +00001821 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00001822 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +00001823 fprintf(stderr, "ERROR from proxy: %s\n", pkt);
1824 return 1;
1825 }
1826
1827 /* clear his proxy connection timeout */
1828
1829 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1830
1831 /* fallthru */
1832
1833 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
1834
1835 #ifdef LWS_OPENSSL_SUPPORT
Ken Atherton8360a472012-05-03 11:45:04 +08001836 if (wsi->use_ssl && !wsi->ssl) {
Andy Greenbe93fef2011-02-14 20:25:43 +00001837
Peter Hinz56885f32011-03-02 22:03:47 +00001838 wsi->ssl = SSL_new(context->ssl_client_ctx);
1839 wsi->client_bio = BIO_new_socket(wsi->sock,
1840 BIO_NOCLOSE);
Andy Greenbe93fef2011-02-14 20:25:43 +00001841 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
1842
Andy Green6901cb32011-02-21 08:06:47 +00001843 SSL_set_ex_data(wsi->ssl,
Andy Green2e24da02011-03-05 16:12:04 +00001844 openssl_websocket_private_data_index,
Peter Hinz56885f32011-03-02 22:03:47 +00001845 context);
Ken Atherton8360a472012-05-03 11:45:04 +08001846 }
Andy Green6901cb32011-02-21 08:06:47 +00001847
Ken Atherton8360a472012-05-03 11:45:04 +08001848 if (wsi->use_ssl) {
Andy Greenbe93fef2011-02-14 20:25:43 +00001849 if (SSL_connect(wsi->ssl) <= 0) {
Ken Atherton8360a472012-05-03 11:45:04 +08001850
1851 /*
1852 * retry if new data comes until we
1853 * run into the connection timeout or win
1854 */
1855
Andy Greenbe93fef2011-02-14 20:25:43 +00001856 fprintf(stderr, "SSL connect error %s\n",
Andy Green687b0182011-02-26 11:04:01 +00001857 ERR_error_string(ERR_get_error(),
1858 ssl_err_buf));
Ken Atherton8360a472012-05-03 11:45:04 +08001859 return 0;
Andy Greenbe93fef2011-02-14 20:25:43 +00001860 }
1861
1862 n = SSL_get_verify_result(wsi->ssl);
Andy Green2e24da02011-03-05 16:12:04 +00001863 if ((n != X509_V_OK) && (
Andy Green687b0182011-02-26 11:04:01 +00001864 n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
1865 wsi->use_ssl != 2)) {
Andy Greenbe93fef2011-02-14 20:25:43 +00001866
Andy Green687b0182011-02-26 11:04:01 +00001867 fprintf(stderr, "server's cert didn't "
1868 "look good %d\n", n);
Peter Hinz56885f32011-03-02 22:03:47 +00001869 libwebsocket_close_and_free_session(context,
1870 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green687b0182011-02-26 11:04:01 +00001871 return 1;
Andy Greenbe93fef2011-02-14 20:25:43 +00001872 }
Ken Atherton8360a472012-05-03 11:45:04 +08001873 } else
Andy Greenbe93fef2011-02-14 20:25:43 +00001874 wsi->ssl = NULL;
1875 #endif
1876
Andy Greena41314f2011-05-23 10:00:03 +01001877 p = libwebsockets_generate_client_handshake(context, wsi, p);
Andy Green6ee372f2012-04-09 15:09:01 +08001878 if (p == NULL)
Andy Greenbe93fef2011-02-14 20:25:43 +00001879 return 1;
Andy Greeneeaacb32011-03-01 20:44:24 +00001880
Andy Greenbe93fef2011-02-14 20:25:43 +00001881 /* send our request to the server */
1882
1883 #ifdef LWS_OPENSSL_SUPPORT
1884 if (wsi->use_ssl)
1885 n = SSL_write(wsi->ssl, pkt, p - pkt);
1886 else
1887 #endif
1888 n = send(wsi->sock, pkt, p - pkt, 0);
1889
1890 if (n < 0) {
1891 fprintf(stderr, "ERROR writing to client socket\n");
Peter Hinz56885f32011-03-02 22:03:47 +00001892 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00001893 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +00001894 return 1;
1895 }
1896
1897 wsi->parser_state = WSI_TOKEN_NAME_PART;
1898 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
1899 libwebsocket_set_timeout(wsi,
David Galeanoc9f1ff82013-01-09 18:01:23 +08001900 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, AWAITING_TIMEOUT);
Andy Greenbe93fef2011-02-14 20:25:43 +00001901
1902 break;
1903
1904 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
1905
1906 /* handle server hung up on us */
1907
1908 if (pollfd->revents & (POLLERR | POLLHUP)) {
1909
1910 fprintf(stderr, "Server connection %p (fd=%d) dead\n",
1911 (void *)wsi, pollfd->fd);
1912
1913 goto bail3;
1914 }
1915
1916
1917 /* interpret the server response */
1918
1919 /*
1920 * HTTP/1.1 101 Switching Protocols
1921 * Upgrade: websocket
1922 * Connection: Upgrade
1923 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
1924 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
1925 * Sec-WebSocket-Protocol: chat
1926 */
1927
Yonathan Yusim3ae39ff2012-04-09 06:42:39 +08001928 /*
1929 * we have to take some care here to only take from the
1930 * socket bytewise. The browser may (and has been seen to
1931 * in the case that onopen() performs websocket traffic)
1932 * coalesce both handshake response and websocket traffic
1933 * in one packet, since at that point the connection is
1934 * definitively ready from browser pov.
1935 */
Andy Greenbe93fef2011-02-14 20:25:43 +00001936
Andy Green7b5af9a2012-04-09 15:23:47 +08001937 len = 1;
Yonathan Yusim3ae39ff2012-04-09 06:42:39 +08001938 while (wsi->parser_state != WSI_PARSING_COMPLETE && len > 0) {
1939#ifdef LWS_OPENSSL_SUPPORT
1940 if (wsi->use_ssl)
1941 len = SSL_read(wsi->ssl, &c, 1);
1942 else
1943#endif
1944 len = recv(wsi->sock, &c, 1, 0);
1945
1946 libwebsocket_parse(wsi, c);
Andy Greenbe93fef2011-02-14 20:25:43 +00001947 }
1948
Andy Green27a0b912011-04-16 10:54:28 +01001949 /*
Andy Green6ee372f2012-04-09 15:09:01 +08001950 * hs may also be coming in multiple packets, there is a 5-sec
Andy Green27a0b912011-04-16 10:54:28 +01001951 * libwebsocket timeout still active here too, so if parsing did
1952 * not complete just wait for next packet coming in this state
1953 */
1954
1955 if (wsi->parser_state != WSI_PARSING_COMPLETE)
1956 break;
Andy Greenbe93fef2011-02-14 20:25:43 +00001957
Yonathan Yusim3ae39ff2012-04-09 06:42:39 +08001958 /*
1959 * otherwise deal with the handshake. If there's any
1960 * packet traffic already arrived we'll trigger poll() again
1961 * right away and deal with it that way
1962 */
1963
Andy Greena41314f2011-05-23 10:00:03 +01001964 return lws_client_interpret_server_handshake(context, wsi);
Andy Greenbe93fef2011-02-14 20:25:43 +00001965
1966bail3:
1967 if (wsi->c_protocol)
1968 free(wsi->c_protocol);
Peter Hinz56885f32011-03-02 22:03:47 +00001969 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001970 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +00001971 return 1;
Andy Greena41314f2011-05-23 10:00:03 +01001972
1973 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
Andy Green6ee372f2012-04-09 15:09:01 +08001974 fprintf(stderr,
1975 "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
Andy Greena41314f2011-05-23 10:00:03 +01001976 break;
1977
1978 case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
Andy Green6ee372f2012-04-09 15:09:01 +08001979 fprintf(stderr,
1980 "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
Andy Greena41314f2011-05-23 10:00:03 +01001981 break;
1982
Andy Greenbe93fef2011-02-14 20:25:43 +00001983
Andy Green0d338332011-02-12 11:57:43 +00001984 case LWS_CONNMODE_WS_SERVING:
1985 case LWS_CONNMODE_WS_CLIENT:
1986
1987 /* handle session socket closed */
1988
1989 if (pollfd->revents & (POLLERR | POLLHUP)) {
1990
Andy Green62c54d22011-02-14 09:14:25 +00001991 fprintf(stderr, "Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +00001992 (void *)wsi, pollfd->fd);
1993
Peter Hinz56885f32011-03-02 22:03:47 +00001994 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00001995 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green4b6fbe12011-02-14 08:03:48 +00001996 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +00001997 }
1998
Andy Green0d338332011-02-12 11:57:43 +00001999 /* the guy requested a callback when it was OK to write */
2000
Andy Greenda527df2011-03-07 07:08:12 +00002001 if ((pollfd->revents & POLLOUT) &&
2002 wsi->state == WSI_STATE_ESTABLISHED)
2003 if (lws_handle_POLLOUT_event(context, wsi,
2004 pollfd) < 0) {
2005 libwebsocket_close_and_free_session(
2006 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green3b84c002011-03-06 13:14:42 +00002007 return 1;
2008 }
Andy Green0d338332011-02-12 11:57:43 +00002009
Andy Green0d338332011-02-12 11:57:43 +00002010
2011 /* any incoming data ready? */
2012
2013 if (!(pollfd->revents & POLLIN))
2014 break;
2015
Andy Greenb45993c2010-12-18 15:13:50 +00002016#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08002017read_pending:
Andy Green0d338332011-02-12 11:57:43 +00002018 if (wsi->ssl)
Andy Green98a717c2011-03-06 13:14:15 +00002019 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Greenb45993c2010-12-18 15:13:50 +00002020 else
2021#endif
Andy Green98a717c2011-03-06 13:14:15 +00002022 eff_buf.token_len =
Andy Green72c34322011-04-16 10:46:21 +01002023 recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002024
Andy Green98a717c2011-03-06 13:14:15 +00002025 if (eff_buf.token_len < 0) {
2026 fprintf(stderr, "Socket read returned %d\n",
2027 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +02002028 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +08002029 libwebsocket_close_and_free_session(context,
2030 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Nick Dowellc04c1932012-04-05 10:29:39 +08002031 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +00002032 }
Andy Green98a717c2011-03-06 13:14:15 +00002033 if (!eff_buf.token_len) {
Peter Hinz56885f32011-03-02 22:03:47 +00002034 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08002035 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +08002036 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00002037 }
2038
Andy Green98a717c2011-03-06 13:14:15 +00002039 /*
2040 * give any active extensions a chance to munge the buffer
2041 * before parse. We pass in a pointer to an lws_tokens struct
2042 * prepared with the default buffer and content length that's in
2043 * there. Rather than rewrite the default buffer, extensions
2044 * that expect to grow the buffer can adapt .token to
2045 * point to their own per-connection buffer in the extension
2046 * user allocation. By default with no extensions or no
2047 * extension callback handling, just the normal input buffer is
2048 * used then so it is efficient.
2049 */
Andy Greenb45993c2010-12-18 15:13:50 +00002050
Andy Green98a717c2011-03-06 13:14:15 +00002051 eff_buf.token = (char *)buf;
Andy Greenb45993c2010-12-18 15:13:50 +00002052
Andy Green98a717c2011-03-06 13:14:15 +00002053 more = 1;
2054 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00002055
Andy Green98a717c2011-03-06 13:14:15 +00002056 more = 0;
2057
2058 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00002059 m = wsi->active_extensions[n]->callback(context,
2060 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00002061 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00002062 wsi->active_extensions_user[n],
2063 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00002064 if (m < 0) {
Andy Green6ee372f2012-04-09 15:09:01 +08002065 fprintf(stderr,
2066 "Extension reports fatal error\n");
2067 libwebsocket_close_and_free_session(
2068 context, wsi,
2069 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green98a717c2011-03-06 13:14:15 +00002070 return 1;
2071 }
2072 if (m)
2073 more = 1;
2074 }
2075
2076 /* service incoming data */
2077
2078 if (eff_buf.token_len) {
2079 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08002080 (unsigned char *)eff_buf.token,
2081 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00002082 if (n < 0)
2083 /* we closed wsi */
2084 return 1;
2085 }
2086
2087 eff_buf.token = NULL;
2088 eff_buf.token_len = 0;
2089 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08002090
2091#ifdef LWS_OPENSSL_SUPPORT
2092 if (wsi->ssl && SSL_pending(wsi->ssl))
2093 goto read_pending;
2094#endif
Andy Green98a717c2011-03-06 13:14:15 +00002095 break;
Andy Greenb45993c2010-12-18 15:13:50 +00002096 }
2097
2098 return 0;
2099}
2100
Andy Green0d338332011-02-12 11:57:43 +00002101
Andy Green6964bb52011-01-23 16:50:33 +00002102/**
2103 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00002104 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00002105 *
2106 * This function closes any active connections and then frees the
2107 * context. After calling this, any further use of the context is
2108 * undefined.
2109 */
2110void
Peter Hinz56885f32011-03-02 22:03:47 +00002111libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00002112{
Andy Green0d338332011-02-12 11:57:43 +00002113 int n;
2114 int m;
2115 struct libwebsocket *wsi;
Andy Greena41314f2011-05-23 10:00:03 +01002116 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +00002117
Andy Green4b6fbe12011-02-14 08:03:48 +00002118 for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
Peter Hinz56885f32011-03-02 22:03:47 +00002119 for (m = 0; m < context->fd_hashtable[n].length; m++) {
2120 wsi = context->fd_hashtable[n].wsi[m];
2121 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +00002122 LWS_CLOSE_STATUS_GOINGAWAY);
Andy Greenf3d3b402011-02-09 07:16:34 +00002123 }
Andy Green6964bb52011-01-23 16:50:33 +00002124
Andy Greena41314f2011-05-23 10:00:03 +01002125 /*
2126 * give all extensions a chance to clean up any per-context
2127 * allocations they might have made
2128 */
2129
2130 ext = context->extensions;
2131 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
2132 if (context->listen_port)
2133 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08002134 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002135 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01002136 ext++;
2137 }
2138
Peter Hinz56885f32011-03-02 22:03:47 +00002139#ifdef WIN32
2140#else
2141 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00002142#endif
2143
Peter Hinz56885f32011-03-02 22:03:47 +00002144#ifdef LWS_OPENSSL_SUPPORT
2145 if (context->ssl_ctx)
2146 SSL_CTX_free(context->ssl_ctx);
2147 if (context->ssl_client_ctx)
2148 SSL_CTX_free(context->ssl_client_ctx);
2149#endif
2150
2151 free(context);
2152
2153#ifdef WIN32
2154 WSACleanup();
2155#endif
Andy Green6964bb52011-01-23 16:50:33 +00002156}
2157
Alon Levy0291eb32012-10-19 11:21:56 +02002158LWS_EXTERN void *
2159libwebsocket_context_user(struct libwebsocket_context *context)
2160{
2161 return context->user_space;
2162}
2163
Andy Green6964bb52011-01-23 16:50:33 +00002164/**
2165 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00002166 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00002167 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
2168 * service otherwise block and service immediately, returning
2169 * after the timeout if nothing needed service.
2170 *
2171 * This function deals with any pending websocket traffic, for three
2172 * kinds of event. It handles these events on both server and client
2173 * types of connection the same.
2174 *
2175 * 1) Accept new connections to our context's server
2176 *
2177 * 2) Perform pending broadcast writes initiated from other forked
2178 * processes (effectively serializing asynchronous broadcasts)
2179 *
2180 * 3) Call the receive callback for incoming frame data received by
2181 * server or client connections.
2182 *
2183 * You need to call this service function periodically to all the above
2184 * functions to happen; if your application is single-threaded you can
2185 * just call it in your main event loop.
2186 *
2187 * Alternatively you can fork a new process that asynchronously handles
2188 * calling this service in a loop. In that case you are happy if this
2189 * call blocks your thread until it needs to take care of something and
2190 * would call it with a large nonzero timeout. Your loop then takes no
2191 * CPU while there is nothing happening.
2192 *
2193 * If you are calling it in a single-threaded app, you don't want it to
2194 * wait around blocking other things in your loop from happening, so you
2195 * would call it with a timeout_ms of 0, so it returns immediately if
2196 * nothing is pending, or as soon as it services whatever was pending.
2197 */
2198
Andy Greenb45993c2010-12-18 15:13:50 +00002199
Andy Greene92cd172011-01-19 13:11:55 +00002200int
Peter Hinz56885f32011-03-02 22:03:47 +00002201libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00002202{
2203 int n;
Andy Greene92cd172011-01-19 13:11:55 +00002204
2205 /* stay dead once we are dead */
2206
Peter Hinz56885f32011-03-02 22:03:47 +00002207 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00002208 return 1;
2209
Andy Green0d338332011-02-12 11:57:43 +00002210 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00002211
Peter Hinz56885f32011-03-02 22:03:47 +00002212 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00002213 if (n == 0) /* poll timeout */
2214 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00002215
Andy Green62c54d22011-02-14 09:14:25 +00002216 if (n < 0) {
Andy Green5e1fa172011-02-10 09:07:05 +00002217 /*
Andy Greene92cd172011-01-19 13:11:55 +00002218 fprintf(stderr, "Listen Socket dead\n");
Andy Green5e1fa172011-02-10 09:07:05 +00002219 */
Andy Green3928f612012-07-20 12:58:38 +08002220 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00002221 }
Andy Greene92cd172011-01-19 13:11:55 +00002222
2223 /* handle accept on listening socket? */
2224
Peter Hinz56885f32011-03-02 22:03:47 +00002225 for (n = 0; n < context->fds_count; n++)
2226 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08002227 if (libwebsocket_service_fd(context,
2228 &context->fds[n]) < 0)
2229 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00002230 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00002231}
2232
Andy Greena41314f2011-05-23 10:00:03 +01002233int
2234lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08002235 struct libwebsocket *wsi,
2236 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01002237 void *v, size_t len)
2238{
2239 int n;
2240 int handled = 0;
2241
2242 /* maybe an extension will take care of it for us */
2243
2244 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
2245 if (!wsi->active_extensions[n]->callback)
2246 continue;
2247
2248 handled |= wsi->active_extensions[n]->callback(context,
2249 wsi->active_extensions[n], wsi,
2250 r, wsi->active_extensions_user[n], v, len);
2251 }
2252
2253 return handled;
2254}
2255
2256
2257void *
2258lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08002259 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01002260{
2261 int n = 0;
2262
Andy Green68b45042011-05-25 21:41:57 +01002263 if (wsi == NULL)
2264 return NULL;
2265
Andy Greena41314f2011-05-23 10:00:03 +01002266 while (n < wsi->count_active_extensions) {
2267 if (wsi->active_extensions[n] != ext) {
2268 n++;
2269 continue;
2270 }
2271 return wsi->active_extensions_user[n];
2272 }
2273
2274 return NULL;
2275}
2276
Andy Green90c7cbc2011-01-27 06:26:52 +00002277/**
2278 * libwebsocket_callback_on_writable() - Request a callback when this socket
2279 * becomes able to be written to without
2280 * blocking
Andy Green32375b72011-02-19 08:32:53 +00002281 *
Peter Hinz56885f32011-03-02 22:03:47 +00002282 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00002283 * @wsi: Websocket connection instance to get callback for
2284 */
2285
2286int
Peter Hinz56885f32011-03-02 22:03:47 +00002287libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08002288 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00002289{
Andy Green90c7cbc2011-01-27 06:26:52 +00002290 int n;
Andy Greena41314f2011-05-23 10:00:03 +01002291 int handled = 0;
2292
2293 /* maybe an extension will take care of it for us */
2294
2295 for (n = 0; n < wsi->count_active_extensions; n++) {
2296 if (!wsi->active_extensions[n]->callback)
2297 continue;
2298
2299 handled |= wsi->active_extensions[n]->callback(context,
2300 wsi->active_extensions[n], wsi,
2301 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
2302 wsi->active_extensions_user[n], NULL, 0);
2303 }
2304
2305 if (handled)
2306 return 1;
Andy Green90c7cbc2011-01-27 06:26:52 +00002307
Peter Hinz56885f32011-03-02 22:03:47 +00002308 for (n = 0; n < context->fds_count; n++)
2309 if (context->fds[n].fd == wsi->sock) {
2310 context->fds[n].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01002311 n = context->fds_count + 1;
Andy Green90c7cbc2011-01-27 06:26:52 +00002312 }
2313
Andy Greena41314f2011-05-23 10:00:03 +01002314 if (n == context->fds_count)
Andy Green6ee372f2012-04-09 15:09:01 +08002315 fprintf(stderr, "libwebsocket_callback_on_writable: "
2316 "failed to find socket %d\n", wsi->sock);
Andy Greena41314f2011-05-23 10:00:03 +01002317
Andy Green3221f922011-02-12 13:14:11 +00002318 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00002319 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00002320 LWS_CALLBACK_SET_MODE_POLL_FD,
2321 (void *)(long)wsi->sock, NULL, POLLOUT);
2322
Andy Green90c7cbc2011-01-27 06:26:52 +00002323 return 1;
2324}
2325
2326/**
2327 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
2328 * all connections using the given protocol when it
2329 * becomes possible to write to each socket without
2330 * blocking in turn.
2331 *
2332 * @protocol: Protocol whose connections will get callbacks
2333 */
2334
2335int
2336libwebsocket_callback_on_writable_all_protocol(
2337 const struct libwebsocket_protocols *protocol)
2338{
Peter Hinz56885f32011-03-02 22:03:47 +00002339 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00002340 int n;
Andy Green0d338332011-02-12 11:57:43 +00002341 int m;
2342 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00002343
Andy Green0d338332011-02-12 11:57:43 +00002344 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
2345
Peter Hinz56885f32011-03-02 22:03:47 +00002346 for (m = 0; m < context->fd_hashtable[n].length; m++) {
Andy Green0d338332011-02-12 11:57:43 +00002347
Peter Hinz56885f32011-03-02 22:03:47 +00002348 wsi = context->fd_hashtable[n].wsi[m];
Andy Green0d338332011-02-12 11:57:43 +00002349
2350 if (wsi->protocol == protocol)
Peter Hinz56885f32011-03-02 22:03:47 +00002351 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002352 }
2353 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002354
2355 return 0;
2356}
2357
Andy Greenbe93fef2011-02-14 20:25:43 +00002358/**
2359 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
2360 *
2361 * You will not need this unless you are doing something special
2362 *
2363 * @wsi: Websocket connection instance
2364 * @reason: timeout reason
2365 * @secs: how many seconds
2366 */
2367
2368void
2369libwebsocket_set_timeout(struct libwebsocket *wsi,
2370 enum pending_timeout reason, int secs)
2371{
2372 struct timeval tv;
2373
2374 gettimeofday(&tv, NULL);
2375
2376 wsi->pending_timeout_limit = tv.tv_sec + secs;
2377 wsi->pending_timeout = reason;
2378}
2379
Andy Greena6cbece2011-01-27 20:06:03 +00002380
2381/**
2382 * libwebsocket_get_socket_fd() - returns the socket file descriptor
2383 *
2384 * You will not need this unless you are doing something special
2385 *
2386 * @wsi: Websocket connection instance
2387 */
2388
2389int
2390libwebsocket_get_socket_fd(struct libwebsocket *wsi)
2391{
2392 return wsi->sock;
2393}
2394
Andy Green90c7cbc2011-01-27 06:26:52 +00002395/**
2396 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
2397 * receieved packets.
2398 *
2399 * If the output side of a server process becomes choked, this allows flow
2400 * control for the input side.
2401 *
2402 * @wsi: Websocket connection instance to get callback for
2403 * @enable: 0 = disable read servicing for this connection, 1 = enable
2404 */
2405
2406int
2407libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
2408{
Peter Hinz56885f32011-03-02 22:03:47 +00002409 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00002410 int n;
2411
Peter Hinz56885f32011-03-02 22:03:47 +00002412 for (n = 0; n < context->fds_count; n++)
2413 if (context->fds[n].fd == wsi->sock) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002414 if (enable)
Peter Hinz56885f32011-03-02 22:03:47 +00002415 context->fds[n].events |= POLLIN;
Andy Green90c7cbc2011-01-27 06:26:52 +00002416 else
Peter Hinz56885f32011-03-02 22:03:47 +00002417 context->fds[n].events &= ~POLLIN;
Andy Green90c7cbc2011-01-27 06:26:52 +00002418
2419 return 0;
2420 }
2421
Andy Green3221f922011-02-12 13:14:11 +00002422 if (enable)
2423 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00002424 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00002425 LWS_CALLBACK_SET_MODE_POLL_FD,
2426 (void *)(long)wsi->sock, NULL, POLLIN);
2427 else
2428 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00002429 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00002430 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
2431 (void *)(long)wsi->sock, NULL, POLLIN);
2432
Andy Greena41314f2011-05-23 10:00:03 +01002433#if 0
2434 fprintf(stderr, "libwebsocket_rx_flow_control "
Andy Green90c7cbc2011-01-27 06:26:52 +00002435 "unable to find socket\n");
Andy Greena41314f2011-05-23 10:00:03 +01002436#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002437 return 1;
2438}
2439
Andy Green2ac5a6f2011-01-28 10:00:18 +00002440/**
2441 * libwebsocket_canonical_hostname() - returns this host's hostname
2442 *
2443 * This is typically used by client code to fill in the host parameter
2444 * when making a client connection. You can only call it after the context
2445 * has been created.
2446 *
Peter Hinz56885f32011-03-02 22:03:47 +00002447 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00002448 */
2449
2450
2451extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00002452libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00002453{
Peter Hinz56885f32011-03-02 22:03:47 +00002454 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00002455}
2456
2457
Andy Green90c7cbc2011-01-27 06:26:52 +00002458static void sigpipe_handler(int x)
2459{
2460}
2461
Andy Green6901cb32011-02-21 08:06:47 +00002462#ifdef LWS_OPENSSL_SUPPORT
2463static int
2464OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
2465{
2466
2467 SSL *ssl;
2468 int n;
Andy Green2e24da02011-03-05 16:12:04 +00002469 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00002470
2471 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2472 SSL_get_ex_data_X509_STORE_CTX_idx());
2473
2474 /*
Andy Green2e24da02011-03-05 16:12:04 +00002475 * !!! nasty openssl requires the index to come as a library-scope
2476 * static
Andy Green6901cb32011-02-21 08:06:47 +00002477 */
Andy Green2e24da02011-03-05 16:12:04 +00002478 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08002479
Peter Hinz56885f32011-03-02 22:03:47 +00002480 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00002481 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
2482 x509_ctx, ssl, preverify_ok);
2483
2484 /* convert return code from 0 = OK to 1 = OK */
2485
2486 if (!n)
2487 n = 1;
2488 else
2489 n = 0;
2490
2491 return n;
2492}
2493#endif
2494
Andy Greenb45993c2010-12-18 15:13:50 +00002495
Andy Greenab990e42010-10-31 12:42:52 +00002496/**
Andy Green4739e5c2011-01-22 12:51:57 +00002497 * libwebsocket_create_context() - Create the websocket handler
2498 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00002499 * any port, that's what you want if you are not running a
2500 * websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +00002501 * @interf: NULL to bind the listen socket to all interfaces, or the
Andy Green32375b72011-02-19 08:32:53 +00002502 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00002503 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00002504 * specific callback for each one. The list is ended with an
2505 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00002506 * It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +00002507 * @extensions: NULL or array of libwebsocket_extension structs listing the
Andy Green6ee372f2012-04-09 15:09:01 +08002508 * extensions this context supports
Andy Green3faa9c72010-11-08 17:03:03 +00002509 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00002510 * to listen using SSL, set to the filepath to fetch the
2511 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00002512 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00002513 * else ignored
David Galeano2f82be82013-01-09 16:25:54 +08002514 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +00002515 * @gid: group id to change to after setting listen socket, or -1.
2516 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00002517 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green15e31f32012-10-19 18:36:28 +08002518 * @user: optional user pointer that can be recovered via the context
2519 * pointer using libwebsocket_context_user
Andy Green05464c62010-11-12 10:44:18 +00002520 *
Andy Green8f037e42010-12-19 22:13:26 +00002521 * This function creates the listening socket and takes care
2522 * of all initialization in one step.
2523 *
Andy Greene92cd172011-01-19 13:11:55 +00002524 * After initialization, it returns a struct libwebsocket_context * that
2525 * represents this server. After calling, user code needs to take care
2526 * of calling libwebsocket_service() with the context pointer to get the
2527 * server's sockets serviced. This can be done in the same process context
2528 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00002529 *
Andy Green8f037e42010-12-19 22:13:26 +00002530 * The protocol callback functions are called for a handful of events
2531 * including http requests coming in, websocket connections becoming
2532 * established, and data arriving; it's also called periodically to allow
2533 * async transmission.
2534 *
2535 * HTTP requests are sent always to the FIRST protocol in @protocol, since
2536 * at that time websocket protocol has not been negotiated. Other
2537 * protocols after the first one never see any HTTP callack activity.
2538 *
2539 * The server created is a simple http server by default; part of the
2540 * websocket standard is upgrading this http connection to a websocket one.
2541 *
2542 * This allows the same server to provide files like scripts and favicon /
2543 * images or whatever over http and dynamic data over websockets all in
2544 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00002545 */
Andy Green4ea60062010-10-30 12:15:07 +01002546
Andy Greene92cd172011-01-19 13:11:55 +00002547struct libwebsocket_context *
Peter Hinz56885f32011-03-02 22:03:47 +00002548libwebsocket_create_context(int port, const char *interf,
Andy Greenb45993c2010-12-18 15:13:50 +00002549 struct libwebsocket_protocols *protocols,
Andy Greend6e09112011-03-05 16:12:15 +00002550 struct libwebsocket_extension *extensions,
Andy Green8f037e42010-12-19 22:13:26 +00002551 const char *ssl_cert_filepath,
2552 const char *ssl_private_key_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002553 const char *ssl_ca_filepath,
Alon Levy0291eb32012-10-19 11:21:56 +02002554 int gid, int uid, unsigned int options,
David Galeano2f82be82013-01-09 16:25:54 +08002555 void *user)
Andy Greenff95d7a2010-10-28 22:36:01 +01002556{
2557 int n;
Andy Greena41314f2011-05-23 10:00:03 +01002558 int m;
Andy Green4739e5c2011-01-22 12:51:57 +00002559 int sockfd = 0;
Andy Green251f6fa2010-11-03 11:13:06 +00002560 int fd;
Andy Greenff95d7a2010-10-28 22:36:01 +01002561 struct sockaddr_in serv_addr, cli_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00002562 int opt = 1;
Peter Hinz56885f32011-03-02 22:03:47 +00002563 struct libwebsocket_context *context = NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00002564 unsigned int slen;
Andy Green9659f372011-01-27 22:01:43 +00002565 char *p;
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002566 char hostname[1024] = "";
Andy Greena69f0512012-05-03 12:32:38 +08002567// struct hostent *he;
Andy Green0d338332011-02-12 11:57:43 +00002568 struct libwebsocket *wsi;
Andy Greena69f0512012-05-03 12:32:38 +08002569 struct sockaddr sa;
Andy Greenff95d7a2010-10-28 22:36:01 +01002570
Andy Green3faa9c72010-11-08 17:03:03 +00002571#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00002572 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00002573 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00002574#endif
2575
Peter Hinz56885f32011-03-02 22:03:47 +00002576#ifdef _WIN32
2577 {
2578 WORD wVersionRequested;
2579 WSADATA wsaData;
2580 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08002581 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00002582
2583 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
2584 wVersionRequested = MAKEWORD(2, 2);
2585
2586 err = WSAStartup(wVersionRequested, &wsaData);
2587 if (err != 0) {
2588 /* Tell the user that we could not find a usable */
2589 /* Winsock DLL. */
2590 fprintf(stderr, "WSAStartup failed with error: %d\n",
2591 err);
2592 return NULL;
2593 }
David Galeano7b11fec2011-10-04 19:55:18 +08002594
Andy Green6ee372f2012-04-09 15:09:01 +08002595 /* default to a poll() made out of select() */
2596 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08002597
Andy Green6ee372f2012-04-09 15:09:01 +08002598 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08002599 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08002600 if (wsdll)
2601 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Peter Hinz56885f32011-03-02 22:03:47 +00002602 }
2603#endif
2604
2605
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002606 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00002607 if (!context) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002608 fprintf(stderr, "No memory for websocket context\n");
2609 return NULL;
2610 }
Peter Hinz56885f32011-03-02 22:03:47 +00002611 context->protocols = protocols;
2612 context->listen_port = port;
2613 context->http_proxy_port = 0;
2614 context->http_proxy_address[0] = '\0';
2615 context->options = options;
2616 context->fds_count = 0;
Andy Greend6e09112011-03-05 16:12:15 +00002617 context->extensions = extensions;
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002618 context->last_timeout_check_s = 0;
Alon Levy0291eb32012-10-19 11:21:56 +02002619 context->user_space = user;
Andy Green9659f372011-01-27 22:01:43 +00002620
Peter Hinz56885f32011-03-02 22:03:47 +00002621#ifdef WIN32
2622 context->fd_random = 0;
2623#else
2624 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2625 if (context->fd_random < 0) {
Andy Green44eee682011-02-10 09:32:24 +00002626 fprintf(stderr, "Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002627 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Andy Green44eee682011-02-10 09:32:24 +00002628 return NULL;
2629 }
Peter Hinz56885f32011-03-02 22:03:47 +00002630#endif
Andy Green44eee682011-02-10 09:32:24 +00002631
Peter Hinz56885f32011-03-02 22:03:47 +00002632#ifdef LWS_OPENSSL_SUPPORT
2633 context->use_ssl = 0;
2634 context->ssl_ctx = NULL;
2635 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002636 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002637#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002638
Andy Green788c4a82012-10-22 12:29:57 +01002639 if (options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME) {
Andy Greena69f0512012-05-03 12:32:38 +08002640
Andy Green788c4a82012-10-22 12:29:57 +01002641 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002642
Andy Green788c4a82012-10-22 12:29:57 +01002643 } else {
2644
2645 /* find canonical hostname */
2646
2647 hostname[(sizeof hostname) - 1] = '\0';
2648 memset(&sa, 0, sizeof(sa));
2649 sa.sa_family = AF_INET;
2650 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
2651 gethostname(hostname, (sizeof hostname) - 1);
2652
2653 n = 0;
2654
David Galeanoed3c8402013-01-10 10:45:24 +08002655 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01002656 strcpy(sa.sa_data, hostname);
2657 // fprintf(stderr, "my host name is %s\n", sa.sa_data);
2658 n = getnameinfo(&sa, sizeof(sa), hostname,
2659 (sizeof hostname) - 1, NULL, 0, 0);
2660 }
2661
2662 if (!n) {
2663 strncpy(context->canonical_hostname, hostname,
2664 sizeof context->canonical_hostname - 1);
2665 context->canonical_hostname[
2666 sizeof context->canonical_hostname - 1] = '\0';
2667 } else
2668 strncpy(context->canonical_hostname, hostname,
2669 sizeof context->canonical_hostname - 1);
2670
2671 // fprintf(stderr, "context->canonical_hostname = %s\n",
2672 // context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002673 }
2674
Andy Green9659f372011-01-27 22:01:43 +00002675 /* split the proxy ads:port if given */
2676
2677 p = getenv("http_proxy");
2678 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002679 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08002680 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002681 context->http_proxy_address[
2682 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002683
Peter Hinz56885f32011-03-02 22:03:47 +00002684 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002685 if (p == NULL) {
2686 fprintf(stderr, "http_proxy needs to be ads:port\n");
2687 return NULL;
2688 }
2689 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002690 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002691
2692 fprintf(stderr, "Using proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002693 context->http_proxy_address,
2694 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002695 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002696
2697 if (port) {
2698
Andy Green3faa9c72010-11-08 17:03:03 +00002699#ifdef LWS_OPENSSL_SUPPORT
Peter Hinz56885f32011-03-02 22:03:47 +00002700 context->use_ssl = ssl_cert_filepath != NULL &&
Andy Green90c7cbc2011-01-27 06:26:52 +00002701 ssl_private_key_filepath != NULL;
Peter Hinz56885f32011-03-02 22:03:47 +00002702 if (context->use_ssl)
Andy Green90c7cbc2011-01-27 06:26:52 +00002703 fprintf(stderr, " Compiled with SSL support, "
2704 "using it\n");
2705 else
2706 fprintf(stderr, " Compiled with SSL support, "
2707 "not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002708
Andy Green90c7cbc2011-01-27 06:26:52 +00002709#else
2710 if (ssl_cert_filepath != NULL &&
2711 ssl_private_key_filepath != NULL) {
2712 fprintf(stderr, " Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00002713 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00002714 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002715 fprintf(stderr, " Compiled without SSL support, "
2716 "serving unencrypted\n");
2717#endif
2718 }
2719
2720 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00002721#ifdef WIN32
2722#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002723 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002724#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002725
2726
2727#ifdef LWS_OPENSSL_SUPPORT
2728
2729 /* basic openssl init */
2730
2731 SSL_library_init();
2732
2733 OpenSSL_add_all_algorithms();
2734 SSL_load_error_strings();
2735
Andy Green2e24da02011-03-05 16:12:04 +00002736 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002737 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2738
Andy Green90c7cbc2011-01-27 06:26:52 +00002739 /*
2740 * Firefox insists on SSLv23 not SSLv3
2741 * Konq disables SSLv2 by default now, SSLv23 works
2742 */
2743
2744 method = (SSL_METHOD *)SSLv23_server_method();
2745 if (!method) {
2746 fprintf(stderr, "problem creating ssl method: %s\n",
2747 ERR_error_string(ERR_get_error(), ssl_err_buf));
2748 return NULL;
2749 }
Peter Hinz56885f32011-03-02 22:03:47 +00002750 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2751 if (!context->ssl_ctx) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002752 fprintf(stderr, "problem creating ssl context: %s\n",
2753 ERR_error_string(ERR_get_error(), ssl_err_buf));
2754 return NULL;
2755 }
2756
David Galeanocc148e42013-01-10 10:18:59 +08002757#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002758 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002759#endif
David Galeano77a677c2013-01-10 10:14:12 +08002760 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08002761 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08002762
Andy Green90c7cbc2011-01-27 06:26:52 +00002763 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002764
2765 if (port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002766 method = (SSL_METHOD *)SSLv23_client_method();
2767 if (!method) {
2768 fprintf(stderr, "problem creating ssl method: %s\n",
2769 ERR_error_string(ERR_get_error(), ssl_err_buf));
2770 return NULL;
2771 }
2772 /* create context */
2773 context->ssl_client_ctx = SSL_CTX_new(method);
2774 if (!context->ssl_client_ctx) {
2775 fprintf(stderr, "problem creating ssl context: %s\n",
2776 ERR_error_string(ERR_get_error(), ssl_err_buf));
2777 return NULL;
2778 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002779
David Galeanocc148e42013-01-10 10:18:59 +08002780#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002781 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002782#endif
David Galeano77a677c2013-01-10 10:14:12 +08002783 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08002784 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08002785
Peter Hinz56885f32011-03-02 22:03:47 +00002786 /* openssl init for cert verification (for client sockets) */
David Galeano2f82be82013-01-09 16:25:54 +08002787 if (!ssl_ca_filepath) {
2788 if (!SSL_CTX_load_verify_locations(
2789 context->ssl_client_ctx, NULL,
2790 LWS_OPENSSL_CLIENT_CERTS))
2791 fprintf(stderr,
2792 "Unable to load SSL Client certs from %s "
2793 "(set by --with-client-cert-dir= in configure) -- "
2794 " client ssl isn't going to work",
2795 LWS_OPENSSL_CLIENT_CERTS);
2796 } else
2797 if (!SSL_CTX_load_verify_locations(
2798 context->ssl_client_ctx, ssl_ca_filepath,
2799 NULL))
2800 fprintf(stderr,
2801 "Unable to load SSL Client certs "
2802 "file from %s -- client ssl isn't "
2803 "going to work", ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002804
2805 /*
2806 * callback allowing user code to load extra verification certs
2807 * helping the client to verify server identity
2808 */
2809
2810 context->protocols[0].callback(context, NULL,
2811 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2812 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002813 }
Andy Green6ee372f2012-04-09 15:09:01 +08002814
Andy Greenc6bf2c22011-02-20 11:10:47 +00002815 /* as a server, are we requiring clients to identify themselves? */
2816
2817 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
2818
2819 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002820
Peter Hinz56885f32011-03-02 22:03:47 +00002821 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002822 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2823 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002824
2825 /*
2826 * give user code a chance to load certs into the server
2827 * allowing it to verify incoming client certs
2828 */
2829
Peter Hinz56885f32011-03-02 22:03:47 +00002830 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002831 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002832 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002833 }
2834
Peter Hinz56885f32011-03-02 22:03:47 +00002835 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002836
2837 /* openssl init for server sockets */
2838
Andy Green3faa9c72010-11-08 17:03:03 +00002839 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002840 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
2841 ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002842 if (n != 1) {
2843 fprintf(stderr, "problem getting cert '%s': %s\n",
2844 ssl_cert_filepath,
2845 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00002846 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00002847 }
2848 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002849 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
2850 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
Andy Green018d8eb2010-11-08 21:04:23 +00002851 fprintf(stderr, "ssl problem getting key '%s': %s\n",
2852 ssl_private_key_filepath,
2853 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00002854 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00002855 }
2856 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002857 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green018d8eb2010-11-08 21:04:23 +00002858 fprintf(stderr, "Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00002859 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00002860 }
2861
2862 /* SSL is happy and has a cert it's content with */
2863 }
2864#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002865
Andy Greendf736162011-01-18 15:39:02 +00002866 /* selftest */
2867
2868 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00002869 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00002870
Andy Green0d338332011-02-12 11:57:43 +00002871 /* fd hashtable init */
2872
2873 for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
Peter Hinz56885f32011-03-02 22:03:47 +00002874 context->fd_hashtable[n].length = 0;
Andy Green0d338332011-02-12 11:57:43 +00002875
Andy Greenb45993c2010-12-18 15:13:50 +00002876 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002877
Andy Green4739e5c2011-01-22 12:51:57 +00002878 if (port) {
Andy Green8f037e42010-12-19 22:13:26 +00002879
Andy Green4739e5c2011-01-22 12:51:57 +00002880 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2881 if (sockfd < 0) {
2882 fprintf(stderr, "ERROR opening socket");
2883 return NULL;
2884 }
Andy Green775c0dd2010-10-29 14:15:22 +01002885
Andy Green4739e5c2011-01-22 12:51:57 +00002886 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08002887 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2888 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002889
2890 /* Disable Nagle */
2891 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002892 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2893 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002894
Andy Green4739e5c2011-01-22 12:51:57 +00002895 bzero((char *) &serv_addr, sizeof(serv_addr));
2896 serv_addr.sin_family = AF_INET;
Peter Hinz56885f32011-03-02 22:03:47 +00002897 if (interf == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002898 serv_addr.sin_addr.s_addr = INADDR_ANY;
2899 else
Peter Hinz56885f32011-03-02 22:03:47 +00002900 interface_to_sa(interf, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002901 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00002902 serv_addr.sin_port = htons(port);
2903
2904 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2905 sizeof(serv_addr));
2906 if (n < 0) {
2907 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00002908 port, n, errno);
Andy Green4739e5c2011-01-22 12:51:57 +00002909 return NULL;
2910 }
Andy Green0d338332011-02-12 11:57:43 +00002911
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002912 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
2913 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002914 wsi->sock = sockfd;
Andy Greend6e09112011-03-05 16:12:15 +00002915 wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00002916 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Peter Hinz56885f32011-03-02 22:03:47 +00002917 insert_wsi(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002918
David Galeano36973092013-01-10 09:58:24 +08002919 listen(sockfd, SOMAXCONN);
Andy Green0d338332011-02-12 11:57:43 +00002920 fprintf(stderr, " Listening on port %d\n", port);
2921
2922 /* list in the internal poll array */
Andy Green6ee372f2012-04-09 15:09:01 +08002923
Peter Hinz56885f32011-03-02 22:03:47 +00002924 context->fds[context->fds_count].fd = sockfd;
2925 context->fds[context->fds_count++].events = POLLIN;
Andy Green3221f922011-02-12 13:14:11 +00002926
2927 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00002928 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00002929 LWS_CALLBACK_ADD_POLL_FD,
2930 (void *)(long)sockfd, NULL, POLLIN);
2931
Andy Green8f037e42010-12-19 22:13:26 +00002932 }
Andy Greenb45993c2010-12-18 15:13:50 +00002933
Andy Green6ee372f2012-04-09 15:09:01 +08002934 /*
2935 * drop any root privs for this process
2936 * to listen on port < 1023 we would have needed root, but now we are
2937 * listening, we don't want the power for anything else
2938 */
Peter Hinz56885f32011-03-02 22:03:47 +00002939#ifdef WIN32
2940#else
Andy Green3faa9c72010-11-08 17:03:03 +00002941 if (gid != -1)
2942 if (setgid(gid))
2943 fprintf(stderr, "setgid: %s\n", strerror(errno));
2944 if (uid != -1)
2945 if (setuid(uid))
2946 fprintf(stderr, "setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002947#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002948
2949 /* set up our internal broadcast trigger sockets per-protocol */
2950
Peter Hinz56885f32011-03-02 22:03:47 +00002951 for (context->count_protocols = 0;
2952 protocols[context->count_protocols].callback;
2953 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002954
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002955 _debug(" Protocol: %s\n", protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002956
Peter Hinz56885f32011-03-02 22:03:47 +00002957 protocols[context->count_protocols].owning_server = context;
2958 protocols[context->count_protocols].protocol_index =
2959 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00002960
2961 fd = socket(AF_INET, SOCK_STREAM, 0);
2962 if (fd < 0) {
2963 fprintf(stderr, "ERROR opening socket");
Andy Greene92cd172011-01-19 13:11:55 +00002964 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00002965 }
Andy Green8f037e42010-12-19 22:13:26 +00002966
Andy Greenb45993c2010-12-18 15:13:50 +00002967 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08002968 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt,
2969 sizeof(opt));
Andy Greenb45993c2010-12-18 15:13:50 +00002970
2971 bzero((char *) &serv_addr, sizeof(serv_addr));
2972 serv_addr.sin_family = AF_INET;
2973 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2974 serv_addr.sin_port = 0; /* pick the port for us */
2975
2976 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
2977 if (n < 0) {
Andy Green8f037e42010-12-19 22:13:26 +00002978 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
Andy Greenb45993c2010-12-18 15:13:50 +00002979 port, n, errno);
Andy Greene92cd172011-01-19 13:11:55 +00002980 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00002981 }
2982
2983 slen = sizeof cli_addr;
2984 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
2985 if (n < 0) {
2986 fprintf(stderr, "getsockname failed\n");
Andy Greene92cd172011-01-19 13:11:55 +00002987 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00002988 }
Peter Hinz56885f32011-03-02 22:03:47 +00002989 protocols[context->count_protocols].broadcast_socket_port =
Andy Greenb45993c2010-12-18 15:13:50 +00002990 ntohs(cli_addr.sin_port);
2991 listen(fd, 5);
2992
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002993 _debug(" Protocol %s broadcast socket %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002994 protocols[context->count_protocols].name,
Andy Greenb45993c2010-12-18 15:13:50 +00002995 ntohs(cli_addr.sin_port));
2996
Andy Green0d338332011-02-12 11:57:43 +00002997 /* dummy wsi per broadcast proxy socket */
2998
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002999 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
3000 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00003001 wsi->sock = fd;
3002 wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
Andy Greend6e09112011-03-05 16:12:15 +00003003 wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00003004 /* note which protocol we are proxying */
Peter Hinz56885f32011-03-02 22:03:47 +00003005 wsi->protocol_index_for_broadcast_proxy =
3006 context->count_protocols;
3007 insert_wsi(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00003008
3009 /* list in internal poll array */
3010
Peter Hinz56885f32011-03-02 22:03:47 +00003011 context->fds[context->fds_count].fd = fd;
3012 context->fds[context->fds_count].events = POLLIN;
3013 context->fds[context->fds_count].revents = 0;
3014 context->fds_count++;
Andy Green3221f922011-02-12 13:14:11 +00003015
3016 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00003017 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00003018 LWS_CALLBACK_ADD_POLL_FD,
3019 (void *)(long)fd, NULL, POLLIN);
Andy Greenb45993c2010-12-18 15:13:50 +00003020 }
3021
Andy Greena41314f2011-05-23 10:00:03 +01003022 /*
3023 * give all extensions a chance to create any per-context
3024 * allocations they need
3025 */
3026
3027 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
3028 if (port)
3029 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08003030
3031 if (extensions) {
3032 while (extensions->callback) {
3033 debug(" Extension: %s\n", extensions->name);
Aaron Zinman4550f1d2013-01-10 12:35:18 +08003034 extensions->callback(context, extensions, NULL,
3035 (enum libwebsocket_extension_callback_reasons)m,
3036 NULL, NULL, 0);
Andrew Chambersd5512172012-05-20 08:17:09 +08003037 extensions++;
3038 }
Andy Greena41314f2011-05-23 10:00:03 +01003039 }
3040
Peter Hinz56885f32011-03-02 22:03:47 +00003041 return context;
Andy Greene92cd172011-01-19 13:11:55 +00003042}
Andy Greenb45993c2010-12-18 15:13:50 +00003043
Andy Green4739e5c2011-01-22 12:51:57 +00003044
Andy Greened11a022011-01-20 10:23:50 +00003045#ifndef LWS_NO_FORK
3046
Andy Greene92cd172011-01-19 13:11:55 +00003047/**
3048 * libwebsockets_fork_service_loop() - Optional helper function forks off
3049 * a process for the websocket server loop.
Andy Green6964bb52011-01-23 16:50:33 +00003050 * You don't have to use this but if not, you
3051 * have to make sure you are calling
3052 * libwebsocket_service periodically to service
3053 * the websocket traffic
Peter Hinz56885f32011-03-02 22:03:47 +00003054 * @context: server context returned by creation function
Andy Greene92cd172011-01-19 13:11:55 +00003055 */
Andy Greenb45993c2010-12-18 15:13:50 +00003056
Andy Greene92cd172011-01-19 13:11:55 +00003057int
Peter Hinz56885f32011-03-02 22:03:47 +00003058libwebsockets_fork_service_loop(struct libwebsocket_context *context)
Andy Greene92cd172011-01-19 13:11:55 +00003059{
Andy Greene92cd172011-01-19 13:11:55 +00003060 int fd;
3061 struct sockaddr_in cli_addr;
3062 int n;
Andy Green3221f922011-02-12 13:14:11 +00003063 int p;
Andy Greenb45993c2010-12-18 15:13:50 +00003064
Andy Greened11a022011-01-20 10:23:50 +00003065 n = fork();
3066 if (n < 0)
3067 return n;
3068
3069 if (!n) {
3070
3071 /* main process context */
3072
Andy Green3221f922011-02-12 13:14:11 +00003073 /*
3074 * set up the proxy sockets to allow broadcast from
3075 * service process context
3076 */
3077
Peter Hinz56885f32011-03-02 22:03:47 +00003078 for (p = 0; p < context->count_protocols; p++) {
Andy Greened11a022011-01-20 10:23:50 +00003079 fd = socket(AF_INET, SOCK_STREAM, 0);
3080 if (fd < 0) {
3081 fprintf(stderr, "Unable to create socket\n");
3082 return -1;
3083 }
3084 cli_addr.sin_family = AF_INET;
3085 cli_addr.sin_port = htons(
Peter Hinz56885f32011-03-02 22:03:47 +00003086 context->protocols[p].broadcast_socket_port);
Andy Greened11a022011-01-20 10:23:50 +00003087 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
3088 n = connect(fd, (struct sockaddr *)&cli_addr,
3089 sizeof cli_addr);
3090 if (n < 0) {
3091 fprintf(stderr, "Unable to connect to "
3092 "broadcast socket %d, %s\n",
Andy Green3221f922011-02-12 13:14:11 +00003093 n, strerror(errno));
Andy Greened11a022011-01-20 10:23:50 +00003094 return -1;
3095 }
3096
Peter Hinz56885f32011-03-02 22:03:47 +00003097 context->protocols[p].broadcast_socket_user_fd = fd;
Andy Greened11a022011-01-20 10:23:50 +00003098 }
3099
Andy Greene92cd172011-01-19 13:11:55 +00003100 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00003101 }
3102
Artem Baguinski91531662011-12-14 22:14:03 +01003103#ifdef HAVE_SYS_PRCTL_H
Andy Greenb45993c2010-12-18 15:13:50 +00003104 /* we want a SIGHUP when our parent goes down */
3105 prctl(PR_SET_PDEATHSIG, SIGHUP);
Artem Baguinski91531662011-12-14 22:14:03 +01003106#endif
Andy Greenb45993c2010-12-18 15:13:50 +00003107
3108 /* in this forked process, sit and service websocket connections */
Andy Green8f037e42010-12-19 22:13:26 +00003109
Artem Baguinski91531662011-12-14 22:14:03 +01003110 while (1) {
Peter Hinz56885f32011-03-02 22:03:47 +00003111 if (libwebsocket_service(context, 1000))
Andy Green3928f612012-07-20 12:58:38 +08003112 break;
Andy Green5e8967a2012-10-17 20:10:44 +08003113//#ifndef HAVE_SYS_PRCTL_H
Artem Baguinski91531662011-12-14 22:14:03 +01003114/*
3115 * on systems without prctl() (i.e. anything but linux) we can notice that our
3116 * parent is dead if getppid() returns 1. FIXME apparently this is not true for
3117 * solaris, could remember ppid right after fork and wait for it to change.
3118 */
3119
3120 if (getppid() == 1)
3121 break;
Andy Green5e8967a2012-10-17 20:10:44 +08003122//#endif
Artem Baguinski91531662011-12-14 22:14:03 +01003123 }
3124
Andy Green8f037e42010-12-19 22:13:26 +00003125
Andy Green3928f612012-07-20 12:58:38 +08003126 return 1;
Andy Greenff95d7a2010-10-28 22:36:01 +01003127}
3128
Andy Greened11a022011-01-20 10:23:50 +00003129#endif
3130
Andy Greenb45993c2010-12-18 15:13:50 +00003131/**
3132 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00003133 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00003134 * @wsi: pointer to struct websocket you want to know the protocol of
3135 *
Andy Green8f037e42010-12-19 22:13:26 +00003136 *
3137 * This is useful to get the protocol to broadcast back to from inside
Andy Greenb45993c2010-12-18 15:13:50 +00003138 * the callback.
3139 */
Andy Greenab990e42010-10-31 12:42:52 +00003140
Andy Greenb45993c2010-12-18 15:13:50 +00003141const struct libwebsocket_protocols *
3142libwebsockets_get_protocol(struct libwebsocket *wsi)
3143{
3144 return wsi->protocol;
3145}
3146
3147/**
Andy Greene92cd172011-01-19 13:11:55 +00003148 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
Andy Green8f037e42010-12-19 22:13:26 +00003149 * connections of the given protocol.
Andy Greenb45993c2010-12-18 15:13:50 +00003150 * @protocol: pointer to the protocol you will broadcast to all members of
3151 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
Andy Green8f037e42010-12-19 22:13:26 +00003152 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
3153 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
3154 * case you are calling this function from callback context.
Andy Greenb45993c2010-12-18 15:13:50 +00003155 * @len: length of payload data in buf, starting from buf.
Andy Green8f037e42010-12-19 22:13:26 +00003156 *
3157 * This function allows bulk sending of a packet to every connection using
Andy Greenb45993c2010-12-18 15:13:50 +00003158 * the given protocol. It does not send the data directly; instead it calls
3159 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
3160 * wants to actually send the data for that connection, the callback itself
3161 * should call libwebsocket_write().
3162 *
3163 * libwebsockets_broadcast() can be called from another fork context without
3164 * having to take any care about data visibility between the processes, it'll
3165 * "just work".
3166 */
3167
3168
3169int
Andy Green8f037e42010-12-19 22:13:26 +00003170libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
Andy Greenb45993c2010-12-18 15:13:50 +00003171 unsigned char *buf, size_t len)
3172{
Peter Hinz56885f32011-03-02 22:03:47 +00003173 struct libwebsocket_context *context = protocol->owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00003174 int n;
Andy Green0d338332011-02-12 11:57:43 +00003175 int m;
Andy Green6ee372f2012-04-09 15:09:01 +08003176 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00003177
3178 if (!protocol->broadcast_socket_user_fd) {
3179 /*
Andy Greene92cd172011-01-19 13:11:55 +00003180 * We are either running unforked / flat, or we are being
3181 * called from poll thread context
Andy Greenb45993c2010-12-18 15:13:50 +00003182 * eg, from a callback. In that case don't use sockets for
3183 * broadcast IPC (since we can't open a socket connection to
3184 * a socket listening on our own thread) but directly do the
3185 * send action.
3186 *
3187 * Locking is not needed because we are by definition being
3188 * called in the poll thread context and are serialized.
3189 */
3190
Andy Green0d338332011-02-12 11:57:43 +00003191 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
Andy Greenb45993c2010-12-18 15:13:50 +00003192
Peter Hinz56885f32011-03-02 22:03:47 +00003193 for (m = 0; m < context->fd_hashtable[n].length; m++) {
Andy Greenb45993c2010-12-18 15:13:50 +00003194
Peter Hinz56885f32011-03-02 22:03:47 +00003195 wsi = context->fd_hashtable[n].wsi[m];
Andy Greenb45993c2010-12-18 15:13:50 +00003196
Andy Green0d338332011-02-12 11:57:43 +00003197 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
3198 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00003199
Andy Green0d338332011-02-12 11:57:43 +00003200 /*
3201 * never broadcast to
3202 * non-established connections
3203 */
3204 if (wsi->state != WSI_STATE_ESTABLISHED)
3205 continue;
3206
3207 /* only broadcast to guys using
3208 * requested protocol
3209 */
3210 if (wsi->protocol != protocol)
3211 continue;
3212
Peter Hinz56885f32011-03-02 22:03:47 +00003213 wsi->protocol->callback(context, wsi,
Andy Green8f037e42010-12-19 22:13:26 +00003214 LWS_CALLBACK_BROADCAST,
Andy Green0d338332011-02-12 11:57:43 +00003215 wsi->user_space,
Andy Greenb45993c2010-12-18 15:13:50 +00003216 buf, len);
Andy Green0d338332011-02-12 11:57:43 +00003217 }
Andy Greenb45993c2010-12-18 15:13:50 +00003218 }
3219
3220 return 0;
3221 }
3222
Andy Green0ca6a172010-12-19 20:50:01 +00003223 /*
3224 * We're being called from a different process context than the server
3225 * loop. Instead of broadcasting directly, we send our
3226 * payload on a socket to do the IPC; the server process will serialize
3227 * the broadcast action in its main poll() loop.
3228 *
3229 * There's one broadcast socket listening for each protocol supported
3230 * set up when the websocket server initializes
3231 */
3232
Andy Green6964bb52011-01-23 16:50:33 +00003233 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
Andy Greenb45993c2010-12-18 15:13:50 +00003234
3235 return n;
3236}
Andy Green82c3d542011-03-07 21:16:31 +00003237
3238int
3239libwebsocket_is_final_fragment(struct libwebsocket *wsi)
3240{
3241 return wsi->final;
3242}
Alex Bligh49146db2011-11-07 17:19:25 +08003243
David Galeanoe2cf9922013-01-09 18:06:55 +08003244unsigned char
3245libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
3246{
3247 return wsi->rsv;
3248}
3249
Alex Bligh49146db2011-11-07 17:19:25 +08003250void *
3251libwebsocket_ensure_user_space(struct libwebsocket *wsi)
3252{
3253 /* allocate the per-connection user memory (if any) */
3254
3255 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
3256 wsi->user_space = malloc(
3257 wsi->protocol->per_session_data_size);
3258 if (wsi->user_space == NULL) {
3259 fprintf(stderr, "Out of memory for "
3260 "conn user space\n");
3261 return NULL;
3262 }
Andy Green6ee372f2012-04-09 15:09:01 +08003263 memset(wsi->user_space, 0,
3264 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08003265 }
3266 return wsi->user_space;
3267}