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