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 | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 23 | #include <ifaddrs.h> |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 24 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 25 | /* |
| 26 | * In-place str to lower case |
| 27 | */ |
| 28 | |
| 29 | static void |
| 30 | strtolower(char *s) |
| 31 | { |
| 32 | while (*s) { |
| 33 | *s = tolower(*s); |
| 34 | s++; |
| 35 | } |
| 36 | } |
| 37 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 38 | /* file descriptor hash management */ |
| 39 | |
| 40 | struct libwebsocket * |
| 41 | wsi_from_fd(struct libwebsocket_context *this, int fd) |
| 42 | { |
| 43 | int h = LWS_FD_HASH(fd); |
| 44 | int n = 0; |
| 45 | |
| 46 | for (n = 0; n < this->fd_hashtable[h].length; n++) |
| 47 | if (this->fd_hashtable[h].wsi[n]->sock == fd) |
| 48 | return this->fd_hashtable[h].wsi[n]; |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | insert_wsi(struct libwebsocket_context *this, struct libwebsocket *wsi) |
| 55 | { |
| 56 | int h = LWS_FD_HASH(wsi->sock); |
| 57 | |
| 58 | if (this->fd_hashtable[h].length == MAX_CLIENTS - 1) { |
| 59 | fprintf(stderr, "hash table overflow\n"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | this->fd_hashtable[h].wsi[this->fd_hashtable[h].length++] = wsi; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int |
| 69 | delete_from_fd(struct libwebsocket_context *this, int fd) |
| 70 | { |
| 71 | int h = LWS_FD_HASH(fd); |
| 72 | int n = 0; |
| 73 | |
| 74 | for (n = 0; n < this->fd_hashtable[h].length; n++) |
| 75 | if (this->fd_hashtable[h].wsi[n]->sock == fd) { |
| 76 | while (n < this->fd_hashtable[h].length) { |
| 77 | this->fd_hashtable[h].wsi[n] = |
| 78 | this->fd_hashtable[h].wsi[n + 1]; |
| 79 | n++; |
| 80 | } |
| 81 | this->fd_hashtable[h].length--; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | fprintf(stderr, "Failed to find fd %d requested for " |
| 87 | "delete in hashtable\n", fd); |
| 88 | return 1; |
| 89 | } |
| 90 | |
Andy Green | 1f9bf52 | 2011-02-14 21:14:37 +0000 | [diff] [blame] | 91 | #ifdef LWS_OPENSSL_SUPPORT |
| 92 | static void |
| 93 | libwebsockets_decode_ssl_error(void) |
| 94 | { |
| 95 | char buf[256]; |
| 96 | u_long err; |
| 97 | |
| 98 | while ((err = ERR_get_error()) != 0) { |
| 99 | ERR_error_string_n(err, buf, sizeof(buf)); |
| 100 | fprintf(stderr, "*** %s\n", buf); |
| 101 | } |
| 102 | } |
| 103 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 104 | |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 105 | |
| 106 | static int |
| 107 | interface_to_sa(const char* ifname, struct sockaddr_in *addr, size_t addrlen) |
| 108 | { |
| 109 | int rc = -1; |
| 110 | struct ifaddrs *ifr; |
| 111 | struct ifaddrs *ifc; |
| 112 | struct sockaddr_in *sin; |
| 113 | |
| 114 | getifaddrs(&ifr); |
| 115 | for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) { |
| 116 | if (strcmp(ifc->ifa_name, ifname)) |
| 117 | continue; |
| 118 | if (ifc->ifa_addr == NULL) |
| 119 | continue; |
| 120 | sin = (struct sockaddr_in *)ifc->ifa_addr; |
| 121 | if (sin->sin_family != AF_INET) |
| 122 | continue; |
| 123 | memcpy(addr, sin, addrlen); |
| 124 | rc = 0; |
| 125 | } |
| 126 | |
| 127 | freeifaddrs(ifr); |
| 128 | |
| 129 | return rc; |
| 130 | } |
| 131 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 132 | void |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 133 | libwebsocket_close_and_free_session(struct libwebsocket_context *this, |
| 134 | struct libwebsocket *wsi) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 135 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 136 | int n; |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 137 | int old_state; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 138 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 139 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 140 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 141 | if (!wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 142 | return; |
| 143 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 144 | old_state = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 145 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 146 | if (old_state == WSI_STATE_DEAD_SOCKET) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 147 | return; |
| 148 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 149 | /* remove this fd from wsi mapping hashtable */ |
| 150 | |
| 151 | delete_from_fd(this, wsi->sock); |
| 152 | |
| 153 | /* delete it from the internal poll list if still present */ |
| 154 | |
| 155 | for (n = 0; n < this->fds_count; n++) { |
| 156 | if (this->fds[n].fd != wsi->sock) |
| 157 | continue; |
| 158 | while (n < this->fds_count - 1) { |
| 159 | this->fds[n] = this->fds[n + 1]; |
| 160 | n++; |
| 161 | } |
| 162 | this->fds_count--; |
| 163 | /* we only have to deal with one */ |
| 164 | n = this->fds_count; |
| 165 | } |
| 166 | |
| 167 | /* remove also from external POLL support via protocol 0 */ |
| 168 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 169 | this->protocols[0].callback(this, wsi, |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 170 | LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0); |
| 171 | |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 172 | /* |
| 173 | * signal we are closing, libsocket_write will |
| 174 | * add any necessary version-specific stuff. If the write fails, |
| 175 | * no worries we are closing anyway. If we didn't initiate this |
| 176 | * close, then our state has been changed to |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 177 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 178 | */ |
| 179 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 180 | if (old_state == WSI_STATE_ESTABLISHED) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 181 | libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 0, |
| 182 | LWS_WRITE_CLOSE); |
| 183 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 184 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 185 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 186 | /* tell the user it's all over for this guy */ |
| 187 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 188 | if (wsi->protocol->callback && old_state == WSI_STATE_ESTABLISHED) |
| 189 | wsi->protocol->callback(this, wsi, LWS_CALLBACK_CLOSED, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 190 | wsi->user_space, NULL, 0); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 191 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 192 | /* free up his allocations */ |
| 193 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 194 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 195 | if (wsi->utf8_token[n].token) |
| 196 | free(wsi->utf8_token[n].token); |
| 197 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 198 | /* fprintf(stderr, "closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 199 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 200 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 201 | if (wsi->ssl) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 202 | n = SSL_get_fd(wsi->ssl); |
| 203 | SSL_shutdown(wsi->ssl); |
| 204 | close(n); |
| 205 | SSL_free(wsi->ssl); |
| 206 | } else { |
| 207 | #endif |
| 208 | shutdown(wsi->sock, SHUT_RDWR); |
| 209 | close(wsi->sock); |
| 210 | #ifdef LWS_OPENSSL_SUPPORT |
| 211 | } |
| 212 | #endif |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 213 | if (wsi->user_space) |
| 214 | free(wsi->user_space); |
| 215 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 216 | free(wsi); |
| 217 | } |
| 218 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 219 | /** |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 220 | * libwebsockets_hangup_on_client() - Server calls to terminate client |
| 221 | * connection |
| 222 | * @this: libwebsockets context |
| 223 | * @fd: Connection socket descriptor |
| 224 | */ |
| 225 | |
| 226 | void |
| 227 | libwebsockets_hangup_on_client(struct libwebsocket_context *this, int fd) |
| 228 | { |
| 229 | struct libwebsocket *wsi = wsi_from_fd(this, fd); |
Andy Green | de6ab32 | 2011-02-13 09:15:10 +0000 | [diff] [blame] | 230 | int n; |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 231 | |
| 232 | if (wsi == NULL) |
| 233 | return; |
| 234 | |
Andy Green | de6ab32 | 2011-02-13 09:15:10 +0000 | [diff] [blame] | 235 | delete_from_fd(this, fd); |
| 236 | |
| 237 | for (n = 0; n < this->fds_count - 1; n++) |
| 238 | if (this->fds[n].fd == fd) { |
| 239 | while (n < this->fds_count - 1) { |
| 240 | this->fds[n] = this->fds[n + 1]; |
| 241 | n++; |
| 242 | } |
| 243 | n = this->fds_count; |
| 244 | this->fds_count--; |
| 245 | } |
| 246 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 247 | libwebsocket_close_and_free_session(this, wsi); |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 252 | * libwebsockets_get_peer_addresses() - Get client address information |
| 253 | * @fd: Connection socket descriptor |
| 254 | * @name: Buffer to take client address name |
| 255 | * @name_len: Length of client address name buffer |
| 256 | * @rip: Buffer to take client address IP qotted quad |
| 257 | * @rip_len: Length of client address IP buffer |
| 258 | * |
| 259 | * This function fills in @name and @rip with the name and IP of |
| 260 | * the client connected with socket descriptor @fd. Names may be |
| 261 | * truncated if there is not enough room. If either cannot be |
| 262 | * determined, they will be returned as valid zero-length strings. |
| 263 | */ |
| 264 | |
| 265 | void |
| 266 | libwebsockets_get_peer_addresses(int fd, char *name, int name_len, |
| 267 | char *rip, int rip_len) |
| 268 | { |
| 269 | unsigned int len; |
| 270 | struct sockaddr_in sin; |
| 271 | struct hostent *host; |
| 272 | struct hostent *host1; |
| 273 | char ip[128]; |
| 274 | char *p; |
| 275 | int n; |
| 276 | |
| 277 | rip[0] = '\0'; |
| 278 | name[0] = '\0'; |
| 279 | |
| 280 | len = sizeof sin; |
| 281 | if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { |
| 282 | perror("getpeername"); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, |
| 287 | AF_INET); |
| 288 | if (host == NULL) { |
| 289 | perror("gethostbyaddr"); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | strncpy(name, host->h_name, name_len); |
| 294 | name[name_len - 1] = '\0'; |
| 295 | |
| 296 | host1 = gethostbyname(host->h_name); |
| 297 | if (host1 == NULL) |
| 298 | return; |
| 299 | p = (char *)host1; |
| 300 | n = 0; |
| 301 | while (p != NULL) { |
| 302 | p = host1->h_addr_list[n++]; |
| 303 | if (p == NULL) |
| 304 | continue; |
| 305 | if (host1->h_addrtype != AF_INET) |
| 306 | continue; |
| 307 | |
| 308 | sprintf(ip, "%d.%d.%d.%d", |
| 309 | p[0], p[1], p[2], p[3]); |
| 310 | p = NULL; |
| 311 | strncpy(rip, ip, rip_len); |
| 312 | rip[rip_len - 1] = '\0'; |
| 313 | } |
| 314 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * libwebsocket_service_fd() - Service polled socket with something waiting |
| 318 | * @this: Websocket context |
| 319 | * @pollfd: The pollfd entry describing the socket fd and which events |
| 320 | * happened. |
| 321 | * |
| 322 | * This function closes any active connections and then frees the |
| 323 | * context. After calling this, any further use of the context is |
| 324 | * undefined. |
| 325 | */ |
| 326 | |
| 327 | int |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 328 | libwebsocket_service_fd(struct libwebsocket_context *this, |
| 329 | struct pollfd *pollfd) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 330 | { |
| 331 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BROADCAST_PAYLOAD + |
| 332 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 333 | struct libwebsocket *wsi; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 334 | struct libwebsocket *new_wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 335 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 336 | int m; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 337 | size_t len; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 338 | int accept_fd; |
| 339 | unsigned int clilen; |
| 340 | struct sockaddr_in cli_addr; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 341 | struct timeval tv; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 342 | static const char magic_websocket_guid[] = |
| 343 | "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 344 | static const char magic_websocket_04_masking_guid[] = |
| 345 | "61AC5F19-FBBA-4540-B96F-6561F1AB40A8"; |
| 346 | char hash[20]; |
| 347 | char pkt[1024]; |
| 348 | char *p = &pkt[0]; |
| 349 | const char *pc; |
| 350 | int okay = 0; |
| 351 | #ifdef LWS_OPENSSL_SUPPORT |
| 352 | char ssl_err_buf[512]; |
| 353 | #endif |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 354 | /* |
| 355 | * you can call us with pollfd = NULL to just allow the once-per-second |
| 356 | * global timeout checks; if less than a second since the last check |
| 357 | * it returns immediately then. |
| 358 | */ |
| 359 | |
| 360 | gettimeofday(&tv, NULL); |
| 361 | |
| 362 | if (this->last_timeout_check_s != tv.tv_sec) { |
| 363 | this->last_timeout_check_s = tv.tv_sec; |
| 364 | |
| 365 | /* global timeout check once per second */ |
| 366 | |
| 367 | for (n = 0; n < this->fds_count; n++) { |
| 368 | wsi = wsi_from_fd(this, this->fds[n].fd); |
| 369 | if (!wsi->pending_timeout) |
| 370 | continue; |
| 371 | |
| 372 | /* |
| 373 | * if we went beyond the allowed time, kill the |
| 374 | * connection |
| 375 | */ |
| 376 | |
| 377 | if (tv.tv_sec > wsi->pending_timeout_limit) |
| 378 | libwebsocket_close_and_free_session(this, wsi); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* just here for timeout management? */ |
| 383 | |
| 384 | if (pollfd == NULL) |
| 385 | return 0; |
| 386 | |
| 387 | /* no, here to service a socket descriptor */ |
| 388 | |
| 389 | wsi = wsi_from_fd(this, pollfd->fd); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 390 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 391 | if (wsi == NULL) |
| 392 | return 1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 393 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 394 | switch (wsi->mode) { |
| 395 | case LWS_CONNMODE_SERVER_LISTENER: |
| 396 | |
| 397 | /* pollin means a client has connected to us then */ |
| 398 | |
| 399 | if (!pollfd->revents & POLLIN) |
| 400 | break; |
| 401 | |
| 402 | /* listen socket got an unencrypted connection... */ |
| 403 | |
| 404 | clilen = sizeof(cli_addr); |
| 405 | accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr, |
| 406 | &clilen); |
| 407 | if (accept_fd < 0) { |
| 408 | fprintf(stderr, "ERROR on accept"); |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | if (this->fds_count >= MAX_CLIENTS) { |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 413 | fprintf(stderr, "too busy to accept new client\n"); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 414 | close(accept_fd); |
| 415 | break; |
| 416 | } |
| 417 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 418 | /* |
| 419 | * look at who we connected to and give user code a chance |
| 420 | * to reject based on client IP. There's no protocol selected |
| 421 | * yet so we issue this to protocols[0] |
| 422 | */ |
| 423 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 424 | if ((this->protocols[0].callback)(this, wsi, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 425 | LWS_CALLBACK_FILTER_NETWORK_CONNECTION, |
| 426 | (void*)(long)accept_fd, NULL, 0)) { |
| 427 | fprintf(stderr, "Callback denied network connection\n"); |
| 428 | close(accept_fd); |
| 429 | break; |
| 430 | } |
| 431 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 432 | /* accepting connection to main listener */ |
| 433 | |
| 434 | new_wsi = malloc(sizeof(struct libwebsocket)); |
| 435 | if (new_wsi == NULL) { |
| 436 | fprintf(stderr, "Out of memory for new connection\n"); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | memset(new_wsi, 0, sizeof (struct libwebsocket)); |
| 441 | new_wsi->sock = accept_fd; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 442 | new_wsi->pending_timeout = NO_PENDING_TIMEOUT; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 443 | |
| 444 | #ifdef LWS_OPENSSL_SUPPORT |
| 445 | new_wsi->ssl = NULL; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 446 | |
| 447 | if (this->use_ssl) { |
| 448 | |
| 449 | new_wsi->ssl = SSL_new(this->ssl_ctx); |
| 450 | if (new_wsi->ssl == NULL) { |
| 451 | fprintf(stderr, "SSL_new failed: %s\n", |
| 452 | ERR_error_string(SSL_get_error( |
| 453 | new_wsi->ssl, 0), NULL)); |
Andy Green | 1f9bf52 | 2011-02-14 21:14:37 +0000 | [diff] [blame] | 454 | libwebsockets_decode_ssl_error(); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 455 | free(new_wsi); |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | SSL_set_fd(new_wsi->ssl, accept_fd); |
| 460 | |
| 461 | n = SSL_accept(new_wsi->ssl); |
| 462 | if (n != 1) { |
| 463 | /* |
| 464 | * browsers seem to probe with various |
| 465 | * ssl params which fail then retry |
| 466 | * and succeed |
| 467 | */ |
| 468 | debug("SSL_accept failed skt %u: %s\n", |
| 469 | pollfd->fd, |
| 470 | ERR_error_string(SSL_get_error( |
| 471 | new_wsi->ssl, n), NULL)); |
| 472 | SSL_free( |
| 473 | new_wsi->ssl); |
| 474 | free(new_wsi); |
| 475 | break; |
| 476 | } |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 477 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 478 | debug("accepted new SSL conn " |
| 479 | "port %u on fd=%d SSL ver %s\n", |
| 480 | ntohs(cli_addr.sin_port), accept_fd, |
| 481 | SSL_get_version(new_wsi->ssl)); |
| 482 | |
| 483 | } else |
| 484 | #endif |
| 485 | debug("accepted new conn port %u on fd=%d\n", |
| 486 | ntohs(cli_addr.sin_port), accept_fd); |
| 487 | |
| 488 | /* intialize the instance struct */ |
| 489 | |
| 490 | new_wsi->state = WSI_STATE_HTTP; |
| 491 | new_wsi->name_buffer_pos = 0; |
| 492 | new_wsi->mode = LWS_CONNMODE_WS_SERVING; |
| 493 | |
| 494 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 495 | new_wsi->utf8_token[n].token = NULL; |
| 496 | new_wsi->utf8_token[n].token_len = 0; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * these can only be set once the protocol is known |
| 501 | * we set an unestablished connection's protocol pointer |
| 502 | * to the start of the supported list, so it can look |
| 503 | * for matching ones during the handshake |
| 504 | */ |
| 505 | new_wsi->protocol = this->protocols; |
| 506 | new_wsi->user_space = NULL; |
| 507 | |
| 508 | /* |
| 509 | * Default protocol is 76 / 00 |
| 510 | * After 76, there's a header specified to inform which |
| 511 | * draft the client wants, when that's seen we modify |
| 512 | * the individual connection's spec revision accordingly |
| 513 | */ |
| 514 | new_wsi->ietf_spec_revision = 0; |
| 515 | |
| 516 | insert_wsi(this, new_wsi); |
| 517 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 518 | /* |
| 519 | * make sure NO events are seen yet on this new socket |
| 520 | * (otherwise we inherit old fds[client].revents from |
| 521 | * previous socket there and die mysteriously! ) |
| 522 | */ |
| 523 | this->fds[this->fds_count].revents = 0; |
| 524 | |
| 525 | this->fds[this->fds_count].events = POLLIN; |
| 526 | this->fds[this->fds_count++].fd = accept_fd; |
| 527 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 528 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 529 | this->protocols[0].callback(this, new_wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 530 | LWS_CALLBACK_ADD_POLL_FD, |
| 531 | (void *)(long)accept_fd, NULL, POLLIN); |
| 532 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 533 | break; |
| 534 | |
| 535 | case LWS_CONNMODE_BROADCAST_PROXY_LISTENER: |
| 536 | |
| 537 | /* as we are listening, POLLIN means accept() is needed */ |
| 538 | |
| 539 | if (!pollfd->revents & POLLIN) |
| 540 | break; |
| 541 | |
| 542 | /* listen socket got an unencrypted connection... */ |
| 543 | |
| 544 | clilen = sizeof(cli_addr); |
| 545 | accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr, |
| 546 | &clilen); |
| 547 | if (accept_fd < 0) { |
| 548 | fprintf(stderr, "ERROR on accept"); |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | if (this->fds_count >= MAX_CLIENTS) { |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 553 | fprintf(stderr, "too busy to accept new broadcast " |
| 554 | "proxy client\n"); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 555 | close(accept_fd); |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | /* create a dummy wsi for the connection and add it */ |
| 560 | |
| 561 | new_wsi = malloc(sizeof(struct libwebsocket)); |
| 562 | memset(new_wsi, 0, sizeof (struct libwebsocket)); |
| 563 | new_wsi->sock = accept_fd; |
| 564 | new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY; |
| 565 | new_wsi->state = WSI_STATE_ESTABLISHED; |
| 566 | /* note which protocol we are proxying */ |
| 567 | new_wsi->protocol_index_for_broadcast_proxy = |
| 568 | wsi->protocol_index_for_broadcast_proxy; |
| 569 | insert_wsi(this, new_wsi); |
| 570 | |
| 571 | /* add connected socket to internal poll array */ |
| 572 | |
| 573 | this->fds[this->fds_count].revents = 0; |
| 574 | this->fds[this->fds_count].events = POLLIN; |
| 575 | this->fds[this->fds_count++].fd = accept_fd; |
| 576 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 577 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 578 | this->protocols[0].callback(this, new_wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 579 | LWS_CALLBACK_ADD_POLL_FD, |
| 580 | (void *)(long)accept_fd, NULL, POLLIN); |
| 581 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 582 | break; |
| 583 | |
| 584 | case LWS_CONNMODE_BROADCAST_PROXY: |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 585 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 586 | /* handle session socket closed */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 587 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 588 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 589 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 590 | debug("Session Socket %p (fd=%d) dead\n", |
Timothy J Fontaine | b86d64e | 2011-02-14 17:55:27 +0000 | [diff] [blame] | 591 | (void *)wsi, pollfd->fd); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 592 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 593 | libwebsocket_close_and_free_session(this, wsi); |
| 594 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 595 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 596 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 597 | /* the guy requested a callback when it was OK to write */ |
| 598 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 599 | if (pollfd->revents & POLLOUT) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 600 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 601 | /* one shot */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 602 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 603 | pollfd->events &= ~POLLOUT; |
| 604 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 605 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 606 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 607 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 608 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 609 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 610 | wsi->protocol->callback(this, wsi, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 611 | LWS_CALLBACK_CLIENT_WRITEABLE, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 612 | wsi->user_space, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 613 | NULL, 0); |
| 614 | } |
| 615 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 616 | /* any incoming data ready? */ |
| 617 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 618 | if (!(pollfd->revents & POLLIN)) |
| 619 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 620 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 621 | /* get the issued broadcast payload from the socket */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 622 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 623 | len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING, |
| 624 | MAX_BROADCAST_PAYLOAD); |
| 625 | if (len < 0) { |
| 626 | fprintf(stderr, "Error reading broadcast payload\n"); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 627 | break; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 628 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 629 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 630 | /* broadcast it to all guys with this protocol index */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 631 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 632 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 633 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 634 | for (m = 0; m < this->fd_hashtable[n].length; m++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 635 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 636 | new_wsi = this->fd_hashtable[n].wsi[m]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 637 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 638 | /* only to clients we are serving to */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 639 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 640 | if (new_wsi->mode != LWS_CONNMODE_WS_SERVING) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 641 | continue; |
| 642 | |
| 643 | /* |
| 644 | * never broadcast to non-established |
| 645 | * connection |
| 646 | */ |
| 647 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 648 | if (new_wsi->state != WSI_STATE_ESTABLISHED) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 649 | continue; |
| 650 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 651 | /* |
| 652 | * only broadcast to connections using |
| 653 | * the requested protocol |
| 654 | */ |
| 655 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 656 | if (new_wsi->protocol->protocol_index != |
| 657 | wsi->protocol_index_for_broadcast_proxy) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 658 | continue; |
| 659 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 660 | /* broadcast it to this connection */ |
| 661 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 662 | new_wsi->protocol->callback(this, new_wsi, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 663 | LWS_CALLBACK_BROADCAST, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 664 | new_wsi->user_space, |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 665 | buf + LWS_SEND_BUFFER_PRE_PADDING, len); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 666 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 667 | } |
| 668 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 669 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 670 | case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: |
| 671 | |
| 672 | /* handle proxy hung up on us */ |
| 673 | |
| 674 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 675 | |
| 676 | fprintf(stderr, "Proxy connection %p (fd=%d) dead\n", |
| 677 | (void *)wsi, pollfd->fd); |
| 678 | |
| 679 | libwebsocket_close_and_free_session(this, wsi); |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | n = recv(wsi->sock, pkt, sizeof pkt, 0); |
| 684 | if (n < 0) { |
| 685 | libwebsocket_close_and_free_session(this, wsi); |
| 686 | fprintf(stderr, "ERROR reading from proxy socket\n"); |
| 687 | return 1; |
| 688 | } |
| 689 | |
| 690 | pkt[13] = '\0'; |
| 691 | if (strcmp(pkt, "HTTP/1.0 200 ") != 0) { |
| 692 | libwebsocket_close_and_free_session(this, wsi); |
| 693 | fprintf(stderr, "ERROR from proxy: %s\n", pkt); |
| 694 | return 1; |
| 695 | } |
| 696 | |
| 697 | /* clear his proxy connection timeout */ |
| 698 | |
| 699 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 700 | |
| 701 | /* fallthru */ |
| 702 | |
| 703 | case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: |
| 704 | |
| 705 | #ifdef LWS_OPENSSL_SUPPORT |
| 706 | if (wsi->use_ssl) { |
| 707 | |
| 708 | wsi->ssl = SSL_new(this->ssl_client_ctx); |
| 709 | wsi->client_bio = BIO_new_socket(wsi->sock, BIO_NOCLOSE); |
| 710 | SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio); |
| 711 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame^] | 712 | SSL_set_ex_data(wsi->ssl, |
| 713 | this->openssl_websocket_private_data_index, this); |
| 714 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 715 | if (SSL_connect(wsi->ssl) <= 0) { |
| 716 | fprintf(stderr, "SSL connect error %s\n", |
| 717 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 718 | libwebsocket_close_and_free_session(this, wsi); |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | n = SSL_get_verify_result(wsi->ssl); |
| 723 | if (n != X509_V_OK) { |
| 724 | if (n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || |
| 725 | wsi->use_ssl != 2) { |
| 726 | |
| 727 | fprintf(stderr, "server's cert didn't " |
| 728 | "look good %d\n", n); |
| 729 | libwebsocket_close_and_free_session(this, wsi); |
| 730 | return 1; |
| 731 | } |
| 732 | } |
| 733 | } else { |
| 734 | wsi->ssl = NULL; |
| 735 | #endif |
| 736 | |
| 737 | |
| 738 | #ifdef LWS_OPENSSL_SUPPORT |
| 739 | } |
| 740 | #endif |
| 741 | |
| 742 | /* |
| 743 | * create the random key |
| 744 | */ |
| 745 | |
| 746 | n = read(this->fd_random, hash, 16); |
| 747 | if (n != 16) { |
| 748 | fprintf(stderr, "Unable to read from random dev %s\n", |
| 749 | SYSTEM_RANDOM_FILEPATH); |
| 750 | free(wsi->c_path); |
| 751 | free(wsi->c_host); |
| 752 | free(wsi->c_origin); |
| 753 | if (wsi->c_protocol) |
| 754 | free(wsi->c_protocol); |
| 755 | libwebsocket_close_and_free_session(this, wsi); |
| 756 | return 1; |
| 757 | } |
| 758 | |
| 759 | lws_b64_encode_string(hash, 16, wsi->key_b64, |
| 760 | sizeof wsi->key_b64); |
| 761 | |
| 762 | /* |
| 763 | * 04 example client handshake |
| 764 | * |
| 765 | * GET /chat HTTP/1.1 |
| 766 | * Host: server.example.com |
| 767 | * Upgrade: websocket |
| 768 | * Connection: Upgrade |
| 769 | * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== |
| 770 | * Sec-WebSocket-Origin: http://example.com |
| 771 | * Sec-WebSocket-Protocol: chat, superchat |
| 772 | * Sec-WebSocket-Version: 4 |
| 773 | */ |
| 774 | |
| 775 | p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path); |
| 776 | p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host); |
| 777 | p += sprintf(p, "Upgrade: websocket\x0d\x0a"); |
| 778 | p += sprintf(p, "Connection: Upgrade\x0d\x0a" |
| 779 | "Sec-WebSocket-Key: "); |
| 780 | strcpy(p, wsi->key_b64); |
| 781 | p += strlen(wsi->key_b64); |
| 782 | p += sprintf(p, "\x0d\x0aSec-WebSocket-Origin: %s\x0d\x0a", |
| 783 | wsi->c_origin); |
| 784 | if (wsi->c_protocol != NULL) |
| 785 | p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a", |
| 786 | wsi->c_protocol); |
| 787 | p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a\x0d\x0a", |
| 788 | wsi->ietf_spec_revision); |
| 789 | |
| 790 | /* done with these now */ |
| 791 | |
| 792 | free(wsi->c_path); |
| 793 | free(wsi->c_host); |
| 794 | free(wsi->c_origin); |
| 795 | |
| 796 | /* prepare the expected server accept response */ |
| 797 | |
| 798 | strcpy((char *)buf, wsi->key_b64); |
| 799 | strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid); |
| 800 | |
| 801 | SHA1(buf, strlen((char *)buf), (unsigned char *)hash); |
| 802 | |
| 803 | lws_b64_encode_string(hash, 20, |
| 804 | wsi->initial_handshake_hash_base64, |
| 805 | sizeof wsi->initial_handshake_hash_base64); |
| 806 | |
| 807 | /* send our request to the server */ |
| 808 | |
| 809 | #ifdef LWS_OPENSSL_SUPPORT |
| 810 | if (wsi->use_ssl) |
| 811 | n = SSL_write(wsi->ssl, pkt, p - pkt); |
| 812 | else |
| 813 | #endif |
| 814 | n = send(wsi->sock, pkt, p - pkt, 0); |
| 815 | |
| 816 | if (n < 0) { |
| 817 | fprintf(stderr, "ERROR writing to client socket\n"); |
| 818 | libwebsocket_close_and_free_session(this, wsi); |
| 819 | return 1; |
| 820 | } |
| 821 | |
| 822 | wsi->parser_state = WSI_TOKEN_NAME_PART; |
| 823 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY; |
| 824 | libwebsocket_set_timeout(wsi, |
| 825 | PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, 5); |
| 826 | |
| 827 | break; |
| 828 | |
| 829 | case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: |
| 830 | |
| 831 | /* handle server hung up on us */ |
| 832 | |
| 833 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 834 | |
| 835 | fprintf(stderr, "Server connection %p (fd=%d) dead\n", |
| 836 | (void *)wsi, pollfd->fd); |
| 837 | |
| 838 | goto bail3; |
| 839 | } |
| 840 | |
| 841 | |
| 842 | /* interpret the server response */ |
| 843 | |
| 844 | /* |
| 845 | * HTTP/1.1 101 Switching Protocols |
| 846 | * Upgrade: websocket |
| 847 | * Connection: Upgrade |
| 848 | * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo= |
| 849 | * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC== |
| 850 | * Sec-WebSocket-Protocol: chat |
| 851 | */ |
| 852 | |
| 853 | #ifdef LWS_OPENSSL_SUPPORT |
| 854 | if (wsi->use_ssl) |
| 855 | len = SSL_read(wsi->ssl, pkt, sizeof pkt); |
| 856 | else |
| 857 | #endif |
| 858 | len = recv(wsi->sock, pkt, sizeof pkt, 0); |
| 859 | |
| 860 | if (len < 0) { |
| 861 | fprintf(stderr, |
| 862 | "libwebsocket_client_handshake read error\n"); |
| 863 | goto bail3; |
| 864 | } |
| 865 | |
| 866 | p = pkt; |
| 867 | for (n = 0; n < len; n++) |
| 868 | libwebsocket_parse(wsi, *p++); |
| 869 | |
| 870 | if (wsi->parser_state != WSI_PARSING_COMPLETE) { |
| 871 | fprintf(stderr, "libwebsocket_client_handshake " |
| 872 | "server response ailed parsing\n"); |
| 873 | goto bail3; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * well, what the server sent looked reasonable for syntax. |
| 878 | * Now let's confirm it sent all the necessary headers |
| 879 | */ |
| 880 | |
| 881 | if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len || |
| 882 | !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || |
| 883 | !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len || |
| 884 | !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len || |
| 885 | !wsi->utf8_token[WSI_TOKEN_NONCE].token_len || |
| 886 | (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len && |
| 887 | wsi->c_protocol != NULL)) { |
| 888 | fprintf(stderr, "libwebsocket_client_handshake " |
| 889 | "missing required header(s)\n"); |
| 890 | pkt[len] = '\0'; |
| 891 | fprintf(stderr, "%s", pkt); |
| 892 | goto bail3; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Everything seems to be there, now take a closer look at what |
| 897 | * is in each header |
| 898 | */ |
| 899 | |
| 900 | strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token); |
| 901 | if (strcmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, |
| 902 | "101 switching protocols")) { |
| 903 | fprintf(stderr, "libwebsocket_client_handshake " |
| 904 | "server sent bad HTTP response '%s'\n", |
| 905 | wsi->utf8_token[WSI_TOKEN_HTTP].token); |
| 906 | goto bail3; |
| 907 | } |
| 908 | |
| 909 | strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token); |
| 910 | if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token, |
| 911 | "websocket")) { |
| 912 | fprintf(stderr, "libwebsocket_client_handshake server " |
| 913 | "sent bad Upgrade header '%s'\n", |
| 914 | wsi->utf8_token[WSI_TOKEN_UPGRADE].token); |
| 915 | goto bail3; |
| 916 | } |
| 917 | |
| 918 | strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token); |
| 919 | if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token, |
| 920 | "upgrade")) { |
| 921 | fprintf(stderr, "libwebsocket_client_handshake server " |
| 922 | "sent bad Connection hdr '%s'\n", |
| 923 | wsi->utf8_token[WSI_TOKEN_CONNECTION].token); |
| 924 | goto bail3; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | pc = wsi->c_protocol; |
| 929 | |
| 930 | /* |
| 931 | * confirm the protocol the server wants to talk was in the list |
| 932 | * of protocols we offered |
| 933 | */ |
| 934 | |
| 935 | if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) { |
| 936 | |
| 937 | /* |
| 938 | * no protocol name to work from, |
| 939 | * default to first protocol |
| 940 | */ |
| 941 | wsi->protocol = &this->protocols[0]; |
| 942 | |
| 943 | free(wsi->c_protocol); |
| 944 | |
| 945 | goto check_accept; |
| 946 | } |
| 947 | |
| 948 | while (*pc && !okay) { |
| 949 | if ((!strncmp(pc, |
| 950 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, |
| 951 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) && |
| 952 | (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' || |
| 953 | pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) { |
| 954 | okay = 1; |
| 955 | continue; |
| 956 | } |
| 957 | while (*pc && *pc != ',') |
| 958 | pc++; |
| 959 | while (*pc && *pc != ' ') |
| 960 | pc++; |
| 961 | } |
| 962 | |
| 963 | /* done with him now */ |
| 964 | |
| 965 | if (wsi->c_protocol) |
| 966 | free(wsi->c_protocol); |
| 967 | |
| 968 | |
| 969 | if (!okay) { |
| 970 | fprintf(stderr, "libwebsocket_client_handshake server " |
| 971 | "sent bad protocol '%s'\n", |
| 972 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); |
| 973 | goto bail2; |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * identify the selected protocol struct and set it |
| 978 | */ |
| 979 | n = 0; |
| 980 | wsi->protocol = NULL; |
| 981 | while (this->protocols[n].callback) { |
| 982 | if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, |
| 983 | this->protocols[n].name) == 0) |
| 984 | wsi->protocol = &this->protocols[n]; |
| 985 | n++; |
| 986 | } |
| 987 | |
| 988 | if (wsi->protocol == NULL) { |
| 989 | fprintf(stderr, "libwebsocket_client_handshake server " |
| 990 | "requested protocol '%s', which we " |
| 991 | "said we supported but we don't!\n", |
| 992 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); |
| 993 | goto bail2; |
| 994 | } |
| 995 | |
| 996 | check_accept: |
| 997 | /* |
| 998 | * Confirm his accept token is the one we precomputed |
| 999 | */ |
| 1000 | |
| 1001 | if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token, |
| 1002 | wsi->initial_handshake_hash_base64)) { |
| 1003 | fprintf(stderr, "libwebsocket_client_handshake server " |
| 1004 | "sent bad ACCEPT '%s' vs computed '%s'\n", |
| 1005 | wsi->utf8_token[WSI_TOKEN_ACCEPT].token, |
| 1006 | wsi->initial_handshake_hash_base64); |
| 1007 | goto bail2; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Calculate the masking key to use when sending data to server |
| 1012 | */ |
| 1013 | |
| 1014 | strcpy((char *)buf, wsi->key_b64); |
| 1015 | p = (char *)buf + strlen(wsi->key_b64); |
| 1016 | strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token); |
| 1017 | p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len; |
| 1018 | strcpy(p, magic_websocket_04_masking_guid); |
| 1019 | SHA1(buf, strlen((char *)buf), wsi->masking_key_04); |
| 1020 | |
| 1021 | /* allocate the per-connection user memory (if any) */ |
| 1022 | |
| 1023 | if (wsi->protocol->per_session_data_size) { |
| 1024 | wsi->user_space = malloc( |
| 1025 | wsi->protocol->per_session_data_size); |
| 1026 | if (wsi->user_space == NULL) { |
| 1027 | fprintf(stderr, "Out of memory for " |
| 1028 | "conn user space\n"); |
| 1029 | goto bail2; |
| 1030 | } |
| 1031 | } else |
| 1032 | wsi->user_space = NULL; |
| 1033 | |
| 1034 | /* clear his proxy connection timeout */ |
| 1035 | |
| 1036 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 1037 | |
| 1038 | /* mark him as being alive */ |
| 1039 | |
| 1040 | wsi->state = WSI_STATE_ESTABLISHED; |
| 1041 | wsi->mode = LWS_CONNMODE_WS_CLIENT; |
| 1042 | |
| 1043 | fprintf(stderr, "handshake OK for protocol %s\n", |
| 1044 | wsi->protocol->name); |
| 1045 | |
| 1046 | /* call him back to inform him he is up */ |
| 1047 | |
| 1048 | wsi->protocol->callback(this, wsi, |
| 1049 | LWS_CALLBACK_CLIENT_ESTABLISHED, |
| 1050 | wsi->user_space, |
| 1051 | NULL, 0); |
| 1052 | |
| 1053 | break; |
| 1054 | |
| 1055 | bail3: |
| 1056 | if (wsi->c_protocol) |
| 1057 | free(wsi->c_protocol); |
| 1058 | |
| 1059 | bail2: |
| 1060 | libwebsocket_close_and_free_session(this, wsi); |
| 1061 | return 1; |
| 1062 | |
| 1063 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1064 | case LWS_CONNMODE_WS_SERVING: |
| 1065 | case LWS_CONNMODE_WS_CLIENT: |
| 1066 | |
| 1067 | /* handle session socket closed */ |
| 1068 | |
| 1069 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 1070 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1071 | fprintf(stderr, "Session Socket %p (fd=%d) dead\n", |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1072 | (void *)wsi, pollfd->fd); |
| 1073 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1074 | libwebsocket_close_and_free_session(this, wsi); |
| 1075 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1078 | /* the guy requested a callback when it was OK to write */ |
| 1079 | |
| 1080 | if (pollfd->revents & POLLOUT) { |
| 1081 | |
| 1082 | pollfd->events &= ~POLLOUT; |
| 1083 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1084 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1085 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1086 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 1087 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 1088 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1089 | wsi->protocol->callback(this, wsi, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1090 | LWS_CALLBACK_CLIENT_WRITEABLE, |
| 1091 | wsi->user_space, |
| 1092 | NULL, 0); |
| 1093 | } |
| 1094 | |
| 1095 | /* any incoming data ready? */ |
| 1096 | |
| 1097 | if (!(pollfd->revents & POLLIN)) |
| 1098 | break; |
| 1099 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1100 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1101 | if (wsi->ssl) |
| 1102 | n = SSL_read(wsi->ssl, buf, sizeof buf); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1103 | else |
| 1104 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1105 | n = recv(pollfd->fd, buf, sizeof buf, 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (n < 0) { |
| 1108 | fprintf(stderr, "Socket read returned %d\n", n); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1109 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1110 | } |
| 1111 | if (!n) { |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1112 | libwebsocket_close_and_free_session(this, wsi); |
| 1113 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1116 | /* service incoming data */ |
| 1117 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1118 | n = libwebsocket_read(this, wsi, buf, n); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1119 | if (n >= 0) |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1120 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1121 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1122 | /* we closed wsi */ |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1123 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1124 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1130 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1131 | /** |
| 1132 | * libwebsocket_context_destroy() - Destroy the websocket context |
| 1133 | * @this: Websocket context |
| 1134 | * |
| 1135 | * This function closes any active connections and then frees the |
| 1136 | * context. After calling this, any further use of the context is |
| 1137 | * undefined. |
| 1138 | */ |
| 1139 | void |
| 1140 | libwebsocket_context_destroy(struct libwebsocket_context *this) |
| 1141 | { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1142 | int n; |
| 1143 | int m; |
| 1144 | struct libwebsocket *wsi; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1145 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1146 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1147 | for (m = 0; m < this->fd_hashtable[n].length; m++) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1148 | wsi = this->fd_hashtable[n].wsi[m]; |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1149 | libwebsocket_close_and_free_session(this, wsi); |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 1150 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1151 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1152 | close(this->fd_random); |
| 1153 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1154 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1155 | if (this->ssl_ctx) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1156 | SSL_CTX_free(this->ssl_ctx); |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1157 | if (this->ssl_client_ctx) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 1158 | SSL_CTX_free(this->ssl_client_ctx); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1159 | #endif |
| 1160 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1161 | free(this); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * libwebsocket_service() - Service any pending websocket activity |
| 1166 | * @this: Websocket context |
| 1167 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 1168 | * service otherwise block and service immediately, returning |
| 1169 | * after the timeout if nothing needed service. |
| 1170 | * |
| 1171 | * This function deals with any pending websocket traffic, for three |
| 1172 | * kinds of event. It handles these events on both server and client |
| 1173 | * types of connection the same. |
| 1174 | * |
| 1175 | * 1) Accept new connections to our context's server |
| 1176 | * |
| 1177 | * 2) Perform pending broadcast writes initiated from other forked |
| 1178 | * processes (effectively serializing asynchronous broadcasts) |
| 1179 | * |
| 1180 | * 3) Call the receive callback for incoming frame data received by |
| 1181 | * server or client connections. |
| 1182 | * |
| 1183 | * You need to call this service function periodically to all the above |
| 1184 | * functions to happen; if your application is single-threaded you can |
| 1185 | * just call it in your main event loop. |
| 1186 | * |
| 1187 | * Alternatively you can fork a new process that asynchronously handles |
| 1188 | * calling this service in a loop. In that case you are happy if this |
| 1189 | * call blocks your thread until it needs to take care of something and |
| 1190 | * would call it with a large nonzero timeout. Your loop then takes no |
| 1191 | * CPU while there is nothing happening. |
| 1192 | * |
| 1193 | * If you are calling it in a single-threaded app, you don't want it to |
| 1194 | * wait around blocking other things in your loop from happening, so you |
| 1195 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 1196 | * nothing is pending, or as soon as it services whatever was pending. |
| 1197 | */ |
| 1198 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1199 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1200 | int |
| 1201 | libwebsocket_service(struct libwebsocket_context *this, int timeout_ms) |
| 1202 | { |
| 1203 | int n; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* stay dead once we are dead */ |
| 1206 | |
| 1207 | if (this == NULL) |
| 1208 | return 1; |
| 1209 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1210 | /* wait for something to need service */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1211 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1212 | n = poll(this->fds, this->fds_count, timeout_ms); |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1213 | if (n == 0) /* poll timeout */ |
| 1214 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1215 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1216 | if (n < 0) { |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 1217 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1218 | fprintf(stderr, "Listen Socket dead\n"); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 1219 | */ |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1220 | return 1; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1221 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1222 | |
| 1223 | /* handle accept on listening socket? */ |
| 1224 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1225 | for (n = 0; n < this->fds_count; n++) |
| 1226 | if (this->fds[n].revents) |
| 1227 | libwebsocket_service_fd(this, &this->fds[n]); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1228 | |
| 1229 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1232 | /** |
| 1233 | * libwebsocket_callback_on_writable() - Request a callback when this socket |
| 1234 | * becomes able to be written to without |
| 1235 | * blocking |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1236 | * |
| 1237 | * @this: libwebsockets context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1238 | * @wsi: Websocket connection instance to get callback for |
| 1239 | */ |
| 1240 | |
| 1241 | int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1242 | libwebsocket_callback_on_writable(struct libwebsocket_context *this, |
| 1243 | struct libwebsocket *wsi) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1244 | { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1245 | int n; |
| 1246 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1247 | for (n = 0; n < this->fds_count; n++) |
| 1248 | if (this->fds[n].fd == wsi->sock) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1249 | this->fds[n].events |= POLLOUT; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1250 | n = this->fds_count; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1253 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1254 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1255 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 1256 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 1257 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1258 | return 1; |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * libwebsocket_callback_on_writable_all_protocol() - Request a callback for |
| 1263 | * all connections using the given protocol when it |
| 1264 | * becomes possible to write to each socket without |
| 1265 | * blocking in turn. |
| 1266 | * |
| 1267 | * @protocol: Protocol whose connections will get callbacks |
| 1268 | */ |
| 1269 | |
| 1270 | int |
| 1271 | libwebsocket_callback_on_writable_all_protocol( |
| 1272 | const struct libwebsocket_protocols *protocol) |
| 1273 | { |
| 1274 | struct libwebsocket_context *this = protocol->owning_server; |
| 1275 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1276 | int m; |
| 1277 | struct libwebsocket *wsi; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1278 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1279 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) { |
| 1280 | |
| 1281 | for (m = 0; m < this->fd_hashtable[n].length; m++) { |
| 1282 | |
| 1283 | wsi = this->fd_hashtable[n].wsi[m]; |
| 1284 | |
| 1285 | if (wsi->protocol == protocol) |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1286 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1293 | /** |
| 1294 | * libwebsocket_set_timeout() - marks the wsi as subject to a timeout |
| 1295 | * |
| 1296 | * You will not need this unless you are doing something special |
| 1297 | * |
| 1298 | * @wsi: Websocket connection instance |
| 1299 | * @reason: timeout reason |
| 1300 | * @secs: how many seconds |
| 1301 | */ |
| 1302 | |
| 1303 | void |
| 1304 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 1305 | enum pending_timeout reason, int secs) |
| 1306 | { |
| 1307 | struct timeval tv; |
| 1308 | |
| 1309 | gettimeofday(&tv, NULL); |
| 1310 | |
| 1311 | wsi->pending_timeout_limit = tv.tv_sec + secs; |
| 1312 | wsi->pending_timeout = reason; |
| 1313 | } |
| 1314 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1315 | |
| 1316 | /** |
| 1317 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 1318 | * |
| 1319 | * You will not need this unless you are doing something special |
| 1320 | * |
| 1321 | * @wsi: Websocket connection instance |
| 1322 | */ |
| 1323 | |
| 1324 | int |
| 1325 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 1326 | { |
| 1327 | return wsi->sock; |
| 1328 | } |
| 1329 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1330 | /** |
| 1331 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
| 1332 | * receieved packets. |
| 1333 | * |
| 1334 | * If the output side of a server process becomes choked, this allows flow |
| 1335 | * control for the input side. |
| 1336 | * |
| 1337 | * @wsi: Websocket connection instance to get callback for |
| 1338 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 1339 | */ |
| 1340 | |
| 1341 | int |
| 1342 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 1343 | { |
| 1344 | struct libwebsocket_context *this = wsi->protocol->owning_server; |
| 1345 | int n; |
| 1346 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1347 | for (n = 0; n < this->fds_count; n++) |
| 1348 | if (this->fds[n].fd == wsi->sock) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1349 | if (enable) |
| 1350 | this->fds[n].events |= POLLIN; |
| 1351 | else |
| 1352 | this->fds[n].events &= ~POLLIN; |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1357 | if (enable) |
| 1358 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1359 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1360 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 1361 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 1362 | else |
| 1363 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1364 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1365 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 1366 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 1367 | |
| 1368 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1369 | fprintf(stderr, "libwebsocket_callback_on_writable " |
| 1370 | "unable to find socket\n"); |
| 1371 | return 1; |
| 1372 | } |
| 1373 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1374 | /** |
| 1375 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 1376 | * |
| 1377 | * This is typically used by client code to fill in the host parameter |
| 1378 | * when making a client connection. You can only call it after the context |
| 1379 | * has been created. |
| 1380 | * |
| 1381 | * @this: Websocket context |
| 1382 | */ |
| 1383 | |
| 1384 | |
| 1385 | extern const char * |
| 1386 | libwebsocket_canonical_hostname(struct libwebsocket_context *this) |
| 1387 | { |
| 1388 | return (const char *)this->canonical_hostname; |
| 1389 | } |
| 1390 | |
| 1391 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1392 | static void sigpipe_handler(int x) |
| 1393 | { |
| 1394 | } |
| 1395 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame^] | 1396 | #ifdef LWS_OPENSSL_SUPPORT |
| 1397 | static int |
| 1398 | OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 1399 | { |
| 1400 | |
| 1401 | SSL *ssl; |
| 1402 | int n; |
| 1403 | // struct libwebsocket_context *this; |
| 1404 | |
| 1405 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 1406 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1407 | |
| 1408 | /* |
| 1409 | * !!! can't get this->openssl_websocket_private_data_index |
| 1410 | * can't store as a static either |
| 1411 | */ |
| 1412 | // this = SSL_get_ex_data(ssl, this->openssl_websocket_private_data_index); |
| 1413 | |
| 1414 | n = this->protocols[0].callback(NULL, NULL, |
| 1415 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
| 1416 | x509_ctx, ssl, preverify_ok); |
| 1417 | |
| 1418 | /* convert return code from 0 = OK to 1 = OK */ |
| 1419 | |
| 1420 | if (!n) |
| 1421 | n = 1; |
| 1422 | else |
| 1423 | n = 0; |
| 1424 | |
| 1425 | return n; |
| 1426 | } |
| 1427 | #endif |
| 1428 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1429 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1430 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1431 | * libwebsocket_create_context() - Create the websocket handler |
| 1432 | * @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] | 1433 | * any port, that's what you want if you are not running a |
| 1434 | * websocket server at all but just using it as a client |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1435 | * @interface: NULL to bind the listen socket to all interfaces, or the |
| 1436 | * interface name, eg, "eth2" |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 1437 | * @protocols: Array of structures listing supported protocols and a protocol- |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1438 | * specific callback for each one. The list is ended with an |
| 1439 | * entry that has a NULL callback pointer. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1440 | * It's not const because we write the owning_server member |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1441 | * @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] | 1442 | * to listen using SSL, set to the filepath to fetch the |
| 1443 | * server cert from, otherwise NULL for unencrypted |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1444 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1445 | * else ignored |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1446 | * @gid: group id to change to after setting listen socket, or -1. |
| 1447 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 1448 | * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1449 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1450 | * This function creates the listening socket and takes care |
| 1451 | * of all initialization in one step. |
| 1452 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1453 | * After initialization, it returns a struct libwebsocket_context * that |
| 1454 | * represents this server. After calling, user code needs to take care |
| 1455 | * of calling libwebsocket_service() with the context pointer to get the |
| 1456 | * server's sockets serviced. This can be done in the same process context |
| 1457 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1458 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1459 | * The protocol callback functions are called for a handful of events |
| 1460 | * including http requests coming in, websocket connections becoming |
| 1461 | * established, and data arriving; it's also called periodically to allow |
| 1462 | * async transmission. |
| 1463 | * |
| 1464 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 1465 | * at that time websocket protocol has not been negotiated. Other |
| 1466 | * protocols after the first one never see any HTTP callack activity. |
| 1467 | * |
| 1468 | * The server created is a simple http server by default; part of the |
| 1469 | * websocket standard is upgrading this http connection to a websocket one. |
| 1470 | * |
| 1471 | * This allows the same server to provide files like scripts and favicon / |
| 1472 | * images or whatever over http and dynamic data over websockets all in |
| 1473 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1474 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1475 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1476 | struct libwebsocket_context * |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1477 | libwebsocket_create_context(int port, const char *interface, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1478 | struct libwebsocket_protocols *protocols, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1479 | const char *ssl_cert_filepath, |
| 1480 | const char *ssl_private_key_filepath, |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 1481 | int gid, int uid, unsigned int options) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1482 | { |
| 1483 | int n; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1484 | int sockfd = 0; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 1485 | int fd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1486 | struct sockaddr_in serv_addr, cli_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 1487 | int opt = 1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1488 | struct libwebsocket_context *this = NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1489 | unsigned int slen; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1490 | char *p; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1491 | char hostname[1024]; |
Andy Green | 42f6914 | 2011-01-30 08:10:02 +0000 | [diff] [blame] | 1492 | struct hostent *he; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1493 | struct libwebsocket *wsi; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1494 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1495 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 1496 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1497 | char ssl_err_buf[512]; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1498 | #endif |
| 1499 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1500 | this = malloc(sizeof(struct libwebsocket_context)); |
| 1501 | if (!this) { |
| 1502 | fprintf(stderr, "No memory for websocket context\n"); |
| 1503 | return NULL; |
| 1504 | } |
| 1505 | this->protocols = protocols; |
| 1506 | this->listen_port = port; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1507 | this->http_proxy_port = 0; |
| 1508 | this->http_proxy_address[0] = '\0'; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 1509 | this->options = options; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1510 | this->fds_count = 0; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1511 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1512 | this->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); |
| 1513 | if (this->fd_random < 0) { |
| 1514 | fprintf(stderr, "Unable to open random device %s %d\n", |
| 1515 | SYSTEM_RANDOM_FILEPATH, this->fd_random); |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1519 | /* find canonical hostname */ |
| 1520 | |
| 1521 | hostname[(sizeof hostname) - 1] = '\0'; |
| 1522 | gethostname(hostname, (sizeof hostname) - 1); |
| 1523 | he = gethostbyname(hostname); |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1524 | if (he) { |
| 1525 | strncpy(this->canonical_hostname, he->h_name, |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1526 | sizeof this->canonical_hostname - 1); |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1527 | this->canonical_hostname[sizeof this->canonical_hostname - 1] = |
| 1528 | '\0'; |
| 1529 | } else |
| 1530 | strncpy(this->canonical_hostname, hostname, |
| 1531 | sizeof this->canonical_hostname - 1); |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1532 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1533 | /* split the proxy ads:port if given */ |
| 1534 | |
| 1535 | p = getenv("http_proxy"); |
| 1536 | if (p) { |
| 1537 | strncpy(this->http_proxy_address, p, |
| 1538 | sizeof this->http_proxy_address - 1); |
| 1539 | this->http_proxy_address[ |
| 1540 | sizeof this->http_proxy_address - 1] = '\0'; |
| 1541 | |
| 1542 | p = strchr(this->http_proxy_address, ':'); |
| 1543 | if (p == NULL) { |
| 1544 | fprintf(stderr, "http_proxy needs to be ads:port\n"); |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | *p = '\0'; |
| 1548 | this->http_proxy_port = atoi(p + 1); |
| 1549 | |
| 1550 | fprintf(stderr, "Using proxy %s:%u\n", |
| 1551 | this->http_proxy_address, |
| 1552 | this->http_proxy_port); |
| 1553 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1554 | |
| 1555 | if (port) { |
| 1556 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1557 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1558 | this->use_ssl = ssl_cert_filepath != NULL && |
| 1559 | ssl_private_key_filepath != NULL; |
| 1560 | if (this->use_ssl) |
| 1561 | fprintf(stderr, " Compiled with SSL support, " |
| 1562 | "using it\n"); |
| 1563 | else |
| 1564 | fprintf(stderr, " Compiled with SSL support, " |
| 1565 | "not using it\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1566 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1567 | #else |
| 1568 | if (ssl_cert_filepath != NULL && |
| 1569 | ssl_private_key_filepath != NULL) { |
| 1570 | fprintf(stderr, " Not compiled for OpenSSl support!\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1571 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1572 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1573 | fprintf(stderr, " Compiled without SSL support, " |
| 1574 | "serving unencrypted\n"); |
| 1575 | #endif |
| 1576 | } |
| 1577 | |
| 1578 | /* ignore SIGPIPE */ |
| 1579 | |
| 1580 | signal(SIGPIPE, sigpipe_handler); |
| 1581 | |
| 1582 | |
| 1583 | #ifdef LWS_OPENSSL_SUPPORT |
| 1584 | |
| 1585 | /* basic openssl init */ |
| 1586 | |
| 1587 | SSL_library_init(); |
| 1588 | |
| 1589 | OpenSSL_add_all_algorithms(); |
| 1590 | SSL_load_error_strings(); |
| 1591 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame^] | 1592 | this->openssl_websocket_private_data_index = |
| 1593 | SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL); |
| 1594 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1595 | /* |
| 1596 | * Firefox insists on SSLv23 not SSLv3 |
| 1597 | * Konq disables SSLv2 by default now, SSLv23 works |
| 1598 | */ |
| 1599 | |
| 1600 | method = (SSL_METHOD *)SSLv23_server_method(); |
| 1601 | if (!method) { |
| 1602 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 1603 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1604 | return NULL; |
| 1605 | } |
| 1606 | this->ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 1607 | if (!this->ssl_ctx) { |
| 1608 | fprintf(stderr, "problem creating ssl context: %s\n", |
| 1609 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1610 | return NULL; |
| 1611 | } |
| 1612 | |
| 1613 | /* client context */ |
| 1614 | |
| 1615 | method = (SSL_METHOD *)SSLv23_client_method(); |
| 1616 | if (!method) { |
| 1617 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 1618 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | this->ssl_client_ctx = SSL_CTX_new(method); /* create context */ |
| 1622 | if (!this->ssl_client_ctx) { |
| 1623 | fprintf(stderr, "problem creating ssl context: %s\n", |
| 1624 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | /* openssl init for cert verification (used with client sockets) */ |
| 1630 | |
| 1631 | if (!SSL_CTX_load_verify_locations(this->ssl_client_ctx, NULL, |
| 1632 | LWS_OPENSSL_CLIENT_CERTS)) { |
| 1633 | fprintf(stderr, "Unable to load SSL Client certs from %s " |
| 1634 | "(set by --with-client-cert-dir= in configure) -- " |
| 1635 | " client ssl isn't going to work", |
| 1636 | LWS_OPENSSL_CLIENT_CERTS); |
| 1637 | } |
| 1638 | |
Andy Green | 0894bda | 2011-02-19 09:09:11 +0000 | [diff] [blame] | 1639 | /* |
| 1640 | * callback allowing user code to load extra verification certs |
| 1641 | * helping the client to verify server identity |
| 1642 | */ |
| 1643 | |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1644 | this->protocols[0].callback(this, NULL, |
Andy Green | 0894bda | 2011-02-19 09:09:11 +0000 | [diff] [blame] | 1645 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
| 1646 | this->ssl_client_ctx, NULL, 0); |
| 1647 | |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1648 | /* as a server, are we requiring clients to identify themselves? */ |
| 1649 | |
| 1650 | if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) { |
| 1651 | |
| 1652 | /* absolutely require the client cert */ |
| 1653 | |
| 1654 | SSL_CTX_set_verify(this->ssl_ctx, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame^] | 1655 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 1656 | OpenSSL_verify_callback); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1657 | |
| 1658 | /* |
| 1659 | * give user code a chance to load certs into the server |
| 1660 | * allowing it to verify incoming client certs |
| 1661 | */ |
| 1662 | |
| 1663 | this->protocols[0].callback(this, NULL, |
| 1664 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
| 1665 | this->ssl_ctx, NULL, 0); |
| 1666 | } |
| 1667 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1668 | if (this->use_ssl) { |
| 1669 | |
| 1670 | /* openssl init for server sockets */ |
| 1671 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1672 | /* set the local certificate from CertFile */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1673 | n = SSL_CTX_use_certificate_file(this->ssl_ctx, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1674 | ssl_cert_filepath, SSL_FILETYPE_PEM); |
| 1675 | if (n != 1) { |
| 1676 | fprintf(stderr, "problem getting cert '%s': %s\n", |
| 1677 | ssl_cert_filepath, |
| 1678 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1679 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1680 | } |
| 1681 | /* set the private key from KeyFile */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1682 | if (SSL_CTX_use_PrivateKey_file(this->ssl_ctx, |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 1683 | ssl_private_key_filepath, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1684 | SSL_FILETYPE_PEM) != 1) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 1685 | fprintf(stderr, "ssl problem getting key '%s': %s\n", |
| 1686 | ssl_private_key_filepath, |
| 1687 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1688 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1689 | } |
| 1690 | /* verify private key */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1691 | if (!SSL_CTX_check_private_key(this->ssl_ctx)) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 1692 | fprintf(stderr, "Private SSL key doesn't match cert\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1693 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | /* SSL is happy and has a cert it's content with */ |
| 1697 | } |
| 1698 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1699 | |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 1700 | /* selftest */ |
| 1701 | |
| 1702 | if (lws_b64_selftest()) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1703 | return NULL; |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 1704 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1705 | /* fd hashtable init */ |
| 1706 | |
| 1707 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) |
| 1708 | this->fd_hashtable[n].length = 0; |
| 1709 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1710 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1711 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1712 | if (port) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1713 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1714 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 1715 | if (sockfd < 0) { |
| 1716 | fprintf(stderr, "ERROR opening socket"); |
| 1717 | return NULL; |
| 1718 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 1719 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1720 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 1721 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1722 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1723 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 1724 | serv_addr.sin_family = AF_INET; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1725 | if (interface == NULL) |
| 1726 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 1727 | else |
| 1728 | interface_to_sa(interface, &serv_addr, |
| 1729 | sizeof(serv_addr)); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1730 | serv_addr.sin_port = htons(port); |
| 1731 | |
| 1732 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 1733 | sizeof(serv_addr)); |
| 1734 | if (n < 0) { |
| 1735 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1736 | port, n, errno); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1737 | return NULL; |
| 1738 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1739 | |
| 1740 | wsi = malloc(sizeof(struct libwebsocket)); |
| 1741 | memset(wsi, 0, sizeof (struct libwebsocket)); |
| 1742 | wsi->sock = sockfd; |
| 1743 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
| 1744 | insert_wsi(this, wsi); |
| 1745 | |
| 1746 | listen(sockfd, 5); |
| 1747 | fprintf(stderr, " Listening on port %d\n", port); |
| 1748 | |
| 1749 | /* list in the internal poll array */ |
| 1750 | |
| 1751 | this->fds[this->fds_count].fd = sockfd; |
| 1752 | this->fds[this->fds_count++].events = POLLIN; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1753 | |
| 1754 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1755 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1756 | LWS_CALLBACK_ADD_POLL_FD, |
| 1757 | (void *)(long)sockfd, NULL, POLLIN); |
| 1758 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1759 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1760 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1761 | /* drop any root privs for this process */ |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1762 | |
| 1763 | if (gid != -1) |
| 1764 | if (setgid(gid)) |
| 1765 | fprintf(stderr, "setgid: %s\n", strerror(errno)); |
| 1766 | if (uid != -1) |
| 1767 | if (setuid(uid)) |
| 1768 | fprintf(stderr, "setuid: %s\n", strerror(errno)); |
| 1769 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* set up our internal broadcast trigger sockets per-protocol */ |
| 1772 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1773 | for (this->count_protocols = 0; |
| 1774 | protocols[this->count_protocols].callback; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1775 | this->count_protocols++) { |
| 1776 | protocols[this->count_protocols].owning_server = this; |
| 1777 | protocols[this->count_protocols].protocol_index = |
| 1778 | this->count_protocols; |
| 1779 | |
| 1780 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 1781 | if (fd < 0) { |
| 1782 | fprintf(stderr, "ERROR opening socket"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1783 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1784 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1785 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1786 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 1787 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 1788 | |
| 1789 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 1790 | serv_addr.sin_family = AF_INET; |
| 1791 | serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 1792 | serv_addr.sin_port = 0; /* pick the port for us */ |
| 1793 | |
| 1794 | n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 1795 | if (n < 0) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1796 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1797 | port, n, errno); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1798 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | slen = sizeof cli_addr; |
| 1802 | n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen); |
| 1803 | if (n < 0) { |
| 1804 | fprintf(stderr, "getsockname failed\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1805 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1806 | } |
| 1807 | protocols[this->count_protocols].broadcast_socket_port = |
| 1808 | ntohs(cli_addr.sin_port); |
| 1809 | listen(fd, 5); |
| 1810 | |
| 1811 | debug(" Protocol %s broadcast socket %d\n", |
| 1812 | protocols[this->count_protocols].name, |
| 1813 | ntohs(cli_addr.sin_port)); |
| 1814 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1815 | /* dummy wsi per broadcast proxy socket */ |
| 1816 | |
| 1817 | wsi = malloc(sizeof(struct libwebsocket)); |
| 1818 | memset(wsi, 0, sizeof (struct libwebsocket)); |
| 1819 | wsi->sock = fd; |
| 1820 | wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER; |
| 1821 | /* note which protocol we are proxying */ |
| 1822 | wsi->protocol_index_for_broadcast_proxy = this->count_protocols; |
| 1823 | insert_wsi(this, wsi); |
| 1824 | |
| 1825 | /* list in internal poll array */ |
| 1826 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1827 | this->fds[this->fds_count].fd = fd; |
| 1828 | this->fds[this->fds_count].events = POLLIN; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1829 | this->fds[this->fds_count].revents = 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1830 | this->fds_count++; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1831 | |
| 1832 | /* external POLL support via protocol 0 */ |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1833 | this->protocols[0].callback(this, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1834 | LWS_CALLBACK_ADD_POLL_FD, |
| 1835 | (void *)(long)fd, NULL, POLLIN); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1838 | return this; |
| 1839 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1840 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1841 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1842 | #ifndef LWS_NO_FORK |
| 1843 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1844 | /** |
| 1845 | * libwebsockets_fork_service_loop() - Optional helper function forks off |
| 1846 | * a process for the websocket server loop. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1847 | * You don't have to use this but if not, you |
| 1848 | * have to make sure you are calling |
| 1849 | * libwebsocket_service periodically to service |
| 1850 | * the websocket traffic |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1851 | * @this: server context returned by creation function |
| 1852 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1853 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1854 | int |
| 1855 | libwebsockets_fork_service_loop(struct libwebsocket_context *this) |
| 1856 | { |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1857 | int fd; |
| 1858 | struct sockaddr_in cli_addr; |
| 1859 | int n; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1860 | int p; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1861 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1862 | n = fork(); |
| 1863 | if (n < 0) |
| 1864 | return n; |
| 1865 | |
| 1866 | if (!n) { |
| 1867 | |
| 1868 | /* main process context */ |
| 1869 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1870 | /* |
| 1871 | * set up the proxy sockets to allow broadcast from |
| 1872 | * service process context |
| 1873 | */ |
| 1874 | |
| 1875 | for (p = 0; p < this->count_protocols; p++) { |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1876 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 1877 | if (fd < 0) { |
| 1878 | fprintf(stderr, "Unable to create socket\n"); |
| 1879 | return -1; |
| 1880 | } |
| 1881 | cli_addr.sin_family = AF_INET; |
| 1882 | cli_addr.sin_port = htons( |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1883 | this->protocols[p].broadcast_socket_port); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1884 | cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 1885 | n = connect(fd, (struct sockaddr *)&cli_addr, |
| 1886 | sizeof cli_addr); |
| 1887 | if (n < 0) { |
| 1888 | fprintf(stderr, "Unable to connect to " |
| 1889 | "broadcast socket %d, %s\n", |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1890 | n, strerror(errno)); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1891 | return -1; |
| 1892 | } |
| 1893 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1894 | this->protocols[p].broadcast_socket_user_fd = fd; |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1897 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /* we want a SIGHUP when our parent goes down */ |
| 1901 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 1902 | |
| 1903 | /* in this forked process, sit and service websocket connections */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1904 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1905 | while (1) |
| 1906 | if (libwebsocket_service(this, 1000)) |
| 1907 | return -1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1908 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 1909 | return 0; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1912 | #endif |
| 1913 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1914 | /** |
| 1915 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1916 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1917 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 1918 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1919 | * |
| 1920 | * 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] | 1921 | * the callback. |
| 1922 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1923 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1924 | const struct libwebsocket_protocols * |
| 1925 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 1926 | { |
| 1927 | return wsi->protocol; |
| 1928 | } |
| 1929 | |
| 1930 | /** |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1931 | * libwebsockets_broadcast() - Sends a buffer to the callback for all active |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1932 | * connections of the given protocol. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1933 | * @protocol: pointer to the protocol you will broadcast to all members of |
| 1934 | * @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] | 1935 | * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before |
| 1936 | * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the |
| 1937 | * case you are calling this function from callback context. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1938 | * @len: length of payload data in buf, starting from buf. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1939 | * |
| 1940 | * This function allows bulk sending of a packet to every connection using |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1941 | * the given protocol. It does not send the data directly; instead it calls |
| 1942 | * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback |
| 1943 | * wants to actually send the data for that connection, the callback itself |
| 1944 | * should call libwebsocket_write(). |
| 1945 | * |
| 1946 | * libwebsockets_broadcast() can be called from another fork context without |
| 1947 | * having to take any care about data visibility between the processes, it'll |
| 1948 | * "just work". |
| 1949 | */ |
| 1950 | |
| 1951 | |
| 1952 | int |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1953 | libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1954 | unsigned char *buf, size_t len) |
| 1955 | { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1956 | struct libwebsocket_context *this = protocol->owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1957 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1958 | int m; |
| 1959 | struct libwebsocket * wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1960 | |
| 1961 | if (!protocol->broadcast_socket_user_fd) { |
| 1962 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1963 | * We are either running unforked / flat, or we are being |
| 1964 | * called from poll thread context |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1965 | * eg, from a callback. In that case don't use sockets for |
| 1966 | * broadcast IPC (since we can't open a socket connection to |
| 1967 | * a socket listening on our own thread) but directly do the |
| 1968 | * send action. |
| 1969 | * |
| 1970 | * Locking is not needed because we are by definition being |
| 1971 | * called in the poll thread context and are serialized. |
| 1972 | */ |
| 1973 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1974 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1975 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1976 | for (m = 0; m < this->fd_hashtable[n].length; m++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1977 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1978 | wsi = this->fd_hashtable[n].wsi[m]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1979 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1980 | if (wsi->mode != LWS_CONNMODE_WS_SERVING) |
| 1981 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1982 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1983 | /* |
| 1984 | * never broadcast to |
| 1985 | * non-established connections |
| 1986 | */ |
| 1987 | if (wsi->state != WSI_STATE_ESTABLISHED) |
| 1988 | continue; |
| 1989 | |
| 1990 | /* only broadcast to guys using |
| 1991 | * requested protocol |
| 1992 | */ |
| 1993 | if (wsi->protocol != protocol) |
| 1994 | continue; |
| 1995 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1996 | wsi->protocol->callback(this, wsi, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1997 | LWS_CALLBACK_BROADCAST, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1998 | wsi->user_space, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1999 | buf, len); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2000 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 2006 | /* |
| 2007 | * We're being called from a different process context than the server |
| 2008 | * loop. Instead of broadcasting directly, we send our |
| 2009 | * payload on a socket to do the IPC; the server process will serialize |
| 2010 | * the broadcast action in its main poll() loop. |
| 2011 | * |
| 2012 | * There's one broadcast socket listening for each protocol supported |
| 2013 | * set up when the websocket server initializes |
| 2014 | */ |
| 2015 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2016 | n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2017 | |
| 2018 | return n; |
| 2019 | } |