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 | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com> |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 22 | #include "private-libwebsockets.h" |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 23 | |
Andy Green | 3d67f51 | 2014-04-03 07:29:50 +0800 | [diff] [blame] | 24 | int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE; |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 25 | static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr; |
| 26 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 27 | static const char * const log_level_names[] = { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 28 | "ERR", |
| 29 | "WARN", |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 30 | "NOTICE", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 31 | "INFO", |
| 32 | "DEBUG", |
| 33 | "PARSER", |
| 34 | "HEADER", |
| 35 | "EXTENSION", |
| 36 | "CLIENT", |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 37 | "LATENCY", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 38 | }; |
| 39 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 40 | void |
=?UTF-8?q?Joakim=20S=C3=B6derberg?= | caf7e3d | 2015-06-25 17:51:07 +0200 | [diff] [blame] | 41 | lws_free_wsi(struct libwebsocket *wsi) |
| 42 | { |
| 43 | if (!wsi) |
| 44 | return; |
| 45 | |
| 46 | /* Protocol user data may be allocated either internally by lws |
| 47 | * or by specified the user. Important we don't free external user data */ |
| 48 | if (wsi->protocol && wsi->protocol->per_session_data_size |
| 49 | && wsi->user_space && !wsi->user_space_externally_allocated) { |
| 50 | lws_free(wsi->user_space); |
| 51 | } |
| 52 | |
| 53 | lws_free2(wsi->rxflow_buffer); |
| 54 | lws_free2(wsi->truncated_send_malloc); |
| 55 | |
| 56 | // TODO: Probably should handle the union structs in wsi->u here depending |
| 57 | // on connection mode as well. Too spaghetti for me to follow however... |
| 58 | |
| 59 | lws_free_header_table(wsi); |
| 60 | lws_free(wsi); |
| 61 | } |
| 62 | |
| 63 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 64 | libwebsocket_close_and_free_session(struct libwebsocket_context *context, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 65 | struct libwebsocket *wsi, enum lws_close_status reason) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 66 | { |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 67 | int n, m, ret; |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 68 | int old_state; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 69 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 70 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 71 | struct lws_tokens eff_buf; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 72 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 73 | if (!wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 74 | return; |
| 75 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 76 | old_state = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 77 | |
Andy Green | 408f537 | 2015-04-12 08:17:26 +0800 | [diff] [blame] | 78 | if (wsi->socket_is_permanently_unusable || |
| 79 | reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY) |
Andy Green | 1e49918 | 2014-10-16 08:23:46 +0800 | [diff] [blame] | 80 | goto just_kill_connection; |
| 81 | |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 82 | switch (old_state) { |
| 83 | case WSI_STATE_DEAD_SOCKET: |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 84 | return; |
| 85 | |
Andy Green | 22524a6 | 2013-02-15 22:48:58 +0800 | [diff] [blame] | 86 | /* we tried the polite way... */ |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 87 | case WSI_STATE_AWAITING_CLOSE_ACK: |
Andy Green | 22524a6 | 2013-02-15 22:48:58 +0800 | [diff] [blame] | 88 | goto just_kill_connection; |
| 89 | |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 90 | case WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE: |
| 91 | if (wsi->truncated_send_len) { |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 92 | lws_callback_on_writable(context, wsi); |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | lwsl_info("wsi %p completed WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi); |
| 96 | goto just_kill_connection; |
| 97 | default: |
| 98 | if (wsi->truncated_send_len) { |
| 99 | lwsl_info("wsi %p entering WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi); |
| 100 | wsi->state = WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE; |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 101 | lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5); |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 107 | wsi->u.ws.close_reason = reason; |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 108 | |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 109 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT || |
Andy Green | 0c8a854 | 2015-10-28 19:50:41 +0800 | [diff] [blame] | 110 | wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 111 | goto just_kill_connection; |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 112 | |
Drew Noakes | 6a77977 | 2015-01-28 04:15:13 +0800 | [diff] [blame] | 113 | if (wsi->mode == LWS_CONNMODE_HTTP_SERVING) |
| 114 | context->protocols[0].callback(context, wsi, |
| 115 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0); |
| 116 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 117 | if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) { |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 118 | if (wsi->u.http.fd != LWS_INVALID_FILE) { |
=?UTF-8?q?Joakim=20S=C3=B6derberg?= | caf7e3d | 2015-06-25 17:51:07 +0200 | [diff] [blame] | 119 | // TODO: If we're just closing with LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY this file descriptor might leak? |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 120 | lwsl_debug("closing http file\n"); |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 121 | compatible_file_close(wsi->u.http.fd); |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 122 | wsi->u.http.fd = LWS_INVALID_FILE; |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 123 | context->protocols[0].callback(context, wsi, |
| 124 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0); |
| 125 | } |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 128 | /* |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 129 | * are his extensions okay with him closing? Eg he might be a mux |
| 130 | * parent and just his ch1 aspect is closing? |
| 131 | */ |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 132 | |
| 133 | if (lws_ext_callback_for_each_active(wsi, |
| 134 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) { |
| 135 | lwsl_ext("extension vetoed close\n"); |
| 136 | return; |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 139 | /* |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 140 | * flush any tx pending from extensions, since we may send close packet |
| 141 | * if there are problems with send, just nuke the connection |
| 142 | */ |
| 143 | |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 144 | do { |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 145 | ret = 0; |
| 146 | eff_buf.token = NULL; |
| 147 | eff_buf.token_len = 0; |
| 148 | |
| 149 | /* show every extension the new incoming data */ |
| 150 | |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 151 | m = lws_ext_callback_for_each_active(wsi, |
| 152 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, &eff_buf, 0); |
| 153 | if (m < 0) { |
| 154 | lwsl_ext("Extension reports fatal error\n"); |
| 155 | goto just_kill_connection; |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 156 | } |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 157 | if (m) |
| 158 | /* |
| 159 | * at least one extension told us he has more |
| 160 | * to spill, so we will go around again after |
| 161 | */ |
| 162 | ret = 1; |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 163 | |
| 164 | /* assuming they left us something to send, send it */ |
| 165 | |
| 166 | if (eff_buf.token_len) |
| 167 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 168 | eff_buf.token_len) != eff_buf.token_len) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 169 | lwsl_debug("close: ext spill failed\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 170 | goto just_kill_connection; |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 171 | } |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 172 | } while (ret); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 175 | * signal we are closing, lws_write will |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 176 | * add any necessary version-specific stuff. If the write fails, |
| 177 | * no worries we are closing anyway. If we didn't initiate this |
| 178 | * close, then our state has been changed to |
| 179 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this. |
| 180 | * |
| 181 | * Likewise if it's a second call to close this connection after we |
| 182 | * sent the close indication to the peer already, we are in state |
| 183 | * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time. |
| 184 | */ |
| 185 | |
| 186 | if (old_state == WSI_STATE_ESTABLISHED && |
Andy Green | 8ec7802 | 2015-03-25 01:53:58 +0800 | [diff] [blame] | 187 | reason != LWS_CLOSE_STATUS_NOSTATUS && |
| 188 | reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY) { |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 189 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 190 | lwsl_debug("sending close indication...\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 191 | |
Andy Green | fdd305a | 2013-02-11 14:32:48 +0800 | [diff] [blame] | 192 | /* make valgrind happy */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 193 | memset(buf, 0, sizeof(buf)); |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 194 | n = lws_write(wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 195 | &buf[LWS_SEND_BUFFER_PRE_PADDING + 2], |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 196 | 0, LWS_WRITE_CLOSE); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 197 | if (n >= 0) { |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 198 | /* |
| 199 | * we have sent a nice protocol level indication we |
| 200 | * now wish to close, we should not send anything more |
| 201 | */ |
| 202 | |
| 203 | wsi->state = WSI_STATE_AWAITING_CLOSE_ACK; |
| 204 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 205 | /* |
| 206 | * ...and we should wait for a reply for a bit |
| 207 | * out of politeness |
| 208 | */ |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 209 | |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 210 | lws_set_timeout(wsi, |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 211 | PENDING_TIMEOUT_CLOSE_ACK, 1); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 212 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 213 | lwsl_debug("sent close indication, awaiting ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 214 | |
| 215 | return; |
| 216 | } |
| 217 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 218 | lwsl_info("close: sending close packet failed, hanging up\n"); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 219 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 220 | /* else, the send failed and we should just hang up */ |
| 221 | } |
| 222 | |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 223 | just_kill_connection: |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 224 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 225 | lwsl_debug("close: just_kill_connection\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 226 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 227 | /* |
| 228 | * we won't be servicing or receiving anything further from this guy |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 229 | * delete socket from the internal poll list if still present |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 230 | */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 231 | |
Andy Green | 5281560 | 2015-01-29 08:36:18 +0800 | [diff] [blame] | 232 | lws_ssl_remove_wsi_from_buffered_list(context, wsi); |
| 233 | |
Andy Green | 1963c9a | 2015-10-15 07:39:33 +0800 | [diff] [blame] | 234 | // checking return redundant since we anyway close |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 235 | remove_wsi_socket_from_fds(context, wsi); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 236 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 237 | wsi->state = WSI_STATE_DEAD_SOCKET; |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 238 | |
| 239 | lws_free2(wsi->rxflow_buffer); |
=?UTF-8?q?Joakim=20S=C3=B6derberg?= | caf7e3d | 2015-06-25 17:51:07 +0200 | [diff] [blame] | 240 | lws_free_header_table(wsi); |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 241 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 242 | if ((old_state == WSI_STATE_ESTABLISHED || |
| 243 | wsi->mode == LWS_CONNMODE_WS_SERVING || |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 244 | wsi->mode == LWS_CONNMODE_WS_CLIENT)) { |
| 245 | |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 246 | lws_free2(wsi->u.ws.rx_user_buffer); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 247 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 248 | if (wsi->truncated_send_malloc) { |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 249 | /* not going to be completed... nuke it */ |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 250 | lws_free2(wsi->truncated_send_malloc); |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 251 | wsi->truncated_send_len = 0; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 252 | } |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 253 | if (wsi->u.ws.ping_payload_buf) { |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 254 | lws_free2(wsi->u.ws.ping_payload_buf); |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 255 | wsi->u.ws.ping_payload_alloc = 0; |
| 256 | wsi->u.ws.ping_payload_len = 0; |
Andy Green | 2fd6e6f | 2015-03-24 21:07:01 +0800 | [diff] [blame] | 257 | wsi->u.ws.ping_pending_flag = 0; |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 258 | } |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 261 | /* tell the user it's all over for this guy */ |
| 262 | |
Andy Green | d430273 | 2011-02-28 07:45:29 +0000 | [diff] [blame] | 263 | if (wsi->protocol && wsi->protocol->callback && |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 264 | ((old_state == WSI_STATE_ESTABLISHED) || |
| 265 | (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || |
Stefan Schuermans | b944cc1 | 2014-09-08 14:01:58 +0800 | [diff] [blame] | 266 | (old_state == WSI_STATE_AWAITING_CLOSE_ACK) || |
| 267 | (old_state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE))) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 268 | lwsl_debug("calling back CLOSED\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 269 | wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 270 | wsi->user_space, NULL, 0); |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 271 | } else if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) { |
Niall T. Davidson | db761be | 2013-06-29 10:16:18 +0800 | [diff] [blame] | 272 | lwsl_debug("calling back CLOSED_HTTP\n"); |
| 273 | context->protocols[0].callback(context, wsi, |
| 274 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 ); |
Andy Green | b7a721f | 2015-04-27 19:07:38 +0800 | [diff] [blame] | 275 | } else if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY || |
| 276 | wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT) { |
Andy Green | bb90f9b | 2015-04-16 19:55:42 +0800 | [diff] [blame] | 277 | lwsl_debug("Connection closed before server reply\n"); |
| 278 | context->protocols[0].callback(context, wsi, |
| 279 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
| 280 | wsi->user_space, NULL, 0 ); |
Andy Green | cc01247 | 2011-11-07 19:53:23 +0800 | [diff] [blame] | 281 | } else |
Andy Green | bb90f9b | 2015-04-16 19:55:42 +0800 | [diff] [blame] | 282 | lwsl_debug("not calling back closed mode=%d state=%d\n", |
| 283 | wsi->mode, old_state); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 284 | |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 285 | /* deallocate any active extension contexts */ |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 286 | |
| 287 | if (lws_ext_callback_for_each_active(wsi, LWS_EXT_CALLBACK_DESTROY, NULL, 0) < 0) |
| 288 | lwsl_warn("extension destruction failed\n"); |
| 289 | #ifndef LWS_NO_EXTENSIONS |
| 290 | for (n = 0; n < wsi->count_active_extensions; n++) |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 291 | lws_free(wsi->active_extensions_user[n]); |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 292 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 293 | /* |
| 294 | * inform all extensions in case they tracked this guy out of band |
| 295 | * even though not active on him specifically |
| 296 | */ |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 297 | if (lws_ext_callback_for_each_extension_type(context, wsi, |
| 298 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0) |
| 299 | lwsl_warn("ext destroy wsi failed\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 300 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 301 | /* lwsl_info("closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 302 | |
Andy Green | fc772cc | 2015-11-14 13:48:58 +0800 | [diff] [blame] | 303 | if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) { |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 304 | #if LWS_POSIX |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 305 | n = shutdown(wsi->sock, SHUT_RDWR); |
| 306 | if (n) |
| 307 | lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 308 | |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 309 | n = compatible_close(wsi->sock); |
| 310 | if (n) |
| 311 | lwsl_debug("closing: close ret %d\n", LWS_ERRNO); |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 312 | |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 313 | #else |
| 314 | compatible_close(wsi->sock); |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 315 | #endif |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 316 | wsi->sock = LWS_SOCK_INVALID; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 317 | } |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame] | 318 | |
| 319 | /* outermost destroy notification for wsi (user_space still intact) */ |
| 320 | context->protocols[0].callback(context, wsi, |
| 321 | LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0); |
| 322 | |
=?UTF-8?q?Joakim=20S=C3=B6derberg?= | caf7e3d | 2015-06-25 17:51:07 +0200 | [diff] [blame] | 323 | lws_free_wsi(wsi); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 326 | LWS_VISIBLE int |
| 327 | libwebsockets_get_addresses(struct libwebsocket_context *context, |
| 328 | void *ads, char *name, int name_len, |
| 329 | char *rip, int rip_len) |
| 330 | { |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 331 | #if LWS_POSIX |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 332 | struct addrinfo ai, *res; |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 333 | struct sockaddr_in addr4; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 334 | |
| 335 | rip[0] = '\0'; |
| 336 | name[0] = '\0'; |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 337 | addr4.sin_family = AF_UNSPEC; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 338 | |
| 339 | #ifdef LWS_USE_IPV6 |
| 340 | if (LWS_IPV6_ENABLED(context)) { |
| 341 | if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) { |
| 342 | lwsl_err("inet_ntop", strerror(LWS_ERRNO)); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | // Strip off the IPv4 to IPv6 header if one exists |
| 347 | if (strncmp(rip, "::ffff:", 7) == 0) |
| 348 | memmove(rip, rip + 7, strlen(rip) - 6); |
| 349 | |
| 350 | getnameinfo((struct sockaddr *)ads, |
| 351 | sizeof(struct sockaddr_in6), name, |
| 352 | name_len, NULL, 0, 0); |
| 353 | |
| 354 | return 0; |
| 355 | } else |
| 356 | #endif |
| 357 | { |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 358 | struct addrinfo *result; |
| 359 | |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 360 | memset(&ai, 0, sizeof ai); |
| 361 | ai.ai_family = PF_UNSPEC; |
| 362 | ai.ai_socktype = SOCK_STREAM; |
| 363 | ai.ai_flags = AI_CANONNAME; |
| 364 | |
| 365 | if (getnameinfo((struct sockaddr *)ads, |
| 366 | sizeof(struct sockaddr_in), |
| 367 | name, name_len, NULL, 0, 0)) |
| 368 | return -1; |
| 369 | |
| 370 | if (!rip) |
| 371 | return 0; |
| 372 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 373 | if (getaddrinfo(name, NULL, &ai, &result)) |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 374 | return -1; |
| 375 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 376 | res = result; |
| 377 | while (addr4.sin_family == AF_UNSPEC && res) { |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 378 | switch (res->ai_family) { |
| 379 | case AF_INET: |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 380 | addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr; |
| 381 | addr4.sin_family = AF_INET; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | |
| 385 | res = res->ai_next; |
| 386 | } |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 387 | freeaddrinfo(result); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 388 | } |
| 389 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 390 | if (addr4.sin_family == AF_UNSPEC) |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 391 | return -1; |
| 392 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 393 | lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 394 | |
| 395 | return 0; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 396 | #else |
| 397 | (void)context; |
| 398 | (void)ads; |
| 399 | (void)name; |
| 400 | (void)name_len; |
| 401 | (void)rip; |
| 402 | (void)rip_len; |
| 403 | |
| 404 | return -1; |
| 405 | #endif |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 406 | } |
| 407 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 408 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 409 | * lws_get_peer_addresses() - Get client address information |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 410 | * @context: Libwebsockets context |
| 411 | * @wsi: Local struct libwebsocket associated with |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 412 | * @fd: Connection socket descriptor |
| 413 | * @name: Buffer to take client address name |
| 414 | * @name_len: Length of client address name buffer |
Drew Noakes | efe10b0 | 2014-10-23 15:34:26 +0100 | [diff] [blame] | 415 | * @rip: Buffer to take client address IP dotted quad |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 416 | * @rip_len: Length of client address IP buffer |
| 417 | * |
| 418 | * 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] | 419 | * the client connected with socket descriptor @fd. Names may be |
| 420 | * truncated if there is not enough room. If either cannot be |
| 421 | * determined, they will be returned as valid zero-length strings. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 422 | */ |
| 423 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 424 | LWS_VISIBLE void |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 425 | lws_get_peer_addresses(struct libwebsocket_context *context, |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 426 | struct libwebsocket *wsi, lws_sockfd_type fd, char *name, int name_len, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 427 | char *rip, int rip_len) |
| 428 | { |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 429 | #if LWS_POSIX |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 430 | socklen_t len; |
Andy Green | 055f297 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 431 | #ifdef LWS_USE_IPV6 |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 432 | struct sockaddr_in6 sin6; |
| 433 | #endif |
| 434 | struct sockaddr_in sin4; |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 435 | int ret = -1; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 436 | void *p; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 437 | |
| 438 | rip[0] = '\0'; |
| 439 | name[0] = '\0'; |
| 440 | |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 441 | lws_latency_pre(context, wsi); |
| 442 | |
Andy Green | 055f297 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 443 | #ifdef LWS_USE_IPV6 |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 444 | if (LWS_IPV6_ENABLED(context)) { |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 445 | len = sizeof(sin6); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 446 | p = &sin6; |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 447 | } else |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 448 | #endif |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 449 | { |
| 450 | len = sizeof(sin4); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 451 | p = &sin4; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 452 | } |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 453 | |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 454 | if (getpeername(fd, p, &len) < 0) { |
| 455 | lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO)); |
| 456 | goto bail; |
| 457 | } |
| 458 | |
| 459 | ret = libwebsockets_get_addresses(context, p, name, name_len, rip, rip_len); |
| 460 | |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 461 | bail: |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 462 | lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1); |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 463 | #else |
| 464 | (void)context; |
| 465 | (void)wsi; |
| 466 | (void)fd; |
| 467 | (void)name; |
| 468 | (void)name_len; |
| 469 | (void)rip; |
| 470 | (void)rip_len; |
| 471 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 472 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 473 | |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 474 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 475 | * lws_context_user() - get the user data associated with the context |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 476 | * @context: Websocket context |
| 477 | * |
| 478 | * This returns the optional user allocation that can be attached to |
| 479 | * the context the sockets live in at context_create time. It's a way |
| 480 | * to let all sockets serviced in the same context share data without |
| 481 | * using globals statics in the user code. |
| 482 | */ |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 483 | LWS_EXTERN void * |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 484 | lws_context_user(struct libwebsocket_context *context) |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 485 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 486 | return context->user_space; |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 489 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 490 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 491 | * lws_callback_all_protocol() - Callback all connections using |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 492 | * the given protocol with the given reason |
| 493 | * |
| 494 | * @protocol: Protocol whose connections will get callbacks |
| 495 | * @reason: Callback reason index |
| 496 | */ |
| 497 | |
| 498 | LWS_VISIBLE int |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 499 | lws_callback_all_protocol( |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 500 | const struct libwebsocket_protocols *protocol, int reason) |
| 501 | { |
| 502 | struct libwebsocket_context *context = protocol->owning_server; |
| 503 | int n; |
| 504 | struct libwebsocket *wsi; |
| 505 | |
| 506 | for (n = 0; n < context->fds_count; n++) { |
Bud Davis | 229bfec | 2015-01-30 10:13:01 +0800 | [diff] [blame] | 507 | wsi = wsi_from_fd(context, context->fds[n].fd); |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 508 | if (!wsi) |
| 509 | continue; |
| 510 | if (wsi->protocol == protocol) |
| 511 | protocol->callback(context, wsi, |
| 512 | reason, wsi->user_space, NULL, 0); |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 519 | * lws_set_timeout() - marks the wsi as subject to a timeout |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 520 | * |
| 521 | * You will not need this unless you are doing something special |
| 522 | * |
| 523 | * @wsi: Websocket connection instance |
| 524 | * @reason: timeout reason |
| 525 | * @secs: how many seconds |
| 526 | */ |
| 527 | |
Craig McQueen | e7fd8b1 | 2014-02-19 09:24:17 +1100 | [diff] [blame] | 528 | LWS_VISIBLE void |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 529 | lws_set_timeout(struct libwebsocket *wsi, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 530 | enum pending_timeout reason, int secs) |
| 531 | { |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 532 | time_t now; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 533 | |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 534 | time(&now); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 535 | |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 536 | wsi->pending_timeout_limit = now + secs; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 537 | wsi->pending_timeout = reason; |
| 538 | } |
| 539 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 540 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 541 | #if LWS_POSIX |
| 542 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 543 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 544 | * lws_get_socket_fd() - returns the socket file descriptor |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 545 | * |
| 546 | * You will not need this unless you are doing something special |
| 547 | * |
| 548 | * @wsi: Websocket connection instance |
| 549 | */ |
| 550 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 551 | LWS_VISIBLE int |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 552 | lws_get_socket_fd(struct libwebsocket *wsi) |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 553 | { |
| 554 | return wsi->sock; |
| 555 | } |
| 556 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 557 | #endif |
| 558 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 559 | #ifdef LWS_LATENCY |
| 560 | void |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 561 | lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, |
| 562 | const char *action, int ret, int completed) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 563 | { |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 564 | unsigned long long u; |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 565 | char buf[256]; |
| 566 | |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 567 | u = time_in_microseconds(); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 568 | |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 569 | if (!action) { |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 570 | wsi->latency_start = u; |
| 571 | if (!wsi->action_start) |
| 572 | wsi->action_start = u; |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 573 | return; |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 574 | } |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 575 | if (completed) { |
| 576 | if (wsi->action_start == wsi->latency_start) |
| 577 | sprintf(buf, |
Cong Ma | 10da117 | 2014-07-05 10:50:47 +0800 | [diff] [blame] | 578 | "Completion first try lat %lluus: %p: ret %d: %s\n", |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 579 | u - wsi->latency_start, |
| 580 | (void *)wsi, ret, action); |
| 581 | else |
| 582 | sprintf(buf, |
Cong Ma | 10da117 | 2014-07-05 10:50:47 +0800 | [diff] [blame] | 583 | "Completion %lluus: lat %lluus: %p: ret %d: %s\n", |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 584 | u - wsi->action_start, |
| 585 | u - wsi->latency_start, |
| 586 | (void *)wsi, ret, action); |
| 587 | wsi->action_start = 0; |
| 588 | } else |
Cong Ma | 10da117 | 2014-07-05 10:50:47 +0800 | [diff] [blame] | 589 | sprintf(buf, "lat %lluus: %p: ret %d: %s\n", |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 590 | u - wsi->latency_start, (void *)wsi, ret, action); |
| 591 | |
| 592 | if (u - wsi->latency_start > context->worst_latency) { |
| 593 | context->worst_latency = u - wsi->latency_start; |
| 594 | strcpy(context->worst_latency_info, buf); |
| 595 | } |
| 596 | lwsl_latency("%s", buf); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 597 | } |
| 598 | #endif |
| 599 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 600 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 601 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 602 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 603 | * lws_rx_flow_control() - Enable and disable socket servicing for |
Drew Noakes | efe10b0 | 2014-10-23 15:34:26 +0100 | [diff] [blame] | 604 | * received packets. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 605 | * |
| 606 | * If the output side of a server process becomes choked, this allows flow |
| 607 | * control for the input side. |
| 608 | * |
| 609 | * @wsi: Websocket connection instance to get callback for |
| 610 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 611 | */ |
| 612 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 613 | LWS_VISIBLE int |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 614 | lws_rx_flow_control(struct libwebsocket *wsi, int enable) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 615 | { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 616 | if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 617 | return 0; |
| 618 | |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 619 | lwsl_info("lws_rx_flow_control(0x%p, %d)\n", wsi, enable); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 620 | wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 621 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 622 | return 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 625 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 626 | * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 627 | * |
| 628 | * When the user server code realizes it can accept more input, it can |
| 629 | * call this to have the RX flow restriction removed from all connections using |
| 630 | * the given protocol. |
| 631 | * |
| 632 | * @protocol: all connections using this protocol will be allowed to receive |
| 633 | */ |
| 634 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 635 | LWS_VISIBLE void |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 636 | lws_rx_flow_allow_all_protocol( |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 637 | const struct libwebsocket_protocols *protocol) |
| 638 | { |
| 639 | struct libwebsocket_context *context = protocol->owning_server; |
| 640 | int n; |
| 641 | struct libwebsocket *wsi; |
| 642 | |
| 643 | for (n = 0; n < context->fds_count; n++) { |
Bud Davis | 229bfec | 2015-01-30 10:13:01 +0800 | [diff] [blame] | 644 | wsi = wsi_from_fd(context, context->fds[n].fd); |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 645 | if (!wsi) |
| 646 | continue; |
| 647 | if (wsi->protocol == protocol) |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 648 | lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW); |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 652 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 653 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 654 | * lws_canonical_hostname() - returns this host's hostname |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 655 | * |
| 656 | * This is typically used by client code to fill in the host parameter |
| 657 | * when making a client connection. You can only call it after the context |
| 658 | * has been created. |
| 659 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 660 | * @context: Websocket context |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 661 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 662 | LWS_VISIBLE extern const char * |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 663 | lws_canonical_hostname(struct libwebsocket_context *context) |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 664 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 665 | return (const char *)context->canonical_hostname; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 668 | int user_callback_handle_rxflow(callback_function callback_function, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 669 | struct libwebsocket_context *context, |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 670 | struct libwebsocket *wsi, |
| 671 | enum libwebsocket_callback_reasons reason, void *user, |
| 672 | void *in, size_t len) |
| 673 | { |
| 674 | int n; |
| 675 | |
| 676 | n = callback_function(context, wsi, reason, user, in, len); |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 677 | if (!n) |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 678 | n = _lws_rx_flow_control(wsi); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 679 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 680 | return n; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 681 | } |
| 682 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 683 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 684 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 685 | * lws_set_proxy() - Setups proxy to libwebsocket_context. |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 686 | * @context: pointer to struct libwebsocket_context you want set proxy to |
| 687 | * @proxy: pointer to c string containing proxy in format address:port |
| 688 | * |
| 689 | * Returns 0 if proxy string was parsed and proxy was setup. |
| 690 | * Returns -1 if @proxy is NULL or has incorrect format. |
| 691 | * |
| 692 | * This is only required if your OS does not provide the http_proxy |
Drew Noakes | efe10b0 | 2014-10-23 15:34:26 +0100 | [diff] [blame] | 693 | * environment variable (eg, OSX) |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 694 | * |
| 695 | * IMPORTANT! You should call this function right after creation of the |
| 696 | * libwebsocket_context and before call to connect. If you call this |
| 697 | * function after connect behavior is undefined. |
| 698 | * This function will override proxy settings made on libwebsocket_context |
| 699 | * creation with genenv() call. |
| 700 | */ |
| 701 | |
| 702 | LWS_VISIBLE int |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 703 | lws_set_proxy(struct libwebsocket_context *context, const char *proxy) |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 704 | { |
| 705 | char *p; |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 706 | char authstring[96]; |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 707 | |
| 708 | if (!proxy) |
| 709 | return -1; |
| 710 | |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 711 | p = strchr(proxy, '@'); |
| 712 | if (p) { /* auth is around */ |
| 713 | |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 714 | if ((unsigned int)(p - proxy) > sizeof(authstring) - 1) |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 715 | goto auth_too_long; |
| 716 | |
Andy Green | f2280d6 | 2015-11-18 19:32:01 +0800 | [diff] [blame] | 717 | strncpy(authstring, proxy, p - proxy); |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 718 | // null termination not needed on input |
| 719 | if (lws_b64_encode_string(authstring, (p - proxy), |
| 720 | context->proxy_basic_auth_token, |
| 721 | sizeof context->proxy_basic_auth_token) < 0) |
| 722 | goto auth_too_long; |
| 723 | |
| 724 | lwsl_notice(" Proxy auth in use\n"); |
| 725 | |
| 726 | proxy = p + 1; |
| 727 | } else |
| 728 | context->proxy_basic_auth_token[0] = '\0'; |
| 729 | |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 730 | strncpy(context->http_proxy_address, proxy, |
| 731 | sizeof(context->http_proxy_address) - 1); |
| 732 | context->http_proxy_address[ |
| 733 | sizeof(context->http_proxy_address) - 1] = '\0'; |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 734 | |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 735 | p = strchr(context->http_proxy_address, ':'); |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 736 | if (!p && !context->http_proxy_port) { |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 737 | lwsl_err("http_proxy needs to be ads:port\n"); |
| 738 | |
| 739 | return -1; |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 740 | } else { |
| 741 | *p = '\0'; |
| 742 | context->http_proxy_port = atoi(p + 1); |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 743 | } |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 744 | |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 745 | lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address, |
| 746 | context->http_proxy_port); |
| 747 | |
| 748 | return 0; |
Andy Green | 6e40556 | 2015-11-08 10:15:01 +0800 | [diff] [blame] | 749 | |
| 750 | auth_too_long: |
| 751 | lwsl_err("proxy auth too long\n"); |
| 752 | |
| 753 | return -1; |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 757 | * lws_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 758 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 759 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 760 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 761 | * |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 762 | * Some apis can act on all live connections of a given protocol, |
| 763 | * this is how you can get a pointer to the active protocol if needed. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 764 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 765 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 766 | LWS_VISIBLE const struct libwebsocket_protocols * |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 767 | lws_get_protocol(struct libwebsocket *wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 768 | { |
| 769 | return wsi->protocol; |
| 770 | } |
| 771 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 772 | LWS_VISIBLE int |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 773 | lws_is_final_fragment(struct libwebsocket *wsi) |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 774 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 775 | return wsi->u.ws.final; |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 776 | } |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 777 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 778 | LWS_VISIBLE unsigned char |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 779 | lws_get_reserved_bits(struct libwebsocket *wsi) |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 780 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 781 | return wsi->u.ws.rsv; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 782 | } |
| 783 | |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 784 | int |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 785 | libwebsocket_ensure_user_space(struct libwebsocket *wsi) |
| 786 | { |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 787 | lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol); |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 788 | if (!wsi->protocol) |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 789 | return 1; |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 790 | |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 791 | /* allocate the per-connection user memory (if any) */ |
| 792 | |
| 793 | if (wsi->protocol->per_session_data_size && !wsi->user_space) { |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 794 | wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 795 | if (wsi->user_space == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 796 | lwsl_err("Out of memory for conn user space\n"); |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 797 | return 1; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 798 | } |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 799 | } else |
| 800 | lwsl_info("%s: %p protocol pss %u, user_space=%d\n", __func__, wsi, wsi->protocol->per_session_data_size, wsi->user_space); |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 801 | return 0; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 802 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 803 | |
Andy Green | b25b85f | 2014-04-03 23:34:09 +0800 | [diff] [blame] | 804 | LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line) |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 805 | { |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 806 | char buf[300]; |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 807 | unsigned long long now; |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 808 | int n; |
| 809 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 810 | buf[0] = '\0'; |
| 811 | for (n = 0; n < LLL_COUNT; n++) |
| 812 | if (level == (1 << n)) { |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 813 | now = time_in_microseconds() / 100; |
Imo Farcher | 97a748a | 2015-01-26 15:39:36 +0800 | [diff] [blame] | 814 | sprintf(buf, "[%llu:%04d] %s: ", (unsigned long long) now / 10000, |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 815 | (int)(now % 10000), log_level_names[n]); |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 816 | break; |
| 817 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 818 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 819 | fprintf(stderr, "%s%s", buf, line); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 820 | } |
| 821 | |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 822 | |
Neal Horman | 98e491f | 2014-12-10 18:50:28 -0600 | [diff] [blame] | 823 | LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 824 | { |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 825 | char buf[256]; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 826 | |
| 827 | if (!(log_level & filter)) |
| 828 | return; |
| 829 | |
Neal Horman | 98e491f | 2014-12-10 18:50:28 -0600 | [diff] [blame] | 830 | vsnprintf(buf, sizeof(buf), format, vl); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 831 | buf[sizeof(buf) - 1] = '\0'; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 832 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 833 | lwsl_emit(filter, buf); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 834 | } |
| 835 | |
Neal Horman | 98e491f | 2014-12-10 18:50:28 -0600 | [diff] [blame] | 836 | LWS_VISIBLE void _lws_log(int filter, const char *format, ...) |
| 837 | { |
| 838 | va_list ap; |
| 839 | |
| 840 | va_start(ap, format); |
| 841 | _lws_logv(filter, format, ap); |
| 842 | va_end(ap); |
| 843 | } |
| 844 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 845 | /** |
| 846 | * lws_set_log_level() - Set the logging bitfield |
| 847 | * @level: OR together the LLL_ debug contexts you want output from |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 848 | * @log_emit_function: NULL to leave it as it is, or a user-supplied |
| 849 | * function to perform log string emission instead of |
| 850 | * the default stderr one. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 851 | * |
Andy Green | c6511a0 | 2013-02-19 10:01:48 +0800 | [diff] [blame] | 852 | * log level defaults to "err", "warn" and "notice" contexts enabled and |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 853 | * emission on stderr. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 854 | */ |
| 855 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 856 | LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 857 | const char *line)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 858 | { |
| 859 | log_level = level; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 860 | if (log_emit_function) |
| 861 | lwsl_emit = log_emit_function; |
vpeter4 | 4dd8ada | 2014-04-27 13:28:22 +0200 | [diff] [blame] | 862 | } |
Andy Green | b128ccc | 2014-08-16 09:54:27 +0800 | [diff] [blame] | 863 | |
| 864 | /** |
| 865 | * lws_use_ssl() - Find out if connection is using SSL |
| 866 | * @wsi: websocket connection to check |
| 867 | * |
| 868 | * Returns 0 if the connection is not using SSL, 1 if using SSL and |
| 869 | * using verified cert, and 2 if using SSL but the cert was not |
| 870 | * checked (appears for client wsi told to skip check on connection) |
| 871 | */ |
| 872 | LWS_VISIBLE int |
| 873 | lws_is_ssl(struct libwebsocket *wsi) |
| 874 | { |
Andy Green | fc9871e | 2014-08-19 08:41:26 +0800 | [diff] [blame] | 875 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | b128ccc | 2014-08-16 09:54:27 +0800 | [diff] [blame] | 876 | return wsi->use_ssl; |
Andy Green | fc9871e | 2014-08-19 08:41:26 +0800 | [diff] [blame] | 877 | #else |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 878 | (void)wsi; |
Andy Green | fc9871e | 2014-08-19 08:41:26 +0800 | [diff] [blame] | 879 | return 0; |
| 880 | #endif |
Andy Green | b128ccc | 2014-08-16 09:54:27 +0800 | [diff] [blame] | 881 | } |
Andy Green | 14425ea | 2014-08-18 22:49:39 +0800 | [diff] [blame] | 882 | |
| 883 | /** |
| 884 | * lws_partial_buffered() - find out if lws buffered the last write |
| 885 | * @wsi: websocket connection to check |
| 886 | * |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 887 | * Returns 1 if you cannot use lws_write because the last |
Andy Green | 14425ea | 2014-08-18 22:49:39 +0800 | [diff] [blame] | 888 | * write on this connection is still buffered, and can't be cleared without |
| 889 | * returning to the service loop and waiting for the connection to be |
| 890 | * writeable again. |
| 891 | * |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 892 | * If you will try to do >1 lws_write call inside a single |
Andy Green | 14425ea | 2014-08-18 22:49:39 +0800 | [diff] [blame] | 893 | * WRITEABLE callback, you must check this after every write and bail if |
| 894 | * set, ask for a new writeable callback and continue writing from there. |
| 895 | * |
| 896 | * This is never set at the start of a writeable callback, but any write |
| 897 | * may set it. |
| 898 | */ |
| 899 | |
| 900 | LWS_VISIBLE int |
| 901 | lws_partial_buffered(struct libwebsocket *wsi) |
| 902 | { |
| 903 | return !!wsi->truncated_send_len; |
| 904 | } |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 905 | |
| 906 | void lws_set_protocol_write_pending(struct libwebsocket_context *context, |
| 907 | struct libwebsocket *wsi, |
| 908 | enum lws_pending_protocol_send pend) |
| 909 | { |
Andy Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 910 | lwsl_info("setting pps %d\n", pend); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 911 | |
| 912 | if (wsi->pps) |
| 913 | lwsl_err("pps overwrite\n"); |
| 914 | wsi->pps = pend; |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame^] | 915 | lws_rx_flow_control(wsi, 0); |
| 916 | lws_callback_on_writable(context, wsi); |
Andy Green | 260a0fc | 2014-10-18 18:52:39 +0800 | [diff] [blame] | 917 | } |
Andy Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 918 | |
| 919 | LWS_VISIBLE size_t |
| 920 | lws_get_peer_write_allowance(struct libwebsocket *wsi) |
| 921 | { |
| 922 | #ifdef LWS_USE_HTTP2 |
| 923 | /* only if we are using HTTP2 on this connection */ |
| 924 | if (wsi->mode != LWS_CONNMODE_HTTP2_SERVING) |
| 925 | return -1; |
| 926 | /* user is only interested in how much he can send, or that he can't */ |
| 927 | if (wsi->u.http2.tx_credit <= 0) |
| 928 | return 0; |
| 929 | |
| 930 | return wsi->u.http2.tx_credit; |
| 931 | #else |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 932 | (void)wsi; |
Andy Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 933 | return -1; |
| 934 | #endif |
| 935 | } |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 936 | |
| 937 | LWS_VISIBLE void |
| 938 | lws_union_transition(struct libwebsocket *wsi, enum connection_mode mode) |
| 939 | { |
| 940 | memset(&wsi->u, 0, sizeof(wsi->u)); |
| 941 | wsi->mode = mode; |
| 942 | } |