Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 1 | /* |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3 | * |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 22 | #include "private-libwebsockets.h" |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 23 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 24 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 25 | SSL_CTX *ssl_ctx; |
| 26 | int use_ssl; |
| 27 | #endif |
| 28 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 29 | void |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 30 | libwebsocket_close_and_free_session(struct libwebsocket *wsi) |
| 31 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 32 | int n; |
| 33 | |
| 34 | if ((unsigned long)wsi < LWS_MAX_PROTOCOLS) |
| 35 | return; |
| 36 | |
| 37 | n = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 38 | |
| 39 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 40 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 41 | if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED) |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 42 | wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED, |
| 43 | wsi->user_space, NULL, 0); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 44 | |
| 45 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 46 | if (wsi->utf8_token[n].token) |
| 47 | free(wsi->utf8_token[n].token); |
| 48 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 49 | /* fprintf(stderr, "closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 50 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 51 | #ifdef LWS_OPENSSL_SUPPORT |
| 52 | if (use_ssl) { |
| 53 | n = SSL_get_fd(wsi->ssl); |
| 54 | SSL_shutdown(wsi->ssl); |
| 55 | close(n); |
| 56 | SSL_free(wsi->ssl); |
| 57 | } else { |
| 58 | #endif |
| 59 | shutdown(wsi->sock, SHUT_RDWR); |
| 60 | close(wsi->sock); |
| 61 | #ifdef LWS_OPENSSL_SUPPORT |
| 62 | } |
| 63 | #endif |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 64 | if (wsi->user_space) |
| 65 | free(wsi->user_space); |
| 66 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 67 | free(wsi); |
| 68 | } |
| 69 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 70 | static int |
| 71 | libwebsocket_poll_connections(struct libwebsocket_context *this) |
| 72 | { |
| 73 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BROADCAST_PAYLOAD + |
| 74 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 75 | int client = this->count_protocols + 1; |
| 76 | struct libwebsocket *wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 77 | int n; |
| 78 | size_t len; |
| 79 | |
| 80 | /* check for activity on client sockets */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 81 | |
| 82 | for (; client < this->fds_count; client++) { |
| 83 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 84 | /* handle session socket closed */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 85 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 86 | if (this->fds[client].revents & (POLLERR | POLLHUP)) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 87 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 88 | debug("Session Socket %d %p (fd=%d) dead\n", |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 89 | client, (void *)this->wsi[client], |
| 90 | this->fds[client].fd); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 91 | |
| 92 | libwebsocket_close_and_free_session(this->wsi[client]); |
| 93 | goto nuke_this; |
| 94 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 95 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 96 | /* any incoming data ready? */ |
| 97 | |
| 98 | if (!(this->fds[client].revents & POLLIN)) |
| 99 | continue; |
| 100 | |
| 101 | /* broadcast? */ |
| 102 | |
| 103 | if ((unsigned long)this->wsi[client] < LWS_MAX_PROTOCOLS) { |
| 104 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 105 | /* get the issued broadcast payload from the socket */ |
| 106 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 107 | len = read(this->fds[client].fd, |
| 108 | buf + LWS_SEND_BUFFER_PRE_PADDING, |
| 109 | MAX_BROADCAST_PAYLOAD); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 110 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 111 | if (len < 0) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 112 | fprintf(stderr, |
| 113 | "Error reading broadcast payload\n"); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 114 | continue; |
| 115 | } |
| 116 | |
| 117 | /* broadcast it to all guys with this protocol index */ |
| 118 | |
| 119 | for (n = this->count_protocols + 1; |
| 120 | n < this->fds_count; n++) { |
| 121 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 122 | wsi = this->wsi[n]; |
| 123 | |
| 124 | if ((unsigned long)wsi < LWS_MAX_PROTOCOLS) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 125 | continue; |
| 126 | |
| 127 | /* |
| 128 | * never broadcast to non-established |
| 129 | * connection |
| 130 | */ |
| 131 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 132 | if (wsi->state != WSI_STATE_ESTABLISHED) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 133 | continue; |
| 134 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 135 | /* only to clients connected to us */ |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 136 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 137 | if (wsi->client_mode) |
| 138 | continue; |
| 139 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 140 | /* |
| 141 | * only broadcast to connections using |
| 142 | * the requested protocol |
| 143 | */ |
| 144 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 145 | if (wsi->protocol->protocol_index != |
| 146 | (int)(unsigned long)this->wsi[client]) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 147 | continue; |
| 148 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 149 | /* broadcast it to this connection */ |
| 150 | |
| 151 | wsi->protocol->callback(wsi, |
| 152 | LWS_CALLBACK_BROADCAST, |
| 153 | wsi->user_space, |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 154 | buf + LWS_SEND_BUFFER_PRE_PADDING, len); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | #ifdef LWS_OPENSSL_SUPPORT |
| 161 | if (this->use_ssl) |
| 162 | n = SSL_read(this->wsi[client]->ssl, buf, sizeof buf); |
| 163 | else |
| 164 | #endif |
| 165 | n = recv(this->fds[client].fd, buf, sizeof buf, 0); |
| 166 | |
| 167 | if (n < 0) { |
| 168 | fprintf(stderr, "Socket read returned %d\n", n); |
| 169 | continue; |
| 170 | } |
| 171 | if (!n) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 172 | libwebsocket_close_and_free_session(this->wsi[client]); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 173 | goto nuke_this; |
| 174 | } |
| 175 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 176 | /* service incoming data */ |
| 177 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 178 | n = libwebsocket_read(this->wsi[client], buf, n); |
| 179 | if (n >= 0) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 180 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 181 | /* |
| 182 | * it closed and nuked wsi[client], so remove the |
| 183 | * socket handle and wsi from our service list |
| 184 | */ |
| 185 | nuke_this: |
| 186 | |
| 187 | debug("nuking wsi %p, fsd_count = %d\n", |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 188 | (void *)this->wsi[client], this->fds_count - 1); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 189 | |
| 190 | this->fds_count--; |
| 191 | for (n = client; n < this->fds_count; n++) { |
| 192 | this->fds[n] = this->fds[n + 1]; |
| 193 | this->wsi[n] = this->wsi[n + 1]; |
| 194 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 195 | |
| 196 | return 0; |
| 197 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 203 | /** |
| 204 | * libwebsocket_context_destroy() - Destroy the websocket context |
| 205 | * @this: Websocket context |
| 206 | * |
| 207 | * This function closes any active connections and then frees the |
| 208 | * context. After calling this, any further use of the context is |
| 209 | * undefined. |
| 210 | */ |
| 211 | void |
| 212 | libwebsocket_context_destroy(struct libwebsocket_context *this) |
| 213 | { |
| 214 | int client; |
| 215 | |
| 216 | /* close listening skt and per-protocol broadcast sockets */ |
| 217 | for (client = 0; client < this->fds_count; client++) |
| 218 | libwebsocket_close_and_free_session(this->wsi[client]); |
| 219 | |
| 220 | #ifdef LWS_OPENSSL_SUPPORT |
| 221 | if (ssl_ctx) |
| 222 | SSL_CTX_free(ssl_ctx); |
| 223 | #endif |
| 224 | |
| 225 | if (this) |
| 226 | free(this); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * libwebsocket_service() - Service any pending websocket activity |
| 231 | * @this: Websocket context |
| 232 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 233 | * service otherwise block and service immediately, returning |
| 234 | * after the timeout if nothing needed service. |
| 235 | * |
| 236 | * This function deals with any pending websocket traffic, for three |
| 237 | * kinds of event. It handles these events on both server and client |
| 238 | * types of connection the same. |
| 239 | * |
| 240 | * 1) Accept new connections to our context's server |
| 241 | * |
| 242 | * 2) Perform pending broadcast writes initiated from other forked |
| 243 | * processes (effectively serializing asynchronous broadcasts) |
| 244 | * |
| 245 | * 3) Call the receive callback for incoming frame data received by |
| 246 | * server or client connections. |
| 247 | * |
| 248 | * You need to call this service function periodically to all the above |
| 249 | * functions to happen; if your application is single-threaded you can |
| 250 | * just call it in your main event loop. |
| 251 | * |
| 252 | * Alternatively you can fork a new process that asynchronously handles |
| 253 | * calling this service in a loop. In that case you are happy if this |
| 254 | * call blocks your thread until it needs to take care of something and |
| 255 | * would call it with a large nonzero timeout. Your loop then takes no |
| 256 | * CPU while there is nothing happening. |
| 257 | * |
| 258 | * If you are calling it in a single-threaded app, you don't want it to |
| 259 | * wait around blocking other things in your loop from happening, so you |
| 260 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 261 | * nothing is pending, or as soon as it services whatever was pending. |
| 262 | */ |
| 263 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 264 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 265 | int |
| 266 | libwebsocket_service(struct libwebsocket_context *this, int timeout_ms) |
| 267 | { |
| 268 | int n; |
| 269 | int client; |
| 270 | unsigned int clilen; |
| 271 | struct sockaddr_in cli_addr; |
| 272 | int fd; |
| 273 | |
| 274 | /* stay dead once we are dead */ |
| 275 | |
| 276 | if (this == NULL) |
| 277 | return 1; |
| 278 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 279 | /* don't check listen socket if we are not listening */ |
| 280 | |
| 281 | if (this->listen_port) |
| 282 | n = poll(this->fds, this->fds_count, timeout_ms); |
| 283 | else |
| 284 | n = poll(&this->fds[1], this->fds_count - 1, timeout_ms); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 285 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 286 | |
| 287 | if (n < 0 || this->fds[0].revents & (POLLERR | POLLHUP)) { |
| 288 | fprintf(stderr, "Listen Socket dead\n"); |
| 289 | goto fatal; |
| 290 | } |
| 291 | if (n == 0) /* poll timeout */ |
| 292 | return 0; |
| 293 | |
| 294 | /* handle accept on listening socket? */ |
| 295 | |
| 296 | for (client = 0; client < this->count_protocols + 1; client++) { |
| 297 | |
| 298 | if (!this->fds[client].revents & POLLIN) |
| 299 | continue; |
| 300 | |
| 301 | /* listen socket got an unencrypted connection... */ |
| 302 | |
| 303 | clilen = sizeof(cli_addr); |
| 304 | fd = accept(this->fds[client].fd, |
| 305 | (struct sockaddr *)&cli_addr, &clilen); |
| 306 | if (fd < 0) { |
| 307 | fprintf(stderr, "ERROR on accept"); |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | if (this->fds_count >= MAX_CLIENTS) { |
| 312 | fprintf(stderr, "too busy"); |
| 313 | close(fd); |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | if (client) { |
| 318 | /* |
| 319 | * accepting a connection to broadcast socket |
| 320 | * set wsi to be protocol index not pointer |
| 321 | */ |
| 322 | |
| 323 | this->wsi[this->fds_count] = |
| 324 | (struct libwebsocket *)(long)(client - 1); |
| 325 | |
| 326 | goto fill_in_fds; |
| 327 | } |
| 328 | |
| 329 | /* accepting connection to main listener */ |
| 330 | |
| 331 | this->wsi[this->fds_count] = |
| 332 | malloc(sizeof(struct libwebsocket)); |
| 333 | if (!this->wsi[this->fds_count]) { |
| 334 | fprintf(stderr, "Out of memory for new connection\n"); |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | #ifdef LWS_OPENSSL_SUPPORT |
| 339 | if (this->use_ssl) { |
| 340 | |
| 341 | this->wsi[this->fds_count]->ssl = SSL_new(ssl_ctx); |
| 342 | if (this->wsi[this->fds_count]->ssl == NULL) { |
| 343 | fprintf(stderr, "SSL_new failed: %s\n", |
| 344 | ERR_error_string(SSL_get_error( |
| 345 | this->wsi[this->fds_count]->ssl, 0), |
| 346 | NULL)); |
| 347 | free(this->wsi[this->fds_count]); |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | SSL_set_fd(this->wsi[this->fds_count]->ssl, fd); |
| 352 | |
| 353 | n = SSL_accept(this->wsi[this->fds_count]->ssl); |
| 354 | if (n != 1) { |
| 355 | /* |
| 356 | * browsers seem to probe with various |
| 357 | * ssl params which fail then retry |
| 358 | * and succeed |
| 359 | */ |
| 360 | debug("SSL_accept failed skt %u: %s\n", |
| 361 | fd, |
| 362 | ERR_error_string(SSL_get_error( |
| 363 | this->wsi[this->fds_count]->ssl, |
| 364 | n), NULL)); |
| 365 | SSL_free( |
| 366 | this->wsi[this->fds_count]->ssl); |
| 367 | free(this->wsi[this->fds_count]); |
| 368 | continue; |
| 369 | } |
| 370 | debug("accepted new SSL conn " |
| 371 | "port %u on fd=%d SSL ver %s\n", |
| 372 | ntohs(cli_addr.sin_port), fd, |
| 373 | SSL_get_version(this->wsi[ |
| 374 | this->fds_count]->ssl)); |
| 375 | |
| 376 | } else |
| 377 | #endif |
| 378 | debug("accepted new conn port %u on fd=%d\n", |
| 379 | ntohs(cli_addr.sin_port), fd); |
| 380 | |
| 381 | /* intialize the instance struct */ |
| 382 | |
| 383 | this->wsi[this->fds_count]->sock = fd; |
| 384 | this->wsi[this->fds_count]->state = WSI_STATE_HTTP; |
| 385 | this->wsi[this->fds_count]->name_buffer_pos = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 386 | this->wsi[this->fds_count]->client_mode = 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 387 | |
| 388 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 389 | this->wsi[this->fds_count]-> |
| 390 | utf8_token[n].token = NULL; |
| 391 | this->wsi[this->fds_count]-> |
| 392 | utf8_token[n].token_len = 0; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * these can only be set once the protocol is known |
| 397 | * we set an unestablished connection's protocol pointer |
| 398 | * to the start of the supported list, so it can look |
| 399 | * for matching ones during the handshake |
| 400 | */ |
| 401 | this->wsi[this->fds_count]->protocol = this->protocols; |
| 402 | this->wsi[this->fds_count]->user_space = NULL; |
| 403 | |
| 404 | /* |
| 405 | * Default protocol is 76 / 00 |
| 406 | * After 76, there's a header specified to inform which |
| 407 | * draft the client wants, when that's seen we modify |
| 408 | * the individual connection's spec revision accordingly |
| 409 | */ |
| 410 | this->wsi[this->fds_count]->ietf_spec_revision = 0; |
| 411 | |
| 412 | fill_in_fds: |
| 413 | |
| 414 | /* |
| 415 | * make sure NO events are seen yet on this new socket |
| 416 | * (otherwise we inherit old fds[client].revents from |
| 417 | * previous socket there and die mysteriously! ) |
| 418 | */ |
| 419 | this->fds[this->fds_count].revents = 0; |
| 420 | |
| 421 | this->fds[this->fds_count].events = POLLIN; |
| 422 | this->fds[this->fds_count++].fd = fd; |
| 423 | |
| 424 | } |
| 425 | |
| 426 | /* service anything incoming on websocket connection */ |
| 427 | |
| 428 | libwebsocket_poll_connections(this); |
| 429 | |
| 430 | /* this round is done */ |
| 431 | |
| 432 | return 0; |
| 433 | |
| 434 | fatal: |
| 435 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 436 | fprintf(stderr, "service hits fatal\n"); |
| 437 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 438 | /* close listening skt and per-protocol broadcast sockets */ |
| 439 | for (client = 0; client < this->fds_count; client++) |
| 440 | close(this->fds[0].fd); |
| 441 | |
| 442 | #ifdef LWS_OPENSSL_SUPPORT |
| 443 | SSL_CTX_free(ssl_ctx); |
| 444 | #endif |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 445 | |
| 446 | if (this) |
| 447 | free(this); |
| 448 | |
| 449 | this = NULL; |
| 450 | |
| 451 | /* inform caller we are dead */ |
| 452 | |
| 453 | return 1; |
| 454 | } |
| 455 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 456 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 457 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 458 | * libwebsocket_create_context() - Create the websocket handler |
| 459 | * @port: Port to listen on... you can use 0 to suppress listening on |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 460 | * any port, that's what you want if you are not running a |
| 461 | * websocket server at all but just using it as a client |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 462 | * @protocols: Array of structures listing supported protocols and a protocol- |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 463 | * specific callback for each one. The list is ended with an |
| 464 | * entry that has a NULL callback pointer. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 465 | * It's not const because we write the owning_server member |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 466 | * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 467 | * to listen using SSL, set to the filepath to fetch the |
| 468 | * server cert from, otherwise NULL for unencrypted |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 469 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 470 | * else ignored |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 471 | * @gid: group id to change to after setting listen socket, or -1. |
| 472 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 473 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 474 | * This function creates the listening socket and takes care |
| 475 | * of all initialization in one step. |
| 476 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 477 | * After initialization, it returns a struct libwebsocket_context * that |
| 478 | * represents this server. After calling, user code needs to take care |
| 479 | * of calling libwebsocket_service() with the context pointer to get the |
| 480 | * server's sockets serviced. This can be done in the same process context |
| 481 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 482 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 483 | * The protocol callback functions are called for a handful of events |
| 484 | * including http requests coming in, websocket connections becoming |
| 485 | * established, and data arriving; it's also called periodically to allow |
| 486 | * async transmission. |
| 487 | * |
| 488 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 489 | * at that time websocket protocol has not been negotiated. Other |
| 490 | * protocols after the first one never see any HTTP callack activity. |
| 491 | * |
| 492 | * The server created is a simple http server by default; part of the |
| 493 | * websocket standard is upgrading this http connection to a websocket one. |
| 494 | * |
| 495 | * This allows the same server to provide files like scripts and favicon / |
| 496 | * images or whatever over http and dynamic data over websockets all in |
| 497 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 498 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 499 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 500 | struct libwebsocket_context * |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 501 | libwebsocket_create_context(int port, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 502 | struct libwebsocket_protocols *protocols, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 503 | const char *ssl_cert_filepath, |
| 504 | const char *ssl_private_key_filepath, |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 505 | int gid, int uid) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 506 | { |
| 507 | int n; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 508 | int sockfd = 0; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 509 | int fd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 510 | struct sockaddr_in serv_addr, cli_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 511 | int opt = 1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 512 | struct libwebsocket_context *this = NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 513 | unsigned int slen; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 514 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 515 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 516 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 517 | char ssl_err_buf[512]; |
| 518 | |
| 519 | use_ssl = ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL; |
| 520 | if (use_ssl) |
| 521 | fprintf(stderr, " Compiled with SSL support, using it\n"); |
| 522 | else |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 523 | fprintf(stderr, " Compiled with SSL support, not using it\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 524 | |
| 525 | #else |
| 526 | if (ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL) { |
| 527 | fprintf(stderr, " Not compiled for OpenSSl support!\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 528 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 529 | } |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 530 | fprintf(stderr, " Compiled without SSL support, serving unencrypted\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 531 | #endif |
| 532 | |
| 533 | #ifdef LWS_OPENSSL_SUPPORT |
| 534 | if (use_ssl) { |
| 535 | SSL_library_init(); |
| 536 | |
| 537 | OpenSSL_add_all_algorithms(); |
| 538 | SSL_load_error_strings(); |
| 539 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 540 | /* |
| 541 | * Firefox insists on SSLv23 not SSLv3 |
| 542 | * Konq disables SSLv2 by default now, SSLv23 works |
| 543 | */ |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 544 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 545 | method = (SSL_METHOD *)SSLv23_server_method(); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 546 | if (!method) { |
| 547 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 548 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 549 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 550 | } |
| 551 | ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 552 | if (!ssl_ctx) { |
| 553 | printf("problem creating ssl context: %s\n", |
| 554 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 555 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 556 | } |
| 557 | /* set the local certificate from CertFile */ |
| 558 | n = SSL_CTX_use_certificate_file(ssl_ctx, |
| 559 | ssl_cert_filepath, SSL_FILETYPE_PEM); |
| 560 | if (n != 1) { |
| 561 | fprintf(stderr, "problem getting cert '%s': %s\n", |
| 562 | ssl_cert_filepath, |
| 563 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 564 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 565 | } |
| 566 | /* set the private key from KeyFile */ |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 567 | if (SSL_CTX_use_PrivateKey_file(ssl_ctx, |
| 568 | ssl_private_key_filepath, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 569 | SSL_FILETYPE_PEM) != 1) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 570 | fprintf(stderr, "ssl problem getting key '%s': %s\n", |
| 571 | ssl_private_key_filepath, |
| 572 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 573 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 574 | } |
| 575 | /* verify private key */ |
| 576 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 577 | fprintf(stderr, "Private SSL key doesn't match cert\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 578 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* SSL is happy and has a cert it's content with */ |
| 582 | } |
| 583 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 584 | |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 585 | /* selftest */ |
| 586 | |
| 587 | if (lws_b64_selftest()) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 588 | return NULL; |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 589 | |
| 590 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 591 | this = malloc(sizeof(struct libwebsocket_context)); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 592 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 593 | this->protocols = protocols; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 594 | this->listen_port = port; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 595 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 596 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 597 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 598 | if (port) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 599 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 600 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 601 | if (sockfd < 0) { |
| 602 | fprintf(stderr, "ERROR opening socket"); |
| 603 | return NULL; |
| 604 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 605 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 606 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 607 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 608 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 609 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 610 | serv_addr.sin_family = AF_INET; |
| 611 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 612 | serv_addr.sin_port = htons(port); |
| 613 | |
| 614 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 615 | sizeof(serv_addr)); |
| 616 | if (n < 0) { |
| 617 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 618 | port, n, errno); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 621 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 622 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 623 | /* drop any root privs for this process */ |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 624 | |
| 625 | if (gid != -1) |
| 626 | if (setgid(gid)) |
| 627 | fprintf(stderr, "setgid: %s\n", strerror(errno)); |
| 628 | if (uid != -1) |
| 629 | if (setuid(uid)) |
| 630 | fprintf(stderr, "setuid: %s\n", strerror(errno)); |
| 631 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 632 | /* |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 633 | * prepare the poll() fd array... it's like this |
| 634 | * |
| 635 | * [0] = external listening socket |
| 636 | * [1 .. this->count_protocols] = per-protocol broadcast sockets |
| 637 | * [this->count_protocols + 1 ... this->fds_count-1] = connection skts |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 638 | */ |
| 639 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 640 | this->fds_count = 1; |
| 641 | this->fds[0].fd = sockfd; |
| 642 | this->fds[0].events = POLLIN; |
| 643 | this->count_protocols = 0; |
| 644 | #ifdef LWS_OPENSSL_SUPPORT |
| 645 | this->use_ssl = use_ssl; |
| 646 | #endif |
| 647 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 648 | if (port) { |
| 649 | listen(sockfd, 5); |
| 650 | fprintf(stderr, " Listening on port %d\n", port); |
| 651 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 652 | |
| 653 | /* set up our internal broadcast trigger sockets per-protocol */ |
| 654 | |
| 655 | for (; protocols[this->count_protocols].callback; |
| 656 | this->count_protocols++) { |
| 657 | protocols[this->count_protocols].owning_server = this; |
| 658 | protocols[this->count_protocols].protocol_index = |
| 659 | this->count_protocols; |
| 660 | |
| 661 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 662 | if (fd < 0) { |
| 663 | fprintf(stderr, "ERROR opening socket"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 664 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 665 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 666 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 667 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 668 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 669 | |
| 670 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 671 | serv_addr.sin_family = AF_INET; |
| 672 | serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 673 | serv_addr.sin_port = 0; /* pick the port for us */ |
| 674 | |
| 675 | n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 676 | if (n < 0) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 677 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 678 | port, n, errno); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 679 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | slen = sizeof cli_addr; |
| 683 | n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen); |
| 684 | if (n < 0) { |
| 685 | fprintf(stderr, "getsockname failed\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 686 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 687 | } |
| 688 | protocols[this->count_protocols].broadcast_socket_port = |
| 689 | ntohs(cli_addr.sin_port); |
| 690 | listen(fd, 5); |
| 691 | |
| 692 | debug(" Protocol %s broadcast socket %d\n", |
| 693 | protocols[this->count_protocols].name, |
| 694 | ntohs(cli_addr.sin_port)); |
| 695 | |
| 696 | this->fds[this->fds_count].fd = fd; |
| 697 | this->fds[this->fds_count].events = POLLIN; |
| 698 | /* wsi only exists for connections, not broadcast listener */ |
| 699 | this->wsi[this->fds_count] = NULL; |
| 700 | this->fds_count++; |
| 701 | } |
| 702 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 703 | return this; |
| 704 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 705 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 706 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 707 | #ifndef LWS_NO_FORK |
| 708 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 709 | /** |
| 710 | * libwebsockets_fork_service_loop() - Optional helper function forks off |
| 711 | * a process for the websocket server loop. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 712 | * You don't have to use this but if not, you |
| 713 | * have to make sure you are calling |
| 714 | * libwebsocket_service periodically to service |
| 715 | * the websocket traffic |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 716 | * @this: server context returned by creation function |
| 717 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 718 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 719 | int |
| 720 | libwebsockets_fork_service_loop(struct libwebsocket_context *this) |
| 721 | { |
| 722 | int client; |
| 723 | int fd; |
| 724 | struct sockaddr_in cli_addr; |
| 725 | int n; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 726 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 727 | n = fork(); |
| 728 | if (n < 0) |
| 729 | return n; |
| 730 | |
| 731 | if (!n) { |
| 732 | |
| 733 | /* main process context */ |
| 734 | |
| 735 | for (client = 1; client < this->count_protocols + 1; client++) { |
| 736 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 737 | if (fd < 0) { |
| 738 | fprintf(stderr, "Unable to create socket\n"); |
| 739 | return -1; |
| 740 | } |
| 741 | cli_addr.sin_family = AF_INET; |
| 742 | cli_addr.sin_port = htons( |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 743 | this->protocols[client - 1].broadcast_socket_port); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 744 | cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 745 | n = connect(fd, (struct sockaddr *)&cli_addr, |
| 746 | sizeof cli_addr); |
| 747 | if (n < 0) { |
| 748 | fprintf(stderr, "Unable to connect to " |
| 749 | "broadcast socket %d, %s\n", |
| 750 | client, strerror(errno)); |
| 751 | return -1; |
| 752 | } |
| 753 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 754 | this->protocols[client - 1].broadcast_socket_user_fd = |
| 755 | fd; |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 759 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /* we want a SIGHUP when our parent goes down */ |
| 763 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 764 | |
| 765 | /* in this forked process, sit and service websocket connections */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 766 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 767 | while (1) |
| 768 | if (libwebsocket_service(this, 1000)) |
| 769 | return -1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 770 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 771 | return 0; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 774 | #endif |
| 775 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 776 | /** |
| 777 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 778 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 779 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 780 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 781 | * |
| 782 | * This is useful to get the protocol to broadcast back to from inside |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 783 | * the callback. |
| 784 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 785 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 786 | const struct libwebsocket_protocols * |
| 787 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 788 | { |
| 789 | return wsi->protocol; |
| 790 | } |
| 791 | |
| 792 | /** |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 793 | * libwebsockets_broadcast() - Sends a buffer to the callback for all active |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 794 | * connections of the given protocol. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 795 | * @protocol: pointer to the protocol you will broadcast to all members of |
| 796 | * @buf: buffer containing the data to be broadcase. NOTE: this has to be |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 797 | * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before |
| 798 | * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the |
| 799 | * case you are calling this function from callback context. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 800 | * @len: length of payload data in buf, starting from buf. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 801 | * |
| 802 | * This function allows bulk sending of a packet to every connection using |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 803 | * the given protocol. It does not send the data directly; instead it calls |
| 804 | * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback |
| 805 | * wants to actually send the data for that connection, the callback itself |
| 806 | * should call libwebsocket_write(). |
| 807 | * |
| 808 | * libwebsockets_broadcast() can be called from another fork context without |
| 809 | * having to take any care about data visibility between the processes, it'll |
| 810 | * "just work". |
| 811 | */ |
| 812 | |
| 813 | |
| 814 | int |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 815 | libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 816 | unsigned char *buf, size_t len) |
| 817 | { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 818 | struct libwebsocket_context *this = protocol->owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 819 | int n; |
| 820 | |
| 821 | if (!protocol->broadcast_socket_user_fd) { |
| 822 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 823 | * We are either running unforked / flat, or we are being |
| 824 | * called from poll thread context |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 825 | * eg, from a callback. In that case don't use sockets for |
| 826 | * broadcast IPC (since we can't open a socket connection to |
| 827 | * a socket listening on our own thread) but directly do the |
| 828 | * send action. |
| 829 | * |
| 830 | * Locking is not needed because we are by definition being |
| 831 | * called in the poll thread context and are serialized. |
| 832 | */ |
| 833 | |
| 834 | for (n = this->count_protocols + 1; n < this->fds_count; n++) { |
| 835 | |
| 836 | if ((unsigned long)this->wsi[n] < LWS_MAX_PROTOCOLS) |
| 837 | continue; |
| 838 | |
| 839 | /* never broadcast to non-established connection */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 840 | if (this->wsi[n]->state != WSI_STATE_ESTABLISHED) |
| 841 | continue; |
| 842 | |
| 843 | /* only broadcast to guys using requested protocol */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 844 | if (this->wsi[n]->protocol != protocol) |
| 845 | continue; |
| 846 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 847 | this->wsi[n]->protocol->callback(this->wsi[n], |
| 848 | LWS_CALLBACK_BROADCAST, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 849 | this->wsi[n]->user_space, |
| 850 | buf, len); |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 856 | /* |
| 857 | * We're being called from a different process context than the server |
| 858 | * loop. Instead of broadcasting directly, we send our |
| 859 | * payload on a socket to do the IPC; the server process will serialize |
| 860 | * the broadcast action in its main poll() loop. |
| 861 | * |
| 862 | * There's one broadcast socket listening for each protocol supported |
| 863 | * set up when the websocket server initializes |
| 864 | */ |
| 865 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame^] | 866 | n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 867 | |
| 868 | return n; |
| 869 | } |