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