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) { |
| 92 | libwebsocket_callback_on_writable(context, wsi); |
| 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 | 9f02c5d | 2015-04-01 05:34:18 +0800 | [diff] [blame] | 101 | libwebsocket_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 | /* |
Drew Noakes | efe10b0 | 2014-10-23 15:34:26 +0100 | [diff] [blame] | 175 | * signal we are closing, libwebsocket_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)); |
| 194 | n = libwebsocket_write(wsi, |
| 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 | |
| 210 | libwebsocket_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 | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 303 | if (!lws_ssl_close(wsi) && wsi->sock >= 0) { |
| 304 | n = shutdown(wsi->sock, SHUT_RDWR); |
| 305 | if (n) |
| 306 | lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 307 | |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 308 | n = compatible_close(wsi->sock); |
| 309 | if (n) |
| 310 | lwsl_debug("closing: close ret %d\n", LWS_ERRNO); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 311 | } |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame] | 312 | |
| 313 | /* outermost destroy notification for wsi (user_space still intact) */ |
| 314 | context->protocols[0].callback(context, wsi, |
| 315 | LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0); |
| 316 | |
=?UTF-8?q?Joakim=20S=C3=B6derberg?= | caf7e3d | 2015-06-25 17:51:07 +0200 | [diff] [blame] | 317 | lws_free_wsi(wsi); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 320 | LWS_VISIBLE int |
| 321 | libwebsockets_get_addresses(struct libwebsocket_context *context, |
| 322 | void *ads, char *name, int name_len, |
| 323 | char *rip, int rip_len) |
| 324 | { |
| 325 | struct addrinfo ai, *res; |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 326 | struct sockaddr_in addr4; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 327 | |
| 328 | rip[0] = '\0'; |
| 329 | name[0] = '\0'; |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 330 | addr4.sin_family = AF_UNSPEC; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 331 | |
| 332 | #ifdef LWS_USE_IPV6 |
| 333 | if (LWS_IPV6_ENABLED(context)) { |
| 334 | if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) { |
| 335 | lwsl_err("inet_ntop", strerror(LWS_ERRNO)); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | // Strip off the IPv4 to IPv6 header if one exists |
| 340 | if (strncmp(rip, "::ffff:", 7) == 0) |
| 341 | memmove(rip, rip + 7, strlen(rip) - 6); |
| 342 | |
| 343 | getnameinfo((struct sockaddr *)ads, |
| 344 | sizeof(struct sockaddr_in6), name, |
| 345 | name_len, NULL, 0, 0); |
| 346 | |
| 347 | return 0; |
| 348 | } else |
| 349 | #endif |
| 350 | { |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 351 | struct addrinfo *result; |
| 352 | |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 353 | memset(&ai, 0, sizeof ai); |
| 354 | ai.ai_family = PF_UNSPEC; |
| 355 | ai.ai_socktype = SOCK_STREAM; |
| 356 | ai.ai_flags = AI_CANONNAME; |
| 357 | |
| 358 | if (getnameinfo((struct sockaddr *)ads, |
| 359 | sizeof(struct sockaddr_in), |
| 360 | name, name_len, NULL, 0, 0)) |
| 361 | return -1; |
| 362 | |
| 363 | if (!rip) |
| 364 | return 0; |
| 365 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 366 | if (getaddrinfo(name, NULL, &ai, &result)) |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 367 | return -1; |
| 368 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 369 | res = result; |
| 370 | while (addr4.sin_family == AF_UNSPEC && res) { |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 371 | switch (res->ai_family) { |
| 372 | case AF_INET: |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 373 | addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr; |
| 374 | addr4.sin_family = AF_INET; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | |
| 378 | res = res->ai_next; |
| 379 | } |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 380 | freeaddrinfo(result); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 381 | } |
| 382 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 383 | if (addr4.sin_family == AF_UNSPEC) |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 384 | return -1; |
| 385 | |
Andrejs Hanins | f94dd2e | 2015-10-15 13:02:03 +0300 | [diff] [blame] | 386 | lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 391 | /** |
| 392 | * libwebsockets_get_peer_addresses() - Get client address information |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 393 | * @context: Libwebsockets context |
| 394 | * @wsi: Local struct libwebsocket associated with |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 395 | * @fd: Connection socket descriptor |
| 396 | * @name: Buffer to take client address name |
| 397 | * @name_len: Length of client address name buffer |
Drew Noakes | efe10b0 | 2014-10-23 15:34:26 +0100 | [diff] [blame] | 398 | * @rip: Buffer to take client address IP dotted quad |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 399 | * @rip_len: Length of client address IP buffer |
| 400 | * |
| 401 | * 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] | 402 | * the client connected with socket descriptor @fd. Names may be |
| 403 | * truncated if there is not enough room. If either cannot be |
| 404 | * determined, they will be returned as valid zero-length strings. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 405 | */ |
| 406 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 407 | LWS_VISIBLE void |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 408 | libwebsockets_get_peer_addresses(struct libwebsocket_context *context, |
| 409 | struct libwebsocket *wsi, int fd, char *name, int name_len, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 410 | char *rip, int rip_len) |
| 411 | { |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 412 | socklen_t len; |
Andy Green | 055f297 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 413 | #ifdef LWS_USE_IPV6 |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 414 | struct sockaddr_in6 sin6; |
| 415 | #endif |
| 416 | struct sockaddr_in sin4; |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 417 | int ret = -1; |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 418 | void *p; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 419 | |
| 420 | rip[0] = '\0'; |
| 421 | name[0] = '\0'; |
| 422 | |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 423 | lws_latency_pre(context, wsi); |
| 424 | |
Andy Green | 055f297 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 425 | #ifdef LWS_USE_IPV6 |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 426 | if (LWS_IPV6_ENABLED(context)) { |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 427 | len = sizeof(sin6); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 428 | p = &sin6; |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 429 | } else |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 430 | #endif |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 431 | { |
| 432 | len = sizeof(sin4); |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 433 | p = &sin4; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 434 | } |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 435 | |
Andy Green | 7e37d10 | 2015-01-28 21:03:49 +0800 | [diff] [blame] | 436 | if (getpeername(fd, p, &len) < 0) { |
| 437 | lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO)); |
| 438 | goto bail; |
| 439 | } |
| 440 | |
| 441 | ret = libwebsockets_get_addresses(context, p, name, name_len, rip, rip_len); |
| 442 | |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 443 | bail: |
| 444 | lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1); |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 445 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 446 | |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 447 | /** |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 448 | * libwebsocket_context_user() - get the user data associated with the context |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 449 | * @context: Websocket context |
| 450 | * |
| 451 | * This returns the optional user allocation that can be attached to |
| 452 | * the context the sockets live in at context_create time. It's a way |
| 453 | * to let all sockets serviced in the same context share data without |
| 454 | * using globals statics in the user code. |
| 455 | */ |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 456 | LWS_EXTERN void * |
| 457 | libwebsocket_context_user(struct libwebsocket_context *context) |
| 458 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 459 | return context->user_space; |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 462 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 463 | /** |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 464 | * libwebsocket_callback_all_protocol() - Callback all connections using |
| 465 | * the given protocol with the given reason |
| 466 | * |
| 467 | * @protocol: Protocol whose connections will get callbacks |
| 468 | * @reason: Callback reason index |
| 469 | */ |
| 470 | |
| 471 | LWS_VISIBLE int |
| 472 | libwebsocket_callback_all_protocol( |
| 473 | const struct libwebsocket_protocols *protocol, int reason) |
| 474 | { |
| 475 | struct libwebsocket_context *context = protocol->owning_server; |
| 476 | int n; |
| 477 | struct libwebsocket *wsi; |
| 478 | |
| 479 | for (n = 0; n < context->fds_count; n++) { |
Bud Davis | 229bfec | 2015-01-30 10:13:01 +0800 | [diff] [blame] | 480 | wsi = wsi_from_fd(context, context->fds[n].fd); |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 481 | if (!wsi) |
| 482 | continue; |
| 483 | if (wsi->protocol == protocol) |
| 484 | protocol->callback(context, wsi, |
| 485 | reason, wsi->user_space, NULL, 0); |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /** |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 492 | * libwebsocket_set_timeout() - marks the wsi as subject to a timeout |
| 493 | * |
| 494 | * You will not need this unless you are doing something special |
| 495 | * |
| 496 | * @wsi: Websocket connection instance |
| 497 | * @reason: timeout reason |
| 498 | * @secs: how many seconds |
| 499 | */ |
| 500 | |
Craig McQueen | e7fd8b1 | 2014-02-19 09:24:17 +1100 | [diff] [blame] | 501 | LWS_VISIBLE void |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 502 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 503 | enum pending_timeout reason, int secs) |
| 504 | { |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 505 | time_t now; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 506 | |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 507 | time(&now); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 508 | |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 509 | wsi->pending_timeout_limit = now + secs; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 510 | wsi->pending_timeout = reason; |
| 511 | } |
| 512 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 516 | * |
| 517 | * You will not need this unless you are doing something special |
| 518 | * |
| 519 | * @wsi: Websocket connection instance |
| 520 | */ |
| 521 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 522 | LWS_VISIBLE int |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 523 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 524 | { |
| 525 | return wsi->sock; |
| 526 | } |
| 527 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 528 | #ifdef LWS_LATENCY |
| 529 | void |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 530 | lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, |
| 531 | const char *action, int ret, int completed) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 532 | { |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 533 | unsigned long long u; |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 534 | char buf[256]; |
| 535 | |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 536 | u = time_in_microseconds(); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 537 | |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 538 | if (!action) { |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 539 | wsi->latency_start = u; |
| 540 | if (!wsi->action_start) |
| 541 | wsi->action_start = u; |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 542 | return; |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 543 | } |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 544 | if (completed) { |
| 545 | if (wsi->action_start == wsi->latency_start) |
| 546 | sprintf(buf, |
Cong Ma | 10da117 | 2014-07-05 10:50:47 +0800 | [diff] [blame] | 547 | "Completion first try lat %lluus: %p: ret %d: %s\n", |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 548 | u - wsi->latency_start, |
| 549 | (void *)wsi, ret, action); |
| 550 | else |
| 551 | sprintf(buf, |
Cong Ma | 10da117 | 2014-07-05 10:50:47 +0800 | [diff] [blame] | 552 | "Completion %lluus: lat %lluus: %p: ret %d: %s\n", |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 553 | u - wsi->action_start, |
| 554 | u - wsi->latency_start, |
| 555 | (void *)wsi, ret, action); |
| 556 | wsi->action_start = 0; |
| 557 | } else |
Cong Ma | 10da117 | 2014-07-05 10:50:47 +0800 | [diff] [blame] | 558 | sprintf(buf, "lat %lluus: %p: ret %d: %s\n", |
Andy Green | bfaea95 | 2014-03-31 11:01:32 +0800 | [diff] [blame] | 559 | u - wsi->latency_start, (void *)wsi, ret, action); |
| 560 | |
| 561 | if (u - wsi->latency_start > context->worst_latency) { |
| 562 | context->worst_latency = u - wsi->latency_start; |
| 563 | strcpy(context->worst_latency_info, buf); |
| 564 | } |
| 565 | lwsl_latency("%s", buf); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 566 | } |
| 567 | #endif |
| 568 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 569 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 570 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 571 | /** |
| 572 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
Drew Noakes | efe10b0 | 2014-10-23 15:34:26 +0100 | [diff] [blame] | 573 | * received packets. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 574 | * |
| 575 | * If the output side of a server process becomes choked, this allows flow |
| 576 | * control for the input side. |
| 577 | * |
| 578 | * @wsi: Websocket connection instance to get callback for |
| 579 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 580 | */ |
| 581 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 582 | LWS_VISIBLE int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 583 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 584 | { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 585 | if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 586 | return 0; |
| 587 | |
| 588 | lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 589 | wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 590 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 591 | return 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 594 | /** |
| 595 | * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive |
| 596 | * |
| 597 | * When the user server code realizes it can accept more input, it can |
| 598 | * call this to have the RX flow restriction removed from all connections using |
| 599 | * the given protocol. |
| 600 | * |
| 601 | * @protocol: all connections using this protocol will be allowed to receive |
| 602 | */ |
| 603 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 604 | LWS_VISIBLE void |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 605 | libwebsocket_rx_flow_allow_all_protocol( |
| 606 | const struct libwebsocket_protocols *protocol) |
| 607 | { |
| 608 | struct libwebsocket_context *context = protocol->owning_server; |
| 609 | int n; |
| 610 | struct libwebsocket *wsi; |
| 611 | |
| 612 | for (n = 0; n < context->fds_count; n++) { |
Bud Davis | 229bfec | 2015-01-30 10:13:01 +0800 | [diff] [blame] | 613 | wsi = wsi_from_fd(context, context->fds[n].fd); |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 614 | if (!wsi) |
| 615 | continue; |
| 616 | if (wsi->protocol == protocol) |
| 617 | libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW); |
| 618 | } |
| 619 | } |
| 620 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 621 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 622 | /** |
| 623 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 624 | * |
| 625 | * This is typically used by client code to fill in the host parameter |
| 626 | * when making a client connection. You can only call it after the context |
| 627 | * has been created. |
| 628 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 629 | * @context: Websocket context |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 630 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 631 | LWS_VISIBLE extern const char * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 632 | libwebsocket_canonical_hostname(struct libwebsocket_context *context) |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 633 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 634 | return (const char *)context->canonical_hostname; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 637 | int user_callback_handle_rxflow(callback_function callback_function, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 638 | struct libwebsocket_context *context, |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 639 | struct libwebsocket *wsi, |
| 640 | enum libwebsocket_callback_reasons reason, void *user, |
| 641 | void *in, size_t len) |
| 642 | { |
| 643 | int n; |
| 644 | |
| 645 | n = callback_function(context, wsi, reason, user, in, len); |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 646 | if (!n) |
| 647 | n = _libwebsocket_rx_flow_control(wsi); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 648 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 649 | return n; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 650 | } |
| 651 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 652 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 653 | /** |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 654 | * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context. |
| 655 | * @context: pointer to struct libwebsocket_context you want set proxy to |
| 656 | * @proxy: pointer to c string containing proxy in format address:port |
| 657 | * |
| 658 | * Returns 0 if proxy string was parsed and proxy was setup. |
| 659 | * Returns -1 if @proxy is NULL or has incorrect format. |
| 660 | * |
| 661 | * 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] | 662 | * environment variable (eg, OSX) |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 663 | * |
| 664 | * IMPORTANT! You should call this function right after creation of the |
| 665 | * libwebsocket_context and before call to connect. If you call this |
| 666 | * function after connect behavior is undefined. |
| 667 | * This function will override proxy settings made on libwebsocket_context |
| 668 | * creation with genenv() call. |
| 669 | */ |
| 670 | |
| 671 | LWS_VISIBLE int |
| 672 | libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy) |
| 673 | { |
| 674 | char *p; |
| 675 | |
| 676 | if (!proxy) |
| 677 | return -1; |
| 678 | |
| 679 | strncpy(context->http_proxy_address, proxy, |
| 680 | sizeof(context->http_proxy_address) - 1); |
| 681 | context->http_proxy_address[ |
| 682 | sizeof(context->http_proxy_address) - 1] = '\0'; |
| 683 | |
| 684 | p = strchr(context->http_proxy_address, ':'); |
| 685 | if (!p) { |
| 686 | lwsl_err("http_proxy needs to be ads:port\n"); |
| 687 | |
| 688 | return -1; |
| 689 | } |
| 690 | *p = '\0'; |
| 691 | context->http_proxy_port = atoi(p + 1); |
| 692 | |
| 693 | lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address, |
| 694 | context->http_proxy_port); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /** |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 700 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 701 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 702 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 703 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 704 | * |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 705 | * Some apis can act on all live connections of a given protocol, |
| 706 | * 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] | 707 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 708 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 709 | LWS_VISIBLE const struct libwebsocket_protocols * |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 710 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 711 | { |
| 712 | return wsi->protocol; |
| 713 | } |
| 714 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 715 | LWS_VISIBLE int |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 716 | libwebsocket_is_final_fragment(struct libwebsocket *wsi) |
| 717 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 718 | return wsi->u.ws.final; |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 719 | } |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 720 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 721 | LWS_VISIBLE unsigned char |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 722 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi) |
| 723 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 724 | return wsi->u.ws.rsv; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 725 | } |
| 726 | |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 727 | int |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 728 | libwebsocket_ensure_user_space(struct libwebsocket *wsi) |
| 729 | { |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 730 | lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol); |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 731 | if (!wsi->protocol) |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 732 | return 1; |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 733 | |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 734 | /* allocate the per-connection user memory (if any) */ |
| 735 | |
| 736 | if (wsi->protocol->per_session_data_size && !wsi->user_space) { |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 737 | wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 738 | if (wsi->user_space == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 739 | lwsl_err("Out of memory for conn user space\n"); |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 740 | return 1; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 741 | } |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 742 | } else |
| 743 | 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] | 744 | return 0; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 745 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 746 | |
Andy Green | b25b85f | 2014-04-03 23:34:09 +0800 | [diff] [blame] | 747 | LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line) |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 748 | { |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 749 | char buf[300]; |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 750 | unsigned long long now; |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 751 | int n; |
| 752 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 753 | buf[0] = '\0'; |
| 754 | for (n = 0; n < LLL_COUNT; n++) |
| 755 | if (level == (1 << n)) { |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 756 | now = time_in_microseconds() / 100; |
Imo Farcher | 97a748a | 2015-01-26 15:39:36 +0800 | [diff] [blame] | 757 | sprintf(buf, "[%llu:%04d] %s: ", (unsigned long long) now / 10000, |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 758 | (int)(now % 10000), log_level_names[n]); |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 759 | break; |
| 760 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 761 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 762 | fprintf(stderr, "%s%s", buf, line); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 763 | } |
| 764 | |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 765 | |
Neal Horman | 98e491f | 2014-12-10 18:50:28 -0600 | [diff] [blame] | 766 | 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] | 767 | { |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 768 | char buf[256]; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 769 | |
| 770 | if (!(log_level & filter)) |
| 771 | return; |
| 772 | |
Neal Horman | 98e491f | 2014-12-10 18:50:28 -0600 | [diff] [blame] | 773 | vsnprintf(buf, sizeof(buf), format, vl); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 774 | buf[sizeof(buf) - 1] = '\0'; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 775 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 776 | lwsl_emit(filter, buf); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 777 | } |
| 778 | |
Neal Horman | 98e491f | 2014-12-10 18:50:28 -0600 | [diff] [blame] | 779 | LWS_VISIBLE void _lws_log(int filter, const char *format, ...) |
| 780 | { |
| 781 | va_list ap; |
| 782 | |
| 783 | va_start(ap, format); |
| 784 | _lws_logv(filter, format, ap); |
| 785 | va_end(ap); |
| 786 | } |
| 787 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 788 | /** |
| 789 | * lws_set_log_level() - Set the logging bitfield |
| 790 | * @level: OR together the LLL_ debug contexts you want output from |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 791 | * @log_emit_function: NULL to leave it as it is, or a user-supplied |
| 792 | * function to perform log string emission instead of |
| 793 | * the default stderr one. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 794 | * |
Andy Green | c6511a0 | 2013-02-19 10:01:48 +0800 | [diff] [blame] | 795 | * log level defaults to "err", "warn" and "notice" contexts enabled and |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 796 | * emission on stderr. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 797 | */ |
| 798 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 799 | 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] | 800 | const char *line)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 801 | { |
| 802 | log_level = level; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 803 | if (log_emit_function) |
| 804 | lwsl_emit = log_emit_function; |
vpeter4 | 4dd8ada | 2014-04-27 13:28:22 +0200 | [diff] [blame] | 805 | } |
Andy Green | b128ccc | 2014-08-16 09:54:27 +0800 | [diff] [blame] | 806 | |
| 807 | /** |
| 808 | * lws_use_ssl() - Find out if connection is using SSL |
| 809 | * @wsi: websocket connection to check |
| 810 | * |
| 811 | * Returns 0 if the connection is not using SSL, 1 if using SSL and |
| 812 | * using verified cert, and 2 if using SSL but the cert was not |
| 813 | * checked (appears for client wsi told to skip check on connection) |
| 814 | */ |
| 815 | LWS_VISIBLE int |
| 816 | lws_is_ssl(struct libwebsocket *wsi) |
| 817 | { |
Andy Green | fc9871e | 2014-08-19 08:41:26 +0800 | [diff] [blame] | 818 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | b128ccc | 2014-08-16 09:54:27 +0800 | [diff] [blame] | 819 | return wsi->use_ssl; |
Andy Green | fc9871e | 2014-08-19 08:41:26 +0800 | [diff] [blame] | 820 | #else |
| 821 | return 0; |
| 822 | #endif |
Andy Green | b128ccc | 2014-08-16 09:54:27 +0800 | [diff] [blame] | 823 | } |
Andy Green | 14425ea | 2014-08-18 22:49:39 +0800 | [diff] [blame] | 824 | |
| 825 | /** |
| 826 | * lws_partial_buffered() - find out if lws buffered the last write |
| 827 | * @wsi: websocket connection to check |
| 828 | * |
| 829 | * Returns 1 if you cannot use libwebsocket_write because the last |
| 830 | * write on this connection is still buffered, and can't be cleared without |
| 831 | * returning to the service loop and waiting for the connection to be |
| 832 | * writeable again. |
| 833 | * |
| 834 | * If you will try to do >1 libwebsocket_write call inside a single |
| 835 | * WRITEABLE callback, you must check this after every write and bail if |
| 836 | * set, ask for a new writeable callback and continue writing from there. |
| 837 | * |
| 838 | * This is never set at the start of a writeable callback, but any write |
| 839 | * may set it. |
| 840 | */ |
| 841 | |
| 842 | LWS_VISIBLE int |
| 843 | lws_partial_buffered(struct libwebsocket *wsi) |
| 844 | { |
| 845 | return !!wsi->truncated_send_len; |
| 846 | } |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 847 | |
| 848 | void lws_set_protocol_write_pending(struct libwebsocket_context *context, |
| 849 | struct libwebsocket *wsi, |
| 850 | enum lws_pending_protocol_send pend) |
| 851 | { |
Andy Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 852 | lwsl_info("setting pps %d\n", pend); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 853 | |
| 854 | if (wsi->pps) |
| 855 | lwsl_err("pps overwrite\n"); |
| 856 | wsi->pps = pend; |
| 857 | libwebsocket_rx_flow_control(wsi, 0); |
| 858 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | 260a0fc | 2014-10-18 18:52:39 +0800 | [diff] [blame] | 859 | } |
Andy Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 860 | |
| 861 | LWS_VISIBLE size_t |
| 862 | lws_get_peer_write_allowance(struct libwebsocket *wsi) |
| 863 | { |
| 864 | #ifdef LWS_USE_HTTP2 |
| 865 | /* only if we are using HTTP2 on this connection */ |
| 866 | if (wsi->mode != LWS_CONNMODE_HTTP2_SERVING) |
| 867 | return -1; |
| 868 | /* user is only interested in how much he can send, or that he can't */ |
| 869 | if (wsi->u.http2.tx_credit <= 0) |
| 870 | return 0; |
| 871 | |
| 872 | return wsi->u.http2.tx_credit; |
| 873 | #else |
| 874 | return -1; |
| 875 | #endif |
| 876 | } |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 877 | |
| 878 | LWS_VISIBLE void |
| 879 | lws_union_transition(struct libwebsocket *wsi, enum connection_mode mode) |
| 880 | { |
| 881 | memset(&wsi->u, 0, sizeof(wsi->u)); |
| 882 | wsi->mode = mode; |
| 883 | } |