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