2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
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.
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.
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,
22 #include "private-libwebsockets.h"
25 libwebsocket_close_and_free_session(struct libwebsocket *wsi)
29 if ((unsigned long)wsi < LWS_MAX_PROTOCOLS)
34 wsi->state = WSI_STATE_DEAD_SOCKET;
36 if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED)
37 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
38 wsi->user_space, NULL, 0);
40 for (n = 0; n < WSI_TOKEN_COUNT; n++)
41 if (wsi->utf8_token[n].token)
42 free(wsi->utf8_token[n].token);
44 /* fprintf(stderr, "closing fd=%d\n", wsi->sock); */
46 #ifdef LWS_OPENSSL_SUPPORT
48 n = SSL_get_fd(wsi->ssl);
49 SSL_shutdown(wsi->ssl);
54 shutdown(wsi->sock, SHUT_RDWR);
56 #ifdef LWS_OPENSSL_SUPPORT
60 free(wsi->user_space);
66 libwebsocket_poll_connections(struct libwebsocket_context *this)
68 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BROADCAST_PAYLOAD +
69 LWS_SEND_BUFFER_POST_PADDING];
70 int client = this->count_protocols + 1;
71 struct libwebsocket *wsi;
75 /* check for activity on client sockets */
77 for (; client < this->fds_count; client++) {
79 /* handle session socket closed */
81 if (this->fds[client].revents & (POLLERR | POLLHUP)) {
83 debug("Session Socket %d %p (fd=%d) dead\n",
84 client, (void *)this->wsi[client],
85 this->fds[client].fd);
87 libwebsocket_close_and_free_session(this->wsi[client]);
91 /* the guy requested a callback when it was OK to write */
93 if ((unsigned long)this->wsi[client] > LWS_MAX_PROTOCOLS &&
94 this->fds[client].revents & POLLOUT) {
96 this->fds[client].events &= ~POLLOUT;
98 this->wsi[client]->protocol->callback(this->wsi[client],
99 LWS_CALLBACK_CLIENT_WRITEABLE,
100 this->wsi[client]->user_space,
104 /* any incoming data ready? */
106 if (!(this->fds[client].revents & POLLIN))
111 if ((unsigned long)this->wsi[client] < LWS_MAX_PROTOCOLS) {
113 /* get the issued broadcast payload from the socket */
115 len = read(this->fds[client].fd,
116 buf + LWS_SEND_BUFFER_PRE_PADDING,
117 MAX_BROADCAST_PAYLOAD);
121 "Error reading broadcast payload\n");
125 /* broadcast it to all guys with this protocol index */
127 for (n = this->count_protocols + 1;
128 n < this->fds_count; n++) {
132 if ((unsigned long)wsi < LWS_MAX_PROTOCOLS)
136 * never broadcast to non-established
140 if (wsi->state != WSI_STATE_ESTABLISHED)
143 /* only to clients connected to us */
145 if (wsi->client_mode)
149 * only broadcast to connections using
150 * the requested protocol
153 if (wsi->protocol->protocol_index !=
154 (int)(unsigned long)this->wsi[client])
157 /* broadcast it to this connection */
159 wsi->protocol->callback(wsi,
160 LWS_CALLBACK_BROADCAST,
162 buf + LWS_SEND_BUFFER_PRE_PADDING, len);
168 #ifdef LWS_OPENSSL_SUPPORT
169 if (this->wsi[client]->ssl)
170 n = SSL_read(this->wsi[client]->ssl, buf, sizeof buf);
173 n = recv(this->fds[client].fd, buf, sizeof buf, 0);
176 fprintf(stderr, "Socket read returned %d\n", n);
180 libwebsocket_close_and_free_session(this->wsi[client]);
184 /* service incoming data */
186 n = libwebsocket_read(this->wsi[client], buf, n);
190 * it closed and nuked wsi[client], so remove the
191 * socket handle and wsi from our service list
195 debug("nuking wsi %p, fsd_count = %d\n",
196 (void *)this->wsi[client], this->fds_count - 1);
199 for (n = client; n < this->fds_count; n++) {
200 this->fds[n] = this->fds[n + 1];
201 this->wsi[n] = this->wsi[n + 1];
212 * libwebsocket_context_destroy() - Destroy the websocket context
213 * @this: Websocket context
215 * This function closes any active connections and then frees the
216 * context. After calling this, any further use of the context is
220 libwebsocket_context_destroy(struct libwebsocket_context *this)
224 /* close listening skt and per-protocol broadcast sockets */
225 for (client = this->count_protocols + 1; client < this->fds_count; client++)
226 if (this->wsi[client]->client_mode)
227 libwebsocket_client_close(this->wsi[client]);
229 libwebsocket_close_and_free_session(this->wsi[client]);
231 #ifdef LWS_OPENSSL_SUPPORT
233 SSL_CTX_free(this->ssl_ctx);
241 * libwebsocket_service() - Service any pending websocket activity
242 * @this: Websocket context
243 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
244 * service otherwise block and service immediately, returning
245 * after the timeout if nothing needed service.
247 * This function deals with any pending websocket traffic, for three
248 * kinds of event. It handles these events on both server and client
249 * types of connection the same.
251 * 1) Accept new connections to our context's server
253 * 2) Perform pending broadcast writes initiated from other forked
254 * processes (effectively serializing asynchronous broadcasts)
256 * 3) Call the receive callback for incoming frame data received by
257 * server or client connections.
259 * You need to call this service function periodically to all the above
260 * functions to happen; if your application is single-threaded you can
261 * just call it in your main event loop.
263 * Alternatively you can fork a new process that asynchronously handles
264 * calling this service in a loop. In that case you are happy if this
265 * call blocks your thread until it needs to take care of something and
266 * would call it with a large nonzero timeout. Your loop then takes no
267 * CPU while there is nothing happening.
269 * If you are calling it in a single-threaded app, you don't want it to
270 * wait around blocking other things in your loop from happening, so you
271 * would call it with a timeout_ms of 0, so it returns immediately if
272 * nothing is pending, or as soon as it services whatever was pending.
277 libwebsocket_service(struct libwebsocket_context *this, int timeout_ms)
282 struct sockaddr_in cli_addr;
285 /* stay dead once we are dead */
290 /* don't check listen socket if we are not listening */
292 if (this->listen_port)
293 n = poll(this->fds, this->fds_count, timeout_ms);
295 n = poll(&this->fds[1], this->fds_count - 1, timeout_ms);
298 if (n < 0 || this->fds[0].revents & (POLLERR | POLLHUP)) {
299 fprintf(stderr, "Listen Socket dead\n");
302 if (n == 0) /* poll timeout */
305 /* handle accept on listening socket? */
307 for (client = 0; client < this->count_protocols + 1; client++) {
309 if (!this->fds[client].revents & POLLIN)
312 /* listen socket got an unencrypted connection... */
314 clilen = sizeof(cli_addr);
315 fd = accept(this->fds[client].fd,
316 (struct sockaddr *)&cli_addr, &clilen);
318 fprintf(stderr, "ERROR on accept");
322 if (this->fds_count >= MAX_CLIENTS) {
323 fprintf(stderr, "too busy");
330 * accepting a connection to broadcast socket
331 * set wsi to be protocol index not pointer
334 this->wsi[this->fds_count] =
335 (struct libwebsocket *)(long)(client - 1);
340 /* accepting connection to main listener */
342 this->wsi[this->fds_count] =
343 malloc(sizeof(struct libwebsocket));
344 if (!this->wsi[this->fds_count]) {
345 fprintf(stderr, "Out of memory for new connection\n");
349 #ifdef LWS_OPENSSL_SUPPORT
350 this->wsi[this->fds_count]->ssl = NULL;
354 this->wsi[this->fds_count]->ssl =
355 SSL_new(this->ssl_ctx);
356 if (this->wsi[this->fds_count]->ssl == NULL) {
357 fprintf(stderr, "SSL_new failed: %s\n",
358 ERR_error_string(SSL_get_error(
359 this->wsi[this->fds_count]->ssl, 0),
361 free(this->wsi[this->fds_count]);
365 SSL_set_fd(this->wsi[this->fds_count]->ssl, fd);
367 n = SSL_accept(this->wsi[this->fds_count]->ssl);
370 * browsers seem to probe with various
371 * ssl params which fail then retry
374 debug("SSL_accept failed skt %u: %s\n",
376 ERR_error_string(SSL_get_error(
377 this->wsi[this->fds_count]->ssl,
380 this->wsi[this->fds_count]->ssl);
381 free(this->wsi[this->fds_count]);
384 debug("accepted new SSL conn "
385 "port %u on fd=%d SSL ver %s\n",
386 ntohs(cli_addr.sin_port), fd,
387 SSL_get_version(this->wsi[
388 this->fds_count]->ssl));
392 debug("accepted new conn port %u on fd=%d\n",
393 ntohs(cli_addr.sin_port), fd);
395 /* intialize the instance struct */
397 this->wsi[this->fds_count]->sock = fd;
398 this->wsi[this->fds_count]->state = WSI_STATE_HTTP;
399 this->wsi[this->fds_count]->name_buffer_pos = 0;
400 this->wsi[this->fds_count]->client_mode = 0;
402 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
403 this->wsi[this->fds_count]->
404 utf8_token[n].token = NULL;
405 this->wsi[this->fds_count]->
406 utf8_token[n].token_len = 0;
410 * these can only be set once the protocol is known
411 * we set an unestablished connection's protocol pointer
412 * to the start of the supported list, so it can look
413 * for matching ones during the handshake
415 this->wsi[this->fds_count]->protocol = this->protocols;
416 this->wsi[this->fds_count]->user_space = NULL;
419 * Default protocol is 76 / 00
420 * After 76, there's a header specified to inform which
421 * draft the client wants, when that's seen we modify
422 * the individual connection's spec revision accordingly
424 this->wsi[this->fds_count]->ietf_spec_revision = 0;
429 * make sure NO events are seen yet on this new socket
430 * (otherwise we inherit old fds[client].revents from
431 * previous socket there and die mysteriously! )
433 this->fds[this->fds_count].revents = 0;
435 this->fds[this->fds_count].events = POLLIN;
436 this->fds[this->fds_count++].fd = fd;
440 /* service anything incoming on websocket connection */
442 libwebsocket_poll_connections(this);
444 /* this round is done */
450 fprintf(stderr, "service hits fatal\n");
452 /* close listening skt and per-protocol broadcast sockets */
453 for (client = 0; client < this->fds_count; client++)
454 close(this->fds[0].fd);
456 #ifdef LWS_OPENSSL_SUPPORT
458 SSL_CTX_free(this->ssl_ctx);
466 /* inform caller we are dead */
472 * libwebsocket_callback_on_writable() - Request a callback when this socket
473 * becomes able to be written to without
476 * @wsi: Websocket connection instance to get callback for
480 libwebsocket_callback_on_writable(struct libwebsocket *wsi)
482 struct libwebsocket_context *this = wsi->protocol->owning_server;
485 for (n = this->count_protocols + 1; n < this->fds_count; n++)
486 if (this->wsi[n] == wsi) {
487 this->fds[n].events |= POLLOUT;
491 fprintf(stderr, "libwebsocket_callback_on_writable "
492 "didn't find socket\n");
497 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
498 * all connections using the given protocol when it
499 * becomes possible to write to each socket without
502 * @protocol: Protocol whose connections will get callbacks
506 libwebsocket_callback_on_writable_all_protocol(
507 const struct libwebsocket_protocols *protocol)
509 struct libwebsocket_context *this = protocol->owning_server;
512 for (n = this->count_protocols + 1; n < this->fds_count; n++)
513 if ((unsigned long)this->wsi[n] > LWS_MAX_PROTOCOLS)
514 if (this->wsi[n]->protocol == protocol)
515 this->fds[n].events |= POLLOUT;
522 * libwebsocket_get_socket_fd() - returns the socket file descriptor
524 * You will not need this unless you are doing something special
526 * @wsi: Websocket connection instance
530 libwebsocket_get_socket_fd(struct libwebsocket *wsi)
536 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
539 * If the output side of a server process becomes choked, this allows flow
540 * control for the input side.
542 * @wsi: Websocket connection instance to get callback for
543 * @enable: 0 = disable read servicing for this connection, 1 = enable
547 libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
549 struct libwebsocket_context *this = wsi->protocol->owning_server;
552 for (n = this->count_protocols + 1; n < this->fds_count; n++)
553 if (this->wsi[n] == wsi) {
555 this->fds[n].events |= POLLIN;
557 this->fds[n].events &= ~POLLIN;
562 fprintf(stderr, "libwebsocket_callback_on_writable "
563 "unable to find socket\n");
568 * libwebsocket_canonical_hostname() - returns this host's hostname
570 * This is typically used by client code to fill in the host parameter
571 * when making a client connection. You can only call it after the context
574 * @this: Websocket context
579 libwebsocket_canonical_hostname(struct libwebsocket_context *this)
581 return (const char *)this->canonical_hostname;
585 static void sigpipe_handler(int x)
591 * libwebsocket_create_context() - Create the websocket handler
592 * @port: Port to listen on... you can use 0 to suppress listening on
593 * any port, that's what you want if you are not running a
594 * websocket server at all but just using it as a client
595 * @protocols: Array of structures listing supported protocols and a protocol-
596 * specific callback for each one. The list is ended with an
597 * entry that has a NULL callback pointer.
598 * It's not const because we write the owning_server member
599 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
600 * to listen using SSL, set to the filepath to fetch the
601 * server cert from, otherwise NULL for unencrypted
602 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
604 * @gid: group id to change to after setting listen socket, or -1.
605 * @uid: user id to change to after setting listen socket, or -1.
607 * This function creates the listening socket and takes care
608 * of all initialization in one step.
610 * After initialization, it returns a struct libwebsocket_context * that
611 * represents this server. After calling, user code needs to take care
612 * of calling libwebsocket_service() with the context pointer to get the
613 * server's sockets serviced. This can be done in the same process context
614 * or a forked process, or another thread,
616 * The protocol callback functions are called for a handful of events
617 * including http requests coming in, websocket connections becoming
618 * established, and data arriving; it's also called periodically to allow
619 * async transmission.
621 * HTTP requests are sent always to the FIRST protocol in @protocol, since
622 * at that time websocket protocol has not been negotiated. Other
623 * protocols after the first one never see any HTTP callack activity.
625 * The server created is a simple http server by default; part of the
626 * websocket standard is upgrading this http connection to a websocket one.
628 * This allows the same server to provide files like scripts and favicon /
629 * images or whatever over http and dynamic data over websockets all in
630 * one place; they're all handled in the user callback.
633 struct libwebsocket_context *
634 libwebsocket_create_context(int port,
635 struct libwebsocket_protocols *protocols,
636 const char *ssl_cert_filepath,
637 const char *ssl_private_key_filepath,
638 int gid, int uid, unsigned int options)
643 struct sockaddr_in serv_addr, cli_addr;
645 struct libwebsocket_context *this = NULL;
651 #ifdef LWS_OPENSSL_SUPPORT
653 char ssl_err_buf[512];
656 this = malloc(sizeof(struct libwebsocket_context));
658 fprintf(stderr, "No memory for websocket context\n");
661 this->protocols = protocols;
662 this->listen_port = port;
663 this->http_proxy_port = 0;
664 this->http_proxy_address[0] = '\0';
665 this->options = options;
667 /* find canonical hostname */
669 hostname[(sizeof hostname) - 1] = '\0';
670 gethostname(hostname, (sizeof hostname) - 1);
671 he = gethostbyname(hostname);
672 strncpy(this->canonical_hostname, he->h_name,
673 sizeof this->canonical_hostname - 1);
674 this->canonical_hostname[sizeof this->canonical_hostname - 1] = '\0';
676 /* split the proxy ads:port if given */
678 p = getenv("http_proxy");
680 strncpy(this->http_proxy_address, p,
681 sizeof this->http_proxy_address - 1);
682 this->http_proxy_address[
683 sizeof this->http_proxy_address - 1] = '\0';
685 p = strchr(this->http_proxy_address, ':');
687 fprintf(stderr, "http_proxy needs to be ads:port\n");
691 this->http_proxy_port = atoi(p + 1);
693 fprintf(stderr, "Using proxy %s:%u\n",
694 this->http_proxy_address,
695 this->http_proxy_port);
700 #ifdef LWS_OPENSSL_SUPPORT
701 this->use_ssl = ssl_cert_filepath != NULL &&
702 ssl_private_key_filepath != NULL;
704 fprintf(stderr, " Compiled with SSL support, "
707 fprintf(stderr, " Compiled with SSL support, "
711 if (ssl_cert_filepath != NULL &&
712 ssl_private_key_filepath != NULL) {
713 fprintf(stderr, " Not compiled for OpenSSl support!\n");
716 fprintf(stderr, " Compiled without SSL support, "
717 "serving unencrypted\n");
723 signal(SIGPIPE, sigpipe_handler);
726 #ifdef LWS_OPENSSL_SUPPORT
728 /* basic openssl init */
732 OpenSSL_add_all_algorithms();
733 SSL_load_error_strings();
736 * Firefox insists on SSLv23 not SSLv3
737 * Konq disables SSLv2 by default now, SSLv23 works
740 method = (SSL_METHOD *)SSLv23_server_method();
742 fprintf(stderr, "problem creating ssl method: %s\n",
743 ERR_error_string(ERR_get_error(), ssl_err_buf));
746 this->ssl_ctx = SSL_CTX_new(method); /* create context */
747 if (!this->ssl_ctx) {
748 fprintf(stderr, "problem creating ssl context: %s\n",
749 ERR_error_string(ERR_get_error(), ssl_err_buf));
755 method = (SSL_METHOD *)SSLv23_client_method();
757 fprintf(stderr, "problem creating ssl method: %s\n",
758 ERR_error_string(ERR_get_error(), ssl_err_buf));
761 this->ssl_client_ctx = SSL_CTX_new(method); /* create context */
762 if (!this->ssl_client_ctx) {
763 fprintf(stderr, "problem creating ssl context: %s\n",
764 ERR_error_string(ERR_get_error(), ssl_err_buf));
769 /* openssl init for cert verification (used with client sockets) */
771 if (!SSL_CTX_load_verify_locations(this->ssl_client_ctx, NULL,
772 LWS_OPENSSL_CLIENT_CERTS)) {
773 fprintf(stderr, "Unable to load SSL Client certs from %s "
774 "(set by --with-client-cert-dir= in configure) -- "
775 " client ssl isn't going to work",
776 LWS_OPENSSL_CLIENT_CERTS);
781 /* openssl init for server sockets */
783 /* set the local certificate from CertFile */
784 n = SSL_CTX_use_certificate_file(this->ssl_ctx,
785 ssl_cert_filepath, SSL_FILETYPE_PEM);
787 fprintf(stderr, "problem getting cert '%s': %s\n",
789 ERR_error_string(ERR_get_error(), ssl_err_buf));
792 /* set the private key from KeyFile */
793 if (SSL_CTX_use_PrivateKey_file(this->ssl_ctx,
794 ssl_private_key_filepath,
795 SSL_FILETYPE_PEM) != 1) {
796 fprintf(stderr, "ssl problem getting key '%s': %s\n",
797 ssl_private_key_filepath,
798 ERR_error_string(ERR_get_error(), ssl_err_buf));
801 /* verify private key */
802 if (!SSL_CTX_check_private_key(this->ssl_ctx)) {
803 fprintf(stderr, "Private SSL key doesn't match cert\n");
807 /* SSL is happy and has a cert it's content with */
813 if (lws_b64_selftest())
816 /* set up our external listening socket we serve on */
820 sockfd = socket(AF_INET, SOCK_STREAM, 0);
822 fprintf(stderr, "ERROR opening socket");
826 /* allow us to restart even if old sockets in TIME_WAIT */
827 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
829 bzero((char *) &serv_addr, sizeof(serv_addr));
830 serv_addr.sin_family = AF_INET;
831 serv_addr.sin_addr.s_addr = INADDR_ANY;
832 serv_addr.sin_port = htons(port);
834 n = bind(sockfd, (struct sockaddr *) &serv_addr,
837 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
843 /* drop any root privs for this process */
847 fprintf(stderr, "setgid: %s\n", strerror(errno));
850 fprintf(stderr, "setuid: %s\n", strerror(errno));
853 * prepare the poll() fd array... it's like this
855 * [0] = external listening socket
856 * [1 .. this->count_protocols] = per-protocol broadcast sockets
857 * [this->count_protocols + 1 ... this->fds_count-1] = connection skts
861 this->fds[0].fd = sockfd;
862 this->fds[0].events = POLLIN;
863 this->count_protocols = 0;
867 fprintf(stderr, " Listening on port %d\n", port);
870 /* set up our internal broadcast trigger sockets per-protocol */
872 for (; protocols[this->count_protocols].callback;
873 this->count_protocols++) {
874 protocols[this->count_protocols].owning_server = this;
875 protocols[this->count_protocols].protocol_index =
876 this->count_protocols;
878 fd = socket(AF_INET, SOCK_STREAM, 0);
880 fprintf(stderr, "ERROR opening socket");
884 /* allow us to restart even if old sockets in TIME_WAIT */
885 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
887 bzero((char *) &serv_addr, sizeof(serv_addr));
888 serv_addr.sin_family = AF_INET;
889 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
890 serv_addr.sin_port = 0; /* pick the port for us */
892 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
894 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
899 slen = sizeof cli_addr;
900 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
902 fprintf(stderr, "getsockname failed\n");
905 protocols[this->count_protocols].broadcast_socket_port =
906 ntohs(cli_addr.sin_port);
909 debug(" Protocol %s broadcast socket %d\n",
910 protocols[this->count_protocols].name,
911 ntohs(cli_addr.sin_port));
913 this->fds[this->fds_count].fd = fd;
914 this->fds[this->fds_count].events = POLLIN;
915 /* wsi only exists for connections, not broadcast listener */
916 this->wsi[this->fds_count] = NULL;
927 * libwebsockets_fork_service_loop() - Optional helper function forks off
928 * a process for the websocket server loop.
929 * You don't have to use this but if not, you
930 * have to make sure you are calling
931 * libwebsocket_service periodically to service
932 * the websocket traffic
933 * @this: server context returned by creation function
937 libwebsockets_fork_service_loop(struct libwebsocket_context *this)
941 struct sockaddr_in cli_addr;
950 /* main process context */
952 for (client = 1; client < this->count_protocols + 1; client++) {
953 fd = socket(AF_INET, SOCK_STREAM, 0);
955 fprintf(stderr, "Unable to create socket\n");
958 cli_addr.sin_family = AF_INET;
959 cli_addr.sin_port = htons(
960 this->protocols[client - 1].broadcast_socket_port);
961 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
962 n = connect(fd, (struct sockaddr *)&cli_addr,
965 fprintf(stderr, "Unable to connect to "
966 "broadcast socket %d, %s\n",
967 client, strerror(errno));
971 this->protocols[client - 1].broadcast_socket_user_fd =
979 /* we want a SIGHUP when our parent goes down */
980 prctl(PR_SET_PDEATHSIG, SIGHUP);
982 /* in this forked process, sit and service websocket connections */
985 if (libwebsocket_service(this, 1000))
994 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
996 * @wsi: pointer to struct websocket you want to know the protocol of
999 * This is useful to get the protocol to broadcast back to from inside
1003 const struct libwebsocket_protocols *
1004 libwebsockets_get_protocol(struct libwebsocket *wsi)
1006 return wsi->protocol;
1010 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
1011 * connections of the given protocol.
1012 * @protocol: pointer to the protocol you will broadcast to all members of
1013 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
1014 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
1015 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
1016 * case you are calling this function from callback context.
1017 * @len: length of payload data in buf, starting from buf.
1019 * This function allows bulk sending of a packet to every connection using
1020 * the given protocol. It does not send the data directly; instead it calls
1021 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
1022 * wants to actually send the data for that connection, the callback itself
1023 * should call libwebsocket_write().
1025 * libwebsockets_broadcast() can be called from another fork context without
1026 * having to take any care about data visibility between the processes, it'll
1032 libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
1033 unsigned char *buf, size_t len)
1035 struct libwebsocket_context *this = protocol->owning_server;
1038 if (!protocol->broadcast_socket_user_fd) {
1040 * We are either running unforked / flat, or we are being
1041 * called from poll thread context
1042 * eg, from a callback. In that case don't use sockets for
1043 * broadcast IPC (since we can't open a socket connection to
1044 * a socket listening on our own thread) but directly do the
1047 * Locking is not needed because we are by definition being
1048 * called in the poll thread context and are serialized.
1051 for (n = this->count_protocols + 1; n < this->fds_count; n++) {
1053 if ((unsigned long)this->wsi[n] < LWS_MAX_PROTOCOLS)
1056 /* never broadcast to non-established connection */
1057 if (this->wsi[n]->state != WSI_STATE_ESTABLISHED)
1060 /* only broadcast to guys using requested protocol */
1061 if (this->wsi[n]->protocol != protocol)
1064 this->wsi[n]->protocol->callback(this->wsi[n],
1065 LWS_CALLBACK_BROADCAST,
1066 this->wsi[n]->user_space,
1074 * We're being called from a different process context than the server
1075 * loop. Instead of broadcasting directly, we send our
1076 * payload on a socket to do the IPC; the server process will serialize
1077 * the broadcast action in its main poll() loop.
1079 * There's one broadcast socket listening for each protocol supported
1080 * set up when the websocket server initializes
1083 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);