1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker /*
12*d9f75844SAndroid Build Coastguard Worker * This is a diagram of how TCP reconnect works for the active side. The
13*d9f75844SAndroid Build Coastguard Worker * passive side just waits for an incoming connection.
14*d9f75844SAndroid Build Coastguard Worker *
15*d9f75844SAndroid Build Coastguard Worker * - Connected: Indicate whether the TCP socket is connected.
16*d9f75844SAndroid Build Coastguard Worker *
17*d9f75844SAndroid Build Coastguard Worker * - Writable: Whether the stun binding is completed. Sending a data packet
18*d9f75844SAndroid Build Coastguard Worker * before stun binding completed will trigger IPC socket layer to shutdown
19*d9f75844SAndroid Build Coastguard Worker * the connection.
20*d9f75844SAndroid Build Coastguard Worker *
21*d9f75844SAndroid Build Coastguard Worker * - PendingTCP: `connection_pending_` indicates whether there is an
22*d9f75844SAndroid Build Coastguard Worker * outstanding TCP connection in progress.
23*d9f75844SAndroid Build Coastguard Worker *
24*d9f75844SAndroid Build Coastguard Worker * - PretendWri: Tracked by `pretending_to_be_writable_`. Marking connection as
25*d9f75844SAndroid Build Coastguard Worker * WRITE_TIMEOUT will cause the connection be deleted. Instead, we're
26*d9f75844SAndroid Build Coastguard Worker * "pretending" we're still writable for a period of time such that reconnect
27*d9f75844SAndroid Build Coastguard Worker * could work.
28*d9f75844SAndroid Build Coastguard Worker *
29*d9f75844SAndroid Build Coastguard Worker * Data could only be sent in state 3. Sening data during state 2 & 6 will get
30*d9f75844SAndroid Build Coastguard Worker * EWOULDBLOCK, 4 & 5 EPIPE.
31*d9f75844SAndroid Build Coastguard Worker *
32*d9f75844SAndroid Build Coastguard Worker * OS Timeout 7 -------------+
33*d9f75844SAndroid Build Coastguard Worker * +----------------------->|Connected: N |
34*d9f75844SAndroid Build Coastguard Worker * | |Writable: N | Timeout
35*d9f75844SAndroid Build Coastguard Worker * | Timeout |Connection is |<----------------+
36*d9f75844SAndroid Build Coastguard Worker * | +------------------->|Dead | |
37*d9f75844SAndroid Build Coastguard Worker * | | +--------------+ |
38*d9f75844SAndroid Build Coastguard Worker * | | ^ |
39*d9f75844SAndroid Build Coastguard Worker * | | OnClose | |
40*d9f75844SAndroid Build Coastguard Worker * | | +-----------------------+ | |
41*d9f75844SAndroid Build Coastguard Worker * | | | | |Timeout |
42*d9f75844SAndroid Build Coastguard Worker * | | v | | |
43*d9f75844SAndroid Build Coastguard Worker * | 4 +----------+ 5 -----+--+--+ 6 -----+-----+
44*d9f75844SAndroid Build Coastguard Worker * | |Connected: N|Send() or |Connected: N| |Connected: Y|
45*d9f75844SAndroid Build Coastguard Worker * | |Writable: Y|Ping() |Writable: Y|OnConnect |Writable: Y|
46*d9f75844SAndroid Build Coastguard Worker * | |PendingTCP:N+--------> |PendingTCP:Y+---------> |PendingTCP:N|
47*d9f75844SAndroid Build Coastguard Worker * | |PretendWri:Y| |PretendWri:Y| |PretendWri:Y|
48*d9f75844SAndroid Build Coastguard Worker * | +-----+------+ +------------+ +---+--+-----+
49*d9f75844SAndroid Build Coastguard Worker * | ^ ^ | |
50*d9f75844SAndroid Build Coastguard Worker * | | | OnClose | |
51*d9f75844SAndroid Build Coastguard Worker * | | +----------------------------------------------+ |
52*d9f75844SAndroid Build Coastguard Worker * | | |
53*d9f75844SAndroid Build Coastguard Worker * | | Stun Binding Completed |
54*d9f75844SAndroid Build Coastguard Worker * | | |
55*d9f75844SAndroid Build Coastguard Worker * | | OnClose |
56*d9f75844SAndroid Build Coastguard Worker * | +------------------------------------------------+ |
57*d9f75844SAndroid Build Coastguard Worker * | | v
58*d9f75844SAndroid Build Coastguard Worker * 1 -----------+ 2 -----------+Stun 3 -----------+
59*d9f75844SAndroid Build Coastguard Worker * |Connected: N| |Connected: Y|Binding |Connected: Y|
60*d9f75844SAndroid Build Coastguard Worker * |Writable: N|OnConnect |Writable: N|Completed |Writable: Y|
61*d9f75844SAndroid Build Coastguard Worker * |PendingTCP:Y+---------> |PendingTCP:N+--------> |PendingTCP:N|
62*d9f75844SAndroid Build Coastguard Worker * |PretendWri:N| |PretendWri:N| |PretendWri:N|
63*d9f75844SAndroid Build Coastguard Worker * +------------+ +------------+ +------------+
64*d9f75844SAndroid Build Coastguard Worker *
65*d9f75844SAndroid Build Coastguard Worker */
66*d9f75844SAndroid Build Coastguard Worker
67*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/tcp_port.h"
68*d9f75844SAndroid Build Coastguard Worker
69*d9f75844SAndroid Build Coastguard Worker #include <errno.h>
70*d9f75844SAndroid Build Coastguard Worker
71*d9f75844SAndroid Build Coastguard Worker #include <utility>
72*d9f75844SAndroid Build Coastguard Worker #include <vector>
73*d9f75844SAndroid Build Coastguard Worker
74*d9f75844SAndroid Build Coastguard Worker #include "absl/algorithm/container.h"
75*d9f75844SAndroid Build Coastguard Worker #include "absl/memory/memory.h"
76*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
77*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/pending_task_safety_flag.h"
78*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
79*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/p2p_constants.h"
80*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
81*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ip_address.h"
82*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
83*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/net_helper.h"
84*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/rate_tracker.h"
85*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/third_party/sigslot/sigslot.h"
86*d9f75844SAndroid Build Coastguard Worker
87*d9f75844SAndroid Build Coastguard Worker namespace cricket {
88*d9f75844SAndroid Build Coastguard Worker using ::webrtc::SafeTask;
89*d9f75844SAndroid Build Coastguard Worker using ::webrtc::TimeDelta;
90*d9f75844SAndroid Build Coastguard Worker
TCPPort(rtc::Thread * thread,rtc::PacketSocketFactory * factory,const rtc::Network * network,uint16_t min_port,uint16_t max_port,absl::string_view username,absl::string_view password,bool allow_listen,const webrtc::FieldTrialsView * field_trials)91*d9f75844SAndroid Build Coastguard Worker TCPPort::TCPPort(rtc::Thread* thread,
92*d9f75844SAndroid Build Coastguard Worker rtc::PacketSocketFactory* factory,
93*d9f75844SAndroid Build Coastguard Worker const rtc::Network* network,
94*d9f75844SAndroid Build Coastguard Worker uint16_t min_port,
95*d9f75844SAndroid Build Coastguard Worker uint16_t max_port,
96*d9f75844SAndroid Build Coastguard Worker absl::string_view username,
97*d9f75844SAndroid Build Coastguard Worker absl::string_view password,
98*d9f75844SAndroid Build Coastguard Worker bool allow_listen,
99*d9f75844SAndroid Build Coastguard Worker const webrtc::FieldTrialsView* field_trials)
100*d9f75844SAndroid Build Coastguard Worker : Port(thread,
101*d9f75844SAndroid Build Coastguard Worker LOCAL_PORT_TYPE,
102*d9f75844SAndroid Build Coastguard Worker factory,
103*d9f75844SAndroid Build Coastguard Worker network,
104*d9f75844SAndroid Build Coastguard Worker min_port,
105*d9f75844SAndroid Build Coastguard Worker max_port,
106*d9f75844SAndroid Build Coastguard Worker username,
107*d9f75844SAndroid Build Coastguard Worker password,
108*d9f75844SAndroid Build Coastguard Worker field_trials),
109*d9f75844SAndroid Build Coastguard Worker allow_listen_(allow_listen),
110*d9f75844SAndroid Build Coastguard Worker error_(0) {
111*d9f75844SAndroid Build Coastguard Worker // TODO(mallinath) - Set preference value as per RFC 6544.
112*d9f75844SAndroid Build Coastguard Worker // http://b/issue?id=7141794
113*d9f75844SAndroid Build Coastguard Worker if (allow_listen_) {
114*d9f75844SAndroid Build Coastguard Worker TryCreateServerSocket();
115*d9f75844SAndroid Build Coastguard Worker }
116*d9f75844SAndroid Build Coastguard Worker // Set TCP_NODELAY (via OPT_NODELAY) for improved performance; this causes
117*d9f75844SAndroid Build Coastguard Worker // small media packets to be sent immediately rather than being buffered up,
118*d9f75844SAndroid Build Coastguard Worker // reducing latency.
119*d9f75844SAndroid Build Coastguard Worker SetOption(rtc::Socket::OPT_NODELAY, 1);
120*d9f75844SAndroid Build Coastguard Worker }
121*d9f75844SAndroid Build Coastguard Worker
~TCPPort()122*d9f75844SAndroid Build Coastguard Worker TCPPort::~TCPPort() {
123*d9f75844SAndroid Build Coastguard Worker listen_socket_ = nullptr;
124*d9f75844SAndroid Build Coastguard Worker std::list<Incoming>::iterator it;
125*d9f75844SAndroid Build Coastguard Worker for (it = incoming_.begin(); it != incoming_.end(); ++it)
126*d9f75844SAndroid Build Coastguard Worker delete it->socket;
127*d9f75844SAndroid Build Coastguard Worker incoming_.clear();
128*d9f75844SAndroid Build Coastguard Worker }
129*d9f75844SAndroid Build Coastguard Worker
CreateConnection(const Candidate & address,CandidateOrigin origin)130*d9f75844SAndroid Build Coastguard Worker Connection* TCPPort::CreateConnection(const Candidate& address,
131*d9f75844SAndroid Build Coastguard Worker CandidateOrigin origin) {
132*d9f75844SAndroid Build Coastguard Worker if (!SupportsProtocol(address.protocol())) {
133*d9f75844SAndroid Build Coastguard Worker return NULL;
134*d9f75844SAndroid Build Coastguard Worker }
135*d9f75844SAndroid Build Coastguard Worker
136*d9f75844SAndroid Build Coastguard Worker if ((address.tcptype() == TCPTYPE_ACTIVE_STR &&
137*d9f75844SAndroid Build Coastguard Worker address.type() != PRFLX_PORT_TYPE) ||
138*d9f75844SAndroid Build Coastguard Worker (address.tcptype().empty() && address.address().port() == 0)) {
139*d9f75844SAndroid Build Coastguard Worker // It's active only candidate, we should not try to create connections
140*d9f75844SAndroid Build Coastguard Worker // for these candidates.
141*d9f75844SAndroid Build Coastguard Worker return NULL;
142*d9f75844SAndroid Build Coastguard Worker }
143*d9f75844SAndroid Build Coastguard Worker
144*d9f75844SAndroid Build Coastguard Worker // We can't accept TCP connections incoming on other ports
145*d9f75844SAndroid Build Coastguard Worker if (origin == ORIGIN_OTHER_PORT)
146*d9f75844SAndroid Build Coastguard Worker return NULL;
147*d9f75844SAndroid Build Coastguard Worker
148*d9f75844SAndroid Build Coastguard Worker // We don't know how to act as an ssl server yet
149*d9f75844SAndroid Build Coastguard Worker if ((address.protocol() == SSLTCP_PROTOCOL_NAME) &&
150*d9f75844SAndroid Build Coastguard Worker (origin == ORIGIN_THIS_PORT)) {
151*d9f75844SAndroid Build Coastguard Worker return NULL;
152*d9f75844SAndroid Build Coastguard Worker }
153*d9f75844SAndroid Build Coastguard Worker
154*d9f75844SAndroid Build Coastguard Worker if (!IsCompatibleAddress(address.address())) {
155*d9f75844SAndroid Build Coastguard Worker return NULL;
156*d9f75844SAndroid Build Coastguard Worker }
157*d9f75844SAndroid Build Coastguard Worker
158*d9f75844SAndroid Build Coastguard Worker TCPConnection* conn = NULL;
159*d9f75844SAndroid Build Coastguard Worker if (rtc::AsyncPacketSocket* socket = GetIncoming(address.address(), true)) {
160*d9f75844SAndroid Build Coastguard Worker // Incoming connection; we already created a socket and connected signals,
161*d9f75844SAndroid Build Coastguard Worker // so we need to hand off the "read packet" responsibility to
162*d9f75844SAndroid Build Coastguard Worker // TCPConnection.
163*d9f75844SAndroid Build Coastguard Worker socket->SignalReadPacket.disconnect(this);
164*d9f75844SAndroid Build Coastguard Worker conn = new TCPConnection(NewWeakPtr(), address, socket);
165*d9f75844SAndroid Build Coastguard Worker } else {
166*d9f75844SAndroid Build Coastguard Worker // Outgoing connection, which will create a new socket for which we still
167*d9f75844SAndroid Build Coastguard Worker // need to connect SignalReadyToSend and SignalSentPacket.
168*d9f75844SAndroid Build Coastguard Worker conn = new TCPConnection(NewWeakPtr(), address);
169*d9f75844SAndroid Build Coastguard Worker if (conn->socket()) {
170*d9f75844SAndroid Build Coastguard Worker conn->socket()->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
171*d9f75844SAndroid Build Coastguard Worker conn->socket()->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
172*d9f75844SAndroid Build Coastguard Worker }
173*d9f75844SAndroid Build Coastguard Worker }
174*d9f75844SAndroid Build Coastguard Worker AddOrReplaceConnection(conn);
175*d9f75844SAndroid Build Coastguard Worker return conn;
176*d9f75844SAndroid Build Coastguard Worker }
177*d9f75844SAndroid Build Coastguard Worker
PrepareAddress()178*d9f75844SAndroid Build Coastguard Worker void TCPPort::PrepareAddress() {
179*d9f75844SAndroid Build Coastguard Worker if (listen_socket_) {
180*d9f75844SAndroid Build Coastguard Worker // Socket may be in the CLOSED state if Listen()
181*d9f75844SAndroid Build Coastguard Worker // failed, we still want to add the socket address.
182*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
183*d9f75844SAndroid Build Coastguard Worker << static_cast<int>(listen_socket_->GetState());
184*d9f75844SAndroid Build Coastguard Worker AddAddress(listen_socket_->GetLocalAddress(),
185*d9f75844SAndroid Build Coastguard Worker listen_socket_->GetLocalAddress(), rtc::SocketAddress(),
186*d9f75844SAndroid Build Coastguard Worker TCP_PROTOCOL_NAME, "", TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
187*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
188*d9f75844SAndroid Build Coastguard Worker } else {
189*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << ToString()
190*d9f75844SAndroid Build Coastguard Worker << ": Not listening due to firewall restrictions.";
191*d9f75844SAndroid Build Coastguard Worker // Note: We still add the address, since otherwise the remote side won't
192*d9f75844SAndroid Build Coastguard Worker // recognize our incoming TCP connections. According to
193*d9f75844SAndroid Build Coastguard Worker // https://tools.ietf.org/html/rfc6544#section-4.5, for active candidate,
194*d9f75844SAndroid Build Coastguard Worker // the port must be set to the discard port, i.e. 9. We can't be 100% sure
195*d9f75844SAndroid Build Coastguard Worker // which IP address will actually be used, so GetBestIP is as good as we
196*d9f75844SAndroid Build Coastguard Worker // can do.
197*d9f75844SAndroid Build Coastguard Worker // TODO(deadbeef): We could do something like create a dummy socket just to
198*d9f75844SAndroid Build Coastguard Worker // see what IP we get. But that may be overkill.
199*d9f75844SAndroid Build Coastguard Worker AddAddress(rtc::SocketAddress(Network()->GetBestIP(), DISCARD_PORT),
200*d9f75844SAndroid Build Coastguard Worker rtc::SocketAddress(Network()->GetBestIP(), 0),
201*d9f75844SAndroid Build Coastguard Worker rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", TCPTYPE_ACTIVE_STR,
202*d9f75844SAndroid Build Coastguard Worker LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
203*d9f75844SAndroid Build Coastguard Worker }
204*d9f75844SAndroid Build Coastguard Worker }
205*d9f75844SAndroid Build Coastguard Worker
SendTo(const void * data,size_t size,const rtc::SocketAddress & addr,const rtc::PacketOptions & options,bool payload)206*d9f75844SAndroid Build Coastguard Worker int TCPPort::SendTo(const void* data,
207*d9f75844SAndroid Build Coastguard Worker size_t size,
208*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& addr,
209*d9f75844SAndroid Build Coastguard Worker const rtc::PacketOptions& options,
210*d9f75844SAndroid Build Coastguard Worker bool payload) {
211*d9f75844SAndroid Build Coastguard Worker rtc::AsyncPacketSocket* socket = NULL;
212*d9f75844SAndroid Build Coastguard Worker TCPConnection* conn = static_cast<TCPConnection*>(GetConnection(addr));
213*d9f75844SAndroid Build Coastguard Worker
214*d9f75844SAndroid Build Coastguard Worker // For Connection, this is the code path used by Ping() to establish
215*d9f75844SAndroid Build Coastguard Worker // WRITABLE. It has to send through the socket directly as TCPConnection::Send
216*d9f75844SAndroid Build Coastguard Worker // checks writability.
217*d9f75844SAndroid Build Coastguard Worker if (conn) {
218*d9f75844SAndroid Build Coastguard Worker if (!conn->connected()) {
219*d9f75844SAndroid Build Coastguard Worker conn->MaybeReconnect();
220*d9f75844SAndroid Build Coastguard Worker return SOCKET_ERROR;
221*d9f75844SAndroid Build Coastguard Worker }
222*d9f75844SAndroid Build Coastguard Worker socket = conn->socket();
223*d9f75844SAndroid Build Coastguard Worker if (!socket) {
224*d9f75844SAndroid Build Coastguard Worker // The failure to initialize should have been logged elsewhere,
225*d9f75844SAndroid Build Coastguard Worker // so this log is not important.
226*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << ToString()
227*d9f75844SAndroid Build Coastguard Worker << ": Attempted to send to an uninitialized socket: "
228*d9f75844SAndroid Build Coastguard Worker << addr.ToSensitiveString();
229*d9f75844SAndroid Build Coastguard Worker error_ = EHOSTUNREACH;
230*d9f75844SAndroid Build Coastguard Worker return SOCKET_ERROR;
231*d9f75844SAndroid Build Coastguard Worker }
232*d9f75844SAndroid Build Coastguard Worker } else {
233*d9f75844SAndroid Build Coastguard Worker socket = GetIncoming(addr);
234*d9f75844SAndroid Build Coastguard Worker if (!socket) {
235*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_ERROR) << ToString()
236*d9f75844SAndroid Build Coastguard Worker << ": Attempted to send to an unknown destination: "
237*d9f75844SAndroid Build Coastguard Worker << addr.ToSensitiveString();
238*d9f75844SAndroid Build Coastguard Worker error_ = EHOSTUNREACH;
239*d9f75844SAndroid Build Coastguard Worker return SOCKET_ERROR;
240*d9f75844SAndroid Build Coastguard Worker }
241*d9f75844SAndroid Build Coastguard Worker }
242*d9f75844SAndroid Build Coastguard Worker rtc::PacketOptions modified_options(options);
243*d9f75844SAndroid Build Coastguard Worker CopyPortInformationToPacketInfo(&modified_options.info_signaled_after_sent);
244*d9f75844SAndroid Build Coastguard Worker int sent = socket->Send(data, size, modified_options);
245*d9f75844SAndroid Build Coastguard Worker if (sent < 0) {
246*d9f75844SAndroid Build Coastguard Worker error_ = socket->GetError();
247*d9f75844SAndroid Build Coastguard Worker // Error from this code path for a Connection (instead of from a bare
248*d9f75844SAndroid Build Coastguard Worker // socket) will not trigger reconnecting. In theory, this shouldn't matter
249*d9f75844SAndroid Build Coastguard Worker // as OnClose should always be called and set connected to false.
250*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_ERROR) << ToString() << ": TCP send of " << size
251*d9f75844SAndroid Build Coastguard Worker << " bytes failed with error " << error_;
252*d9f75844SAndroid Build Coastguard Worker }
253*d9f75844SAndroid Build Coastguard Worker return sent;
254*d9f75844SAndroid Build Coastguard Worker }
255*d9f75844SAndroid Build Coastguard Worker
GetOption(rtc::Socket::Option opt,int * value)256*d9f75844SAndroid Build Coastguard Worker int TCPPort::GetOption(rtc::Socket::Option opt, int* value) {
257*d9f75844SAndroid Build Coastguard Worker auto const& it = socket_options_.find(opt);
258*d9f75844SAndroid Build Coastguard Worker if (it == socket_options_.end()) {
259*d9f75844SAndroid Build Coastguard Worker return -1;
260*d9f75844SAndroid Build Coastguard Worker }
261*d9f75844SAndroid Build Coastguard Worker *value = it->second;
262*d9f75844SAndroid Build Coastguard Worker return 0;
263*d9f75844SAndroid Build Coastguard Worker }
264*d9f75844SAndroid Build Coastguard Worker
SetOption(rtc::Socket::Option opt,int value)265*d9f75844SAndroid Build Coastguard Worker int TCPPort::SetOption(rtc::Socket::Option opt, int value) {
266*d9f75844SAndroid Build Coastguard Worker socket_options_[opt] = value;
267*d9f75844SAndroid Build Coastguard Worker return 0;
268*d9f75844SAndroid Build Coastguard Worker }
269*d9f75844SAndroid Build Coastguard Worker
GetError()270*d9f75844SAndroid Build Coastguard Worker int TCPPort::GetError() {
271*d9f75844SAndroid Build Coastguard Worker return error_;
272*d9f75844SAndroid Build Coastguard Worker }
273*d9f75844SAndroid Build Coastguard Worker
SupportsProtocol(absl::string_view protocol) const274*d9f75844SAndroid Build Coastguard Worker bool TCPPort::SupportsProtocol(absl::string_view protocol) const {
275*d9f75844SAndroid Build Coastguard Worker return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME;
276*d9f75844SAndroid Build Coastguard Worker }
277*d9f75844SAndroid Build Coastguard Worker
GetProtocol() const278*d9f75844SAndroid Build Coastguard Worker ProtocolType TCPPort::GetProtocol() const {
279*d9f75844SAndroid Build Coastguard Worker return PROTO_TCP;
280*d9f75844SAndroid Build Coastguard Worker }
281*d9f75844SAndroid Build Coastguard Worker
OnNewConnection(rtc::AsyncListenSocket * socket,rtc::AsyncPacketSocket * new_socket)282*d9f75844SAndroid Build Coastguard Worker void TCPPort::OnNewConnection(rtc::AsyncListenSocket* socket,
283*d9f75844SAndroid Build Coastguard Worker rtc::AsyncPacketSocket* new_socket) {
284*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(socket, listen_socket_.get());
285*d9f75844SAndroid Build Coastguard Worker
286*d9f75844SAndroid Build Coastguard Worker for (const auto& option : socket_options_) {
287*d9f75844SAndroid Build Coastguard Worker new_socket->SetOption(option.first, option.second);
288*d9f75844SAndroid Build Coastguard Worker }
289*d9f75844SAndroid Build Coastguard Worker Incoming incoming;
290*d9f75844SAndroid Build Coastguard Worker incoming.addr = new_socket->GetRemoteAddress();
291*d9f75844SAndroid Build Coastguard Worker incoming.socket = new_socket;
292*d9f75844SAndroid Build Coastguard Worker incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket);
293*d9f75844SAndroid Build Coastguard Worker incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
294*d9f75844SAndroid Build Coastguard Worker incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
295*d9f75844SAndroid Build Coastguard Worker
296*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_VERBOSE) << ToString() << ": Accepted connection from "
297*d9f75844SAndroid Build Coastguard Worker << incoming.addr.ToSensitiveString();
298*d9f75844SAndroid Build Coastguard Worker incoming_.push_back(incoming);
299*d9f75844SAndroid Build Coastguard Worker }
300*d9f75844SAndroid Build Coastguard Worker
TryCreateServerSocket()301*d9f75844SAndroid Build Coastguard Worker void TCPPort::TryCreateServerSocket() {
302*d9f75844SAndroid Build Coastguard Worker listen_socket_ = absl::WrapUnique(socket_factory()->CreateServerTcpSocket(
303*d9f75844SAndroid Build Coastguard Worker rtc::SocketAddress(Network()->GetBestIP(), 0), min_port(), max_port(),
304*d9f75844SAndroid Build Coastguard Worker false /* ssl */));
305*d9f75844SAndroid Build Coastguard Worker if (!listen_socket_) {
306*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_WARNING)
307*d9f75844SAndroid Build Coastguard Worker << ToString()
308*d9f75844SAndroid Build Coastguard Worker << ": TCP server socket creation failed; continuing anyway.";
309*d9f75844SAndroid Build Coastguard Worker return;
310*d9f75844SAndroid Build Coastguard Worker }
311*d9f75844SAndroid Build Coastguard Worker listen_socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection);
312*d9f75844SAndroid Build Coastguard Worker }
313*d9f75844SAndroid Build Coastguard Worker
GetIncoming(const rtc::SocketAddress & addr,bool remove)314*d9f75844SAndroid Build Coastguard Worker rtc::AsyncPacketSocket* TCPPort::GetIncoming(const rtc::SocketAddress& addr,
315*d9f75844SAndroid Build Coastguard Worker bool remove) {
316*d9f75844SAndroid Build Coastguard Worker rtc::AsyncPacketSocket* socket = NULL;
317*d9f75844SAndroid Build Coastguard Worker for (std::list<Incoming>::iterator it = incoming_.begin();
318*d9f75844SAndroid Build Coastguard Worker it != incoming_.end(); ++it) {
319*d9f75844SAndroid Build Coastguard Worker if (it->addr == addr) {
320*d9f75844SAndroid Build Coastguard Worker socket = it->socket;
321*d9f75844SAndroid Build Coastguard Worker if (remove)
322*d9f75844SAndroid Build Coastguard Worker incoming_.erase(it);
323*d9f75844SAndroid Build Coastguard Worker break;
324*d9f75844SAndroid Build Coastguard Worker }
325*d9f75844SAndroid Build Coastguard Worker }
326*d9f75844SAndroid Build Coastguard Worker return socket;
327*d9f75844SAndroid Build Coastguard Worker }
328*d9f75844SAndroid Build Coastguard Worker
OnReadPacket(rtc::AsyncPacketSocket * socket,const char * data,size_t size,const rtc::SocketAddress & remote_addr,const int64_t & packet_time_us)329*d9f75844SAndroid Build Coastguard Worker void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket,
330*d9f75844SAndroid Build Coastguard Worker const char* data,
331*d9f75844SAndroid Build Coastguard Worker size_t size,
332*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& remote_addr,
333*d9f75844SAndroid Build Coastguard Worker const int64_t& packet_time_us) {
334*d9f75844SAndroid Build Coastguard Worker Port::OnReadPacket(data, size, remote_addr, PROTO_TCP);
335*d9f75844SAndroid Build Coastguard Worker }
336*d9f75844SAndroid Build Coastguard Worker
OnSentPacket(rtc::AsyncPacketSocket * socket,const rtc::SentPacket & sent_packet)337*d9f75844SAndroid Build Coastguard Worker void TCPPort::OnSentPacket(rtc::AsyncPacketSocket* socket,
338*d9f75844SAndroid Build Coastguard Worker const rtc::SentPacket& sent_packet) {
339*d9f75844SAndroid Build Coastguard Worker PortInterface::SignalSentPacket(sent_packet);
340*d9f75844SAndroid Build Coastguard Worker }
341*d9f75844SAndroid Build Coastguard Worker
OnReadyToSend(rtc::AsyncPacketSocket * socket)342*d9f75844SAndroid Build Coastguard Worker void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
343*d9f75844SAndroid Build Coastguard Worker Port::OnReadyToSend();
344*d9f75844SAndroid Build Coastguard Worker }
345*d9f75844SAndroid Build Coastguard Worker
346*d9f75844SAndroid Build Coastguard Worker // TODO(qingsi): `CONNECTION_WRITE_CONNECT_TIMEOUT` is overriden by
347*d9f75844SAndroid Build Coastguard Worker // `ice_unwritable_timeout` in IceConfig when determining the writability state.
348*d9f75844SAndroid Build Coastguard Worker // Replace this constant with the config parameter assuming the default value if
349*d9f75844SAndroid Build Coastguard Worker // we decide it is also applicable here.
TCPConnection(rtc::WeakPtr<Port> tcp_port,const Candidate & candidate,rtc::AsyncPacketSocket * socket)350*d9f75844SAndroid Build Coastguard Worker TCPConnection::TCPConnection(rtc::WeakPtr<Port> tcp_port,
351*d9f75844SAndroid Build Coastguard Worker const Candidate& candidate,
352*d9f75844SAndroid Build Coastguard Worker rtc::AsyncPacketSocket* socket)
353*d9f75844SAndroid Build Coastguard Worker : Connection(std::move(tcp_port), 0, candidate),
354*d9f75844SAndroid Build Coastguard Worker socket_(socket),
355*d9f75844SAndroid Build Coastguard Worker error_(0),
356*d9f75844SAndroid Build Coastguard Worker outgoing_(socket == NULL),
357*d9f75844SAndroid Build Coastguard Worker connection_pending_(false),
358*d9f75844SAndroid Build Coastguard Worker pretending_to_be_writable_(false),
359*d9f75844SAndroid Build Coastguard Worker reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) {
360*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(port()->GetProtocol(), PROTO_TCP); // Needs to be TCPPort.
361*d9f75844SAndroid Build Coastguard Worker if (outgoing_) {
362*d9f75844SAndroid Build Coastguard Worker CreateOutgoingTcpSocket();
363*d9f75844SAndroid Build Coastguard Worker } else {
364*d9f75844SAndroid Build Coastguard Worker // Incoming connections should match one of the network addresses. Same as
365*d9f75844SAndroid Build Coastguard Worker // what's being checked in OnConnect, but just DCHECKing here.
366*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_VERBOSE) << ToString() << ": socket ipaddr: "
367*d9f75844SAndroid Build Coastguard Worker << socket_->GetLocalAddress().ToSensitiveString()
368*d9f75844SAndroid Build Coastguard Worker << ", port() Network:" << port()->Network()->ToString();
369*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(absl::c_any_of(
370*d9f75844SAndroid Build Coastguard Worker port_->Network()->GetIPs(), [this](const rtc::InterfaceAddress& addr) {
371*d9f75844SAndroid Build Coastguard Worker return socket_->GetLocalAddress().ipaddr() == addr;
372*d9f75844SAndroid Build Coastguard Worker }));
373*d9f75844SAndroid Build Coastguard Worker ConnectSocketSignals(socket);
374*d9f75844SAndroid Build Coastguard Worker }
375*d9f75844SAndroid Build Coastguard Worker }
376*d9f75844SAndroid Build Coastguard Worker
~TCPConnection()377*d9f75844SAndroid Build Coastguard Worker TCPConnection::~TCPConnection() {
378*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(network_thread_);
379*d9f75844SAndroid Build Coastguard Worker }
380*d9f75844SAndroid Build Coastguard Worker
Send(const void * data,size_t size,const rtc::PacketOptions & options)381*d9f75844SAndroid Build Coastguard Worker int TCPConnection::Send(const void* data,
382*d9f75844SAndroid Build Coastguard Worker size_t size,
383*d9f75844SAndroid Build Coastguard Worker const rtc::PacketOptions& options) {
384*d9f75844SAndroid Build Coastguard Worker if (!socket_) {
385*d9f75844SAndroid Build Coastguard Worker error_ = ENOTCONN;
386*d9f75844SAndroid Build Coastguard Worker return SOCKET_ERROR;
387*d9f75844SAndroid Build Coastguard Worker }
388*d9f75844SAndroid Build Coastguard Worker
389*d9f75844SAndroid Build Coastguard Worker // Sending after OnClose on active side will trigger a reconnect for a
390*d9f75844SAndroid Build Coastguard Worker // outgoing connection. Note that the write state is still WRITABLE as we want
391*d9f75844SAndroid Build Coastguard Worker // to spend a few seconds attempting a reconnect before saying we're
392*d9f75844SAndroid Build Coastguard Worker // unwritable.
393*d9f75844SAndroid Build Coastguard Worker if (!connected()) {
394*d9f75844SAndroid Build Coastguard Worker MaybeReconnect();
395*d9f75844SAndroid Build Coastguard Worker return SOCKET_ERROR;
396*d9f75844SAndroid Build Coastguard Worker }
397*d9f75844SAndroid Build Coastguard Worker
398*d9f75844SAndroid Build Coastguard Worker // Note that this is important to put this after the previous check to give
399*d9f75844SAndroid Build Coastguard Worker // the connection a chance to reconnect.
400*d9f75844SAndroid Build Coastguard Worker if (pretending_to_be_writable_ || write_state() != STATE_WRITABLE) {
401*d9f75844SAndroid Build Coastguard Worker // TODO(?): Should STATE_WRITE_TIMEOUT return a non-blocking error?
402*d9f75844SAndroid Build Coastguard Worker error_ = ENOTCONN;
403*d9f75844SAndroid Build Coastguard Worker return SOCKET_ERROR;
404*d9f75844SAndroid Build Coastguard Worker }
405*d9f75844SAndroid Build Coastguard Worker stats_.sent_total_packets++;
406*d9f75844SAndroid Build Coastguard Worker rtc::PacketOptions modified_options(options);
407*d9f75844SAndroid Build Coastguard Worker tcp_port()->CopyPortInformationToPacketInfo(
408*d9f75844SAndroid Build Coastguard Worker &modified_options.info_signaled_after_sent);
409*d9f75844SAndroid Build Coastguard Worker int sent = socket_->Send(data, size, modified_options);
410*d9f75844SAndroid Build Coastguard Worker int64_t now = rtc::TimeMillis();
411*d9f75844SAndroid Build Coastguard Worker if (sent < 0) {
412*d9f75844SAndroid Build Coastguard Worker stats_.sent_discarded_packets++;
413*d9f75844SAndroid Build Coastguard Worker error_ = socket_->GetError();
414*d9f75844SAndroid Build Coastguard Worker } else {
415*d9f75844SAndroid Build Coastguard Worker send_rate_tracker_.AddSamplesAtTime(now, sent);
416*d9f75844SAndroid Build Coastguard Worker }
417*d9f75844SAndroid Build Coastguard Worker last_send_data_ = now;
418*d9f75844SAndroid Build Coastguard Worker return sent;
419*d9f75844SAndroid Build Coastguard Worker }
420*d9f75844SAndroid Build Coastguard Worker
GetError()421*d9f75844SAndroid Build Coastguard Worker int TCPConnection::GetError() {
422*d9f75844SAndroid Build Coastguard Worker return error_;
423*d9f75844SAndroid Build Coastguard Worker }
424*d9f75844SAndroid Build Coastguard Worker
OnConnectionRequestResponse(StunRequest * req,StunMessage * response)425*d9f75844SAndroid Build Coastguard Worker void TCPConnection::OnConnectionRequestResponse(StunRequest* req,
426*d9f75844SAndroid Build Coastguard Worker StunMessage* response) {
427*d9f75844SAndroid Build Coastguard Worker // Process the STUN response before we inform upper layer ready to send.
428*d9f75844SAndroid Build Coastguard Worker Connection::OnConnectionRequestResponse(req, response);
429*d9f75844SAndroid Build Coastguard Worker
430*d9f75844SAndroid Build Coastguard Worker // If we're in the state of pretending to be writeable, we should inform the
431*d9f75844SAndroid Build Coastguard Worker // upper layer it's ready to send again as previous EWOULDLBLOCK from socket
432*d9f75844SAndroid Build Coastguard Worker // would have stopped the outgoing stream.
433*d9f75844SAndroid Build Coastguard Worker if (pretending_to_be_writable_) {
434*d9f75844SAndroid Build Coastguard Worker Connection::OnReadyToSend();
435*d9f75844SAndroid Build Coastguard Worker }
436*d9f75844SAndroid Build Coastguard Worker pretending_to_be_writable_ = false;
437*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(write_state() == STATE_WRITABLE);
438*d9f75844SAndroid Build Coastguard Worker }
439*d9f75844SAndroid Build Coastguard Worker
OnConnect(rtc::AsyncPacketSocket * socket)440*d9f75844SAndroid Build Coastguard Worker void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) {
441*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(socket, socket_.get());
442*d9f75844SAndroid Build Coastguard Worker
443*d9f75844SAndroid Build Coastguard Worker if (!port_) {
444*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_ERROR) << "TCPConnection: Port has been deleted.";
445*d9f75844SAndroid Build Coastguard Worker return;
446*d9f75844SAndroid Build Coastguard Worker }
447*d9f75844SAndroid Build Coastguard Worker
448*d9f75844SAndroid Build Coastguard Worker // Do not use this port if the socket bound to an address not associated with
449*d9f75844SAndroid Build Coastguard Worker // the desired network interface. This is seen in Chrome, where TCP sockets
450*d9f75844SAndroid Build Coastguard Worker // cannot be given a binding address, and the platform is expected to pick
451*d9f75844SAndroid Build Coastguard Worker // the correct local address.
452*d9f75844SAndroid Build Coastguard Worker //
453*d9f75844SAndroid Build Coastguard Worker // However, there are two situations in which we allow the bound address to
454*d9f75844SAndroid Build Coastguard Worker // not be one of the addresses of the requested interface:
455*d9f75844SAndroid Build Coastguard Worker // 1. The bound address is the loopback address. This happens when a proxy
456*d9f75844SAndroid Build Coastguard Worker // forces TCP to bind to only the localhost address (see issue 3927).
457*d9f75844SAndroid Build Coastguard Worker // 2. The bound address is the "any address". This happens when
458*d9f75844SAndroid Build Coastguard Worker // multiple_routes is disabled (see issue 4780).
459*d9f75844SAndroid Build Coastguard Worker //
460*d9f75844SAndroid Build Coastguard Worker // Note that, aside from minor differences in log statements, this logic is
461*d9f75844SAndroid Build Coastguard Worker // identical to that in TurnPort.
462*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& socket_address = socket->GetLocalAddress();
463*d9f75844SAndroid Build Coastguard Worker if (absl::c_any_of(port_->Network()->GetIPs(),
464*d9f75844SAndroid Build Coastguard Worker [socket_address](const rtc::InterfaceAddress& addr) {
465*d9f75844SAndroid Build Coastguard Worker return socket_address.ipaddr() == addr;
466*d9f75844SAndroid Build Coastguard Worker })) {
467*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_VERBOSE) << ToString() << ": Connection established to "
468*d9f75844SAndroid Build Coastguard Worker << socket->GetRemoteAddress().ToSensitiveString();
469*d9f75844SAndroid Build Coastguard Worker } else {
470*d9f75844SAndroid Build Coastguard Worker if (socket->GetLocalAddress().IsLoopbackIP()) {
471*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_WARNING) << "Socket is bound to the address:"
472*d9f75844SAndroid Build Coastguard Worker << socket_address.ipaddr().ToSensitiveString()
473*d9f75844SAndroid Build Coastguard Worker << ", rather than an address associated with network:"
474*d9f75844SAndroid Build Coastguard Worker << port_->Network()->ToString()
475*d9f75844SAndroid Build Coastguard Worker << ". Still allowing it since it's localhost.";
476*d9f75844SAndroid Build Coastguard Worker } else if (IPIsAny(port_->Network()->GetBestIP())) {
477*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_WARNING)
478*d9f75844SAndroid Build Coastguard Worker << "Socket is bound to the address:"
479*d9f75844SAndroid Build Coastguard Worker << socket_address.ipaddr().ToSensitiveString()
480*d9f75844SAndroid Build Coastguard Worker << ", rather than an address associated with network:"
481*d9f75844SAndroid Build Coastguard Worker << port_->Network()->ToString()
482*d9f75844SAndroid Build Coastguard Worker << ". Still allowing it since it's the 'any' address"
483*d9f75844SAndroid Build Coastguard Worker ", possibly caused by multiple_routes being disabled.";
484*d9f75844SAndroid Build Coastguard Worker } else {
485*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_WARNING) << "Dropping connection as TCP socket bound to IP "
486*d9f75844SAndroid Build Coastguard Worker << socket_address.ipaddr().ToSensitiveString()
487*d9f75844SAndroid Build Coastguard Worker << ", rather than an address associated with network:"
488*d9f75844SAndroid Build Coastguard Worker << port_->Network()->ToString();
489*d9f75844SAndroid Build Coastguard Worker OnClose(socket, 0);
490*d9f75844SAndroid Build Coastguard Worker return;
491*d9f75844SAndroid Build Coastguard Worker }
492*d9f75844SAndroid Build Coastguard Worker }
493*d9f75844SAndroid Build Coastguard Worker
494*d9f75844SAndroid Build Coastguard Worker // Connection is established successfully.
495*d9f75844SAndroid Build Coastguard Worker set_connected(true);
496*d9f75844SAndroid Build Coastguard Worker connection_pending_ = false;
497*d9f75844SAndroid Build Coastguard Worker }
498*d9f75844SAndroid Build Coastguard Worker
OnClose(rtc::AsyncPacketSocket * socket,int error)499*d9f75844SAndroid Build Coastguard Worker void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) {
500*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(socket, socket_.get());
501*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << ToString() << ": Connection closed with error " << error;
502*d9f75844SAndroid Build Coastguard Worker
503*d9f75844SAndroid Build Coastguard Worker if (!port_) {
504*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_ERROR) << "TCPConnection: Port has been deleted.";
505*d9f75844SAndroid Build Coastguard Worker return;
506*d9f75844SAndroid Build Coastguard Worker }
507*d9f75844SAndroid Build Coastguard Worker
508*d9f75844SAndroid Build Coastguard Worker // Guard against the condition where IPC socket will call OnClose for every
509*d9f75844SAndroid Build Coastguard Worker // packet it can't send.
510*d9f75844SAndroid Build Coastguard Worker if (connected()) {
511*d9f75844SAndroid Build Coastguard Worker set_connected(false);
512*d9f75844SAndroid Build Coastguard Worker
513*d9f75844SAndroid Build Coastguard Worker // Prevent the connection from being destroyed by redundant SignalClose
514*d9f75844SAndroid Build Coastguard Worker // events.
515*d9f75844SAndroid Build Coastguard Worker pretending_to_be_writable_ = true;
516*d9f75844SAndroid Build Coastguard Worker
517*d9f75844SAndroid Build Coastguard Worker // If this connection can't become connected and writable again in 5
518*d9f75844SAndroid Build Coastguard Worker // seconds, it's time to tear this down. This is the case for the original
519*d9f75844SAndroid Build Coastguard Worker // TCP connection on passive side during a reconnect.
520*d9f75844SAndroid Build Coastguard Worker // We don't attempt reconnect right here. This is to avoid a case where the
521*d9f75844SAndroid Build Coastguard Worker // shutdown is intentional and reconnect is not necessary. We only reconnect
522*d9f75844SAndroid Build Coastguard Worker // when the connection is used to Send() or Ping().
523*d9f75844SAndroid Build Coastguard Worker network_thread()->PostDelayedTask(
524*d9f75844SAndroid Build Coastguard Worker SafeTask(network_safety_.flag(),
525*d9f75844SAndroid Build Coastguard Worker [this]() {
526*d9f75844SAndroid Build Coastguard Worker if (pretending_to_be_writable_) {
527*d9f75844SAndroid Build Coastguard Worker Destroy();
528*d9f75844SAndroid Build Coastguard Worker }
529*d9f75844SAndroid Build Coastguard Worker }),
530*d9f75844SAndroid Build Coastguard Worker TimeDelta::Millis(reconnection_timeout()));
531*d9f75844SAndroid Build Coastguard Worker } else if (!pretending_to_be_writable_) {
532*d9f75844SAndroid Build Coastguard Worker // OnClose could be called when the underneath socket times out during the
533*d9f75844SAndroid Build Coastguard Worker // initial connect() (i.e. `pretending_to_be_writable_` is false) . We have
534*d9f75844SAndroid Build Coastguard Worker // to manually destroy here as this connection, as never connected, will not
535*d9f75844SAndroid Build Coastguard Worker // be scheduled for ping to trigger destroy.
536*d9f75844SAndroid Build Coastguard Worker socket_->UnsubscribeClose(this);
537*d9f75844SAndroid Build Coastguard Worker port()->DestroyConnectionAsync(this);
538*d9f75844SAndroid Build Coastguard Worker }
539*d9f75844SAndroid Build Coastguard Worker }
540*d9f75844SAndroid Build Coastguard Worker
MaybeReconnect()541*d9f75844SAndroid Build Coastguard Worker void TCPConnection::MaybeReconnect() {
542*d9f75844SAndroid Build Coastguard Worker // Only reconnect for an outgoing TCPConnection when OnClose was signaled and
543*d9f75844SAndroid Build Coastguard Worker // no outstanding reconnect is pending.
544*d9f75844SAndroid Build Coastguard Worker if (connected() || connection_pending_ || !outgoing_) {
545*d9f75844SAndroid Build Coastguard Worker return;
546*d9f75844SAndroid Build Coastguard Worker }
547*d9f75844SAndroid Build Coastguard Worker
548*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << ToString()
549*d9f75844SAndroid Build Coastguard Worker << ": TCP Connection with remote is closed, "
550*d9f75844SAndroid Build Coastguard Worker "trying to reconnect";
551*d9f75844SAndroid Build Coastguard Worker
552*d9f75844SAndroid Build Coastguard Worker CreateOutgoingTcpSocket();
553*d9f75844SAndroid Build Coastguard Worker error_ = EPIPE;
554*d9f75844SAndroid Build Coastguard Worker }
555*d9f75844SAndroid Build Coastguard Worker
OnReadPacket(rtc::AsyncPacketSocket * socket,const char * data,size_t size,const rtc::SocketAddress & remote_addr,const int64_t & packet_time_us)556*d9f75844SAndroid Build Coastguard Worker void TCPConnection::OnReadPacket(rtc::AsyncPacketSocket* socket,
557*d9f75844SAndroid Build Coastguard Worker const char* data,
558*d9f75844SAndroid Build Coastguard Worker size_t size,
559*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& remote_addr,
560*d9f75844SAndroid Build Coastguard Worker const int64_t& packet_time_us) {
561*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(socket, socket_.get());
562*d9f75844SAndroid Build Coastguard Worker Connection::OnReadPacket(data, size, packet_time_us);
563*d9f75844SAndroid Build Coastguard Worker }
564*d9f75844SAndroid Build Coastguard Worker
OnReadyToSend(rtc::AsyncPacketSocket * socket)565*d9f75844SAndroid Build Coastguard Worker void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
566*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_EQ(socket, socket_.get());
567*d9f75844SAndroid Build Coastguard Worker Connection::OnReadyToSend();
568*d9f75844SAndroid Build Coastguard Worker }
569*d9f75844SAndroid Build Coastguard Worker
CreateOutgoingTcpSocket()570*d9f75844SAndroid Build Coastguard Worker void TCPConnection::CreateOutgoingTcpSocket() {
571*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(outgoing_);
572*d9f75844SAndroid Build Coastguard Worker int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME)
573*d9f75844SAndroid Build Coastguard Worker ? rtc::PacketSocketFactory::OPT_TLS_FAKE
574*d9f75844SAndroid Build Coastguard Worker : 0;
575*d9f75844SAndroid Build Coastguard Worker
576*d9f75844SAndroid Build Coastguard Worker if (socket_) {
577*d9f75844SAndroid Build Coastguard Worker socket_->UnsubscribeClose(this);
578*d9f75844SAndroid Build Coastguard Worker }
579*d9f75844SAndroid Build Coastguard Worker
580*d9f75844SAndroid Build Coastguard Worker rtc::PacketSocketTcpOptions tcp_opts;
581*d9f75844SAndroid Build Coastguard Worker tcp_opts.opts = opts;
582*d9f75844SAndroid Build Coastguard Worker socket_.reset(port()->socket_factory()->CreateClientTcpSocket(
583*d9f75844SAndroid Build Coastguard Worker rtc::SocketAddress(port()->Network()->GetBestIP(), 0),
584*d9f75844SAndroid Build Coastguard Worker remote_candidate().address(), port()->proxy(), port()->user_agent(),
585*d9f75844SAndroid Build Coastguard Worker tcp_opts));
586*d9f75844SAndroid Build Coastguard Worker if (socket_) {
587*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_VERBOSE) << ToString() << ": Connecting from "
588*d9f75844SAndroid Build Coastguard Worker << socket_->GetLocalAddress().ToSensitiveString()
589*d9f75844SAndroid Build Coastguard Worker << " to "
590*d9f75844SAndroid Build Coastguard Worker << remote_candidate().address().ToSensitiveString();
591*d9f75844SAndroid Build Coastguard Worker set_connected(false);
592*d9f75844SAndroid Build Coastguard Worker connection_pending_ = true;
593*d9f75844SAndroid Build Coastguard Worker ConnectSocketSignals(socket_.get());
594*d9f75844SAndroid Build Coastguard Worker } else {
595*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_WARNING) << ToString() << ": Failed to create connection to "
596*d9f75844SAndroid Build Coastguard Worker << remote_candidate().address().ToSensitiveString();
597*d9f75844SAndroid Build Coastguard Worker set_state(IceCandidatePairState::FAILED);
598*d9f75844SAndroid Build Coastguard Worker // We can't FailAndPrune directly here. FailAndPrune and deletes all
599*d9f75844SAndroid Build Coastguard Worker // the StunRequests from the request_map_. And if this is in the stack
600*d9f75844SAndroid Build Coastguard Worker // of Connection::Ping(), we are still using the request.
601*d9f75844SAndroid Build Coastguard Worker // Unwind the stack and defer the FailAndPrune.
602*d9f75844SAndroid Build Coastguard Worker network_thread()->PostTask(
603*d9f75844SAndroid Build Coastguard Worker SafeTask(network_safety_.flag(), [this]() { FailAndPrune(); }));
604*d9f75844SAndroid Build Coastguard Worker }
605*d9f75844SAndroid Build Coastguard Worker }
606*d9f75844SAndroid Build Coastguard Worker
ConnectSocketSignals(rtc::AsyncPacketSocket * socket)607*d9f75844SAndroid Build Coastguard Worker void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) {
608*d9f75844SAndroid Build Coastguard Worker if (outgoing_) {
609*d9f75844SAndroid Build Coastguard Worker socket->SignalConnect.connect(this, &TCPConnection::OnConnect);
610*d9f75844SAndroid Build Coastguard Worker }
611*d9f75844SAndroid Build Coastguard Worker socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket);
612*d9f75844SAndroid Build Coastguard Worker socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend);
613*d9f75844SAndroid Build Coastguard Worker socket->SubscribeClose(this, [this, safety = network_safety_.flag()](
614*d9f75844SAndroid Build Coastguard Worker rtc::AsyncPacketSocket* s, int err) {
615*d9f75844SAndroid Build Coastguard Worker if (safety->alive())
616*d9f75844SAndroid Build Coastguard Worker OnClose(s, err);
617*d9f75844SAndroid Build Coastguard Worker });
618*d9f75844SAndroid Build Coastguard Worker }
619*d9f75844SAndroid Build Coastguard Worker
620*d9f75844SAndroid Build Coastguard Worker } // namespace cricket
621