1 /* 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3 * 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. 9 */ 10 11 #ifndef P2P_BASE_TCP_PORT_H_ 12 #define P2P_BASE_TCP_PORT_H_ 13 14 #include <list> 15 #include <memory> 16 #include <string> 17 18 #include "absl/memory/memory.h" 19 #include "p2p/base/connection.h" 20 #include "p2p/base/port.h" 21 #include "rtc_base/async_packet_socket.h" 22 23 namespace cricket { 24 25 class TCPConnection; 26 27 // Communicates using a local TCP port. 28 // 29 // This class is designed to allow subclasses to take advantage of the 30 // connection management provided by this class. A subclass should take of all 31 // packet sending and preparation, but when a packet is received, it should 32 // call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection. 33 class TCPPort : public Port { 34 public: Create(rtc::Thread * thread,rtc::PacketSocketFactory * factory,rtc::Network * network,uint16_t min_port,uint16_t max_port,const std::string & username,const std::string & password,bool allow_listen)35 static std::unique_ptr<TCPPort> Create(rtc::Thread* thread, 36 rtc::PacketSocketFactory* factory, 37 rtc::Network* network, 38 uint16_t min_port, 39 uint16_t max_port, 40 const std::string& username, 41 const std::string& password, 42 bool allow_listen) { 43 // Using `new` to access a non-public constructor. 44 return absl::WrapUnique(new TCPPort(thread, factory, network, min_port, 45 max_port, username, password, 46 allow_listen)); 47 } 48 ~TCPPort() override; 49 50 Connection* CreateConnection(const Candidate& address, 51 CandidateOrigin origin) override; 52 53 void PrepareAddress() override; 54 55 int GetOption(rtc::Socket::Option opt, int* value) override; 56 int SetOption(rtc::Socket::Option opt, int value) override; 57 int GetError() override; 58 bool SupportsProtocol(const std::string& protocol) const override; 59 ProtocolType GetProtocol() const override; 60 61 protected: 62 TCPPort(rtc::Thread* thread, 63 rtc::PacketSocketFactory* factory, 64 rtc::Network* network, 65 uint16_t min_port, 66 uint16_t max_port, 67 const std::string& username, 68 const std::string& password, 69 bool allow_listen); 70 71 // Handles sending using the local TCP socket. 72 int SendTo(const void* data, 73 size_t size, 74 const rtc::SocketAddress& addr, 75 const rtc::PacketOptions& options, 76 bool payload) override; 77 78 // Accepts incoming TCP connection. 79 void OnNewConnection(rtc::AsyncPacketSocket* socket, 80 rtc::AsyncPacketSocket* new_socket); 81 82 private: 83 struct Incoming { 84 rtc::SocketAddress addr; 85 rtc::AsyncPacketSocket* socket; 86 }; 87 88 void TryCreateServerSocket(); 89 90 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr, 91 bool remove = false); 92 93 // Receives packet signal from the local TCP Socket. 94 void OnReadPacket(rtc::AsyncPacketSocket* socket, 95 const char* data, 96 size_t size, 97 const rtc::SocketAddress& remote_addr, 98 const int64_t& packet_time_us); 99 100 void OnSentPacket(rtc::AsyncPacketSocket* socket, 101 const rtc::SentPacket& sent_packet) override; 102 103 void OnReadyToSend(rtc::AsyncPacketSocket* socket); 104 105 void OnAddressReady(rtc::AsyncPacketSocket* socket, 106 const rtc::SocketAddress& address); 107 108 bool allow_listen_; 109 rtc::AsyncPacketSocket* socket_; 110 int error_; 111 std::list<Incoming> incoming_; 112 113 friend class TCPConnection; 114 }; 115 116 class TCPConnection : public Connection { 117 public: 118 // Connection is outgoing unless socket is specified 119 TCPConnection(TCPPort* port, 120 const Candidate& candidate, 121 rtc::AsyncPacketSocket* socket = 0); 122 ~TCPConnection() override; 123 124 int Send(const void* data, 125 size_t size, 126 const rtc::PacketOptions& options) override; 127 int GetError() override; 128 socket()129 rtc::AsyncPacketSocket* socket() { return socket_.get(); } 130 131 void OnMessage(rtc::Message* pmsg) override; 132 133 // Allow test cases to overwrite the default timeout period. reconnection_timeout()134 int reconnection_timeout() const { return reconnection_timeout_; } set_reconnection_timeout(int timeout_in_ms)135 void set_reconnection_timeout(int timeout_in_ms) { 136 reconnection_timeout_ = timeout_in_ms; 137 } 138 139 protected: 140 enum { 141 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE, 142 MSG_TCPCONNECTION_FAILED_CREATE_SOCKET, 143 }; 144 145 // Set waiting_for_stun_binding_complete_ to false to allow data packets in 146 // addition to what Port::OnConnectionRequestResponse does. 147 void OnConnectionRequestResponse(ConnectionRequest* req, 148 StunMessage* response) override; 149 150 private: 151 // Helper function to handle the case when Ping or Send fails with error 152 // related to socket close. 153 void MaybeReconnect(); 154 155 void CreateOutgoingTcpSocket(); 156 157 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket); 158 159 void OnConnect(rtc::AsyncPacketSocket* socket); 160 void OnClose(rtc::AsyncPacketSocket* socket, int error); 161 void OnReadPacket(rtc::AsyncPacketSocket* socket, 162 const char* data, 163 size_t size, 164 const rtc::SocketAddress& remote_addr, 165 const int64_t& packet_time_us); 166 void OnReadyToSend(rtc::AsyncPacketSocket* socket); 167 168 std::unique_ptr<rtc::AsyncPacketSocket> socket_; 169 int error_; 170 bool outgoing_; 171 172 // Guard against multiple outgoing tcp connection during a reconnect. 173 bool connection_pending_; 174 175 // Guard against data packets sent when we reconnect a TCP connection. During 176 // reconnecting, when a new tcp connection has being made, we can't send data 177 // packets out until the STUN binding is completed (i.e. the write state is 178 // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC 179 // socket, when receiving data packets before that, will trigger OnError which 180 // will terminate the newly created connection. 181 bool pretending_to_be_writable_; 182 183 // Allow test case to overwrite the default timeout period. 184 int reconnection_timeout_; 185 186 friend class TCPPort; 187 }; 188 189 } // namespace cricket 190 191 #endif // P2P_BASE_TCP_PORT_H_ 192