henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [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 17:11:00 | [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 | [diff] [blame] | 13 | |
| 14 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 16 | #include <string> |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 17 | |
tzik | f0e926f | 2018-10-15 04:52:10 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Ali Tofigh | ea5a944 | 2022-06-14 13:20:15 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Artem Titov | c374d11 | 2022-06-16 19:27:45 | [diff] [blame] | 20 | #include "api/task_queue/pending_task_safety_flag.h" |
Jonas Oreland | e8e7d7b | 2019-05-29 07:30:55 | [diff] [blame] | 21 | #include "p2p/base/connection.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "rtc_base/async_packet_socket.h" |
Niels Möller | 646fddc | 2021-11-02 14:56:05 | [diff] [blame] | 24 | #include "rtc_base/containers/flat_map.h" |
Per K | c09cc4f | 2023-11-20 11:21:34 | [diff] [blame] | 25 | #include "rtc_base/network/received_packet.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 26 | |
| 27 | namespace cricket { |
| 28 | |
| 29 | class TCPConnection; |
| 30 | |
| 31 | // Communicates using a local TCP port. |
| 32 | // |
| 33 | // This class is designed to allow subclasses to take advantage of the |
| 34 | // connection management provided by this class. A subclass should take of all |
| 35 | // packet sending and preparation, but when a packet is received, it should |
| 36 | // call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection. |
| 37 | class TCPPort : public Port { |
| 38 | public: |
Jonas Oreland | c06fe8b | 2022-03-28 12:58:26 | [diff] [blame] | 39 | static std::unique_ptr<TCPPort> Create( |
| 40 | rtc::Thread* thread, |
| 41 | rtc::PacketSocketFactory* factory, |
| 42 | const rtc::Network* network, |
| 43 | uint16_t min_port, |
| 44 | uint16_t max_port, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 45 | absl::string_view username, |
| 46 | absl::string_view password, |
Jonas Oreland | c06fe8b | 2022-03-28 12:58:26 | [diff] [blame] | 47 | bool allow_listen, |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 48 | const webrtc::FieldTrialsView* field_trials = nullptr) { |
Steve Anton | a8f1e56 | 2018-10-10 18:29:44 | [diff] [blame] | 49 | // Using `new` to access a non-public constructor. |
| 50 | return absl::WrapUnique(new TCPPort(thread, factory, network, min_port, |
| 51 | max_port, username, password, |
Jonas Oreland | c06fe8b | 2022-03-28 12:58:26 | [diff] [blame] | 52 | allow_listen, field_trials)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 53 | } |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 54 | ~TCPPort() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 55 | |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 56 | Connection* CreateConnection(const Candidate& address, |
| 57 | CandidateOrigin origin) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 58 | |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 59 | void PrepareAddress() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 60 | |
Niels Möller | 646fddc | 2021-11-02 14:56:05 | [diff] [blame] | 61 | // Options apply to accepted sockets. |
| 62 | // TODO(bugs.webrtc.org/13065): Apply also to outgoing and existing |
| 63 | // connections. |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 64 | int GetOption(rtc::Socket::Option opt, int* value) override; |
| 65 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 66 | int GetError() override; |
Ali Tofigh | ea5a944 | 2022-06-14 13:20:15 | [diff] [blame] | 67 | bool SupportsProtocol(absl::string_view protocol) const override; |
Steve Anton | 1cf1b7d | 2017-10-30 17:00:15 | [diff] [blame] | 68 | ProtocolType GetProtocol() const override; |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 69 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 70 | protected: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 | [diff] [blame] | 71 | TCPPort(rtc::Thread* thread, |
| 72 | rtc::PacketSocketFactory* factory, |
Niels Möller | e0c6bdf | 2022-03-24 14:18:02 | [diff] [blame] | 73 | const rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 74 | uint16_t min_port, |
| 75 | uint16_t max_port, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 76 | absl::string_view username, |
| 77 | absl::string_view password, |
Jonas Oreland | c06fe8b | 2022-03-28 12:58:26 | [diff] [blame] | 78 | bool allow_listen, |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 79 | const webrtc::FieldTrialsView* field_trials); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 80 | |
| 81 | // Handles sending using the local TCP socket. |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 82 | int SendTo(const void* data, |
| 83 | size_t size, |
| 84 | const rtc::SocketAddress& addr, |
| 85 | const rtc::PacketOptions& options, |
| 86 | bool payload) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 87 | |
| 88 | // Accepts incoming TCP connection. |
Niels Möller | 6d19d14 | 2021-10-06 09:19:03 | [diff] [blame] | 89 | void OnNewConnection(rtc::AsyncListenSocket* socket, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 90 | rtc::AsyncPacketSocket* new_socket); |
| 91 | |
| 92 | private: |
| 93 | struct Incoming { |
| 94 | rtc::SocketAddress addr; |
| 95 | rtc::AsyncPacketSocket* socket; |
| 96 | }; |
| 97 | |
deadbeef | 1ee2125 | 2017-06-13 22:49:45 | [diff] [blame] | 98 | void TryCreateServerSocket(); |
| 99 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 100 | rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr, |
| 101 | bool remove = false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 102 | |
| 103 | // Receives packet signal from the local TCP Socket. |
| 104 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
Per K | c09cc4f | 2023-11-20 11:21:34 | [diff] [blame] | 105 | const rtc::ReceivedPacket& packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 106 | |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 107 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 108 | const rtc::SentPacket& sent_packet) override; |
| 109 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 110 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 111 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 112 | bool allow_listen_; |
Niels Möller | 6d19d14 | 2021-10-06 09:19:03 | [diff] [blame] | 113 | std::unique_ptr<rtc::AsyncListenSocket> listen_socket_; |
Niels Möller | 646fddc | 2021-11-02 14:56:05 | [diff] [blame] | 114 | // Options to be applied to accepted sockets. |
| 115 | // TODO(bugs.webrtc:13065): Configure connect/accept in the same way, but |
| 116 | // currently, setting OPT_NODELAY for client sockets is done (unconditionally) |
| 117 | // by BasicPacketSocketFactory::CreateClientTcpSocket. |
| 118 | webrtc::flat_map<rtc::Socket::Option, int> socket_options_; |
| 119 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 120 | int error_; |
| 121 | std::list<Incoming> incoming_; |
| 122 | |
| 123 | friend class TCPConnection; |
| 124 | }; |
| 125 | |
Tommi | b44a794 | 2022-04-28 10:31:47 | [diff] [blame] | 126 | class TCPConnection : public Connection, public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 127 | public: |
| 128 | // Connection is outgoing unless socket is specified |
Tommi | d7e5cfb | 2022-03-30 18:13:06 | [diff] [blame] | 129 | TCPConnection(rtc::WeakPtr<Port> tcp_port, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 130 | const Candidate& candidate, |
Tommi | d7e5cfb | 2022-03-30 18:13:06 | [diff] [blame] | 131 | rtc::AsyncPacketSocket* socket = nullptr); |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 132 | ~TCPConnection() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 133 | |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 134 | int Send(const void* data, |
| 135 | size_t size, |
| 136 | const rtc::PacketOptions& options) override; |
| 137 | int GetError() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 138 | |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 139 | rtc::AsyncPacketSocket* socket() { return socket_.get(); } |
| 140 | |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 141 | // Allow test cases to overwrite the default timeout period. |
| 142 | int reconnection_timeout() const { return reconnection_timeout_; } |
| 143 | void set_reconnection_timeout(int timeout_in_ms) { |
| 144 | reconnection_timeout_ = timeout_in_ms; |
| 145 | } |
| 146 | |
| 147 | protected: |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 148 | // Set waiting_for_stun_binding_complete_ to false to allow data packets in |
| 149 | // addition to what Port::OnConnectionRequestResponse does. |
Tommi | c85e473 | 2022-05-27 14:37:42 | [diff] [blame] | 150 | void OnConnectionRequestResponse(StunRequest* req, |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 151 | StunMessage* response) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 152 | |
| 153 | private: |
Tommi | cbaf91b | 2023-09-04 17:34:10 | [diff] [blame] | 154 | friend class TCPPort; // For `MaybeReconnect()`. |
| 155 | |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 156 | // Helper function to handle the case when Ping or Send fails with error |
| 157 | // related to socket close. |
| 158 | void MaybeReconnect(); |
| 159 | |
Tommi | cbaf91b | 2023-09-04 17:34:10 | [diff] [blame] | 160 | void CreateOutgoingTcpSocket() RTC_RUN_ON(network_thread()); |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 161 | |
Tommi | cbaf91b | 2023-09-04 17:34:10 | [diff] [blame] | 162 | void ConnectSocketSignals(rtc::AsyncPacketSocket* socket) |
| 163 | RTC_RUN_ON(network_thread()); |
| 164 | |
| 165 | void DisconnectSocketSignals(rtc::AsyncPacketSocket* socket) |
| 166 | RTC_RUN_ON(network_thread()); |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 167 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 168 | void OnConnect(rtc::AsyncPacketSocket* socket); |
| 169 | void OnClose(rtc::AsyncPacketSocket* socket, int error); |
| 170 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
Per K | c09cc4f | 2023-11-20 11:21:34 | [diff] [blame] | 171 | const rtc::ReceivedPacket& packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 172 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
Tommi | cbaf91b | 2023-09-04 17:34:10 | [diff] [blame] | 173 | void OnDestroyed(Connection* c); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 174 | |
Tommi | d7e5cfb | 2022-03-30 18:13:06 | [diff] [blame] | 175 | TCPPort* tcp_port() { |
| 176 | RTC_DCHECK_EQ(port()->GetProtocol(), PROTO_TCP); |
| 177 | return static_cast<TCPPort*>(port()); |
| 178 | } |
| 179 | |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 180 | std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 181 | int error_; |
Tommi | cbaf91b | 2023-09-04 17:34:10 | [diff] [blame] | 182 | const bool outgoing_; |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 183 | |
| 184 | // Guard against multiple outgoing tcp connection during a reconnect. |
| 185 | bool connection_pending_; |
| 186 | |
| 187 | // Guard against data packets sent when we reconnect a TCP connection. During |
| 188 | // reconnecting, when a new tcp connection has being made, we can't send data |
| 189 | // packets out until the STUN binding is completed (i.e. the write state is |
| 190 | // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC |
| 191 | // socket, when receiving data packets before that, will trigger OnError which |
| 192 | // will terminate the newly created connection. |
| 193 | bool pretending_to_be_writable_; |
| 194 | |
| 195 | // Allow test case to overwrite the default timeout period. |
| 196 | int reconnection_timeout_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 197 | |
Mirko Bonadei | 0cb1cfa | 2022-02-25 10:45:32 | [diff] [blame] | 198 | webrtc::ScopedTaskSafety network_safety_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // namespace cricket |
| 202 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 203 | #endif // P2P_BASE_TCP_PORT_H_ |