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