blob: afb0f0715f61a442be8de894f4004f70494d59d1 [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 Greenff95d7a2010-10-28 22:36:01 +010023
Andy Green8f037e42010-12-19 22:13:26 +000024void
Andy Green251f6fa2010-11-03 11:13:06 +000025libwebsocket_close_and_free_session(struct libwebsocket *wsi)
26{
Andy Greenb45993c2010-12-18 15:13:50 +000027 int n;
Andy Green5e1fa172011-02-10 09:07:05 +000028 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
29 LWS_SEND_BUFFER_POST_PADDING];
Andy Greenb45993c2010-12-18 15:13:50 +000030
31 if ((unsigned long)wsi < LWS_MAX_PROTOCOLS)
32 return;
33
34 n = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +000035
Andy Green5e1fa172011-02-10 09:07:05 +000036 if (n == WSI_STATE_DEAD_SOCKET)
37 return;
38
39 /*
40 * signal we are closing, libsocket_write will
41 * add any necessary version-specific stuff. If the write fails,
42 * no worries we are closing anyway. If we didn't initiate this
43 * close, then our state has been changed to
44 * WSI_STATE_RETURNED_CLOSE_ALREADY and we can skip this
45 */
46
47 if (n == WSI_STATE_ESTABLISHED)
48 libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 0,
49 LWS_WRITE_CLOSE);
50
Andy Green251f6fa2010-11-03 11:13:06 +000051 wsi->state = WSI_STATE_DEAD_SOCKET;
52
Andy Green4f3943a2010-11-12 10:44:16 +000053 if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED)
Andy Greene77ddd82010-11-13 10:03:47 +000054 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
55 wsi->user_space, NULL, 0);
Andy Green251f6fa2010-11-03 11:13:06 +000056
57 for (n = 0; n < WSI_TOKEN_COUNT; n++)
58 if (wsi->utf8_token[n].token)
59 free(wsi->utf8_token[n].token);
60
Andy Green0ca6a172010-12-19 20:50:01 +000061/* fprintf(stderr, "closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +000062
Andy Green3faa9c72010-11-08 17:03:03 +000063#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +000064 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +000065 n = SSL_get_fd(wsi->ssl);
66 SSL_shutdown(wsi->ssl);
67 close(n);
68 SSL_free(wsi->ssl);
69 } else {
70#endif
71 shutdown(wsi->sock, SHUT_RDWR);
72 close(wsi->sock);
73#ifdef LWS_OPENSSL_SUPPORT
74 }
75#endif
Andy Green4f3943a2010-11-12 10:44:16 +000076 if (wsi->user_space)
77 free(wsi->user_space);
78
Andy Green251f6fa2010-11-03 11:13:06 +000079 free(wsi);
80}
81
Andy Greenb45993c2010-12-18 15:13:50 +000082static int
83libwebsocket_poll_connections(struct libwebsocket_context *this)
84{
85 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BROADCAST_PAYLOAD +
86 LWS_SEND_BUFFER_POST_PADDING];
Andy Green8f037e42010-12-19 22:13:26 +000087 int client = this->count_protocols + 1;
88 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +000089 int n;
90 size_t len;
91
92 /* check for activity on client sockets */
Andy Green8f037e42010-12-19 22:13:26 +000093
94 for (; client < this->fds_count; client++) {
95
Andy Greenb45993c2010-12-18 15:13:50 +000096 /* handle session socket closed */
Andy Green8f037e42010-12-19 22:13:26 +000097
Andy Greenb45993c2010-12-18 15:13:50 +000098 if (this->fds[client].revents & (POLLERR | POLLHUP)) {
Andy Green8f037e42010-12-19 22:13:26 +000099
Andy Greenb45993c2010-12-18 15:13:50 +0000100 debug("Session Socket %d %p (fd=%d) dead\n",
Andy Green6964bb52011-01-23 16:50:33 +0000101 client, (void *)this->wsi[client],
102 this->fds[client].fd);
Andy Greenb45993c2010-12-18 15:13:50 +0000103
104 libwebsocket_close_and_free_session(this->wsi[client]);
105 goto nuke_this;
106 }
Andy Green8f037e42010-12-19 22:13:26 +0000107
Andy Green90c7cbc2011-01-27 06:26:52 +0000108 /* the guy requested a callback when it was OK to write */
109
110 if ((unsigned long)this->wsi[client] > LWS_MAX_PROTOCOLS &&
111 this->fds[client].revents & POLLOUT) {
112
113 this->fds[client].events &= ~POLLOUT;
114
115 this->wsi[client]->protocol->callback(this->wsi[client],
116 LWS_CALLBACK_CLIENT_WRITEABLE,
117 this->wsi[client]->user_space,
118 NULL, 0);
119 }
120
Andy Greenb45993c2010-12-18 15:13:50 +0000121 /* any incoming data ready? */
122
123 if (!(this->fds[client].revents & POLLIN))
124 continue;
125
126 /* broadcast? */
127
128 if ((unsigned long)this->wsi[client] < LWS_MAX_PROTOCOLS) {
129
Andy Green8f037e42010-12-19 22:13:26 +0000130 /* get the issued broadcast payload from the socket */
131
Andy Greenb45993c2010-12-18 15:13:50 +0000132 len = read(this->fds[client].fd,
133 buf + LWS_SEND_BUFFER_PRE_PADDING,
134 MAX_BROADCAST_PAYLOAD);
Andy Green8f037e42010-12-19 22:13:26 +0000135
Andy Greenb45993c2010-12-18 15:13:50 +0000136 if (len < 0) {
Andy Green8f037e42010-12-19 22:13:26 +0000137 fprintf(stderr,
138 "Error reading broadcast payload\n");
Andy Greenb45993c2010-12-18 15:13:50 +0000139 continue;
140 }
141
142 /* broadcast it to all guys with this protocol index */
143
144 for (n = this->count_protocols + 1;
145 n < this->fds_count; n++) {
146
Andy Green8f037e42010-12-19 22:13:26 +0000147 wsi = this->wsi[n];
148
149 if ((unsigned long)wsi < LWS_MAX_PROTOCOLS)
Andy Greenb45993c2010-12-18 15:13:50 +0000150 continue;
151
152 /*
153 * never broadcast to non-established
154 * connection
155 */
156
Andy Green8f037e42010-12-19 22:13:26 +0000157 if (wsi->state != WSI_STATE_ESTABLISHED)
Andy Greenb45993c2010-12-18 15:13:50 +0000158 continue;
159
Andy Green4739e5c2011-01-22 12:51:57 +0000160 /* only to clients connected to us */
Andy Green6964bb52011-01-23 16:50:33 +0000161
Andy Greenf3d3b402011-02-09 07:16:34 +0000162 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
Andy Green4739e5c2011-01-22 12:51:57 +0000163 continue;
164
Andy Greenb45993c2010-12-18 15:13:50 +0000165 /*
166 * only broadcast to connections using
167 * the requested protocol
168 */
169
Andy Green8f037e42010-12-19 22:13:26 +0000170 if (wsi->protocol->protocol_index !=
171 (int)(unsigned long)this->wsi[client])
Andy Greenb45993c2010-12-18 15:13:50 +0000172 continue;
173
Andy Green8f037e42010-12-19 22:13:26 +0000174 /* broadcast it to this connection */
175
176 wsi->protocol->callback(wsi,
177 LWS_CALLBACK_BROADCAST,
178 wsi->user_space,
Andy Green0ca6a172010-12-19 20:50:01 +0000179 buf + LWS_SEND_BUFFER_PRE_PADDING, len);
Andy Greenb45993c2010-12-18 15:13:50 +0000180 }
181
182 continue;
183 }
184
185#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000186 if (this->wsi[client]->ssl)
Andy Greenb45993c2010-12-18 15:13:50 +0000187 n = SSL_read(this->wsi[client]->ssl, buf, sizeof buf);
188 else
189#endif
190 n = recv(this->fds[client].fd, buf, sizeof buf, 0);
191
192 if (n < 0) {
193 fprintf(stderr, "Socket read returned %d\n", n);
194 continue;
195 }
196 if (!n) {
Andy Green8f037e42010-12-19 22:13:26 +0000197 libwebsocket_close_and_free_session(this->wsi[client]);
Andy Greenb45993c2010-12-18 15:13:50 +0000198 goto nuke_this;
199 }
200
Andy Greenb45993c2010-12-18 15:13:50 +0000201 /* service incoming data */
202
Andy Green6964bb52011-01-23 16:50:33 +0000203 n = libwebsocket_read(this->wsi[client], buf, n);
204 if (n >= 0)
Andy Greenb45993c2010-12-18 15:13:50 +0000205 continue;
Andy Greenb45993c2010-12-18 15:13:50 +0000206 /*
207 * it closed and nuked wsi[client], so remove the
208 * socket handle and wsi from our service list
209 */
210nuke_this:
211
212 debug("nuking wsi %p, fsd_count = %d\n",
Andy Green6964bb52011-01-23 16:50:33 +0000213 (void *)this->wsi[client], this->fds_count - 1);
Andy Greenb45993c2010-12-18 15:13:50 +0000214
215 this->fds_count--;
216 for (n = client; n < this->fds_count; n++) {
217 this->fds[n] = this->fds[n + 1];
218 this->wsi[n] = this->wsi[n + 1];
219 }
Andy Green6964bb52011-01-23 16:50:33 +0000220
221 return 0;
222
Andy Greenb45993c2010-12-18 15:13:50 +0000223 }
224
225 return 0;
226}
227
Andy Green6964bb52011-01-23 16:50:33 +0000228/**
229 * libwebsocket_context_destroy() - Destroy the websocket context
230 * @this: Websocket context
231 *
232 * This function closes any active connections and then frees the
233 * context. After calling this, any further use of the context is
234 * undefined.
235 */
236void
237libwebsocket_context_destroy(struct libwebsocket_context *this)
238{
239 int client;
240
241 /* close listening skt and per-protocol broadcast sockets */
Andy Green6a980542011-01-30 20:40:32 +0000242 for (client = this->count_protocols + 1; client < this->fds_count; client++)
Andy Greenf3d3b402011-02-09 07:16:34 +0000243 switch (this->wsi[client]->mode) {
244 case LWS_CONNMODE_WS_SERVING:
Andy Green6a980542011-01-30 20:40:32 +0000245 libwebsocket_close_and_free_session(this->wsi[client]);
Andy Greenf3d3b402011-02-09 07:16:34 +0000246 break;
247 case LWS_CONNMODE_WS_CLIENT:
248 libwebsocket_client_close(this->wsi[client]);
249 break;
250 }
Andy Green6964bb52011-01-23 16:50:33 +0000251
252#ifdef LWS_OPENSSL_SUPPORT
Andy Green5e1fa172011-02-10 09:07:05 +0000253 if (this && this->ssl_ctx)
Andy Green90c7cbc2011-01-27 06:26:52 +0000254 SSL_CTX_free(this->ssl_ctx);
Andy Green5e1fa172011-02-10 09:07:05 +0000255 if (this && this->ssl_client_ctx)
256 SSL_CTX_free(this->ssl_client_ctx);
Andy Green6964bb52011-01-23 16:50:33 +0000257#endif
258
259 if (this)
260 free(this);
261}
262
263/**
264 * libwebsocket_service() - Service any pending websocket activity
265 * @this: Websocket context
266 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
267 * service otherwise block and service immediately, returning
268 * after the timeout if nothing needed service.
269 *
270 * This function deals with any pending websocket traffic, for three
271 * kinds of event. It handles these events on both server and client
272 * types of connection the same.
273 *
274 * 1) Accept new connections to our context's server
275 *
276 * 2) Perform pending broadcast writes initiated from other forked
277 * processes (effectively serializing asynchronous broadcasts)
278 *
279 * 3) Call the receive callback for incoming frame data received by
280 * server or client connections.
281 *
282 * You need to call this service function periodically to all the above
283 * functions to happen; if your application is single-threaded you can
284 * just call it in your main event loop.
285 *
286 * Alternatively you can fork a new process that asynchronously handles
287 * calling this service in a loop. In that case you are happy if this
288 * call blocks your thread until it needs to take care of something and
289 * would call it with a large nonzero timeout. Your loop then takes no
290 * CPU while there is nothing happening.
291 *
292 * If you are calling it in a single-threaded app, you don't want it to
293 * wait around blocking other things in your loop from happening, so you
294 * would call it with a timeout_ms of 0, so it returns immediately if
295 * nothing is pending, or as soon as it services whatever was pending.
296 */
297
Andy Greenb45993c2010-12-18 15:13:50 +0000298
Andy Greene92cd172011-01-19 13:11:55 +0000299int
300libwebsocket_service(struct libwebsocket_context *this, int timeout_ms)
301{
302 int n;
303 int client;
304 unsigned int clilen;
305 struct sockaddr_in cli_addr;
306 int fd;
307
308 /* stay dead once we are dead */
309
310 if (this == NULL)
311 return 1;
312
Andy Green4739e5c2011-01-22 12:51:57 +0000313 /* don't check listen socket if we are not listening */
314
315 if (this->listen_port)
316 n = poll(this->fds, this->fds_count, timeout_ms);
317 else
318 n = poll(&this->fds[1], this->fds_count - 1, timeout_ms);
Andy Green6964bb52011-01-23 16:50:33 +0000319
Andy Greene92cd172011-01-19 13:11:55 +0000320
321 if (n < 0 || this->fds[0].revents & (POLLERR | POLLHUP)) {
Andy Green5e1fa172011-02-10 09:07:05 +0000322 /*
Andy Greene92cd172011-01-19 13:11:55 +0000323 fprintf(stderr, "Listen Socket dead\n");
Andy Green5e1fa172011-02-10 09:07:05 +0000324 */
Andy Greene92cd172011-01-19 13:11:55 +0000325 goto fatal;
326 }
327 if (n == 0) /* poll timeout */
328 return 0;
329
330 /* handle accept on listening socket? */
331
332 for (client = 0; client < this->count_protocols + 1; client++) {
333
334 if (!this->fds[client].revents & POLLIN)
335 continue;
336
337 /* listen socket got an unencrypted connection... */
338
339 clilen = sizeof(cli_addr);
340 fd = accept(this->fds[client].fd,
341 (struct sockaddr *)&cli_addr, &clilen);
342 if (fd < 0) {
343 fprintf(stderr, "ERROR on accept");
344 continue;
345 }
346
347 if (this->fds_count >= MAX_CLIENTS) {
348 fprintf(stderr, "too busy");
349 close(fd);
350 continue;
351 }
352
353 if (client) {
354 /*
355 * accepting a connection to broadcast socket
356 * set wsi to be protocol index not pointer
357 */
358
359 this->wsi[this->fds_count] =
360 (struct libwebsocket *)(long)(client - 1);
361
362 goto fill_in_fds;
363 }
364
365 /* accepting connection to main listener */
366
367 this->wsi[this->fds_count] =
368 malloc(sizeof(struct libwebsocket));
369 if (!this->wsi[this->fds_count]) {
370 fprintf(stderr, "Out of memory for new connection\n");
371 continue;
372 }
373
374#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000375 this->wsi[this->fds_count]->ssl = NULL;
Andy Green5e1fa172011-02-10 09:07:05 +0000376 this->ssl_ctx = NULL;
Andy Green90c7cbc2011-01-27 06:26:52 +0000377
Andy Greene92cd172011-01-19 13:11:55 +0000378 if (this->use_ssl) {
379
Andy Green90c7cbc2011-01-27 06:26:52 +0000380 this->wsi[this->fds_count]->ssl =
381 SSL_new(this->ssl_ctx);
Andy Greene92cd172011-01-19 13:11:55 +0000382 if (this->wsi[this->fds_count]->ssl == NULL) {
383 fprintf(stderr, "SSL_new failed: %s\n",
384 ERR_error_string(SSL_get_error(
385 this->wsi[this->fds_count]->ssl, 0),
386 NULL));
387 free(this->wsi[this->fds_count]);
388 continue;
389 }
390
391 SSL_set_fd(this->wsi[this->fds_count]->ssl, fd);
392
393 n = SSL_accept(this->wsi[this->fds_count]->ssl);
394 if (n != 1) {
395 /*
396 * browsers seem to probe with various
397 * ssl params which fail then retry
398 * and succeed
399 */
400 debug("SSL_accept failed skt %u: %s\n",
401 fd,
402 ERR_error_string(SSL_get_error(
403 this->wsi[this->fds_count]->ssl,
404 n), NULL));
405 SSL_free(
406 this->wsi[this->fds_count]->ssl);
407 free(this->wsi[this->fds_count]);
408 continue;
409 }
410 debug("accepted new SSL conn "
411 "port %u on fd=%d SSL ver %s\n",
412 ntohs(cli_addr.sin_port), fd,
413 SSL_get_version(this->wsi[
414 this->fds_count]->ssl));
415
416 } else
417#endif
418 debug("accepted new conn port %u on fd=%d\n",
419 ntohs(cli_addr.sin_port), fd);
420
421 /* intialize the instance struct */
422
423 this->wsi[this->fds_count]->sock = fd;
424 this->wsi[this->fds_count]->state = WSI_STATE_HTTP;
425 this->wsi[this->fds_count]->name_buffer_pos = 0;
Andy Greenf3d3b402011-02-09 07:16:34 +0000426 this->wsi[this->fds_count]->mode = LWS_CONNMODE_WS_SERVING;
Andy Greene92cd172011-01-19 13:11:55 +0000427
428 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
429 this->wsi[this->fds_count]->
430 utf8_token[n].token = NULL;
431 this->wsi[this->fds_count]->
432 utf8_token[n].token_len = 0;
433 }
434
435 /*
436 * these can only be set once the protocol is known
437 * we set an unestablished connection's protocol pointer
438 * to the start of the supported list, so it can look
439 * for matching ones during the handshake
440 */
441 this->wsi[this->fds_count]->protocol = this->protocols;
442 this->wsi[this->fds_count]->user_space = NULL;
443
444 /*
445 * Default protocol is 76 / 00
446 * After 76, there's a header specified to inform which
447 * draft the client wants, when that's seen we modify
448 * the individual connection's spec revision accordingly
449 */
450 this->wsi[this->fds_count]->ietf_spec_revision = 0;
451
452fill_in_fds:
453
454 /*
455 * make sure NO events are seen yet on this new socket
456 * (otherwise we inherit old fds[client].revents from
457 * previous socket there and die mysteriously! )
458 */
459 this->fds[this->fds_count].revents = 0;
460
461 this->fds[this->fds_count].events = POLLIN;
462 this->fds[this->fds_count++].fd = fd;
463
464 }
465
466 /* service anything incoming on websocket connection */
467
468 libwebsocket_poll_connections(this);
469
470 /* this round is done */
471
472 return 0;
473
474fatal:
475
Andy Greene92cd172011-01-19 13:11:55 +0000476 /* inform caller we are dead */
477
478 return 1;
479}
480
Andy Green90c7cbc2011-01-27 06:26:52 +0000481/**
482 * libwebsocket_callback_on_writable() - Request a callback when this socket
483 * becomes able to be written to without
484 * blocking
485 *
486 * @wsi: Websocket connection instance to get callback for
487 */
488
489int
490libwebsocket_callback_on_writable(struct libwebsocket *wsi)
491{
492 struct libwebsocket_context *this = wsi->protocol->owning_server;
493 int n;
494
495 for (n = this->count_protocols + 1; n < this->fds_count; n++)
496 if (this->wsi[n] == wsi) {
497 this->fds[n].events |= POLLOUT;
498 return 0;
499 }
500
501 fprintf(stderr, "libwebsocket_callback_on_writable "
502 "didn't find socket\n");
503 return 1;
504}
505
506/**
507 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
508 * all connections using the given protocol when it
509 * becomes possible to write to each socket without
510 * blocking in turn.
511 *
512 * @protocol: Protocol whose connections will get callbacks
513 */
514
515int
516libwebsocket_callback_on_writable_all_protocol(
517 const struct libwebsocket_protocols *protocol)
518{
519 struct libwebsocket_context *this = protocol->owning_server;
520 int n;
521
522 for (n = this->count_protocols + 1; n < this->fds_count; n++)
523 if ((unsigned long)this->wsi[n] > LWS_MAX_PROTOCOLS)
524 if (this->wsi[n]->protocol == protocol)
525 this->fds[n].events |= POLLOUT;
526
527 return 0;
528}
529
Andy Greena6cbece2011-01-27 20:06:03 +0000530
531/**
532 * libwebsocket_get_socket_fd() - returns the socket file descriptor
533 *
534 * You will not need this unless you are doing something special
535 *
536 * @wsi: Websocket connection instance
537 */
538
539int
540libwebsocket_get_socket_fd(struct libwebsocket *wsi)
541{
542 return wsi->sock;
543}
544
Andy Green90c7cbc2011-01-27 06:26:52 +0000545/**
546 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
547 * receieved packets.
548 *
549 * If the output side of a server process becomes choked, this allows flow
550 * control for the input side.
551 *
552 * @wsi: Websocket connection instance to get callback for
553 * @enable: 0 = disable read servicing for this connection, 1 = enable
554 */
555
556int
557libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
558{
559 struct libwebsocket_context *this = wsi->protocol->owning_server;
560 int n;
561
562 for (n = this->count_protocols + 1; n < this->fds_count; n++)
563 if (this->wsi[n] == wsi) {
564 if (enable)
565 this->fds[n].events |= POLLIN;
566 else
567 this->fds[n].events &= ~POLLIN;
568
569 return 0;
570 }
571
572 fprintf(stderr, "libwebsocket_callback_on_writable "
573 "unable to find socket\n");
574 return 1;
575}
576
Andy Green2ac5a6f2011-01-28 10:00:18 +0000577/**
578 * libwebsocket_canonical_hostname() - returns this host's hostname
579 *
580 * This is typically used by client code to fill in the host parameter
581 * when making a client connection. You can only call it after the context
582 * has been created.
583 *
584 * @this: Websocket context
585 */
586
587
588extern const char *
589libwebsocket_canonical_hostname(struct libwebsocket_context *this)
590{
591 return (const char *)this->canonical_hostname;
592}
593
594
Andy Green90c7cbc2011-01-27 06:26:52 +0000595static void sigpipe_handler(int x)
596{
597}
598
Andy Greenb45993c2010-12-18 15:13:50 +0000599
Andy Greenab990e42010-10-31 12:42:52 +0000600/**
Andy Green4739e5c2011-01-22 12:51:57 +0000601 * libwebsocket_create_context() - Create the websocket handler
602 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +0000603 * any port, that's what you want if you are not running a
604 * websocket server at all but just using it as a client
Andy Green4f3943a2010-11-12 10:44:16 +0000605 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +0000606 * specific callback for each one. The list is ended with an
607 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +0000608 * It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +0000609 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +0000610 * to listen using SSL, set to the filepath to fetch the
611 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +0000612 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +0000613 * else ignored
Andy Green3faa9c72010-11-08 17:03:03 +0000614 * @gid: group id to change to after setting listen socket, or -1.
615 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +0000616 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green05464c62010-11-12 10:44:18 +0000617 *
Andy Green8f037e42010-12-19 22:13:26 +0000618 * This function creates the listening socket and takes care
619 * of all initialization in one step.
620 *
Andy Greene92cd172011-01-19 13:11:55 +0000621 * After initialization, it returns a struct libwebsocket_context * that
622 * represents this server. After calling, user code needs to take care
623 * of calling libwebsocket_service() with the context pointer to get the
624 * server's sockets serviced. This can be done in the same process context
625 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +0000626 *
Andy Green8f037e42010-12-19 22:13:26 +0000627 * The protocol callback functions are called for a handful of events
628 * including http requests coming in, websocket connections becoming
629 * established, and data arriving; it's also called periodically to allow
630 * async transmission.
631 *
632 * HTTP requests are sent always to the FIRST protocol in @protocol, since
633 * at that time websocket protocol has not been negotiated. Other
634 * protocols after the first one never see any HTTP callack activity.
635 *
636 * The server created is a simple http server by default; part of the
637 * websocket standard is upgrading this http connection to a websocket one.
638 *
639 * This allows the same server to provide files like scripts and favicon /
640 * images or whatever over http and dynamic data over websockets all in
641 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +0000642 */
Andy Green4ea60062010-10-30 12:15:07 +0100643
Andy Greene92cd172011-01-19 13:11:55 +0000644struct libwebsocket_context *
Andy Green4739e5c2011-01-22 12:51:57 +0000645libwebsocket_create_context(int port,
Andy Greenb45993c2010-12-18 15:13:50 +0000646 struct libwebsocket_protocols *protocols,
Andy Green8f037e42010-12-19 22:13:26 +0000647 const char *ssl_cert_filepath,
648 const char *ssl_private_key_filepath,
Andy Green8014b292011-01-30 20:57:25 +0000649 int gid, int uid, unsigned int options)
Andy Greenff95d7a2010-10-28 22:36:01 +0100650{
651 int n;
Andy Green4739e5c2011-01-22 12:51:57 +0000652 int sockfd = 0;
Andy Green251f6fa2010-11-03 11:13:06 +0000653 int fd;
Andy Greenff95d7a2010-10-28 22:36:01 +0100654 struct sockaddr_in serv_addr, cli_addr;
Andy Green251f6fa2010-11-03 11:13:06 +0000655 int opt = 1;
Andy Green8f037e42010-12-19 22:13:26 +0000656 struct libwebsocket_context *this = NULL;
Andy Greenb45993c2010-12-18 15:13:50 +0000657 unsigned int slen;
Andy Green9659f372011-01-27 22:01:43 +0000658 char *p;
Andy Green2ac5a6f2011-01-28 10:00:18 +0000659 char hostname[1024];
Andy Green42f69142011-01-30 08:10:02 +0000660 struct hostent *he;
Andy Greenff95d7a2010-10-28 22:36:01 +0100661
Andy Green3faa9c72010-11-08 17:03:03 +0000662#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +0000663 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +0000664 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +0000665#endif
666
Andy Green90c7cbc2011-01-27 06:26:52 +0000667 this = malloc(sizeof(struct libwebsocket_context));
668 if (!this) {
669 fprintf(stderr, "No memory for websocket context\n");
670 return NULL;
671 }
672 this->protocols = protocols;
673 this->listen_port = port;
Andy Green9659f372011-01-27 22:01:43 +0000674 this->http_proxy_port = 0;
675 this->http_proxy_address[0] = '\0';
Andy Green8014b292011-01-30 20:57:25 +0000676 this->options = options;
Andy Green9659f372011-01-27 22:01:43 +0000677
Andy Green2ac5a6f2011-01-28 10:00:18 +0000678 /* find canonical hostname */
679
680 hostname[(sizeof hostname) - 1] = '\0';
681 gethostname(hostname, (sizeof hostname) - 1);
682 he = gethostbyname(hostname);
683 strncpy(this->canonical_hostname, he->h_name,
684 sizeof this->canonical_hostname - 1);
685 this->canonical_hostname[sizeof this->canonical_hostname - 1] = '\0';
Andy Green1efb63c2011-02-06 17:42:06 +0000686 fprintf(stderr, " canonical hostname = '%s'\n",
687 this->canonical_hostname);
Andy Green2ac5a6f2011-01-28 10:00:18 +0000688
Andy Green9659f372011-01-27 22:01:43 +0000689 /* split the proxy ads:port if given */
690
691 p = getenv("http_proxy");
692 if (p) {
693 strncpy(this->http_proxy_address, p,
694 sizeof this->http_proxy_address - 1);
695 this->http_proxy_address[
696 sizeof this->http_proxy_address - 1] = '\0';
697
698 p = strchr(this->http_proxy_address, ':');
699 if (p == NULL) {
700 fprintf(stderr, "http_proxy needs to be ads:port\n");
701 return NULL;
702 }
703 *p = '\0';
704 this->http_proxy_port = atoi(p + 1);
705
706 fprintf(stderr, "Using proxy %s:%u\n",
707 this->http_proxy_address,
708 this->http_proxy_port);
709 }
Andy Green90c7cbc2011-01-27 06:26:52 +0000710
711 if (port) {
712
Andy Green3faa9c72010-11-08 17:03:03 +0000713#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000714 this->use_ssl = ssl_cert_filepath != NULL &&
715 ssl_private_key_filepath != NULL;
716 if (this->use_ssl)
717 fprintf(stderr, " Compiled with SSL support, "
718 "using it\n");
719 else
720 fprintf(stderr, " Compiled with SSL support, "
721 "not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +0000722
Andy Green90c7cbc2011-01-27 06:26:52 +0000723#else
724 if (ssl_cert_filepath != NULL &&
725 ssl_private_key_filepath != NULL) {
726 fprintf(stderr, " Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +0000727 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +0000728 }
Andy Green90c7cbc2011-01-27 06:26:52 +0000729 fprintf(stderr, " Compiled without SSL support, "
730 "serving unencrypted\n");
731#endif
732 }
733
734 /* ignore SIGPIPE */
735
736 signal(SIGPIPE, sigpipe_handler);
737
738
739#ifdef LWS_OPENSSL_SUPPORT
740
741 /* basic openssl init */
742
743 SSL_library_init();
744
745 OpenSSL_add_all_algorithms();
746 SSL_load_error_strings();
747
748 /*
749 * Firefox insists on SSLv23 not SSLv3
750 * Konq disables SSLv2 by default now, SSLv23 works
751 */
752
753 method = (SSL_METHOD *)SSLv23_server_method();
754 if (!method) {
755 fprintf(stderr, "problem creating ssl method: %s\n",
756 ERR_error_string(ERR_get_error(), ssl_err_buf));
757 return NULL;
758 }
759 this->ssl_ctx = SSL_CTX_new(method); /* create context */
760 if (!this->ssl_ctx) {
761 fprintf(stderr, "problem creating ssl context: %s\n",
762 ERR_error_string(ERR_get_error(), ssl_err_buf));
763 return NULL;
764 }
765
766 /* client context */
767
768 method = (SSL_METHOD *)SSLv23_client_method();
769 if (!method) {
770 fprintf(stderr, "problem creating ssl method: %s\n",
771 ERR_error_string(ERR_get_error(), ssl_err_buf));
772 return NULL;
773 }
774 this->ssl_client_ctx = SSL_CTX_new(method); /* create context */
775 if (!this->ssl_client_ctx) {
776 fprintf(stderr, "problem creating ssl context: %s\n",
777 ERR_error_string(ERR_get_error(), ssl_err_buf));
778 return NULL;
779 }
780
781
782 /* openssl init for cert verification (used with client sockets) */
783
784 if (!SSL_CTX_load_verify_locations(this->ssl_client_ctx, NULL,
785 LWS_OPENSSL_CLIENT_CERTS)) {
786 fprintf(stderr, "Unable to load SSL Client certs from %s "
787 "(set by --with-client-cert-dir= in configure) -- "
788 " client ssl isn't going to work",
789 LWS_OPENSSL_CLIENT_CERTS);
790 }
791
792 if (this->use_ssl) {
793
794 /* openssl init for server sockets */
795
Andy Green3faa9c72010-11-08 17:03:03 +0000796 /* set the local certificate from CertFile */
Andy Green90c7cbc2011-01-27 06:26:52 +0000797 n = SSL_CTX_use_certificate_file(this->ssl_ctx,
Andy Green3faa9c72010-11-08 17:03:03 +0000798 ssl_cert_filepath, SSL_FILETYPE_PEM);
799 if (n != 1) {
800 fprintf(stderr, "problem getting cert '%s': %s\n",
801 ssl_cert_filepath,
802 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +0000803 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +0000804 }
805 /* set the private key from KeyFile */
Andy Green90c7cbc2011-01-27 06:26:52 +0000806 if (SSL_CTX_use_PrivateKey_file(this->ssl_ctx,
Andy Green018d8eb2010-11-08 21:04:23 +0000807 ssl_private_key_filepath,
Andy Green4739e5c2011-01-22 12:51:57 +0000808 SSL_FILETYPE_PEM) != 1) {
Andy Green018d8eb2010-11-08 21:04:23 +0000809 fprintf(stderr, "ssl problem getting key '%s': %s\n",
810 ssl_private_key_filepath,
811 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +0000812 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +0000813 }
814 /* verify private key */
Andy Green90c7cbc2011-01-27 06:26:52 +0000815 if (!SSL_CTX_check_private_key(this->ssl_ctx)) {
Andy Green018d8eb2010-11-08 21:04:23 +0000816 fprintf(stderr, "Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +0000817 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +0000818 }
819
820 /* SSL is happy and has a cert it's content with */
821 }
822#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000823
Andy Greendf736162011-01-18 15:39:02 +0000824 /* selftest */
825
826 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +0000827 return NULL;
Andy Greendf736162011-01-18 15:39:02 +0000828
Andy Greenb45993c2010-12-18 15:13:50 +0000829 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +0000830
Andy Green4739e5c2011-01-22 12:51:57 +0000831 if (port) {
Andy Green8f037e42010-12-19 22:13:26 +0000832
Andy Green4739e5c2011-01-22 12:51:57 +0000833 sockfd = socket(AF_INET, SOCK_STREAM, 0);
834 if (sockfd < 0) {
835 fprintf(stderr, "ERROR opening socket");
836 return NULL;
837 }
Andy Green775c0dd2010-10-29 14:15:22 +0100838
Andy Green4739e5c2011-01-22 12:51:57 +0000839 /* allow us to restart even if old sockets in TIME_WAIT */
840 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
Andy Greene77ddd82010-11-13 10:03:47 +0000841
Andy Green4739e5c2011-01-22 12:51:57 +0000842 bzero((char *) &serv_addr, sizeof(serv_addr));
843 serv_addr.sin_family = AF_INET;
844 serv_addr.sin_addr.s_addr = INADDR_ANY;
845 serv_addr.sin_port = htons(port);
846
847 n = bind(sockfd, (struct sockaddr *) &serv_addr,
848 sizeof(serv_addr));
849 if (n < 0) {
850 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +0000851 port, n, errno);
Andy Green4739e5c2011-01-22 12:51:57 +0000852 return NULL;
853 }
Andy Green8f037e42010-12-19 22:13:26 +0000854 }
Andy Greenb45993c2010-12-18 15:13:50 +0000855
Andy Greene77ddd82010-11-13 10:03:47 +0000856 /* drop any root privs for this process */
Andy Green3faa9c72010-11-08 17:03:03 +0000857
858 if (gid != -1)
859 if (setgid(gid))
860 fprintf(stderr, "setgid: %s\n", strerror(errno));
861 if (uid != -1)
862 if (setuid(uid))
863 fprintf(stderr, "setuid: %s\n", strerror(errno));
864
Andy Green8f037e42010-12-19 22:13:26 +0000865 /*
Andy Greenb45993c2010-12-18 15:13:50 +0000866 * prepare the poll() fd array... it's like this
867 *
868 * [0] = external listening socket
869 * [1 .. this->count_protocols] = per-protocol broadcast sockets
870 * [this->count_protocols + 1 ... this->fds_count-1] = connection skts
Andy Green4f3943a2010-11-12 10:44:16 +0000871 */
872
Andy Greenb45993c2010-12-18 15:13:50 +0000873 this->fds_count = 1;
874 this->fds[0].fd = sockfd;
875 this->fds[0].events = POLLIN;
876 this->count_protocols = 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000877
Andy Green4739e5c2011-01-22 12:51:57 +0000878 if (port) {
879 listen(sockfd, 5);
880 fprintf(stderr, " Listening on port %d\n", port);
881 }
Andy Greenb45993c2010-12-18 15:13:50 +0000882
883 /* set up our internal broadcast trigger sockets per-protocol */
884
885 for (; protocols[this->count_protocols].callback;
886 this->count_protocols++) {
887 protocols[this->count_protocols].owning_server = this;
888 protocols[this->count_protocols].protocol_index =
889 this->count_protocols;
890
891 fd = socket(AF_INET, SOCK_STREAM, 0);
892 if (fd < 0) {
893 fprintf(stderr, "ERROR opening socket");
Andy Greene92cd172011-01-19 13:11:55 +0000894 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +0000895 }
Andy Green8f037e42010-12-19 22:13:26 +0000896
Andy Greenb45993c2010-12-18 15:13:50 +0000897 /* allow us to restart even if old sockets in TIME_WAIT */
898 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
899
900 bzero((char *) &serv_addr, sizeof(serv_addr));
901 serv_addr.sin_family = AF_INET;
902 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
903 serv_addr.sin_port = 0; /* pick the port for us */
904
905 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
906 if (n < 0) {
Andy Green8f037e42010-12-19 22:13:26 +0000907 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
Andy Greenb45993c2010-12-18 15:13:50 +0000908 port, n, errno);
Andy Greene92cd172011-01-19 13:11:55 +0000909 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +0000910 }
911
912 slen = sizeof cli_addr;
913 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
914 if (n < 0) {
915 fprintf(stderr, "getsockname failed\n");
Andy Greene92cd172011-01-19 13:11:55 +0000916 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +0000917 }
918 protocols[this->count_protocols].broadcast_socket_port =
919 ntohs(cli_addr.sin_port);
920 listen(fd, 5);
921
922 debug(" Protocol %s broadcast socket %d\n",
923 protocols[this->count_protocols].name,
924 ntohs(cli_addr.sin_port));
925
926 this->fds[this->fds_count].fd = fd;
927 this->fds[this->fds_count].events = POLLIN;
928 /* wsi only exists for connections, not broadcast listener */
929 this->wsi[this->fds_count] = NULL;
930 this->fds_count++;
931 }
932
Andy Greene92cd172011-01-19 13:11:55 +0000933 return this;
934}
Andy Greenb45993c2010-12-18 15:13:50 +0000935
Andy Green4739e5c2011-01-22 12:51:57 +0000936
Andy Greened11a022011-01-20 10:23:50 +0000937#ifndef LWS_NO_FORK
938
Andy Greene92cd172011-01-19 13:11:55 +0000939/**
940 * libwebsockets_fork_service_loop() - Optional helper function forks off
941 * a process for the websocket server loop.
Andy Green6964bb52011-01-23 16:50:33 +0000942 * You don't have to use this but if not, you
943 * have to make sure you are calling
944 * libwebsocket_service periodically to service
945 * the websocket traffic
Andy Greene92cd172011-01-19 13:11:55 +0000946 * @this: server context returned by creation function
947 */
Andy Greenb45993c2010-12-18 15:13:50 +0000948
Andy Greene92cd172011-01-19 13:11:55 +0000949int
950libwebsockets_fork_service_loop(struct libwebsocket_context *this)
951{
952 int client;
953 int fd;
954 struct sockaddr_in cli_addr;
955 int n;
Andy Greenb45993c2010-12-18 15:13:50 +0000956
Andy Greened11a022011-01-20 10:23:50 +0000957 n = fork();
958 if (n < 0)
959 return n;
960
961 if (!n) {
962
963 /* main process context */
964
965 for (client = 1; client < this->count_protocols + 1; client++) {
966 fd = socket(AF_INET, SOCK_STREAM, 0);
967 if (fd < 0) {
968 fprintf(stderr, "Unable to create socket\n");
969 return -1;
970 }
971 cli_addr.sin_family = AF_INET;
972 cli_addr.sin_port = htons(
Andy Green4739e5c2011-01-22 12:51:57 +0000973 this->protocols[client - 1].broadcast_socket_port);
Andy Greened11a022011-01-20 10:23:50 +0000974 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
975 n = connect(fd, (struct sockaddr *)&cli_addr,
976 sizeof cli_addr);
977 if (n < 0) {
978 fprintf(stderr, "Unable to connect to "
979 "broadcast socket %d, %s\n",
980 client, strerror(errno));
981 return -1;
982 }
983
Andy Green4739e5c2011-01-22 12:51:57 +0000984 this->protocols[client - 1].broadcast_socket_user_fd =
985 fd;
Andy Greened11a022011-01-20 10:23:50 +0000986 }
987
988
Andy Greene92cd172011-01-19 13:11:55 +0000989 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000990 }
991
992 /* we want a SIGHUP when our parent goes down */
993 prctl(PR_SET_PDEATHSIG, SIGHUP);
994
995 /* in this forked process, sit and service websocket connections */
Andy Green8f037e42010-12-19 22:13:26 +0000996
Andy Greene92cd172011-01-19 13:11:55 +0000997 while (1)
998 if (libwebsocket_service(this, 1000))
999 return -1;
Andy Green8f037e42010-12-19 22:13:26 +00001000
Andy Green251f6fa2010-11-03 11:13:06 +00001001 return 0;
Andy Greenff95d7a2010-10-28 22:36:01 +01001002}
1003
Andy Greened11a022011-01-20 10:23:50 +00001004#endif
1005
Andy Greenb45993c2010-12-18 15:13:50 +00001006/**
1007 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00001008 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00001009 * @wsi: pointer to struct websocket you want to know the protocol of
1010 *
Andy Green8f037e42010-12-19 22:13:26 +00001011 *
1012 * This is useful to get the protocol to broadcast back to from inside
Andy Greenb45993c2010-12-18 15:13:50 +00001013 * the callback.
1014 */
Andy Greenab990e42010-10-31 12:42:52 +00001015
Andy Greenb45993c2010-12-18 15:13:50 +00001016const struct libwebsocket_protocols *
1017libwebsockets_get_protocol(struct libwebsocket *wsi)
1018{
1019 return wsi->protocol;
1020}
1021
1022/**
Andy Greene92cd172011-01-19 13:11:55 +00001023 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
Andy Green8f037e42010-12-19 22:13:26 +00001024 * connections of the given protocol.
Andy Greenb45993c2010-12-18 15:13:50 +00001025 * @protocol: pointer to the protocol you will broadcast to all members of
1026 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
Andy Green8f037e42010-12-19 22:13:26 +00001027 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
1028 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
1029 * case you are calling this function from callback context.
Andy Greenb45993c2010-12-18 15:13:50 +00001030 * @len: length of payload data in buf, starting from buf.
Andy Green8f037e42010-12-19 22:13:26 +00001031 *
1032 * This function allows bulk sending of a packet to every connection using
Andy Greenb45993c2010-12-18 15:13:50 +00001033 * the given protocol. It does not send the data directly; instead it calls
1034 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
1035 * wants to actually send the data for that connection, the callback itself
1036 * should call libwebsocket_write().
1037 *
1038 * libwebsockets_broadcast() can be called from another fork context without
1039 * having to take any care about data visibility between the processes, it'll
1040 * "just work".
1041 */
1042
1043
1044int
Andy Green8f037e42010-12-19 22:13:26 +00001045libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
Andy Greenb45993c2010-12-18 15:13:50 +00001046 unsigned char *buf, size_t len)
1047{
Andy Green8f037e42010-12-19 22:13:26 +00001048 struct libwebsocket_context *this = protocol->owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00001049 int n;
1050
1051 if (!protocol->broadcast_socket_user_fd) {
1052 /*
Andy Greene92cd172011-01-19 13:11:55 +00001053 * We are either running unforked / flat, or we are being
1054 * called from poll thread context
Andy Greenb45993c2010-12-18 15:13:50 +00001055 * eg, from a callback. In that case don't use sockets for
1056 * broadcast IPC (since we can't open a socket connection to
1057 * a socket listening on our own thread) but directly do the
1058 * send action.
1059 *
1060 * Locking is not needed because we are by definition being
1061 * called in the poll thread context and are serialized.
1062 */
1063
1064 for (n = this->count_protocols + 1; n < this->fds_count; n++) {
1065
1066 if ((unsigned long)this->wsi[n] < LWS_MAX_PROTOCOLS)
1067 continue;
1068
1069 /* never broadcast to non-established connection */
Andy Greenb45993c2010-12-18 15:13:50 +00001070 if (this->wsi[n]->state != WSI_STATE_ESTABLISHED)
1071 continue;
1072
1073 /* only broadcast to guys using requested protocol */
Andy Greenb45993c2010-12-18 15:13:50 +00001074 if (this->wsi[n]->protocol != protocol)
1075 continue;
1076
Andy Green8f037e42010-12-19 22:13:26 +00001077 this->wsi[n]->protocol->callback(this->wsi[n],
1078 LWS_CALLBACK_BROADCAST,
Andy Greenb45993c2010-12-18 15:13:50 +00001079 this->wsi[n]->user_space,
1080 buf, len);
1081 }
1082
1083 return 0;
1084 }
1085
Andy Green0ca6a172010-12-19 20:50:01 +00001086 /*
1087 * We're being called from a different process context than the server
1088 * loop. Instead of broadcasting directly, we send our
1089 * payload on a socket to do the IPC; the server process will serialize
1090 * the broadcast action in its main poll() loop.
1091 *
1092 * There's one broadcast socket listening for each protocol supported
1093 * set up when the websocket server initializes
1094 */
1095
Andy Green6964bb52011-01-23 16:50:33 +00001096 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
Andy Greenb45993c2010-12-18 15:13:50 +00001097
1098 return n;
1099}