2014-10-28 22:20:11 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-06 12:48:47 -07:00
|
|
|
/*
|
|
|
|
|
* This is a diagram of how TCP reconnect works for the active side. The
|
|
|
|
|
* passive side just waits for an incoming connection.
|
|
|
|
|
*
|
|
|
|
|
* - Connected: Indicate whether the TCP socket is connected.
|
|
|
|
|
*
|
|
|
|
|
* - Writable: Whether the stun binding is completed. Sending a data packet
|
|
|
|
|
* before stun binding completed will trigger IPC socket layer to shutdown
|
|
|
|
|
* the connection.
|
|
|
|
|
*
|
|
|
|
|
* - PendingTCP: |connection_pending_| indicates whether there is an
|
|
|
|
|
* outstanding TCP connection in progress.
|
|
|
|
|
*
|
|
|
|
|
* - PretendWri: Tracked by |pretending_to_be_writable_|. Marking connection as
|
|
|
|
|
* WRITE_TIMEOUT will cause the connection be deleted. Instead, we're
|
|
|
|
|
* "pretending" we're still writable for a period of time such that reconnect
|
|
|
|
|
* could work.
|
|
|
|
|
*
|
|
|
|
|
* Data could only be sent in state 3. Sening data during state 2 & 6 will get
|
|
|
|
|
* EWOULDBLOCK, 4 & 5 EPIPE.
|
|
|
|
|
*
|
2015-08-25 11:02:55 -07:00
|
|
|
* OS Timeout 7 -------------+
|
|
|
|
|
* +----------------------->|Connected: N |
|
|
|
|
|
* | |Writable: N | Timeout
|
|
|
|
|
* | Timeout |Connection is |<----------------+
|
|
|
|
|
* | +------------------->|Dead | |
|
|
|
|
|
* | | +--------------+ |
|
|
|
|
|
* | | ^ |
|
|
|
|
|
* | | OnClose | |
|
|
|
|
|
* | | +-----------------------+ | |
|
|
|
|
|
* | | | | |Timeout |
|
|
|
|
|
* | | v | | |
|
|
|
|
|
* | 4 +----------+ 5 -----+--+--+ 6 -----+-----+
|
|
|
|
|
* | |Connected: N|Send() or |Connected: N| |Connected: Y|
|
|
|
|
|
* | |Writable: Y|Ping() |Writable: Y|OnConnect |Writable: Y|
|
|
|
|
|
* | |PendingTCP:N+--------> |PendingTCP:Y+---------> |PendingTCP:N|
|
|
|
|
|
* | |PretendWri:Y| |PretendWri:Y| |PretendWri:Y|
|
|
|
|
|
* | +-----+------+ +------------+ +---+--+-----+
|
|
|
|
|
* | ^ ^ | |
|
|
|
|
|
* | | | OnClose | |
|
|
|
|
|
* | | +----------------------------------------------+ |
|
|
|
|
|
* | | |
|
|
|
|
|
* | | Stun Binding Completed |
|
|
|
|
|
* | | |
|
|
|
|
|
* | | OnClose |
|
|
|
|
|
* | +------------------------------------------------+ |
|
|
|
|
|
* | | v
|
2015-04-06 12:48:47 -07:00
|
|
|
* 1 -----------+ 2 -----------+Stun 3 -----------+
|
|
|
|
|
* |Connected: N| |Connected: Y|Binding |Connected: Y|
|
|
|
|
|
* |Writable: N|OnConnect |Writable: N|Completed |Writable: Y|
|
|
|
|
|
* |PendingTCP:Y+---------> |PendingTCP:N+--------> |PendingTCP:N|
|
|
|
|
|
* |PretendWri:N| |PretendWri:N| |PretendWri:N|
|
|
|
|
|
* +------------+ +------------+ +------------+
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/tcp_port.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <errno.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2017-11-29 10:25:58 -08:00
|
|
|
#include <vector>
|
|
|
|
|
|
2019-01-29 12:47:38 -08:00
|
|
|
#include "absl/algorithm/container.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/p2p_constants.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/ip_address.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "rtc_base/location.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/net_helper.h"
|
|
|
|
|
#include "rtc_base/rate_tracker.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "rtc_base/third_party/sigslot/sigslot.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
|
|
TCPPort::TCPPort(rtc::Thread* thread,
|
|
|
|
|
rtc::PacketSocketFactory* factory,
|
2014-11-06 20:19:22 +00:00
|
|
|
rtc::Network* network,
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint16_t min_port,
|
|
|
|
|
uint16_t max_port,
|
2014-11-06 20:19:22 +00:00
|
|
|
const std::string& username,
|
|
|
|
|
const std::string& password,
|
|
|
|
|
bool allow_listen)
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
: Port(thread,
|
|
|
|
|
LOCAL_PORT_TYPE,
|
|
|
|
|
factory,
|
|
|
|
|
network,
|
|
|
|
|
min_port,
|
|
|
|
|
max_port,
|
|
|
|
|
username,
|
|
|
|
|
password),
|
2014-10-28 22:20:11 +00:00
|
|
|
allow_listen_(allow_listen),
|
|
|
|
|
socket_(NULL),
|
|
|
|
|
error_(0) {
|
|
|
|
|
// TODO(mallinath) - Set preference value as per RFC 6544.
|
|
|
|
|
// http://b/issue?id=7141794
|
|
|
|
|
if (allow_listen_) {
|
2017-06-13 15:49:45 -07:00
|
|
|
TryCreateServerSocket();
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TCPPort::~TCPPort() {
|
|
|
|
|
delete socket_;
|
|
|
|
|
std::list<Incoming>::iterator it;
|
|
|
|
|
for (it = incoming_.begin(); it != incoming_.end(); ++it)
|
|
|
|
|
delete it->socket;
|
|
|
|
|
incoming_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection* TCPPort::CreateConnection(const Candidate& address,
|
|
|
|
|
CandidateOrigin origin) {
|
2015-12-15 12:20:13 -08:00
|
|
|
if (!SupportsProtocol(address.protocol())) {
|
2014-10-28 22:20:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (address.tcptype() == TCPTYPE_ACTIVE_STR ||
|
|
|
|
|
(address.tcptype().empty() && address.address().port() == 0)) {
|
|
|
|
|
// It's active only candidate, we should not try to create connections
|
|
|
|
|
// for these candidates.
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We can't accept TCP connections incoming on other ports
|
|
|
|
|
if (origin == ORIGIN_OTHER_PORT)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// We don't know how to act as an ssl server yet
|
|
|
|
|
if ((address.protocol() == SSLTCP_PROTOCOL_NAME) &&
|
|
|
|
|
(origin == ORIGIN_THIS_PORT)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IsCompatibleAddress(address.address())) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TCPConnection* conn = NULL;
|
|
|
|
|
if (rtc::AsyncPacketSocket* socket = GetIncoming(address.address(), true)) {
|
2017-04-21 14:22:23 -07:00
|
|
|
// Incoming connection; we already created a socket and connected signals,
|
|
|
|
|
// so we need to hand off the "read packet" responsibility to
|
|
|
|
|
// TCPConnection.
|
2014-10-28 22:20:11 +00:00
|
|
|
socket->SignalReadPacket.disconnect(this);
|
|
|
|
|
conn = new TCPConnection(this, address, socket);
|
|
|
|
|
} else {
|
2017-04-21 14:22:23 -07:00
|
|
|
// Outgoing connection, which will create a new socket for which we still
|
|
|
|
|
// need to connect SignalReadyToSend and SignalSentPacket.
|
2014-10-28 22:20:11 +00:00
|
|
|
conn = new TCPConnection(this, address);
|
2017-04-21 14:22:23 -07:00
|
|
|
if (conn->socket()) {
|
|
|
|
|
conn->socket()->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
|
|
|
|
|
conn->socket()->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
2016-06-01 15:57:03 -07:00
|
|
|
AddOrReplaceConnection(conn);
|
2014-10-28 22:20:11 +00:00
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPPort::PrepareAddress() {
|
|
|
|
|
if (socket_) {
|
|
|
|
|
// If socket isn't bound yet the address will be added in
|
|
|
|
|
// OnAddressReady(). Socket may be in the CLOSED state if Listen()
|
|
|
|
|
// failed, we still want to add the socket address.
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
|
|
|
|
|
<< socket_->GetState();
|
2014-10-28 22:20:11 +00:00
|
|
|
if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND ||
|
|
|
|
|
socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED)
|
|
|
|
|
AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(),
|
2015-08-19 16:51:15 -07:00
|
|
|
rtc::SocketAddress(), TCP_PROTOCOL_NAME, "",
|
|
|
|
|
TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
|
2017-02-13 12:47:27 -08:00
|
|
|
ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
|
2014-10-28 22:20:11 +00:00
|
|
|
} else {
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_INFO) << ToString()
|
|
|
|
|
<< ": Not listening due to firewall restrictions.";
|
2014-10-28 22:20:11 +00:00
|
|
|
// Note: We still add the address, since otherwise the remote side won't
|
2015-11-17 19:15:50 -08:00
|
|
|
// recognize our incoming TCP connections. According to
|
|
|
|
|
// https://tools.ietf.org/html/rfc6544#section-4.5, for active candidate,
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
// the port must be set to the discard port, i.e. 9. We can't be 100% sure
|
|
|
|
|
// which IP address will actually be used, so GetBestIP is as good as we
|
|
|
|
|
// can do.
|
|
|
|
|
// TODO(deadbeef): We could do something like create a dummy socket just to
|
|
|
|
|
// see what IP we get. But that may be overkill.
|
|
|
|
|
AddAddress(rtc::SocketAddress(Network()->GetBestIP(), DISCARD_PORT),
|
|
|
|
|
rtc::SocketAddress(Network()->GetBestIP(), 0),
|
|
|
|
|
rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", TCPTYPE_ACTIVE_STR,
|
|
|
|
|
LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TCPPort::SendTo(const void* data,
|
|
|
|
|
size_t size,
|
|
|
|
|
const rtc::SocketAddress& addr,
|
|
|
|
|
const rtc::PacketOptions& options,
|
|
|
|
|
bool payload) {
|
|
|
|
|
rtc::AsyncPacketSocket* socket = NULL;
|
2015-04-06 12:48:47 -07:00
|
|
|
TCPConnection* conn = static_cast<TCPConnection*>(GetConnection(addr));
|
|
|
|
|
|
|
|
|
|
// For Connection, this is the code path used by Ping() to establish
|
|
|
|
|
// WRITABLE. It has to send through the socket directly as TCPConnection::Send
|
|
|
|
|
// checks writability.
|
|
|
|
|
if (conn) {
|
|
|
|
|
if (!conn->connected()) {
|
|
|
|
|
conn->MaybeReconnect();
|
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
socket = conn->socket();
|
|
|
|
|
} else {
|
|
|
|
|
socket = GetIncoming(addr);
|
|
|
|
|
}
|
|
|
|
|
if (!socket) {
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_ERROR) << ToString()
|
|
|
|
|
<< ": Attempted to send to an unknown destination: "
|
|
|
|
|
<< addr.ToSensitiveString();
|
2015-04-06 12:48:47 -07:00
|
|
|
return SOCKET_ERROR; // TODO(tbd): Set error_
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
2018-04-11 20:14:17 -07:00
|
|
|
rtc::PacketOptions modified_options(options);
|
|
|
|
|
CopyPortInformationToPacketInfo(&modified_options.info_signaled_after_sent);
|
|
|
|
|
int sent = socket->Send(data, size, modified_options);
|
2014-10-28 22:20:11 +00:00
|
|
|
if (sent < 0) {
|
|
|
|
|
error_ = socket->GetError();
|
2015-04-06 12:48:47 -07:00
|
|
|
// Error from this code path for a Connection (instead of from a bare
|
|
|
|
|
// socket) will not trigger reconnecting. In theory, this shouldn't matter
|
|
|
|
|
// as OnClose should always be called and set connected to false.
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_ERROR) << ToString() << ": TCP send of " << size
|
|
|
|
|
<< " bytes failed with error " << error_;
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
return sent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TCPPort::GetOption(rtc::Socket::Option opt, int* value) {
|
|
|
|
|
if (socket_) {
|
|
|
|
|
return socket_->GetOption(opt, value);
|
|
|
|
|
} else {
|
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TCPPort::SetOption(rtc::Socket::Option opt, int value) {
|
|
|
|
|
if (socket_) {
|
|
|
|
|
return socket_->SetOption(opt, value);
|
|
|
|
|
} else {
|
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TCPPort::GetError() {
|
|
|
|
|
return error_;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 10:00:15 -07:00
|
|
|
bool TCPPort::SupportsProtocol(const std::string& protocol) const {
|
|
|
|
|
return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProtocolType TCPPort::GetProtocol() const {
|
|
|
|
|
return PROTO_TCP;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket,
|
|
|
|
|
rtc::AsyncPacketSocket* new_socket) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(socket == socket_);
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
Incoming incoming;
|
|
|
|
|
incoming.addr = new_socket->GetRemoteAddress();
|
|
|
|
|
incoming.socket = new_socket;
|
|
|
|
|
incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket);
|
|
|
|
|
incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
|
2016-01-14 15:49:16 +01:00
|
|
|
incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket);
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_VERBOSE) << ToString() << ": Accepted connection from "
|
|
|
|
|
<< incoming.addr.ToSensitiveString();
|
2014-10-28 22:20:11 +00:00
|
|
|
incoming_.push_back(incoming);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 15:49:45 -07:00
|
|
|
void TCPPort::TryCreateServerSocket() {
|
|
|
|
|
socket_ = socket_factory()->CreateServerTcpSocket(
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
rtc::SocketAddress(Network()->GetBestIP(), 0), min_port(), max_port(),
|
|
|
|
|
false /* ssl */);
|
2017-06-13 15:49:45 -07:00
|
|
|
if (!socket_) {
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_WARNING)
|
|
|
|
|
<< ToString()
|
|
|
|
|
<< ": TCP server socket creation failed; continuing anyway.";
|
2017-06-13 15:49:45 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection);
|
|
|
|
|
socket_->SignalAddressReady.connect(this, &TCPPort::OnAddressReady);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
rtc::AsyncPacketSocket* TCPPort::GetIncoming(const rtc::SocketAddress& addr,
|
|
|
|
|
bool remove) {
|
|
|
|
|
rtc::AsyncPacketSocket* socket = NULL;
|
|
|
|
|
for (std::list<Incoming>::iterator it = incoming_.begin();
|
|
|
|
|
it != incoming_.end(); ++it) {
|
|
|
|
|
if (it->addr == addr) {
|
|
|
|
|
socket = it->socket;
|
|
|
|
|
if (remove)
|
|
|
|
|
incoming_.erase(it);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket,
|
|
|
|
|
const char* data,
|
|
|
|
|
size_t size,
|
|
|
|
|
const rtc::SocketAddress& remote_addr,
|
2018-11-05 13:01:41 +01:00
|
|
|
const int64_t& packet_time_us) {
|
2014-10-28 22:20:11 +00:00
|
|
|
Port::OnReadPacket(data, size, remote_addr, PROTO_TCP);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-14 15:49:16 +01:00
|
|
|
void TCPPort::OnSentPacket(rtc::AsyncPacketSocket* socket,
|
|
|
|
|
const rtc::SentPacket& sent_packet) {
|
|
|
|
|
PortInterface::SignalSentPacket(sent_packet);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
|
|
|
|
|
Port::OnReadyToSend();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPPort::OnAddressReady(rtc::AsyncPacketSocket* socket,
|
|
|
|
|
const rtc::SocketAddress& address) {
|
2015-08-19 16:51:15 -07:00
|
|
|
AddAddress(address, address, rtc::SocketAddress(), TCP_PROTOCOL_NAME, "",
|
|
|
|
|
TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP,
|
2017-02-13 12:47:27 -08:00
|
|
|
0, "", true);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-13 10:53:57 -07:00
|
|
|
// TODO(qingsi): |CONNECTION_WRITE_CONNECT_TIMEOUT| is overriden by
|
|
|
|
|
// |ice_unwritable_timeout| in IceConfig when determining the writability state.
|
|
|
|
|
// Replace this constant with the config parameter assuming the default value if
|
|
|
|
|
// we decide it is also applicable here.
|
2015-04-06 12:48:47 -07:00
|
|
|
TCPConnection::TCPConnection(TCPPort* port,
|
|
|
|
|
const Candidate& candidate,
|
2014-10-28 22:20:11 +00:00
|
|
|
rtc::AsyncPacketSocket* socket)
|
2015-04-06 12:48:47 -07:00
|
|
|
: Connection(port, 0, candidate),
|
|
|
|
|
socket_(socket),
|
|
|
|
|
error_(0),
|
|
|
|
|
outgoing_(socket == NULL),
|
|
|
|
|
connection_pending_(false),
|
|
|
|
|
pretending_to_be_writable_(false),
|
|
|
|
|
reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) {
|
|
|
|
|
if (outgoing_) {
|
|
|
|
|
CreateOutgoingTcpSocket();
|
2014-10-28 22:20:11 +00:00
|
|
|
} else {
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
// Incoming connections should match one of the network addresses. Same as
|
|
|
|
|
// what's being checked in OnConnect, but just DCHECKing here.
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_VERBOSE) << ToString() << ": socket ipaddr: "
|
2019-09-06 12:51:17 -07:00
|
|
|
<< socket_->GetLocalAddress().ToSensitiveString()
|
2018-03-28 09:47:51 +02:00
|
|
|
<< ", port() Network:" << port->Network()->ToString();
|
2019-01-29 12:47:38 -08:00
|
|
|
RTC_DCHECK(absl::c_any_of(
|
|
|
|
|
port_->Network()->GetIPs(), [this](const rtc::InterfaceAddress& addr) {
|
|
|
|
|
return socket_->GetLocalAddress().ipaddr() == addr;
|
|
|
|
|
}));
|
2015-04-06 12:48:47 -07:00
|
|
|
ConnectSocketSignals(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TCPConnection::~TCPConnection() {}
|
|
|
|
|
|
|
|
|
|
int TCPConnection::Send(const void* data,
|
|
|
|
|
size_t size,
|
|
|
|
|
const rtc::PacketOptions& options) {
|
|
|
|
|
if (!socket_) {
|
|
|
|
|
error_ = ENOTCONN;
|
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 12:48:47 -07:00
|
|
|
// Sending after OnClose on active side will trigger a reconnect for a
|
|
|
|
|
// outgoing connection. Note that the write state is still WRITABLE as we want
|
|
|
|
|
// to spend a few seconds attempting a reconnect before saying we're
|
|
|
|
|
// unwritable.
|
|
|
|
|
if (!connected()) {
|
|
|
|
|
MaybeReconnect();
|
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note that this is important to put this after the previous check to give
|
|
|
|
|
// the connection a chance to reconnect.
|
|
|
|
|
if (pretending_to_be_writable_ || write_state() != STATE_WRITABLE) {
|
2017-11-29 10:25:58 -08:00
|
|
|
// TODO(?): Should STATE_WRITE_TIMEOUT return a non-blocking error?
|
2016-07-28 17:15:20 -07:00
|
|
|
error_ = ENOTCONN;
|
2014-10-28 22:20:11 +00:00
|
|
|
return SOCKET_ERROR;
|
|
|
|
|
}
|
2016-06-01 17:09:15 -07:00
|
|
|
stats_.sent_total_packets++;
|
2018-04-11 20:14:17 -07:00
|
|
|
rtc::PacketOptions modified_options(options);
|
|
|
|
|
static_cast<TCPPort*>(port_)->CopyPortInformationToPacketInfo(
|
|
|
|
|
&modified_options.info_signaled_after_sent);
|
|
|
|
|
int sent = socket_->Send(data, size, modified_options);
|
2014-10-28 22:20:11 +00:00
|
|
|
if (sent < 0) {
|
2016-06-01 17:09:15 -07:00
|
|
|
stats_.sent_discarded_packets++;
|
2014-10-28 22:20:11 +00:00
|
|
|
error_ = socket_->GetError();
|
|
|
|
|
} else {
|
2015-09-14 10:38:08 -07:00
|
|
|
send_rate_tracker_.AddSamples(sent);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
return sent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TCPConnection::GetError() {
|
|
|
|
|
return error_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 12:48:47 -07:00
|
|
|
void TCPConnection::OnConnectionRequestResponse(ConnectionRequest* req,
|
|
|
|
|
StunMessage* response) {
|
2015-08-24 11:58:03 -07:00
|
|
|
// Process the STUN response before we inform upper layer ready to send.
|
2015-04-06 12:48:47 -07:00
|
|
|
Connection::OnConnectionRequestResponse(req, response);
|
2015-08-24 11:58:03 -07:00
|
|
|
|
|
|
|
|
// If we're in the state of pretending to be writeable, we should inform the
|
|
|
|
|
// upper layer it's ready to send again as previous EWOULDLBLOCK from socket
|
|
|
|
|
// would have stopped the outgoing stream.
|
|
|
|
|
if (pretending_to_be_writable_) {
|
|
|
|
|
Connection::OnReadyToSend();
|
|
|
|
|
}
|
|
|
|
|
pretending_to_be_writable_ = false;
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(write_state() == STATE_WRITABLE);
|
2015-04-06 12:48:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(socket == socket_.get());
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
// Do not use this port if the socket bound to an address not associated with
|
|
|
|
|
// the desired network interface. This is seen in Chrome, where TCP sockets
|
|
|
|
|
// cannot be given a binding address, and the platform is expected to pick
|
|
|
|
|
// the correct local address.
|
|
|
|
|
//
|
|
|
|
|
// However, there are two situations in which we allow the bound address to
|
|
|
|
|
// not be one of the addresses of the requested interface:
|
|
|
|
|
// 1. The bound address is the loopback address. This happens when a proxy
|
|
|
|
|
// forces TCP to bind to only the localhost address (see issue 3927).
|
|
|
|
|
// 2. The bound address is the "any address". This happens when
|
|
|
|
|
// multiple_routes is disabled (see issue 4780).
|
|
|
|
|
//
|
|
|
|
|
// Note that, aside from minor differences in log statements, this logic is
|
|
|
|
|
// identical to that in TurnPort.
|
|
|
|
|
const rtc::SocketAddress& socket_address = socket->GetLocalAddress();
|
2019-01-29 12:47:38 -08:00
|
|
|
if (absl::c_any_of(port_->Network()->GetIPs(),
|
|
|
|
|
[socket_address](const rtc::InterfaceAddress& addr) {
|
|
|
|
|
return socket_address.ipaddr() == addr;
|
|
|
|
|
})) {
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_VERBOSE) << ToString() << ": Connection established to "
|
|
|
|
|
<< socket->GetRemoteAddress().ToSensitiveString();
|
2014-10-28 22:20:11 +00:00
|
|
|
} else {
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
if (socket->GetLocalAddress().IsLoopbackIP()) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "Socket is bound to the address:"
|
2019-09-06 12:51:17 -07:00
|
|
|
<< socket_address.ipaddr().ToSensitiveString()
|
2018-03-02 10:58:25 -08:00
|
|
|
<< ", rather than an address associated with network:"
|
2017-11-09 11:09:25 +01:00
|
|
|
<< port_->Network()->ToString()
|
|
|
|
|
<< ". Still allowing it since it's localhost.";
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
} else if (IPIsAny(port_->Network()->GetBestIP())) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING)
|
|
|
|
|
<< "Socket is bound to the address:"
|
2019-09-06 12:51:17 -07:00
|
|
|
<< socket_address.ipaddr().ToSensitiveString()
|
2018-03-02 10:58:25 -08:00
|
|
|
<< ", rather than an address associated with network:"
|
2017-11-09 11:09:25 +01:00
|
|
|
<< port_->Network()->ToString()
|
|
|
|
|
<< ". Still allowing it since it's the 'any' address"
|
2018-03-28 09:47:51 +02:00
|
|
|
", possibly caused by multiple_routes being disabled.";
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
} else {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "Dropping connection as TCP socket bound to IP "
|
2019-09-06 12:51:17 -07:00
|
|
|
<< socket_address.ipaddr().ToSensitiveString()
|
2018-03-02 10:58:25 -08:00
|
|
|
<< ", rather than an address associated with network:"
|
2017-11-09 11:09:25 +01:00
|
|
|
<< port_->Network()->ToString();
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
OnClose(socket, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
2016-05-14 03:19:31 -07:00
|
|
|
|
|
|
|
|
// Connection is established successfully.
|
|
|
|
|
set_connected(true);
|
|
|
|
|
connection_pending_ = false;
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(socket == socket_.get());
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_INFO) << ToString() << ": Connection closed with error " << error;
|
2015-04-06 12:48:47 -07:00
|
|
|
|
|
|
|
|
// Guard against the condition where IPC socket will call OnClose for every
|
|
|
|
|
// packet it can't send.
|
|
|
|
|
if (connected()) {
|
|
|
|
|
set_connected(false);
|
2015-08-25 11:02:55 -07:00
|
|
|
|
|
|
|
|
// Prevent the connection from being destroyed by redundant SignalClose
|
|
|
|
|
// events.
|
2015-04-06 12:48:47 -07:00
|
|
|
pretending_to_be_writable_ = true;
|
|
|
|
|
|
|
|
|
|
// We don't attempt reconnect right here. This is to avoid a case where the
|
|
|
|
|
// shutdown is intentional and reconnect is not necessary. We only reconnect
|
|
|
|
|
// when the connection is used to Send() or Ping().
|
2016-06-10 14:17:27 -07:00
|
|
|
port()->thread()->PostDelayed(RTC_FROM_HERE, reconnection_timeout(), this,
|
2015-04-06 12:48:47 -07:00
|
|
|
MSG_TCPCONNECTION_DELAYED_ONCLOSE);
|
2015-08-25 11:02:55 -07:00
|
|
|
} else if (!pretending_to_be_writable_) {
|
|
|
|
|
// OnClose could be called when the underneath socket times out during the
|
|
|
|
|
// initial connect() (i.e. |pretending_to_be_writable_| is false) . We have
|
|
|
|
|
// to manually destroy here as this connection, as never connected, will not
|
|
|
|
|
// be scheduled for ping to trigger destroy.
|
|
|
|
|
Destroy();
|
2015-04-06 12:48:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPConnection::OnMessage(rtc::Message* pmsg) {
|
|
|
|
|
switch (pmsg->message_id) {
|
|
|
|
|
case MSG_TCPCONNECTION_DELAYED_ONCLOSE:
|
|
|
|
|
// If this connection can't become connected and writable again in 5
|
|
|
|
|
// seconds, it's time to tear this down. This is the case for the original
|
|
|
|
|
// TCP connection on passive side during a reconnect.
|
|
|
|
|
if (pretending_to_be_writable_) {
|
2015-08-25 11:02:55 -07:00
|
|
|
Destroy();
|
2015-04-06 12:48:47 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Connection::OnMessage(pmsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPConnection::MaybeReconnect() {
|
|
|
|
|
// Only reconnect for an outgoing TCPConnection when OnClose was signaled and
|
|
|
|
|
// no outstanding reconnect is pending.
|
|
|
|
|
if (connected() || connection_pending_ || !outgoing_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_INFO) << ToString()
|
|
|
|
|
<< ": TCP Connection with remote is closed, "
|
|
|
|
|
"trying to reconnect";
|
2015-04-06 12:48:47 -07:00
|
|
|
|
|
|
|
|
CreateOutgoingTcpSocket();
|
|
|
|
|
error_ = EPIPE;
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPConnection::OnReadPacket(rtc::AsyncPacketSocket* socket,
|
|
|
|
|
const char* data,
|
|
|
|
|
size_t size,
|
|
|
|
|
const rtc::SocketAddress& remote_addr,
|
2018-11-05 13:01:41 +01:00
|
|
|
const int64_t& packet_time_us) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(socket == socket_.get());
|
2018-11-05 13:01:41 +01:00
|
|
|
Connection::OnReadPacket(data, size, packet_time_us);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(socket == socket_.get());
|
2014-10-28 22:20:11 +00:00
|
|
|
Connection::OnReadyToSend();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 12:48:47 -07:00
|
|
|
void TCPConnection::CreateOutgoingTcpSocket() {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(outgoing_);
|
2015-04-06 12:48:47 -07:00
|
|
|
// TODO(guoweis): Handle failures here (unlikely since TCP).
|
|
|
|
|
int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME)
|
2017-01-09 08:35:45 -08:00
|
|
|
? rtc::PacketSocketFactory::OPT_TLS_FAKE
|
2015-04-06 12:48:47 -07:00
|
|
|
: 0;
|
2019-09-05 14:35:04 +02:00
|
|
|
rtc::PacketSocketTcpOptions tcp_opts;
|
|
|
|
|
tcp_opts.opts = opts;
|
2015-04-06 12:48:47 -07:00
|
|
|
socket_.reset(port()->socket_factory()->CreateClientTcpSocket(
|
Make Port (and subclasses) fully "Network"-based, instead of IP-based.
For ICE, we want sockets that are bound to specific network interfaces,
rather than to specific IP addresses. So, a while ago, we added a
"Network" class that gets passed into the Port constructor, in
addition to the IP address as before.
But we never finished the job of removing the IP address field, such that
a Port only guarantees something about the network interface it's
associated with, and not the specific IP address it ends up with.
This CL does that, and as a consequence, if a port ends up bound to
an IP address other than the "best" one (returned by Network::GetBestIP),
this *won't* be treated as an error.
This is relevant to Android, where even though we pass an IP address
into "Bind" as a way of identifying the network, the socket actually
gets bound using "android_setsocknetwork", which doesn't provide any
guarantees about the IP address. So, if a network interface has multiple
IPv6 addresses (for instance), we may not correctly predict the one
the OS will choose, and that's ok.
This CL also moves "SetAlternateLocalAddress" from VirtualSocket to
VirtualSocketServer, which makes for much more readable test code.
The next step, if there is one, is to pass along the Network class all
the way to SocketServer::Bind. Then the socket server could do smart
things with the network information. We could even stick a platform-
specific network handle in the Network object, such that the socket
server could use it for the binding, or for "sendmsg", for example.
See bug 7026 for more context about the sendmsg idea.
BUG=webrtc:7715
Review-Url: https://codereview.webrtc.org/2989303002
Cr-Commit-Position: refs/heads/master@{#19251}
2017-08-04 15:01:57 -07:00
|
|
|
rtc::SocketAddress(port()->Network()->GetBestIP(), 0),
|
|
|
|
|
remote_candidate().address(), port()->proxy(), port()->user_agent(),
|
2019-09-05 14:35:04 +02:00
|
|
|
tcp_opts));
|
2015-04-06 12:48:47 -07:00
|
|
|
if (socket_) {
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_VERBOSE) << ToString() << ": Connecting from "
|
|
|
|
|
<< socket_->GetLocalAddress().ToSensitiveString()
|
|
|
|
|
<< " to "
|
|
|
|
|
<< remote_candidate().address().ToSensitiveString();
|
2015-04-06 12:48:47 -07:00
|
|
|
set_connected(false);
|
|
|
|
|
connection_pending_ = true;
|
|
|
|
|
ConnectSocketSignals(socket_.get());
|
|
|
|
|
} else {
|
2018-03-28 09:47:51 +02:00
|
|
|
RTC_LOG(LS_WARNING) << ToString() << ": Failed to create connection to "
|
|
|
|
|
<< remote_candidate().address().ToSensitiveString();
|
2015-04-06 12:48:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) {
|
|
|
|
|
if (outgoing_) {
|
|
|
|
|
socket->SignalConnect.connect(this, &TCPConnection::OnConnect);
|
|
|
|
|
}
|
|
|
|
|
socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket);
|
|
|
|
|
socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend);
|
|
|
|
|
socket->SignalClose.connect(this, &TCPConnection::OnClose);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
} // namespace cricket
|