blob: 4fd527e5132a6a25e67b38e18d5adb4b48da5d01 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 Shiehbe508a12015-04-06 12:48:47 -070011/*
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 Shieh1eb87c72015-08-25 11:02:55 -070032 * 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 Shiehbe508a12015-04-06 12:48:47 -070058 * 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.org269fb4b2014-10-28 22:20:11 +000067#include "webrtc/p2p/base/tcpport.h"
68
69#include "webrtc/p2p/base/common.h"
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020070#include "webrtc/base/checks.h"
71#include "webrtc/base/logging.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072
73namespace cricket {
74
75TCPPort::TCPPort(rtc::Thread* thread,
76 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000077 rtc::Network* network,
78 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 uint16_t min_port,
80 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000081 const std::string& username,
82 const std::string& password,
83 bool allow_listen)
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 : Port(thread,
85 LOCAL_PORT_TYPE,
86 factory,
87 network,
88 ip,
89 min_port,
90 max_port,
91 username,
92 password),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 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.org269fb4b2014-10-28 22:20:11 +000099 if (allow_listen_) {
deadbeef1ee21252017-06-13 15:49:45 -0700100 TryCreateServerSocket();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102}
103
104TCPPort::~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
112Connection* TCPPort::CreateConnection(const Candidate& address,
113 CandidateOrigin origin) {
Honghai Zhangf9945b22015-12-15 12:20:13 -0800114 if (!SupportsProtocol(address.protocol())) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 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)) {
deadbeef06878292017-04-21 14:22:23 -0700146 // 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.org269fb4b2014-10-28 22:20:11 +0000149 socket->SignalReadPacket.disconnect(this);
150 conn = new TCPConnection(this, address, socket);
151 } else {
deadbeef06878292017-04-21 14:22:23 -0700152 // Outgoing connection, which will create a new socket for which we still
153 // need to connect SignalReadyToSend and SignalSentPacket.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154 conn = new TCPConnection(this, address);
deadbeef06878292017-04-21 14:22:23 -0700155 if (conn->socket()) {
156 conn->socket()->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
157 conn->socket()->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
158 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159 }
honghaiz36f50e82016-06-01 15:57:03 -0700160 AddOrReplaceConnection(conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 return conn;
162}
163
164void 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 Shieh3d564c12015-08-19 16:51:15 -0700174 rtc::SocketAddress(), TCP_PROTOCOL_NAME, "",
175 TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
zhihuang26d99c22017-02-13 12:47:27 -0800176 ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 } 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 Shieh310b0932015-11-17 19:15:50 -0800180 // 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,
zhihuang26d99c22017-02-13 12:47:27 -0800186 ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187 }
188}
189
190int 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 Shiehbe508a12015-04-06 12:48:47 -0700195 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.org269fb4b2014-10-28 22:20:11 +0000205 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 Shiehbe508a12015-04-06 12:48:47 -0700212 return SOCKET_ERROR; // TODO(tbd): Set error_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 }
214
215 int sent = socket->Send(data, size, options);
216 if (sent < 0) {
217 error_ = socket->GetError();
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700218 // 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.org269fb4b2014-10-28 22:20:11 +0000221 LOG_J(LS_ERROR, this) << "TCP send of " << size
222 << " bytes failed with error " << error_;
223 }
224 return sent;
225}
226
227int 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
235int 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
243int TCPPort::GetError() {
244 return error_;
245}
246
247void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket,
248 rtc::AsyncPacketSocket* new_socket) {
nisseede5da42017-01-12 05:15:36 -0800249 RTC_DCHECK(socket == socket_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000250
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 Holmer55674ff2016-01-14 15:49:16 +0100256 incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257
258 LOG_J(LS_VERBOSE, this) << "Accepted connection from "
259 << incoming.addr.ToSensitiveString();
260 incoming_.push_back(incoming);
261}
262
deadbeef1ee21252017-06-13 15:49:45 -0700263void 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.org269fb4b2014-10-28 22:20:11 +0000275rtc::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
290void 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 Holmer55674ff2016-01-14 15:49:16 +0100297void TCPPort::OnSentPacket(rtc::AsyncPacketSocket* socket,
298 const rtc::SentPacket& sent_packet) {
Stefan Holmer55674ff2016-01-14 15:49:16 +0100299 PortInterface::SignalSentPacket(sent_packet);
300}
301
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
303 Port::OnReadyToSend();
304}
305
306void TCPPort::OnAddressReady(rtc::AsyncPacketSocket* socket,
307 const rtc::SocketAddress& address) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700308 AddAddress(address, address, rtc::SocketAddress(), TCP_PROTOCOL_NAME, "",
309 TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP,
zhihuang26d99c22017-02-13 12:47:27 -0800310 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000311}
312
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700313TCPConnection::TCPConnection(TCPPort* port,
314 const Candidate& candidate,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000315 rtc::AsyncPacketSocket* socket)
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700316 : 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.org269fb4b2014-10-28 22:20:11 +0000325 } else {
326 // Incoming connections should match the network address.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700327 LOG_J(LS_VERBOSE, this)
328 << "socket ipaddr: " << socket_->GetLocalAddress().ToString()
329 << ",port() ip:" << port->ip().ToString();
nisseede5da42017-01-12 05:15:36 -0800330 RTC_DCHECK(socket_->GetLocalAddress().ipaddr() == port->ip());
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700331 ConnectSocketSignals(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000332 }
333}
334
335TCPConnection::~TCPConnection() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336}
337
338int 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 Shiehbe508a12015-04-06 12:48:47 -0700345 // 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.org269fb4b2014-10-28 22:20:11 +0000357 // TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error?
skvladc309e0e2016-07-28 17:15:20 -0700358 error_ = ENOTCONN;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000359 return SOCKET_ERROR;
360 }
zhihuang5ecf16c2016-06-01 17:09:15 -0700361 stats_.sent_total_packets++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000362 int sent = socket_->Send(data, size, options);
363 if (sent < 0) {
zhihuang5ecf16c2016-06-01 17:09:15 -0700364 stats_.sent_discarded_packets++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000365 error_ = socket_->GetError();
366 } else {
Tim Psiaki63046262015-09-14 10:38:08 -0700367 send_rate_tracker_.AddSamples(sent);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000368 }
369 return sent;
370}
371
372int TCPConnection::GetError() {
373 return error_;
374}
375
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700376void TCPConnection::OnConnectionRequestResponse(ConnectionRequest* req,
377 StunMessage* response) {
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700378 // Process the STUN response before we inform upper layer ready to send.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700379 Connection::OnConnectionRequestResponse(req, response);
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700380
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;
nisseede5da42017-01-12 05:15:36 -0800388 RTC_DCHECK(write_state() == STATE_WRITABLE);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700389}
390
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000391void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800392 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000393 // 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.
tommi5ce1a2a2016-05-14 03:19:31 -0700397 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.org269fb4b2014-10-28 22:20:11 +0000414 } else {
henrike@webrtc.org43e033e2014-11-10 19:40:29 +0000415 LOG_J(LS_WARNING, this) << "Dropping connection as TCP socket bound to IP "
tommi5ce1a2a2016-05-14 03:19:31 -0700416 << socket_addr.ipaddr().ToSensitiveString()
henrike@webrtc.org43e033e2014-11-10 19:40:29 +0000417 << ", different from the local candidate IP "
418 << port()->ip().ToSensitiveString();
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700419 OnClose(socket, 0);
tommi5ce1a2a2016-05-14 03:19:31 -0700420 return;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000421 }
tommi5ce1a2a2016-05-14 03:19:31 -0700422
423 // Connection is established successfully.
424 set_connected(true);
425 connection_pending_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000426}
427
428void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) {
nisseede5da42017-01-12 05:15:36 -0800429 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org43e033e2014-11-10 19:40:29 +0000430 LOG_J(LS_INFO, this) << "Connection closed with error " << error;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700431
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 Shieh1eb87c72015-08-25 11:02:55 -0700436
437 // Prevent the connection from being destroyed by redundant SignalClose
438 // events.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700439 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 Brandstetter5d97a9a2016-06-10 14:17:27 -0700444 port()->thread()->PostDelayed(RTC_FROM_HERE, reconnection_timeout(), this,
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700445 MSG_TCPCONNECTION_DELAYED_ONCLOSE);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700446 } 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 Shiehbe508a12015-04-06 12:48:47 -0700452 }
453}
454
455void 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 Shieh1eb87c72015-08-25 11:02:55 -0700462 Destroy();
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700463 }
464 break;
465 default:
466 Connection::OnMessage(pmsg);
467 }
468}
469
470void 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.org269fb4b2014-10-28 22:20:11 +0000482}
483
484void TCPConnection::OnReadPacket(
485 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
486 const rtc::SocketAddress& remote_addr,
487 const rtc::PacketTime& packet_time) {
nisseede5da42017-01-12 05:15:36 -0800488 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000489 Connection::OnReadPacket(data, size, packet_time);
490}
491
492void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800493 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000494 Connection::OnReadyToSend();
495}
496
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700497void TCPConnection::CreateOutgoingTcpSocket() {
nisseede5da42017-01-12 05:15:36 -0800498 RTC_DCHECK(outgoing_);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700499 // TODO(guoweis): Handle failures here (unlikely since TCP).
500 int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME)
hnsl04833622017-01-09 08:35:45 -0800501 ? rtc::PacketSocketFactory::OPT_TLS_FAKE
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700502 : 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
519void 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.org269fb4b2014-10-28 22:20:11 +0000528} // namespace cricket