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