2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/p2p/base/tcpport.h"
13 #include "webrtc/p2p/base/common.h"
14 #include "webrtc/base/common.h"
15 #include "webrtc/base/logging.h"
19 TCPPort::TCPPort(rtc::Thread* thread,
20 rtc::PacketSocketFactory* factory,
21 rtc::Network* network,
22 const rtc::IPAddress& ip,
25 const std::string& username,
26 const std::string& password,
28 : Port(thread, LOCAL_PORT_TYPE, factory, network, ip, min_port, max_port,
30 incoming_only_(false),
31 allow_listen_(allow_listen),
34 // TODO(mallinath) - Set preference value as per RFC 6544.
35 // http://b/issue?id=7141794
38 bool TCPPort::Init() {
40 // Treat failure to create or bind a TCP socket as fatal. This
41 // should never happen.
42 socket_ = socket_factory()->CreateServerTcpSocket(
43 rtc::SocketAddress(ip(), 0), min_port(), max_port(),
46 LOG_J(LS_ERROR, this) << "TCP socket creation failed.";
49 socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection);
50 socket_->SignalAddressReady.connect(this, &TCPPort::OnAddressReady);
57 std::list<Incoming>::iterator it;
58 for (it = incoming_.begin(); it != incoming_.end(); ++it)
63 Connection* TCPPort::CreateConnection(const Candidate& address,
64 CandidateOrigin origin) {
65 // We only support TCP protocols
66 if ((address.protocol() != TCP_PROTOCOL_NAME) &&
67 (address.protocol() != SSLTCP_PROTOCOL_NAME)) {
71 if (address.tcptype() == TCPTYPE_ACTIVE_STR ||
72 (address.tcptype().empty() && address.address().port() == 0)) {
73 // It's active only candidate, we should not try to create connections
74 // for these candidates.
78 // We can't accept TCP connections incoming on other ports
79 if (origin == ORIGIN_OTHER_PORT)
82 // Check if we are allowed to make outgoing TCP connections
83 if (incoming_only_ && (origin == ORIGIN_MESSAGE))
86 // We don't know how to act as an ssl server yet
87 if ((address.protocol() == SSLTCP_PROTOCOL_NAME) &&
88 (origin == ORIGIN_THIS_PORT)) {
92 if (!IsCompatibleAddress(address.address())) {
96 TCPConnection* conn = NULL;
97 if (rtc::AsyncPacketSocket* socket =
98 GetIncoming(address.address(), true)) {
99 socket->SignalReadPacket.disconnect(this);
100 conn = new TCPConnection(this, address, socket);
102 conn = new TCPConnection(this, address);
108 void TCPPort::PrepareAddress() {
110 // If socket isn't bound yet the address will be added in
111 // OnAddressReady(). Socket may be in the CLOSED state if Listen()
112 // failed, we still want to add the socket address.
113 LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
114 << socket_->GetState();
115 if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND ||
116 socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED)
117 AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(),
118 rtc::SocketAddress(),
119 TCP_PROTOCOL_NAME, TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
120 ICE_TYPE_PREFERENCE_HOST_TCP, 0, true);
122 LOG_J(LS_INFO, this) << "Not listening due to firewall restrictions.";
123 // Note: We still add the address, since otherwise the remote side won't
124 // recognize our incoming TCP connections.
125 AddAddress(rtc::SocketAddress(ip(), 0),
126 rtc::SocketAddress(ip(), 0), rtc::SocketAddress(),
127 TCP_PROTOCOL_NAME, TCPTYPE_ACTIVE_STR, LOCAL_PORT_TYPE,
128 ICE_TYPE_PREFERENCE_HOST_TCP, 0, true);
132 int TCPPort::SendTo(const void* data, size_t size,
133 const rtc::SocketAddress& addr,
134 const rtc::PacketOptions& options,
136 rtc::AsyncPacketSocket * socket = NULL;
137 if (TCPConnection * conn = static_cast<TCPConnection*>(GetConnection(addr))) {
138 socket = conn->socket();
140 socket = GetIncoming(addr);
143 LOG_J(LS_ERROR, this) << "Attempted to send to an unknown destination, "
144 << addr.ToSensitiveString();
145 return -1; // TODO: Set error_
148 int sent = socket->Send(data, size, options);
150 error_ = socket->GetError();
151 LOG_J(LS_ERROR, this) << "TCP send of " << size
152 << " bytes failed with error " << error_;
157 int TCPPort::GetOption(rtc::Socket::Option opt, int* value) {
159 return socket_->GetOption(opt, value);
165 int TCPPort::SetOption(rtc::Socket::Option opt, int value) {
167 return socket_->SetOption(opt, value);
173 int TCPPort::GetError() {
177 void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket,
178 rtc::AsyncPacketSocket* new_socket) {
179 ASSERT(socket == socket_);
182 incoming.addr = new_socket->GetRemoteAddress();
183 incoming.socket = new_socket;
184 incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket);
185 incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend);
187 LOG_J(LS_VERBOSE, this) << "Accepted connection from "
188 << incoming.addr.ToSensitiveString();
189 incoming_.push_back(incoming);
192 rtc::AsyncPacketSocket* TCPPort::GetIncoming(
193 const rtc::SocketAddress& addr, bool remove) {
194 rtc::AsyncPacketSocket* socket = NULL;
195 for (std::list<Incoming>::iterator it = incoming_.begin();
196 it != incoming_.end(); ++it) {
197 if (it->addr == addr) {
207 void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket,
208 const char* data, size_t size,
209 const rtc::SocketAddress& remote_addr,
210 const rtc::PacketTime& packet_time) {
211 Port::OnReadPacket(data, size, remote_addr, PROTO_TCP);
214 void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
215 Port::OnReadyToSend();
218 void TCPPort::OnAddressReady(rtc::AsyncPacketSocket* socket,
219 const rtc::SocketAddress& address) {
220 AddAddress(address, address, rtc::SocketAddress(),
221 TCP_PROTOCOL_NAME, TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
222 ICE_TYPE_PREFERENCE_HOST_TCP, 0, true);
225 TCPConnection::TCPConnection(TCPPort* port, const Candidate& candidate,
226 rtc::AsyncPacketSocket* socket)
227 : Connection(port, 0, candidate), socket_(socket), error_(0) {
228 bool outgoing = (socket_ == NULL);
230 // TODO: Handle failures here (unlikely since TCP).
231 int opts = (candidate.protocol() == SSLTCP_PROTOCOL_NAME) ?
232 rtc::PacketSocketFactory::OPT_SSLTCP : 0;
233 socket_ = port->socket_factory()->CreateClientTcpSocket(
234 rtc::SocketAddress(port->ip(), 0),
235 candidate.address(), port->proxy(), port->user_agent(), opts);
237 LOG_J(LS_VERBOSE, this) << "Connecting from "
238 << socket_->GetLocalAddress().ToSensitiveString()
240 << candidate.address().ToSensitiveString();
241 set_connected(false);
242 socket_->SignalConnect.connect(this, &TCPConnection::OnConnect);
244 LOG_J(LS_WARNING, this) << "Failed to create connection to "
245 << candidate.address().ToSensitiveString();
248 // Incoming connections should match the network address.
249 ASSERT(socket_->GetLocalAddress().ipaddr() == port->ip());
253 socket_->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket);
254 socket_->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend);
255 socket_->SignalClose.connect(this, &TCPConnection::OnClose);
259 TCPConnection::~TCPConnection() {
263 int TCPConnection::Send(const void* data, size_t size,
264 const rtc::PacketOptions& options) {
270 if (write_state() != STATE_WRITABLE) {
271 // TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error?
272 error_ = EWOULDBLOCK;
275 int sent = socket_->Send(data, size, options);
277 error_ = socket_->GetError();
279 send_rate_tracker_.Update(sent);
284 int TCPConnection::GetError() {
288 void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) {
289 ASSERT(socket == socket_);
290 // Do not use this connection if the socket bound to a different address than
291 // the one we asked for. This is seen in Chrome, where TCP sockets cannot be
292 // given a binding address, and the platform is expected to pick the
293 // correct local address.
294 if (socket->GetLocalAddress().ipaddr() == port()->ip()) {
295 LOG_J(LS_VERBOSE, this) << "Connection established to "
296 << socket->GetRemoteAddress().ToSensitiveString();
299 LOG_J(LS_WARNING, this) << "Dropping connection as TCP socket bound to a "
300 << "different address from the local candidate.";
305 void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) {
306 ASSERT(socket == socket_);
307 LOG_J(LS_VERBOSE, this) << "Connection closed with error " << error;
308 set_connected(false);
309 set_write_state(STATE_WRITE_TIMEOUT);
312 void TCPConnection::OnReadPacket(
313 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
314 const rtc::SocketAddress& remote_addr,
315 const rtc::PacketTime& packet_time) {
316 ASSERT(socket == socket_);
317 Connection::OnReadPacket(data, size, packet_time);
320 void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
321 ASSERT(socket == socket_);
322 Connection::OnReadyToSend();
325 } // namespace cricket