blob: 12c8778f13e1272842fc00857a2ef4c92e3f0af5 [file] [log] [blame]
Andy Green05a0a7b2010-10-31 17:51:39 +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 Green32375b72011-02-19 08:32:53 +000023#include <ifaddrs.h>
Andy Greenff95d7a2010-10-28 22:36:01 +010024
Andy Greenbe93fef2011-02-14 20:25:43 +000025/*
26 * In-place str to lower case
27 */
28
29static void
30strtolower(char *s)
31{
32 while (*s) {
33 *s = tolower(*s);
34 s++;
35 }
36}
37
Andy Green0d338332011-02-12 11:57:43 +000038/* file descriptor hash management */
39
40struct libwebsocket *
41wsi_from_fd(struct libwebsocket_context *this, int fd)
42{
43 int h = LWS_FD_HASH(fd);
44 int n = 0;
45
46 for (n = 0; n < this->fd_hashtable[h].length; n++)
47 if (this->fd_hashtable[h].wsi[n]->sock == fd)
48 return this->fd_hashtable[h].wsi[n];
49
50 return NULL;
51}
52
53int
54insert_wsi(struct libwebsocket_context *this, struct libwebsocket *wsi)
55{
56 int h = LWS_FD_HASH(wsi->sock);
57
58 if (this->fd_hashtable[h].length == MAX_CLIENTS - 1) {
59 fprintf(stderr, "hash table overflow\n");
60 return 1;
61 }
62
63 this->fd_hashtable[h].wsi[this->fd_hashtable[h].length++] = wsi;
64
65 return 0;
66}
67
68int
69delete_from_fd(struct libwebsocket_context *this, int fd)
70{
71 int h = LWS_FD_HASH(fd);
72 int n = 0;
73
74 for (n = 0; n < this->fd_hashtable[h].length; n++)
75 if (this->fd_hashtable[h].wsi[n]->sock == fd) {
76 while (n < this->fd_hashtable[h].length) {
77 this->fd_hashtable[h].wsi[n] =
78 this->fd_hashtable[h].wsi[n + 1];
79 n++;
80 }
81 this->fd_hashtable[h].length--;
82
83 return 0;
84 }
85
86 fprintf(stderr, "Failed to find fd %d requested for "
87 "delete in hashtable\n", fd);
88 return 1;
89}
90
Andy Green1f9bf522011-02-14 21:14:37 +000091#ifdef LWS_OPENSSL_SUPPORT
92static void
93libwebsockets_decode_ssl_error(void)
94{
95 char buf[256];
96 u_long err;
97
98 while ((err = ERR_get_error()) != 0) {
99 ERR_error_string_n(err, buf, sizeof(buf));
100 fprintf(stderr, "*** %s\n", buf);
101 }
102}
103#endif
Andy Green0d338332011-02-12 11:57:43 +0000104
Andy Green32375b72011-02-19 08:32:53 +0000105
106static int
107interface_to_sa(const char* ifname, struct sockaddr_in *addr, size_t addrlen)
108{
109 int rc = -1;
110 struct ifaddrs *ifr;
111 struct ifaddrs *ifc;
112 struct sockaddr_in *sin;
113
114 getifaddrs(&ifr);
115 for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
116 if (strcmp(ifc->ifa_name, ifname))
117 continue;
118 if (ifc->ifa_addr == NULL)
119 continue;
120 sin = (struct sockaddr_in *)ifc->ifa_addr;
121 if (sin->sin_family != AF_INET)
122 continue;
123 memcpy(addr, sin, addrlen);
124 rc = 0;
125 }
126
127 freeifaddrs(ifr);
128
129 return rc;
130}
131
Andy Green8f037e42010-12-19 22:13:26 +0000132void
Andy Green4b6fbe12011-02-14 08:03:48 +0000133libwebsocket_close_and_free_session(struct libwebsocket_context *this,
Andy Green687b0182011-02-26 11:04:01 +0000134 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000135{
Andy Greenb45993c2010-12-18 15:13:50 +0000136 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000137 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000138 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
139 LWS_SEND_BUFFER_POST_PADDING];
Andy Greenb45993c2010-12-18 15:13:50 +0000140
Andy Green4b6fbe12011-02-14 08:03:48 +0000141 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000142 return;
143
Andy Green62c54d22011-02-14 09:14:25 +0000144 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000145
Andy Green62c54d22011-02-14 09:14:25 +0000146 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000147 return;
148
Andy Green4b6fbe12011-02-14 08:03:48 +0000149 /* remove this fd from wsi mapping hashtable */
150
151 delete_from_fd(this, wsi->sock);
152
153 /* delete it from the internal poll list if still present */
154
155 for (n = 0; n < this->fds_count; n++) {
156 if (this->fds[n].fd != wsi->sock)
157 continue;
158 while (n < this->fds_count - 1) {
159 this->fds[n] = this->fds[n + 1];
160 n++;
161 }
162 this->fds_count--;
163 /* we only have to deal with one */
164 n = this->fds_count;
165 }
166
167 /* remove also from external POLL support via protocol 0 */
168
Andy Green62c54d22011-02-14 09:14:25 +0000169 this->protocols[0].callback(this, wsi,
Andy Green4b6fbe12011-02-14 08:03:48 +0000170 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
171
Andy Green687b0182011-02-26 11:04:01 +0000172 wsi->close_reason = reason;
173
Andy Green5e1fa172011-02-10 09:07:05 +0000174 /*
175 * signal we are closing, libsocket_write will
176 * add any necessary version-specific stuff. If the write fails,
177 * no worries we are closing anyway. If we didn't initiate this
178 * close, then our state has been changed to
Andy Green4b6fbe12011-02-14 08:03:48 +0000179 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this
Andy Green5e1fa172011-02-10 09:07:05 +0000180 */
181
Andy Green62c54d22011-02-14 09:14:25 +0000182 if (old_state == WSI_STATE_ESTABLISHED)
Andy Green5e1fa172011-02-10 09:07:05 +0000183 libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 0,
184 LWS_WRITE_CLOSE);
185
Andy Green251f6fa2010-11-03 11:13:06 +0000186 wsi->state = WSI_STATE_DEAD_SOCKET;
187
Andy Green4b6fbe12011-02-14 08:03:48 +0000188 /* tell the user it's all over for this guy */
189
Andy Green62c54d22011-02-14 09:14:25 +0000190 if (wsi->protocol->callback && old_state == WSI_STATE_ESTABLISHED)
191 wsi->protocol->callback(this, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000192 wsi->user_space, NULL, 0);
Andy Green251f6fa2010-11-03 11:13:06 +0000193
Andy Green4b6fbe12011-02-14 08:03:48 +0000194 /* free up his allocations */
195
Andy Green251f6fa2010-11-03 11:13:06 +0000196 for (n = 0; n < WSI_TOKEN_COUNT; n++)
197 if (wsi->utf8_token[n].token)
198 free(wsi->utf8_token[n].token);
199
Andy Green0ca6a172010-12-19 20:50:01 +0000200/* fprintf(stderr, "closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000201
Andy Green3faa9c72010-11-08 17:03:03 +0000202#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000203 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000204 n = SSL_get_fd(wsi->ssl);
205 SSL_shutdown(wsi->ssl);
206 close(n);
207 SSL_free(wsi->ssl);
208 } else {
209#endif
210 shutdown(wsi->sock, SHUT_RDWR);
211 close(wsi->sock);
212#ifdef LWS_OPENSSL_SUPPORT
213 }
214#endif
Andy Green4f3943a2010-11-12 10:44:16 +0000215 if (wsi->user_space)
216 free(wsi->user_space);
217
Andy Green251f6fa2010-11-03 11:13:06 +0000218 free(wsi);
219}
220
Andy Green07034092011-02-13 08:37:12 +0000221/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000222 * libwebsockets_hangup_on_client() - Server calls to terminate client
223 * connection
224 * @this: libwebsockets context
225 * @fd: Connection socket descriptor
226 */
227
228void
229libwebsockets_hangup_on_client(struct libwebsocket_context *this, int fd)
230{
231 struct libwebsocket *wsi = wsi_from_fd(this, fd);
Andy Greende6ab322011-02-13 09:15:10 +0000232 int n;
Andy Greenf7ee5492011-02-13 09:04:21 +0000233
234 if (wsi == NULL)
235 return;
236
Andy Greende6ab322011-02-13 09:15:10 +0000237 delete_from_fd(this, fd);
238
239 for (n = 0; n < this->fds_count - 1; n++)
240 if (this->fds[n].fd == fd) {
241 while (n < this->fds_count - 1) {
242 this->fds[n] = this->fds[n + 1];
243 n++;
244 }
245 n = this->fds_count;
246 this->fds_count--;
247 }
248
Andy Green687b0182011-02-26 11:04:01 +0000249 libwebsocket_close_and_free_session(this, wsi, 0);
Andy Greenf7ee5492011-02-13 09:04:21 +0000250}
251
252
253/**
Andy Green07034092011-02-13 08:37:12 +0000254 * libwebsockets_get_peer_addresses() - Get client address information
255 * @fd: Connection socket descriptor
256 * @name: Buffer to take client address name
257 * @name_len: Length of client address name buffer
258 * @rip: Buffer to take client address IP qotted quad
259 * @rip_len: Length of client address IP buffer
260 *
261 * This function fills in @name and @rip with the name and IP of
262 * the client connected with socket descriptor @fd. Names may be
263 * truncated if there is not enough room. If either cannot be
264 * determined, they will be returned as valid zero-length strings.
265 */
266
267void
268libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
269 char *rip, int rip_len)
270{
271 unsigned int len;
272 struct sockaddr_in sin;
273 struct hostent *host;
274 struct hostent *host1;
275 char ip[128];
276 char *p;
277 int n;
278
279 rip[0] = '\0';
280 name[0] = '\0';
281
282 len = sizeof sin;
283 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
284 perror("getpeername");
285 return;
286 }
287
288 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
289 AF_INET);
290 if (host == NULL) {
291 perror("gethostbyaddr");
292 return;
293 }
294
295 strncpy(name, host->h_name, name_len);
296 name[name_len - 1] = '\0';
297
298 host1 = gethostbyname(host->h_name);
299 if (host1 == NULL)
300 return;
301 p = (char *)host1;
302 n = 0;
303 while (p != NULL) {
304 p = host1->h_addr_list[n++];
305 if (p == NULL)
306 continue;
307 if (host1->h_addrtype != AF_INET)
308 continue;
309
310 sprintf(ip, "%d.%d.%d.%d",
311 p[0], p[1], p[2], p[3]);
312 p = NULL;
313 strncpy(rip, ip, rip_len);
314 rip[rip_len - 1] = '\0';
315 }
316}
Andy Green9f990342011-02-12 11:57:45 +0000317
318/**
319 * libwebsocket_service_fd() - Service polled socket with something waiting
320 * @this: Websocket context
321 * @pollfd: The pollfd entry describing the socket fd and which events
322 * happened.
323 *
324 * This function closes any active connections and then frees the
325 * context. After calling this, any further use of the context is
326 * undefined.
327 */
328
329int
Andy Green0d338332011-02-12 11:57:43 +0000330libwebsocket_service_fd(struct libwebsocket_context *this,
331 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000332{
333 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BROADCAST_PAYLOAD +
334 LWS_SEND_BUFFER_POST_PADDING];
Andy Greena71eafc2011-02-14 17:59:43 +0000335 struct libwebsocket *wsi;
Andy Green0d338332011-02-12 11:57:43 +0000336 struct libwebsocket *new_wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000337 int n;
Andy Green0d338332011-02-12 11:57:43 +0000338 int m;
Andy Greenb45993c2010-12-18 15:13:50 +0000339 size_t len;
Andy Green0d338332011-02-12 11:57:43 +0000340 int accept_fd;
341 unsigned int clilen;
342 struct sockaddr_in cli_addr;
Andy Greena71eafc2011-02-14 17:59:43 +0000343 struct timeval tv;
Andy Greenbe93fef2011-02-14 20:25:43 +0000344 static const char magic_websocket_guid[] =
345 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
346 static const char magic_websocket_04_masking_guid[] =
347 "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
348 char hash[20];
349 char pkt[1024];
350 char *p = &pkt[0];
351 const char *pc;
352 int okay = 0;
353#ifdef LWS_OPENSSL_SUPPORT
354 char ssl_err_buf[512];
355#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000356 /*
357 * you can call us with pollfd = NULL to just allow the once-per-second
358 * global timeout checks; if less than a second since the last check
359 * it returns immediately then.
360 */
361
362 gettimeofday(&tv, NULL);
363
364 if (this->last_timeout_check_s != tv.tv_sec) {
365 this->last_timeout_check_s = tv.tv_sec;
366
367 /* global timeout check once per second */
368
369 for (n = 0; n < this->fds_count; n++) {
370 wsi = wsi_from_fd(this, this->fds[n].fd);
371 if (!wsi->pending_timeout)
372 continue;
373
374 /*
375 * if we went beyond the allowed time, kill the
376 * connection
377 */
378
379 if (tv.tv_sec > wsi->pending_timeout_limit)
Andy Green687b0182011-02-26 11:04:01 +0000380 libwebsocket_close_and_free_session(this, wsi,
381 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena71eafc2011-02-14 17:59:43 +0000382 }
383 }
384
385 /* just here for timeout management? */
386
387 if (pollfd == NULL)
388 return 0;
389
390 /* no, here to service a socket descriptor */
391
392 wsi = wsi_from_fd(this, pollfd->fd);
Andy Greenb45993c2010-12-18 15:13:50 +0000393
Andy Green0d338332011-02-12 11:57:43 +0000394 if (wsi == NULL)
395 return 1;
Andy Green8f037e42010-12-19 22:13:26 +0000396
Andy Green0d338332011-02-12 11:57:43 +0000397 switch (wsi->mode) {
398 case LWS_CONNMODE_SERVER_LISTENER:
399
400 /* pollin means a client has connected to us then */
401
402 if (!pollfd->revents & POLLIN)
403 break;
404
405 /* listen socket got an unencrypted connection... */
406
407 clilen = sizeof(cli_addr);
408 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
409 &clilen);
410 if (accept_fd < 0) {
411 fprintf(stderr, "ERROR on accept");
412 break;
413 }
414
415 if (this->fds_count >= MAX_CLIENTS) {
Andy Green3221f922011-02-12 13:14:11 +0000416 fprintf(stderr, "too busy to accept new client\n");
Andy Green0d338332011-02-12 11:57:43 +0000417 close(accept_fd);
418 break;
419 }
420
Andy Green07034092011-02-13 08:37:12 +0000421 /*
422 * look at who we connected to and give user code a chance
423 * to reject based on client IP. There's no protocol selected
424 * yet so we issue this to protocols[0]
425 */
426
Andy Green62c54d22011-02-14 09:14:25 +0000427 if ((this->protocols[0].callback)(this, wsi,
Andy Green07034092011-02-13 08:37:12 +0000428 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
429 (void*)(long)accept_fd, NULL, 0)) {
430 fprintf(stderr, "Callback denied network connection\n");
431 close(accept_fd);
432 break;
433 }
434
Andy Green0d338332011-02-12 11:57:43 +0000435 /* accepting connection to main listener */
436
437 new_wsi = malloc(sizeof(struct libwebsocket));
438 if (new_wsi == NULL) {
439 fprintf(stderr, "Out of memory for new connection\n");
440 break;
441 }
442
443 memset(new_wsi, 0, sizeof (struct libwebsocket));
444 new_wsi->sock = accept_fd;
Andy Greena71eafc2011-02-14 17:59:43 +0000445 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green0d338332011-02-12 11:57:43 +0000446
447#ifdef LWS_OPENSSL_SUPPORT
448 new_wsi->ssl = NULL;
Andy Green0d338332011-02-12 11:57:43 +0000449
450 if (this->use_ssl) {
451
452 new_wsi->ssl = SSL_new(this->ssl_ctx);
453 if (new_wsi->ssl == NULL) {
454 fprintf(stderr, "SSL_new failed: %s\n",
455 ERR_error_string(SSL_get_error(
456 new_wsi->ssl, 0), NULL));
Andy Green1f9bf522011-02-14 21:14:37 +0000457 libwebsockets_decode_ssl_error();
Andy Green0d338332011-02-12 11:57:43 +0000458 free(new_wsi);
459 break;
460 }
461
462 SSL_set_fd(new_wsi->ssl, accept_fd);
463
464 n = SSL_accept(new_wsi->ssl);
465 if (n != 1) {
466 /*
467 * browsers seem to probe with various
468 * ssl params which fail then retry
469 * and succeed
470 */
471 debug("SSL_accept failed skt %u: %s\n",
472 pollfd->fd,
473 ERR_error_string(SSL_get_error(
474 new_wsi->ssl, n), NULL));
475 SSL_free(
476 new_wsi->ssl);
477 free(new_wsi);
478 break;
479 }
Andy Greenc6bf2c22011-02-20 11:10:47 +0000480
Andy Green0d338332011-02-12 11:57:43 +0000481 debug("accepted new SSL conn "
482 "port %u on fd=%d SSL ver %s\n",
483 ntohs(cli_addr.sin_port), accept_fd,
484 SSL_get_version(new_wsi->ssl));
485
486 } else
487#endif
488 debug("accepted new conn port %u on fd=%d\n",
489 ntohs(cli_addr.sin_port), accept_fd);
490
491 /* intialize the instance struct */
492
493 new_wsi->state = WSI_STATE_HTTP;
494 new_wsi->name_buffer_pos = 0;
495 new_wsi->mode = LWS_CONNMODE_WS_SERVING;
496
497 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
498 new_wsi->utf8_token[n].token = NULL;
499 new_wsi->utf8_token[n].token_len = 0;
500 }
501
502 /*
503 * these can only be set once the protocol is known
504 * we set an unestablished connection's protocol pointer
505 * to the start of the supported list, so it can look
506 * for matching ones during the handshake
507 */
508 new_wsi->protocol = this->protocols;
509 new_wsi->user_space = NULL;
510
511 /*
512 * Default protocol is 76 / 00
513 * After 76, there's a header specified to inform which
514 * draft the client wants, when that's seen we modify
515 * the individual connection's spec revision accordingly
516 */
517 new_wsi->ietf_spec_revision = 0;
518
519 insert_wsi(this, new_wsi);
520
Andy Green0d338332011-02-12 11:57:43 +0000521 /*
522 * make sure NO events are seen yet on this new socket
523 * (otherwise we inherit old fds[client].revents from
524 * previous socket there and die mysteriously! )
525 */
526 this->fds[this->fds_count].revents = 0;
527
528 this->fds[this->fds_count].events = POLLIN;
529 this->fds[this->fds_count++].fd = accept_fd;
530
Andy Green3221f922011-02-12 13:14:11 +0000531 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +0000532 this->protocols[0].callback(this, new_wsi,
Andy Green3221f922011-02-12 13:14:11 +0000533 LWS_CALLBACK_ADD_POLL_FD,
534 (void *)(long)accept_fd, NULL, POLLIN);
535
Andy Green0d338332011-02-12 11:57:43 +0000536 break;
537
538 case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
539
540 /* as we are listening, POLLIN means accept() is needed */
541
542 if (!pollfd->revents & POLLIN)
543 break;
544
545 /* listen socket got an unencrypted connection... */
546
547 clilen = sizeof(cli_addr);
548 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
549 &clilen);
550 if (accept_fd < 0) {
551 fprintf(stderr, "ERROR on accept");
552 break;
553 }
554
555 if (this->fds_count >= MAX_CLIENTS) {
Andy Green3221f922011-02-12 13:14:11 +0000556 fprintf(stderr, "too busy to accept new broadcast "
557 "proxy client\n");
Andy Green0d338332011-02-12 11:57:43 +0000558 close(accept_fd);
559 break;
560 }
561
562 /* create a dummy wsi for the connection and add it */
563
564 new_wsi = malloc(sizeof(struct libwebsocket));
565 memset(new_wsi, 0, sizeof (struct libwebsocket));
566 new_wsi->sock = accept_fd;
567 new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY;
568 new_wsi->state = WSI_STATE_ESTABLISHED;
569 /* note which protocol we are proxying */
570 new_wsi->protocol_index_for_broadcast_proxy =
571 wsi->protocol_index_for_broadcast_proxy;
572 insert_wsi(this, new_wsi);
573
574 /* add connected socket to internal poll array */
575
576 this->fds[this->fds_count].revents = 0;
577 this->fds[this->fds_count].events = POLLIN;
578 this->fds[this->fds_count++].fd = accept_fd;
579
Andy Green3221f922011-02-12 13:14:11 +0000580 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +0000581 this->protocols[0].callback(this, new_wsi,
Andy Green3221f922011-02-12 13:14:11 +0000582 LWS_CALLBACK_ADD_POLL_FD,
583 (void *)(long)accept_fd, NULL, POLLIN);
584
Andy Green0d338332011-02-12 11:57:43 +0000585 break;
586
587 case LWS_CONNMODE_BROADCAST_PROXY:
Andy Green8f037e42010-12-19 22:13:26 +0000588
Andy Greenb45993c2010-12-18 15:13:50 +0000589 /* handle session socket closed */
Andy Green8f037e42010-12-19 22:13:26 +0000590
Andy Green0d338332011-02-12 11:57:43 +0000591 if (pollfd->revents & (POLLERR | POLLHUP)) {
Andy Green8f037e42010-12-19 22:13:26 +0000592
Andy Green0d338332011-02-12 11:57:43 +0000593 debug("Session Socket %p (fd=%d) dead\n",
Timothy J Fontaineb86d64e2011-02-14 17:55:27 +0000594 (void *)wsi, pollfd->fd);
Andy Greenb45993c2010-12-18 15:13:50 +0000595
Andy Green687b0182011-02-26 11:04:01 +0000596 libwebsocket_close_and_free_session(this, wsi,
597 LWS_CLOSE_STATUS_NORMAL);
Andy Green4b6fbe12011-02-14 08:03:48 +0000598 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +0000599 }
Andy Green8f037e42010-12-19 22:13:26 +0000600
Andy Green90c7cbc2011-01-27 06:26:52 +0000601 /* the guy requested a callback when it was OK to write */
602
Andy Green0d338332011-02-12 11:57:43 +0000603 if (pollfd->revents & POLLOUT) {
Andy Green90c7cbc2011-01-27 06:26:52 +0000604
Andy Green0d338332011-02-12 11:57:43 +0000605 /* one shot */
Andy Green90c7cbc2011-01-27 06:26:52 +0000606
Andy Green0d338332011-02-12 11:57:43 +0000607 pollfd->events &= ~POLLOUT;
608
Andy Green3221f922011-02-12 13:14:11 +0000609 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +0000610 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +0000611 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
612 (void *)(long)wsi->sock, NULL, POLLOUT);
613
Andy Green62c54d22011-02-14 09:14:25 +0000614 wsi->protocol->callback(this, wsi,
Andy Green90c7cbc2011-01-27 06:26:52 +0000615 LWS_CALLBACK_CLIENT_WRITEABLE,
Andy Green0d338332011-02-12 11:57:43 +0000616 wsi->user_space,
Andy Green90c7cbc2011-01-27 06:26:52 +0000617 NULL, 0);
618 }
619
Andy Greenb45993c2010-12-18 15:13:50 +0000620 /* any incoming data ready? */
621
Andy Green0d338332011-02-12 11:57:43 +0000622 if (!(pollfd->revents & POLLIN))
623 break;
Andy Greenb45993c2010-12-18 15:13:50 +0000624
Andy Green0d338332011-02-12 11:57:43 +0000625 /* get the issued broadcast payload from the socket */
Andy Greenb45993c2010-12-18 15:13:50 +0000626
Andy Green0d338332011-02-12 11:57:43 +0000627 len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING,
628 MAX_BROADCAST_PAYLOAD);
629 if (len < 0) {
630 fprintf(stderr, "Error reading broadcast payload\n");
Andy Green4b6fbe12011-02-14 08:03:48 +0000631 break;
Andy Green0d338332011-02-12 11:57:43 +0000632 }
Andy Greenb45993c2010-12-18 15:13:50 +0000633
Andy Green0d338332011-02-12 11:57:43 +0000634 /* broadcast it to all guys with this protocol index */
Andy Green8f037e42010-12-19 22:13:26 +0000635
Andy Green0d338332011-02-12 11:57:43 +0000636 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
Andy Green8f037e42010-12-19 22:13:26 +0000637
Andy Green0d338332011-02-12 11:57:43 +0000638 for (m = 0; m < this->fd_hashtable[n].length; m++) {
Andy Greenb45993c2010-12-18 15:13:50 +0000639
Andy Green0d338332011-02-12 11:57:43 +0000640 new_wsi = this->fd_hashtable[n].wsi[m];
Andy Greenb45993c2010-12-18 15:13:50 +0000641
Andy Green0d338332011-02-12 11:57:43 +0000642 /* only to clients we are serving to */
Andy Greenb45993c2010-12-18 15:13:50 +0000643
Andy Green0d338332011-02-12 11:57:43 +0000644 if (new_wsi->mode != LWS_CONNMODE_WS_SERVING)
Andy Greenb45993c2010-12-18 15:13:50 +0000645 continue;
646
647 /*
648 * never broadcast to non-established
649 * connection
650 */
651
Andy Green0d338332011-02-12 11:57:43 +0000652 if (new_wsi->state != WSI_STATE_ESTABLISHED)
Andy Green4739e5c2011-01-22 12:51:57 +0000653 continue;
654
Andy Greenb45993c2010-12-18 15:13:50 +0000655 /*
656 * only broadcast to connections using
657 * the requested protocol
658 */
659
Andy Green0d338332011-02-12 11:57:43 +0000660 if (new_wsi->protocol->protocol_index !=
661 wsi->protocol_index_for_broadcast_proxy)
Andy Greenb45993c2010-12-18 15:13:50 +0000662 continue;
663
Andy Green8f037e42010-12-19 22:13:26 +0000664 /* broadcast it to this connection */
665
Andy Green62c54d22011-02-14 09:14:25 +0000666 new_wsi->protocol->callback(this, new_wsi,
Andy Green8f037e42010-12-19 22:13:26 +0000667 LWS_CALLBACK_BROADCAST,
Andy Green0d338332011-02-12 11:57:43 +0000668 new_wsi->user_space,
Andy Green0ca6a172010-12-19 20:50:01 +0000669 buf + LWS_SEND_BUFFER_PRE_PADDING, len);
Andy Greenb45993c2010-12-18 15:13:50 +0000670 }
Andy Green0d338332011-02-12 11:57:43 +0000671 }
672 break;
Andy Greenb45993c2010-12-18 15:13:50 +0000673
Andy Greenbe93fef2011-02-14 20:25:43 +0000674 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
675
676 /* handle proxy hung up on us */
677
678 if (pollfd->revents & (POLLERR | POLLHUP)) {
679
680 fprintf(stderr, "Proxy connection %p (fd=%d) dead\n",
681 (void *)wsi, pollfd->fd);
682
Andy Green687b0182011-02-26 11:04:01 +0000683 libwebsocket_close_and_free_session(this, wsi,
684 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +0000685 return 1;
686 }
687
688 n = recv(wsi->sock, pkt, sizeof pkt, 0);
689 if (n < 0) {
Andy Green687b0182011-02-26 11:04:01 +0000690 libwebsocket_close_and_free_session(this, wsi,
691 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +0000692 fprintf(stderr, "ERROR reading from proxy socket\n");
693 return 1;
694 }
695
696 pkt[13] = '\0';
697 if (strcmp(pkt, "HTTP/1.0 200 ") != 0) {
Andy Green687b0182011-02-26 11:04:01 +0000698 libwebsocket_close_and_free_session(this, wsi,
699 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +0000700 fprintf(stderr, "ERROR from proxy: %s\n", pkt);
701 return 1;
702 }
703
704 /* clear his proxy connection timeout */
705
706 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
707
708 /* fallthru */
709
710 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
711
712 #ifdef LWS_OPENSSL_SUPPORT
713 if (wsi->use_ssl) {
714
715 wsi->ssl = SSL_new(this->ssl_client_ctx);
716 wsi->client_bio = BIO_new_socket(wsi->sock, BIO_NOCLOSE);
717 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
718
Andy Green6901cb32011-02-21 08:06:47 +0000719 SSL_set_ex_data(wsi->ssl,
720 this->openssl_websocket_private_data_index, this);
721
Andy Greenbe93fef2011-02-14 20:25:43 +0000722 if (SSL_connect(wsi->ssl) <= 0) {
723 fprintf(stderr, "SSL connect error %s\n",
Andy Green687b0182011-02-26 11:04:01 +0000724 ERR_error_string(ERR_get_error(),
725 ssl_err_buf));
726 libwebsocket_close_and_free_session(this, wsi,
727 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +0000728 return 1;
729 }
730
731 n = SSL_get_verify_result(wsi->ssl);
Andy Green687b0182011-02-26 11:04:01 +0000732 if (n != X509_V_OK) && (
733 n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
734 wsi->use_ssl != 2)) {
Andy Greenbe93fef2011-02-14 20:25:43 +0000735
Andy Green687b0182011-02-26 11:04:01 +0000736 fprintf(stderr, "server's cert didn't "
737 "look good %d\n", n);
738 libwebsocket_close_and_free_session(this, wsi,
739 LWS_CLOSE_STATUS_NOSTATUS);
740 return 1;
Andy Greenbe93fef2011-02-14 20:25:43 +0000741 }
742 } else {
743 wsi->ssl = NULL;
744 #endif
745
746
747 #ifdef LWS_OPENSSL_SUPPORT
748 }
749 #endif
750
751 /*
752 * create the random key
753 */
754
755 n = read(this->fd_random, hash, 16);
756 if (n != 16) {
757 fprintf(stderr, "Unable to read from random dev %s\n",
758 SYSTEM_RANDOM_FILEPATH);
759 free(wsi->c_path);
760 free(wsi->c_host);
Andy Green08d33922011-02-26 10:22:49 +0000761 if (wsi->c_origin)
762 free(wsi->c_origin);
Andy Greenbe93fef2011-02-14 20:25:43 +0000763 if (wsi->c_protocol)
764 free(wsi->c_protocol);
Andy Green687b0182011-02-26 11:04:01 +0000765 libwebsocket_close_and_free_session(this, wsi,
766 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +0000767 return 1;
768 }
769
770 lws_b64_encode_string(hash, 16, wsi->key_b64,
771 sizeof wsi->key_b64);
772
773 /*
774 * 04 example client handshake
775 *
776 * GET /chat HTTP/1.1
777 * Host: server.example.com
778 * Upgrade: websocket
779 * Connection: Upgrade
780 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
781 * Sec-WebSocket-Origin: http://example.com
782 * Sec-WebSocket-Protocol: chat, superchat
783 * Sec-WebSocket-Version: 4
784 */
785
Andy Green08d33922011-02-26 10:22:49 +0000786 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
787 p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
788 p += sprintf(p, "Upgrade: websocket\x0d\x0a");
789 p += sprintf(p, "Connection: Upgrade\x0d\x0a"
Andy Greenbe93fef2011-02-14 20:25:43 +0000790 "Sec-WebSocket-Key: ");
Andy Green08d33922011-02-26 10:22:49 +0000791 strcpy(p, wsi->key_b64);
792 p += strlen(wsi->key_b64);
793 p += sprintf(p, "\x0d\x0a");
794 if (wsi->c_origin)
795 p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
Andy Greenbe93fef2011-02-14 20:25:43 +0000796 wsi->c_origin);
Andy Green08d33922011-02-26 10:22:49 +0000797 if (wsi->c_protocol)
Andy Greenbe93fef2011-02-14 20:25:43 +0000798 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
799 wsi->c_protocol);
Andy Green08d33922011-02-26 10:22:49 +0000800 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a\x0d\x0a",
Andy Greenbe93fef2011-02-14 20:25:43 +0000801 wsi->ietf_spec_revision);
802
803 /* done with these now */
804
805 free(wsi->c_path);
806 free(wsi->c_host);
Andy Green08d33922011-02-26 10:22:49 +0000807 if (wsi->c_origin)
808 free(wsi->c_origin);
Andy Greenbe93fef2011-02-14 20:25:43 +0000809
810 /* prepare the expected server accept response */
811
812 strcpy((char *)buf, wsi->key_b64);
813 strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
814
815 SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
816
817 lws_b64_encode_string(hash, 20,
818 wsi->initial_handshake_hash_base64,
819 sizeof wsi->initial_handshake_hash_base64);
820
821 /* send our request to the server */
822
823 #ifdef LWS_OPENSSL_SUPPORT
824 if (wsi->use_ssl)
825 n = SSL_write(wsi->ssl, pkt, p - pkt);
826 else
827 #endif
828 n = send(wsi->sock, pkt, p - pkt, 0);
829
830 if (n < 0) {
831 fprintf(stderr, "ERROR writing to client socket\n");
Andy Green687b0182011-02-26 11:04:01 +0000832 libwebsocket_close_and_free_session(this, wsi,
833 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +0000834 return 1;
835 }
836
837 wsi->parser_state = WSI_TOKEN_NAME_PART;
838 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
839 libwebsocket_set_timeout(wsi,
840 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, 5);
841
842 break;
843
844 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
845
846 /* handle server hung up on us */
847
848 if (pollfd->revents & (POLLERR | POLLHUP)) {
849
850 fprintf(stderr, "Server connection %p (fd=%d) dead\n",
851 (void *)wsi, pollfd->fd);
852
853 goto bail3;
854 }
855
856
857 /* interpret the server response */
858
859 /*
860 * HTTP/1.1 101 Switching Protocols
861 * Upgrade: websocket
862 * Connection: Upgrade
863 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
864 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
865 * Sec-WebSocket-Protocol: chat
866 */
867
868 #ifdef LWS_OPENSSL_SUPPORT
869 if (wsi->use_ssl)
870 len = SSL_read(wsi->ssl, pkt, sizeof pkt);
871 else
872 #endif
873 len = recv(wsi->sock, pkt, sizeof pkt, 0);
874
875 if (len < 0) {
876 fprintf(stderr,
877 "libwebsocket_client_handshake read error\n");
878 goto bail3;
879 }
880
881 p = pkt;
882 for (n = 0; n < len; n++)
883 libwebsocket_parse(wsi, *p++);
884
885 if (wsi->parser_state != WSI_PARSING_COMPLETE) {
886 fprintf(stderr, "libwebsocket_client_handshake "
887 "server response ailed parsing\n");
888 goto bail3;
889 }
890
891 /*
892 * well, what the server sent looked reasonable for syntax.
893 * Now let's confirm it sent all the necessary headers
894 */
895
896 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
897 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
898 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
899 !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
900 !wsi->utf8_token[WSI_TOKEN_NONCE].token_len ||
901 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
902 wsi->c_protocol != NULL)) {
903 fprintf(stderr, "libwebsocket_client_handshake "
Andy Green687b0182011-02-26 11:04:01 +0000904 "missing required header(s)\n");
Andy Greenbe93fef2011-02-14 20:25:43 +0000905 pkt[len] = '\0';
906 fprintf(stderr, "%s", pkt);
907 goto bail3;
908 }
909
910 /*
911 * Everything seems to be there, now take a closer look at what
912 * is in each header
913 */
914
915 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
916 if (strcmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
917 "101 switching protocols")) {
918 fprintf(stderr, "libwebsocket_client_handshake "
919 "server sent bad HTTP response '%s'\n",
920 wsi->utf8_token[WSI_TOKEN_HTTP].token);
921 goto bail3;
922 }
923
924 strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
925 if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
926 "websocket")) {
927 fprintf(stderr, "libwebsocket_client_handshake server "
928 "sent bad Upgrade header '%s'\n",
929 wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
930 goto bail3;
931 }
932
933 strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
934 if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
935 "upgrade")) {
936 fprintf(stderr, "libwebsocket_client_handshake server "
937 "sent bad Connection hdr '%s'\n",
938 wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
939 goto bail3;
940 }
941
942
943 pc = wsi->c_protocol;
944
945 /*
946 * confirm the protocol the server wants to talk was in the list
947 * of protocols we offered
948 */
949
950 if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
951
952 /*
953 * no protocol name to work from,
954 * default to first protocol
955 */
956 wsi->protocol = &this->protocols[0];
957
958 free(wsi->c_protocol);
959
960 goto check_accept;
961 }
962
963 while (*pc && !okay) {
964 if ((!strncmp(pc,
965 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
966 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
967 (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
968 pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
969 okay = 1;
970 continue;
971 }
972 while (*pc && *pc != ',')
973 pc++;
974 while (*pc && *pc != ' ')
975 pc++;
976 }
977
978 /* done with him now */
979
980 if (wsi->c_protocol)
981 free(wsi->c_protocol);
982
983
984 if (!okay) {
985 fprintf(stderr, "libwebsocket_client_handshake server "
986 "sent bad protocol '%s'\n",
987 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
988 goto bail2;
989 }
990
991 /*
992 * identify the selected protocol struct and set it
993 */
994 n = 0;
995 wsi->protocol = NULL;
996 while (this->protocols[n].callback) {
997 if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
998 this->protocols[n].name) == 0)
999 wsi->protocol = &this->protocols[n];
1000 n++;
1001 }
1002
1003 if (wsi->protocol == NULL) {
1004 fprintf(stderr, "libwebsocket_client_handshake server "
1005 "requested protocol '%s', which we "
1006 "said we supported but we don't!\n",
1007 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
1008 goto bail2;
1009 }
1010
1011 check_accept:
1012 /*
1013 * Confirm his accept token is the one we precomputed
1014 */
1015
1016 if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
1017 wsi->initial_handshake_hash_base64)) {
1018 fprintf(stderr, "libwebsocket_client_handshake server "
1019 "sent bad ACCEPT '%s' vs computed '%s'\n",
1020 wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
1021 wsi->initial_handshake_hash_base64);
1022 goto bail2;
1023 }
1024
1025 /*
1026 * Calculate the masking key to use when sending data to server
1027 */
1028
1029 strcpy((char *)buf, wsi->key_b64);
1030 p = (char *)buf + strlen(wsi->key_b64);
1031 strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
1032 p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
1033 strcpy(p, magic_websocket_04_masking_guid);
1034 SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
1035
1036 /* allocate the per-connection user memory (if any) */
1037
1038 if (wsi->protocol->per_session_data_size) {
1039 wsi->user_space = malloc(
1040 wsi->protocol->per_session_data_size);
1041 if (wsi->user_space == NULL) {
1042 fprintf(stderr, "Out of memory for "
1043 "conn user space\n");
1044 goto bail2;
1045 }
1046 } else
1047 wsi->user_space = NULL;
1048
1049 /* clear his proxy connection timeout */
1050
1051 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1052
1053 /* mark him as being alive */
1054
1055 wsi->state = WSI_STATE_ESTABLISHED;
1056 wsi->mode = LWS_CONNMODE_WS_CLIENT;
1057
1058 fprintf(stderr, "handshake OK for protocol %s\n",
1059 wsi->protocol->name);
1060
1061 /* call him back to inform him he is up */
1062
1063 wsi->protocol->callback(this, wsi,
1064 LWS_CALLBACK_CLIENT_ESTABLISHED,
1065 wsi->user_space,
1066 NULL, 0);
1067
1068 break;
1069
1070bail3:
1071 if (wsi->c_protocol)
1072 free(wsi->c_protocol);
1073
1074bail2:
Andy Green687b0182011-02-26 11:04:01 +00001075 libwebsocket_close_and_free_session(this, wsi,
1076 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenbe93fef2011-02-14 20:25:43 +00001077 return 1;
1078
1079
Andy Green0d338332011-02-12 11:57:43 +00001080 case LWS_CONNMODE_WS_SERVING:
1081 case LWS_CONNMODE_WS_CLIENT:
1082
1083 /* handle session socket closed */
1084
1085 if (pollfd->revents & (POLLERR | POLLHUP)) {
1086
Andy Green62c54d22011-02-14 09:14:25 +00001087 fprintf(stderr, "Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +00001088 (void *)wsi, pollfd->fd);
1089
Andy Green687b0182011-02-26 11:04:01 +00001090 libwebsocket_close_and_free_session(this, wsi,
1091 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green4b6fbe12011-02-14 08:03:48 +00001092 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +00001093 }
1094
Andy Green0d338332011-02-12 11:57:43 +00001095 /* the guy requested a callback when it was OK to write */
1096
1097 if (pollfd->revents & POLLOUT) {
1098
1099 pollfd->events &= ~POLLOUT;
1100
Andy Green3221f922011-02-12 13:14:11 +00001101 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +00001102 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001103 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1104 (void *)(long)wsi->sock, NULL, POLLOUT);
1105
Andy Green62c54d22011-02-14 09:14:25 +00001106 wsi->protocol->callback(this, wsi,
Andy Green0d338332011-02-12 11:57:43 +00001107 LWS_CALLBACK_CLIENT_WRITEABLE,
1108 wsi->user_space,
1109 NULL, 0);
1110 }
1111
1112 /* any incoming data ready? */
1113
1114 if (!(pollfd->revents & POLLIN))
1115 break;
1116
Andy Greenb45993c2010-12-18 15:13:50 +00001117#ifdef LWS_OPENSSL_SUPPORT
Andy Green0d338332011-02-12 11:57:43 +00001118 if (wsi->ssl)
1119 n = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Greenb45993c2010-12-18 15:13:50 +00001120 else
1121#endif
Andy Green0d338332011-02-12 11:57:43 +00001122 n = recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001123
1124 if (n < 0) {
1125 fprintf(stderr, "Socket read returned %d\n", n);
Andy Green4b6fbe12011-02-14 08:03:48 +00001126 break;
Andy Greenb45993c2010-12-18 15:13:50 +00001127 }
1128 if (!n) {
Andy Green687b0182011-02-26 11:04:01 +00001129 libwebsocket_close_and_free_session(this, wsi,
1130 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green4b6fbe12011-02-14 08:03:48 +00001131 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +00001132 }
1133
Andy Greenb45993c2010-12-18 15:13:50 +00001134 /* service incoming data */
1135
Andy Green4b6fbe12011-02-14 08:03:48 +00001136 n = libwebsocket_read(this, wsi, buf, n);
Andy Green6964bb52011-01-23 16:50:33 +00001137 if (n >= 0)
Andy Green4b6fbe12011-02-14 08:03:48 +00001138 break;
Andy Greenb45993c2010-12-18 15:13:50 +00001139
Andy Green4b6fbe12011-02-14 08:03:48 +00001140 /* we closed wsi */
Andy Green0d338332011-02-12 11:57:43 +00001141
Andy Green4b6fbe12011-02-14 08:03:48 +00001142 return 1;
Andy Greenb45993c2010-12-18 15:13:50 +00001143 }
1144
1145 return 0;
1146}
1147
Andy Green0d338332011-02-12 11:57:43 +00001148
Andy Green6964bb52011-01-23 16:50:33 +00001149/**
1150 * libwebsocket_context_destroy() - Destroy the websocket context
1151 * @this: Websocket context
1152 *
1153 * This function closes any active connections and then frees the
1154 * context. After calling this, any further use of the context is
1155 * undefined.
1156 */
1157void
1158libwebsocket_context_destroy(struct libwebsocket_context *this)
1159{
Andy Green0d338332011-02-12 11:57:43 +00001160 int n;
1161 int m;
1162 struct libwebsocket *wsi;
Andy Green6964bb52011-01-23 16:50:33 +00001163
Andy Green4b6fbe12011-02-14 08:03:48 +00001164 for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
Andy Green0d338332011-02-12 11:57:43 +00001165 for (m = 0; m < this->fd_hashtable[n].length; m++) {
Andy Green0d338332011-02-12 11:57:43 +00001166 wsi = this->fd_hashtable[n].wsi[m];
Andy Green687b0182011-02-26 11:04:01 +00001167 libwebsocket_close_and_free_session(this, wsi,
1168 LWS_CLOSE_STATUS_GOINGAWAY);
Andy Greenf3d3b402011-02-09 07:16:34 +00001169 }
Andy Green6964bb52011-01-23 16:50:33 +00001170
Andy Green44eee682011-02-10 09:32:24 +00001171 close(this->fd_random);
1172
Andy Green6964bb52011-01-23 16:50:33 +00001173#ifdef LWS_OPENSSL_SUPPORT
Andy Green44eee682011-02-10 09:32:24 +00001174 if (this->ssl_ctx)
Andy Green90c7cbc2011-01-27 06:26:52 +00001175 SSL_CTX_free(this->ssl_ctx);
Andy Green44eee682011-02-10 09:32:24 +00001176 if (this->ssl_client_ctx)
Andy Green5e1fa172011-02-10 09:07:05 +00001177 SSL_CTX_free(this->ssl_client_ctx);
Andy Green6964bb52011-01-23 16:50:33 +00001178#endif
1179
Andy Green44eee682011-02-10 09:32:24 +00001180 free(this);
Andy Green6964bb52011-01-23 16:50:33 +00001181}
1182
1183/**
1184 * libwebsocket_service() - Service any pending websocket activity
1185 * @this: Websocket context
1186 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1187 * service otherwise block and service immediately, returning
1188 * after the timeout if nothing needed service.
1189 *
1190 * This function deals with any pending websocket traffic, for three
1191 * kinds of event. It handles these events on both server and client
1192 * types of connection the same.
1193 *
1194 * 1) Accept new connections to our context's server
1195 *
1196 * 2) Perform pending broadcast writes initiated from other forked
1197 * processes (effectively serializing asynchronous broadcasts)
1198 *
1199 * 3) Call the receive callback for incoming frame data received by
1200 * server or client connections.
1201 *
1202 * You need to call this service function periodically to all the above
1203 * functions to happen; if your application is single-threaded you can
1204 * just call it in your main event loop.
1205 *
1206 * Alternatively you can fork a new process that asynchronously handles
1207 * calling this service in a loop. In that case you are happy if this
1208 * call blocks your thread until it needs to take care of something and
1209 * would call it with a large nonzero timeout. Your loop then takes no
1210 * CPU while there is nothing happening.
1211 *
1212 * If you are calling it in a single-threaded app, you don't want it to
1213 * wait around blocking other things in your loop from happening, so you
1214 * would call it with a timeout_ms of 0, so it returns immediately if
1215 * nothing is pending, or as soon as it services whatever was pending.
1216 */
1217
Andy Greenb45993c2010-12-18 15:13:50 +00001218
Andy Greene92cd172011-01-19 13:11:55 +00001219int
1220libwebsocket_service(struct libwebsocket_context *this, int timeout_ms)
1221{
1222 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001223
1224 /* stay dead once we are dead */
1225
1226 if (this == NULL)
1227 return 1;
1228
Andy Green0d338332011-02-12 11:57:43 +00001229 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001230
Andy Green0d338332011-02-12 11:57:43 +00001231 n = poll(this->fds, this->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001232 if (n == 0) /* poll timeout */
1233 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001234
Andy Green62c54d22011-02-14 09:14:25 +00001235 if (n < 0) {
Andy Green5e1fa172011-02-10 09:07:05 +00001236 /*
Andy Greene92cd172011-01-19 13:11:55 +00001237 fprintf(stderr, "Listen Socket dead\n");
Andy Green5e1fa172011-02-10 09:07:05 +00001238 */
Andy Green0d338332011-02-12 11:57:43 +00001239 return 1;
Andy Greene92cd172011-01-19 13:11:55 +00001240 }
Andy Greene92cd172011-01-19 13:11:55 +00001241
1242 /* handle accept on listening socket? */
1243
Andy Green0d338332011-02-12 11:57:43 +00001244 for (n = 0; n < this->fds_count; n++)
1245 if (this->fds[n].revents)
1246 libwebsocket_service_fd(this, &this->fds[n]);
Andy Greene92cd172011-01-19 13:11:55 +00001247
1248 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001249}
1250
Andy Green90c7cbc2011-01-27 06:26:52 +00001251/**
1252 * libwebsocket_callback_on_writable() - Request a callback when this socket
1253 * becomes able to be written to without
1254 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001255 *
1256 * @this: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001257 * @wsi: Websocket connection instance to get callback for
1258 */
1259
1260int
Andy Green62c54d22011-02-14 09:14:25 +00001261libwebsocket_callback_on_writable(struct libwebsocket_context *this,
1262 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001263{
Andy Green90c7cbc2011-01-27 06:26:52 +00001264 int n;
1265
Andy Green0d338332011-02-12 11:57:43 +00001266 for (n = 0; n < this->fds_count; n++)
1267 if (this->fds[n].fd == wsi->sock) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001268 this->fds[n].events |= POLLOUT;
Andy Green3221f922011-02-12 13:14:11 +00001269 n = this->fds_count;
Andy Green90c7cbc2011-01-27 06:26:52 +00001270 }
1271
Andy Green3221f922011-02-12 13:14:11 +00001272 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +00001273 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001274 LWS_CALLBACK_SET_MODE_POLL_FD,
1275 (void *)(long)wsi->sock, NULL, POLLOUT);
1276
Andy Green90c7cbc2011-01-27 06:26:52 +00001277 return 1;
1278}
1279
1280/**
1281 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1282 * all connections using the given protocol when it
1283 * becomes possible to write to each socket without
1284 * blocking in turn.
1285 *
1286 * @protocol: Protocol whose connections will get callbacks
1287 */
1288
1289int
1290libwebsocket_callback_on_writable_all_protocol(
1291 const struct libwebsocket_protocols *protocol)
1292{
1293 struct libwebsocket_context *this = protocol->owning_server;
1294 int n;
Andy Green0d338332011-02-12 11:57:43 +00001295 int m;
1296 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001297
Andy Green0d338332011-02-12 11:57:43 +00001298 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
1299
1300 for (m = 0; m < this->fd_hashtable[n].length; m++) {
1301
1302 wsi = this->fd_hashtable[n].wsi[m];
1303
1304 if (wsi->protocol == protocol)
Andy Green62c54d22011-02-14 09:14:25 +00001305 libwebsocket_callback_on_writable(this, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001306 }
1307 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001308
1309 return 0;
1310}
1311
Andy Greenbe93fef2011-02-14 20:25:43 +00001312/**
1313 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1314 *
1315 * You will not need this unless you are doing something special
1316 *
1317 * @wsi: Websocket connection instance
1318 * @reason: timeout reason
1319 * @secs: how many seconds
1320 */
1321
1322void
1323libwebsocket_set_timeout(struct libwebsocket *wsi,
1324 enum pending_timeout reason, int secs)
1325{
1326 struct timeval tv;
1327
1328 gettimeofday(&tv, NULL);
1329
1330 wsi->pending_timeout_limit = tv.tv_sec + secs;
1331 wsi->pending_timeout = reason;
1332}
1333
Andy Greena6cbece2011-01-27 20:06:03 +00001334
1335/**
1336 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1337 *
1338 * You will not need this unless you are doing something special
1339 *
1340 * @wsi: Websocket connection instance
1341 */
1342
1343int
1344libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1345{
1346 return wsi->sock;
1347}
1348
Andy Green90c7cbc2011-01-27 06:26:52 +00001349/**
1350 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1351 * receieved packets.
1352 *
1353 * If the output side of a server process becomes choked, this allows flow
1354 * control for the input side.
1355 *
1356 * @wsi: Websocket connection instance to get callback for
1357 * @enable: 0 = disable read servicing for this connection, 1 = enable
1358 */
1359
1360int
1361libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1362{
1363 struct libwebsocket_context *this = wsi->protocol->owning_server;
1364 int n;
1365
Andy Green0d338332011-02-12 11:57:43 +00001366 for (n = 0; n < this->fds_count; n++)
1367 if (this->fds[n].fd == wsi->sock) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001368 if (enable)
1369 this->fds[n].events |= POLLIN;
1370 else
1371 this->fds[n].events &= ~POLLIN;
1372
1373 return 0;
1374 }
1375
Andy Green3221f922011-02-12 13:14:11 +00001376 if (enable)
1377 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +00001378 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001379 LWS_CALLBACK_SET_MODE_POLL_FD,
1380 (void *)(long)wsi->sock, NULL, POLLIN);
1381 else
1382 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +00001383 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001384 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1385 (void *)(long)wsi->sock, NULL, POLLIN);
1386
1387
Andy Green90c7cbc2011-01-27 06:26:52 +00001388 fprintf(stderr, "libwebsocket_callback_on_writable "
1389 "unable to find socket\n");
1390 return 1;
1391}
1392
Andy Green2ac5a6f2011-01-28 10:00:18 +00001393/**
1394 * libwebsocket_canonical_hostname() - returns this host's hostname
1395 *
1396 * This is typically used by client code to fill in the host parameter
1397 * when making a client connection. You can only call it after the context
1398 * has been created.
1399 *
1400 * @this: Websocket context
1401 */
1402
1403
1404extern const char *
1405libwebsocket_canonical_hostname(struct libwebsocket_context *this)
1406{
1407 return (const char *)this->canonical_hostname;
1408}
1409
1410
Andy Green90c7cbc2011-01-27 06:26:52 +00001411static void sigpipe_handler(int x)
1412{
1413}
1414
Andy Green6901cb32011-02-21 08:06:47 +00001415#ifdef LWS_OPENSSL_SUPPORT
1416static int
1417OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1418{
1419
1420 SSL *ssl;
1421 int n;
1422// struct libwebsocket_context *this;
1423
1424 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1425 SSL_get_ex_data_X509_STORE_CTX_idx());
1426
1427 /*
1428 * !!! can't get this->openssl_websocket_private_data_index
1429 * can't store as a static either
1430 */
1431// this = SSL_get_ex_data(ssl, this->openssl_websocket_private_data_index);
1432
1433 n = this->protocols[0].callback(NULL, NULL,
1434 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1435 x509_ctx, ssl, preverify_ok);
1436
1437 /* convert return code from 0 = OK to 1 = OK */
1438
1439 if (!n)
1440 n = 1;
1441 else
1442 n = 0;
1443
1444 return n;
1445}
1446#endif
1447
Andy Greenb45993c2010-12-18 15:13:50 +00001448
Andy Greenab990e42010-10-31 12:42:52 +00001449/**
Andy Green4739e5c2011-01-22 12:51:57 +00001450 * libwebsocket_create_context() - Create the websocket handler
1451 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00001452 * any port, that's what you want if you are not running a
1453 * websocket server at all but just using it as a client
Andy Green32375b72011-02-19 08:32:53 +00001454 * @interface: NULL to bind the listen socket to all interfaces, or the
1455 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00001456 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00001457 * specific callback for each one. The list is ended with an
1458 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00001459 * It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +00001460 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00001461 * to listen using SSL, set to the filepath to fetch the
1462 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00001463 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00001464 * else ignored
Andy Green3faa9c72010-11-08 17:03:03 +00001465 * @gid: group id to change to after setting listen socket, or -1.
1466 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00001467 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green05464c62010-11-12 10:44:18 +00001468 *
Andy Green8f037e42010-12-19 22:13:26 +00001469 * This function creates the listening socket and takes care
1470 * of all initialization in one step.
1471 *
Andy Greene92cd172011-01-19 13:11:55 +00001472 * After initialization, it returns a struct libwebsocket_context * that
1473 * represents this server. After calling, user code needs to take care
1474 * of calling libwebsocket_service() with the context pointer to get the
1475 * server's sockets serviced. This can be done in the same process context
1476 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001477 *
Andy Green8f037e42010-12-19 22:13:26 +00001478 * The protocol callback functions are called for a handful of events
1479 * including http requests coming in, websocket connections becoming
1480 * established, and data arriving; it's also called periodically to allow
1481 * async transmission.
1482 *
1483 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1484 * at that time websocket protocol has not been negotiated. Other
1485 * protocols after the first one never see any HTTP callack activity.
1486 *
1487 * The server created is a simple http server by default; part of the
1488 * websocket standard is upgrading this http connection to a websocket one.
1489 *
1490 * This allows the same server to provide files like scripts and favicon /
1491 * images or whatever over http and dynamic data over websockets all in
1492 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001493 */
Andy Green4ea60062010-10-30 12:15:07 +01001494
Andy Greene92cd172011-01-19 13:11:55 +00001495struct libwebsocket_context *
Andy Green32375b72011-02-19 08:32:53 +00001496libwebsocket_create_context(int port, const char *interface,
Andy Greenb45993c2010-12-18 15:13:50 +00001497 struct libwebsocket_protocols *protocols,
Andy Green8f037e42010-12-19 22:13:26 +00001498 const char *ssl_cert_filepath,
1499 const char *ssl_private_key_filepath,
Andy Green8014b292011-01-30 20:57:25 +00001500 int gid, int uid, unsigned int options)
Andy Greenff95d7a2010-10-28 22:36:01 +01001501{
1502 int n;
Andy Green4739e5c2011-01-22 12:51:57 +00001503 int sockfd = 0;
Andy Green251f6fa2010-11-03 11:13:06 +00001504 int fd;
Andy Greenff95d7a2010-10-28 22:36:01 +01001505 struct sockaddr_in serv_addr, cli_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00001506 int opt = 1;
Andy Green8f037e42010-12-19 22:13:26 +00001507 struct libwebsocket_context *this = NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001508 unsigned int slen;
Andy Green9659f372011-01-27 22:01:43 +00001509 char *p;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001510 char hostname[1024];
Andy Green42f69142011-01-30 08:10:02 +00001511 struct hostent *he;
Andy Green0d338332011-02-12 11:57:43 +00001512 struct libwebsocket *wsi;
Andy Greenff95d7a2010-10-28 22:36:01 +01001513
Andy Green3faa9c72010-11-08 17:03:03 +00001514#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001515 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001516 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001517#endif
1518
Andy Green90c7cbc2011-01-27 06:26:52 +00001519 this = malloc(sizeof(struct libwebsocket_context));
1520 if (!this) {
1521 fprintf(stderr, "No memory for websocket context\n");
1522 return NULL;
1523 }
1524 this->protocols = protocols;
1525 this->listen_port = port;
Andy Green9659f372011-01-27 22:01:43 +00001526 this->http_proxy_port = 0;
1527 this->http_proxy_address[0] = '\0';
Andy Green8014b292011-01-30 20:57:25 +00001528 this->options = options;
Andy Green0d338332011-02-12 11:57:43 +00001529 this->fds_count = 0;
Andy Green9659f372011-01-27 22:01:43 +00001530
Andy Green44eee682011-02-10 09:32:24 +00001531 this->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1532 if (this->fd_random < 0) {
1533 fprintf(stderr, "Unable to open random device %s %d\n",
1534 SYSTEM_RANDOM_FILEPATH, this->fd_random);
1535 return NULL;
1536 }
1537
Andy Green2ac5a6f2011-01-28 10:00:18 +00001538 /* find canonical hostname */
1539
1540 hostname[(sizeof hostname) - 1] = '\0';
1541 gethostname(hostname, (sizeof hostname) - 1);
1542 he = gethostbyname(hostname);
Darin Willitsc19456f2011-02-14 17:52:39 +00001543 if (he) {
1544 strncpy(this->canonical_hostname, he->h_name,
Andy Green2ac5a6f2011-01-28 10:00:18 +00001545 sizeof this->canonical_hostname - 1);
Darin Willitsc19456f2011-02-14 17:52:39 +00001546 this->canonical_hostname[sizeof this->canonical_hostname - 1] =
1547 '\0';
1548 } else
1549 strncpy(this->canonical_hostname, hostname,
1550 sizeof this->canonical_hostname - 1);
Andy Green2ac5a6f2011-01-28 10:00:18 +00001551
Andy Green9659f372011-01-27 22:01:43 +00001552 /* split the proxy ads:port if given */
1553
1554 p = getenv("http_proxy");
1555 if (p) {
1556 strncpy(this->http_proxy_address, p,
1557 sizeof this->http_proxy_address - 1);
1558 this->http_proxy_address[
1559 sizeof this->http_proxy_address - 1] = '\0';
1560
1561 p = strchr(this->http_proxy_address, ':');
1562 if (p == NULL) {
1563 fprintf(stderr, "http_proxy needs to be ads:port\n");
1564 return NULL;
1565 }
1566 *p = '\0';
1567 this->http_proxy_port = atoi(p + 1);
1568
1569 fprintf(stderr, "Using proxy %s:%u\n",
1570 this->http_proxy_address,
1571 this->http_proxy_port);
1572 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001573
1574 if (port) {
1575
Andy Green3faa9c72010-11-08 17:03:03 +00001576#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +00001577 this->use_ssl = ssl_cert_filepath != NULL &&
1578 ssl_private_key_filepath != NULL;
1579 if (this->use_ssl)
1580 fprintf(stderr, " Compiled with SSL support, "
1581 "using it\n");
1582 else
1583 fprintf(stderr, " Compiled with SSL support, "
1584 "not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001585
Andy Green90c7cbc2011-01-27 06:26:52 +00001586#else
1587 if (ssl_cert_filepath != NULL &&
1588 ssl_private_key_filepath != NULL) {
1589 fprintf(stderr, " Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00001590 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001591 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001592 fprintf(stderr, " Compiled without SSL support, "
1593 "serving unencrypted\n");
1594#endif
1595 }
1596
1597 /* ignore SIGPIPE */
1598
1599 signal(SIGPIPE, sigpipe_handler);
1600
1601
1602#ifdef LWS_OPENSSL_SUPPORT
1603
1604 /* basic openssl init */
1605
1606 SSL_library_init();
1607
1608 OpenSSL_add_all_algorithms();
1609 SSL_load_error_strings();
1610
Andy Green6901cb32011-02-21 08:06:47 +00001611 this->openssl_websocket_private_data_index =
1612 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1613
Andy Green90c7cbc2011-01-27 06:26:52 +00001614 /*
1615 * Firefox insists on SSLv23 not SSLv3
1616 * Konq disables SSLv2 by default now, SSLv23 works
1617 */
1618
1619 method = (SSL_METHOD *)SSLv23_server_method();
1620 if (!method) {
1621 fprintf(stderr, "problem creating ssl method: %s\n",
1622 ERR_error_string(ERR_get_error(), ssl_err_buf));
1623 return NULL;
1624 }
1625 this->ssl_ctx = SSL_CTX_new(method); /* create context */
1626 if (!this->ssl_ctx) {
1627 fprintf(stderr, "problem creating ssl context: %s\n",
1628 ERR_error_string(ERR_get_error(), ssl_err_buf));
1629 return NULL;
1630 }
1631
1632 /* client context */
1633
1634 method = (SSL_METHOD *)SSLv23_client_method();
1635 if (!method) {
1636 fprintf(stderr, "problem creating ssl method: %s\n",
1637 ERR_error_string(ERR_get_error(), ssl_err_buf));
1638 return NULL;
1639 }
1640 this->ssl_client_ctx = SSL_CTX_new(method); /* create context */
1641 if (!this->ssl_client_ctx) {
1642 fprintf(stderr, "problem creating ssl context: %s\n",
1643 ERR_error_string(ERR_get_error(), ssl_err_buf));
1644 return NULL;
1645 }
1646
1647
1648 /* openssl init for cert verification (used with client sockets) */
1649
1650 if (!SSL_CTX_load_verify_locations(this->ssl_client_ctx, NULL,
1651 LWS_OPENSSL_CLIENT_CERTS)) {
1652 fprintf(stderr, "Unable to load SSL Client certs from %s "
1653 "(set by --with-client-cert-dir= in configure) -- "
1654 " client ssl isn't going to work",
1655 LWS_OPENSSL_CLIENT_CERTS);
1656 }
1657
Andy Green0894bda2011-02-19 09:09:11 +00001658 /*
1659 * callback allowing user code to load extra verification certs
1660 * helping the client to verify server identity
1661 */
1662
Andy Greenc6bf2c22011-02-20 11:10:47 +00001663 this->protocols[0].callback(this, NULL,
Andy Green0894bda2011-02-19 09:09:11 +00001664 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1665 this->ssl_client_ctx, NULL, 0);
1666
Andy Greenc6bf2c22011-02-20 11:10:47 +00001667 /* as a server, are we requiring clients to identify themselves? */
1668
1669 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
1670
1671 /* absolutely require the client cert */
1672
1673 SSL_CTX_set_verify(this->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001674 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1675 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001676
1677 /*
1678 * give user code a chance to load certs into the server
1679 * allowing it to verify incoming client certs
1680 */
1681
1682 this->protocols[0].callback(this, NULL,
1683 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
1684 this->ssl_ctx, NULL, 0);
1685 }
1686
Andy Green90c7cbc2011-01-27 06:26:52 +00001687 if (this->use_ssl) {
1688
1689 /* openssl init for server sockets */
1690
Andy Green3faa9c72010-11-08 17:03:03 +00001691 /* set the local certificate from CertFile */
Andy Green90c7cbc2011-01-27 06:26:52 +00001692 n = SSL_CTX_use_certificate_file(this->ssl_ctx,
Andy Green3faa9c72010-11-08 17:03:03 +00001693 ssl_cert_filepath, SSL_FILETYPE_PEM);
1694 if (n != 1) {
1695 fprintf(stderr, "problem getting cert '%s': %s\n",
1696 ssl_cert_filepath,
1697 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001698 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001699 }
1700 /* set the private key from KeyFile */
Andy Green90c7cbc2011-01-27 06:26:52 +00001701 if (SSL_CTX_use_PrivateKey_file(this->ssl_ctx,
Andy Green018d8eb2010-11-08 21:04:23 +00001702 ssl_private_key_filepath,
Andy Green4739e5c2011-01-22 12:51:57 +00001703 SSL_FILETYPE_PEM) != 1) {
Andy Green018d8eb2010-11-08 21:04:23 +00001704 fprintf(stderr, "ssl problem getting key '%s': %s\n",
1705 ssl_private_key_filepath,
1706 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001707 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001708 }
1709 /* verify private key */
Andy Green90c7cbc2011-01-27 06:26:52 +00001710 if (!SSL_CTX_check_private_key(this->ssl_ctx)) {
Andy Green018d8eb2010-11-08 21:04:23 +00001711 fprintf(stderr, "Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00001712 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001713 }
1714
1715 /* SSL is happy and has a cert it's content with */
1716 }
1717#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001718
Andy Greendf736162011-01-18 15:39:02 +00001719 /* selftest */
1720
1721 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00001722 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00001723
Andy Green0d338332011-02-12 11:57:43 +00001724 /* fd hashtable init */
1725
1726 for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
1727 this->fd_hashtable[n].length = 0;
1728
Andy Greenb45993c2010-12-18 15:13:50 +00001729 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001730
Andy Green4739e5c2011-01-22 12:51:57 +00001731 if (port) {
Andy Green8f037e42010-12-19 22:13:26 +00001732
Andy Green4739e5c2011-01-22 12:51:57 +00001733 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1734 if (sockfd < 0) {
1735 fprintf(stderr, "ERROR opening socket");
1736 return NULL;
1737 }
Andy Green775c0dd2010-10-29 14:15:22 +01001738
Andy Green4739e5c2011-01-22 12:51:57 +00001739 /* allow us to restart even if old sockets in TIME_WAIT */
1740 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
Andy Greene77ddd82010-11-13 10:03:47 +00001741
Andy Green4739e5c2011-01-22 12:51:57 +00001742 bzero((char *) &serv_addr, sizeof(serv_addr));
1743 serv_addr.sin_family = AF_INET;
Andy Green32375b72011-02-19 08:32:53 +00001744 if (interface == NULL)
1745 serv_addr.sin_addr.s_addr = INADDR_ANY;
1746 else
1747 interface_to_sa(interface, &serv_addr,
1748 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00001749 serv_addr.sin_port = htons(port);
1750
1751 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1752 sizeof(serv_addr));
1753 if (n < 0) {
1754 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00001755 port, n, errno);
Andy Green4739e5c2011-01-22 12:51:57 +00001756 return NULL;
1757 }
Andy Green0d338332011-02-12 11:57:43 +00001758
1759 wsi = malloc(sizeof(struct libwebsocket));
1760 memset(wsi, 0, sizeof (struct libwebsocket));
1761 wsi->sock = sockfd;
1762 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
1763 insert_wsi(this, wsi);
1764
1765 listen(sockfd, 5);
1766 fprintf(stderr, " Listening on port %d\n", port);
1767
1768 /* list in the internal poll array */
1769
1770 this->fds[this->fds_count].fd = sockfd;
1771 this->fds[this->fds_count++].events = POLLIN;
Andy Green3221f922011-02-12 13:14:11 +00001772
1773 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +00001774 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001775 LWS_CALLBACK_ADD_POLL_FD,
1776 (void *)(long)sockfd, NULL, POLLIN);
1777
Andy Green8f037e42010-12-19 22:13:26 +00001778 }
Andy Greenb45993c2010-12-18 15:13:50 +00001779
Andy Greene77ddd82010-11-13 10:03:47 +00001780 /* drop any root privs for this process */
Andy Green3faa9c72010-11-08 17:03:03 +00001781
1782 if (gid != -1)
1783 if (setgid(gid))
1784 fprintf(stderr, "setgid: %s\n", strerror(errno));
1785 if (uid != -1)
1786 if (setuid(uid))
1787 fprintf(stderr, "setuid: %s\n", strerror(errno));
1788
Andy Greenb45993c2010-12-18 15:13:50 +00001789
1790 /* set up our internal broadcast trigger sockets per-protocol */
1791
Andy Green0d338332011-02-12 11:57:43 +00001792 for (this->count_protocols = 0;
1793 protocols[this->count_protocols].callback;
Andy Greenb45993c2010-12-18 15:13:50 +00001794 this->count_protocols++) {
1795 protocols[this->count_protocols].owning_server = this;
1796 protocols[this->count_protocols].protocol_index =
1797 this->count_protocols;
1798
1799 fd = socket(AF_INET, SOCK_STREAM, 0);
1800 if (fd < 0) {
1801 fprintf(stderr, "ERROR opening socket");
Andy Greene92cd172011-01-19 13:11:55 +00001802 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001803 }
Andy Green8f037e42010-12-19 22:13:26 +00001804
Andy Greenb45993c2010-12-18 15:13:50 +00001805 /* allow us to restart even if old sockets in TIME_WAIT */
1806 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
1807
1808 bzero((char *) &serv_addr, sizeof(serv_addr));
1809 serv_addr.sin_family = AF_INET;
1810 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1811 serv_addr.sin_port = 0; /* pick the port for us */
1812
1813 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
1814 if (n < 0) {
Andy Green8f037e42010-12-19 22:13:26 +00001815 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
Andy Greenb45993c2010-12-18 15:13:50 +00001816 port, n, errno);
Andy Greene92cd172011-01-19 13:11:55 +00001817 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001818 }
1819
1820 slen = sizeof cli_addr;
1821 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
1822 if (n < 0) {
1823 fprintf(stderr, "getsockname failed\n");
Andy Greene92cd172011-01-19 13:11:55 +00001824 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001825 }
1826 protocols[this->count_protocols].broadcast_socket_port =
1827 ntohs(cli_addr.sin_port);
1828 listen(fd, 5);
1829
1830 debug(" Protocol %s broadcast socket %d\n",
1831 protocols[this->count_protocols].name,
1832 ntohs(cli_addr.sin_port));
1833
Andy Green0d338332011-02-12 11:57:43 +00001834 /* dummy wsi per broadcast proxy socket */
1835
1836 wsi = malloc(sizeof(struct libwebsocket));
1837 memset(wsi, 0, sizeof (struct libwebsocket));
1838 wsi->sock = fd;
1839 wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
1840 /* note which protocol we are proxying */
1841 wsi->protocol_index_for_broadcast_proxy = this->count_protocols;
1842 insert_wsi(this, wsi);
1843
1844 /* list in internal poll array */
1845
Andy Greenb45993c2010-12-18 15:13:50 +00001846 this->fds[this->fds_count].fd = fd;
1847 this->fds[this->fds_count].events = POLLIN;
Andy Green3221f922011-02-12 13:14:11 +00001848 this->fds[this->fds_count].revents = 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001849 this->fds_count++;
Andy Green3221f922011-02-12 13:14:11 +00001850
1851 /* external POLL support via protocol 0 */
Andy Green62c54d22011-02-14 09:14:25 +00001852 this->protocols[0].callback(this, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001853 LWS_CALLBACK_ADD_POLL_FD,
1854 (void *)(long)fd, NULL, POLLIN);
Andy Greenb45993c2010-12-18 15:13:50 +00001855 }
1856
Andy Greene92cd172011-01-19 13:11:55 +00001857 return this;
1858}
Andy Greenb45993c2010-12-18 15:13:50 +00001859
Andy Green4739e5c2011-01-22 12:51:57 +00001860
Andy Greened11a022011-01-20 10:23:50 +00001861#ifndef LWS_NO_FORK
1862
Andy Greene92cd172011-01-19 13:11:55 +00001863/**
1864 * libwebsockets_fork_service_loop() - Optional helper function forks off
1865 * a process for the websocket server loop.
Andy Green6964bb52011-01-23 16:50:33 +00001866 * You don't have to use this but if not, you
1867 * have to make sure you are calling
1868 * libwebsocket_service periodically to service
1869 * the websocket traffic
Andy Greene92cd172011-01-19 13:11:55 +00001870 * @this: server context returned by creation function
1871 */
Andy Greenb45993c2010-12-18 15:13:50 +00001872
Andy Greene92cd172011-01-19 13:11:55 +00001873int
1874libwebsockets_fork_service_loop(struct libwebsocket_context *this)
1875{
Andy Greene92cd172011-01-19 13:11:55 +00001876 int fd;
1877 struct sockaddr_in cli_addr;
1878 int n;
Andy Green3221f922011-02-12 13:14:11 +00001879 int p;
Andy Greenb45993c2010-12-18 15:13:50 +00001880
Andy Greened11a022011-01-20 10:23:50 +00001881 n = fork();
1882 if (n < 0)
1883 return n;
1884
1885 if (!n) {
1886
1887 /* main process context */
1888
Andy Green3221f922011-02-12 13:14:11 +00001889 /*
1890 * set up the proxy sockets to allow broadcast from
1891 * service process context
1892 */
1893
1894 for (p = 0; p < this->count_protocols; p++) {
Andy Greened11a022011-01-20 10:23:50 +00001895 fd = socket(AF_INET, SOCK_STREAM, 0);
1896 if (fd < 0) {
1897 fprintf(stderr, "Unable to create socket\n");
1898 return -1;
1899 }
1900 cli_addr.sin_family = AF_INET;
1901 cli_addr.sin_port = htons(
Andy Green3221f922011-02-12 13:14:11 +00001902 this->protocols[p].broadcast_socket_port);
Andy Greened11a022011-01-20 10:23:50 +00001903 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1904 n = connect(fd, (struct sockaddr *)&cli_addr,
1905 sizeof cli_addr);
1906 if (n < 0) {
1907 fprintf(stderr, "Unable to connect to "
1908 "broadcast socket %d, %s\n",
Andy Green3221f922011-02-12 13:14:11 +00001909 n, strerror(errno));
Andy Greened11a022011-01-20 10:23:50 +00001910 return -1;
1911 }
1912
Andy Green3221f922011-02-12 13:14:11 +00001913 this->protocols[p].broadcast_socket_user_fd = fd;
Andy Greened11a022011-01-20 10:23:50 +00001914 }
1915
Andy Greene92cd172011-01-19 13:11:55 +00001916 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001917 }
1918
1919 /* we want a SIGHUP when our parent goes down */
1920 prctl(PR_SET_PDEATHSIG, SIGHUP);
1921
1922 /* in this forked process, sit and service websocket connections */
Andy Green8f037e42010-12-19 22:13:26 +00001923
Andy Greene92cd172011-01-19 13:11:55 +00001924 while (1)
1925 if (libwebsocket_service(this, 1000))
1926 return -1;
Andy Green8f037e42010-12-19 22:13:26 +00001927
Andy Green251f6fa2010-11-03 11:13:06 +00001928 return 0;
Andy Greenff95d7a2010-10-28 22:36:01 +01001929}
1930
Andy Greened11a022011-01-20 10:23:50 +00001931#endif
1932
Andy Greenb45993c2010-12-18 15:13:50 +00001933/**
1934 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00001935 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00001936 * @wsi: pointer to struct websocket you want to know the protocol of
1937 *
Andy Green8f037e42010-12-19 22:13:26 +00001938 *
1939 * This is useful to get the protocol to broadcast back to from inside
Andy Greenb45993c2010-12-18 15:13:50 +00001940 * the callback.
1941 */
Andy Greenab990e42010-10-31 12:42:52 +00001942
Andy Greenb45993c2010-12-18 15:13:50 +00001943const struct libwebsocket_protocols *
1944libwebsockets_get_protocol(struct libwebsocket *wsi)
1945{
1946 return wsi->protocol;
1947}
1948
1949/**
Andy Greene92cd172011-01-19 13:11:55 +00001950 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
Andy Green8f037e42010-12-19 22:13:26 +00001951 * connections of the given protocol.
Andy Greenb45993c2010-12-18 15:13:50 +00001952 * @protocol: pointer to the protocol you will broadcast to all members of
1953 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
Andy Green8f037e42010-12-19 22:13:26 +00001954 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
1955 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
1956 * case you are calling this function from callback context.
Andy Greenb45993c2010-12-18 15:13:50 +00001957 * @len: length of payload data in buf, starting from buf.
Andy Green8f037e42010-12-19 22:13:26 +00001958 *
1959 * This function allows bulk sending of a packet to every connection using
Andy Greenb45993c2010-12-18 15:13:50 +00001960 * the given protocol. It does not send the data directly; instead it calls
1961 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
1962 * wants to actually send the data for that connection, the callback itself
1963 * should call libwebsocket_write().
1964 *
1965 * libwebsockets_broadcast() can be called from another fork context without
1966 * having to take any care about data visibility between the processes, it'll
1967 * "just work".
1968 */
1969
1970
1971int
Andy Green8f037e42010-12-19 22:13:26 +00001972libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
Andy Greenb45993c2010-12-18 15:13:50 +00001973 unsigned char *buf, size_t len)
1974{
Andy Green8f037e42010-12-19 22:13:26 +00001975 struct libwebsocket_context *this = protocol->owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00001976 int n;
Andy Green0d338332011-02-12 11:57:43 +00001977 int m;
1978 struct libwebsocket * wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00001979
1980 if (!protocol->broadcast_socket_user_fd) {
1981 /*
Andy Greene92cd172011-01-19 13:11:55 +00001982 * We are either running unforked / flat, or we are being
1983 * called from poll thread context
Andy Greenb45993c2010-12-18 15:13:50 +00001984 * eg, from a callback. In that case don't use sockets for
1985 * broadcast IPC (since we can't open a socket connection to
1986 * a socket listening on our own thread) but directly do the
1987 * send action.
1988 *
1989 * Locking is not needed because we are by definition being
1990 * called in the poll thread context and are serialized.
1991 */
1992
Andy Green0d338332011-02-12 11:57:43 +00001993 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
Andy Greenb45993c2010-12-18 15:13:50 +00001994
Andy Green0d338332011-02-12 11:57:43 +00001995 for (m = 0; m < this->fd_hashtable[n].length; m++) {
Andy Greenb45993c2010-12-18 15:13:50 +00001996
Andy Green0d338332011-02-12 11:57:43 +00001997 wsi = this->fd_hashtable[n].wsi[m];
Andy Greenb45993c2010-12-18 15:13:50 +00001998
Andy Green0d338332011-02-12 11:57:43 +00001999 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
2000 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002001
Andy Green0d338332011-02-12 11:57:43 +00002002 /*
2003 * never broadcast to
2004 * non-established connections
2005 */
2006 if (wsi->state != WSI_STATE_ESTABLISHED)
2007 continue;
2008
2009 /* only broadcast to guys using
2010 * requested protocol
2011 */
2012 if (wsi->protocol != protocol)
2013 continue;
2014
Andy Green62c54d22011-02-14 09:14:25 +00002015 wsi->protocol->callback(this, wsi,
Andy Green8f037e42010-12-19 22:13:26 +00002016 LWS_CALLBACK_BROADCAST,
Andy Green0d338332011-02-12 11:57:43 +00002017 wsi->user_space,
Andy Greenb45993c2010-12-18 15:13:50 +00002018 buf, len);
Andy Green0d338332011-02-12 11:57:43 +00002019 }
Andy Greenb45993c2010-12-18 15:13:50 +00002020 }
2021
2022 return 0;
2023 }
2024
Andy Green0ca6a172010-12-19 20:50:01 +00002025 /*
2026 * We're being called from a different process context than the server
2027 * loop. Instead of broadcasting directly, we send our
2028 * payload on a socket to do the IPC; the server process will serialize
2029 * the broadcast action in its main poll() loop.
2030 *
2031 * There's one broadcast socket listening for each protocol supported
2032 * set up when the websocket server initializes
2033 */
2034
Andy Green6964bb52011-01-23 16:50:33 +00002035 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
Andy Greenb45993c2010-12-18 15:13:50 +00002036
2037 return n;
2038}