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