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