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