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"
31 #ifdef LWS_OPENSSL_SUPPORT
32 int openssl_websocket_private_data_index;
36 * In-place str to lower case
48 /* file descriptor hash management */
51 wsi_from_fd(struct libwebsocket_context *context, int fd)
53 int h = LWS_FD_HASH(fd);
56 for (n = 0; n < context->fd_hashtable[h].length; n++)
57 if (context->fd_hashtable[h].wsi[n]->sock == fd)
58 return context->fd_hashtable[h].wsi[n];
64 insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi)
66 int h = LWS_FD_HASH(wsi->sock);
68 if (context->fd_hashtable[h].length == MAX_CLIENTS - 1) {
69 fprintf(stderr, "hash table overflow\n");
73 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
79 delete_from_fd(struct libwebsocket_context *context, int fd)
81 int h = LWS_FD_HASH(fd);
84 for (n = 0; n < context->fd_hashtable[h].length; n++)
85 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
86 while (n < context->fd_hashtable[h].length) {
87 context->fd_hashtable[h].wsi[n] =
88 context->fd_hashtable[h].wsi[n + 1];
91 context->fd_hashtable[h].length--;
96 fprintf(stderr, "Failed to find fd %d requested for "
97 "delete in hashtable\n", fd);
101 #ifdef LWS_OPENSSL_SUPPORT
103 libwebsockets_decode_ssl_error(void)
108 while ((err = ERR_get_error()) != 0) {
109 ERR_error_string_n(err, buf, sizeof(buf));
110 fprintf(stderr, "*** %s\n", buf);
117 interface_to_sa(const char* ifname, struct sockaddr_in *addr, size_t addrlen)
125 struct sockaddr_in *sin;
128 for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
129 if (strcmp(ifc->ifa_name, ifname))
131 if (ifc->ifa_addr == NULL)
133 sin = (struct sockaddr_in *)ifc->ifa_addr;
134 if (sin->sin_family != AF_INET)
136 memcpy(addr, sin, addrlen);
146 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
147 struct libwebsocket *wsi, enum lws_close_status reason)
151 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
152 LWS_SEND_BUFFER_POST_PADDING];
155 struct lws_tokens eff_buf;
156 struct libwebsocket_extension *ext;
161 old_state = wsi->state;
163 if (old_state == WSI_STATE_DEAD_SOCKET)
166 wsi->close_reason = reason;
169 * flush any tx pending from extensions, since we may send close packet
170 * if there are problems with send, just nuke the connection
176 /* default to nobody has more to spill */
179 eff_buf.token = NULL;
180 eff_buf.token_len = 0;
182 /* show every extension the new incoming data */
184 for (n = 0; n < wsi->count_active_extensions; n++) {
185 m = wsi->active_extensions[n]->callback(
186 wsi->protocol->owning_server,
187 wsi->active_extensions[n], wsi,
188 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
189 wsi->active_extensions_user[n], &eff_buf, 0);
191 fprintf(stderr, "Extension reports "
193 goto just_kill_connection;
197 * at least one extension told us he has more
198 * to spill, so we will go around again after
203 /* assuming they left us something to send, send it */
205 if (eff_buf.token_len)
206 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
208 goto just_kill_connection;
212 * signal we are closing, libsocket_write will
213 * add any necessary version-specific stuff. If the write fails,
214 * no worries we are closing anyway. If we didn't initiate this
215 * close, then our state has been changed to
216 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
218 * Likewise if it's a second call to close this connection after we
219 * sent the close indication to the peer already, we are in state
220 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
223 if (old_state == WSI_STATE_ESTABLISHED &&
224 reason != LWS_CLOSE_STATUS_NOSTATUS) {
225 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
229 * we have sent a nice protocol level indication we
230 * now wish to close, we should not send anything more
233 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
235 /* and we should wait for a reply for a bit */
237 libwebsocket_set_timeout(wsi,
238 PENDING_TIMEOUT_CLOSE_ACK, 5);
240 fprintf(stderr, "sent close indication, awaiting ack\n");
245 /* else, the send failed and we should just hang up */
248 just_kill_connection:
250 * we won't be servicing or receiving anything further from this guy
251 * remove this fd from wsi mapping hashtable
255 delete_from_fd(context, wsi->sock);
257 /* delete it from the internal poll list if still present */
259 for (n = 0; n < context->fds_count; n++) {
260 if (context->fds[n].fd != wsi->sock)
262 while (n < context->fds_count - 1) {
263 context->fds[n] = context->fds[n + 1];
266 context->fds_count--;
267 /* we only have to deal with one */
268 n = context->fds_count;
271 /* remove also from external POLL support via protocol 0 */
273 context->protocols[0].callback(context, wsi,
274 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
276 wsi->state = WSI_STATE_DEAD_SOCKET;
278 /* tell the user it's all over for this guy */
280 if (wsi->protocol && wsi->protocol->callback &&
281 old_state == WSI_STATE_ESTABLISHED)
282 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
283 wsi->user_space, NULL, 0);
285 /* deallocate any active extension contexts */
287 for (n = 0; n < wsi->count_active_extensions; n++) {
288 if (!wsi->active_extensions[n]->callback)
291 wsi->active_extensions[n]->callback(context,
292 wsi->active_extensions[n], wsi,
293 LWS_EXT_CALLBACK_DESTROY,
294 wsi->active_extensions_user[n], NULL, 0);
296 free(wsi->active_extensions_user[n]);
300 * inform all extensions in case they tracked this guy out of band
301 * even though not active on him specifically
304 ext = context->extensions;
305 while (ext && ext->callback) {
306 ext->callback(context, ext, wsi,
307 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
312 /* free up his parsing allocations */
314 for (n = 0; n < WSI_TOKEN_COUNT; n++)
315 if (wsi->utf8_token[n].token)
316 free(wsi->utf8_token[n].token);
319 free(wsi->c_address);
321 /* fprintf(stderr, "closing fd=%d\n", wsi->sock); */
323 #ifdef LWS_OPENSSL_SUPPORT
325 n = SSL_get_fd(wsi->ssl);
326 SSL_shutdown(wsi->ssl);
335 shutdown(wsi->sock, SHUT_RDWR);
337 closesocket(wsi->sock);
341 #ifdef LWS_OPENSSL_SUPPORT
345 free(wsi->user_space);
351 * libwebsockets_hangup_on_client() - Server calls to terminate client
353 * @context: libwebsockets context
354 * @fd: Connection socket descriptor
358 libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
360 struct libwebsocket *wsi = wsi_from_fd(context, fd);
365 libwebsocket_close_and_free_session(context, wsi,
366 LWS_CLOSE_STATUS_NOSTATUS);
371 * libwebsockets_get_peer_addresses() - Get client address information
372 * @fd: Connection socket descriptor
373 * @name: Buffer to take client address name
374 * @name_len: Length of client address name buffer
375 * @rip: Buffer to take client address IP qotted quad
376 * @rip_len: Length of client address IP buffer
378 * This function fills in @name and @rip with the name and IP of
379 * the client connected with socket descriptor @fd. Names may be
380 * truncated if there is not enough room. If either cannot be
381 * determined, they will be returned as valid zero-length strings.
385 libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
386 char *rip, int rip_len)
389 struct sockaddr_in sin;
390 struct hostent *host;
391 struct hostent *host1;
395 struct sockaddr_un *un;
401 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
402 perror("getpeername");
406 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
409 perror("gethostbyaddr");
413 strncpy(name, host->h_name, name_len);
414 name[name_len - 1] = '\0';
416 host1 = gethostbyname(host->h_name);
419 p = (unsigned char *)host1;
422 p = (unsigned char *)host1->h_addr_list[n++];
425 if ((host1->h_addrtype != AF_INET)
427 && (host1->h_addrtype != AF_LOCAL)
432 if (host1->h_addrtype == AF_INET)
433 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
436 un = (struct sockaddr_un *)p;
437 strncpy(ip, un->sun_path, sizeof(ip) -1);
438 ip[sizeof(ip) - 1] = '\0';
442 strncpy(rip, ip, rip_len);
443 rip[rip_len - 1] = '\0';
447 int libwebsockets_get_random(struct libwebsocket_context *context,
454 for (n = 0; n < len; n++)
455 p[n] = (unsigned char)rand();
457 n = read(context->fd_random, p, len);
464 libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md)
466 return SHA1(d, n, md);
469 void libwebsockets_00_spaceout(char *key, int spaces, int seed)
475 if (*key && (seed & 1))
479 p = key + strlen(key);
488 void libwebsockets_00_spam(char *key, int count, int seed)
495 if (*key && (seed & 1))
499 p = key + strlen(key);
504 *key++ = 0x21 + ((seed & 0xffff) % 15);
505 /* 4 would use it up too fast.. not like it matters */
510 int lws_send_pipe_choked(struct libwebsocket *wsi)
515 fds.events = POLLOUT;
518 if (poll(&fds, 1, 0) != 1)
521 if ((fds.revents & POLLOUT) == 0)
524 /* okay to send another packet without blocking */
530 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
531 struct libwebsocket *wsi, struct pollfd *pollfd)
533 struct lws_tokens eff_buf;
539 for (n = 0; n < wsi->count_active_extensions; n++) {
540 if (!wsi->active_extensions[n]->callback)
543 m = wsi->active_extensions[n]->callback(context,
544 wsi->active_extensions[n], wsi,
545 LWS_EXT_CALLBACK_IS_WRITEABLE,
546 wsi->active_extensions_user[n], NULL, 0);
554 if (!wsi->extension_data_pending || handled == 2)
558 * check in on the active extensions, see if they
559 * had pending stuff to spill... they need to get the
560 * first look-in otherwise sequence will be disordered
562 * NULL, zero-length eff_buf means just spill pending
568 /* default to nobody has more to spill */
571 eff_buf.token = NULL;
572 eff_buf.token_len = 0;
574 /* give every extension a chance to spill */
576 for (n = 0; n < wsi->count_active_extensions; n++) {
577 m = wsi->active_extensions[n]->callback(
578 wsi->protocol->owning_server,
579 wsi->active_extensions[n], wsi,
580 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
581 wsi->active_extensions_user[n], &eff_buf, 0);
583 fprintf(stderr, "extension reports fatal error\n");
588 * at least one extension told us he has more
589 * to spill, so we will go around again after
594 /* assuming they gave us something to send, send it */
596 if (eff_buf.token_len) {
597 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
603 /* no extension has more to spill */
609 * There's more to spill from an extension, but we just sent
610 * something... did that leave the pipe choked?
613 if (!lws_send_pipe_choked(wsi))
614 /* no we could add more */
617 fprintf(stderr, "choked in POLLOUT service\n");
620 * Yes, he's choked. Leave the POLLOUT masked on so we will
621 * come back here when he is unchoked. Don't call the user
622 * callback to enforce ordering of spilling, he'll get called
623 * when we come back here and there's nothing more to spill.
629 wsi->extension_data_pending = 0;
635 pollfd->events &= ~POLLOUT;
637 /* external POLL support via protocol 0 */
638 context->protocols[0].callback(context, wsi,
639 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
640 (void *)(long)wsi->sock, NULL, POLLOUT);
645 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
646 n = LWS_CALLBACK_CLIENT_WRITEABLE;
648 n = LWS_CALLBACK_SERVER_WRITEABLE;
650 wsi->protocol->callback(context, wsi, n, wsi->user_space, NULL, 0);
658 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
659 struct libwebsocket *wsi, unsigned int sec)
664 * if extensions want in on it (eg, we are a mux parent)
665 * give them a chance to service child timeouts
668 for (n = 0; n < wsi->count_active_extensions; n++)
669 wsi->active_extensions[n]->callback(
670 context, wsi->active_extensions[n],
671 wsi, LWS_EXT_CALLBACK_1HZ,
672 wsi->active_extensions_user[n], NULL, sec);
674 if (!wsi->pending_timeout)
678 * if we went beyond the allowed time, kill the
682 if (sec > wsi->pending_timeout_limit) {
683 fprintf(stderr, "TIMEDOUT WAITING\n");
684 libwebsocket_close_and_free_session(context,
685 wsi, LWS_CLOSE_STATUS_NOSTATUS);
689 struct libwebsocket *
690 libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
692 struct libwebsocket *new_wsi;
695 new_wsi = malloc(sizeof(struct libwebsocket));
696 if (new_wsi == NULL) {
697 fprintf(stderr, "Out of memory for new connection\n");
701 memset(new_wsi, 0, sizeof (struct libwebsocket));
702 new_wsi->count_active_extensions = 0;
703 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
705 /* intialize the instance struct */
707 new_wsi->state = WSI_STATE_HTTP;
708 new_wsi->name_buffer_pos = 0;
709 new_wsi->mode = LWS_CONNMODE_WS_SERVING;
711 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
712 new_wsi->utf8_token[n].token = NULL;
713 new_wsi->utf8_token[n].token_len = 0;
717 * these can only be set once the protocol is known
718 * we set an unestablished connection's protocol pointer
719 * to the start of the supported list, so it can look
720 * for matching ones during the handshake
722 new_wsi->protocol = context->protocols;
723 new_wsi->user_space = NULL;
726 * Default protocol is 76 / 00
727 * After 76, there's a header specified to inform which
728 * draft the client wants, when that's seen we modify
729 * the individual connection's spec revision accordingly
731 new_wsi->ietf_spec_revision = 0;
737 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
738 struct libwebsocket *wsi, char *pkt)
743 struct libwebsocket_extension *ext;
745 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
746 LWS_SEND_BUFFER_POST_PADDING];
747 static const char magic_websocket_guid[] =
748 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
751 * create the random key
754 n = libwebsockets_get_random(context, hash, 16);
756 fprintf(stderr, "Unable to read from random dev %s\n",
757 SYSTEM_RANDOM_FILEPATH);
763 free(wsi->c_protocol);
764 libwebsocket_close_and_free_session(context, wsi,
765 LWS_CLOSE_STATUS_NOSTATUS);
769 lws_b64_encode_string(hash, 16, wsi->key_b64,
770 sizeof wsi->key_b64);
773 * 00 example client handshake
775 * GET /socket.io/websocket HTTP/1.1
777 * Connection: Upgrade
778 * Host: 127.0.0.1:9999
779 * Origin: http://127.0.0.1
780 * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^
781 * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
782 * Cookie: socketio=websocket
786 * 04 example client handshake
789 * Host: server.example.com
791 * Connection: Upgrade
792 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
793 * Sec-WebSocket-Origin: http://example.com
794 * Sec-WebSocket-Protocol: chat, superchat
795 * Sec-WebSocket-Version: 4
798 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
800 if (wsi->ietf_spec_revision == 0) {
801 unsigned char spaces_1, spaces_2;
802 unsigned int max_1, max_2;
803 unsigned int num_1, num_2;
804 unsigned long product_1, product_2;
811 libwebsockets_get_random(context, &spaces_1,
813 libwebsockets_get_random(context, &spaces_2,
816 spaces_1 = (spaces_1 % 12) + 1;
817 spaces_2 = (spaces_2 % 12) + 1;
819 max_1 = 4294967295 / spaces_1;
820 max_2 = 4294967295 / spaces_2;
822 libwebsockets_get_random(context, &num_1, sizeof(int));
823 libwebsockets_get_random(context, &num_2, sizeof(int));
825 num_1 = (num_1 % max_1);
826 num_2 = (num_2 % max_2);
828 challenge[0] = num_1 >> 24;
829 challenge[1] = num_1 >> 16;
830 challenge[2] = num_1 >> 8;
831 challenge[3] = num_1;
832 challenge[4] = num_2 >> 24;
833 challenge[5] = num_2 >> 16;
834 challenge[6] = num_2 >> 8;
835 challenge[7] = num_2;
837 product_1 = num_1 * spaces_1;
838 product_2 = num_2 * spaces_2;
840 sprintf(key_1, "%lu", product_1);
841 sprintf(key_2, "%lu", product_2);
843 libwebsockets_get_random(context, &seed, sizeof(int));
844 libwebsockets_get_random(context, &count, sizeof(int));
846 libwebsockets_00_spam(key_1, (count % 12) + 1, seed);
848 libwebsockets_get_random(context, &seed, sizeof(int));
849 libwebsockets_get_random(context, &count, sizeof(int));
851 libwebsockets_00_spam(key_2, (count % 12) + 1, seed);
853 libwebsockets_get_random(context, &seed, sizeof(int));
855 libwebsockets_00_spaceout(key_1, spaces_1, seed);
856 libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16);
858 p += sprintf(p, "Upgrade: WebSocket\x0d\x0a"
859 "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
862 p += sprintf(p, "Origin: %s\x0d\x0a",
866 p += sprintf(p, "Sec-WebSocket-Protocol: %s"
867 "\x0d\x0a", wsi->c_protocol);
869 p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a",
871 p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a",
874 /* give userland a chance to append, eg, cookies */
876 context->protocols[0].callback(context, wsi,
877 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
878 NULL, &p, (pkt + sizeof(pkt)) - p - 12);
880 p += sprintf(p, "\x0d\x0a");
882 if (libwebsockets_get_random(context, p, 8) != 8)
884 memcpy(&challenge[8], p, 8);
887 /* precompute what we want to see from the server */
889 MD5((unsigned char *)challenge, 16,
890 (unsigned char *)wsi->initial_handshake_hash_base64);
895 p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
896 p += sprintf(p, "Upgrade: websocket\x0d\x0a");
897 p += sprintf(p, "Connection: Upgrade\x0d\x0a"
898 "Sec-WebSocket-Key: ");
899 strcpy(p, wsi->key_b64);
900 p += strlen(wsi->key_b64);
901 p += sprintf(p, "\x0d\x0a");
903 p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
906 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
909 /* tell the server what extensions we could support */
911 p += sprintf(p, "Sec-WebSocket-Extensions: ");
913 ext =context->extensions;
914 while (ext && ext->callback) {
917 n = context->protocols[0].callback(context, wsi,
918 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
919 wsi->user_space, (char *)ext->name, 0);
922 * zero return from callback means
923 * go ahead and allow the extension,
924 * it's what we get if the callback is
937 p += sprintf(p, "%s", ext->name);
943 p += sprintf(p, "\x0d\x0a");
945 if (wsi->ietf_spec_revision)
946 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
947 wsi->ietf_spec_revision);
949 /* give userland a chance to append, eg, cookies */
951 context->protocols[0].callback(context, wsi,
952 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
953 NULL, &p, (pkt + sizeof(pkt)) - p - 12);
955 p += sprintf(p, "\x0d\x0a");
957 /* prepare the expected server accept response */
959 strcpy((char *)buf, wsi->key_b64);
960 strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
962 SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
964 lws_b64_encode_string(hash, 20,
965 wsi->initial_handshake_hash_base64,
966 sizeof wsi->initial_handshake_hash_base64);
970 /* done with these now */
981 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
982 struct libwebsocket *wsi)
984 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
985 LWS_SEND_BUFFER_POST_PADDING];
993 struct libwebsocket_extension *ext;
997 static const char magic_websocket_04_masking_guid[] =
998 "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
1003 * HTTP/1.1 101 WebSocket Protocol Handshake
1004 * Upgrade: WebSocket
1005 * Connection: Upgrade
1006 * Sec-WebSocket-Origin: http://127.0.0.1
1007 * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket
1012 if (wsi->ietf_spec_revision == 0) {
1013 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
1014 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
1015 !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
1016 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
1017 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
1018 wsi->c_protocol != NULL)) {
1019 fprintf(stderr, "libwebsocket_client_handshake "
1020 "missing required header(s)\n");
1022 fprintf(stderr, "%s", pkt);
1026 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
1027 if (strcmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
1028 "101 websocket protocol handshake")) {
1029 fprintf(stderr, "libwebsocket_client_handshake "
1030 "server sent bad HTTP response '%s'\n",
1031 wsi->utf8_token[WSI_TOKEN_HTTP].token);
1035 if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len <
1037 fprintf(stderr, "libwebsocket_client_handshake "
1038 "challenge reply too short %d\n",
1040 WSI_TOKEN_CHALLENGE].token_len);
1042 fprintf(stderr, "%s", pkt);
1047 goto select_protocol;
1051 * well, what the server sent looked reasonable for syntax.
1052 * Now let's confirm it sent all the necessary headers
1055 fprintf(stderr, "WSI_TOKEN_HTTP: %d\n", wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
1056 fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n", wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
1057 fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n", wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
1058 fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n", wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
1059 fprintf(stderr, "WSI_TOKEN_NONCE: %d\n", wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
1060 fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n", wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
1062 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
1063 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
1064 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
1065 !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
1066 (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
1067 wsi->ietf_spec_revision == 4) ||
1068 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
1069 wsi->c_protocol != NULL)) {
1070 fprintf(stderr, "libwebsocket_client_handshake "
1071 "missing required header(s)\n");
1073 fprintf(stderr, "%s", pkt);
1078 * Everything seems to be there, now take a closer look at what
1082 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
1083 if (strcmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
1084 "101 switching protocols")) {
1085 fprintf(stderr, "libwebsocket_client_handshake "
1086 "server sent bad HTTP response '%s'\n",
1087 wsi->utf8_token[WSI_TOKEN_HTTP].token);
1091 strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
1092 if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
1094 fprintf(stderr, "libwebsocket_client_handshake server "
1095 "sent bad Upgrade header '%s'\n",
1096 wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
1100 strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
1101 if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
1103 fprintf(stderr, "libwebsocket_client_handshake server "
1104 "sent bad Connection hdr '%s'\n",
1105 wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
1110 pc = wsi->c_protocol;
1112 fprintf(stderr, "lws_client_interpret_server_handshake: NULL c_protocol\n");
1114 fprintf(stderr, "lws_client_interpret_server_handshake: cPprotocol='%s'\n", pc);
1117 * confirm the protocol the server wants to talk was in the list
1118 * of protocols we offered
1121 if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
1123 fprintf(stderr, "lws_client_interpret_server_handshake WSI_TOKEN_PROTOCOL is null\n");
1125 * no protocol name to work from,
1126 * default to first protocol
1128 wsi->protocol = &context->protocols[0];
1130 free(wsi->c_protocol);
1135 while (*pc && !okay) {
1137 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
1138 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
1139 (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
1140 pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
1144 while (*pc && *pc != ',')
1146 while (*pc && *pc != ' ')
1150 /* done with him now */
1152 if (wsi->c_protocol)
1153 free(wsi->c_protocol);
1157 fprintf(stderr, "libwebsocket_client_handshake server "
1158 "sent bad protocol '%s'\n",
1159 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
1164 * identify the selected protocol struct and set it
1167 wsi->protocol = NULL;
1168 while (context->protocols[n].callback) {
1169 if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
1170 context->protocols[n].name) == 0)
1171 wsi->protocol = &context->protocols[n];
1175 if (wsi->protocol == NULL) {
1176 fprintf(stderr, "libwebsocket_client_handshake server "
1177 "requested protocol '%s', which we "
1178 "said we supported but we don't!\n",
1179 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
1184 /* instantiate the accepted extensions */
1186 if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
1187 fprintf(stderr, "no client extenstions allowed by server \n");
1192 * break down the list of server accepted extensions
1193 * and go through matching them or identifying bogons
1196 c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
1200 if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
1202 if (n < sizeof(ext_name) - 1)
1215 /* check we actually support it */
1217 fprintf(stderr, "checking client ext %s\n", ext_name);
1220 ext = wsi->protocol->owning_server->extensions;
1221 while (ext && ext->callback) {
1223 if (strcmp(ext_name, ext->name)) {
1230 fprintf(stderr, "instantiating client ext %s\n", ext_name);
1232 /* instantiate the extension on this conn */
1234 wsi->active_extensions_user[
1235 wsi->count_active_extensions] =
1236 malloc(ext->per_session_data_size);
1237 wsi->active_extensions[
1238 wsi->count_active_extensions] = ext;
1240 /* allow him to construct his context */
1242 ext->callback(wsi->protocol->owning_server,
1244 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
1245 wsi->active_extensions_user[
1246 wsi->count_active_extensions],
1249 wsi->count_active_extensions++;
1255 fprintf(stderr, "Server said we should use"
1256 "an unknown extension '%s'!\n", ext_name);
1266 if (wsi->ietf_spec_revision == 0) {
1268 if (memcmp(wsi->initial_handshake_hash_base64,
1269 wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
1270 fprintf(stderr, "libwebsocket_client_handshake "
1271 "failed 00 challenge compare\n");
1273 fprintf(stderr, "%s", pkt);
1281 * Confirm his accept token is the one we precomputed
1284 if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
1285 wsi->initial_handshake_hash_base64)) {
1286 fprintf(stderr, "libwebsocket_client_handshake server "
1287 "sent bad ACCEPT '%s' vs computed '%s'\n",
1288 wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
1289 wsi->initial_handshake_hash_base64);
1293 if (wsi->ietf_spec_revision == 4) {
1295 * Calculate the 04 masking key to use when
1296 * sending data to server
1299 strcpy((char *)buf, wsi->key_b64);
1300 p = (char *)buf + strlen(wsi->key_b64);
1301 strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
1302 p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
1303 strcpy(p, magic_websocket_04_masking_guid);
1304 SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
1308 /* allocate the per-connection user memory (if any) */
1310 if (wsi->protocol->per_session_data_size) {
1311 wsi->user_space = malloc(
1312 wsi->protocol->per_session_data_size);
1313 if (wsi->user_space == NULL) {
1314 fprintf(stderr, "Out of memory for "
1315 "conn user space\n");
1319 wsi->user_space = NULL;
1321 /* clear his proxy connection timeout */
1323 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1325 /* mark him as being alive */
1327 wsi->state = WSI_STATE_ESTABLISHED;
1328 wsi->mode = LWS_CONNMODE_WS_CLIENT;
1330 fprintf(stderr, "handshake OK for protocol %s\n",
1331 wsi->protocol->name);
1333 /* call him back to inform him he is up */
1335 wsi->protocol->callback(context, wsi,
1336 LWS_CALLBACK_CLIENT_ESTABLISHED,
1341 * inform all extensions, not just active ones since they
1345 ext = context->extensions;
1347 while (ext && ext->callback) {
1349 for (n = 0; n < wsi->count_active_extensions; n++)
1350 if (wsi->active_extensions[n] == ext)
1351 v = wsi->active_extensions_user[n];
1353 ext->callback(context, ext, wsi,
1354 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
1361 if (wsi->c_protocol)
1362 free(wsi->c_protocol);
1365 libwebsocket_close_and_free_session(context, wsi,
1366 LWS_CLOSE_STATUS_NOSTATUS);
1373 * libwebsocket_service_fd() - Service polled socket with something waiting
1374 * @context: Websocket context
1375 * @pollfd: The pollfd entry describing the socket fd and which events
1378 * This function closes any active connections and then frees the
1379 * context. After calling this, any further use of the context is
1384 libwebsocket_service_fd(struct libwebsocket_context *context,
1385 struct pollfd *pollfd)
1387 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
1388 LWS_SEND_BUFFER_POST_PADDING];
1389 struct libwebsocket *wsi;
1390 struct libwebsocket *new_wsi;
1395 unsigned int clilen;
1396 struct sockaddr_in cli_addr;
1401 struct lws_tokens eff_buf;
1404 #ifdef LWS_OPENSSL_SUPPORT
1405 char ssl_err_buf[512];
1408 * you can call us with pollfd = NULL to just allow the once-per-second
1409 * global timeout checks; if less than a second since the last check
1410 * it returns immediately then.
1413 gettimeofday(&tv, NULL);
1415 if (context->last_timeout_check_s != tv.tv_sec) {
1416 context->last_timeout_check_s = tv.tv_sec;
1418 /* global timeout check once per second */
1420 for (n = 0; n < context->fds_count; n++) {
1421 wsi = wsi_from_fd(context, context->fds[n].fd);
1423 libwebsocket_service_timeout_check(context, wsi,
1428 /* just here for timeout management? */
1433 /* no, here to service a socket descriptor */
1435 wsi = wsi_from_fd(context, pollfd->fd);
1440 switch (wsi->mode) {
1441 case LWS_CONNMODE_SERVER_LISTENER:
1443 /* pollin means a client has connected to us then */
1445 if (!pollfd->revents & POLLIN)
1448 /* listen socket got an unencrypted connection... */
1450 clilen = sizeof(cli_addr);
1451 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
1453 if (accept_fd < 0) {
1454 fprintf(stderr, "ERROR on accept");
1460 setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY, &opt,
1463 if (context->fds_count >= MAX_CLIENTS) {
1464 fprintf(stderr, "too busy to accept new client\n");
1466 closesocket(accept_fd);
1474 * look at who we connected to and give user code a chance
1475 * to reject based on client IP. There's no protocol selected
1476 * yet so we issue this to protocols[0]
1479 if ((context->protocols[0].callback)(context, wsi,
1480 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
1481 (void*)(long)accept_fd, NULL, 0)) {
1482 fprintf(stderr, "Callback denied network connection\n");
1484 closesocket(accept_fd);
1491 /* accepting connection to main listener */
1493 new_wsi = libwebsocket_create_new_server_wsi(context);
1494 if (new_wsi == NULL)
1497 new_wsi->sock = accept_fd;
1500 #ifdef LWS_OPENSSL_SUPPORT
1501 new_wsi->ssl = NULL;
1503 if (context->use_ssl) {
1505 new_wsi->ssl = SSL_new(context->ssl_ctx);
1506 if (new_wsi->ssl == NULL) {
1507 fprintf(stderr, "SSL_new failed: %s\n",
1508 ERR_error_string(SSL_get_error(
1509 new_wsi->ssl, 0), NULL));
1510 libwebsockets_decode_ssl_error();
1515 SSL_set_fd(new_wsi->ssl, accept_fd);
1517 n = SSL_accept(new_wsi->ssl);
1520 * browsers seem to probe with various
1521 * ssl params which fail then retry
1524 debug("SSL_accept failed skt %u: %s\n",
1526 ERR_error_string(SSL_get_error(
1527 new_wsi->ssl, n), NULL));
1534 debug("accepted new SSL conn "
1535 "port %u on fd=%d SSL ver %s\n",
1536 ntohs(cli_addr.sin_port), accept_fd,
1537 SSL_get_version(new_wsi->ssl));
1541 debug("accepted new conn port %u on fd=%d\n",
1542 ntohs(cli_addr.sin_port), accept_fd);
1544 insert_wsi(context, new_wsi);
1547 * make sure NO events are seen yet on this new socket
1548 * (otherwise we inherit old fds[client].revents from
1549 * previous socket there and die mysteriously! )
1551 context->fds[context->fds_count].revents = 0;
1553 context->fds[context->fds_count].events = POLLIN;
1554 context->fds[context->fds_count++].fd = accept_fd;
1556 /* external POLL support via protocol 0 */
1557 context->protocols[0].callback(context, new_wsi,
1558 LWS_CALLBACK_ADD_POLL_FD,
1559 (void *)(long)accept_fd, NULL, POLLIN);
1563 case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
1565 /* as we are listening, POLLIN means accept() is needed */
1567 if (!pollfd->revents & POLLIN)
1570 /* listen socket got an unencrypted connection... */
1572 clilen = sizeof(cli_addr);
1573 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
1575 if (accept_fd < 0) {
1576 fprintf(stderr, "ERROR on accept");
1580 if (context->fds_count >= MAX_CLIENTS) {
1581 fprintf(stderr, "too busy to accept new broadcast "
1584 closesocket(accept_fd);
1591 /* create a dummy wsi for the connection and add it */
1593 new_wsi = malloc(sizeof(struct libwebsocket));
1594 memset(new_wsi, 0, sizeof (struct libwebsocket));
1595 new_wsi->sock = accept_fd;
1596 new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY;
1597 new_wsi->state = WSI_STATE_ESTABLISHED;
1598 new_wsi->count_active_extensions = 0;
1599 /* note which protocol we are proxying */
1600 new_wsi->protocol_index_for_broadcast_proxy =
1601 wsi->protocol_index_for_broadcast_proxy;
1602 insert_wsi(context, new_wsi);
1604 /* add connected socket to internal poll array */
1606 context->fds[context->fds_count].revents = 0;
1607 context->fds[context->fds_count].events = POLLIN;
1608 context->fds[context->fds_count++].fd = accept_fd;
1610 /* external POLL support via protocol 0 */
1611 context->protocols[0].callback(context, new_wsi,
1612 LWS_CALLBACK_ADD_POLL_FD,
1613 (void *)(long)accept_fd, NULL, POLLIN);
1617 case LWS_CONNMODE_BROADCAST_PROXY:
1619 /* handle session socket closed */
1621 if (pollfd->revents & (POLLERR | POLLHUP)) {
1623 debug("Session Socket %p (fd=%d) dead\n",
1624 (void *)wsi, pollfd->fd);
1626 libwebsocket_close_and_free_session(context, wsi,
1627 LWS_CLOSE_STATUS_NORMAL);
1632 * either extension code with stuff to spill, or the user code,
1633 * requested a callback when it was OK to write
1636 if (pollfd->revents & POLLOUT)
1637 if (lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
1638 libwebsocket_close_and_free_session(context, wsi,
1639 LWS_CLOSE_STATUS_NORMAL);
1643 /* any incoming data ready? */
1645 if (!(pollfd->revents & POLLIN))
1648 /* get the issued broadcast payload from the socket */
1650 len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING,
1651 MAX_BROADCAST_PAYLOAD);
1653 fprintf(stderr, "Error reading broadcast payload\n");
1657 /* broadcast it to all guys with this protocol index */
1659 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
1661 for (m = 0; m < context->fd_hashtable[n].length; m++) {
1663 new_wsi = context->fd_hashtable[n].wsi[m];
1665 /* only to clients we are serving to */
1667 if (new_wsi->mode != LWS_CONNMODE_WS_SERVING)
1671 * never broadcast to non-established
1675 if (new_wsi->state != WSI_STATE_ESTABLISHED)
1679 * only broadcast to connections using
1680 * the requested protocol
1683 if (new_wsi->protocol->protocol_index !=
1684 wsi->protocol_index_for_broadcast_proxy)
1687 /* broadcast it to this connection */
1689 new_wsi->protocol->callback(context, new_wsi,
1690 LWS_CALLBACK_BROADCAST,
1691 new_wsi->user_space,
1692 buf + LWS_SEND_BUFFER_PRE_PADDING, len);
1697 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
1699 /* handle proxy hung up on us */
1701 if (pollfd->revents & (POLLERR | POLLHUP)) {
1703 fprintf(stderr, "Proxy connection %p (fd=%d) dead\n",
1704 (void *)wsi, pollfd->fd);
1706 libwebsocket_close_and_free_session(context, wsi,
1707 LWS_CLOSE_STATUS_NOSTATUS);
1711 n = recv(wsi->sock, pkt, sizeof pkt, 0);
1713 libwebsocket_close_and_free_session(context, wsi,
1714 LWS_CLOSE_STATUS_NOSTATUS);
1715 fprintf(stderr, "ERROR reading from proxy socket\n");
1720 if (strcmp(pkt, "HTTP/1.0 200 ") != 0) {
1721 libwebsocket_close_and_free_session(context, wsi,
1722 LWS_CLOSE_STATUS_NOSTATUS);
1723 fprintf(stderr, "ERROR from proxy: %s\n", pkt);
1727 /* clear his proxy connection timeout */
1729 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1733 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
1735 #ifdef LWS_OPENSSL_SUPPORT
1738 wsi->ssl = SSL_new(context->ssl_client_ctx);
1739 wsi->client_bio = BIO_new_socket(wsi->sock,
1741 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
1743 SSL_set_ex_data(wsi->ssl,
1744 openssl_websocket_private_data_index,
1747 if (SSL_connect(wsi->ssl) <= 0) {
1748 fprintf(stderr, "SSL connect error %s\n",
1749 ERR_error_string(ERR_get_error(),
1751 libwebsocket_close_and_free_session(context, wsi,
1752 LWS_CLOSE_STATUS_NOSTATUS);
1756 n = SSL_get_verify_result(wsi->ssl);
1757 if ((n != X509_V_OK) && (
1758 n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
1759 wsi->use_ssl != 2)) {
1761 fprintf(stderr, "server's cert didn't "
1762 "look good %d\n", n);
1763 libwebsocket_close_and_free_session(context,
1764 wsi, LWS_CLOSE_STATUS_NOSTATUS);
1772 #ifdef LWS_OPENSSL_SUPPORT
1776 p = libwebsockets_generate_client_handshake(context, wsi, p);
1780 /* send our request to the server */
1782 #ifdef LWS_OPENSSL_SUPPORT
1784 n = SSL_write(wsi->ssl, pkt, p - pkt);
1787 n = send(wsi->sock, pkt, p - pkt, 0);
1790 fprintf(stderr, "ERROR writing to client socket\n");
1791 libwebsocket_close_and_free_session(context, wsi,
1792 LWS_CLOSE_STATUS_NOSTATUS);
1796 wsi->parser_state = WSI_TOKEN_NAME_PART;
1797 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
1798 libwebsocket_set_timeout(wsi,
1799 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, 5);
1803 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
1805 /* handle server hung up on us */
1807 if (pollfd->revents & (POLLERR | POLLHUP)) {
1809 fprintf(stderr, "Server connection %p (fd=%d) dead\n",
1810 (void *)wsi, pollfd->fd);
1816 /* interpret the server response */
1819 * HTTP/1.1 101 Switching Protocols
1820 * Upgrade: websocket
1821 * Connection: Upgrade
1822 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
1823 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
1824 * Sec-WebSocket-Protocol: chat
1827 #ifdef LWS_OPENSSL_SUPPORT
1829 len = SSL_read(wsi->ssl, pkt, sizeof pkt);
1832 len = recv(wsi->sock, pkt, sizeof pkt, 0);
1836 "libwebsocket_client_handshake read error\n");
1841 for (n = 0; n < len; n++)
1842 libwebsocket_parse(wsi, *p++);
1845 * may be coming in multiple packets, there is a 5-second
1846 * libwebsocket timeout still active here too, so if parsing did
1847 * not complete just wait for next packet coming in this state
1850 if (wsi->parser_state != WSI_PARSING_COMPLETE)
1853 return lws_client_interpret_server_handshake(context, wsi);
1856 if (wsi->c_protocol)
1857 free(wsi->c_protocol);
1858 libwebsocket_close_and_free_session(context, wsi,
1859 LWS_CLOSE_STATUS_NOSTATUS);
1862 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
1863 fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
1866 case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
1867 fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
1871 case LWS_CONNMODE_WS_SERVING:
1872 case LWS_CONNMODE_WS_CLIENT:
1874 /* handle session socket closed */
1876 if (pollfd->revents & (POLLERR | POLLHUP)) {
1878 fprintf(stderr, "Session Socket %p (fd=%d) dead\n",
1879 (void *)wsi, pollfd->fd);
1881 libwebsocket_close_and_free_session(context, wsi,
1882 LWS_CLOSE_STATUS_NOSTATUS);
1886 /* the guy requested a callback when it was OK to write */
1888 if ((pollfd->revents & POLLOUT) &&
1889 wsi->state == WSI_STATE_ESTABLISHED)
1890 if (lws_handle_POLLOUT_event(context, wsi,
1892 libwebsocket_close_and_free_session(
1893 context, wsi, LWS_CLOSE_STATUS_NORMAL);
1898 /* any incoming data ready? */
1900 if (!(pollfd->revents & POLLIN))
1903 #ifdef LWS_OPENSSL_SUPPORT
1905 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
1909 recv(pollfd->fd, buf, sizeof buf, 0);
1911 if (eff_buf.token_len < 0) {
1912 fprintf(stderr, "Socket read returned %d\n",
1916 if (!eff_buf.token_len) {
1917 libwebsocket_close_and_free_session(context, wsi,
1918 LWS_CLOSE_STATUS_NOSTATUS);
1923 * give any active extensions a chance to munge the buffer
1924 * before parse. We pass in a pointer to an lws_tokens struct
1925 * prepared with the default buffer and content length that's in
1926 * there. Rather than rewrite the default buffer, extensions
1927 * that expect to grow the buffer can adapt .token to
1928 * point to their own per-connection buffer in the extension
1929 * user allocation. By default with no extensions or no
1930 * extension callback handling, just the normal input buffer is
1931 * used then so it is efficient.
1934 eff_buf.token = (char *)buf;
1941 for (n = 0; n < wsi->count_active_extensions; n++) {
1942 m = wsi->active_extensions[n]->callback(context,
1943 wsi->active_extensions[n], wsi,
1944 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
1945 wsi->active_extensions_user[n],
1948 fprintf(stderr, "Extension reports fatal error\n");
1949 libwebsocket_close_and_free_session(context, wsi,
1950 LWS_CLOSE_STATUS_NOSTATUS);
1957 /* service incoming data */
1959 if (eff_buf.token_len) {
1960 n = libwebsocket_read(context, wsi,
1961 (unsigned char *)eff_buf.token, eff_buf.token_len);
1967 eff_buf.token = NULL;
1968 eff_buf.token_len = 0;
1978 * libwebsocket_context_destroy() - Destroy the websocket context
1979 * @context: Websocket context
1981 * This function closes any active connections and then frees the
1982 * context. After calling this, any further use of the context is
1986 libwebsocket_context_destroy(struct libwebsocket_context *context)
1990 struct libwebsocket *wsi;
1991 struct libwebsocket_extension *ext;
1993 for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
1994 for (m = 0; m < context->fd_hashtable[n].length; m++) {
1995 wsi = context->fd_hashtable[n].wsi[m];
1996 libwebsocket_close_and_free_session(context, wsi,
1997 LWS_CLOSE_STATUS_GOINGAWAY);
2001 * give all extensions a chance to clean up any per-context
2002 * allocations they might have made
2005 ext = context->extensions;
2006 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
2007 if (context->listen_port)
2008 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
2009 while (ext->callback) {
2010 ext->callback(context, ext, NULL, m, NULL, NULL, 0);
2016 close(context->fd_random);
2019 #ifdef LWS_OPENSSL_SUPPORT
2020 if (context->ssl_ctx)
2021 SSL_CTX_free(context->ssl_ctx);
2022 if (context->ssl_client_ctx)
2023 SSL_CTX_free(context->ssl_client_ctx);
2034 * libwebsocket_service() - Service any pending websocket activity
2035 * @context: Websocket context
2036 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
2037 * service otherwise block and service immediately, returning
2038 * after the timeout if nothing needed service.
2040 * This function deals with any pending websocket traffic, for three
2041 * kinds of event. It handles these events on both server and client
2042 * types of connection the same.
2044 * 1) Accept new connections to our context's server
2046 * 2) Perform pending broadcast writes initiated from other forked
2047 * processes (effectively serializing asynchronous broadcasts)
2049 * 3) Call the receive callback for incoming frame data received by
2050 * server or client connections.
2052 * You need to call this service function periodically to all the above
2053 * functions to happen; if your application is single-threaded you can
2054 * just call it in your main event loop.
2056 * Alternatively you can fork a new process that asynchronously handles
2057 * calling this service in a loop. In that case you are happy if this
2058 * call blocks your thread until it needs to take care of something and
2059 * would call it with a large nonzero timeout. Your loop then takes no
2060 * CPU while there is nothing happening.
2062 * If you are calling it in a single-threaded app, you don't want it to
2063 * wait around blocking other things in your loop from happening, so you
2064 * would call it with a timeout_ms of 0, so it returns immediately if
2065 * nothing is pending, or as soon as it services whatever was pending.
2070 libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
2074 /* stay dead once we are dead */
2076 if (context == NULL)
2079 /* wait for something to need service */
2081 n = poll(context->fds, context->fds_count, timeout_ms);
2082 if (n == 0) /* poll timeout */
2087 fprintf(stderr, "Listen Socket dead\n");
2092 /* handle accept on listening socket? */
2094 for (n = 0; n < context->fds_count; n++)
2095 if (context->fds[n].revents)
2096 libwebsocket_service_fd(context, &context->fds[n]);
2102 lws_any_extension_handled(struct libwebsocket_context *context,
2103 struct libwebsocket *wsi,
2104 enum libwebsocket_extension_callback_reasons r,
2105 void *v, size_t len)
2110 /* maybe an extension will take care of it for us */
2112 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
2113 if (!wsi->active_extensions[n]->callback)
2116 handled |= wsi->active_extensions[n]->callback(context,
2117 wsi->active_extensions[n], wsi,
2118 r, wsi->active_extensions_user[n], v, len);
2126 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
2127 struct libwebsocket_extension * ext)
2131 while (n < wsi->count_active_extensions) {
2132 if (wsi->active_extensions[n] != ext) {
2136 return wsi->active_extensions_user[n];
2143 * libwebsocket_callback_on_writable() - Request a callback when this socket
2144 * becomes able to be written to without
2147 * @context: libwebsockets context
2148 * @wsi: Websocket connection instance to get callback for
2152 libwebsocket_callback_on_writable(struct libwebsocket_context *context,
2153 struct libwebsocket *wsi)
2158 /* maybe an extension will take care of it for us */
2160 for (n = 0; n < wsi->count_active_extensions; n++) {
2161 if (!wsi->active_extensions[n]->callback)
2164 handled |= wsi->active_extensions[n]->callback(context,
2165 wsi->active_extensions[n], wsi,
2166 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
2167 wsi->active_extensions_user[n], NULL, 0);
2173 for (n = 0; n < context->fds_count; n++)
2174 if (context->fds[n].fd == wsi->sock) {
2175 context->fds[n].events |= POLLOUT;
2176 n = context->fds_count + 1;
2179 if (n == context->fds_count)
2180 fprintf(stderr, "libwebsocket_callback_on_writable: failed to find socket %d\n", wsi->sock);
2182 /* external POLL support via protocol 0 */
2183 context->protocols[0].callback(context, wsi,
2184 LWS_CALLBACK_SET_MODE_POLL_FD,
2185 (void *)(long)wsi->sock, NULL, POLLOUT);
2191 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
2192 * all connections using the given protocol when it
2193 * becomes possible to write to each socket without
2196 * @protocol: Protocol whose connections will get callbacks
2200 libwebsocket_callback_on_writable_all_protocol(
2201 const struct libwebsocket_protocols *protocol)
2203 struct libwebsocket_context *context = protocol->owning_server;
2206 struct libwebsocket *wsi;
2208 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
2210 for (m = 0; m < context->fd_hashtable[n].length; m++) {
2212 wsi = context->fd_hashtable[n].wsi[m];
2214 if (wsi->protocol == protocol)
2215 libwebsocket_callback_on_writable(context, wsi);
2223 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
2225 * You will not need this unless you are doing something special
2227 * @wsi: Websocket connection instance
2228 * @reason: timeout reason
2229 * @secs: how many seconds
2233 libwebsocket_set_timeout(struct libwebsocket *wsi,
2234 enum pending_timeout reason, int secs)
2238 gettimeofday(&tv, NULL);
2240 wsi->pending_timeout_limit = tv.tv_sec + secs;
2241 wsi->pending_timeout = reason;
2246 * libwebsocket_get_socket_fd() - returns the socket file descriptor
2248 * You will not need this unless you are doing something special
2250 * @wsi: Websocket connection instance
2254 libwebsocket_get_socket_fd(struct libwebsocket *wsi)
2260 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
2261 * receieved packets.
2263 * If the output side of a server process becomes choked, this allows flow
2264 * control for the input side.
2266 * @wsi: Websocket connection instance to get callback for
2267 * @enable: 0 = disable read servicing for this connection, 1 = enable
2271 libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
2273 struct libwebsocket_context *context = wsi->protocol->owning_server;
2276 for (n = 0; n < context->fds_count; n++)
2277 if (context->fds[n].fd == wsi->sock) {
2279 context->fds[n].events |= POLLIN;
2281 context->fds[n].events &= ~POLLIN;
2287 /* external POLL support via protocol 0 */
2288 context->protocols[0].callback(context, wsi,
2289 LWS_CALLBACK_SET_MODE_POLL_FD,
2290 (void *)(long)wsi->sock, NULL, POLLIN);
2292 /* external POLL support via protocol 0 */
2293 context->protocols[0].callback(context, wsi,
2294 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
2295 (void *)(long)wsi->sock, NULL, POLLIN);
2298 fprintf(stderr, "libwebsocket_rx_flow_control "
2299 "unable to find socket\n");
2305 * libwebsocket_canonical_hostname() - returns this host's hostname
2307 * This is typically used by client code to fill in the host parameter
2308 * when making a client connection. You can only call it after the context
2311 * @context: Websocket context
2316 libwebsocket_canonical_hostname(struct libwebsocket_context *context)
2318 return (const char *)context->canonical_hostname;
2322 static void sigpipe_handler(int x)
2326 #ifdef LWS_OPENSSL_SUPPORT
2328 OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
2333 struct libwebsocket_context *context;
2335 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2336 SSL_get_ex_data_X509_STORE_CTX_idx());
2339 * !!! nasty openssl requires the index to come as a library-scope
2342 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
2344 n = context->protocols[0].callback(NULL, NULL,
2345 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
2346 x509_ctx, ssl, preverify_ok);
2348 /* convert return code from 0 = OK to 1 = OK */
2361 * libwebsocket_create_context() - Create the websocket handler
2362 * @port: Port to listen on... you can use 0 to suppress listening on
2363 * any port, that's what you want if you are not running a
2364 * websocket server at all but just using it as a client
2365 * @interf: NULL to bind the listen socket to all interfaces, or the
2366 * interface name, eg, "eth2"
2367 * @protocols: Array of structures listing supported protocols and a protocol-
2368 * specific callback for each one. The list is ended with an
2369 * entry that has a NULL callback pointer.
2370 * It's not const because we write the owning_server member
2371 * @extensions: NULL or array of libwebsocket_extension structs listing the
2372 * extensions this context supports
2373 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
2374 * to listen using SSL, set to the filepath to fetch the
2375 * server cert from, otherwise NULL for unencrypted
2376 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
2378 * @gid: group id to change to after setting listen socket, or -1.
2379 * @uid: user id to change to after setting listen socket, or -1.
2380 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
2382 * This function creates the listening socket and takes care
2383 * of all initialization in one step.
2385 * After initialization, it returns a struct libwebsocket_context * that
2386 * represents this server. After calling, user code needs to take care
2387 * of calling libwebsocket_service() with the context pointer to get the
2388 * server's sockets serviced. This can be done in the same process context
2389 * or a forked process, or another thread,
2391 * The protocol callback functions are called for a handful of events
2392 * including http requests coming in, websocket connections becoming
2393 * established, and data arriving; it's also called periodically to allow
2394 * async transmission.
2396 * HTTP requests are sent always to the FIRST protocol in @protocol, since
2397 * at that time websocket protocol has not been negotiated. Other
2398 * protocols after the first one never see any HTTP callack activity.
2400 * The server created is a simple http server by default; part of the
2401 * websocket standard is upgrading this http connection to a websocket one.
2403 * This allows the same server to provide files like scripts and favicon /
2404 * images or whatever over http and dynamic data over websockets all in
2405 * one place; they're all handled in the user callback.
2408 struct libwebsocket_context *
2409 libwebsocket_create_context(int port, const char *interf,
2410 struct libwebsocket_protocols *protocols,
2411 struct libwebsocket_extension *extensions,
2412 const char *ssl_cert_filepath,
2413 const char *ssl_private_key_filepath,
2414 int gid, int uid, unsigned int options)
2420 struct sockaddr_in serv_addr, cli_addr;
2422 struct libwebsocket_context *context = NULL;
2425 char hostname[1024];
2427 struct libwebsocket *wsi;
2429 #ifdef LWS_OPENSSL_SUPPORT
2431 char ssl_err_buf[512];
2436 WORD wVersionRequested;
2440 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
2441 wVersionRequested = MAKEWORD(2, 2);
2443 err = WSAStartup(wVersionRequested, &wsaData);
2445 /* Tell the user that we could not find a usable */
2447 fprintf(stderr, "WSAStartup failed with error: %d\n",
2455 context = malloc(sizeof(struct libwebsocket_context));
2457 fprintf(stderr, "No memory for websocket context\n");
2460 context->protocols = protocols;
2461 context->listen_port = port;
2462 context->http_proxy_port = 0;
2463 context->http_proxy_address[0] = '\0';
2464 context->options = options;
2465 context->fds_count = 0;
2466 context->extensions = extensions;
2469 context->fd_random = 0;
2471 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2472 if (context->fd_random < 0) {
2473 fprintf(stderr, "Unable to open random device %s %d\n",
2474 SYSTEM_RANDOM_FILEPATH, context->fd_random);
2479 #ifdef LWS_OPENSSL_SUPPORT
2480 context->use_ssl = 0;
2481 context->ssl_ctx = NULL;
2482 context->ssl_client_ctx = NULL;
2483 openssl_websocket_private_data_index = 0;
2485 /* find canonical hostname */
2487 hostname[(sizeof hostname) - 1] = '\0';
2488 gethostname(hostname, (sizeof hostname) - 1);
2489 he = gethostbyname(hostname);
2491 strncpy(context->canonical_hostname, he->h_name,
2492 sizeof context->canonical_hostname - 1);
2493 context->canonical_hostname[
2494 sizeof context->canonical_hostname - 1] = '\0';
2496 strncpy(context->canonical_hostname, hostname,
2497 sizeof context->canonical_hostname - 1);
2499 /* split the proxy ads:port if given */
2501 p = getenv("http_proxy");
2503 strncpy(context->http_proxy_address, p,
2504 sizeof context->http_proxy_address - 1);
2505 context->http_proxy_address[
2506 sizeof context->http_proxy_address - 1] = '\0';
2508 p = strchr(context->http_proxy_address, ':');
2510 fprintf(stderr, "http_proxy needs to be ads:port\n");
2514 context->http_proxy_port = atoi(p + 1);
2516 fprintf(stderr, "Using proxy %s:%u\n",
2517 context->http_proxy_address,
2518 context->http_proxy_port);
2523 #ifdef LWS_OPENSSL_SUPPORT
2524 context->use_ssl = ssl_cert_filepath != NULL &&
2525 ssl_private_key_filepath != NULL;
2526 if (context->use_ssl)
2527 fprintf(stderr, " Compiled with SSL support, "
2530 fprintf(stderr, " Compiled with SSL support, "
2534 if (ssl_cert_filepath != NULL &&
2535 ssl_private_key_filepath != NULL) {
2536 fprintf(stderr, " Not compiled for OpenSSl support!\n");
2539 fprintf(stderr, " Compiled without SSL support, "
2540 "serving unencrypted\n");
2544 /* ignore SIGPIPE */
2547 signal(SIGPIPE, sigpipe_handler);
2551 #ifdef LWS_OPENSSL_SUPPORT
2553 /* basic openssl init */
2557 OpenSSL_add_all_algorithms();
2558 SSL_load_error_strings();
2560 openssl_websocket_private_data_index =
2561 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2564 * Firefox insists on SSLv23 not SSLv3
2565 * Konq disables SSLv2 by default now, SSLv23 works
2568 method = (SSL_METHOD *)SSLv23_server_method();
2570 fprintf(stderr, "problem creating ssl method: %s\n",
2571 ERR_error_string(ERR_get_error(), ssl_err_buf));
2574 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2575 if (!context->ssl_ctx) {
2576 fprintf(stderr, "problem creating ssl context: %s\n",
2577 ERR_error_string(ERR_get_error(), ssl_err_buf));
2581 /* client context */
2582 if (port == CONTEXT_PORT_NO_LISTEN)
2584 method = (SSL_METHOD *)SSLv23_client_method();
2586 fprintf(stderr, "problem creating ssl method: %s\n",
2587 ERR_error_string(ERR_get_error(), ssl_err_buf));
2590 /* create context */
2591 context->ssl_client_ctx = SSL_CTX_new(method);
2592 if (!context->ssl_client_ctx) {
2593 fprintf(stderr, "problem creating ssl context: %s\n",
2594 ERR_error_string(ERR_get_error(), ssl_err_buf));
2598 /* openssl init for cert verification (for client sockets) */
2600 if (!SSL_CTX_load_verify_locations(
2601 context->ssl_client_ctx, NULL,
2602 LWS_OPENSSL_CLIENT_CERTS))
2604 "Unable to load SSL Client certs from %s "
2605 "(set by --with-client-cert-dir= in configure) -- "
2606 " client ssl isn't going to work",
2607 LWS_OPENSSL_CLIENT_CERTS);
2610 * callback allowing user code to load extra verification certs
2611 * helping the client to verify server identity
2614 context->protocols[0].callback(context, NULL,
2615 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2616 context->ssl_client_ctx, NULL, 0);
2618 /* as a server, are we requiring clients to identify themselves? */
2620 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
2622 /* absolutely require the client cert */
2624 SSL_CTX_set_verify(context->ssl_ctx,
2625 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2626 OpenSSL_verify_callback);
2629 * give user code a chance to load certs into the server
2630 * allowing it to verify incoming client certs
2633 context->protocols[0].callback(context, NULL,
2634 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
2635 context->ssl_ctx, NULL, 0);
2638 if (context->use_ssl) {
2640 /* openssl init for server sockets */
2642 /* set the local certificate from CertFile */
2643 n = SSL_CTX_use_certificate_file(context->ssl_ctx,
2644 ssl_cert_filepath, SSL_FILETYPE_PEM);
2646 fprintf(stderr, "problem getting cert '%s': %s\n",
2648 ERR_error_string(ERR_get_error(), ssl_err_buf));
2651 /* set the private key from KeyFile */
2652 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
2653 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
2654 fprintf(stderr, "ssl problem getting key '%s': %s\n",
2655 ssl_private_key_filepath,
2656 ERR_error_string(ERR_get_error(), ssl_err_buf));
2659 /* verify private key */
2660 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
2661 fprintf(stderr, "Private SSL key doesn't match cert\n");
2665 /* SSL is happy and has a cert it's content with */
2671 if (lws_b64_selftest())
2674 /* fd hashtable init */
2676 for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
2677 context->fd_hashtable[n].length = 0;
2679 /* set up our external listening socket we serve on */
2683 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2685 fprintf(stderr, "ERROR opening socket");
2689 /* allow us to restart even if old sockets in TIME_WAIT */
2690 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
2695 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
2697 bzero((char *) &serv_addr, sizeof(serv_addr));
2698 serv_addr.sin_family = AF_INET;
2700 serv_addr.sin_addr.s_addr = INADDR_ANY;
2702 interface_to_sa(interf, &serv_addr,
2704 serv_addr.sin_port = htons(port);
2706 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2709 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
2714 wsi = malloc(sizeof(struct libwebsocket));
2715 memset(wsi, 0, sizeof (struct libwebsocket));
2717 wsi->count_active_extensions = 0;
2718 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
2719 insert_wsi(context, wsi);
2722 fprintf(stderr, " Listening on port %d\n", port);
2724 /* list in the internal poll array */
2726 context->fds[context->fds_count].fd = sockfd;
2727 context->fds[context->fds_count++].events = POLLIN;
2729 /* external POLL support via protocol 0 */
2730 context->protocols[0].callback(context, wsi,
2731 LWS_CALLBACK_ADD_POLL_FD,
2732 (void *)(long)sockfd, NULL, POLLIN);
2736 /* drop any root privs for this process */
2741 fprintf(stderr, "setgid: %s\n", strerror(errno));
2744 fprintf(stderr, "setuid: %s\n", strerror(errno));
2747 /* set up our internal broadcast trigger sockets per-protocol */
2749 for (context->count_protocols = 0;
2750 protocols[context->count_protocols].callback;
2751 context->count_protocols++) {
2752 protocols[context->count_protocols].owning_server = context;
2753 protocols[context->count_protocols].protocol_index =
2754 context->count_protocols;
2756 fd = socket(AF_INET, SOCK_STREAM, 0);
2758 fprintf(stderr, "ERROR opening socket");
2762 /* allow us to restart even if old sockets in TIME_WAIT */
2763 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
2765 bzero((char *) &serv_addr, sizeof(serv_addr));
2766 serv_addr.sin_family = AF_INET;
2767 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2768 serv_addr.sin_port = 0; /* pick the port for us */
2770 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
2772 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n",
2777 slen = sizeof cli_addr;
2778 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
2780 fprintf(stderr, "getsockname failed\n");
2783 protocols[context->count_protocols].broadcast_socket_port =
2784 ntohs(cli_addr.sin_port);
2787 debug(" Protocol %s broadcast socket %d\n",
2788 protocols[context->count_protocols].name,
2789 ntohs(cli_addr.sin_port));
2791 /* dummy wsi per broadcast proxy socket */
2793 wsi = malloc(sizeof(struct libwebsocket));
2794 memset(wsi, 0, sizeof (struct libwebsocket));
2796 wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
2797 wsi->count_active_extensions = 0;
2798 /* note which protocol we are proxying */
2799 wsi->protocol_index_for_broadcast_proxy =
2800 context->count_protocols;
2801 insert_wsi(context, wsi);
2803 /* list in internal poll array */
2805 context->fds[context->fds_count].fd = fd;
2806 context->fds[context->fds_count].events = POLLIN;
2807 context->fds[context->fds_count].revents = 0;
2808 context->fds_count++;
2810 /* external POLL support via protocol 0 */
2811 context->protocols[0].callback(context, wsi,
2812 LWS_CALLBACK_ADD_POLL_FD,
2813 (void *)(long)fd, NULL, POLLIN);
2817 * give all extensions a chance to create any per-context
2818 * allocations they need
2821 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
2823 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
2824 while (extensions->callback) {
2825 extensions->callback(context, extensions,
2826 NULL, m, NULL, NULL, 0);
2837 * libwebsockets_fork_service_loop() - Optional helper function forks off
2838 * a process for the websocket server loop.
2839 * You don't have to use this but if not, you
2840 * have to make sure you are calling
2841 * libwebsocket_service periodically to service
2842 * the websocket traffic
2843 * @context: server context returned by creation function
2847 libwebsockets_fork_service_loop(struct libwebsocket_context *context)
2850 struct sockaddr_in cli_addr;
2860 /* main process context */
2863 * set up the proxy sockets to allow broadcast from
2864 * service process context
2867 for (p = 0; p < context->count_protocols; p++) {
2868 fd = socket(AF_INET, SOCK_STREAM, 0);
2870 fprintf(stderr, "Unable to create socket\n");
2873 cli_addr.sin_family = AF_INET;
2874 cli_addr.sin_port = htons(
2875 context->protocols[p].broadcast_socket_port);
2876 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2877 n = connect(fd, (struct sockaddr *)&cli_addr,
2880 fprintf(stderr, "Unable to connect to "
2881 "broadcast socket %d, %s\n",
2882 n, strerror(errno));
2886 context->protocols[p].broadcast_socket_user_fd = fd;
2892 /* we want a SIGHUP when our parent goes down */
2893 prctl(PR_SET_PDEATHSIG, SIGHUP);
2895 /* in this forked process, sit and service websocket connections */
2898 if (libwebsocket_service(context, 1000))
2907 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
2909 * @wsi: pointer to struct websocket you want to know the protocol of
2912 * This is useful to get the protocol to broadcast back to from inside
2916 const struct libwebsocket_protocols *
2917 libwebsockets_get_protocol(struct libwebsocket *wsi)
2919 return wsi->protocol;
2923 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
2924 * connections of the given protocol.
2925 * @protocol: pointer to the protocol you will broadcast to all members of
2926 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
2927 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
2928 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
2929 * case you are calling this function from callback context.
2930 * @len: length of payload data in buf, starting from buf.
2932 * This function allows bulk sending of a packet to every connection using
2933 * the given protocol. It does not send the data directly; instead it calls
2934 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
2935 * wants to actually send the data for that connection, the callback itself
2936 * should call libwebsocket_write().
2938 * libwebsockets_broadcast() can be called from another fork context without
2939 * having to take any care about data visibility between the processes, it'll
2945 libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
2946 unsigned char *buf, size_t len)
2948 struct libwebsocket_context *context = protocol->owning_server;
2951 struct libwebsocket * wsi;
2953 if (!protocol->broadcast_socket_user_fd) {
2955 * We are either running unforked / flat, or we are being
2956 * called from poll thread context
2957 * eg, from a callback. In that case don't use sockets for
2958 * broadcast IPC (since we can't open a socket connection to
2959 * a socket listening on our own thread) but directly do the
2962 * Locking is not needed because we are by definition being
2963 * called in the poll thread context and are serialized.
2966 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
2968 for (m = 0; m < context->fd_hashtable[n].length; m++) {
2970 wsi = context->fd_hashtable[n].wsi[m];
2972 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
2976 * never broadcast to
2977 * non-established connections
2979 if (wsi->state != WSI_STATE_ESTABLISHED)
2982 /* only broadcast to guys using
2983 * requested protocol
2985 if (wsi->protocol != protocol)
2988 wsi->protocol->callback(context, wsi,
2989 LWS_CALLBACK_BROADCAST,
2999 * We're being called from a different process context than the server
3000 * loop. Instead of broadcasting directly, we send our
3001 * payload on a socket to do the IPC; the server process will serialize
3002 * the broadcast action in its main poll() loop.
3004 * There's one broadcast socket listening for each protocol supported
3005 * set up when the websocket server initializes
3008 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
3014 libwebsocket_is_final_fragment(struct libwebsocket *wsi)