blob: ff69e6e48b4e4af7b54c1f67683672587547e204 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 09:11:00 -080011#ifndef P2P_BASE_TCP_PORT_H_
12#define P2P_BASE_TCP_PORT_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <list>
kwiberg3ec46792016-04-27 07:22:53 -070015#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <string>
kwiberg3ec46792016-04-27 07:22:53 -070017
tzikf0e926f2018-10-15 13:52:10 +090018#include "absl/memory/memory.h"
Ali Tofighea5a9442022-06-14 15:20:15 +020019#include "absl/strings/string_view.h"
Artem Titovc374d112022-06-16 21:27:45 +020020#include "api/task_queue/pending_task_safety_flag.h"
Jonas Orelande8e7d7b2019-05-29 09:30:55 +020021#include "p2p/base/connection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/async_packet_socket.h"
Niels Möller646fddc2021-11-02 15:56:05 +010024#include "rtc_base/containers/flat_map.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
26namespace cricket {
27
28class 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.
36class TCPPort : public Port {
37 public:
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020038 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 Tofighde2ac5a2022-06-30 11:58:26 +020044 absl::string_view username,
45 absl::string_view password,
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020046 bool allow_listen,
Jonas Orelande62c2f22022-03-29 11:04:48 +020047 const webrtc::FieldTrialsView* field_trials = nullptr) {
Steve Antona8f1e562018-10-10 11:29:44 -070048 // 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 Orelandc06fe8b2022-03-28 14:58:26 +020051 allow_listen, field_trials));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010053 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054
Stefan Holmer55674ff2016-01-14 15:49:16 +010055 Connection* CreateConnection(const Candidate& address,
56 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057
Stefan Holmer55674ff2016-01-14 15:49:16 +010058 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059
Niels Möller646fddc2021-11-02 15:56:05 +010060 // Options apply to accepted sockets.
61 // TODO(bugs.webrtc.org/13065): Apply also to outgoing and existing
62 // connections.
Stefan Holmer55674ff2016-01-14 15:49:16 +010063 int GetOption(rtc::Socket::Option opt, int* value) override;
64 int SetOption(rtc::Socket::Option opt, int value) override;
65 int GetError() override;
Ali Tofighea5a9442022-06-14 15:20:15 +020066 bool SupportsProtocol(absl::string_view protocol) const override;
Steve Anton1cf1b7d2017-10-30 10:00:15 -070067 ProtocolType GetProtocol() const override;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070068
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000070 TCPPort(rtc::Thread* thread,
71 rtc::PacketSocketFactory* factory,
Niels Möllere0c6bdf2022-03-24 15:18:02 +010072 const rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 uint16_t min_port,
74 uint16_t max_port,
Ali Tofighde2ac5a2022-06-30 11:58:26 +020075 absl::string_view username,
76 absl::string_view password,
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020077 bool allow_listen,
Jonas Orelande62c2f22022-03-29 11:04:48 +020078 const webrtc::FieldTrialsView* field_trials);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079
80 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +010081 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.org269fb4b2014-10-28 22:20:11 +000086
87 // Accepts incoming TCP connection.
Niels Möller6d19d142021-10-06 11:19:03 +020088 void OnNewConnection(rtc::AsyncListenSocket* socket,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089 rtc::AsyncPacketSocket* new_socket);
90
91 private:
92 struct Incoming {
93 rtc::SocketAddress addr;
94 rtc::AsyncPacketSocket* socket;
95 };
96
deadbeef1ee21252017-06-13 15:49:45 -070097 void TryCreateServerSocket();
98
Yves Gerey665174f2018-06-19 15:03:05 +020099 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
100 bool remove = false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101
102 // Receives packet signal from the local TCP Socket.
103 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200104 const char* data,
105 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100107 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108
Stefan Holmer55674ff2016-01-14 15:49:16 +0100109 void OnSentPacket(rtc::AsyncPacketSocket* socket,
110 const rtc::SentPacket& sent_packet) override;
111
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
113
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 bool allow_listen_;
Niels Möller6d19d142021-10-06 11:19:03 +0200115 std::unique_ptr<rtc::AsyncListenSocket> listen_socket_;
Niels Möller646fddc2021-11-02 15:56:05 +0100116 // 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.org269fb4b2014-10-28 22:20:11 +0000122 int error_;
123 std::list<Incoming> incoming_;
124
125 friend class TCPConnection;
126};
127
Tommib44a7942022-04-28 12:31:47 +0200128class TCPConnection : public Connection, public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 public:
130 // Connection is outgoing unless socket is specified
Tommid7e5cfb2022-03-30 20:13:06 +0200131 TCPConnection(rtc::WeakPtr<Port> tcp_port,
Yves Gerey665174f2018-06-19 15:03:05 +0200132 const Candidate& candidate,
Tommid7e5cfb2022-03-30 20:13:06 +0200133 rtc::AsyncPacketSocket* socket = nullptr);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100134 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135
Stefan Holmer55674ff2016-01-14 15:49:16 +0100136 int Send(const void* data,
137 size_t size,
138 const rtc::PacketOptions& options) override;
139 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700141 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
142
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700143 // 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 Shiehbe508a12015-04-06 12:48:47 -0700150 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
151 // addition to what Port::OnConnectionRequestResponse does.
Tommic85e4732022-05-27 16:37:42 +0200152 void OnConnectionRequestResponse(StunRequest* req,
Stefan Holmer55674ff2016-01-14 15:49:16 +0100153 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154
155 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700156 // 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.org269fb4b2014-10-28 22:20:11 +0000164 void OnConnect(rtc::AsyncPacketSocket* socket);
165 void OnClose(rtc::AsyncPacketSocket* socket, int error);
166 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200167 const char* data,
168 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100170 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
172
Tommid7e5cfb2022-03-30 20:13:06 +0200173 TCPPort* tcp_port() {
174 RTC_DCHECK_EQ(port()->GetProtocol(), PROTO_TCP);
175 return static_cast<TCPPort*>(port());
176 }
177
kwiberg3ec46792016-04-27 07:22:53 -0700178 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000179 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700180 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.org269fb4b2014-10-28 22:20:11 +0000195
Mirko Bonadei0cb1cfa2022-02-25 10:45:32 +0000196 webrtc::ScopedTaskSafety network_safety_;
197
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 friend class TCPPort;
199};
200
201} // namespace cricket
202
Steve Anton10542f22019-01-11 09:11:00 -0800203#endif // P2P_BASE_TCP_PORT_H_