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