blob: bd7ed4c1102aea2db303797b20f4e8182adeb938 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
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 Anton10542f22019-01-11 17:11:0011#ifndef P2P_BASE_TCP_PORT_H_
12#define P2P_BASE_TCP_PORT_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:1113
14#include <list>
kwiberg3ec46792016-04-27 14:22:5315#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:1116#include <string>
kwiberg3ec46792016-04-27 14:22:5317
tzikf0e926f2018-10-15 04:52:1018#include "absl/memory/memory.h"
Ali Tofighea5a9442022-06-14 13:20:1519#include "absl/strings/string_view.h"
Artem Titovc374d112022-06-16 19:27:4520#include "api/task_queue/pending_task_safety_flag.h"
Jonas Orelande8e7d7b2019-05-29 07:30:5521#include "p2p/base/connection.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/async_packet_socket.h"
Niels Möller646fddc2021-11-02 14:56:0524#include "rtc_base/containers/flat_map.h"
Per Kc09cc4f2023-11-20 11:21:3425#include "rtc_base/network/received_packet.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1126
27namespace cricket {
28
29class 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.
37class TCPPort : public Port {
38 public:
Jonas Orelandc06fe8b2022-03-28 12:58:2639 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 Tofighde2ac5a2022-06-30 09:58:2645 absl::string_view username,
46 absl::string_view password,
Jonas Orelandc06fe8b2022-03-28 12:58:2647 bool allow_listen,
Jonas Orelande62c2f22022-03-29 09:04:4848 const webrtc::FieldTrialsView* field_trials = nullptr) {
Steve Antona8f1e562018-10-10 18:29:4449 // 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 Orelandc06fe8b2022-03-28 12:58:2652 allow_listen, field_trials));
henrike@webrtc.org269fb4b2014-10-28 22:20:1153 }
Stefan Holmer55674ff2016-01-14 14:49:1654 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:1155
Stefan Holmer55674ff2016-01-14 14:49:1656 Connection* CreateConnection(const Candidate& address,
57 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:1158
Stefan Holmer55674ff2016-01-14 14:49:1659 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:1160
Niels Möller646fddc2021-11-02 14:56:0561 // Options apply to accepted sockets.
62 // TODO(bugs.webrtc.org/13065): Apply also to outgoing and existing
63 // connections.
Stefan Holmer55674ff2016-01-14 14:49:1664 int GetOption(rtc::Socket::Option opt, int* value) override;
65 int SetOption(rtc::Socket::Option opt, int value) override;
66 int GetError() override;
Ali Tofighea5a9442022-06-14 13:20:1567 bool SupportsProtocol(absl::string_view protocol) const override;
Steve Anton1cf1b7d2017-10-30 17:00:1568 ProtocolType GetProtocol() const override;
Honghai Zhangb9e7b4a2016-07-01 03:52:0269
henrike@webrtc.org269fb4b2014-10-28 22:20:1170 protected:
pkasting@chromium.org332331f2014-11-06 20:19:2271 TCPPort(rtc::Thread* thread,
72 rtc::PacketSocketFactory* factory,
Niels Möllere0c6bdf2022-03-24 14:18:0273 const rtc::Network* network,
Peter Boström0c4e06b2015-10-07 10:23:2174 uint16_t min_port,
75 uint16_t max_port,
Ali Tofighde2ac5a2022-06-30 09:58:2676 absl::string_view username,
77 absl::string_view password,
Jonas Orelandc06fe8b2022-03-28 12:58:2678 bool allow_listen,
Jonas Orelande62c2f22022-03-29 09:04:4879 const webrtc::FieldTrialsView* field_trials);
henrike@webrtc.org269fb4b2014-10-28 22:20:1180
81 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 14:49:1682 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.org269fb4b2014-10-28 22:20:1187
88 // Accepts incoming TCP connection.
Niels Möller6d19d142021-10-06 09:19:0389 void OnNewConnection(rtc::AsyncListenSocket* socket,
henrike@webrtc.org269fb4b2014-10-28 22:20:1190 rtc::AsyncPacketSocket* new_socket);
91
92 private:
93 struct Incoming {
94 rtc::SocketAddress addr;
95 rtc::AsyncPacketSocket* socket;
96 };
97
deadbeef1ee21252017-06-13 22:49:4598 void TryCreateServerSocket();
99
Yves Gerey665174f2018-06-19 13:03:05100 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
101 bool remove = false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11102
103 // Receives packet signal from the local TCP Socket.
104 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Per Kc09cc4f2023-11-20 11:21:34105 const rtc::ReceivedPacket& packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11106
Stefan Holmer55674ff2016-01-14 14:49:16107 void OnSentPacket(rtc::AsyncPacketSocket* socket,
108 const rtc::SentPacket& sent_packet) override;
109
henrike@webrtc.org269fb4b2014-10-28 22:20:11110 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
111
henrike@webrtc.org269fb4b2014-10-28 22:20:11112 bool allow_listen_;
Niels Möller6d19d142021-10-06 09:19:03113 std::unique_ptr<rtc::AsyncListenSocket> listen_socket_;
Niels Möller646fddc2021-11-02 14:56:05114 // 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.org269fb4b2014-10-28 22:20:11120 int error_;
121 std::list<Incoming> incoming_;
122
123 friend class TCPConnection;
124};
125
Tommib44a7942022-04-28 10:31:47126class TCPConnection : public Connection, public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11127 public:
128 // Connection is outgoing unless socket is specified
Tommid7e5cfb2022-03-30 18:13:06129 TCPConnection(rtc::WeakPtr<Port> tcp_port,
Yves Gerey665174f2018-06-19 13:03:05130 const Candidate& candidate,
Tommid7e5cfb2022-03-30 18:13:06131 rtc::AsyncPacketSocket* socket = nullptr);
Stefan Holmer55674ff2016-01-14 14:49:16132 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11133
Stefan Holmer55674ff2016-01-14 14:49:16134 int Send(const void* data,
135 size_t size,
136 const rtc::PacketOptions& options) override;
137 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11138
Guo-wei Shiehbe508a12015-04-06 19:48:47139 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
140
Guo-wei Shiehbe508a12015-04-06 19:48:47141 // 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 Shiehbe508a12015-04-06 19:48:47148 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
149 // addition to what Port::OnConnectionRequestResponse does.
Tommic85e4732022-05-27 14:37:42150 void OnConnectionRequestResponse(StunRequest* req,
Stefan Holmer55674ff2016-01-14 14:49:16151 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11152
153 private:
Tommicbaf91b2023-09-04 17:34:10154 friend class TCPPort; // For `MaybeReconnect()`.
155
Guo-wei Shiehbe508a12015-04-06 19:48:47156 // Helper function to handle the case when Ping or Send fails with error
157 // related to socket close.
158 void MaybeReconnect();
159
Tommicbaf91b2023-09-04 17:34:10160 void CreateOutgoingTcpSocket() RTC_RUN_ON(network_thread());
Guo-wei Shiehbe508a12015-04-06 19:48:47161
Tommicbaf91b2023-09-04 17:34:10162 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 Shiehbe508a12015-04-06 19:48:47167
henrike@webrtc.org269fb4b2014-10-28 22:20:11168 void OnConnect(rtc::AsyncPacketSocket* socket);
169 void OnClose(rtc::AsyncPacketSocket* socket, int error);
170 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Per Kc09cc4f2023-11-20 11:21:34171 const rtc::ReceivedPacket& packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11172 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
Tommicbaf91b2023-09-04 17:34:10173 void OnDestroyed(Connection* c);
henrike@webrtc.org269fb4b2014-10-28 22:20:11174
Tommid7e5cfb2022-03-30 18:13:06175 TCPPort* tcp_port() {
176 RTC_DCHECK_EQ(port()->GetProtocol(), PROTO_TCP);
177 return static_cast<TCPPort*>(port());
178 }
179
kwiberg3ec46792016-04-27 14:22:53180 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11181 int error_;
Tommicbaf91b2023-09-04 17:34:10182 const bool outgoing_;
Guo-wei Shiehbe508a12015-04-06 19:48:47183
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.org269fb4b2014-10-28 22:20:11197
Mirko Bonadei0cb1cfa2022-02-25 10:45:32198 webrtc::ScopedTaskSafety network_safety_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11199};
200
201} // namespace cricket
202
Steve Anton10542f22019-01-11 17:11:00203#endif // P2P_BASE_TCP_PORT_H_