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 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 34 | /* document the generic callback (it's a fake prototype under this) */ |
| 35 | /** |
| 36 | * callback() - User server actions |
| 37 | * @wsi: Opaque websocket instance pointer |
| 38 | * @reason: The reason for the call |
| 39 | * @user: Pointer to per-session user data allocated by library |
| 40 | * @in: Pointer used for some callback reasons |
| 41 | * @len: Length set for some callback reasons |
| 42 | * |
| 43 | * This callback is the way the user controls what is served. All the |
| 44 | * protocol detail is hidden and handled by the library. |
| 45 | * |
| 46 | * For each connection / session there is user data allocated that is |
| 47 | * pointed to by "user". You set the size of this user data area when |
| 48 | * the library is initialized with libwebsocket_create_server. |
| 49 | * |
| 50 | * You get an opportunity to initialize user data when called back with |
| 51 | * LWS_CALLBACK_ESTABLISHED reason. |
| 52 | * |
| 53 | * LWS_CALLBACK_ESTABLISHED: after successful websocket handshake |
| 54 | * |
| 55 | * LWS_CALLBACK_CLOSED: when the websocket session ends |
| 56 | * |
| 57 | * LWS_CALLBACK_SEND: opportunity to send to client (you would use |
| 58 | * libwebsocket_write() taking care about the |
| 59 | * special buffer requirements |
| 60 | * LWS_CALLBACK_RECEIVE: data has appeared for the server, it can be |
| 61 | * found at *in and is len bytes long |
| 62 | * |
| 63 | * LWS_CALLBACK_HTTP: an http request has come from a client that is not |
| 64 | * asking to upgrade the connection to a websocket |
| 65 | * one. This is a chance to serve http content, |
| 66 | * for example, to send a script to the client |
| 67 | * which will then open the websockets connection. |
| 68 | * @in points to the URI path requested and |
| 69 | * libwebsockets_serve_http_file() makes it very |
| 70 | * simple to send back a file to the client. |
| 71 | */ |
| 72 | extern int callback(struct libwebsocket * wsi, |
| 73 | enum libwebsocket_callback_reasons reason, void * user, |
| 74 | void *in, size_t len); |
| 75 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 76 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 77 | void |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 78 | libwebsocket_close_and_free_session(struct libwebsocket *wsi) |
| 79 | { |
| 80 | int n = wsi->state; |
| 81 | |
| 82 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 83 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 84 | if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED) |
| 85 | wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED, &wsi->user_space, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 86 | NULL, 0); |
| 87 | |
| 88 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 89 | if (wsi->utf8_token[n].token) |
| 90 | free(wsi->utf8_token[n].token); |
| 91 | |
| 92 | // fprintf(stderr, "closing fd=%d\n", wsi->sock); |
| 93 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 94 | #ifdef LWS_OPENSSL_SUPPORT |
| 95 | if (use_ssl) { |
| 96 | n = SSL_get_fd(wsi->ssl); |
| 97 | SSL_shutdown(wsi->ssl); |
| 98 | close(n); |
| 99 | SSL_free(wsi->ssl); |
| 100 | } else { |
| 101 | #endif |
| 102 | shutdown(wsi->sock, SHUT_RDWR); |
| 103 | close(wsi->sock); |
| 104 | #ifdef LWS_OPENSSL_SUPPORT |
| 105 | } |
| 106 | #endif |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 107 | if (wsi->user_space) |
| 108 | free(wsi->user_space); |
| 109 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 110 | free(wsi); |
| 111 | } |
| 112 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 113 | /** |
| 114 | * libwebsocket_create_server() - Create the listening websockets server |
| 115 | * @port: Port to listen on |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 116 | * @protocols: Array of structures listing supported protocols and a protocol- |
| 117 | * specific callback for each one. The list is ended with an |
| 118 | * entry that has a NULL callback pointer. |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 119 | * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want |
| 120 | * to listen using SSL, set to the filepath to fetch the |
| 121 | * server cert from, otherwise NULL for unencrypted |
| 122 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
| 123 | * else ignored |
| 124 | * @gid: group id to change to after setting listen socket, or -1. |
| 125 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 126 | * |
| 127 | * This function forks to create the listening socket and takes care |
| 128 | * of all initialization in one step. |
| 129 | * |
| 130 | * The callback function is called for a handful of events including |
| 131 | * http requests coming in, websocket connections becoming |
| 132 | * established, and data arriving; it's also called periodically to allow |
| 133 | * async transmission. |
| 134 | * |
| 135 | * The server created is a simple http server by default; part of the |
| 136 | * websocket standard is upgrading this http connection to a websocket one. |
| 137 | * |
| 138 | * This allows the same server to provide files like scripts and favicon / |
| 139 | * images or whatever over http and dynamic data over websockets all in |
| 140 | * one place; they're all handled in the user callback. |
| 141 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 142 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 143 | int libwebsocket_create_server(int port, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 144 | const struct libwebsocket_protocols *protocols, |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 145 | const char * ssl_cert_filepath, |
| 146 | const char * ssl_private_key_filepath, |
| 147 | int gid, int uid) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 148 | { |
| 149 | int n; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 150 | int client; |
Andy Green | 69fa072 | 2010-11-03 08:25:13 +0000 | [diff] [blame] | 151 | int sockfd; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 152 | int fd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 153 | unsigned int clilen; |
| 154 | struct sockaddr_in serv_addr, cli_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 155 | struct libwebsocket *wsi[MAX_CLIENTS + 1]; |
| 156 | struct pollfd fds[MAX_CLIENTS + 1]; |
| 157 | int fds_count = 0; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 158 | unsigned char buf[1024]; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 159 | int opt = 1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 160 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 161 | #ifdef LWS_OPENSSL_SUPPORT |
| 162 | const SSL_METHOD *method; |
| 163 | char ssl_err_buf[512]; |
| 164 | |
| 165 | use_ssl = ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL; |
| 166 | if (use_ssl) |
| 167 | fprintf(stderr, " Compiled with SSL support, using it\n"); |
| 168 | else |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 169 | fprintf(stderr, " Compiled with SSL support, not using it\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 170 | |
| 171 | #else |
| 172 | if (ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL) { |
| 173 | fprintf(stderr, " Not compiled for OpenSSl support!\n"); |
| 174 | return -1; |
| 175 | } |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 176 | fprintf(stderr, " Compiled without SSL support, serving unencrypted\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | #ifdef LWS_OPENSSL_SUPPORT |
| 180 | if (use_ssl) { |
| 181 | SSL_library_init(); |
| 182 | |
| 183 | OpenSSL_add_all_algorithms(); |
| 184 | SSL_load_error_strings(); |
| 185 | |
| 186 | // Firefox insists on SSLv23 not SSLv3 |
| 187 | // Konq disables SSLv2 by default now, SSLv23 works |
| 188 | |
| 189 | method = SSLv23_server_method(); // create server instance |
| 190 | if (!method) { |
| 191 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 192 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 193 | return -1; |
| 194 | } |
| 195 | ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 196 | if (!ssl_ctx) { |
| 197 | printf("problem creating ssl context: %s\n", |
| 198 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 199 | return -1; |
| 200 | } |
| 201 | /* set the local certificate from CertFile */ |
| 202 | n = SSL_CTX_use_certificate_file(ssl_ctx, |
| 203 | ssl_cert_filepath, SSL_FILETYPE_PEM); |
| 204 | if (n != 1) { |
| 205 | fprintf(stderr, "problem getting cert '%s': %s\n", |
| 206 | ssl_cert_filepath, |
| 207 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 208 | return -1; |
| 209 | } |
| 210 | /* set the private key from KeyFile */ |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 211 | if (SSL_CTX_use_PrivateKey_file(ssl_ctx, |
| 212 | ssl_private_key_filepath, |
| 213 | SSL_FILETYPE_PEM) != 1) { |
| 214 | fprintf(stderr, "ssl problem getting key '%s': %s\n", |
| 215 | ssl_private_key_filepath, |
| 216 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 217 | return (-1); |
| 218 | } |
| 219 | /* verify private key */ |
| 220 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 221 | fprintf(stderr, "Private SSL key doesn't match cert\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 222 | return (-1); |
| 223 | } |
| 224 | |
| 225 | /* SSL is happy and has a cert it's content with */ |
| 226 | } |
| 227 | #endif |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 228 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 229 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 230 | if (sockfd < 0) { |
| 231 | fprintf(stderr, "ERROR opening socket"); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 232 | return -1; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 233 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 234 | |
| 235 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 236 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 237 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 238 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 239 | serv_addr.sin_family = AF_INET; |
| 240 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 241 | serv_addr.sin_port = htons(port); |
| 242 | n = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 243 | if (n < 0) { |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 244 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", port, n, |
| 245 | errno); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | /* fork off a master server for this websocket server */ |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 250 | |
| 251 | n = fork(); |
| 252 | if (n < 0) { |
| 253 | fprintf(stderr, "Failed on forking server thread: %d\n", n); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 254 | return -1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* we are done as far as the caller is concerned */ |
| 258 | |
| 259 | if (n) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 260 | return sockfd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 261 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 262 | /* drop any root privs for this thread */ |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 263 | |
| 264 | if (gid != -1) |
| 265 | if (setgid(gid)) |
| 266 | fprintf(stderr, "setgid: %s\n", strerror(errno)); |
| 267 | if (uid != -1) |
| 268 | if (setuid(uid)) |
| 269 | fprintf(stderr, "setuid: %s\n", strerror(errno)); |
| 270 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 271 | /* |
| 272 | * sit there listening for connects, accept and service connections |
| 273 | * in a poll loop, without any further forking |
| 274 | */ |
| 275 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 276 | listen(sockfd, 5); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 277 | fprintf(stderr, " Listening on port %d\n", port); |
| 278 | |
| 279 | fds[0].fd = sockfd; |
| 280 | fds_count = 1; |
| 281 | fds[0].events = POLLIN; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 282 | |
| 283 | while (1) { |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 284 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 285 | n = poll(fds, fds_count, 50); |
| 286 | if (n < 0 || fds[0].revents & (POLLERR | POLLHUP)) { |
| 287 | // fprintf(stderr, "Listen Socket dead\n"); |
| 288 | goto fatal; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 289 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 290 | if (n == 0) /* poll timeout */ |
| 291 | goto poll_out; |
| 292 | |
| 293 | if (fds[0].revents & POLLIN) { |
| 294 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 295 | /* listen socket got an unencrypted connection... */ |
| 296 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 297 | clilen = sizeof(cli_addr); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 298 | fd = accept(sockfd, |
| 299 | (struct sockaddr *)&cli_addr, |
| 300 | &clilen); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 301 | if (fd < 0) { |
| 302 | fprintf(stderr, "ERROR on accept"); |
| 303 | continue; |
| 304 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 305 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 306 | if (fds_count >= MAX_CLIENTS) { |
| 307 | fprintf(stderr, "too busy"); |
| 308 | close(fd); |
| 309 | continue; |
| 310 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 311 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 312 | wsi[fds_count] = malloc(sizeof(struct libwebsocket)); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 313 | if (!wsi[fds_count]) |
| 314 | return -1; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 315 | |
| 316 | |
| 317 | #ifdef LWS_OPENSSL_SUPPORT |
| 318 | if (use_ssl) { |
| 319 | |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 320 | wsi[fds_count]->ssl = SSL_new(ssl_ctx); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 321 | if (wsi[fds_count]->ssl == NULL) { |
| 322 | fprintf(stderr, "SSL_new failed: %s\n", |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 323 | ERR_error_string(SSL_get_error( |
| 324 | wsi[fds_count]->ssl, 0), NULL)); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 325 | free(wsi[fds_count]); |
| 326 | continue; |
| 327 | } |
| 328 | |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 329 | SSL_set_fd(wsi[fds_count]->ssl, fd); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 330 | |
| 331 | n = SSL_accept(wsi[fds_count]->ssl); |
| 332 | if (n != 1) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 333 | /* |
| 334 | * browsers seem to probe with various |
| 335 | * ssl params which fail then retry |
| 336 | * and succeed |
| 337 | */ |
| 338 | debug("SSL_accept failed skt %u: %s\n", |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 339 | fd, |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 340 | ERR_error_string(SSL_get_error( |
| 341 | wsi[fds_count]->ssl, n), NULL)); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 342 | SSL_free(wsi[fds_count]->ssl); |
| 343 | free(wsi[fds_count]); |
| 344 | continue; |
| 345 | } |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 346 | debug("accepted new SSL conn " |
| 347 | "port %u on fd=%d SSL ver %s\n", |
| 348 | ntohs(cli_addr.sin_port), fd, |
| 349 | SSL_get_version(wsi[fds_count]->ssl)); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 350 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 351 | } else |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 352 | #endif |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 353 | debug("accepted new conn port %u on fd=%d\n", |
| 354 | ntohs(cli_addr.sin_port), fd); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 355 | |
| 356 | /* intialize the instance struct */ |
| 357 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 358 | wsi[fds_count]->sock = fd; |
| 359 | wsi[fds_count]->state = WSI_STATE_HTTP; |
| 360 | wsi[fds_count]->name_buffer_pos = 0; |
| 361 | |
| 362 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 363 | wsi[fds_count]->utf8_token[n].token = NULL; |
| 364 | wsi[fds_count]->utf8_token[n].token_len = 0; |
| 365 | } |
| 366 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 367 | /* |
| 368 | * these can only be set once the protocol is known |
| 369 | * we set an unestablished connection's protocol pointer |
| 370 | * to the start of the supported list, so it can look |
| 371 | * for matching ones during the handshake |
| 372 | */ |
| 373 | wsi[fds_count]->protocol = protocols; |
| 374 | wsi[fds_count]->user_space = NULL; |
| 375 | |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 376 | /* |
| 377 | * Default protocol is 76 |
| 378 | * After 76, there's a header specified to inform which |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 379 | * draft the client wants, when that's seen we modify |
| 380 | * the individual connection's spec revision accordingly |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 381 | */ |
| 382 | wsi[fds_count]->ietf_spec_revision = 76; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 383 | |
| 384 | fds[fds_count].events = POLLIN; |
| 385 | fds[fds_count++].fd = fd; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 388 | /* check for activity on client sockets */ |
| 389 | |
| 390 | for (client = 1; client < fds_count; client++) { |
| 391 | |
| 392 | /* handle session socket closed */ |
| 393 | |
| 394 | if (fds[client].revents & (POLLERR | POLLHUP)) { |
| 395 | |
| 396 | fprintf(stderr, "Session Socket dead\n"); |
| 397 | |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 398 | libwebsocket_close_and_free_session( |
| 399 | wsi[client]); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 400 | goto nuke_this; |
| 401 | } |
| 402 | |
| 403 | /* any incoming data ready? */ |
| 404 | |
| 405 | if (!(fds[client].revents & POLLIN)) |
| 406 | continue; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 407 | |
| 408 | #ifdef LWS_OPENSSL_SUPPORT |
| 409 | if (use_ssl) |
| 410 | n = SSL_read(wsi[client]->ssl, buf, sizeof buf); |
| 411 | else |
| 412 | #endif |
| 413 | n = recv(fds[client].fd, buf, sizeof(buf), 0); |
| 414 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 415 | if (n < 0) { |
| 416 | fprintf(stderr, "Socket read returned %d\n", n); |
| 417 | continue; |
| 418 | } |
| 419 | if (!n) { |
| 420 | // fprintf(stderr, "POLLIN with 0 len waiting\n"); |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 421 | libwebsocket_close_and_free_session( |
| 422 | wsi[client]); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 423 | goto nuke_this; |
| 424 | } |
| 425 | |
| 426 | /* service incoming data */ |
| 427 | |
| 428 | if (libwebsocket_read(wsi[client], buf, n) >= 0) |
| 429 | continue; |
| 430 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 431 | /* |
| 432 | * it closed and nuked wsi[client], so remove the |
| 433 | * socket handle and wsi from our service list |
| 434 | */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 435 | nuke_this: |
| 436 | for (n = client; n < fds_count - 1; n++) { |
| 437 | fds[n] = fds[n + 1]; |
| 438 | wsi[n] = wsi[n + 1]; |
| 439 | } |
| 440 | fds_count--; |
| 441 | client--; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 444 | poll_out: |
| 445 | for (client = 1; client < fds_count; client++) { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 446 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 447 | if (wsi[client]->state != WSI_STATE_ESTABLISHED) |
| 448 | continue; |
Andy Green | 69fa072 | 2010-11-03 08:25:13 +0000 | [diff] [blame] | 449 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame^] | 450 | wsi[client]->protocol->callback(wsi[client], LWS_CALLBACK_SEND, |
| 451 | &wsi[client]->user_space, NULL, 0); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | continue; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 455 | } |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 456 | |
| 457 | fatal: |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 458 | /* listening socket */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 459 | close(fds[0].fd); |
| 460 | for (client = 1; client < fds_count; client++) |
| 461 | libwebsocket_close_and_free_session(wsi[client]); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 462 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 463 | #ifdef LWS_OPENSSL_SUPPORT |
| 464 | SSL_CTX_free(ssl_ctx); |
| 465 | #endif |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 466 | kill(0, SIGTERM); |
| 467 | |
| 468 | return 0; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 471 | |