henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef P2P_BASE_TCP_PORT_H_ |
| 12 | #define P2P_BASE_TCP_PORT_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include <string> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 17 | |
tzik | f0e926f | 2018-10-15 13:52:10 +0900 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/async_packet_socket.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
| 23 | |
| 24 | class TCPConnection; |
| 25 | |
| 26 | // Communicates using a local TCP port. |
| 27 | // |
| 28 | // This class is designed to allow subclasses to take advantage of the |
| 29 | // connection management provided by this class. A subclass should take of all |
| 30 | // packet sending and preparation, but when a packet is received, it should |
| 31 | // call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection. |
| 32 | class TCPPort : public Port { |
| 33 | public: |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 34 | static std::unique_ptr<TCPPort> Create(rtc::Thread* thread, |
| 35 | rtc::PacketSocketFactory* factory, |
| 36 | rtc::Network* network, |
| 37 | uint16_t min_port, |
| 38 | uint16_t max_port, |
| 39 | const std::string& username, |
| 40 | const std::string& password, |
| 41 | bool allow_listen) { |
| 42 | // Using `new` to access a non-public constructor. |
| 43 | return absl::WrapUnique(new TCPPort(thread, factory, network, min_port, |
| 44 | max_port, username, password, |
| 45 | allow_listen)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | } |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 47 | ~TCPPort() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 48 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 49 | Connection* CreateConnection(const Candidate& address, |
| 50 | CandidateOrigin origin) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 52 | void PrepareAddress() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 54 | int GetOption(rtc::Socket::Option opt, int* value) override; |
| 55 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 56 | int GetError() override; |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 57 | bool SupportsProtocol(const std::string& protocol) const override; |
| 58 | ProtocolType GetProtocol() const override; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 59 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | protected: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 61 | TCPPort(rtc::Thread* thread, |
| 62 | rtc::PacketSocketFactory* factory, |
| 63 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 64 | uint16_t min_port, |
| 65 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 66 | const std::string& username, |
| 67 | const std::string& password, |
| 68 | bool allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | |
| 70 | // Handles sending using the local TCP socket. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 71 | int SendTo(const void* data, |
| 72 | size_t size, |
| 73 | const rtc::SocketAddress& addr, |
| 74 | const rtc::PacketOptions& options, |
| 75 | bool payload) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | |
| 77 | // Accepts incoming TCP connection. |
| 78 | void OnNewConnection(rtc::AsyncPacketSocket* socket, |
| 79 | rtc::AsyncPacketSocket* new_socket); |
| 80 | |
| 81 | private: |
| 82 | struct Incoming { |
| 83 | rtc::SocketAddress addr; |
| 84 | rtc::AsyncPacketSocket* socket; |
| 85 | }; |
| 86 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 87 | void TryCreateServerSocket(); |
| 88 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr, |
| 90 | bool remove = false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | |
| 92 | // Receives packet signal from the local TCP Socket. |
| 93 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | const char* data, |
| 95 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 96 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 97 | const int64_t& packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 98 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 99 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 100 | const rtc::SentPacket& sent_packet) override; |
| 101 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 103 | |
| 104 | void OnAddressReady(rtc::AsyncPacketSocket* socket, |
| 105 | const rtc::SocketAddress& address); |
| 106 | |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 107 | // TODO(?): Is this still needed? |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 108 | bool incoming_only_; |
| 109 | bool allow_listen_; |
| 110 | rtc::AsyncPacketSocket* socket_; |
| 111 | int error_; |
| 112 | std::list<Incoming> incoming_; |
| 113 | |
| 114 | friend class TCPConnection; |
| 115 | }; |
| 116 | |
| 117 | class TCPConnection : public Connection { |
| 118 | public: |
| 119 | // Connection is outgoing unless socket is specified |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | TCPConnection(TCPPort* port, |
| 121 | const Candidate& candidate, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 122 | rtc::AsyncPacketSocket* socket = 0); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 123 | ~TCPConnection() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 125 | int Send(const void* data, |
| 126 | size_t size, |
| 127 | const rtc::PacketOptions& options) override; |
| 128 | int GetError() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 130 | rtc::AsyncPacketSocket* socket() { return socket_.get(); } |
| 131 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 132 | void OnMessage(rtc::Message* pmsg) override; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 133 | |
| 134 | // Allow test cases to overwrite the default timeout period. |
| 135 | int reconnection_timeout() const { return reconnection_timeout_; } |
| 136 | void set_reconnection_timeout(int timeout_in_ms) { |
| 137 | reconnection_timeout_ = timeout_in_ms; |
| 138 | } |
| 139 | |
| 140 | protected: |
| 141 | enum { |
| 142 | MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE, |
| 143 | }; |
| 144 | |
| 145 | // Set waiting_for_stun_binding_complete_ to false to allow data packets in |
| 146 | // addition to what Port::OnConnectionRequestResponse does. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 147 | void OnConnectionRequestResponse(ConnectionRequest* req, |
| 148 | StunMessage* response) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 149 | |
| 150 | private: |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 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 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | void OnConnect(rtc::AsyncPacketSocket* socket); |
| 160 | void OnClose(rtc::AsyncPacketSocket* socket, int error); |
| 161 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 162 | const char* data, |
| 163 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 164 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 165 | const int64_t& packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 166 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 167 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 168 | std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 169 | int error_; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 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_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | |
| 186 | friend class TCPPort; |
| 187 | }; |
| 188 | |
| 189 | } // namespace cricket |
| 190 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 191 | #endif // P2P_BASE_TCP_PORT_H_ |