You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/p2p/base/tcpport.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/p2p/base/common.h"
  31. namespace cricket {
  32. TCPPort::TCPPort(talk_base::Thread* thread,
  33. talk_base::PacketSocketFactory* factory,
  34. talk_base::Network* network, const talk_base::IPAddress& ip,
  35. int min_port, int max_port, const std::string& username,
  36. const std::string& password, bool allow_listen)
  37. : Port(thread, LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP,
  38. factory, network, ip, min_port, max_port,
  39. username, password),
  40. incoming_only_(false),
  41. allow_listen_(allow_listen),
  42. socket_(NULL),
  43. error_(0) {
  44. // TODO(mallinath) - Set preference value as per RFC 6544.
  45. // http://b/issue?id=7141794
  46. }
  47. bool TCPPort::Init() {
  48. if (allow_listen_) {
  49. // Treat failure to create or bind a TCP socket as fatal. This
  50. // should never happen.
  51. socket_ = socket_factory()->CreateServerTcpSocket(
  52. talk_base::SocketAddress(ip(), 0), min_port(), max_port(),
  53. false /* ssl */);
  54. if (!socket_) {
  55. LOG_J(LS_ERROR, this) << "TCP socket creation failed.";
  56. return false;
  57. }
  58. socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection);
  59. socket_->SignalAddressReady.connect(this, &TCPPort::OnAddressReady);
  60. }
  61. return true;
  62. }
  63. TCPPort::~TCPPort() {
  64. delete socket_;
  65. }
  66. Connection* TCPPort::CreateConnection(const Candidate& address,
  67. CandidateOrigin origin) {
  68. // We only support TCP protocols
  69. if ((address.protocol() != "tcp") && (address.protocol() != "ssltcp"))
  70. return NULL;
  71. // We can't accept TCP connections incoming on other ports
  72. if (origin == ORIGIN_OTHER_PORT)
  73. return NULL;
  74. // Check if we are allowed to make outgoing TCP connections
  75. if (incoming_only_ && (origin == ORIGIN_MESSAGE))
  76. return NULL;
  77. // We don't know how to act as an ssl server yet
  78. if ((address.protocol() == "ssltcp") && (origin == ORIGIN_THIS_PORT))
  79. return NULL;
  80. if (!IsCompatibleAddress(address.address())) {
  81. return NULL;
  82. }
  83. TCPConnection* conn = NULL;
  84. if (talk_base::AsyncPacketSocket* socket =
  85. GetIncoming(address.address(), true)) {
  86. socket->SignalReadPacket.disconnect(this);
  87. conn = new TCPConnection(this, address, socket);
  88. } else {
  89. conn = new TCPConnection(this, address);
  90. }
  91. AddConnection(conn);
  92. return conn;
  93. }
  94. void TCPPort::PrepareAddress() {
  95. if (socket_) {
  96. // If socket isn't bound yet the address will be added in
  97. // OnAddressReady(). Socket may be in the CLOSED state if Listen()
  98. // failed, we still want ot add the socket address.
  99. LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
  100. << socket_->GetState();
  101. if (socket_->GetState() == talk_base::AsyncPacketSocket::STATE_BOUND ||
  102. socket_->GetState() == talk_base::AsyncPacketSocket::STATE_CLOSED)
  103. AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(),
  104. "tcp", LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, true);
  105. } else {
  106. LOG_J(LS_INFO, this) << "Not listening due to firewall restrictions.";
  107. // Note: We still add the address, since otherwise the remote side won't
  108. // recognize our incoming TCP connections.
  109. AddAddress(talk_base::SocketAddress(ip(), 0),
  110. talk_base::SocketAddress(ip(), 0), "tcp",
  111. LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, true);
  112. }
  113. }
  114. int TCPPort::SendTo(const void* data, size_t size,
  115. const talk_base::SocketAddress& addr, bool payload) {
  116. talk_base::AsyncPacketSocket * socket = NULL;
  117. if (TCPConnection * conn = static_cast<TCPConnection*>(GetConnection(addr))) {
  118. socket = conn->socket();
  119. } else {
  120. socket = GetIncoming(addr);
  121. }
  122. if (!socket) {
  123. LOG_J(LS_ERROR, this) << "Attempted to send to an unknown destination, "
  124. << addr.ToString();
  125. return -1; // TODO: Set error_
  126. }
  127. int sent = socket->Send(data, size);
  128. if (sent < 0) {
  129. error_ = socket->GetError();
  130. LOG_J(LS_ERROR, this) << "TCP send of " << size
  131. << " bytes failed with error " << error_;
  132. }
  133. return sent;
  134. }
  135. int TCPPort::GetOption(talk_base::Socket::Option opt, int* value) {
  136. if (socket_) {
  137. return socket_->GetOption(opt, value);
  138. } else {
  139. return SOCKET_ERROR;
  140. }
  141. }
  142. int TCPPort::SetOption(talk_base::Socket::Option opt, int value) {
  143. if (socket_) {
  144. return socket_->SetOption(opt, value);
  145. } else {
  146. return SOCKET_ERROR;
  147. }
  148. }
  149. int TCPPort::GetError() {
  150. return error_;
  151. }
  152. void TCPPort::OnNewConnection(talk_base::AsyncPacketSocket* socket,
  153. talk_base::AsyncPacketSocket* new_socket) {
  154. ASSERT(socket == socket_);
  155. Incoming incoming;
  156. incoming.addr = new_socket->GetRemoteAddress();
  157. incoming.socket = new_socket;
  158. incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket);
  159. LOG_J(LS_VERBOSE, this) << "Accepted connection from "
  160. << incoming.addr.ToString();
  161. incoming_.push_back(incoming);
  162. }
  163. talk_base::AsyncPacketSocket* TCPPort::GetIncoming(
  164. const talk_base::SocketAddress& addr, bool remove) {
  165. talk_base::AsyncPacketSocket* socket = NULL;
  166. for (std::list<Incoming>::iterator it = incoming_.begin();
  167. it != incoming_.end(); ++it) {
  168. if (it->addr == addr) {
  169. socket = it->socket;
  170. if (remove)
  171. incoming_.erase(it);
  172. break;
  173. }
  174. }
  175. return socket;
  176. }
  177. void TCPPort::OnReadPacket(talk_base::AsyncPacketSocket* socket,
  178. const char* data, size_t size,
  179. const talk_base::SocketAddress& remote_addr) {
  180. Port::OnReadPacket(data, size, remote_addr, PROTO_TCP);
  181. }
  182. void TCPPort::OnAddressReady(talk_base::AsyncPacketSocket* socket,
  183. const talk_base::SocketAddress& address) {
  184. AddAddress(address, address, "tcp",
  185. LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP,
  186. true);
  187. }
  188. TCPConnection::TCPConnection(TCPPort* port, const Candidate& candidate,
  189. talk_base::AsyncPacketSocket* socket)
  190. : Connection(port, 0, candidate), socket_(socket), error_(0) {
  191. bool outgoing = (socket_ == NULL);
  192. if (outgoing) {
  193. // TODO: Handle failures here (unlikely since TCP).
  194. socket_ = port->socket_factory()->CreateClientTcpSocket(
  195. talk_base::SocketAddress(port_->Network()->ip(), 0),
  196. candidate.address(), port->proxy(), port->user_agent(),
  197. candidate.protocol() == "ssltcp");
  198. if (socket_) {
  199. LOG_J(LS_VERBOSE, this) << "Connecting from "
  200. << socket_->GetLocalAddress().ToString() << " to "
  201. << candidate.address().ToString();
  202. set_connected(false);
  203. socket_->SignalConnect.connect(this, &TCPConnection::OnConnect);
  204. } else {
  205. LOG_J(LS_WARNING, this) << "Failed to create connection to "
  206. << candidate.address().ToString();
  207. }
  208. } else {
  209. // Incoming connections should match the network address.
  210. ASSERT(socket_->GetLocalAddress().ipaddr() == port->ip());
  211. }
  212. if (socket_) {
  213. socket_->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket);
  214. socket_->SignalClose.connect(this, &TCPConnection::OnClose);
  215. }
  216. }
  217. TCPConnection::~TCPConnection() {
  218. delete socket_;
  219. }
  220. int TCPConnection::Send(const void* data, size_t size) {
  221. if (!socket_) {
  222. error_ = ENOTCONN;
  223. return SOCKET_ERROR;
  224. }
  225. if (write_state() != STATE_WRITABLE) {
  226. // TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error?
  227. error_ = EWOULDBLOCK;
  228. return SOCKET_ERROR;
  229. }
  230. int sent = socket_->Send(data, size);
  231. if (sent < 0) {
  232. error_ = socket_->GetError();
  233. } else {
  234. send_rate_tracker_.Update(sent);
  235. }
  236. return sent;
  237. }
  238. int TCPConnection::GetError() {
  239. return error_;
  240. }
  241. void TCPConnection::OnConnect(talk_base::AsyncPacketSocket* socket) {
  242. ASSERT(socket == socket_);
  243. LOG_J(LS_VERBOSE, this) << "Connection established to "
  244. << socket->GetRemoteAddress().ToString();
  245. set_connected(true);
  246. }
  247. void TCPConnection::OnClose(talk_base::AsyncPacketSocket* socket, int error) {
  248. ASSERT(socket == socket_);
  249. LOG_J(LS_VERBOSE, this) << "Connection closed with error " << error;
  250. set_connected(false);
  251. set_write_state(STATE_WRITE_TIMEOUT);
  252. }
  253. void TCPConnection::OnReadPacket(talk_base::AsyncPacketSocket* socket,
  254. const char* data, size_t size,
  255. const talk_base::SocketAddress& remote_addr) {
  256. ASSERT(socket == socket_);
  257. Connection::OnReadPacket(data, size);
  258. }
  259. } // namespace cricket