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