blob: 79843d82de729eba851360513815e0334a5b7c99 [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"
nisseede5da42017-01-12 05:15:36 -080070#include "webrtc/base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071#include "webrtc/base/logging.h"
72
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
99}
100
101bool 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
118TCPPort::~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
126Connection* TCPPort::CreateConnection(const Candidate& address,
127 CandidateOrigin origin) {
Honghai Zhangf9945b22015-12-15 12:20:13 -0800128 if (!SupportsProtocol(address.protocol())) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 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)) {
deadbeef06878292017-04-21 14:22:23 -0700160 // Incoming connection; we already created a socket and connected signals,
161 // so we need to hand off the "read packet" responsibility to
162 // TCPConnection.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 socket->SignalReadPacket.disconnect(this);
164 conn = new TCPConnection(this, address, socket);
165 } else {
deadbeef06878292017-04-21 14:22:23 -0700166 // Outgoing connection, which will create a new socket for which we still
167 // need to connect SignalReadyToSend and SignalSentPacket.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 conn = new TCPConnection(this, address);
deadbeef06878292017-04-21 14:22:23 -0700169 if (conn->socket()) {
170 conn->socket()->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
171 conn->socket()->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
172 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 }
honghaiz36f50e82016-06-01 15:57:03 -0700174 AddOrReplaceConnection(conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175 return conn;
176}
177
178void TCPPort::PrepareAddress() {
179 if (socket_) {
180 // If socket isn't bound yet the address will be added in
181 // OnAddressReady(). Socket may be in the CLOSED state if Listen()
182 // failed, we still want to add the socket address.
183 LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
184 << socket_->GetState();
185 if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND ||
186 socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED)
187 AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(),
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700188 rtc::SocketAddress(), TCP_PROTOCOL_NAME, "",
189 TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
zhihuang26d99c22017-02-13 12:47:27 -0800190 ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 } else {
192 LOG_J(LS_INFO, this) << "Not listening due to firewall restrictions.";
193 // Note: We still add the address, since otherwise the remote side won't
Guo-wei Shieh310b0932015-11-17 19:15:50 -0800194 // recognize our incoming TCP connections. According to
195 // https://tools.ietf.org/html/rfc6544#section-4.5, for active candidate,
196 // the port must be set to the discard port, i.e. 9.
197 AddAddress(rtc::SocketAddress(ip(), DISCARD_PORT),
198 rtc::SocketAddress(ip(), 0), rtc::SocketAddress(),
199 TCP_PROTOCOL_NAME, "", TCPTYPE_ACTIVE_STR, LOCAL_PORT_TYPE,
zhihuang26d99c22017-02-13 12:47:27 -0800200 ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 }
202}
203
204int TCPPort::SendTo(const void* data, size_t size,
205 const rtc::SocketAddress& addr,
206 const rtc::PacketOptions& options,
207 bool payload) {
208 rtc::AsyncPacketSocket * socket = NULL;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700209 TCPConnection* conn = static_cast<TCPConnection*>(GetConnection(addr));
210
211 // For Connection, this is the code path used by Ping() to establish
212 // WRITABLE. It has to send through the socket directly as TCPConnection::Send
213 // checks writability.
214 if (conn) {
215 if (!conn->connected()) {
216 conn->MaybeReconnect();
217 return SOCKET_ERROR;
218 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 socket = conn->socket();
220 } else {
221 socket = GetIncoming(addr);
222 }
223 if (!socket) {
224 LOG_J(LS_ERROR, this) << "Attempted to send to an unknown destination, "
225 << addr.ToSensitiveString();
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700226 return SOCKET_ERROR; // TODO(tbd): Set error_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227 }
228
229 int sent = socket->Send(data, size, options);
230 if (sent < 0) {
231 error_ = socket->GetError();
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700232 // Error from this code path for a Connection (instead of from a bare
233 // socket) will not trigger reconnecting. In theory, this shouldn't matter
234 // as OnClose should always be called and set connected to false.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 LOG_J(LS_ERROR, this) << "TCP send of " << size
236 << " bytes failed with error " << error_;
237 }
238 return sent;
239}
240
241int TCPPort::GetOption(rtc::Socket::Option opt, int* value) {
242 if (socket_) {
243 return socket_->GetOption(opt, value);
244 } else {
245 return SOCKET_ERROR;
246 }
247}
248
249int TCPPort::SetOption(rtc::Socket::Option opt, int value) {
250 if (socket_) {
251 return socket_->SetOption(opt, value);
252 } else {
253 return SOCKET_ERROR;
254 }
255}
256
257int TCPPort::GetError() {
258 return error_;
259}
260
261void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket,
262 rtc::AsyncPacketSocket* new_socket) {
nisseede5da42017-01-12 05:15:36 -0800263 RTC_DCHECK(socket == socket_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000264
265 Incoming incoming;
266 incoming.addr = new_socket->GetRemoteAddress();
267 incoming.socket = new_socket;
268 incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket);
269 incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100270 incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000271
272 LOG_J(LS_VERBOSE, this) << "Accepted connection from "
273 << incoming.addr.ToSensitiveString();
274 incoming_.push_back(incoming);
275}
276
277rtc::AsyncPacketSocket* TCPPort::GetIncoming(
278 const rtc::SocketAddress& addr, bool remove) {
279 rtc::AsyncPacketSocket* socket = NULL;
280 for (std::list<Incoming>::iterator it = incoming_.begin();
281 it != incoming_.end(); ++it) {
282 if (it->addr == addr) {
283 socket = it->socket;
284 if (remove)
285 incoming_.erase(it);
286 break;
287 }
288 }
289 return socket;
290}
291
292void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket,
293 const char* data, size_t size,
294 const rtc::SocketAddress& remote_addr,
295 const rtc::PacketTime& packet_time) {
296 Port::OnReadPacket(data, size, remote_addr, PROTO_TCP);
297}
298
Stefan Holmer55674ff2016-01-14 15:49:16 +0100299void TCPPort::OnSentPacket(rtc::AsyncPacketSocket* socket,
300 const rtc::SentPacket& sent_packet) {
Stefan Holmer55674ff2016-01-14 15:49:16 +0100301 PortInterface::SignalSentPacket(sent_packet);
302}
303
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000304void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
305 Port::OnReadyToSend();
306}
307
308void TCPPort::OnAddressReady(rtc::AsyncPacketSocket* socket,
309 const rtc::SocketAddress& address) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700310 AddAddress(address, address, rtc::SocketAddress(), TCP_PROTOCOL_NAME, "",
311 TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP,
zhihuang26d99c22017-02-13 12:47:27 -0800312 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000313}
314
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700315TCPConnection::TCPConnection(TCPPort* port,
316 const Candidate& candidate,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000317 rtc::AsyncPacketSocket* socket)
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700318 : Connection(port, 0, candidate),
319 socket_(socket),
320 error_(0),
321 outgoing_(socket == NULL),
322 connection_pending_(false),
323 pretending_to_be_writable_(false),
324 reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) {
325 if (outgoing_) {
326 CreateOutgoingTcpSocket();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000327 } else {
328 // Incoming connections should match the network address.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700329 LOG_J(LS_VERBOSE, this)
330 << "socket ipaddr: " << socket_->GetLocalAddress().ToString()
331 << ",port() ip:" << port->ip().ToString();
nisseede5da42017-01-12 05:15:36 -0800332 RTC_DCHECK(socket_->GetLocalAddress().ipaddr() == port->ip());
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700333 ConnectSocketSignals(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334 }
335}
336
337TCPConnection::~TCPConnection() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000338}
339
340int TCPConnection::Send(const void* data, size_t size,
341 const rtc::PacketOptions& options) {
342 if (!socket_) {
343 error_ = ENOTCONN;
344 return SOCKET_ERROR;
345 }
346
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700347 // Sending after OnClose on active side will trigger a reconnect for a
348 // outgoing connection. Note that the write state is still WRITABLE as we want
349 // to spend a few seconds attempting a reconnect before saying we're
350 // unwritable.
351 if (!connected()) {
352 MaybeReconnect();
353 return SOCKET_ERROR;
354 }
355
356 // Note that this is important to put this after the previous check to give
357 // the connection a chance to reconnect.
358 if (pretending_to_be_writable_ || write_state() != STATE_WRITABLE) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000359 // TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error?
skvladc309e0e2016-07-28 17:15:20 -0700360 error_ = ENOTCONN;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000361 return SOCKET_ERROR;
362 }
zhihuang5ecf16c2016-06-01 17:09:15 -0700363 stats_.sent_total_packets++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 int sent = socket_->Send(data, size, options);
365 if (sent < 0) {
zhihuang5ecf16c2016-06-01 17:09:15 -0700366 stats_.sent_discarded_packets++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000367 error_ = socket_->GetError();
368 } else {
Tim Psiaki63046262015-09-14 10:38:08 -0700369 send_rate_tracker_.AddSamples(sent);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000370 }
371 return sent;
372}
373
374int TCPConnection::GetError() {
375 return error_;
376}
377
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700378void TCPConnection::OnConnectionRequestResponse(ConnectionRequest* req,
379 StunMessage* response) {
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700380 // Process the STUN response before we inform upper layer ready to send.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700381 Connection::OnConnectionRequestResponse(req, response);
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700382
383 // If we're in the state of pretending to be writeable, we should inform the
384 // upper layer it's ready to send again as previous EWOULDLBLOCK from socket
385 // would have stopped the outgoing stream.
386 if (pretending_to_be_writable_) {
387 Connection::OnReadyToSend();
388 }
389 pretending_to_be_writable_ = false;
nisseede5da42017-01-12 05:15:36 -0800390 RTC_DCHECK(write_state() == STATE_WRITABLE);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700391}
392
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000393void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800394 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000395 // Do not use this connection if the socket bound to a different address than
396 // the one we asked for. This is seen in Chrome, where TCP sockets cannot be
397 // given a binding address, and the platform is expected to pick the
398 // correct local address.
tommi5ce1a2a2016-05-14 03:19:31 -0700399 const rtc::SocketAddress& socket_addr = socket->GetLocalAddress();
400 if (socket_addr.ipaddr() == port()->ip()) {
401 LOG_J(LS_VERBOSE, this) << "Connection established to "
402 << socket->GetRemoteAddress().ToSensitiveString();
403 } else if (IPIsAny(port()->ip())) {
404 LOG(LS_WARNING) << "Socket is bound to a different address:"
405 << socket_addr.ipaddr().ToString()
406 << ", rather then the local port:"
407 << port()->ip().ToString()
408 << ". Still allowing it since it's any address"
409 << ", possibly caused by multi-routes being disabled.";
410 } else if (socket_addr.IsLoopbackIP()) {
411 LOG(LS_WARNING) << "Socket is bound to a different address:"
412 << socket_addr.ipaddr().ToString()
413 << ", rather then the local port:"
414 << port()->ip().ToString()
415 << ". Still allowing it since it's localhost.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000416 } else {
henrike@webrtc.org43e033e2014-11-10 19:40:29 +0000417 LOG_J(LS_WARNING, this) << "Dropping connection as TCP socket bound to IP "
tommi5ce1a2a2016-05-14 03:19:31 -0700418 << socket_addr.ipaddr().ToSensitiveString()
henrike@webrtc.org43e033e2014-11-10 19:40:29 +0000419 << ", different from the local candidate IP "
420 << port()->ip().ToSensitiveString();
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700421 OnClose(socket, 0);
tommi5ce1a2a2016-05-14 03:19:31 -0700422 return;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000423 }
tommi5ce1a2a2016-05-14 03:19:31 -0700424
425 // Connection is established successfully.
426 set_connected(true);
427 connection_pending_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000428}
429
430void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) {
nisseede5da42017-01-12 05:15:36 -0800431 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org43e033e2014-11-10 19:40:29 +0000432 LOG_J(LS_INFO, this) << "Connection closed with error " << error;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700433
434 // Guard against the condition where IPC socket will call OnClose for every
435 // packet it can't send.
436 if (connected()) {
437 set_connected(false);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700438
439 // Prevent the connection from being destroyed by redundant SignalClose
440 // events.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700441 pretending_to_be_writable_ = true;
442
443 // We don't attempt reconnect right here. This is to avoid a case where the
444 // shutdown is intentional and reconnect is not necessary. We only reconnect
445 // when the connection is used to Send() or Ping().
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700446 port()->thread()->PostDelayed(RTC_FROM_HERE, reconnection_timeout(), this,
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700447 MSG_TCPCONNECTION_DELAYED_ONCLOSE);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700448 } else if (!pretending_to_be_writable_) {
449 // OnClose could be called when the underneath socket times out during the
450 // initial connect() (i.e. |pretending_to_be_writable_| is false) . We have
451 // to manually destroy here as this connection, as never connected, will not
452 // be scheduled for ping to trigger destroy.
453 Destroy();
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700454 }
455}
456
457void TCPConnection::OnMessage(rtc::Message* pmsg) {
458 switch (pmsg->message_id) {
459 case MSG_TCPCONNECTION_DELAYED_ONCLOSE:
460 // If this connection can't become connected and writable again in 5
461 // seconds, it's time to tear this down. This is the case for the original
462 // TCP connection on passive side during a reconnect.
463 if (pretending_to_be_writable_) {
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700464 Destroy();
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700465 }
466 break;
467 default:
468 Connection::OnMessage(pmsg);
469 }
470}
471
472void TCPConnection::MaybeReconnect() {
473 // Only reconnect for an outgoing TCPConnection when OnClose was signaled and
474 // no outstanding reconnect is pending.
475 if (connected() || connection_pending_ || !outgoing_) {
476 return;
477 }
478
479 LOG_J(LS_INFO, this) << "TCP Connection with remote is closed, "
480 << "trying to reconnect";
481
482 CreateOutgoingTcpSocket();
483 error_ = EPIPE;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000484}
485
486void TCPConnection::OnReadPacket(
487 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
488 const rtc::SocketAddress& remote_addr,
489 const rtc::PacketTime& packet_time) {
nisseede5da42017-01-12 05:15:36 -0800490 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000491 Connection::OnReadPacket(data, size, packet_time);
492}
493
494void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800495 RTC_DCHECK(socket == socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000496 Connection::OnReadyToSend();
497}
498
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700499void TCPConnection::CreateOutgoingTcpSocket() {
nisseede5da42017-01-12 05:15:36 -0800500 RTC_DCHECK(outgoing_);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700501 // TODO(guoweis): Handle failures here (unlikely since TCP).
502 int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME)
hnsl04833622017-01-09 08:35:45 -0800503 ? rtc::PacketSocketFactory::OPT_TLS_FAKE
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700504 : 0;
505 socket_.reset(port()->socket_factory()->CreateClientTcpSocket(
506 rtc::SocketAddress(port()->ip(), 0), remote_candidate().address(),
507 port()->proxy(), port()->user_agent(), opts));
508 if (socket_) {
509 LOG_J(LS_VERBOSE, this)
510 << "Connecting from " << socket_->GetLocalAddress().ToSensitiveString()
511 << " to " << remote_candidate().address().ToSensitiveString();
512 set_connected(false);
513 connection_pending_ = true;
514 ConnectSocketSignals(socket_.get());
515 } else {
516 LOG_J(LS_WARNING, this) << "Failed to create connection to "
517 << remote_candidate().address().ToSensitiveString();
518 }
519}
520
521void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) {
522 if (outgoing_) {
523 socket->SignalConnect.connect(this, &TCPConnection::OnConnect);
524 }
525 socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket);
526 socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend);
527 socket->SignalClose.connect(this, &TCPConnection::OnClose);
528}
529
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000530} // namespace cricket