henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/p2p/base/tcpport.h" |
| 29 | |
| 30 | #include "talk/base/common.h" |
| 31 | #include "talk/base/logging.h" |
| 32 | #include "talk/p2p/base/common.h" |
| 33 | |
| 34 | namespace cricket { |
| 35 | |
| 36 | TCPPort::TCPPort(talk_base::Thread* thread, |
| 37 | talk_base::PacketSocketFactory* factory, |
| 38 | talk_base::Network* network, const talk_base::IPAddress& ip, |
| 39 | int min_port, int max_port, const std::string& username, |
| 40 | const std::string& password, bool allow_listen) |
| 41 | : Port(thread, LOCAL_PORT_TYPE, factory, network, ip, min_port, max_port, |
| 42 | username, password), |
| 43 | incoming_only_(false), |
| 44 | allow_listen_(allow_listen), |
| 45 | socket_(NULL), |
| 46 | error_(0) { |
| 47 | // TODO(mallinath) - Set preference value as per RFC 6544. |
| 48 | // http://b/issue?id=7141794 |
| 49 | } |
| 50 | |
| 51 | bool TCPPort::Init() { |
| 52 | if (allow_listen_) { |
| 53 | // Treat failure to create or bind a TCP socket as fatal. This |
| 54 | // should never happen. |
| 55 | socket_ = socket_factory()->CreateServerTcpSocket( |
| 56 | talk_base::SocketAddress(ip(), 0), min_port(), max_port(), |
| 57 | false /* ssl */); |
| 58 | if (!socket_) { |
| 59 | LOG_J(LS_ERROR, this) << "TCP socket creation failed."; |
| 60 | return false; |
| 61 | } |
| 62 | socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection); |
| 63 | socket_->SignalAddressReady.connect(this, &TCPPort::OnAddressReady); |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | TCPPort::~TCPPort() { |
| 69 | delete socket_; |
fischman@webrtc.org | 32001ef | 2013-08-12 23:26:21 +0000 | [diff] [blame] | 70 | std::list<Incoming>::iterator it; |
| 71 | for (it = incoming_.begin(); it != incoming_.end(); ++it) |
| 72 | delete it->socket; |
| 73 | incoming_.clear(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Connection* TCPPort::CreateConnection(const Candidate& address, |
| 77 | CandidateOrigin origin) { |
| 78 | // We only support TCP protocols |
| 79 | if ((address.protocol() != TCP_PROTOCOL_NAME) && |
| 80 | (address.protocol() != SSLTCP_PROTOCOL_NAME)) { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | // We can't accept TCP connections incoming on other ports |
| 85 | if (origin == ORIGIN_OTHER_PORT) |
| 86 | return NULL; |
| 87 | |
| 88 | // Check if we are allowed to make outgoing TCP connections |
| 89 | if (incoming_only_ && (origin == ORIGIN_MESSAGE)) |
| 90 | return NULL; |
| 91 | |
| 92 | // We don't know how to act as an ssl server yet |
| 93 | if ((address.protocol() == SSLTCP_PROTOCOL_NAME) && |
| 94 | (origin == ORIGIN_THIS_PORT)) { |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (!IsCompatibleAddress(address.address())) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | TCPConnection* conn = NULL; |
| 103 | if (talk_base::AsyncPacketSocket* socket = |
| 104 | GetIncoming(address.address(), true)) { |
| 105 | socket->SignalReadPacket.disconnect(this); |
| 106 | conn = new TCPConnection(this, address, socket); |
| 107 | } else { |
| 108 | conn = new TCPConnection(this, address); |
| 109 | } |
| 110 | AddConnection(conn); |
| 111 | return conn; |
| 112 | } |
| 113 | |
| 114 | void TCPPort::PrepareAddress() { |
| 115 | if (socket_) { |
| 116 | // If socket isn't bound yet the address will be added in |
| 117 | // OnAddressReady(). Socket may be in the CLOSED state if Listen() |
| 118 | // failed, we still want ot add the socket address. |
| 119 | LOG(LS_VERBOSE) << "Preparing TCP address, current state: " |
| 120 | << socket_->GetState(); |
| 121 | if (socket_->GetState() == talk_base::AsyncPacketSocket::STATE_BOUND || |
| 122 | socket_->GetState() == talk_base::AsyncPacketSocket::STATE_CLOSED) |
| 123 | AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(), |
| 124 | TCP_PROTOCOL_NAME, LOCAL_PORT_TYPE, |
| 125 | ICE_TYPE_PREFERENCE_HOST_TCP, true); |
| 126 | } else { |
| 127 | LOG_J(LS_INFO, this) << "Not listening due to firewall restrictions."; |
henrike@webrtc.org | 9de257d | 2013-07-17 14:42:53 +0000 | [diff] [blame] | 128 | // Note: We still add the address, since otherwise the remote side won't |
| 129 | // recognize our incoming TCP connections. |
| 130 | AddAddress(talk_base::SocketAddress(ip(), 0), |
| 131 | talk_base::SocketAddress(ip(), 0), TCP_PROTOCOL_NAME, |
| 132 | LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | int TCPPort::SendTo(const void* data, size_t size, |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 137 | const talk_base::SocketAddress& addr, |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 138 | const talk_base::PacketOptions& options, |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 139 | bool payload) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | talk_base::AsyncPacketSocket * socket = NULL; |
| 141 | if (TCPConnection * conn = static_cast<TCPConnection*>(GetConnection(addr))) { |
| 142 | socket = conn->socket(); |
| 143 | } else { |
| 144 | socket = GetIncoming(addr); |
| 145 | } |
| 146 | if (!socket) { |
| 147 | LOG_J(LS_ERROR, this) << "Attempted to send to an unknown destination, " |
| 148 | << addr.ToSensitiveString(); |
| 149 | return -1; // TODO: Set error_ |
| 150 | } |
| 151 | |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 152 | int sent = socket->Send(data, size, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | if (sent < 0) { |
| 154 | error_ = socket->GetError(); |
| 155 | LOG_J(LS_ERROR, this) << "TCP send of " << size |
| 156 | << " bytes failed with error " << error_; |
| 157 | } |
| 158 | return sent; |
| 159 | } |
| 160 | |
| 161 | int TCPPort::GetOption(talk_base::Socket::Option opt, int* value) { |
| 162 | if (socket_) { |
| 163 | return socket_->GetOption(opt, value); |
| 164 | } else { |
| 165 | return SOCKET_ERROR; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | int TCPPort::SetOption(talk_base::Socket::Option opt, int value) { |
| 170 | if (socket_) { |
| 171 | return socket_->SetOption(opt, value); |
| 172 | } else { |
| 173 | return SOCKET_ERROR; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int TCPPort::GetError() { |
| 178 | return error_; |
| 179 | } |
| 180 | |
| 181 | void TCPPort::OnNewConnection(talk_base::AsyncPacketSocket* socket, |
| 182 | talk_base::AsyncPacketSocket* new_socket) { |
| 183 | ASSERT(socket == socket_); |
| 184 | |
| 185 | Incoming incoming; |
| 186 | incoming.addr = new_socket->GetRemoteAddress(); |
| 187 | incoming.socket = new_socket; |
| 188 | incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket); |
| 189 | incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); |
| 190 | |
| 191 | LOG_J(LS_VERBOSE, this) << "Accepted connection from " |
| 192 | << incoming.addr.ToSensitiveString(); |
| 193 | incoming_.push_back(incoming); |
| 194 | } |
| 195 | |
| 196 | talk_base::AsyncPacketSocket* TCPPort::GetIncoming( |
| 197 | const talk_base::SocketAddress& addr, bool remove) { |
| 198 | talk_base::AsyncPacketSocket* socket = NULL; |
| 199 | for (std::list<Incoming>::iterator it = incoming_.begin(); |
| 200 | it != incoming_.end(); ++it) { |
| 201 | if (it->addr == addr) { |
| 202 | socket = it->socket; |
| 203 | if (remove) |
| 204 | incoming_.erase(it); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | return socket; |
| 209 | } |
| 210 | |
| 211 | void TCPPort::OnReadPacket(talk_base::AsyncPacketSocket* socket, |
| 212 | const char* data, size_t size, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 213 | const talk_base::SocketAddress& remote_addr, |
| 214 | const talk_base::PacketTime& packet_time) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | Port::OnReadPacket(data, size, remote_addr, PROTO_TCP); |
| 216 | } |
| 217 | |
| 218 | void TCPPort::OnReadyToSend(talk_base::AsyncPacketSocket* socket) { |
| 219 | Port::OnReadyToSend(); |
| 220 | } |
| 221 | |
| 222 | void TCPPort::OnAddressReady(talk_base::AsyncPacketSocket* socket, |
| 223 | const talk_base::SocketAddress& address) { |
| 224 | AddAddress(address, address, "tcp", |
| 225 | LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, |
| 226 | true); |
| 227 | } |
| 228 | |
| 229 | TCPConnection::TCPConnection(TCPPort* port, const Candidate& candidate, |
| 230 | talk_base::AsyncPacketSocket* socket) |
| 231 | : Connection(port, 0, candidate), socket_(socket), error_(0) { |
| 232 | bool outgoing = (socket_ == NULL); |
| 233 | if (outgoing) { |
| 234 | // TODO: Handle failures here (unlikely since TCP). |
| 235 | int opts = (candidate.protocol() == SSLTCP_PROTOCOL_NAME) ? |
| 236 | talk_base::PacketSocketFactory::OPT_SSLTCP : 0; |
| 237 | socket_ = port->socket_factory()->CreateClientTcpSocket( |
| 238 | talk_base::SocketAddress(port_->Network()->ip(), 0), |
| 239 | candidate.address(), port->proxy(), port->user_agent(), opts); |
| 240 | if (socket_) { |
| 241 | LOG_J(LS_VERBOSE, this) << "Connecting from " |
| 242 | << socket_->GetLocalAddress().ToSensitiveString() |
| 243 | << " to " |
| 244 | << candidate.address().ToSensitiveString(); |
| 245 | set_connected(false); |
| 246 | socket_->SignalConnect.connect(this, &TCPConnection::OnConnect); |
| 247 | } else { |
| 248 | LOG_J(LS_WARNING, this) << "Failed to create connection to " |
| 249 | << candidate.address().ToSensitiveString(); |
| 250 | } |
| 251 | } else { |
| 252 | // Incoming connections should match the network address. |
| 253 | ASSERT(socket_->GetLocalAddress().ipaddr() == port->ip()); |
| 254 | } |
| 255 | |
| 256 | if (socket_) { |
| 257 | socket_->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket); |
| 258 | socket_->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend); |
| 259 | socket_->SignalClose.connect(this, &TCPConnection::OnClose); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | TCPConnection::~TCPConnection() { |
| 264 | delete socket_; |
| 265 | } |
| 266 | |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 267 | int TCPConnection::Send(const void* data, size_t size, |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 268 | const talk_base::PacketOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | if (!socket_) { |
| 270 | error_ = ENOTCONN; |
| 271 | return SOCKET_ERROR; |
| 272 | } |
| 273 | |
| 274 | if (write_state() != STATE_WRITABLE) { |
| 275 | // TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error? |
| 276 | error_ = EWOULDBLOCK; |
| 277 | return SOCKET_ERROR; |
| 278 | } |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 279 | int sent = socket_->Send(data, size, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | if (sent < 0) { |
| 281 | error_ = socket_->GetError(); |
| 282 | } else { |
| 283 | send_rate_tracker_.Update(sent); |
| 284 | } |
| 285 | return sent; |
| 286 | } |
| 287 | |
| 288 | int TCPConnection::GetError() { |
| 289 | return error_; |
| 290 | } |
| 291 | |
| 292 | void TCPConnection::OnConnect(talk_base::AsyncPacketSocket* socket) { |
| 293 | ASSERT(socket == socket_); |
| 294 | LOG_J(LS_VERBOSE, this) << "Connection established to " |
| 295 | << socket->GetRemoteAddress().ToSensitiveString(); |
| 296 | set_connected(true); |
| 297 | } |
| 298 | |
| 299 | void TCPConnection::OnClose(talk_base::AsyncPacketSocket* socket, int error) { |
| 300 | ASSERT(socket == socket_); |
| 301 | LOG_J(LS_VERBOSE, this) << "Connection closed with error " << error; |
| 302 | set_connected(false); |
| 303 | set_write_state(STATE_WRITE_TIMEOUT); |
| 304 | } |
| 305 | |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 306 | void TCPConnection::OnReadPacket( |
| 307 | talk_base::AsyncPacketSocket* socket, const char* data, size_t size, |
| 308 | const talk_base::SocketAddress& remote_addr, |
| 309 | const talk_base::PacketTime& packet_time) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 310 | ASSERT(socket == socket_); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 311 | Connection::OnReadPacket(data, size, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void TCPConnection::OnReadyToSend(talk_base::AsyncPacketSocket* socket) { |
| 315 | ASSERT(socket == socket_); |
| 316 | Connection::OnReadyToSend(); |
| 317 | } |
| 318 | |
| 319 | } // namespace cricket |