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 |
| 3 | * |
| 4 | * Copyright (C) 2010 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame^] | 22 | #include "private-libwebsockets.h" |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 23 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 24 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 25 | SSL_CTX *ssl_ctx; |
| 26 | int use_ssl; |
| 27 | #endif |
| 28 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 29 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame^] | 30 | extern int |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 31 | libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 32 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 33 | |
| 34 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame^] | 35 | void |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 36 | libwebsocket_close_and_free_session(struct libwebsocket *wsi) |
| 37 | { |
| 38 | int n = wsi->state; |
| 39 | |
| 40 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 41 | |
| 42 | if (wsi->callback && n == WSI_STATE_ESTABLISHED) |
| 43 | wsi->callback(wsi, LWS_CALLBACK_CLOSED, &wsi->user_space[0], |
| 44 | NULL, 0); |
| 45 | |
| 46 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 47 | if (wsi->utf8_token[n].token) |
| 48 | free(wsi->utf8_token[n].token); |
| 49 | |
| 50 | // fprintf(stderr, "closing fd=%d\n", wsi->sock); |
| 51 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 52 | #ifdef LWS_OPENSSL_SUPPORT |
| 53 | if (use_ssl) { |
| 54 | n = SSL_get_fd(wsi->ssl); |
| 55 | SSL_shutdown(wsi->ssl); |
| 56 | close(n); |
| 57 | SSL_free(wsi->ssl); |
| 58 | } else { |
| 59 | #endif |
| 60 | shutdown(wsi->sock, SHUT_RDWR); |
| 61 | close(wsi->sock); |
| 62 | #ifdef LWS_OPENSSL_SUPPORT |
| 63 | } |
| 64 | #endif |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 65 | free(wsi); |
| 66 | } |
| 67 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 68 | /** |
| 69 | * libwebsocket_create_server() - Create the listening websockets server |
| 70 | * @port: Port to listen on |
| 71 | * @callback: The callback in user code to perform actual serving |
| 72 | * @protocol: Which version of the websockets protocol (currently 76) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 73 | * @user_area_size: How much memory to allocate per connection session |
| 74 | * which will be used by the user application to store |
| 75 | * per-session data. A pointer to this space is given |
| 76 | * when the user callback is called. |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 77 | * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want |
| 78 | * to listen using SSL, set to the filepath to fetch the |
| 79 | * server cert from, otherwise NULL for unencrypted |
| 80 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
| 81 | * else ignored |
| 82 | * @gid: group id to change to after setting listen socket, or -1. |
| 83 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 84 | * |
| 85 | * This function forks to create the listening socket and takes care |
| 86 | * of all initialization in one step. |
| 87 | * |
| 88 | * The callback function is called for a handful of events including |
| 89 | * http requests coming in, websocket connections becoming |
| 90 | * established, and data arriving; it's also called periodically to allow |
| 91 | * async transmission. |
| 92 | * |
| 93 | * The server created is a simple http server by default; part of the |
| 94 | * websocket standard is upgrading this http connection to a websocket one. |
| 95 | * |
| 96 | * This allows the same server to provide files like scripts and favicon / |
| 97 | * images or whatever over http and dynamic data over websockets all in |
| 98 | * one place; they're all handled in the user callback. |
| 99 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 100 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 101 | int libwebsocket_create_server(int port, |
| 102 | int (*callback)(struct libwebsocket *, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 103 | enum libwebsocket_callback_reasons, |
| 104 | void *, void *, size_t), |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 105 | int protocol, size_t user_area_size, |
| 106 | const char * ssl_cert_filepath, |
| 107 | const char * ssl_private_key_filepath, |
| 108 | int gid, int uid) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 109 | { |
| 110 | int n; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 111 | int client; |
Andy Green | 69fa072 | 2010-11-03 08:25:13 +0000 | [diff] [blame] | 112 | int sockfd; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 113 | int fd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 114 | unsigned int clilen; |
| 115 | struct sockaddr_in serv_addr, cli_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 116 | struct libwebsocket *wsi[MAX_CLIENTS + 1]; |
| 117 | struct pollfd fds[MAX_CLIENTS + 1]; |
| 118 | int fds_count = 0; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 119 | unsigned char buf[1024]; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 120 | int opt = 1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 121 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 122 | #ifdef LWS_OPENSSL_SUPPORT |
| 123 | const SSL_METHOD *method; |
| 124 | char ssl_err_buf[512]; |
| 125 | |
| 126 | use_ssl = ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL; |
| 127 | if (use_ssl) |
| 128 | fprintf(stderr, " Compiled with SSL support, using it\n"); |
| 129 | else |
| 130 | fprintf(stderr, " Compiled with SSL support, but not using it\n"); |
| 131 | |
| 132 | #else |
| 133 | if (ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL) { |
| 134 | fprintf(stderr, " Not compiled for OpenSSl support!\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | fprintf(stderr, " Compiled without SSL support, listening unencrypted\n"); |
| 138 | #endif |
| 139 | |
| 140 | #ifdef LWS_OPENSSL_SUPPORT |
| 141 | if (use_ssl) { |
| 142 | SSL_library_init(); |
| 143 | |
| 144 | OpenSSL_add_all_algorithms(); |
| 145 | SSL_load_error_strings(); |
| 146 | |
| 147 | // Firefox insists on SSLv23 not SSLv3 |
| 148 | // Konq disables SSLv2 by default now, SSLv23 works |
| 149 | |
| 150 | method = SSLv23_server_method(); // create server instance |
| 151 | if (!method) { |
| 152 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 153 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 154 | return -1; |
| 155 | } |
| 156 | ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 157 | if (!ssl_ctx) { |
| 158 | printf("problem creating ssl context: %s\n", |
| 159 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 160 | return -1; |
| 161 | } |
| 162 | /* set the local certificate from CertFile */ |
| 163 | n = SSL_CTX_use_certificate_file(ssl_ctx, |
| 164 | ssl_cert_filepath, SSL_FILETYPE_PEM); |
| 165 | if (n != 1) { |
| 166 | fprintf(stderr, "problem getting cert '%s': %s\n", |
| 167 | ssl_cert_filepath, |
| 168 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 169 | return -1; |
| 170 | } |
| 171 | /* set the private key from KeyFile */ |
| 172 | if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_private_key_filepath, |
| 173 | SSL_FILETYPE_PEM) != 1) { |
| 174 | fprintf(stderr, "ssl problem getting key '%s': %s\n", ssl_private_key_filepath, ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 175 | return (-1); |
| 176 | } |
| 177 | /* verify private key */ |
| 178 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
| 179 | fprintf(stderr, "Private SSL key does not match cert\n"); |
| 180 | return (-1); |
| 181 | } |
| 182 | |
| 183 | /* SSL is happy and has a cert it's content with */ |
| 184 | } |
| 185 | #endif |
| 186 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 187 | /* sanity check */ |
| 188 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 189 | switch (protocol) { |
| 190 | case 0: |
| 191 | case 2: |
| 192 | case 76: |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 193 | fprintf(stderr, " Using protocol v%d\n", protocol); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 194 | break; |
| 195 | default: |
| 196 | fprintf(stderr, "protocol %d not supported (try 0 2 or 76)\n", |
| 197 | protocol); |
| 198 | return -1; |
| 199 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 200 | |
| 201 | if (!callback) { |
| 202 | fprintf(stderr, "callback is not optional!\n"); |
| 203 | return -1; |
| 204 | } |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 205 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 206 | /* sit there listening for connects, accept and spawn session servers */ |
| 207 | |
| 208 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 209 | if (sockfd < 0) { |
| 210 | fprintf(stderr, "ERROR opening socket"); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 211 | return -1; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 212 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 213 | |
| 214 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 215 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 216 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 217 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 218 | serv_addr.sin_family = AF_INET; |
| 219 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 220 | serv_addr.sin_port = htons(port); |
| 221 | n = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 222 | if (n < 0) { |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 223 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", port, n, |
| 224 | errno); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | /* fork off a master server for this websocket server */ |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 229 | |
| 230 | n = fork(); |
| 231 | if (n < 0) { |
| 232 | fprintf(stderr, "Failed on forking server thread: %d\n", n); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 233 | return -1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* we are done as far as the caller is concerned */ |
| 237 | |
| 238 | if (n) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 239 | return sockfd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 240 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 241 | // drop any root privs for this thread |
| 242 | |
| 243 | if (gid != -1) |
| 244 | if (setgid(gid)) |
| 245 | fprintf(stderr, "setgid: %s\n", strerror(errno)); |
| 246 | if (uid != -1) |
| 247 | if (setuid(uid)) |
| 248 | fprintf(stderr, "setuid: %s\n", strerror(errno)); |
| 249 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 250 | /* we are running in a forked subprocess now */ |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 251 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 252 | listen(sockfd, 5); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 253 | fprintf(stderr, " Listening on port %d\n", port); |
| 254 | |
| 255 | fds[0].fd = sockfd; |
| 256 | fds_count = 1; |
| 257 | fds[0].events = POLLIN; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 258 | |
| 259 | while (1) { |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 260 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 261 | n = poll(fds, fds_count, 50); |
| 262 | if (n < 0 || fds[0].revents & (POLLERR | POLLHUP)) { |
| 263 | // fprintf(stderr, "Listen Socket dead\n"); |
| 264 | goto fatal; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 265 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 266 | if (n == 0) /* poll timeout */ |
| 267 | goto poll_out; |
| 268 | |
| 269 | if (fds[0].revents & POLLIN) { |
| 270 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 271 | /* listen socket got an unencrypted connection... */ |
| 272 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 273 | clilen = sizeof(cli_addr); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 274 | fd = accept(sockfd, |
| 275 | (struct sockaddr *)&cli_addr, |
| 276 | &clilen); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 277 | if (fd < 0) { |
| 278 | fprintf(stderr, "ERROR on accept"); |
| 279 | continue; |
| 280 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 281 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 282 | if (fds_count >= MAX_CLIENTS) { |
| 283 | fprintf(stderr, "too busy"); |
| 284 | close(fd); |
| 285 | continue; |
| 286 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 287 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 288 | wsi[fds_count] = malloc(sizeof(struct libwebsocket) + |
| 289 | user_area_size); |
| 290 | if (!wsi[fds_count]) |
| 291 | return -1; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 292 | |
| 293 | |
| 294 | #ifdef LWS_OPENSSL_SUPPORT |
| 295 | if (use_ssl) { |
| 296 | |
| 297 | wsi[fds_count]->ssl = SSL_new(ssl_ctx); // get new SSL state with context |
| 298 | if (wsi[fds_count]->ssl == NULL) { |
| 299 | fprintf(stderr, "SSL_new failed: %s\n", |
| 300 | ERR_error_string(SSL_get_error(wsi[fds_count]->ssl, 0), NULL)); |
| 301 | free(wsi[fds_count]); |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | SSL_set_fd(wsi[fds_count]->ssl, fd); // set SSL socket |
| 306 | |
| 307 | n = SSL_accept(wsi[fds_count]->ssl); |
| 308 | if (n != 1) { |
| 309 | /* browsers seem to probe with various ssl params which fail then retry */ |
| 310 | debug("SSL_accept failed for socket %u: %s\n", |
| 311 | fd, |
| 312 | ERR_error_string(SSL_get_error(wsi[fds_count]->ssl, n), |
| 313 | NULL)); |
| 314 | SSL_free(wsi[fds_count]->ssl); |
| 315 | free(wsi[fds_count]); |
| 316 | continue; |
| 317 | } |
| 318 | debug("accepted new SSL conn port %u on fd=%d SSL ver %s\n", |
| 319 | ntohs(cli_addr.sin_port), fd, SSL_get_version(wsi[fds_count]->ssl)); |
| 320 | |
| 321 | } else { |
| 322 | // fprintf(stderr, "accepted new conn port %u on fd=%d\n", |
| 323 | // ntohs(cli_addr.sin_port), fd); |
| 324 | } |
| 325 | #endif |
| 326 | |
| 327 | /* intialize the instance struct */ |
| 328 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 329 | wsi[fds_count]->sock = fd; |
| 330 | wsi[fds_count]->state = WSI_STATE_HTTP; |
| 331 | wsi[fds_count]->name_buffer_pos = 0; |
| 332 | |
| 333 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 334 | wsi[fds_count]->utf8_token[n].token = NULL; |
| 335 | wsi[fds_count]->utf8_token[n].token_len = 0; |
| 336 | } |
| 337 | |
| 338 | wsi[fds_count]->callback = callback; |
| 339 | wsi[fds_count]->ietf_spec_revision = protocol; |
| 340 | |
| 341 | fds[fds_count].events = POLLIN; |
| 342 | fds[fds_count++].fd = fd; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 345 | /* check for activity on client sockets */ |
| 346 | |
| 347 | for (client = 1; client < fds_count; client++) { |
| 348 | |
| 349 | /* handle session socket closed */ |
| 350 | |
| 351 | if (fds[client].revents & (POLLERR | POLLHUP)) { |
| 352 | |
| 353 | fprintf(stderr, "Session Socket dead\n"); |
| 354 | |
| 355 | libwebsocket_close_and_free_session(wsi[client]); |
| 356 | goto nuke_this; |
| 357 | } |
| 358 | |
| 359 | /* any incoming data ready? */ |
| 360 | |
| 361 | if (!(fds[client].revents & POLLIN)) |
| 362 | continue; |
| 363 | |
| 364 | // fprintf(stderr, "POLLIN\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 365 | |
| 366 | #ifdef LWS_OPENSSL_SUPPORT |
| 367 | if (use_ssl) |
| 368 | n = SSL_read(wsi[client]->ssl, buf, sizeof buf); |
| 369 | else |
| 370 | #endif |
| 371 | n = recv(fds[client].fd, buf, sizeof(buf), 0); |
| 372 | |
| 373 | // fprintf(stderr, "read returned %d\n", n); |
| 374 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 375 | if (n < 0) { |
| 376 | fprintf(stderr, "Socket read returned %d\n", n); |
| 377 | continue; |
| 378 | } |
| 379 | if (!n) { |
| 380 | // fprintf(stderr, "POLLIN with 0 len waiting\n"); |
| 381 | libwebsocket_close_and_free_session(wsi[client]); |
| 382 | goto nuke_this; |
| 383 | } |
| 384 | |
| 385 | /* service incoming data */ |
| 386 | |
| 387 | if (libwebsocket_read(wsi[client], buf, n) >= 0) |
| 388 | continue; |
| 389 | |
| 390 | /* it closed and nuked wsi[client] */ |
| 391 | nuke_this: |
| 392 | for (n = client; n < fds_count - 1; n++) { |
| 393 | fds[n] = fds[n + 1]; |
| 394 | wsi[n] = wsi[n + 1]; |
| 395 | } |
| 396 | fds_count--; |
| 397 | client--; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 400 | poll_out: |
| 401 | for (client = 1; client < fds_count; client++) { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 402 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 403 | if (wsi[client]->state != WSI_STATE_ESTABLISHED) |
| 404 | continue; |
| 405 | |
| 406 | if (!wsi[client]->callback) |
| 407 | continue; |
Andy Green | 69fa072 | 2010-11-03 08:25:13 +0000 | [diff] [blame] | 408 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 409 | wsi[client]->callback(wsi[client], LWS_CALLBACK_SEND, |
| 410 | &wsi[client]->user_space[0], NULL, 0); |
| 411 | } |
| 412 | |
| 413 | continue; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 414 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 415 | |
| 416 | fatal: |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 417 | /* listening socket */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 418 | close(fds[0].fd); |
| 419 | for (client = 1; client < fds_count; client++) |
| 420 | libwebsocket_close_and_free_session(wsi[client]); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 421 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 422 | #ifdef LWS_OPENSSL_SUPPORT |
| 423 | SSL_CTX_free(ssl_ctx); |
| 424 | #endif |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 425 | kill(0, SIGTERM); |
| 426 | |
| 427 | return 0; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 430 | |