Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 1 | /* |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3 | * |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 22 | #include "private-libwebsockets.h" |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 23 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 24 | void |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 25 | libwebsocket_close_and_free_session(struct libwebsocket *wsi) |
| 26 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 27 | int n; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 28 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 29 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 30 | |
| 31 | if ((unsigned long)wsi < LWS_MAX_PROTOCOLS) |
| 32 | return; |
| 33 | |
| 34 | n = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 35 | |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 36 | if (n == WSI_STATE_DEAD_SOCKET) |
| 37 | return; |
| 38 | |
| 39 | /* |
| 40 | * signal we are closing, libsocket_write will |
| 41 | * add any necessary version-specific stuff. If the write fails, |
| 42 | * no worries we are closing anyway. If we didn't initiate this |
| 43 | * close, then our state has been changed to |
| 44 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we can skip this |
| 45 | */ |
| 46 | |
| 47 | if (n == WSI_STATE_ESTABLISHED) |
| 48 | libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 0, |
| 49 | LWS_WRITE_CLOSE); |
| 50 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 51 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 52 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 53 | if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED) |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 54 | wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED, |
| 55 | wsi->user_space, NULL, 0); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 56 | |
| 57 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 58 | if (wsi->utf8_token[n].token) |
| 59 | free(wsi->utf8_token[n].token); |
| 60 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 61 | /* fprintf(stderr, "closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 62 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 63 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 64 | if (wsi->ssl) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 65 | n = SSL_get_fd(wsi->ssl); |
| 66 | SSL_shutdown(wsi->ssl); |
| 67 | close(n); |
| 68 | SSL_free(wsi->ssl); |
| 69 | } else { |
| 70 | #endif |
| 71 | shutdown(wsi->sock, SHUT_RDWR); |
| 72 | close(wsi->sock); |
| 73 | #ifdef LWS_OPENSSL_SUPPORT |
| 74 | } |
| 75 | #endif |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 76 | if (wsi->user_space) |
| 77 | free(wsi->user_space); |
| 78 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 79 | free(wsi); |
| 80 | } |
| 81 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 82 | static int |
| 83 | libwebsocket_poll_connections(struct libwebsocket_context *this) |
| 84 | { |
| 85 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BROADCAST_PAYLOAD + |
| 86 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 87 | int client = this->count_protocols + 1; |
| 88 | struct libwebsocket *wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 89 | int n; |
| 90 | size_t len; |
| 91 | |
| 92 | /* check for activity on client sockets */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 93 | |
| 94 | for (; client < this->fds_count; client++) { |
| 95 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 96 | /* handle session socket closed */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 97 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 98 | if (this->fds[client].revents & (POLLERR | POLLHUP)) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 99 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 100 | debug("Session Socket %d %p (fd=%d) dead\n", |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 101 | client, (void *)this->wsi[client], |
| 102 | this->fds[client].fd); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 103 | |
| 104 | libwebsocket_close_and_free_session(this->wsi[client]); |
| 105 | goto nuke_this; |
| 106 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 107 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 108 | /* the guy requested a callback when it was OK to write */ |
| 109 | |
| 110 | if ((unsigned long)this->wsi[client] > LWS_MAX_PROTOCOLS && |
| 111 | this->fds[client].revents & POLLOUT) { |
| 112 | |
| 113 | this->fds[client].events &= ~POLLOUT; |
| 114 | |
| 115 | this->wsi[client]->protocol->callback(this->wsi[client], |
| 116 | LWS_CALLBACK_CLIENT_WRITEABLE, |
| 117 | this->wsi[client]->user_space, |
| 118 | NULL, 0); |
| 119 | } |
| 120 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 121 | /* any incoming data ready? */ |
| 122 | |
| 123 | if (!(this->fds[client].revents & POLLIN)) |
| 124 | continue; |
| 125 | |
| 126 | /* broadcast? */ |
| 127 | |
| 128 | if ((unsigned long)this->wsi[client] < LWS_MAX_PROTOCOLS) { |
| 129 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 130 | /* get the issued broadcast payload from the socket */ |
| 131 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 132 | len = read(this->fds[client].fd, |
| 133 | buf + LWS_SEND_BUFFER_PRE_PADDING, |
| 134 | MAX_BROADCAST_PAYLOAD); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 135 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 136 | if (len < 0) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 137 | fprintf(stderr, |
| 138 | "Error reading broadcast payload\n"); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 139 | continue; |
| 140 | } |
| 141 | |
| 142 | /* broadcast it to all guys with this protocol index */ |
| 143 | |
| 144 | for (n = this->count_protocols + 1; |
| 145 | n < this->fds_count; n++) { |
| 146 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 147 | wsi = this->wsi[n]; |
| 148 | |
| 149 | if ((unsigned long)wsi < LWS_MAX_PROTOCOLS) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 150 | continue; |
| 151 | |
| 152 | /* |
| 153 | * never broadcast to non-established |
| 154 | * connection |
| 155 | */ |
| 156 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 157 | if (wsi->state != WSI_STATE_ESTABLISHED) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 158 | continue; |
| 159 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 160 | /* only to clients connected to us */ |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 161 | |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 162 | if (wsi->mode != LWS_CONNMODE_WS_SERVING) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 163 | continue; |
| 164 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 165 | /* |
| 166 | * only broadcast to connections using |
| 167 | * the requested protocol |
| 168 | */ |
| 169 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 170 | if (wsi->protocol->protocol_index != |
| 171 | (int)(unsigned long)this->wsi[client]) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 172 | continue; |
| 173 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 174 | /* broadcast it to this connection */ |
| 175 | |
| 176 | wsi->protocol->callback(wsi, |
| 177 | LWS_CALLBACK_BROADCAST, |
| 178 | wsi->user_space, |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 179 | buf + LWS_SEND_BUFFER_PRE_PADDING, len); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 186 | if (this->wsi[client]->ssl) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 187 | n = SSL_read(this->wsi[client]->ssl, buf, sizeof buf); |
| 188 | else |
| 189 | #endif |
| 190 | n = recv(this->fds[client].fd, buf, sizeof buf, 0); |
| 191 | |
| 192 | if (n < 0) { |
| 193 | fprintf(stderr, "Socket read returned %d\n", n); |
| 194 | continue; |
| 195 | } |
| 196 | if (!n) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 197 | libwebsocket_close_and_free_session(this->wsi[client]); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 198 | goto nuke_this; |
| 199 | } |
| 200 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 201 | /* service incoming data */ |
| 202 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 203 | n = libwebsocket_read(this->wsi[client], buf, n); |
| 204 | if (n >= 0) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 205 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 206 | /* |
| 207 | * it closed and nuked wsi[client], so remove the |
| 208 | * socket handle and wsi from our service list |
| 209 | */ |
| 210 | nuke_this: |
| 211 | |
| 212 | debug("nuking wsi %p, fsd_count = %d\n", |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 213 | (void *)this->wsi[client], this->fds_count - 1); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 214 | |
| 215 | this->fds_count--; |
| 216 | for (n = client; n < this->fds_count; n++) { |
| 217 | this->fds[n] = this->fds[n + 1]; |
| 218 | this->wsi[n] = this->wsi[n + 1]; |
| 219 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 228 | /** |
| 229 | * libwebsocket_context_destroy() - Destroy the websocket context |
| 230 | * @this: Websocket context |
| 231 | * |
| 232 | * This function closes any active connections and then frees the |
| 233 | * context. After calling this, any further use of the context is |
| 234 | * undefined. |
| 235 | */ |
| 236 | void |
| 237 | libwebsocket_context_destroy(struct libwebsocket_context *this) |
| 238 | { |
| 239 | int client; |
| 240 | |
| 241 | /* close listening skt and per-protocol broadcast sockets */ |
Andy Green | 6a98054 | 2011-01-30 20:40:32 +0000 | [diff] [blame] | 242 | for (client = this->count_protocols + 1; client < this->fds_count; client++) |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 243 | switch (this->wsi[client]->mode) { |
| 244 | case LWS_CONNMODE_WS_SERVING: |
Andy Green | 6a98054 | 2011-01-30 20:40:32 +0000 | [diff] [blame] | 245 | libwebsocket_close_and_free_session(this->wsi[client]); |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 246 | break; |
| 247 | case LWS_CONNMODE_WS_CLIENT: |
| 248 | libwebsocket_client_close(this->wsi[client]); |
| 249 | break; |
| 250 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 251 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame^] | 252 | close(this->fd_random); |
| 253 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 254 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame^] | 255 | if (this->ssl_ctx) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 256 | SSL_CTX_free(this->ssl_ctx); |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame^] | 257 | if (this->ssl_client_ctx) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 258 | SSL_CTX_free(this->ssl_client_ctx); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 259 | #endif |
| 260 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame^] | 261 | free(this); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * libwebsocket_service() - Service any pending websocket activity |
| 266 | * @this: Websocket context |
| 267 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 268 | * service otherwise block and service immediately, returning |
| 269 | * after the timeout if nothing needed service. |
| 270 | * |
| 271 | * This function deals with any pending websocket traffic, for three |
| 272 | * kinds of event. It handles these events on both server and client |
| 273 | * types of connection the same. |
| 274 | * |
| 275 | * 1) Accept new connections to our context's server |
| 276 | * |
| 277 | * 2) Perform pending broadcast writes initiated from other forked |
| 278 | * processes (effectively serializing asynchronous broadcasts) |
| 279 | * |
| 280 | * 3) Call the receive callback for incoming frame data received by |
| 281 | * server or client connections. |
| 282 | * |
| 283 | * You need to call this service function periodically to all the above |
| 284 | * functions to happen; if your application is single-threaded you can |
| 285 | * just call it in your main event loop. |
| 286 | * |
| 287 | * Alternatively you can fork a new process that asynchronously handles |
| 288 | * calling this service in a loop. In that case you are happy if this |
| 289 | * call blocks your thread until it needs to take care of something and |
| 290 | * would call it with a large nonzero timeout. Your loop then takes no |
| 291 | * CPU while there is nothing happening. |
| 292 | * |
| 293 | * If you are calling it in a single-threaded app, you don't want it to |
| 294 | * wait around blocking other things in your loop from happening, so you |
| 295 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 296 | * nothing is pending, or as soon as it services whatever was pending. |
| 297 | */ |
| 298 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 299 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 300 | int |
| 301 | libwebsocket_service(struct libwebsocket_context *this, int timeout_ms) |
| 302 | { |
| 303 | int n; |
| 304 | int client; |
| 305 | unsigned int clilen; |
| 306 | struct sockaddr_in cli_addr; |
| 307 | int fd; |
| 308 | |
| 309 | /* stay dead once we are dead */ |
| 310 | |
| 311 | if (this == NULL) |
| 312 | return 1; |
| 313 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 314 | /* don't check listen socket if we are not listening */ |
| 315 | |
| 316 | if (this->listen_port) |
| 317 | n = poll(this->fds, this->fds_count, timeout_ms); |
| 318 | else |
| 319 | n = poll(&this->fds[1], this->fds_count - 1, timeout_ms); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 320 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 321 | |
| 322 | if (n < 0 || this->fds[0].revents & (POLLERR | POLLHUP)) { |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 323 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 324 | fprintf(stderr, "Listen Socket dead\n"); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 325 | */ |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 326 | goto fatal; |
| 327 | } |
| 328 | if (n == 0) /* poll timeout */ |
| 329 | return 0; |
| 330 | |
| 331 | /* handle accept on listening socket? */ |
| 332 | |
| 333 | for (client = 0; client < this->count_protocols + 1; client++) { |
| 334 | |
| 335 | if (!this->fds[client].revents & POLLIN) |
| 336 | continue; |
| 337 | |
| 338 | /* listen socket got an unencrypted connection... */ |
| 339 | |
| 340 | clilen = sizeof(cli_addr); |
| 341 | fd = accept(this->fds[client].fd, |
| 342 | (struct sockaddr *)&cli_addr, &clilen); |
| 343 | if (fd < 0) { |
| 344 | fprintf(stderr, "ERROR on accept"); |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | if (this->fds_count >= MAX_CLIENTS) { |
| 349 | fprintf(stderr, "too busy"); |
| 350 | close(fd); |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | if (client) { |
| 355 | /* |
| 356 | * accepting a connection to broadcast socket |
| 357 | * set wsi to be protocol index not pointer |
| 358 | */ |
| 359 | |
| 360 | this->wsi[this->fds_count] = |
| 361 | (struct libwebsocket *)(long)(client - 1); |
| 362 | |
| 363 | goto fill_in_fds; |
| 364 | } |
| 365 | |
| 366 | /* accepting connection to main listener */ |
| 367 | |
| 368 | this->wsi[this->fds_count] = |
| 369 | malloc(sizeof(struct libwebsocket)); |
| 370 | if (!this->wsi[this->fds_count]) { |
| 371 | fprintf(stderr, "Out of memory for new connection\n"); |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 376 | this->wsi[this->fds_count]->ssl = NULL; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 377 | this->ssl_ctx = NULL; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 378 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 379 | if (this->use_ssl) { |
| 380 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 381 | this->wsi[this->fds_count]->ssl = |
| 382 | SSL_new(this->ssl_ctx); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 383 | if (this->wsi[this->fds_count]->ssl == NULL) { |
| 384 | fprintf(stderr, "SSL_new failed: %s\n", |
| 385 | ERR_error_string(SSL_get_error( |
| 386 | this->wsi[this->fds_count]->ssl, 0), |
| 387 | NULL)); |
| 388 | free(this->wsi[this->fds_count]); |
| 389 | continue; |
| 390 | } |
| 391 | |
| 392 | SSL_set_fd(this->wsi[this->fds_count]->ssl, fd); |
| 393 | |
| 394 | n = SSL_accept(this->wsi[this->fds_count]->ssl); |
| 395 | if (n != 1) { |
| 396 | /* |
| 397 | * browsers seem to probe with various |
| 398 | * ssl params which fail then retry |
| 399 | * and succeed |
| 400 | */ |
| 401 | debug("SSL_accept failed skt %u: %s\n", |
| 402 | fd, |
| 403 | ERR_error_string(SSL_get_error( |
| 404 | this->wsi[this->fds_count]->ssl, |
| 405 | n), NULL)); |
| 406 | SSL_free( |
| 407 | this->wsi[this->fds_count]->ssl); |
| 408 | free(this->wsi[this->fds_count]); |
| 409 | continue; |
| 410 | } |
| 411 | debug("accepted new SSL conn " |
| 412 | "port %u on fd=%d SSL ver %s\n", |
| 413 | ntohs(cli_addr.sin_port), fd, |
| 414 | SSL_get_version(this->wsi[ |
| 415 | this->fds_count]->ssl)); |
| 416 | |
| 417 | } else |
| 418 | #endif |
| 419 | debug("accepted new conn port %u on fd=%d\n", |
| 420 | ntohs(cli_addr.sin_port), fd); |
| 421 | |
| 422 | /* intialize the instance struct */ |
| 423 | |
| 424 | this->wsi[this->fds_count]->sock = fd; |
| 425 | this->wsi[this->fds_count]->state = WSI_STATE_HTTP; |
| 426 | this->wsi[this->fds_count]->name_buffer_pos = 0; |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 427 | this->wsi[this->fds_count]->mode = LWS_CONNMODE_WS_SERVING; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 428 | |
| 429 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 430 | this->wsi[this->fds_count]-> |
| 431 | utf8_token[n].token = NULL; |
| 432 | this->wsi[this->fds_count]-> |
| 433 | utf8_token[n].token_len = 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * these can only be set once the protocol is known |
| 438 | * we set an unestablished connection's protocol pointer |
| 439 | * to the start of the supported list, so it can look |
| 440 | * for matching ones during the handshake |
| 441 | */ |
| 442 | this->wsi[this->fds_count]->protocol = this->protocols; |
| 443 | this->wsi[this->fds_count]->user_space = NULL; |
| 444 | |
| 445 | /* |
| 446 | * Default protocol is 76 / 00 |
| 447 | * After 76, there's a header specified to inform which |
| 448 | * draft the client wants, when that's seen we modify |
| 449 | * the individual connection's spec revision accordingly |
| 450 | */ |
| 451 | this->wsi[this->fds_count]->ietf_spec_revision = 0; |
| 452 | |
| 453 | fill_in_fds: |
| 454 | |
| 455 | /* |
| 456 | * make sure NO events are seen yet on this new socket |
| 457 | * (otherwise we inherit old fds[client].revents from |
| 458 | * previous socket there and die mysteriously! ) |
| 459 | */ |
| 460 | this->fds[this->fds_count].revents = 0; |
| 461 | |
| 462 | this->fds[this->fds_count].events = POLLIN; |
| 463 | this->fds[this->fds_count++].fd = fd; |
| 464 | |
| 465 | } |
| 466 | |
| 467 | /* service anything incoming on websocket connection */ |
| 468 | |
| 469 | libwebsocket_poll_connections(this); |
| 470 | |
| 471 | /* this round is done */ |
| 472 | |
| 473 | return 0; |
| 474 | |
| 475 | fatal: |
| 476 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 477 | /* inform caller we are dead */ |
| 478 | |
| 479 | return 1; |
| 480 | } |
| 481 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 482 | /** |
| 483 | * libwebsocket_callback_on_writable() - Request a callback when this socket |
| 484 | * becomes able to be written to without |
| 485 | * blocking |
| 486 | * |
| 487 | * @wsi: Websocket connection instance to get callback for |
| 488 | */ |
| 489 | |
| 490 | int |
| 491 | libwebsocket_callback_on_writable(struct libwebsocket *wsi) |
| 492 | { |
| 493 | struct libwebsocket_context *this = wsi->protocol->owning_server; |
| 494 | int n; |
| 495 | |
| 496 | for (n = this->count_protocols + 1; n < this->fds_count; n++) |
| 497 | if (this->wsi[n] == wsi) { |
| 498 | this->fds[n].events |= POLLOUT; |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | fprintf(stderr, "libwebsocket_callback_on_writable " |
| 503 | "didn't find socket\n"); |
| 504 | return 1; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * libwebsocket_callback_on_writable_all_protocol() - Request a callback for |
| 509 | * all connections using the given protocol when it |
| 510 | * becomes possible to write to each socket without |
| 511 | * blocking in turn. |
| 512 | * |
| 513 | * @protocol: Protocol whose connections will get callbacks |
| 514 | */ |
| 515 | |
| 516 | int |
| 517 | libwebsocket_callback_on_writable_all_protocol( |
| 518 | const struct libwebsocket_protocols *protocol) |
| 519 | { |
| 520 | struct libwebsocket_context *this = protocol->owning_server; |
| 521 | int n; |
| 522 | |
| 523 | for (n = this->count_protocols + 1; n < this->fds_count; n++) |
| 524 | if ((unsigned long)this->wsi[n] > LWS_MAX_PROTOCOLS) |
| 525 | if (this->wsi[n]->protocol == protocol) |
| 526 | this->fds[n].events |= POLLOUT; |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 531 | |
| 532 | /** |
| 533 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 534 | * |
| 535 | * You will not need this unless you are doing something special |
| 536 | * |
| 537 | * @wsi: Websocket connection instance |
| 538 | */ |
| 539 | |
| 540 | int |
| 541 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 542 | { |
| 543 | return wsi->sock; |
| 544 | } |
| 545 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 546 | /** |
| 547 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
| 548 | * receieved packets. |
| 549 | * |
| 550 | * If the output side of a server process becomes choked, this allows flow |
| 551 | * control for the input side. |
| 552 | * |
| 553 | * @wsi: Websocket connection instance to get callback for |
| 554 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 555 | */ |
| 556 | |
| 557 | int |
| 558 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 559 | { |
| 560 | struct libwebsocket_context *this = wsi->protocol->owning_server; |
| 561 | int n; |
| 562 | |
| 563 | for (n = this->count_protocols + 1; n < this->fds_count; n++) |
| 564 | if (this->wsi[n] == wsi) { |
| 565 | if (enable) |
| 566 | this->fds[n].events |= POLLIN; |
| 567 | else |
| 568 | this->fds[n].events &= ~POLLIN; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | fprintf(stderr, "libwebsocket_callback_on_writable " |
| 574 | "unable to find socket\n"); |
| 575 | return 1; |
| 576 | } |
| 577 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 578 | /** |
| 579 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 580 | * |
| 581 | * This is typically used by client code to fill in the host parameter |
| 582 | * when making a client connection. You can only call it after the context |
| 583 | * has been created. |
| 584 | * |
| 585 | * @this: Websocket context |
| 586 | */ |
| 587 | |
| 588 | |
| 589 | extern const char * |
| 590 | libwebsocket_canonical_hostname(struct libwebsocket_context *this) |
| 591 | { |
| 592 | return (const char *)this->canonical_hostname; |
| 593 | } |
| 594 | |
| 595 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 596 | static void sigpipe_handler(int x) |
| 597 | { |
| 598 | } |
| 599 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 600 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 601 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 602 | * libwebsocket_create_context() - Create the websocket handler |
| 603 | * @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] | 604 | * any port, that's what you want if you are not running a |
| 605 | * websocket server at all but just using it as a client |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 606 | * @protocols: Array of structures listing supported protocols and a protocol- |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 607 | * specific callback for each one. The list is ended with an |
| 608 | * entry that has a NULL callback pointer. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 609 | * It's not const because we write the owning_server member |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 610 | * @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] | 611 | * to listen using SSL, set to the filepath to fetch the |
| 612 | * server cert from, otherwise NULL for unencrypted |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 613 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 614 | * else ignored |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 615 | * @gid: group id to change to after setting listen socket, or -1. |
| 616 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 617 | * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 618 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 619 | * This function creates the listening socket and takes care |
| 620 | * of all initialization in one step. |
| 621 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 622 | * After initialization, it returns a struct libwebsocket_context * that |
| 623 | * represents this server. After calling, user code needs to take care |
| 624 | * of calling libwebsocket_service() with the context pointer to get the |
| 625 | * server's sockets serviced. This can be done in the same process context |
| 626 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 627 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 628 | * The protocol callback functions are called for a handful of events |
| 629 | * including http requests coming in, websocket connections becoming |
| 630 | * established, and data arriving; it's also called periodically to allow |
| 631 | * async transmission. |
| 632 | * |
| 633 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 634 | * at that time websocket protocol has not been negotiated. Other |
| 635 | * protocols after the first one never see any HTTP callack activity. |
| 636 | * |
| 637 | * The server created is a simple http server by default; part of the |
| 638 | * websocket standard is upgrading this http connection to a websocket one. |
| 639 | * |
| 640 | * This allows the same server to provide files like scripts and favicon / |
| 641 | * images or whatever over http and dynamic data over websockets all in |
| 642 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 643 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 644 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 645 | struct libwebsocket_context * |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 646 | libwebsocket_create_context(int port, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 647 | struct libwebsocket_protocols *protocols, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 648 | const char *ssl_cert_filepath, |
| 649 | const char *ssl_private_key_filepath, |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 650 | int gid, int uid, unsigned int options) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 651 | { |
| 652 | int n; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 653 | int sockfd = 0; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 654 | int fd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 655 | struct sockaddr_in serv_addr, cli_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 656 | int opt = 1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 657 | struct libwebsocket_context *this = NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 658 | unsigned int slen; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 659 | char *p; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 660 | char hostname[1024]; |
Andy Green | 42f6914 | 2011-01-30 08:10:02 +0000 | [diff] [blame] | 661 | struct hostent *he; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 662 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 663 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 664 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 665 | char ssl_err_buf[512]; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 666 | #endif |
| 667 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 668 | this = malloc(sizeof(struct libwebsocket_context)); |
| 669 | if (!this) { |
| 670 | fprintf(stderr, "No memory for websocket context\n"); |
| 671 | return NULL; |
| 672 | } |
| 673 | this->protocols = protocols; |
| 674 | this->listen_port = port; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 675 | this->http_proxy_port = 0; |
| 676 | this->http_proxy_address[0] = '\0'; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 677 | this->options = options; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 678 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame^] | 679 | this->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); |
| 680 | if (this->fd_random < 0) { |
| 681 | fprintf(stderr, "Unable to open random device %s %d\n", |
| 682 | SYSTEM_RANDOM_FILEPATH, this->fd_random); |
| 683 | return NULL; |
| 684 | } |
| 685 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 686 | /* find canonical hostname */ |
| 687 | |
| 688 | hostname[(sizeof hostname) - 1] = '\0'; |
| 689 | gethostname(hostname, (sizeof hostname) - 1); |
| 690 | he = gethostbyname(hostname); |
| 691 | strncpy(this->canonical_hostname, he->h_name, |
| 692 | sizeof this->canonical_hostname - 1); |
| 693 | this->canonical_hostname[sizeof this->canonical_hostname - 1] = '\0'; |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 694 | fprintf(stderr, " canonical hostname = '%s'\n", |
| 695 | this->canonical_hostname); |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 696 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 697 | /* split the proxy ads:port if given */ |
| 698 | |
| 699 | p = getenv("http_proxy"); |
| 700 | if (p) { |
| 701 | strncpy(this->http_proxy_address, p, |
| 702 | sizeof this->http_proxy_address - 1); |
| 703 | this->http_proxy_address[ |
| 704 | sizeof this->http_proxy_address - 1] = '\0'; |
| 705 | |
| 706 | p = strchr(this->http_proxy_address, ':'); |
| 707 | if (p == NULL) { |
| 708 | fprintf(stderr, "http_proxy needs to be ads:port\n"); |
| 709 | return NULL; |
| 710 | } |
| 711 | *p = '\0'; |
| 712 | this->http_proxy_port = atoi(p + 1); |
| 713 | |
| 714 | fprintf(stderr, "Using proxy %s:%u\n", |
| 715 | this->http_proxy_address, |
| 716 | this->http_proxy_port); |
| 717 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 718 | |
| 719 | if (port) { |
| 720 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 721 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 722 | this->use_ssl = ssl_cert_filepath != NULL && |
| 723 | ssl_private_key_filepath != NULL; |
| 724 | if (this->use_ssl) |
| 725 | fprintf(stderr, " Compiled with SSL support, " |
| 726 | "using it\n"); |
| 727 | else |
| 728 | fprintf(stderr, " Compiled with SSL support, " |
| 729 | "not using it\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 730 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 731 | #else |
| 732 | if (ssl_cert_filepath != NULL && |
| 733 | ssl_private_key_filepath != NULL) { |
| 734 | fprintf(stderr, " Not compiled for OpenSSl support!\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 735 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 736 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 737 | fprintf(stderr, " Compiled without SSL support, " |
| 738 | "serving unencrypted\n"); |
| 739 | #endif |
| 740 | } |
| 741 | |
| 742 | /* ignore SIGPIPE */ |
| 743 | |
| 744 | signal(SIGPIPE, sigpipe_handler); |
| 745 | |
| 746 | |
| 747 | #ifdef LWS_OPENSSL_SUPPORT |
| 748 | |
| 749 | /* basic openssl init */ |
| 750 | |
| 751 | SSL_library_init(); |
| 752 | |
| 753 | OpenSSL_add_all_algorithms(); |
| 754 | SSL_load_error_strings(); |
| 755 | |
| 756 | /* |
| 757 | * Firefox insists on SSLv23 not SSLv3 |
| 758 | * Konq disables SSLv2 by default now, SSLv23 works |
| 759 | */ |
| 760 | |
| 761 | method = (SSL_METHOD *)SSLv23_server_method(); |
| 762 | if (!method) { |
| 763 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 764 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 765 | return NULL; |
| 766 | } |
| 767 | this->ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 768 | if (!this->ssl_ctx) { |
| 769 | fprintf(stderr, "problem creating ssl context: %s\n", |
| 770 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 771 | return NULL; |
| 772 | } |
| 773 | |
| 774 | /* client context */ |
| 775 | |
| 776 | method = (SSL_METHOD *)SSLv23_client_method(); |
| 777 | if (!method) { |
| 778 | fprintf(stderr, "problem creating ssl method: %s\n", |
| 779 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 780 | return NULL; |
| 781 | } |
| 782 | this->ssl_client_ctx = SSL_CTX_new(method); /* create context */ |
| 783 | if (!this->ssl_client_ctx) { |
| 784 | fprintf(stderr, "problem creating ssl context: %s\n", |
| 785 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 786 | return NULL; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /* openssl init for cert verification (used with client sockets) */ |
| 791 | |
| 792 | if (!SSL_CTX_load_verify_locations(this->ssl_client_ctx, NULL, |
| 793 | LWS_OPENSSL_CLIENT_CERTS)) { |
| 794 | fprintf(stderr, "Unable to load SSL Client certs from %s " |
| 795 | "(set by --with-client-cert-dir= in configure) -- " |
| 796 | " client ssl isn't going to work", |
| 797 | LWS_OPENSSL_CLIENT_CERTS); |
| 798 | } |
| 799 | |
| 800 | if (this->use_ssl) { |
| 801 | |
| 802 | /* openssl init for server sockets */ |
| 803 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 804 | /* set the local certificate from CertFile */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 805 | n = SSL_CTX_use_certificate_file(this->ssl_ctx, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 806 | ssl_cert_filepath, SSL_FILETYPE_PEM); |
| 807 | if (n != 1) { |
| 808 | fprintf(stderr, "problem getting cert '%s': %s\n", |
| 809 | ssl_cert_filepath, |
| 810 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 811 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 812 | } |
| 813 | /* set the private key from KeyFile */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 814 | if (SSL_CTX_use_PrivateKey_file(this->ssl_ctx, |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 815 | ssl_private_key_filepath, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 816 | SSL_FILETYPE_PEM) != 1) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 817 | fprintf(stderr, "ssl problem getting key '%s': %s\n", |
| 818 | ssl_private_key_filepath, |
| 819 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 820 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 821 | } |
| 822 | /* verify private key */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 823 | if (!SSL_CTX_check_private_key(this->ssl_ctx)) { |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 824 | fprintf(stderr, "Private SSL key doesn't match cert\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 825 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /* SSL is happy and has a cert it's content with */ |
| 829 | } |
| 830 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 831 | |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 832 | /* selftest */ |
| 833 | |
| 834 | if (lws_b64_selftest()) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 835 | return NULL; |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 836 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 837 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 838 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 839 | if (port) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 840 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 841 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 842 | if (sockfd < 0) { |
| 843 | fprintf(stderr, "ERROR opening socket"); |
| 844 | return NULL; |
| 845 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 846 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 847 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 848 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 849 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 850 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 851 | serv_addr.sin_family = AF_INET; |
| 852 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 853 | serv_addr.sin_port = htons(port); |
| 854 | |
| 855 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 856 | sizeof(serv_addr)); |
| 857 | if (n < 0) { |
| 858 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 859 | port, n, errno); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 860 | return NULL; |
| 861 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 862 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 863 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 864 | /* drop any root privs for this process */ |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 865 | |
| 866 | if (gid != -1) |
| 867 | if (setgid(gid)) |
| 868 | fprintf(stderr, "setgid: %s\n", strerror(errno)); |
| 869 | if (uid != -1) |
| 870 | if (setuid(uid)) |
| 871 | fprintf(stderr, "setuid: %s\n", strerror(errno)); |
| 872 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 873 | /* |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 874 | * prepare the poll() fd array... it's like this |
| 875 | * |
| 876 | * [0] = external listening socket |
| 877 | * [1 .. this->count_protocols] = per-protocol broadcast sockets |
| 878 | * [this->count_protocols + 1 ... this->fds_count-1] = connection skts |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 879 | */ |
| 880 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 881 | this->fds_count = 1; |
| 882 | this->fds[0].fd = sockfd; |
| 883 | this->fds[0].events = POLLIN; |
| 884 | this->count_protocols = 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 885 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 886 | if (port) { |
| 887 | listen(sockfd, 5); |
| 888 | fprintf(stderr, " Listening on port %d\n", port); |
| 889 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 890 | |
| 891 | /* set up our internal broadcast trigger sockets per-protocol */ |
| 892 | |
| 893 | for (; protocols[this->count_protocols].callback; |
| 894 | this->count_protocols++) { |
| 895 | protocols[this->count_protocols].owning_server = this; |
| 896 | protocols[this->count_protocols].protocol_index = |
| 897 | this->count_protocols; |
| 898 | |
| 899 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 900 | if (fd < 0) { |
| 901 | fprintf(stderr, "ERROR opening socket"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 902 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 903 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 904 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 905 | /* allow us to restart even if old sockets in TIME_WAIT */ |
| 906 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 907 | |
| 908 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 909 | serv_addr.sin_family = AF_INET; |
| 910 | serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 911 | serv_addr.sin_port = 0; /* pick the port for us */ |
| 912 | |
| 913 | n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 914 | if (n < 0) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 915 | fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 916 | port, n, errno); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 917 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | slen = sizeof cli_addr; |
| 921 | n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen); |
| 922 | if (n < 0) { |
| 923 | fprintf(stderr, "getsockname failed\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 924 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 925 | } |
| 926 | protocols[this->count_protocols].broadcast_socket_port = |
| 927 | ntohs(cli_addr.sin_port); |
| 928 | listen(fd, 5); |
| 929 | |
| 930 | debug(" Protocol %s broadcast socket %d\n", |
| 931 | protocols[this->count_protocols].name, |
| 932 | ntohs(cli_addr.sin_port)); |
| 933 | |
| 934 | this->fds[this->fds_count].fd = fd; |
| 935 | this->fds[this->fds_count].events = POLLIN; |
| 936 | /* wsi only exists for connections, not broadcast listener */ |
| 937 | this->wsi[this->fds_count] = NULL; |
| 938 | this->fds_count++; |
| 939 | } |
| 940 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 941 | return this; |
| 942 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 943 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 944 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 945 | #ifndef LWS_NO_FORK |
| 946 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 947 | /** |
| 948 | * libwebsockets_fork_service_loop() - Optional helper function forks off |
| 949 | * a process for the websocket server loop. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 950 | * You don't have to use this but if not, you |
| 951 | * have to make sure you are calling |
| 952 | * libwebsocket_service periodically to service |
| 953 | * the websocket traffic |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 954 | * @this: server context returned by creation function |
| 955 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 956 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 957 | int |
| 958 | libwebsockets_fork_service_loop(struct libwebsocket_context *this) |
| 959 | { |
| 960 | int client; |
| 961 | int fd; |
| 962 | struct sockaddr_in cli_addr; |
| 963 | int n; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 964 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 965 | n = fork(); |
| 966 | if (n < 0) |
| 967 | return n; |
| 968 | |
| 969 | if (!n) { |
| 970 | |
| 971 | /* main process context */ |
| 972 | |
| 973 | for (client = 1; client < this->count_protocols + 1; client++) { |
| 974 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 975 | if (fd < 0) { |
| 976 | fprintf(stderr, "Unable to create socket\n"); |
| 977 | return -1; |
| 978 | } |
| 979 | cli_addr.sin_family = AF_INET; |
| 980 | cli_addr.sin_port = htons( |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 981 | this->protocols[client - 1].broadcast_socket_port); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 982 | cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 983 | n = connect(fd, (struct sockaddr *)&cli_addr, |
| 984 | sizeof cli_addr); |
| 985 | if (n < 0) { |
| 986 | fprintf(stderr, "Unable to connect to " |
| 987 | "broadcast socket %d, %s\n", |
| 988 | client, strerror(errno)); |
| 989 | return -1; |
| 990 | } |
| 991 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 992 | this->protocols[client - 1].broadcast_socket_user_fd = |
| 993 | fd; |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 997 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | /* we want a SIGHUP when our parent goes down */ |
| 1001 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 1002 | |
| 1003 | /* in this forked process, sit and service websocket connections */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1004 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1005 | while (1) |
| 1006 | if (libwebsocket_service(this, 1000)) |
| 1007 | return -1; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1008 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 1009 | return 0; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1012 | #endif |
| 1013 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1014 | /** |
| 1015 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1016 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1017 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 1018 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1019 | * |
| 1020 | * 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] | 1021 | * the callback. |
| 1022 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1023 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1024 | const struct libwebsocket_protocols * |
| 1025 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 1026 | { |
| 1027 | return wsi->protocol; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1031 | * libwebsockets_broadcast() - Sends a buffer to the callback for all active |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1032 | * connections of the given protocol. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1033 | * @protocol: pointer to the protocol you will broadcast to all members of |
| 1034 | * @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] | 1035 | * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before |
| 1036 | * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the |
| 1037 | * case you are calling this function from callback context. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1038 | * @len: length of payload data in buf, starting from buf. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1039 | * |
| 1040 | * This function allows bulk sending of a packet to every connection using |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1041 | * the given protocol. It does not send the data directly; instead it calls |
| 1042 | * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback |
| 1043 | * wants to actually send the data for that connection, the callback itself |
| 1044 | * should call libwebsocket_write(). |
| 1045 | * |
| 1046 | * libwebsockets_broadcast() can be called from another fork context without |
| 1047 | * having to take any care about data visibility between the processes, it'll |
| 1048 | * "just work". |
| 1049 | */ |
| 1050 | |
| 1051 | |
| 1052 | int |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1053 | libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1054 | unsigned char *buf, size_t len) |
| 1055 | { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1056 | struct libwebsocket_context *this = protocol->owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1057 | int n; |
| 1058 | |
| 1059 | if (!protocol->broadcast_socket_user_fd) { |
| 1060 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1061 | * We are either running unforked / flat, or we are being |
| 1062 | * called from poll thread context |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1063 | * eg, from a callback. In that case don't use sockets for |
| 1064 | * broadcast IPC (since we can't open a socket connection to |
| 1065 | * a socket listening on our own thread) but directly do the |
| 1066 | * send action. |
| 1067 | * |
| 1068 | * Locking is not needed because we are by definition being |
| 1069 | * called in the poll thread context and are serialized. |
| 1070 | */ |
| 1071 | |
| 1072 | for (n = this->count_protocols + 1; n < this->fds_count; n++) { |
| 1073 | |
| 1074 | if ((unsigned long)this->wsi[n] < LWS_MAX_PROTOCOLS) |
| 1075 | continue; |
| 1076 | |
| 1077 | /* never broadcast to non-established connection */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1078 | if (this->wsi[n]->state != WSI_STATE_ESTABLISHED) |
| 1079 | continue; |
| 1080 | |
| 1081 | /* only broadcast to guys using requested protocol */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1082 | if (this->wsi[n]->protocol != protocol) |
| 1083 | continue; |
| 1084 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1085 | this->wsi[n]->protocol->callback(this->wsi[n], |
| 1086 | LWS_CALLBACK_BROADCAST, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1087 | this->wsi[n]->user_space, |
| 1088 | buf, len); |
| 1089 | } |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 1094 | /* |
| 1095 | * We're being called from a different process context than the server |
| 1096 | * loop. Instead of broadcasting directly, we send our |
| 1097 | * payload on a socket to do the IPC; the server process will serialize |
| 1098 | * the broadcast action in its main poll() loop. |
| 1099 | * |
| 1100 | * There's one broadcast socket listening for each protocol supported |
| 1101 | * set up when the websocket server initializes |
| 1102 | */ |
| 1103 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1104 | n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1105 | |
| 1106 | return n; |
| 1107 | } |