2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2004--2005, Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#include "talk/p2p/base/tcpport.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#include "talk/p2p/base/common.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/common.h"
|
|
|
|
#include "webrtc/base/logging.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
TCPPort::TCPPort(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network, const rtc::IPAddress& ip,
|
2013-07-10 02:45:36 +02:00
|
|
|
int min_port, int max_port, const std::string& username,
|
|
|
|
const std::string& password, bool allow_listen)
|
|
|
|
: Port(thread, LOCAL_PORT_TYPE, factory, network, ip, min_port, max_port,
|
|
|
|
username, password),
|
|
|
|
incoming_only_(false),
|
|
|
|
allow_listen_(allow_listen),
|
|
|
|
socket_(NULL),
|
|
|
|
error_(0) {
|
|
|
|
// TODO(mallinath) - Set preference value as per RFC 6544.
|
|
|
|
// http://b/issue?id=7141794
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TCPPort::Init() {
|
|
|
|
if (allow_listen_) {
|
|
|
|
// Treat failure to create or bind a TCP socket as fatal. This
|
|
|
|
// should never happen.
|
|
|
|
socket_ = socket_factory()->CreateServerTcpSocket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress(ip(), 0), min_port(), max_port(),
|
2013-07-10 02:45:36 +02:00
|
|
|
false /* ssl */);
|
|
|
|
if (!socket_) {
|
|
|
|
LOG_J(LS_ERROR, this) << "TCP socket creation failed.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection);
|
|
|
|
socket_->SignalAddressReady.connect(this, &TCPPort::OnAddressReady);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPPort::~TCPPort() {
|
|
|
|
delete socket_;
|
2013-08-13 01:26:21 +02:00
|
|
|
std::list<Incoming>::iterator it;
|
|
|
|
for (it = incoming_.begin(); it != incoming_.end(); ++it)
|
|
|
|
delete it->socket;
|
|
|
|
incoming_.clear();
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection* TCPPort::CreateConnection(const Candidate& address,
|
|
|
|
CandidateOrigin origin) {
|
|
|
|
// We only support TCP protocols
|
|
|
|
if ((address.protocol() != TCP_PROTOCOL_NAME) &&
|
|
|
|
(address.protocol() != SSLTCP_PROTOCOL_NAME)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:29:20 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
// We can't accept TCP connections incoming on other ports
|
|
|
|
if (origin == ORIGIN_OTHER_PORT)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Check if we are allowed to make outgoing TCP connections
|
|
|
|
if (incoming_only_ && (origin == ORIGIN_MESSAGE))
|
|
|
|
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;
|
2014-07-29 19:36:52 +02:00
|
|
|
if (rtc::AsyncPacketSocket* socket =
|
2013-07-10 02:45:36 +02:00
|
|
|
GetIncoming(address.address(), true)) {
|
|
|
|
socket->SignalReadPacket.disconnect(this);
|
|
|
|
conn = new TCPConnection(this, address, socket);
|
|
|
|
} else {
|
|
|
|
conn = new TCPConnection(this, address);
|
|
|
|
}
|
|
|
|
AddConnection(conn);
|
|
|
|
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()
|
2014-08-09 00:29:20 +02:00
|
|
|
// failed, we still want to add the socket address.
|
2013-07-10 02:45:36 +02:00
|
|
|
LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
|
|
|
|
<< socket_->GetState();
|
2014-07-29 19:36:52 +02:00
|
|
|
if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND ||
|
|
|
|
socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED)
|
2013-07-10 02:45:36 +02:00
|
|
|
AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(),
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress(),
|
2014-08-09 00:29:20 +02:00
|
|
|
TCP_PROTOCOL_NAME, TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
|
|
|
|
ICE_TYPE_PREFERENCE_HOST_TCP, 0, true);
|
2013-07-10 02:45:36 +02:00
|
|
|
} else {
|
|
|
|
LOG_J(LS_INFO, this) << "Not listening due to firewall restrictions.";
|
2013-07-17 16:42:53 +02:00
|
|
|
// Note: We still add the address, since otherwise the remote side won't
|
|
|
|
// recognize our incoming TCP connections.
|
2014-07-29 19:36:52 +02:00
|
|
|
AddAddress(rtc::SocketAddress(ip(), 0),
|
|
|
|
rtc::SocketAddress(ip(), 0), rtc::SocketAddress(),
|
2014-08-09 00:29:20 +02:00
|
|
|
TCP_PROTOCOL_NAME, TCPTYPE_ACTIVE_STR, LOCAL_PORT_TYPE,
|
|
|
|
ICE_TYPE_PREFERENCE_HOST_TCP, 0, true);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPPort::SendTo(const void* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& addr,
|
|
|
|
const rtc::PacketOptions& options,
|
2013-09-23 22:34:45 +02:00
|
|
|
bool payload) {
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket * socket = NULL;
|
2013-07-10 02:45:36 +02:00
|
|
|
if (TCPConnection * conn = static_cast<TCPConnection*>(GetConnection(addr))) {
|
|
|
|
socket = conn->socket();
|
|
|
|
} else {
|
|
|
|
socket = GetIncoming(addr);
|
|
|
|
}
|
|
|
|
if (!socket) {
|
|
|
|
LOG_J(LS_ERROR, this) << "Attempted to send to an unknown destination, "
|
|
|
|
<< addr.ToSensitiveString();
|
|
|
|
return -1; // TODO: Set error_
|
|
|
|
}
|
|
|
|
|
2014-02-14 01:56:12 +01:00
|
|
|
int sent = socket->Send(data, size, options);
|
2013-07-10 02:45:36 +02:00
|
|
|
if (sent < 0) {
|
|
|
|
error_ = socket->GetError();
|
|
|
|
LOG_J(LS_ERROR, this) << "TCP send of " << size
|
|
|
|
<< " bytes failed with error " << error_;
|
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
int TCPPort::GetOption(rtc::Socket::Option opt, int* value) {
|
2013-07-10 02:45:36 +02:00
|
|
|
if (socket_) {
|
|
|
|
return socket_->GetOption(opt, value);
|
|
|
|
} else {
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
int TCPPort::SetOption(rtc::Socket::Option opt, int value) {
|
2013-07-10 02:45:36 +02:00
|
|
|
if (socket_) {
|
|
|
|
return socket_->SetOption(opt, value);
|
|
|
|
} else {
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPPort::GetError() {
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket,
|
|
|
|
rtc::AsyncPacketSocket* new_socket) {
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT(socket == socket_);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
LOG_J(LS_VERBOSE, this) << "Accepted connection from "
|
|
|
|
<< incoming.addr.ToSensitiveString();
|
|
|
|
incoming_.push_back(incoming);
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* TCPPort::GetIncoming(
|
|
|
|
const rtc::SocketAddress& addr, bool remove) {
|
|
|
|
rtc::AsyncPacketSocket* socket = NULL;
|
2013-07-10 02:45:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket,
|
2013-07-10 02:45:36 +02:00
|
|
|
const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time) {
|
2013-07-10 02:45:36 +02:00
|
|
|
Port::OnReadPacket(data, size, remote_addr, PROTO_TCP);
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
|
2013-07-10 02:45:36 +02:00
|
|
|
Port::OnReadyToSend();
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPPort::OnAddressReady(rtc::AsyncPacketSocket* socket,
|
|
|
|
const rtc::SocketAddress& address) {
|
2014-08-09 00:29:20 +02:00
|
|
|
AddAddress(address, address, rtc::SocketAddress(),
|
|
|
|
TCP_PROTOCOL_NAME, TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
|
|
|
|
ICE_TYPE_PREFERENCE_HOST_TCP, 0, true);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TCPConnection::TCPConnection(TCPPort* port, const Candidate& candidate,
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* socket)
|
2013-07-10 02:45:36 +02:00
|
|
|
: Connection(port, 0, candidate), socket_(socket), error_(0) {
|
|
|
|
bool outgoing = (socket_ == NULL);
|
|
|
|
if (outgoing) {
|
|
|
|
// TODO: Handle failures here (unlikely since TCP).
|
|
|
|
int opts = (candidate.protocol() == SSLTCP_PROTOCOL_NAME) ?
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::PacketSocketFactory::OPT_SSLTCP : 0;
|
2013-07-10 02:45:36 +02:00
|
|
|
socket_ = port->socket_factory()->CreateClientTcpSocket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress(port->ip(), 0),
|
2013-07-10 02:45:36 +02:00
|
|
|
candidate.address(), port->proxy(), port->user_agent(), opts);
|
|
|
|
if (socket_) {
|
|
|
|
LOG_J(LS_VERBOSE, this) << "Connecting from "
|
|
|
|
<< socket_->GetLocalAddress().ToSensitiveString()
|
|
|
|
<< " to "
|
|
|
|
<< candidate.address().ToSensitiveString();
|
|
|
|
set_connected(false);
|
|
|
|
socket_->SignalConnect.connect(this, &TCPConnection::OnConnect);
|
|
|
|
} else {
|
|
|
|
LOG_J(LS_WARNING, this) << "Failed to create connection to "
|
|
|
|
<< candidate.address().ToSensitiveString();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Incoming connections should match the network address.
|
|
|
|
ASSERT(socket_->GetLocalAddress().ipaddr() == port->ip());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket_) {
|
|
|
|
socket_->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket);
|
|
|
|
socket_->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend);
|
|
|
|
socket_->SignalClose.connect(this, &TCPConnection::OnClose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPConnection::~TCPConnection() {
|
|
|
|
delete socket_;
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:34:45 +02:00
|
|
|
int TCPConnection::Send(const void* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::PacketOptions& options) {
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!socket_) {
|
|
|
|
error_ = ENOTCONN;
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write_state() != STATE_WRITABLE) {
|
|
|
|
// TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error?
|
|
|
|
error_ = EWOULDBLOCK;
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
}
|
2014-02-14 01:56:12 +01:00
|
|
|
int sent = socket_->Send(data, size, options);
|
2013-07-10 02:45:36 +02:00
|
|
|
if (sent < 0) {
|
|
|
|
error_ = socket_->GetError();
|
|
|
|
} else {
|
|
|
|
send_rate_tracker_.Update(sent);
|
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPConnection::GetError() {
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) {
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT(socket == socket_);
|
2014-04-24 02:00:12 +02:00
|
|
|
// Do not use this connection if the socket bound to a different address than
|
|
|
|
// the one we asked for. 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.
|
|
|
|
if (socket->GetLocalAddress().ipaddr() == port()->ip()) {
|
|
|
|
LOG_J(LS_VERBOSE, this) << "Connection established to "
|
|
|
|
<< socket->GetRemoteAddress().ToSensitiveString();
|
|
|
|
set_connected(true);
|
|
|
|
} else {
|
|
|
|
LOG_J(LS_WARNING, this) << "Dropping connection as TCP socket bound to a "
|
|
|
|
<< "different address from the local candidate.";
|
|
|
|
socket_->Close();
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) {
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT(socket == socket_);
|
|
|
|
LOG_J(LS_VERBOSE, this) << "Connection closed with error " << error;
|
|
|
|
set_connected(false);
|
|
|
|
set_write_state(STATE_WRITE_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:03 +01:00
|
|
|
void TCPConnection::OnReadPacket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* socket, const char* data, size_t size,
|
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time) {
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT(socket == socket_);
|
2013-12-13 01:21:03 +01:00
|
|
|
Connection::OnReadPacket(data, size, packet_time);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT(socket == socket_);
|
|
|
|
Connection::OnReadyToSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cricket
|