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