Andy Green | 58eaa74 | 2011-03-07 17:54:06 +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 | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 23 | #include <syslog.h> |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 24 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 25 | #ifdef WIN32 |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 26 | #include <tchar.h> |
| 27 | #include <io.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 28 | #else |
David | c4ef7b1 | 2013-01-12 20:39:47 +0800 | [diff] [blame] | 29 | #ifdef LWS_BUILTIN_GETIFADDRS |
| 30 | #include <getifaddrs.h> |
| 31 | #else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 32 | #include <ifaddrs.h> |
David | c4ef7b1 | 2013-01-12 20:39:47 +0800 | [diff] [blame] | 33 | #endif |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 34 | #include <sys/un.h> |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 35 | #include <sys/socket.h> |
| 36 | #include <netdb.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 37 | #endif |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 38 | |
| 39 | #ifdef LWS_OPENSSL_SUPPORT |
| 40 | int openssl_websocket_private_data_index; |
| 41 | #endif |
| 42 | |
Andy Green | aa6fc44 | 2012-04-12 13:26:49 +0800 | [diff] [blame] | 43 | #ifdef __MINGW32__ |
| 44 | #include "../win32port/win32helpers/websock-w32.c" |
| 45 | #else |
| 46 | #ifdef __MINGW64__ |
| 47 | #include "../win32port/win32helpers/websock-w32.c" |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 51 | |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 52 | static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE; |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 53 | static void lwsl_emit_stderr(int level, const char *line); |
| 54 | static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr; |
| 55 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 56 | static const char *log_level_names[] = { |
| 57 | "ERR", |
| 58 | "WARN", |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 59 | "NOTICE", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 60 | "INFO", |
| 61 | "DEBUG", |
| 62 | "PARSER", |
| 63 | "HEADER", |
| 64 | "EXTENSION", |
| 65 | "CLIENT", |
| 66 | }; |
| 67 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 68 | int |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 69 | insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 70 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 71 | if (context->fds_count >= context->max_fds) { |
| 72 | lwsl_err("Reached limit of fds tracking (%d)\n", context->max_fds); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 76 | if (wsi->sock > context->max_fds) { |
| 77 | lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | assert(wsi); |
| 82 | assert(wsi->sock); |
| 83 | |
| 84 | lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count); |
| 85 | |
| 86 | context->lws_lookup[wsi->sock] = wsi; |
| 87 | wsi->position_in_fds_table = context->fds_count; |
| 88 | context->fds[context->fds_count].fd = wsi->sock; |
| 89 | context->fds[context->fds_count].events = POLLIN; |
| 90 | context->fds[context->fds_count++].revents = 0; |
| 91 | |
| 92 | /* external POLL support via protocol 0 */ |
| 93 | context->protocols[0].callback(context, wsi, |
| 94 | LWS_CALLBACK_ADD_POLL_FD, |
| 95 | (void *)(long)wsi->sock, NULL, POLLIN); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 100 | static int |
| 101 | remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 102 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 103 | int m; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 104 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 105 | if (!--context->fds_count) |
| 106 | goto do_ext; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 107 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 108 | if (wsi->sock > context->max_fds) { |
| 109 | lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds); |
| 110 | return 1; |
| 111 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 112 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 113 | lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, wsi->position_in_fds_table); |
| 114 | |
| 115 | m = wsi->position_in_fds_table; /* replace the contents for this */ |
| 116 | |
| 117 | /* have the last guy take up the vacant slot */ |
| 118 | context->fds[m] = context->fds[context->fds_count]; /* vacant fds slot filled with end one */ |
| 119 | /* end guy's fds_lookup entry remains unchanged (still same fd pointing to same wsi) */ |
| 120 | /* end guy's "position in fds table" changed */ |
| 121 | context->lws_lookup[context->fds[context->fds_count].fd]->position_in_fds_table = m; |
| 122 | /* deletion guy's lws_lookup entry needs nuking */ |
| 123 | context->lws_lookup[wsi->sock] = NULL; /* no WSI for the socket of the wsi being removed*/ |
| 124 | wsi->position_in_fds_table = -1; /* removed wsi has no position any more */ |
| 125 | |
| 126 | do_ext: |
| 127 | /* remove also from external POLL support via protocol 0 */ |
| 128 | if (wsi->sock) |
| 129 | context->protocols[0].callback(context, wsi, |
| 130 | LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0); |
| 131 | |
| 132 | return 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 135 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 136 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 137 | libwebsocket_close_and_free_session(struct libwebsocket_context *context, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 138 | struct libwebsocket *wsi, enum lws_close_status reason) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 139 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 140 | int n; |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 141 | int old_state; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 142 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 143 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 144 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 145 | int ret; |
| 146 | int m; |
| 147 | struct lws_tokens eff_buf; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 148 | struct libwebsocket_extension *ext; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 149 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 150 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 151 | if (!wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 152 | return; |
| 153 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 154 | old_state = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 155 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 156 | if (old_state == WSI_STATE_DEAD_SOCKET) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 157 | return; |
| 158 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 159 | wsi->u.ws.close_reason = reason; |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 160 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 161 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 162 | /* |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 163 | * are his extensions okay with him closing? Eg he might be a mux |
| 164 | * parent and just his ch1 aspect is closing? |
| 165 | */ |
| 166 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 167 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 168 | if (!wsi->active_extensions[n]->callback) |
| 169 | continue; |
| 170 | |
| 171 | m = wsi->active_extensions[n]->callback(context, |
| 172 | wsi->active_extensions[n], wsi, |
| 173 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, |
| 174 | wsi->active_extensions_user[n], NULL, 0); |
| 175 | |
| 176 | /* |
| 177 | * if somebody vetoed actually closing him at this time.... |
| 178 | * up to the extension to track the attempted close, let's |
| 179 | * just bail |
| 180 | */ |
| 181 | |
| 182 | if (m) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 183 | lwsl_ext("extension vetoed close\n"); |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 188 | /* |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 189 | * flush any tx pending from extensions, since we may send close packet |
| 190 | * if there are problems with send, just nuke the connection |
| 191 | */ |
| 192 | |
| 193 | ret = 1; |
| 194 | while (ret == 1) { |
| 195 | |
| 196 | /* default to nobody has more to spill */ |
| 197 | |
| 198 | ret = 0; |
| 199 | eff_buf.token = NULL; |
| 200 | eff_buf.token_len = 0; |
| 201 | |
| 202 | /* show every extension the new incoming data */ |
| 203 | |
| 204 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 205 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 206 | wsi->protocol->owning_server, |
| 207 | wsi->active_extensions[n], wsi, |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 208 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, |
| 209 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 210 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 211 | lwsl_ext("Extension reports fatal error\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 212 | goto just_kill_connection; |
| 213 | } |
| 214 | if (m) |
| 215 | /* |
| 216 | * at least one extension told us he has more |
| 217 | * to spill, so we will go around again after |
| 218 | */ |
| 219 | ret = 1; |
| 220 | } |
| 221 | |
| 222 | /* assuming they left us something to send, send it */ |
| 223 | |
| 224 | if (eff_buf.token_len) |
| 225 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 226 | eff_buf.token_len)) { |
| 227 | lwsl_debug("close: sending final extension spill had problems\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 228 | goto just_kill_connection; |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 229 | } |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 230 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 231 | #endif |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 232 | |
| 233 | /* |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 234 | * signal we are closing, libsocket_write will |
| 235 | * add any necessary version-specific stuff. If the write fails, |
| 236 | * no worries we are closing anyway. If we didn't initiate this |
| 237 | * close, then our state has been changed to |
| 238 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this. |
| 239 | * |
| 240 | * Likewise if it's a second call to close this connection after we |
| 241 | * sent the close indication to the peer already, we are in state |
| 242 | * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time. |
| 243 | */ |
| 244 | |
| 245 | if (old_state == WSI_STATE_ESTABLISHED && |
| 246 | reason != LWS_CLOSE_STATUS_NOSTATUS) { |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 247 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 248 | lwsl_debug("sending close indication...\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 249 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 250 | n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], |
| 251 | 0, LWS_WRITE_CLOSE); |
| 252 | if (!n) { |
| 253 | /* |
| 254 | * we have sent a nice protocol level indication we |
| 255 | * now wish to close, we should not send anything more |
| 256 | */ |
| 257 | |
| 258 | wsi->state = WSI_STATE_AWAITING_CLOSE_ACK; |
| 259 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 260 | /* and we should wait for a reply for a bit out of politeness */ |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 261 | |
| 262 | libwebsocket_set_timeout(wsi, |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 263 | PENDING_TIMEOUT_CLOSE_ACK, 1); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 264 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 265 | lwsl_debug("sent close indication, awaiting ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 266 | |
| 267 | return; |
| 268 | } |
| 269 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 270 | lwsl_info("close: sending the close packet failed, hanging up\n"); |
| 271 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 272 | /* else, the send failed and we should just hang up */ |
| 273 | } |
| 274 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 275 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 276 | just_kill_connection: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 277 | #endif |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 278 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 279 | lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 280 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 281 | /* |
| 282 | * we won't be servicing or receiving anything further from this guy |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 283 | * delete socket from the internal poll list if still present |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 284 | */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 285 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 286 | remove_wsi_socket_from_fds(context, wsi); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 287 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 288 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 289 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 290 | /* tell the user it's all over for this guy */ |
| 291 | |
Andy Green | d430273 | 2011-02-28 07:45:29 +0000 | [diff] [blame] | 292 | if (wsi->protocol && wsi->protocol->callback && |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 293 | ((old_state == WSI_STATE_ESTABLISHED) || |
| 294 | (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || |
| 295 | (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 296 | lwsl_debug("calling back CLOSED\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 297 | wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 298 | wsi->user_space, NULL, 0); |
Andy Green | cc01247 | 2011-11-07 19:53:23 +0800 | [diff] [blame] | 299 | } else |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 300 | lwsl_debug("not calling back closed, old_state=%d\n", old_state); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 301 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 302 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 303 | /* deallocate any active extension contexts */ |
| 304 | |
| 305 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 306 | if (!wsi->active_extensions[n]->callback) |
| 307 | continue; |
| 308 | |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 309 | wsi->active_extensions[n]->callback(context, |
| 310 | wsi->active_extensions[n], wsi, |
| 311 | LWS_EXT_CALLBACK_DESTROY, |
| 312 | wsi->active_extensions_user[n], NULL, 0); |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 313 | |
| 314 | free(wsi->active_extensions_user[n]); |
| 315 | } |
| 316 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 317 | /* |
| 318 | * inform all extensions in case they tracked this guy out of band |
| 319 | * even though not active on him specifically |
| 320 | */ |
| 321 | |
| 322 | ext = context->extensions; |
| 323 | while (ext && ext->callback) { |
| 324 | ext->callback(context, ext, wsi, |
| 325 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, |
| 326 | NULL, NULL, 0); |
| 327 | ext++; |
| 328 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 329 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 330 | |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 331 | /* free up his parsing allocations */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 332 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 333 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 334 | if (wsi->utf8_token[n].token) |
| 335 | free(wsi->utf8_token[n].token); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 336 | #ifndef LWS_NO_CLIENT |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 337 | if (wsi->c_address) |
| 338 | free(wsi->c_address); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 339 | #endif |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 340 | if (wsi->u.ws.rxflow_buffer) |
| 341 | free(wsi->u.ws.rxflow_buffer); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 342 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 343 | /* lwsl_info("closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 344 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 345 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 346 | if (wsi->ssl) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 347 | n = SSL_get_fd(wsi->ssl); |
| 348 | SSL_shutdown(wsi->ssl); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 349 | compatible_close(n); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 350 | SSL_free(wsi->ssl); |
| 351 | } else { |
| 352 | #endif |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 353 | if (wsi->sock) { |
| 354 | n = shutdown(wsi->sock, SHUT_RDWR); |
| 355 | if (n) |
| 356 | lwsl_debug("closing: shutdown returned %d\n", errno); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 357 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 358 | n = compatible_close(wsi->sock); |
| 359 | if (n) |
| 360 | lwsl_debug("closing: close returned %d\n", errno); |
| 361 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 362 | #ifdef LWS_OPENSSL_SUPPORT |
| 363 | } |
| 364 | #endif |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 365 | if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 366 | free(wsi->user_space); |
| 367 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 368 | free(wsi); |
| 369 | } |
| 370 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 371 | /** |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 372 | * libwebsockets_hangup_on_client() - Server calls to terminate client |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 373 | * connection |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 374 | * @context: libwebsockets context |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 375 | * @fd: Connection socket descriptor |
| 376 | */ |
| 377 | |
| 378 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 379 | libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd) |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 380 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 381 | struct libwebsocket *wsi = context->lws_lookup[fd]; |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 382 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 383 | if (wsi) { |
| 384 | libwebsocket_close_and_free_session(context, |
| 385 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
| 386 | } else |
| 387 | close(fd); |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
| 391 | /** |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 392 | * libwebsockets_get_peer_addresses() - Get client address information |
| 393 | * @fd: Connection socket descriptor |
| 394 | * @name: Buffer to take client address name |
| 395 | * @name_len: Length of client address name buffer |
| 396 | * @rip: Buffer to take client address IP qotted quad |
| 397 | * @rip_len: Length of client address IP buffer |
| 398 | * |
| 399 | * This function fills in @name and @rip with the name and IP of |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 400 | * the client connected with socket descriptor @fd. Names may be |
| 401 | * truncated if there is not enough room. If either cannot be |
| 402 | * determined, they will be returned as valid zero-length strings. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 403 | */ |
| 404 | |
| 405 | void |
| 406 | libwebsockets_get_peer_addresses(int fd, char *name, int name_len, |
| 407 | char *rip, int rip_len) |
| 408 | { |
| 409 | unsigned int len; |
| 410 | struct sockaddr_in sin; |
| 411 | struct hostent *host; |
| 412 | struct hostent *host1; |
| 413 | char ip[128]; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 414 | unsigned char *p; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 415 | int n; |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 416 | #ifdef AF_LOCAL |
| 417 | struct sockaddr_un *un; |
| 418 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 419 | |
| 420 | rip[0] = '\0'; |
| 421 | name[0] = '\0'; |
| 422 | |
| 423 | len = sizeof sin; |
| 424 | if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { |
| 425 | perror("getpeername"); |
| 426 | return; |
| 427 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 428 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 429 | host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, |
| 430 | AF_INET); |
| 431 | if (host == NULL) { |
| 432 | perror("gethostbyaddr"); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | strncpy(name, host->h_name, name_len); |
| 437 | name[name_len - 1] = '\0'; |
| 438 | |
| 439 | host1 = gethostbyname(host->h_name); |
| 440 | if (host1 == NULL) |
| 441 | return; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 442 | p = (unsigned char *)host1; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 443 | n = 0; |
| 444 | while (p != NULL) { |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 445 | p = (unsigned char *)host1->h_addr_list[n++]; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 446 | if (p == NULL) |
| 447 | continue; |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 448 | if ((host1->h_addrtype != AF_INET) |
| 449 | #ifdef AF_LOCAL |
| 450 | && (host1->h_addrtype != AF_LOCAL) |
| 451 | #endif |
| 452 | ) |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 453 | continue; |
| 454 | |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 455 | if (host1->h_addrtype == AF_INET) |
| 456 | sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]); |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 457 | #ifdef AF_LOCAL |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 458 | else { |
| 459 | un = (struct sockaddr_un *)p; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 460 | strncpy(ip, un->sun_path, sizeof(ip) - 1); |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 461 | ip[sizeof(ip) - 1] = '\0'; |
| 462 | } |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 463 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 464 | p = NULL; |
| 465 | strncpy(rip, ip, rip_len); |
| 466 | rip[rip_len - 1] = '\0'; |
| 467 | } |
| 468 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 469 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 470 | int libwebsockets_get_random(struct libwebsocket_context *context, |
| 471 | void *buf, int len) |
| 472 | { |
| 473 | int n; |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 474 | char *p = (char *)buf; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 475 | |
| 476 | #ifdef WIN32 |
| 477 | for (n = 0; n < len; n++) |
| 478 | p[n] = (unsigned char)rand(); |
| 479 | #else |
| 480 | n = read(context->fd_random, p, len); |
| 481 | #endif |
| 482 | |
| 483 | return n; |
| 484 | } |
| 485 | |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 486 | int lws_send_pipe_choked(struct libwebsocket *wsi) |
| 487 | { |
| 488 | struct pollfd fds; |
| 489 | |
| 490 | fds.fd = wsi->sock; |
| 491 | fds.events = POLLOUT; |
| 492 | fds.revents = 0; |
| 493 | |
| 494 | if (poll(&fds, 1, 0) != 1) |
| 495 | return 1; |
| 496 | |
| 497 | if ((fds.revents & POLLOUT) == 0) |
| 498 | return 1; |
| 499 | |
| 500 | /* okay to send another packet without blocking */ |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 505 | int |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 506 | lws_handle_POLLOUT_event(struct libwebsocket_context *context, |
| 507 | struct libwebsocket *wsi, struct pollfd *pollfd) |
| 508 | { |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 509 | int n; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 510 | #ifndef LWS_NO_EXTENSIONS |
| 511 | struct lws_tokens eff_buf; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 512 | int ret; |
| 513 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 514 | int handled = 0; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 515 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 516 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 517 | if (!wsi->active_extensions[n]->callback) |
| 518 | continue; |
| 519 | |
| 520 | m = wsi->active_extensions[n]->callback(context, |
| 521 | wsi->active_extensions[n], wsi, |
| 522 | LWS_EXT_CALLBACK_IS_WRITEABLE, |
| 523 | wsi->active_extensions_user[n], NULL, 0); |
| 524 | if (m > handled) |
| 525 | handled = m; |
| 526 | } |
| 527 | |
| 528 | if (handled == 1) |
| 529 | goto notify_action; |
| 530 | |
| 531 | if (!wsi->extension_data_pending || handled == 2) |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 532 | goto user_service; |
| 533 | |
| 534 | /* |
| 535 | * check in on the active extensions, see if they |
| 536 | * had pending stuff to spill... they need to get the |
| 537 | * first look-in otherwise sequence will be disordered |
| 538 | * |
| 539 | * NULL, zero-length eff_buf means just spill pending |
| 540 | */ |
| 541 | |
| 542 | ret = 1; |
| 543 | while (ret == 1) { |
| 544 | |
| 545 | /* default to nobody has more to spill */ |
| 546 | |
| 547 | ret = 0; |
| 548 | eff_buf.token = NULL; |
| 549 | eff_buf.token_len = 0; |
| 550 | |
| 551 | /* give every extension a chance to spill */ |
| 552 | |
| 553 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 554 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 555 | wsi->protocol->owning_server, |
| 556 | wsi->active_extensions[n], wsi, |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 557 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
| 558 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 559 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 560 | lwsl_err("ext reports fatal error\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 561 | return -1; |
| 562 | } |
| 563 | if (m) |
| 564 | /* |
| 565 | * at least one extension told us he has more |
| 566 | * to spill, so we will go around again after |
| 567 | */ |
| 568 | ret = 1; |
| 569 | } |
| 570 | |
| 571 | /* assuming they gave us something to send, send it */ |
| 572 | |
| 573 | if (eff_buf.token_len) { |
| 574 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
| 575 | eff_buf.token_len)) |
| 576 | return -1; |
| 577 | } else |
| 578 | continue; |
| 579 | |
| 580 | /* no extension has more to spill */ |
| 581 | |
| 582 | if (!ret) |
| 583 | continue; |
| 584 | |
| 585 | /* |
| 586 | * There's more to spill from an extension, but we just sent |
| 587 | * something... did that leave the pipe choked? |
| 588 | */ |
| 589 | |
| 590 | if (!lws_send_pipe_choked(wsi)) |
| 591 | /* no we could add more */ |
| 592 | continue; |
| 593 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 594 | lwsl_info("choked in POLLOUT service\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * Yes, he's choked. Leave the POLLOUT masked on so we will |
| 598 | * come back here when he is unchoked. Don't call the user |
| 599 | * callback to enforce ordering of spilling, he'll get called |
| 600 | * when we come back here and there's nothing more to spill. |
| 601 | */ |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | wsi->extension_data_pending = 0; |
| 607 | |
| 608 | user_service: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 609 | #endif |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 610 | /* one shot */ |
| 611 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 612 | if (pollfd) { |
| 613 | pollfd->events &= ~POLLOUT; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 614 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 615 | /* external POLL support via protocol 0 */ |
| 616 | context->protocols[0].callback(context, wsi, |
| 617 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 618 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 619 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 620 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 621 | notify_action: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 622 | #endif |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 623 | |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 624 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT) |
| 625 | n = LWS_CALLBACK_CLIENT_WRITEABLE; |
| 626 | else |
| 627 | n = LWS_CALLBACK_SERVER_WRITEABLE; |
| 628 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 629 | user_callback_handle_rxflow(wsi->protocol->callback, context, |
| 630 | wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 637 | void |
| 638 | libwebsocket_service_timeout_check(struct libwebsocket_context *context, |
| 639 | struct libwebsocket *wsi, unsigned int sec) |
| 640 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 641 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 642 | int n; |
| 643 | |
| 644 | /* |
| 645 | * if extensions want in on it (eg, we are a mux parent) |
| 646 | * give them a chance to service child timeouts |
| 647 | */ |
| 648 | |
| 649 | for (n = 0; n < wsi->count_active_extensions; n++) |
| 650 | wsi->active_extensions[n]->callback( |
| 651 | context, wsi->active_extensions[n], |
| 652 | wsi, LWS_EXT_CALLBACK_1HZ, |
| 653 | wsi->active_extensions_user[n], NULL, sec); |
| 654 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 655 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 656 | if (!wsi->pending_timeout) |
| 657 | return; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 658 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 659 | /* |
| 660 | * if we went beyond the allowed time, kill the |
| 661 | * connection |
| 662 | */ |
| 663 | |
| 664 | if (sec > wsi->pending_timeout_limit) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 665 | lwsl_info("TIMEDOUT WAITING\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 666 | libwebsocket_close_and_free_session(context, |
| 667 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
| 668 | } |
| 669 | } |
| 670 | |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 671 | /** |
| 672 | * libwebsocket_service_fd() - Service polled socket with something waiting |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 673 | * @context: Websocket context |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 674 | * @pollfd: The pollfd entry describing the socket fd and which events |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 675 | * happened. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 676 | * |
Andy Green | 7500617 | 2013-01-22 12:32:11 +0800 | [diff] [blame] | 677 | * This function takes a pollfd that has POLLIN or POLLOUT activity and |
| 678 | * services it according to the state of the associated struct libwebsocket. |
| 679 | * |
| 680 | * The one call deals with all "service" that might happen on a socket |
| 681 | * including listen accepts, http files as well as websocket protocol. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 682 | */ |
| 683 | |
| 684 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 685 | libwebsocket_service_fd(struct libwebsocket_context *context, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 686 | struct pollfd *pollfd) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 687 | { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 688 | struct libwebsocket *wsi; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 689 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + |
| 690 | MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 691 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 692 | int m; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 693 | struct timeval tv; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 694 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 695 | int more = 1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 696 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 697 | struct lws_tokens eff_buf; |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 698 | #ifndef LWS_NO_CLIENT |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 699 | extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd); |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 700 | #endif |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 701 | #ifndef LWS_NO_SERVER |
| 702 | extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd); |
| 703 | #endif |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 704 | /* |
| 705 | * you can call us with pollfd = NULL to just allow the once-per-second |
| 706 | * global timeout checks; if less than a second since the last check |
| 707 | * it returns immediately then. |
| 708 | */ |
| 709 | |
| 710 | gettimeofday(&tv, NULL); |
| 711 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 712 | if (context->last_timeout_check_s != tv.tv_sec) { |
| 713 | context->last_timeout_check_s = tv.tv_sec; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 714 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 715 | /* if our parent went down, don't linger around */ |
| 716 | if (context->started_with_parent && kill(context->started_with_parent, 0) < 0) |
| 717 | kill(getpid(), SIGTERM); |
| 718 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 719 | /* global timeout check once per second */ |
| 720 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 721 | for (n = 0; n < context->fds_count; n++) { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 722 | struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd]; |
| 723 | if (!new_wsi) |
| 724 | continue; |
| 725 | libwebsocket_service_timeout_check(context, |
| 726 | new_wsi, tv.tv_sec); |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | /* just here for timeout management? */ |
| 731 | |
| 732 | if (pollfd == NULL) |
| 733 | return 0; |
| 734 | |
| 735 | /* no, here to service a socket descriptor */ |
| 736 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 737 | /* |
| 738 | * deal with listen service piggybacking |
| 739 | * every listen_service_modulo services of other fds, we |
| 740 | * sneak one in to service the listen socket if there's anything waiting |
| 741 | * |
| 742 | * To handle connection storms, as found in ab, if we previously saw a |
| 743 | * pending connection here, it causes us to check again next time. |
| 744 | */ |
| 745 | |
| 746 | if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) { |
| 747 | context->listen_service_count++; |
| 748 | if (context->listen_service_extraseen || |
| 749 | context->listen_service_count == context->listen_service_modulo) { |
| 750 | context->listen_service_count = 0; |
| 751 | m = 1; |
| 752 | if (context->listen_service_extraseen > 5) |
| 753 | m = 2; |
| 754 | while (m--) { |
| 755 | /* even with extpoll, we prepared this internal fds for listen */ |
| 756 | n = poll(&context->fds[0], 1, 0); |
| 757 | if (n > 0) { /* there's a connection waiting for us */ |
| 758 | libwebsocket_service_fd(context, &context->fds[0]); |
| 759 | context->listen_service_extraseen++; |
| 760 | } else { |
| 761 | if (context->listen_service_extraseen) |
| 762 | context->listen_service_extraseen--; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | } |
| 769 | |
| 770 | /* okay, what we came here to do... */ |
| 771 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 772 | wsi = context->lws_lookup[pollfd->fd]; |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 773 | if (wsi == NULL) { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 774 | if (pollfd->fd > 11) |
| 775 | lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count); |
Andy Green | fa3f405 | 2012-10-07 20:40:35 +0800 | [diff] [blame] | 776 | return 0; |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 777 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 778 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 779 | switch (wsi->mode) { |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 780 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 781 | #ifndef LWS_NO_SERVER |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 782 | case LWS_CONNMODE_HTTP_SERVING: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 783 | case LWS_CONNMODE_SERVER_LISTENER: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 784 | case LWS_CONNMODE_BROADCAST_PROXY_LISTENER: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 785 | case LWS_CONNMODE_BROADCAST_PROXY: |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 786 | return lws_server_socket_service(context, wsi, pollfd); |
| 787 | #endif |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 788 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 789 | case LWS_CONNMODE_WS_SERVING: |
| 790 | case LWS_CONNMODE_WS_CLIENT: |
| 791 | |
| 792 | /* handle session socket closed */ |
| 793 | |
| 794 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 795 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 796 | lwsl_debug("Session Socket %p (fd=%d) dead\n", |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 797 | (void *)wsi, pollfd->fd); |
| 798 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 799 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 800 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 801 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 804 | /* the guy requested a callback when it was OK to write */ |
| 805 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 806 | if ((pollfd->revents & POLLOUT) && |
| 807 | wsi->state == WSI_STATE_ESTABLISHED) |
| 808 | if (lws_handle_POLLOUT_event(context, wsi, |
| 809 | pollfd) < 0) { |
| 810 | libwebsocket_close_and_free_session( |
| 811 | context, wsi, LWS_CLOSE_STATUS_NORMAL); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 812 | return 0; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 813 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 814 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 815 | |
| 816 | /* any incoming data ready? */ |
| 817 | |
| 818 | if (!(pollfd->revents & POLLIN)) |
| 819 | break; |
| 820 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 821 | #ifdef LWS_OPENSSL_SUPPORT |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 822 | read_pending: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 823 | if (wsi->ssl) |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 824 | eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 825 | else |
| 826 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 827 | eff_buf.token_len = |
Andy Green | 72c3432 | 2011-04-16 10:46:21 +0100 | [diff] [blame] | 828 | recv(pollfd->fd, buf, sizeof buf, 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 829 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 830 | if (eff_buf.token_len < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 831 | lwsl_debug("Socket read returned %d\n", |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 832 | eff_buf.token_len); |
Alon Levy | dc93b7f | 2012-10-19 11:21:57 +0200 | [diff] [blame] | 833 | if (errno != EINTR && errno != EAGAIN) |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 834 | libwebsocket_close_and_free_session(context, |
| 835 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 836 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 837 | } |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 838 | if (!eff_buf.token_len) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 839 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 840 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | fa3f405 | 2012-10-07 20:40:35 +0800 | [diff] [blame] | 841 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 844 | /* |
| 845 | * give any active extensions a chance to munge the buffer |
| 846 | * before parse. We pass in a pointer to an lws_tokens struct |
| 847 | * prepared with the default buffer and content length that's in |
| 848 | * there. Rather than rewrite the default buffer, extensions |
| 849 | * that expect to grow the buffer can adapt .token to |
| 850 | * point to their own per-connection buffer in the extension |
| 851 | * user allocation. By default with no extensions or no |
| 852 | * extension callback handling, just the normal input buffer is |
| 853 | * used then so it is efficient. |
| 854 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 855 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 856 | eff_buf.token = (char *)buf; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 857 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 858 | more = 1; |
| 859 | while (more) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 860 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 861 | more = 0; |
| 862 | |
| 863 | for (n = 0; n < wsi->count_active_extensions; n++) { |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 864 | m = wsi->active_extensions[n]->callback(context, |
| 865 | wsi->active_extensions[n], wsi, |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 866 | LWS_EXT_CALLBACK_PACKET_RX_PREPARSE, |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 867 | wsi->active_extensions_user[n], |
| 868 | &eff_buf, 0); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 869 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 870 | lwsl_ext( |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 871 | "Extension reports fatal error\n"); |
| 872 | libwebsocket_close_and_free_session( |
| 873 | context, wsi, |
| 874 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 875 | return 0; |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 876 | } |
| 877 | if (m) |
| 878 | more = 1; |
| 879 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 880 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 881 | /* service incoming data */ |
| 882 | |
| 883 | if (eff_buf.token_len) { |
| 884 | n = libwebsocket_read(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 885 | (unsigned char *)eff_buf.token, |
| 886 | eff_buf.token_len); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 887 | if (n < 0) |
| 888 | /* we closed wsi */ |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 889 | return 0; |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 890 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 891 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 892 | eff_buf.token = NULL; |
| 893 | eff_buf.token_len = 0; |
| 894 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 895 | #endif |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 896 | |
| 897 | #ifdef LWS_OPENSSL_SUPPORT |
| 898 | if (wsi->ssl && SSL_pending(wsi->ssl)) |
| 899 | goto read_pending; |
| 900 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 901 | break; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 902 | |
| 903 | default: |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 904 | #ifdef LWS_NO_CLIENT |
| 905 | break; |
| 906 | #else |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 907 | return lws_client_socket_service(context, wsi, pollfd); |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 908 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 914 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 915 | /** |
| 916 | * libwebsocket_context_destroy() - Destroy the websocket context |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 917 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 918 | * |
| 919 | * This function closes any active connections and then frees the |
| 920 | * context. After calling this, any further use of the context is |
| 921 | * undefined. |
| 922 | */ |
| 923 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 924 | libwebsocket_context_destroy(struct libwebsocket_context *context) |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 925 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 926 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 927 | int n; |
| 928 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 929 | struct libwebsocket_extension *ext; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 930 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 931 | for (n = 0; n < context->fds_count; n++) { |
| 932 | struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd]; |
| 933 | libwebsocket_close_and_free_session(context, |
| 934 | wsi, LWS_CLOSE_STATUS_GOINGAWAY); |
| 935 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 936 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 937 | /* |
| 938 | * give all extensions a chance to clean up any per-context |
| 939 | * allocations they might have made |
| 940 | */ |
| 941 | |
| 942 | ext = context->extensions; |
| 943 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT; |
| 944 | if (context->listen_port) |
| 945 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT; |
Paulo Roberto Urio | 1f680ab | 2012-06-04 08:40:28 +0800 | [diff] [blame] | 946 | while (ext && ext->callback) { |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 947 | ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 948 | ext++; |
| 949 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 950 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 951 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 952 | #ifdef WIN32 |
| 953 | #else |
| 954 | close(context->fd_random); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 955 | #endif |
| 956 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 957 | #ifdef LWS_OPENSSL_SUPPORT |
| 958 | if (context->ssl_ctx) |
| 959 | SSL_CTX_free(context->ssl_ctx); |
| 960 | if (context->ssl_client_ctx) |
| 961 | SSL_CTX_free(context->ssl_client_ctx); |
| 962 | #endif |
| 963 | |
| 964 | free(context); |
| 965 | |
| 966 | #ifdef WIN32 |
| 967 | WSACleanup(); |
| 968 | #endif |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame^] | 971 | /** |
| 972 | * libwebsocket_context_user() - get the user data associated with the whole context |
| 973 | * @context: Websocket context |
| 974 | * |
| 975 | * This returns the optional user allocation that can be attached to |
| 976 | * the context the sockets live in at context_create time. It's a way |
| 977 | * to let all sockets serviced in the same context share data without |
| 978 | * using globals statics in the user code. |
| 979 | */ |
| 980 | |
| 981 | |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 982 | LWS_EXTERN void * |
| 983 | libwebsocket_context_user(struct libwebsocket_context *context) |
| 984 | { |
| 985 | return context->user_space; |
| 986 | } |
| 987 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 988 | /** |
| 989 | * libwebsocket_service() - Service any pending websocket activity |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 990 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 991 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 992 | * service otherwise block and service immediately, returning |
| 993 | * after the timeout if nothing needed service. |
| 994 | * |
| 995 | * This function deals with any pending websocket traffic, for three |
| 996 | * kinds of event. It handles these events on both server and client |
| 997 | * types of connection the same. |
| 998 | * |
| 999 | * 1) Accept new connections to our context's server |
| 1000 | * |
| 1001 | * 2) Perform pending broadcast writes initiated from other forked |
| 1002 | * processes (effectively serializing asynchronous broadcasts) |
| 1003 | * |
| 1004 | * 3) Call the receive callback for incoming frame data received by |
| 1005 | * server or client connections. |
| 1006 | * |
| 1007 | * You need to call this service function periodically to all the above |
| 1008 | * functions to happen; if your application is single-threaded you can |
| 1009 | * just call it in your main event loop. |
| 1010 | * |
| 1011 | * Alternatively you can fork a new process that asynchronously handles |
| 1012 | * calling this service in a loop. In that case you are happy if this |
| 1013 | * call blocks your thread until it needs to take care of something and |
| 1014 | * would call it with a large nonzero timeout. Your loop then takes no |
| 1015 | * CPU while there is nothing happening. |
| 1016 | * |
| 1017 | * If you are calling it in a single-threaded app, you don't want it to |
| 1018 | * wait around blocking other things in your loop from happening, so you |
| 1019 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 1020 | * nothing is pending, or as soon as it services whatever was pending. |
| 1021 | */ |
| 1022 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1023 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1024 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1025 | libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1026 | { |
| 1027 | int n; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* stay dead once we are dead */ |
| 1030 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1031 | if (context == NULL) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1032 | return 1; |
| 1033 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1034 | /* wait for something to need service */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1035 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1036 | n = poll(context->fds, context->fds_count, timeout_ms); |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1037 | if (n == 0) /* poll timeout */ |
| 1038 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1039 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1040 | if (n < 0) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 1041 | return -1; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1042 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1043 | /* any socket with events to service? */ |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1044 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1045 | for (n = 0; n < context->fds_count; n++) |
| 1046 | if (context->fds[n].revents) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 1047 | if (libwebsocket_service_fd(context, |
| 1048 | &context->fds[n]) < 0) |
| 1049 | return -1; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1050 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1053 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1054 | int |
| 1055 | lws_any_extension_handled(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1056 | struct libwebsocket *wsi, |
| 1057 | enum libwebsocket_extension_callback_reasons r, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1058 | void *v, size_t len) |
| 1059 | { |
| 1060 | int n; |
| 1061 | int handled = 0; |
| 1062 | |
| 1063 | /* maybe an extension will take care of it for us */ |
| 1064 | |
| 1065 | for (n = 0; n < wsi->count_active_extensions && !handled; n++) { |
| 1066 | if (!wsi->active_extensions[n]->callback) |
| 1067 | continue; |
| 1068 | |
| 1069 | handled |= wsi->active_extensions[n]->callback(context, |
| 1070 | wsi->active_extensions[n], wsi, |
| 1071 | r, wsi->active_extensions_user[n], v, len); |
| 1072 | } |
| 1073 | |
| 1074 | return handled; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | void * |
| 1079 | lws_get_extension_user_matching_ext(struct libwebsocket *wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1080 | struct libwebsocket_extension *ext) |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1081 | { |
| 1082 | int n = 0; |
| 1083 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 1084 | if (wsi == NULL) |
| 1085 | return NULL; |
| 1086 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1087 | while (n < wsi->count_active_extensions) { |
| 1088 | if (wsi->active_extensions[n] != ext) { |
| 1089 | n++; |
| 1090 | continue; |
| 1091 | } |
| 1092 | return wsi->active_extensions_user[n]; |
| 1093 | } |
| 1094 | |
| 1095 | return NULL; |
| 1096 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1097 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1098 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1099 | /** |
| 1100 | * libwebsocket_callback_on_writable() - Request a callback when this socket |
| 1101 | * becomes able to be written to without |
| 1102 | * blocking |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1103 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1104 | * @context: libwebsockets context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1105 | * @wsi: Websocket connection instance to get callback for |
| 1106 | */ |
| 1107 | |
| 1108 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1109 | libwebsocket_callback_on_writable(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1110 | struct libwebsocket *wsi) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1111 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1112 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1113 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1114 | int handled = 0; |
| 1115 | |
| 1116 | /* maybe an extension will take care of it for us */ |
| 1117 | |
| 1118 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 1119 | if (!wsi->active_extensions[n]->callback) |
| 1120 | continue; |
| 1121 | |
| 1122 | handled |= wsi->active_extensions[n]->callback(context, |
| 1123 | wsi->active_extensions[n], wsi, |
| 1124 | LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, |
| 1125 | wsi->active_extensions_user[n], NULL, 0); |
| 1126 | } |
| 1127 | |
| 1128 | if (handled) |
| 1129 | return 1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1130 | #endif |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1131 | if (wsi->position_in_fds_table < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1132 | lwsl_err("libwebsocket_callback_on_writable: " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1133 | "failed to find socket %d\n", wsi->sock); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1134 | return -1; |
| 1135 | } |
| 1136 | |
| 1137 | context->fds[wsi->position_in_fds_table].events |= POLLOUT; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1138 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1139 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1140 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1141 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 1142 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 1143 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1144 | return 1; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * libwebsocket_callback_on_writable_all_protocol() - Request a callback for |
| 1149 | * all connections using the given protocol when it |
| 1150 | * becomes possible to write to each socket without |
| 1151 | * blocking in turn. |
| 1152 | * |
| 1153 | * @protocol: Protocol whose connections will get callbacks |
| 1154 | */ |
| 1155 | |
| 1156 | int |
| 1157 | libwebsocket_callback_on_writable_all_protocol( |
| 1158 | const struct libwebsocket_protocols *protocol) |
| 1159 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1160 | struct libwebsocket_context *context = protocol->owning_server; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1161 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1162 | struct libwebsocket *wsi; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1163 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1164 | for (n = 0; n < context->fds_count; n++) { |
| 1165 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 1166 | if (!wsi) |
| 1167 | continue; |
| 1168 | if (wsi->protocol == protocol) |
| 1169 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1170 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1175 | /** |
| 1176 | * libwebsocket_set_timeout() - marks the wsi as subject to a timeout |
| 1177 | * |
| 1178 | * You will not need this unless you are doing something special |
| 1179 | * |
| 1180 | * @wsi: Websocket connection instance |
| 1181 | * @reason: timeout reason |
| 1182 | * @secs: how many seconds |
| 1183 | */ |
| 1184 | |
| 1185 | void |
| 1186 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 1187 | enum pending_timeout reason, int secs) |
| 1188 | { |
| 1189 | struct timeval tv; |
| 1190 | |
| 1191 | gettimeofday(&tv, NULL); |
| 1192 | |
| 1193 | wsi->pending_timeout_limit = tv.tv_sec + secs; |
| 1194 | wsi->pending_timeout = reason; |
| 1195 | } |
| 1196 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1197 | |
| 1198 | /** |
| 1199 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 1200 | * |
| 1201 | * You will not need this unless you are doing something special |
| 1202 | * |
| 1203 | * @wsi: Websocket connection instance |
| 1204 | */ |
| 1205 | |
| 1206 | int |
| 1207 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 1208 | { |
| 1209 | return wsi->sock; |
| 1210 | } |
| 1211 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1212 | #ifdef LWS_NO_SERVER |
| 1213 | int |
| 1214 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
| 1215 | { |
| 1216 | return 0; |
| 1217 | } |
| 1218 | #else |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1219 | int |
| 1220 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
| 1221 | { |
| 1222 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
| 1223 | int n; |
| 1224 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1225 | if (!(wsi->u.ws.rxflow_change_to & 2)) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1226 | return 0; |
| 1227 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1228 | wsi->u.ws.rxflow_change_to &= ~2; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1229 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1230 | lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1231 | |
| 1232 | /* if we're letting it come again, did we interrupt anything? */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1233 | if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) { |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1234 | n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0); |
| 1235 | if (n < 0) { |
| 1236 | libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS); |
| 1237 | return -1; |
| 1238 | } |
| 1239 | if (n) |
| 1240 | /* oh he stuck again, do nothing */ |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1244 | if (wsi->u.ws.rxflow_change_to & 1) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1245 | context->fds[wsi->position_in_fds_table].events |= POLLIN; |
| 1246 | else |
| 1247 | context->fds[wsi->position_in_fds_table].events &= ~POLLIN; |
| 1248 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1249 | if (wsi->u.ws.rxflow_change_to & 1) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1250 | /* external POLL support via protocol 0 */ |
| 1251 | context->protocols[0].callback(context, wsi, |
| 1252 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 1253 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 1254 | else |
| 1255 | /* external POLL support via protocol 0 */ |
| 1256 | context->protocols[0].callback(context, wsi, |
| 1257 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 1258 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 1259 | |
| 1260 | return 1; |
| 1261 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1262 | #endif |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1263 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1264 | /** |
| 1265 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
| 1266 | * receieved packets. |
| 1267 | * |
| 1268 | * If the output side of a server process becomes choked, this allows flow |
| 1269 | * control for the input side. |
| 1270 | * |
| 1271 | * @wsi: Websocket connection instance to get callback for |
| 1272 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 1273 | */ |
| 1274 | |
| 1275 | int |
| 1276 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 1277 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1278 | wsi->u.ws.rxflow_change_to = 2 | !!enable; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1279 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1280 | return 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1283 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1284 | /** |
| 1285 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 1286 | * |
| 1287 | * This is typically used by client code to fill in the host parameter |
| 1288 | * when making a client connection. You can only call it after the context |
| 1289 | * has been created. |
| 1290 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1291 | * @context: Websocket context |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1292 | */ |
| 1293 | |
| 1294 | |
| 1295 | extern const char * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1296 | libwebsocket_canonical_hostname(struct libwebsocket_context *context) |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1297 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1298 | return (const char *)context->canonical_hostname; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1302 | static void sigpipe_handler(int x) |
| 1303 | { |
| 1304 | } |
| 1305 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1306 | #ifdef LWS_OPENSSL_SUPPORT |
| 1307 | static int |
| 1308 | OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 1309 | { |
| 1310 | |
| 1311 | SSL *ssl; |
| 1312 | int n; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1313 | struct libwebsocket_context *context; |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1314 | |
| 1315 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 1316 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1317 | |
| 1318 | /* |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1319 | * !!! nasty openssl requires the index to come as a library-scope |
| 1320 | * static |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1321 | */ |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1322 | context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1323 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1324 | n = context->protocols[0].callback(NULL, NULL, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1325 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
| 1326 | x509_ctx, ssl, preverify_ok); |
| 1327 | |
| 1328 | /* convert return code from 0 = OK to 1 = OK */ |
| 1329 | |
| 1330 | if (!n) |
| 1331 | n = 1; |
| 1332 | else |
| 1333 | n = 0; |
| 1334 | |
| 1335 | return n; |
| 1336 | } |
| 1337 | #endif |
| 1338 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1339 | int user_callback_handle_rxflow(callback_function callback_function, |
| 1340 | struct libwebsocket_context * context, |
| 1341 | struct libwebsocket *wsi, |
| 1342 | enum libwebsocket_callback_reasons reason, void *user, |
| 1343 | void *in, size_t len) |
| 1344 | { |
| 1345 | int n; |
| 1346 | |
| 1347 | n = callback_function(context, wsi, reason, user, in, len); |
| 1348 | if (n < 0) |
| 1349 | return n; |
| 1350 | |
| 1351 | _libwebsocket_rx_flow_control(wsi); |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1356 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1357 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1358 | * libwebsocket_create_context() - Create the websocket handler |
| 1359 | * @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] | 1360 | * any port, that's what you want if you are not running a |
| 1361 | * websocket server at all but just using it as a client |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1362 | * @interf: NULL to bind the listen socket to all interfaces, or the |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1363 | * interface name, eg, "eth2" |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 1364 | * @protocols: Array of structures listing supported protocols and a protocol- |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1365 | * specific callback for each one. The list is ended with an |
| 1366 | * entry that has a NULL callback pointer. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1367 | * It's not const because we write the owning_server member |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 1368 | * @extensions: NULL or array of libwebsocket_extension structs listing the |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1369 | * extensions this context supports. If you configured with |
| 1370 | * --without-extensions, you should give NULL here. |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1371 | * @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] | 1372 | * to listen using SSL, set to the filepath to fetch the |
| 1373 | * server cert from, otherwise NULL for unencrypted |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1374 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1375 | * else ignored |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 1376 | * @ssl_ca_filepath: CA certificate filepath or NULL |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1377 | * @gid: group id to change to after setting listen socket, or -1. |
| 1378 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 1379 | * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
Andy Green | 15e31f3 | 2012-10-19 18:36:28 +0800 | [diff] [blame] | 1380 | * @user: optional user pointer that can be recovered via the context |
| 1381 | * pointer using libwebsocket_context_user |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1382 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1383 | * This function creates the listening socket and takes care |
| 1384 | * of all initialization in one step. |
| 1385 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1386 | * After initialization, it returns a struct libwebsocket_context * that |
| 1387 | * represents this server. After calling, user code needs to take care |
| 1388 | * of calling libwebsocket_service() with the context pointer to get the |
| 1389 | * server's sockets serviced. This can be done in the same process context |
| 1390 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1391 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1392 | * The protocol callback functions are called for a handful of events |
| 1393 | * including http requests coming in, websocket connections becoming |
| 1394 | * established, and data arriving; it's also called periodically to allow |
| 1395 | * async transmission. |
| 1396 | * |
| 1397 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 1398 | * at that time websocket protocol has not been negotiated. Other |
| 1399 | * protocols after the first one never see any HTTP callack activity. |
| 1400 | * |
| 1401 | * The server created is a simple http server by default; part of the |
| 1402 | * websocket standard is upgrading this http connection to a websocket one. |
| 1403 | * |
| 1404 | * This allows the same server to provide files like scripts and favicon / |
| 1405 | * images or whatever over http and dynamic data over websockets all in |
| 1406 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1407 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1408 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1409 | struct libwebsocket_context * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1410 | libwebsocket_create_context(int port, const char *interf, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1411 | struct libwebsocket_protocols *protocols, |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 1412 | struct libwebsocket_extension *extensions, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1413 | const char *ssl_cert_filepath, |
| 1414 | const char *ssl_private_key_filepath, |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 1415 | const char *ssl_ca_filepath, |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 1416 | int gid, int uid, unsigned int options, |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 1417 | void *user) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1418 | { |
| 1419 | int n; |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 1420 | struct sockaddr_in serv_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 1421 | int opt = 1; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1422 | struct libwebsocket_context *context = NULL; |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 1423 | #ifndef LWS_NO_FORK |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1424 | unsigned int slen; |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 1425 | struct sockaddr_in cli_addr; |
| 1426 | int fd; |
| 1427 | #endif |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1428 | char *p; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1429 | struct libwebsocket *wsi; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1430 | #ifndef LWS_NO_EXTENSIONS |
| 1431 | int m; |
| 1432 | #endif |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1433 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1434 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 1435 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1436 | char ssl_err_buf[512]; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1437 | #endif |
| 1438 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1439 | lwsl_notice("Initial logging level %d\n", log_level); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1440 | lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH); |
| 1441 | lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN); |
| 1442 | lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC); |
| 1443 | lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC); |
| 1444 | lwsl_info(" MAX_USER_RX_BUFFER: %u\n", MAX_USER_RX_BUFFER); |
| 1445 | lwsl_info(" MAX_BROADCAST_PAYLOAD: %u\n", MAX_BROADCAST_PAYLOAD); |
| 1446 | lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1447 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1448 | lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1449 | #else |
| 1450 | lwsl_notice(" Configured without extension support\n"); |
| 1451 | #endif |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1452 | lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED); |
| 1453 | lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT); |
| 1454 | lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING); |
| 1455 | lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH); |
| 1456 | lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1457 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1458 | #ifdef _WIN32 |
| 1459 | { |
| 1460 | WORD wVersionRequested; |
| 1461 | WSADATA wsaData; |
| 1462 | int err; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1463 | HMODULE wsdll; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */ |
| 1466 | wVersionRequested = MAKEWORD(2, 2); |
| 1467 | |
| 1468 | err = WSAStartup(wVersionRequested, &wsaData); |
| 1469 | if (err != 0) { |
| 1470 | /* Tell the user that we could not find a usable */ |
| 1471 | /* Winsock DLL. */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1472 | lwsl_err("WSAStartup failed with error: %d\n", err); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1473 | return NULL; |
| 1474 | } |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 1475 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1476 | /* default to a poll() made out of select() */ |
| 1477 | poll = emulated_poll; |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 1478 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1479 | /* if windows socket lib available, use his WSAPoll */ |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 1480 | wsdll = GetModuleHandle(_T("Ws2_32.dll")); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1481 | if (wsdll) |
| 1482 | poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1483 | } |
| 1484 | #endif |
| 1485 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1486 | context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1487 | if (!context) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1488 | lwsl_err("No memory for websocket context\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1489 | return NULL; |
| 1490 | } |
Andy Green | 35f332b | 2013-01-21 13:06:38 +0800 | [diff] [blame] | 1491 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 1492 | extern int pid_daemon; |
| 1493 | context->started_with_parent = pid_daemon; |
| 1494 | lwsl_notice(" Started with daemon pid %d\n", pid_daemon); |
| 1495 | #endif |
| 1496 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1497 | context->protocols = protocols; |
| 1498 | context->listen_port = port; |
| 1499 | context->http_proxy_port = 0; |
| 1500 | context->http_proxy_address[0] = '\0'; |
| 1501 | context->options = options; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1502 | /* to reduce this allocation, */ |
| 1503 | context->max_fds = getdtablesize(); |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1504 | lwsl_notice(" max fd tracked: %u\n", context->max_fds); |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 1505 | lwsl_notice(" static allocation: %u bytes\n", |
| 1506 | (sizeof(struct pollfd) * context->max_fds) + |
| 1507 | (sizeof(struct libwebsocket *) * context->max_fds)); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1508 | |
| 1509 | context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds); |
| 1510 | if (context->fds == NULL) { |
| 1511 | lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds); |
| 1512 | free(context); |
| 1513 | return NULL; |
| 1514 | } |
Edwin van den Oetelaar | f6eeabc | 2013-01-19 20:01:01 +0800 | [diff] [blame] | 1515 | context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1516 | if (context->lws_lookup == NULL) { |
| 1517 | lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds); |
| 1518 | free(context->fds); |
| 1519 | free(context); |
| 1520 | return NULL; |
| 1521 | } |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 1522 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1523 | context->fds_count = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1524 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 1525 | context->extensions = extensions; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1526 | #endif |
Paulo Roberto Urio | 1e32663 | 2012-06-04 10:52:19 +0800 | [diff] [blame] | 1527 | context->last_timeout_check_s = 0; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1528 | context->user_space = user; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1529 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1530 | #ifdef WIN32 |
| 1531 | context->fd_random = 0; |
| 1532 | #else |
| 1533 | context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); |
| 1534 | if (context->fd_random < 0) { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1535 | free(context); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1536 | lwsl_err("Unable to open random device %s %d\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1537 | SYSTEM_RANDOM_FILEPATH, context->fd_random); |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1538 | return NULL; |
| 1539 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1540 | #endif |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1541 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1542 | #ifdef LWS_OPENSSL_SUPPORT |
| 1543 | context->use_ssl = 0; |
| 1544 | context->ssl_ctx = NULL; |
| 1545 | context->ssl_client_ctx = NULL; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1546 | openssl_websocket_private_data_index = 0; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1547 | #endif |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1548 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1549 | strcpy(context->canonical_hostname, "unknown"); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 1550 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1551 | #ifndef LWS_NO_SERVER |
| 1552 | if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) { |
| 1553 | struct sockaddr sa; |
| 1554 | char hostname[1024] = ""; |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 1555 | |
| 1556 | /* find canonical hostname */ |
| 1557 | |
| 1558 | hostname[(sizeof hostname) - 1] = '\0'; |
| 1559 | memset(&sa, 0, sizeof(sa)); |
| 1560 | sa.sa_family = AF_INET; |
| 1561 | sa.sa_data[(sizeof sa.sa_data) - 1] = '\0'; |
| 1562 | gethostname(hostname, (sizeof hostname) - 1); |
| 1563 | |
| 1564 | n = 0; |
| 1565 | |
David Galeano | ed3c840 | 2013-01-10 10:45:24 +0800 | [diff] [blame] | 1566 | if (strlen(hostname) < sizeof(sa.sa_data) - 1) { |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 1567 | strcpy(sa.sa_data, hostname); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1568 | // lwsl_debug("my host name is %s\n", sa.sa_data); |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 1569 | n = getnameinfo(&sa, sizeof(sa), hostname, |
| 1570 | (sizeof hostname) - 1, NULL, 0, 0); |
| 1571 | } |
| 1572 | |
| 1573 | if (!n) { |
| 1574 | strncpy(context->canonical_hostname, hostname, |
| 1575 | sizeof context->canonical_hostname - 1); |
| 1576 | context->canonical_hostname[ |
| 1577 | sizeof context->canonical_hostname - 1] = '\0'; |
| 1578 | } else |
| 1579 | strncpy(context->canonical_hostname, hostname, |
| 1580 | sizeof context->canonical_hostname - 1); |
| 1581 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1582 | lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 1583 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1584 | #endif |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 1585 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1586 | /* split the proxy ads:port if given */ |
| 1587 | |
| 1588 | p = getenv("http_proxy"); |
| 1589 | if (p) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1590 | strncpy(context->http_proxy_address, p, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1591 | sizeof context->http_proxy_address - 1); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1592 | context->http_proxy_address[ |
| 1593 | sizeof context->http_proxy_address - 1] = '\0'; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1594 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1595 | p = strchr(context->http_proxy_address, ':'); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1596 | if (p == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1597 | lwsl_err("http_proxy needs to be ads:port\n"); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1598 | return NULL; |
| 1599 | } |
| 1600 | *p = '\0'; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1601 | context->http_proxy_port = atoi(p + 1); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1602 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1603 | lwsl_notice(" Proxy %s:%u\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1604 | context->http_proxy_address, |
| 1605 | context->http_proxy_port); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1606 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1607 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1608 | #ifndef LWS_NO_SERVER |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1609 | if (port) { |
| 1610 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1611 | #ifdef LWS_OPENSSL_SUPPORT |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1612 | context->use_ssl = ssl_cert_filepath != NULL && |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1613 | ssl_private_key_filepath != NULL; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1614 | if (context->use_ssl) |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1615 | lwsl_notice(" Compiled with SSL support, using it\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1616 | else |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1617 | lwsl_notice(" Compiled with SSL support, not using it\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1618 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1619 | #else |
| 1620 | if (ssl_cert_filepath != NULL && |
| 1621 | ssl_private_key_filepath != NULL) { |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1622 | lwsl_notice(" Not compiled for OpenSSl support!\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1623 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1624 | } |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1625 | lwsl_notice(" Compiled without SSL support, " |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1626 | "serving unencrypted\n"); |
| 1627 | #endif |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 1628 | |
| 1629 | lwsl_notice(" per-connection allocation: %u + headers\n", sizeof(struct libwebsocket)); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1630 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1631 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1632 | |
| 1633 | /* ignore SIGPIPE */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1634 | #ifdef WIN32 |
| 1635 | #else |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1636 | signal(SIGPIPE, sigpipe_handler); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1637 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1638 | |
| 1639 | |
| 1640 | #ifdef LWS_OPENSSL_SUPPORT |
| 1641 | |
| 1642 | /* basic openssl init */ |
| 1643 | |
| 1644 | SSL_library_init(); |
| 1645 | |
| 1646 | OpenSSL_add_all_algorithms(); |
| 1647 | SSL_load_error_strings(); |
| 1648 | |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1649 | openssl_websocket_private_data_index = |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1650 | SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL); |
| 1651 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1652 | /* |
| 1653 | * Firefox insists on SSLv23 not SSLv3 |
| 1654 | * Konq disables SSLv2 by default now, SSLv23 works |
| 1655 | */ |
| 1656 | |
| 1657 | method = (SSL_METHOD *)SSLv23_server_method(); |
| 1658 | if (!method) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1659 | lwsl_err("problem creating ssl method: %s\n", |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1660 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1661 | return NULL; |
| 1662 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1663 | context->ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 1664 | if (!context->ssl_ctx) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1665 | lwsl_err("problem creating ssl context: %s\n", |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1666 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 1670 | #ifdef SSL_OP_NO_COMPRESSION |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 1671 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 1672 | #endif |
David Galeano | 77a677c | 2013-01-10 10:14:12 +0800 | [diff] [blame] | 1673 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); |
David Galeano | f177f2a | 2013-01-10 10:15:19 +0800 | [diff] [blame] | 1674 | SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 1675 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1676 | #ifndef LWS_NO_CLIENT |
| 1677 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1678 | /* client context */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1679 | |
| 1680 | if (port == CONTEXT_PORT_NO_LISTEN) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1681 | method = (SSL_METHOD *)SSLv23_client_method(); |
| 1682 | if (!method) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1683 | lwsl_err("problem creating ssl method: %s\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1684 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | /* create context */ |
| 1688 | context->ssl_client_ctx = SSL_CTX_new(method); |
| 1689 | if (!context->ssl_client_ctx) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1690 | lwsl_err("problem creating ssl context: %s\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1691 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 1692 | return NULL; |
| 1693 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1694 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 1695 | #ifdef SSL_OP_NO_COMPRESSION |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 1696 | SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 1697 | #endif |
David Galeano | 77a677c | 2013-01-10 10:14:12 +0800 | [diff] [blame] | 1698 | SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); |
David Galeano | f177f2a | 2013-01-10 10:15:19 +0800 | [diff] [blame] | 1699 | SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 1700 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1701 | /* openssl init for cert verification (for client sockets) */ |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 1702 | if (!ssl_ca_filepath) { |
| 1703 | if (!SSL_CTX_load_verify_locations( |
| 1704 | context->ssl_client_ctx, NULL, |
| 1705 | LWS_OPENSSL_CLIENT_CERTS)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1706 | lwsl_err( |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 1707 | "Unable to load SSL Client certs from %s " |
| 1708 | "(set by --with-client-cert-dir= in configure) -- " |
| 1709 | " client ssl isn't going to work", |
| 1710 | LWS_OPENSSL_CLIENT_CERTS); |
| 1711 | } else |
| 1712 | if (!SSL_CTX_load_verify_locations( |
| 1713 | context->ssl_client_ctx, ssl_ca_filepath, |
| 1714 | NULL)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1715 | lwsl_err( |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 1716 | "Unable to load SSL Client certs " |
| 1717 | "file from %s -- client ssl isn't " |
| 1718 | "going to work", ssl_ca_filepath); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1719 | |
| 1720 | /* |
| 1721 | * callback allowing user code to load extra verification certs |
| 1722 | * helping the client to verify server identity |
| 1723 | */ |
| 1724 | |
| 1725 | context->protocols[0].callback(context, NULL, |
| 1726 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
| 1727 | context->ssl_client_ctx, NULL, 0); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1728 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1729 | #endif |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1730 | |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1731 | /* as a server, are we requiring clients to identify themselves? */ |
| 1732 | |
| 1733 | if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) { |
| 1734 | |
| 1735 | /* absolutely require the client cert */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1736 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1737 | SSL_CTX_set_verify(context->ssl_ctx, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1738 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 1739 | OpenSSL_verify_callback); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1740 | |
| 1741 | /* |
| 1742 | * give user code a chance to load certs into the server |
| 1743 | * allowing it to verify incoming client certs |
| 1744 | */ |
| 1745 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1746 | context->protocols[0].callback(context, NULL, |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1747 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1748 | context->ssl_ctx, NULL, 0); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1751 | if (context->use_ssl) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1752 | |
| 1753 | /* openssl init for server sockets */ |
| 1754 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1755 | /* set the local certificate from CertFile */ |
David Galeano | 9b3d4b2 | 2013-01-10 10:11:21 +0800 | [diff] [blame] | 1756 | n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx, |
| 1757 | ssl_cert_filepath); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1758 | if (n != 1) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1759 | lwsl_err("problem getting cert '%s': %s\n", |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1760 | ssl_cert_filepath, |
| 1761 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1762 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1763 | } |
| 1764 | /* set the private key from KeyFile */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1765 | if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx, |
| 1766 | ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1767 | lwsl_err("ssl problem getting key '%s': %s\n", |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 1768 | ssl_private_key_filepath, |
| 1769 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1770 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1771 | } |
| 1772 | /* verify private key */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1773 | if (!SSL_CTX_check_private_key(context->ssl_ctx)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1774 | lwsl_err("Private SSL key doesn't match cert\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1775 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /* SSL is happy and has a cert it's content with */ |
| 1779 | } |
| 1780 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1781 | |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 1782 | /* selftest */ |
| 1783 | |
| 1784 | if (lws_b64_selftest()) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1785 | return NULL; |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 1786 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1787 | #ifndef LWS_NO_SERVER |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1788 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1789 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1790 | if (port) { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1791 | extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen); |
| 1792 | int sockfd; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1793 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1794 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 1795 | if (sockfd < 0) { |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 1796 | lwsl_err("ERROR opening socket\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1797 | return NULL; |
| 1798 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 1799 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1800 | /* allow us to restart even if old sockets in TIME_WAIT */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1801 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
| 1802 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 1803 | |
| 1804 | /* Disable Nagle */ |
| 1805 | opt = 1; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1806 | setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, |
| 1807 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 1808 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1809 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 1810 | serv_addr.sin_family = AF_INET; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1811 | if (interf == NULL) |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1812 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 1813 | else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1814 | interface_to_sa(interf, &serv_addr, |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1815 | sizeof(serv_addr)); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1816 | serv_addr.sin_port = htons(port); |
| 1817 | |
| 1818 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 1819 | sizeof(serv_addr)); |
| 1820 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1821 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1822 | port, n, errno); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1823 | close(sockfd); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1824 | return NULL; |
| 1825 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1826 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1827 | wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1828 | if (wsi == NULL) { |
| 1829 | lwsl_err("Out of mem\n"); |
| 1830 | close(sockfd); |
| 1831 | return NULL; |
| 1832 | } |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1833 | memset(wsi, 0, sizeof (struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1834 | wsi->sock = sockfd; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1835 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 1836 | wsi->count_active_extensions = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1837 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1838 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1839 | |
| 1840 | insert_wsi_socket_into_fds(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1841 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1842 | context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO; |
| 1843 | context->listen_service_count = 0; |
| 1844 | context->listen_service_fd = sockfd; |
| 1845 | |
Andy Green | a824d18 | 2013-01-15 20:52:29 +0800 | [diff] [blame] | 1846 | listen(sockfd, LWS_SOMAXCONN); |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1847 | lwsl_notice(" Listening on port %d\n", port); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1848 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1849 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1850 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1851 | /* |
| 1852 | * drop any root privs for this process |
| 1853 | * to listen on port < 1023 we would have needed root, but now we are |
| 1854 | * listening, we don't want the power for anything else |
| 1855 | */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1856 | #ifdef WIN32 |
| 1857 | #else |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1858 | if (gid != -1) |
| 1859 | if (setgid(gid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1860 | lwsl_warn("setgid: %s\n", strerror(errno)); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1861 | if (uid != -1) |
| 1862 | if (setuid(uid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1863 | lwsl_warn("setuid: %s\n", strerror(errno)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1864 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1865 | |
| 1866 | /* set up our internal broadcast trigger sockets per-protocol */ |
| 1867 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1868 | for (context->count_protocols = 0; |
| 1869 | protocols[context->count_protocols].callback; |
| 1870 | context->count_protocols++) { |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 1871 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1872 | lwsl_parser(" Protocol: %s\n", |
| 1873 | protocols[context->count_protocols].name); |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 1874 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1875 | protocols[context->count_protocols].owning_server = context; |
| 1876 | protocols[context->count_protocols].protocol_index = |
| 1877 | context->count_protocols; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1878 | |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 1879 | #ifndef LWS_NO_FORK |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1880 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 1881 | if (fd < 0) { |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 1882 | lwsl_err("ERROR opening socket\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1883 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1884 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1885 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1886 | /* allow us to restart even if old sockets in TIME_WAIT */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1887 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, |
| 1888 | sizeof(opt)); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1889 | |
| 1890 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 1891 | serv_addr.sin_family = AF_INET; |
| 1892 | serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 1893 | serv_addr.sin_port = 0; /* pick the port for us */ |
| 1894 | |
| 1895 | n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 1896 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1897 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1898 | port, n, errno); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1899 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | slen = sizeof cli_addr; |
| 1903 | n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen); |
| 1904 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1905 | lwsl_err("getsockname failed\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1906 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1907 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1908 | protocols[context->count_protocols].broadcast_socket_port = |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1909 | ntohs(cli_addr.sin_port); |
| 1910 | listen(fd, 5); |
| 1911 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1912 | lwsl_debug(" Protocol %s broadcast socket %d\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1913 | protocols[context->count_protocols].name, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1914 | ntohs(cli_addr.sin_port)); |
| 1915 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1916 | /* dummy wsi per broadcast proxy socket */ |
| 1917 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1918 | wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1919 | if (wsi == NULL) { |
| 1920 | lwsl_err("Out of mem\n"); |
| 1921 | close(fd); |
| 1922 | return NULL; |
| 1923 | } |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1924 | memset(wsi, 0, sizeof (struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1925 | wsi->sock = fd; |
| 1926 | wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1927 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 1928 | wsi->count_active_extensions = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1929 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1930 | /* note which protocol we are proxying */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1931 | wsi->protocol_index_for_broadcast_proxy = |
| 1932 | context->count_protocols; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1933 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1934 | insert_wsi_socket_into_fds(context, wsi); |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 1935 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1936 | } |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 1937 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1938 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1939 | /* |
| 1940 | * give all extensions a chance to create any per-context |
| 1941 | * allocations they need |
| 1942 | */ |
| 1943 | |
| 1944 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT; |
| 1945 | if (port) |
| 1946 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT; |
Andrew Chambers | d551217 | 2012-05-20 08:17:09 +0800 | [diff] [blame] | 1947 | |
| 1948 | if (extensions) { |
| 1949 | while (extensions->callback) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1950 | lwsl_ext(" Extension: %s\n", extensions->name); |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1951 | extensions->callback(context, extensions, NULL, |
| 1952 | (enum libwebsocket_extension_callback_reasons)m, |
| 1953 | NULL, NULL, 0); |
Andrew Chambers | d551217 | 2012-05-20 08:17:09 +0800 | [diff] [blame] | 1954 | extensions++; |
| 1955 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1956 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1957 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1958 | return context; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1959 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1960 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1961 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1962 | #ifndef LWS_NO_FORK |
| 1963 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1964 | /** |
| 1965 | * libwebsockets_fork_service_loop() - Optional helper function forks off |
| 1966 | * a process for the websocket server loop. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1967 | * You don't have to use this but if not, you |
| 1968 | * have to make sure you are calling |
| 1969 | * libwebsocket_service periodically to service |
| 1970 | * the websocket traffic |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1971 | * @context: server context returned by creation function |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1972 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1973 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1974 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1975 | libwebsockets_fork_service_loop(struct libwebsocket_context *context) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1976 | { |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1977 | int fd; |
| 1978 | struct sockaddr_in cli_addr; |
| 1979 | int n; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1980 | int p; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1981 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1982 | n = fork(); |
| 1983 | if (n < 0) |
| 1984 | return n; |
| 1985 | |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 1986 | if (n) { |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1987 | |
| 1988 | /* main process context */ |
| 1989 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1990 | /* |
| 1991 | * set up the proxy sockets to allow broadcast from |
| 1992 | * service process context |
| 1993 | */ |
| 1994 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1995 | for (p = 0; p < context->count_protocols; p++) { |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1996 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 1997 | if (fd < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1998 | lwsl_err("Unable to create socket\n"); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 1999 | return -1; |
| 2000 | } |
| 2001 | cli_addr.sin_family = AF_INET; |
| 2002 | cli_addr.sin_port = htons( |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2003 | context->protocols[p].broadcast_socket_port); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 2004 | cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 2005 | n = connect(fd, (struct sockaddr *)&cli_addr, |
| 2006 | sizeof cli_addr); |
| 2007 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2008 | lwsl_err("Unable to connect to " |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 2009 | "broadcast socket %d, %s\n", |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2010 | n, strerror(errno)); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 2011 | return -1; |
| 2012 | } |
| 2013 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2014 | context->protocols[p].broadcast_socket_user_fd = fd; |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2017 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 2020 | #ifdef HAVE_SYS_PRCTL_H |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2021 | /* we want a SIGHUP when our parent goes down */ |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 2022 | signal(SIGHUP, SIG_DFL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2023 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 2024 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2025 | |
| 2026 | /* in this forked process, sit and service websocket connections */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2027 | |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 2028 | while (1) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2029 | if (libwebsocket_service(context, 1000)) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 2030 | break; |
Andy Green | 5e8967a | 2012-10-17 20:10:44 +0800 | [diff] [blame] | 2031 | //#ifndef HAVE_SYS_PRCTL_H |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 2032 | /* |
| 2033 | * on systems without prctl() (i.e. anything but linux) we can notice that our |
| 2034 | * parent is dead if getppid() returns 1. FIXME apparently this is not true for |
| 2035 | * solaris, could remember ppid right after fork and wait for it to change. |
| 2036 | */ |
| 2037 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 2038 | /* if our parent went down, don't linger around */ |
| 2039 | if (context->started_with_parent && kill(context->started_with_parent, 0) < 0) |
| 2040 | kill(getpid(), SIGTERM); |
| 2041 | |
| 2042 | if (getppid() == 1) |
| 2043 | break; |
Andy Green | 5e8967a | 2012-10-17 20:10:44 +0800 | [diff] [blame] | 2044 | //#endif |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 2045 | } |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 2046 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2047 | |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 2048 | return 1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 2051 | #endif |
| 2052 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2053 | /** |
| 2054 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2055 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2056 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 2057 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2058 | * |
| 2059 | * 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] | 2060 | * the callback. |
| 2061 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 2062 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2063 | const struct libwebsocket_protocols * |
| 2064 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 2065 | { |
| 2066 | return wsi->protocol; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2070 | * libwebsockets_broadcast() - Sends a buffer to the callback for all active |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2071 | * connections of the given protocol. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2072 | * @protocol: pointer to the protocol you will broadcast to all members of |
| 2073 | * @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] | 2074 | * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before |
| 2075 | * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the |
| 2076 | * case you are calling this function from callback context. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2077 | * @len: length of payload data in buf, starting from buf. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2078 | * |
| 2079 | * This function allows bulk sending of a packet to every connection using |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2080 | * the given protocol. It does not send the data directly; instead it calls |
| 2081 | * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback |
| 2082 | * wants to actually send the data for that connection, the callback itself |
| 2083 | * should call libwebsocket_write(). |
| 2084 | * |
| 2085 | * libwebsockets_broadcast() can be called from another fork context without |
| 2086 | * having to take any care about data visibility between the processes, it'll |
| 2087 | * "just work". |
| 2088 | */ |
| 2089 | |
| 2090 | |
| 2091 | int |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2092 | libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2093 | unsigned char *buf, size_t len) |
| 2094 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2095 | struct libwebsocket_context *context = protocol->owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2096 | int n; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2097 | struct libwebsocket *wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2098 | |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 2099 | if (!context) |
| 2100 | return 1; |
| 2101 | |
| 2102 | #ifndef LWS_NO_FORK |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2103 | if (!protocol->broadcast_socket_user_fd) { |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 2104 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2105 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2106 | * We are either running unforked / flat, or we are being |
| 2107 | * called from poll thread context |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2108 | * eg, from a callback. In that case don't use sockets for |
| 2109 | * broadcast IPC (since we can't open a socket connection to |
| 2110 | * a socket listening on our own thread) but directly do the |
| 2111 | * send action. |
| 2112 | * |
| 2113 | * Locking is not needed because we are by definition being |
| 2114 | * called in the poll thread context and are serialized. |
| 2115 | */ |
| 2116 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2117 | for (n = 0; n < context->fds_count; n++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2118 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2119 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 2120 | if (!wsi) |
| 2121 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2122 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2123 | if (wsi->mode != LWS_CONNMODE_WS_SERVING) |
| 2124 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2125 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2126 | /* |
| 2127 | * never broadcast to non-established connections |
| 2128 | */ |
| 2129 | if (wsi->state != WSI_STATE_ESTABLISHED) |
| 2130 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2131 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2132 | /* only broadcast to guys using |
| 2133 | * requested protocol |
| 2134 | */ |
| 2135 | if (wsi->protocol != protocol) |
| 2136 | continue; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2137 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 2138 | user_callback_handle_rxflow(wsi->protocol->callback, |
| 2139 | context, wsi, |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2140 | LWS_CALLBACK_BROADCAST, |
| 2141 | wsi->user_space, |
| 2142 | buf, len); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | return 0; |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 2146 | #ifndef LWS_NO_FORK |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 2149 | /* |
| 2150 | * We're being called from a different process context than the server |
| 2151 | * loop. Instead of broadcasting directly, we send our |
| 2152 | * payload on a socket to do the IPC; the server process will serialize |
| 2153 | * the broadcast action in its main poll() loop. |
| 2154 | * |
| 2155 | * There's one broadcast socket listening for each protocol supported |
| 2156 | * set up when the websocket server initializes |
| 2157 | */ |
| 2158 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2159 | n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2160 | |
| 2161 | return n; |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 2162 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2163 | } |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 2164 | |
| 2165 | int |
| 2166 | libwebsocket_is_final_fragment(struct libwebsocket *wsi) |
| 2167 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 2168 | return wsi->u.ws.final; |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 2169 | } |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2170 | |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 2171 | unsigned char |
| 2172 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi) |
| 2173 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 2174 | return wsi->u.ws.rsv; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2177 | void * |
| 2178 | libwebsocket_ensure_user_space(struct libwebsocket *wsi) |
| 2179 | { |
| 2180 | /* allocate the per-connection user memory (if any) */ |
| 2181 | |
| 2182 | if (wsi->protocol->per_session_data_size && !wsi->user_space) { |
| 2183 | wsi->user_space = malloc( |
| 2184 | wsi->protocol->per_session_data_size); |
| 2185 | if (wsi->user_space == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2186 | lwsl_err("Out of memory for conn user space\n"); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2187 | return NULL; |
| 2188 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2189 | memset(wsi->user_space, 0, |
| 2190 | wsi->protocol->per_session_data_size); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2191 | } |
| 2192 | return wsi->user_space; |
| 2193 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2194 | |
Andy Green | acbaee6 | 2013-01-18 22:00:22 +0800 | [diff] [blame] | 2195 | /** |
| 2196 | * lws_confirm_legit_wsi: returns nonzero if the wsi looks bad |
| 2197 | * |
| 2198 | * @wsi: struct libwebsocket to assess |
| 2199 | * |
| 2200 | * Performs consistecy checks on what the wsi claims and what the |
| 2201 | * polling arrays hold. This'll catch a closed wsi still in use. |
| 2202 | * Don't try to use on the listen (nonconnection) wsi as it will |
| 2203 | * fail it. Otherwise 0 return == wsi seems consistent. |
| 2204 | */ |
| 2205 | |
| 2206 | int lws_confirm_legit_wsi(struct libwebsocket *wsi) |
| 2207 | { |
| 2208 | struct libwebsocket_context *context; |
| 2209 | |
| 2210 | if (!(wsi && wsi->protocol && wsi->protocol->owning_server)) |
| 2211 | return 1; |
| 2212 | |
| 2213 | context = wsi->protocol->owning_server; |
| 2214 | |
| 2215 | if (!context) |
| 2216 | return 2; |
| 2217 | |
| 2218 | if (!wsi->position_in_fds_table) |
| 2219 | return 3; /* position in fds table looks bad */ |
| 2220 | if (context->fds[wsi->position_in_fds_table].fd != wsi->sock) |
| 2221 | return 4; /* pollfd entry does not wait on our socket descriptor */ |
| 2222 | if (context->lws_lookup[wsi->sock] != wsi) |
| 2223 | return 5; /* lookup table does not agree with wsi */ |
| 2224 | |
| 2225 | return 0; |
| 2226 | } |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2227 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2228 | static void lwsl_emit_stderr(int level, const char *line) |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2229 | { |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2230 | char buf[300]; |
| 2231 | struct timeval tv; |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2232 | int n; |
| 2233 | |
| 2234 | gettimeofday(&tv, NULL); |
| 2235 | |
| 2236 | buf[0] = '\0'; |
| 2237 | for (n = 0; n < LLL_COUNT; n++) |
| 2238 | if (level == (1 << n)) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 2239 | sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec, |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2240 | (int)(tv.tv_usec / 100), log_level_names[n]); |
| 2241 | break; |
| 2242 | } |
| 2243 | |
| 2244 | fprintf(stderr, "%s%s", buf, line); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2245 | } |
| 2246 | |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2247 | void lwsl_emit_syslog(int level, const char *line) |
| 2248 | { |
| 2249 | int syslog_level = LOG_DEBUG; |
| 2250 | |
| 2251 | switch (level) { |
| 2252 | case LLL_ERR: |
| 2253 | syslog_level = LOG_ERR; |
| 2254 | break; |
| 2255 | case LLL_WARN: |
| 2256 | syslog_level = LOG_WARNING; |
| 2257 | break; |
| 2258 | case LLL_NOTICE: |
| 2259 | syslog_level = LOG_NOTICE; |
| 2260 | break; |
| 2261 | case LLL_INFO: |
| 2262 | syslog_level = LOG_INFO; |
| 2263 | break; |
| 2264 | } |
Edwin van den Oetelaar | f6eeabc | 2013-01-19 20:01:01 +0800 | [diff] [blame] | 2265 | syslog(syslog_level, "%s", line); |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2266 | } |
| 2267 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2268 | void _lws_log(int filter, const char *format, ...) |
| 2269 | { |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2270 | char buf[256]; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2271 | va_list ap; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2272 | |
| 2273 | if (!(log_level & filter)) |
| 2274 | return; |
| 2275 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2276 | va_start(ap, format); |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2277 | vsnprintf(buf, (sizeof buf), format, ap); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2278 | buf[(sizeof buf) - 1] = '\0'; |
| 2279 | va_end(ap); |
| 2280 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2281 | lwsl_emit(filter, buf); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | /** |
| 2285 | * lws_set_log_level() - Set the logging bitfield |
| 2286 | * @level: OR together the LLL_ debug contexts you want output from |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2287 | * @log_emit_function: NULL to leave it as it is, or a user-supplied |
| 2288 | * function to perform log string emission instead of |
| 2289 | * the default stderr one. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2290 | * |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2291 | * log level defaults to "err" and "warn" contexts enabled only and |
| 2292 | * emission on stderr. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2293 | */ |
| 2294 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 2295 | void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2296 | { |
| 2297 | log_level = level; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2298 | if (log_emit_function) |
| 2299 | lwsl_emit = log_emit_function; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2300 | } |