henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 11 | /* |
| 12 | * This is a diagram of how TCP reconnect works for the active side. The |
| 13 | * passive side just waits for an incoming connection. |
| 14 | * |
| 15 | * - Connected: Indicate whether the TCP socket is connected. |
| 16 | * |
| 17 | * - Writable: Whether the stun binding is completed. Sending a data packet |
| 18 | * before stun binding completed will trigger IPC socket layer to shutdown |
| 19 | * the connection. |
| 20 | * |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 21 | * - PendingTCP: `connection_pending_` indicates whether there is an |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 22 | * outstanding TCP connection in progress. |
| 23 | * |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 24 | * - PretendWri: Tracked by `pretending_to_be_writable_`. Marking connection as |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 25 | * WRITE_TIMEOUT will cause the connection be deleted. Instead, we're |
| 26 | * "pretending" we're still writable for a period of time such that reconnect |
| 27 | * could work. |
| 28 | * |
| 29 | * Data could only be sent in state 3. Sening data during state 2 & 6 will get |
| 30 | * EWOULDBLOCK, 4 & 5 EPIPE. |
| 31 | * |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 32 | * OS Timeout 7 -------------+ |
| 33 | * +----------------------->|Connected: N | |
| 34 | * | |Writable: N | Timeout |
| 35 | * | Timeout |Connection is |<----------------+ |
| 36 | * | +------------------->|Dead | | |
| 37 | * | | +--------------+ | |
| 38 | * | | ^ | |
| 39 | * | | OnClose | | |
| 40 | * | | +-----------------------+ | | |
| 41 | * | | | | |Timeout | |
| 42 | * | | v | | | |
| 43 | * | 4 +----------+ 5 -----+--+--+ 6 -----+-----+ |
| 44 | * | |Connected: N|Send() or |Connected: N| |Connected: Y| |
| 45 | * | |Writable: Y|Ping() |Writable: Y|OnConnect |Writable: Y| |
| 46 | * | |PendingTCP:N+--------> |PendingTCP:Y+---------> |PendingTCP:N| |
| 47 | * | |PretendWri:Y| |PretendWri:Y| |PretendWri:Y| |
| 48 | * | +-----+------+ +------------+ +---+--+-----+ |
| 49 | * | ^ ^ | | |
| 50 | * | | | OnClose | | |
| 51 | * | | +----------------------------------------------+ | |
| 52 | * | | | |
| 53 | * | | Stun Binding Completed | |
| 54 | * | | | |
| 55 | * | | OnClose | |
| 56 | * | +------------------------------------------------+ | |
| 57 | * | | v |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 58 | * 1 -----------+ 2 -----------+Stun 3 -----------+ |
| 59 | * |Connected: N| |Connected: Y|Binding |Connected: Y| |
| 60 | * |Writable: N|OnConnect |Writable: N|Completed |Writable: Y| |
| 61 | * |PendingTCP:Y+---------> |PendingTCP:N+--------> |PendingTCP:N| |
| 62 | * |PretendWri:N| |PretendWri:N| |PretendWri:N| |
| 63 | * +------------+ +------------+ +------------+ |
| 64 | * |
| 65 | */ |
| 66 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 67 | #include "p2p/base/tcp_port.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 68 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 69 | #include <errno.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 70 | |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 71 | #include <utility> |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 72 | #include <vector> |
| 73 | |
Steve Anton | ae226f6 | 2019-01-29 12:47:38 -0800 | [diff] [blame] | 74 | #include "absl/algorithm/container.h" |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 75 | #include "absl/memory/memory.h" |
Ali Tofigh | ea5a944 | 2022-06-14 15:20:15 +0200 | [diff] [blame] | 76 | #include "absl/strings/string_view.h" |
Danil Chapovalov | 7b19036 | 2022-07-07 14:13:02 +0200 | [diff] [blame^] | 77 | #include "api/task_queue/pending_task_safety_flag.h" |
| 78 | #include "api/units/time_delta.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 79 | #include "p2p/base/p2p_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 80 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 81 | #include "rtc_base/ip_address.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 82 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 83 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 84 | #include "rtc_base/net_helper.h" |
| 85 | #include "rtc_base/rate_tracker.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 86 | #include "rtc_base/third_party/sigslot/sigslot.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 87 | |
| 88 | namespace cricket { |
Danil Chapovalov | 7b19036 | 2022-07-07 14:13:02 +0200 | [diff] [blame^] | 89 | using ::webrtc::SafeTask; |
| 90 | using ::webrtc::TimeDelta; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | |
| 92 | TCPPort::TCPPort(rtc::Thread* thread, |
| 93 | rtc::PacketSocketFactory* factory, |
Niels Möller | e0c6bdf | 2022-03-24 15:18:02 +0100 | [diff] [blame] | 94 | const rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 95 | uint16_t min_port, |
| 96 | uint16_t max_port, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame] | 97 | absl::string_view username, |
| 98 | absl::string_view password, |
Jonas Oreland | c06fe8b | 2022-03-28 14:58:26 +0200 | [diff] [blame] | 99 | bool allow_listen, |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 100 | const webrtc::FieldTrialsView* field_trials) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 101 | : Port(thread, |
| 102 | LOCAL_PORT_TYPE, |
| 103 | factory, |
| 104 | network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 105 | min_port, |
| 106 | max_port, |
| 107 | username, |
Jonas Oreland | c06fe8b | 2022-03-28 14:58:26 +0200 | [diff] [blame] | 108 | password, |
| 109 | field_trials), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | allow_listen_(allow_listen), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | error_(0) { |
| 112 | // TODO(mallinath) - Set preference value as per RFC 6544. |
| 113 | // http://b/issue?id=7141794 |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 114 | if (allow_listen_) { |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 115 | TryCreateServerSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | } |
Niels Möller | 646fddc | 2021-11-02 15:56:05 +0100 | [diff] [blame] | 117 | // Set TCP_NODELAY (via OPT_NODELAY) for improved performance; this causes |
| 118 | // small media packets to be sent immediately rather than being buffered up, |
| 119 | // reducing latency. |
| 120 | SetOption(rtc::Socket::OPT_NODELAY, 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TCPPort::~TCPPort() { |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 124 | listen_socket_ = nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 125 | std::list<Incoming>::iterator it; |
| 126 | for (it = incoming_.begin(); it != incoming_.end(); ++it) |
| 127 | delete it->socket; |
| 128 | incoming_.clear(); |
| 129 | } |
| 130 | |
| 131 | Connection* TCPPort::CreateConnection(const Candidate& address, |
| 132 | CandidateOrigin origin) { |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 133 | if (!SupportsProtocol(address.protocol())) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | |
Philipp Hancke | e283d1c | 2020-03-27 09:56:51 +0100 | [diff] [blame] | 137 | if ((address.tcptype() == TCPTYPE_ACTIVE_STR && |
| 138 | address.type() != PRFLX_PORT_TYPE) || |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 139 | (address.tcptype().empty() && address.address().port() == 0)) { |
| 140 | // It's active only candidate, we should not try to create connections |
| 141 | // for these candidates. |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | // We can't accept TCP connections incoming on other ports |
| 146 | if (origin == ORIGIN_OTHER_PORT) |
| 147 | return NULL; |
| 148 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 149 | // We don't know how to act as an ssl server yet |
| 150 | if ((address.protocol() == SSLTCP_PROTOCOL_NAME) && |
| 151 | (origin == ORIGIN_THIS_PORT)) { |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | if (!IsCompatibleAddress(address.address())) { |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | TCPConnection* conn = NULL; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | if (rtc::AsyncPacketSocket* socket = GetIncoming(address.address(), true)) { |
deadbeef | 0687829 | 2017-04-21 14:22:23 -0700 | [diff] [blame] | 161 | // Incoming connection; we already created a socket and connected signals, |
| 162 | // so we need to hand off the "read packet" responsibility to |
| 163 | // TCPConnection. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 164 | socket->SignalReadPacket.disconnect(this); |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 165 | conn = new TCPConnection(NewWeakPtr(), address, socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 166 | } else { |
deadbeef | 0687829 | 2017-04-21 14:22:23 -0700 | [diff] [blame] | 167 | // Outgoing connection, which will create a new socket for which we still |
| 168 | // need to connect SignalReadyToSend and SignalSentPacket. |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 169 | conn = new TCPConnection(NewWeakPtr(), address); |
deadbeef | 0687829 | 2017-04-21 14:22:23 -0700 | [diff] [blame] | 170 | if (conn->socket()) { |
| 171 | conn->socket()->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); |
| 172 | conn->socket()->SignalSentPacket.connect(this, &TCPPort::OnSentPacket); |
| 173 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 174 | } |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 175 | AddOrReplaceConnection(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | return conn; |
| 177 | } |
| 178 | |
| 179 | void TCPPort::PrepareAddress() { |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 180 | if (listen_socket_) { |
Niels Möller | 4a1c2c4 | 2021-09-28 10:17:07 +0200 | [diff] [blame] | 181 | // Socket may be in the CLOSED state if Listen() |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 182 | // failed, we still want to add the socket address. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 183 | RTC_LOG(LS_VERBOSE) << "Preparing TCP address, current state: " |
Niels Möller | d30ece1 | 2021-10-19 10:11:02 +0200 | [diff] [blame] | 184 | << static_cast<int>(listen_socket_->GetState()); |
| 185 | AddAddress(listen_socket_->GetLocalAddress(), |
| 186 | listen_socket_->GetLocalAddress(), rtc::SocketAddress(), |
| 187 | TCP_PROTOCOL_NAME, "", TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, |
| 188 | ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | } else { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 190 | RTC_LOG(LS_INFO) << ToString() |
| 191 | << ": Not listening due to firewall restrictions."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 192 | // Note: We still add the address, since otherwise the remote side won't |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 193 | // recognize our incoming TCP connections. According to |
| 194 | // https://tools.ietf.org/html/rfc6544#section-4.5, for active candidate, |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 195 | // the port must be set to the discard port, i.e. 9. We can't be 100% sure |
| 196 | // which IP address will actually be used, so GetBestIP is as good as we |
| 197 | // can do. |
| 198 | // TODO(deadbeef): We could do something like create a dummy socket just to |
| 199 | // see what IP we get. But that may be overkill. |
| 200 | AddAddress(rtc::SocketAddress(Network()->GetBestIP(), DISCARD_PORT), |
| 201 | rtc::SocketAddress(Network()->GetBestIP(), 0), |
| 202 | rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", TCPTYPE_ACTIVE_STR, |
| 203 | LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 207 | int TCPPort::SendTo(const void* data, |
| 208 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 209 | const rtc::SocketAddress& addr, |
| 210 | const rtc::PacketOptions& options, |
| 211 | bool payload) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 212 | rtc::AsyncPacketSocket* socket = NULL; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 213 | TCPConnection* conn = static_cast<TCPConnection*>(GetConnection(addr)); |
| 214 | |
| 215 | // For Connection, this is the code path used by Ping() to establish |
| 216 | // WRITABLE. It has to send through the socket directly as TCPConnection::Send |
| 217 | // checks writability. |
| 218 | if (conn) { |
| 219 | if (!conn->connected()) { |
| 220 | conn->MaybeReconnect(); |
| 221 | return SOCKET_ERROR; |
| 222 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 223 | socket = conn->socket(); |
Harald Alvestrand | dc80017 | 2020-01-06 20:01:36 +0100 | [diff] [blame] | 224 | if (!socket) { |
| 225 | // The failure to initialize should have been logged elsewhere, |
| 226 | // so this log is not important. |
| 227 | RTC_LOG(LS_INFO) << ToString() |
| 228 | << ": Attempted to send to an uninitialized socket: " |
| 229 | << addr.ToSensitiveString(); |
| 230 | error_ = EHOSTUNREACH; |
| 231 | return SOCKET_ERROR; |
| 232 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 233 | } else { |
| 234 | socket = GetIncoming(addr); |
Harald Alvestrand | dc80017 | 2020-01-06 20:01:36 +0100 | [diff] [blame] | 235 | if (!socket) { |
| 236 | RTC_LOG(LS_ERROR) << ToString() |
| 237 | << ": Attempted to send to an unknown destination: " |
| 238 | << addr.ToSensitiveString(); |
| 239 | error_ = EHOSTUNREACH; |
| 240 | return SOCKET_ERROR; |
| 241 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 242 | } |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 243 | rtc::PacketOptions modified_options(options); |
| 244 | CopyPortInformationToPacketInfo(&modified_options.info_signaled_after_sent); |
| 245 | int sent = socket->Send(data, size, modified_options); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 246 | if (sent < 0) { |
| 247 | error_ = socket->GetError(); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 248 | // Error from this code path for a Connection (instead of from a bare |
| 249 | // socket) will not trigger reconnecting. In theory, this shouldn't matter |
| 250 | // as OnClose should always be called and set connected to false. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 251 | RTC_LOG(LS_ERROR) << ToString() << ": TCP send of " << size |
| 252 | << " bytes failed with error " << error_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 253 | } |
| 254 | return sent; |
| 255 | } |
| 256 | |
| 257 | int TCPPort::GetOption(rtc::Socket::Option opt, int* value) { |
Niels Möller | 646fddc | 2021-11-02 15:56:05 +0100 | [diff] [blame] | 258 | auto const& it = socket_options_.find(opt); |
| 259 | if (it == socket_options_.end()) { |
| 260 | return -1; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | } |
Niels Möller | 646fddc | 2021-11-02 15:56:05 +0100 | [diff] [blame] | 262 | *value = it->second; |
| 263 | return 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int TCPPort::SetOption(rtc::Socket::Option opt, int value) { |
Niels Möller | 646fddc | 2021-11-02 15:56:05 +0100 | [diff] [blame] | 267 | socket_options_[opt] = value; |
| 268 | return 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | int TCPPort::GetError() { |
| 272 | return error_; |
| 273 | } |
| 274 | |
Ali Tofigh | ea5a944 | 2022-06-14 15:20:15 +0200 | [diff] [blame] | 275 | bool TCPPort::SupportsProtocol(absl::string_view protocol) const { |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 276 | return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME; |
| 277 | } |
| 278 | |
| 279 | ProtocolType TCPPort::GetProtocol() const { |
| 280 | return PROTO_TCP; |
| 281 | } |
| 282 | |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 283 | void TCPPort::OnNewConnection(rtc::AsyncListenSocket* socket, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | rtc::AsyncPacketSocket* new_socket) { |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 285 | RTC_DCHECK(socket == listen_socket_.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 286 | |
Niels Möller | 646fddc | 2021-11-02 15:56:05 +0100 | [diff] [blame] | 287 | for (const auto& option : socket_options_) { |
| 288 | new_socket->SetOption(option.first, option.second); |
| 289 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 290 | Incoming incoming; |
| 291 | incoming.addr = new_socket->GetRemoteAddress(); |
| 292 | incoming.socket = new_socket; |
| 293 | incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket); |
| 294 | incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 295 | incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 296 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 297 | RTC_LOG(LS_VERBOSE) << ToString() << ": Accepted connection from " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 298 | << incoming.addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 299 | incoming_.push_back(incoming); |
| 300 | } |
| 301 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 302 | void TCPPort::TryCreateServerSocket() { |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 303 | listen_socket_ = absl::WrapUnique(socket_factory()->CreateServerTcpSocket( |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 304 | rtc::SocketAddress(Network()->GetBestIP(), 0), min_port(), max_port(), |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 305 | false /* ssl */)); |
| 306 | if (!listen_socket_) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 307 | RTC_LOG(LS_WARNING) |
| 308 | << ToString() |
| 309 | << ": TCP server socket creation failed; continuing anyway."; |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 310 | return; |
| 311 | } |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 312 | listen_socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection); |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 315 | rtc::AsyncPacketSocket* TCPPort::GetIncoming(const rtc::SocketAddress& addr, |
| 316 | bool remove) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 317 | rtc::AsyncPacketSocket* socket = NULL; |
| 318 | for (std::list<Incoming>::iterator it = incoming_.begin(); |
| 319 | it != incoming_.end(); ++it) { |
| 320 | if (it->addr == addr) { |
| 321 | socket = it->socket; |
| 322 | if (remove) |
| 323 | incoming_.erase(it); |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | return socket; |
| 328 | } |
| 329 | |
| 330 | void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 331 | const char* data, |
| 332 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 333 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 334 | const int64_t& packet_time_us) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 335 | Port::OnReadPacket(data, size, remote_addr, PROTO_TCP); |
| 336 | } |
| 337 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 338 | void TCPPort::OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 339 | const rtc::SentPacket& sent_packet) { |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 340 | PortInterface::SignalSentPacket(sent_packet); |
| 341 | } |
| 342 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 343 | void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
| 344 | Port::OnReadyToSend(); |
| 345 | } |
| 346 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 347 | // TODO(qingsi): `CONNECTION_WRITE_CONNECT_TIMEOUT` is overriden by |
| 348 | // `ice_unwritable_timeout` in IceConfig when determining the writability state. |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 349 | // Replace this constant with the config parameter assuming the default value if |
| 350 | // we decide it is also applicable here. |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 351 | TCPConnection::TCPConnection(rtc::WeakPtr<Port> tcp_port, |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 352 | const Candidate& candidate, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 353 | rtc::AsyncPacketSocket* socket) |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 354 | : Connection(std::move(tcp_port), 0, candidate), |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 355 | socket_(socket), |
| 356 | error_(0), |
| 357 | outgoing_(socket == NULL), |
| 358 | connection_pending_(false), |
| 359 | pretending_to_be_writable_(false), |
| 360 | reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) { |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 361 | RTC_DCHECK_EQ(port()->GetProtocol(), PROTO_TCP); // Needs to be TCPPort. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 362 | if (outgoing_) { |
| 363 | CreateOutgoingTcpSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 364 | } else { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 365 | // Incoming connections should match one of the network addresses. Same as |
| 366 | // what's being checked in OnConnect, but just DCHECKing here. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 367 | RTC_LOG(LS_VERBOSE) << ToString() << ": socket ipaddr: " |
Qingsi Wang | 20232a9 | 2019-09-06 12:51:17 -0700 | [diff] [blame] | 368 | << socket_->GetLocalAddress().ToSensitiveString() |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 369 | << ", port() Network:" << port()->Network()->ToString(); |
Steve Anton | ae226f6 | 2019-01-29 12:47:38 -0800 | [diff] [blame] | 370 | RTC_DCHECK(absl::c_any_of( |
| 371 | port_->Network()->GetIPs(), [this](const rtc::InterfaceAddress& addr) { |
| 372 | return socket_->GetLocalAddress().ipaddr() == addr; |
| 373 | })); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 374 | ConnectSocketSignals(socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Mirko Bonadei | 0cb1cfa | 2022-02-25 10:45:32 +0000 | [diff] [blame] | 378 | TCPConnection::~TCPConnection() { |
| 379 | RTC_DCHECK_RUN_ON(network_thread_); |
| 380 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 381 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 382 | int TCPConnection::Send(const void* data, |
| 383 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | const rtc::PacketOptions& options) { |
| 385 | if (!socket_) { |
| 386 | error_ = ENOTCONN; |
| 387 | return SOCKET_ERROR; |
| 388 | } |
| 389 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 390 | // Sending after OnClose on active side will trigger a reconnect for a |
| 391 | // outgoing connection. Note that the write state is still WRITABLE as we want |
| 392 | // to spend a few seconds attempting a reconnect before saying we're |
| 393 | // unwritable. |
| 394 | if (!connected()) { |
| 395 | MaybeReconnect(); |
| 396 | return SOCKET_ERROR; |
| 397 | } |
| 398 | |
| 399 | // Note that this is important to put this after the previous check to give |
| 400 | // the connection a chance to reconnect. |
| 401 | if (pretending_to_be_writable_ || write_state() != STATE_WRITABLE) { |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 402 | // TODO(?): Should STATE_WRITE_TIMEOUT return a non-blocking error? |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 403 | error_ = ENOTCONN; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 404 | return SOCKET_ERROR; |
| 405 | } |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 406 | stats_.sent_total_packets++; |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 407 | rtc::PacketOptions modified_options(options); |
Tommi | d7e5cfb | 2022-03-30 20:13:06 +0200 | [diff] [blame] | 408 | tcp_port()->CopyPortInformationToPacketInfo( |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 409 | &modified_options.info_signaled_after_sent); |
| 410 | int sent = socket_->Send(data, size, modified_options); |
Jonas Oreland | 3c5d582 | 2020-12-14 12:45:05 +0100 | [diff] [blame] | 411 | int64_t now = rtc::TimeMillis(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 412 | if (sent < 0) { |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 413 | stats_.sent_discarded_packets++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 414 | error_ = socket_->GetError(); |
| 415 | } else { |
Jonas Oreland | 3c5d582 | 2020-12-14 12:45:05 +0100 | [diff] [blame] | 416 | send_rate_tracker_.AddSamplesAtTime(now, sent); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 417 | } |
Jonas Oreland | 3c5d582 | 2020-12-14 12:45:05 +0100 | [diff] [blame] | 418 | last_send_data_ = now; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 419 | return sent; |
| 420 | } |
| 421 | |
| 422 | int TCPConnection::GetError() { |
| 423 | return error_; |
| 424 | } |
| 425 | |
Tommi | c85e473 | 2022-05-27 16:37:42 +0200 | [diff] [blame] | 426 | void TCPConnection::OnConnectionRequestResponse(StunRequest* req, |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 427 | StunMessage* response) { |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 428 | // Process the STUN response before we inform upper layer ready to send. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 429 | Connection::OnConnectionRequestResponse(req, response); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 430 | |
| 431 | // If we're in the state of pretending to be writeable, we should inform the |
| 432 | // upper layer it's ready to send again as previous EWOULDLBLOCK from socket |
| 433 | // would have stopped the outgoing stream. |
| 434 | if (pretending_to_be_writable_) { |
| 435 | Connection::OnReadyToSend(); |
| 436 | } |
| 437 | pretending_to_be_writable_ = false; |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 438 | RTC_DCHECK(write_state() == STATE_WRITABLE); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 439 | } |
| 440 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 441 | void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 442 | RTC_DCHECK(socket == socket_.get()); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 443 | // Do not use this port if the socket bound to an address not associated with |
| 444 | // the desired network interface. This is seen in Chrome, where TCP sockets |
| 445 | // cannot be given a binding address, and the platform is expected to pick |
| 446 | // the correct local address. |
| 447 | // |
| 448 | // However, there are two situations in which we allow the bound address to |
| 449 | // not be one of the addresses of the requested interface: |
| 450 | // 1. The bound address is the loopback address. This happens when a proxy |
| 451 | // forces TCP to bind to only the localhost address (see issue 3927). |
| 452 | // 2. The bound address is the "any address". This happens when |
| 453 | // multiple_routes is disabled (see issue 4780). |
| 454 | // |
| 455 | // Note that, aside from minor differences in log statements, this logic is |
| 456 | // identical to that in TurnPort. |
| 457 | const rtc::SocketAddress& socket_address = socket->GetLocalAddress(); |
Steve Anton | ae226f6 | 2019-01-29 12:47:38 -0800 | [diff] [blame] | 458 | if (absl::c_any_of(port_->Network()->GetIPs(), |
| 459 | [socket_address](const rtc::InterfaceAddress& addr) { |
| 460 | return socket_address.ipaddr() == addr; |
| 461 | })) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 462 | RTC_LOG(LS_VERBOSE) << ToString() << ": Connection established to " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 463 | << socket->GetRemoteAddress().ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 464 | } else { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 465 | if (socket->GetLocalAddress().IsLoopbackIP()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 466 | RTC_LOG(LS_WARNING) << "Socket is bound to the address:" |
Qingsi Wang | 20232a9 | 2019-09-06 12:51:17 -0700 | [diff] [blame] | 467 | << socket_address.ipaddr().ToSensitiveString() |
Taylor Brandstetter | 3ba7a57 | 2018-03-02 10:58:25 -0800 | [diff] [blame] | 468 | << ", rather than an address associated with network:" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 469 | << port_->Network()->ToString() |
| 470 | << ". Still allowing it since it's localhost."; |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 471 | } else if (IPIsAny(port_->Network()->GetBestIP())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 472 | RTC_LOG(LS_WARNING) |
| 473 | << "Socket is bound to the address:" |
Qingsi Wang | 20232a9 | 2019-09-06 12:51:17 -0700 | [diff] [blame] | 474 | << socket_address.ipaddr().ToSensitiveString() |
Taylor Brandstetter | 3ba7a57 | 2018-03-02 10:58:25 -0800 | [diff] [blame] | 475 | << ", rather than an address associated with network:" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 476 | << port_->Network()->ToString() |
| 477 | << ". Still allowing it since it's the 'any' address" |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 478 | ", possibly caused by multiple_routes being disabled."; |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 479 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 480 | RTC_LOG(LS_WARNING) << "Dropping connection as TCP socket bound to IP " |
Qingsi Wang | 20232a9 | 2019-09-06 12:51:17 -0700 | [diff] [blame] | 481 | << socket_address.ipaddr().ToSensitiveString() |
Taylor Brandstetter | 3ba7a57 | 2018-03-02 10:58:25 -0800 | [diff] [blame] | 482 | << ", rather than an address associated with network:" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 483 | << port_->Network()->ToString(); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 484 | OnClose(socket, 0); |
| 485 | return; |
| 486 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 487 | } |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 488 | |
| 489 | // Connection is established successfully. |
| 490 | set_connected(true); |
| 491 | connection_pending_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 495 | RTC_DCHECK(socket == socket_.get()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 496 | RTC_LOG(LS_INFO) << ToString() << ": Connection closed with error " << error; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 497 | |
Tommi | 6fd77f3 | 2022-06-09 21:45:56 +0200 | [diff] [blame] | 498 | RTC_DCHECK(port()); |
| 499 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 500 | // Guard against the condition where IPC socket will call OnClose for every |
| 501 | // packet it can't send. |
| 502 | if (connected()) { |
| 503 | set_connected(false); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 504 | |
| 505 | // Prevent the connection from being destroyed by redundant SignalClose |
| 506 | // events. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 507 | pretending_to_be_writable_ = true; |
| 508 | |
Mirko Bonadei | 0cb1cfa | 2022-02-25 10:45:32 +0000 | [diff] [blame] | 509 | // If this connection can't become connected and writable again in 5 |
| 510 | // seconds, it's time to tear this down. This is the case for the original |
| 511 | // TCP connection on passive side during a reconnect. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 512 | // We don't attempt reconnect right here. This is to avoid a case where the |
| 513 | // shutdown is intentional and reconnect is not necessary. We only reconnect |
| 514 | // when the connection is used to Send() or Ping(). |
Tommi | 1043fcd | 2022-05-26 13:54:35 +0200 | [diff] [blame] | 515 | network_thread()->PostDelayedTask( |
Danil Chapovalov | 7b19036 | 2022-07-07 14:13:02 +0200 | [diff] [blame^] | 516 | SafeTask(network_safety_.flag(), |
| 517 | [this]() { |
| 518 | if (pretending_to_be_writable_) { |
| 519 | Destroy(); |
| 520 | } |
| 521 | }), |
| 522 | TimeDelta::Millis(reconnection_timeout())); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 523 | } else if (!pretending_to_be_writable_) { |
| 524 | // OnClose could be called when the underneath socket times out during the |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 525 | // initial connect() (i.e. `pretending_to_be_writable_` is false) . We have |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 526 | // to manually destroy here as this connection, as never connected, will not |
| 527 | // be scheduled for ping to trigger destroy. |
Tomas Gunnarsson | f15189d | 2022-04-13 09:03:52 +0000 | [diff] [blame] | 528 | socket_->UnsubscribeClose(this); |
Tommi | 6fd77f3 | 2022-06-09 21:45:56 +0200 | [diff] [blame] | 529 | port()->DestroyConnectionAsync(this); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 533 | void TCPConnection::MaybeReconnect() { |
| 534 | // Only reconnect for an outgoing TCPConnection when OnClose was signaled and |
| 535 | // no outstanding reconnect is pending. |
| 536 | if (connected() || connection_pending_ || !outgoing_) { |
| 537 | return; |
| 538 | } |
| 539 | |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 540 | RTC_LOG(LS_INFO) << ToString() |
| 541 | << ": TCP Connection with remote is closed, " |
| 542 | "trying to reconnect"; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 543 | |
| 544 | CreateOutgoingTcpSocket(); |
| 545 | error_ = EPIPE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 548 | void TCPConnection::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 549 | const char* data, |
| 550 | size_t size, |
| 551 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 552 | const int64_t& packet_time_us) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 553 | RTC_DCHECK(socket == socket_.get()); |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 554 | Connection::OnReadPacket(data, size, packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 558 | RTC_DCHECK(socket == socket_.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 559 | Connection::OnReadyToSend(); |
| 560 | } |
| 561 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 562 | void TCPConnection::CreateOutgoingTcpSocket() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 563 | RTC_DCHECK(outgoing_); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 564 | int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME) |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 565 | ? rtc::PacketSocketFactory::OPT_TLS_FAKE |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 566 | : 0; |
Tomas Gunnarsson | f15189d | 2022-04-13 09:03:52 +0000 | [diff] [blame] | 567 | |
| 568 | if (socket_) { |
| 569 | socket_->UnsubscribeClose(this); |
| 570 | } |
| 571 | |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame] | 572 | rtc::PacketSocketTcpOptions tcp_opts; |
| 573 | tcp_opts.opts = opts; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 574 | socket_.reset(port()->socket_factory()->CreateClientTcpSocket( |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 575 | rtc::SocketAddress(port()->Network()->GetBestIP(), 0), |
| 576 | remote_candidate().address(), port()->proxy(), port()->user_agent(), |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame] | 577 | tcp_opts)); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 578 | if (socket_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 579 | RTC_LOG(LS_VERBOSE) << ToString() << ": Connecting from " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 580 | << socket_->GetLocalAddress().ToSensitiveString() |
| 581 | << " to " |
| 582 | << remote_candidate().address().ToSensitiveString(); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 583 | set_connected(false); |
| 584 | connection_pending_ = true; |
| 585 | ConnectSocketSignals(socket_.get()); |
| 586 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 587 | RTC_LOG(LS_WARNING) << ToString() << ": Failed to create connection to " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 588 | << remote_candidate().address().ToSensitiveString(); |
Mirko Bonadei | 0cb1cfa | 2022-02-25 10:45:32 +0000 | [diff] [blame] | 589 | set_state(IceCandidatePairState::FAILED); |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 590 | // We can't FailAndPrune directly here. FailAndPrune and deletes all |
| 591 | // the StunRequests from the request_map_. And if this is in the stack |
| 592 | // of Connection::Ping(), we are still using the request. |
| 593 | // Unwind the stack and defer the FailAndPrune. |
Tommi | 1043fcd | 2022-05-26 13:54:35 +0200 | [diff] [blame] | 594 | network_thread()->PostTask( |
Danil Chapovalov | 7b19036 | 2022-07-07 14:13:02 +0200 | [diff] [blame^] | 595 | SafeTask(network_safety_.flag(), [this]() { FailAndPrune(); })); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) { |
| 600 | if (outgoing_) { |
| 601 | socket->SignalConnect.connect(this, &TCPConnection::OnConnect); |
| 602 | } |
| 603 | socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket); |
| 604 | socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend); |
Tommi | 1043fcd | 2022-05-26 13:54:35 +0200 | [diff] [blame] | 605 | socket->SubscribeClose(this, [this, safety = network_safety_.flag()]( |
| 606 | rtc::AsyncPacketSocket* s, int err) { |
| 607 | if (safety->alive()) |
| 608 | OnClose(s, err); |
| 609 | }); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 610 | } |
| 611 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 612 | } // namespace cricket |