blob: 0635ed950e0bbd42e30d77f4dbf5e1473fc4339c [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"
Jonas Orelande8e7d7b2019-05-29 09:30:55 +020019#include "p2p/base/connection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/async_packet_socket.h"
Niels Möller646fddc2021-11-02 15:56:05 +010022#include "rtc_base/containers/flat_map.h"
Mirko Bonadei0cb1cfa2022-02-25 10:45:32 +000023#include "rtc_base/task_utils/pending_task_safety_flag.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
27class 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.
35class TCPPort : public Port {
36 public:
Steve Antona8f1e562018-10-10 11:29:44 -070037 static std::unique_ptr<TCPPort> Create(rtc::Thread* thread,
38 rtc::PacketSocketFactory* factory,
Niels Möllere0c6bdf2022-03-24 15:18:02 +010039 const rtc::Network* network,
Steve Antona8f1e562018-10-10 11:29:44 -070040 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.org269fb4b2014-10-28 22:20:11 +000049 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010050 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051
Stefan Holmer55674ff2016-01-14 15:49:16 +010052 Connection* CreateConnection(const Candidate& address,
53 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054
Stefan Holmer55674ff2016-01-14 15:49:16 +010055 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000056
Niels Möller646fddc2021-11-02 15:56:05 +010057 // Options apply to accepted sockets.
58 // TODO(bugs.webrtc.org/13065): Apply also to outgoing and existing
59 // connections.
Stefan Holmer55674ff2016-01-14 15:49:16 +010060 int GetOption(rtc::Socket::Option opt, int* value) override;
61 int SetOption(rtc::Socket::Option opt, int value) override;
62 int GetError() override;
Steve Anton1cf1b7d2017-10-30 10:00:15 -070063 bool SupportsProtocol(const std::string& protocol) const override;
64 ProtocolType GetProtocol() const override;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070065
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000067 TCPPort(rtc::Thread* thread,
68 rtc::PacketSocketFactory* factory,
Niels Möllere0c6bdf2022-03-24 15:18:02 +010069 const rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020070 uint16_t min_port,
71 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000072 const std::string& username,
73 const std::string& password,
74 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075
76 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +010077 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.org269fb4b2014-10-28 22:20:11 +000082
83 // Accepts incoming TCP connection.
Niels Möller6d19d142021-10-06 11:19:03 +020084 void OnNewConnection(rtc::AsyncListenSocket* socket,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085 rtc::AsyncPacketSocket* new_socket);
86
87 private:
88 struct Incoming {
89 rtc::SocketAddress addr;
90 rtc::AsyncPacketSocket* socket;
91 };
92
deadbeef1ee21252017-06-13 15:49:45 -070093 void TryCreateServerSocket();
94
Yves Gerey665174f2018-06-19 15:03:05 +020095 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
96 bool remove = false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097
98 // Receives packet signal from the local TCP Socket.
99 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200100 const char* data,
101 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100103 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104
Stefan Holmer55674ff2016-01-14 15:49:16 +0100105 void OnSentPacket(rtc::AsyncPacketSocket* socket,
106 const rtc::SentPacket& sent_packet) override;
107
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
109
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110 bool allow_listen_;
Niels Möller6d19d142021-10-06 11:19:03 +0200111 std::unique_ptr<rtc::AsyncListenSocket> listen_socket_;
Niels Möller646fddc2021-11-02 15:56:05 +0100112 // 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.org269fb4b2014-10-28 22:20:11 +0000118 int error_;
119 std::list<Incoming> incoming_;
120
121 friend class TCPConnection;
122};
123
124class TCPConnection : public Connection {
125 public:
126 // Connection is outgoing unless socket is specified
Yves Gerey665174f2018-06-19 15:03:05 +0200127 TCPConnection(TCPPort* port,
128 const Candidate& candidate,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 rtc::AsyncPacketSocket* socket = 0);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100130 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131
Stefan Holmer55674ff2016-01-14 15:49:16 +0100132 int Send(const void* data,
133 size_t size,
134 const rtc::PacketOptions& options) override;
135 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700137 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
138
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700139 // 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 Shiehbe508a12015-04-06 12:48:47 -0700146 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
147 // addition to what Port::OnConnectionRequestResponse does.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100148 void OnConnectionRequestResponse(ConnectionRequest* req,
149 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150
151 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700152 // 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.org269fb4b2014-10-28 22:20:11 +0000160 void OnConnect(rtc::AsyncPacketSocket* socket);
161 void OnClose(rtc::AsyncPacketSocket* socket, int error);
162 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200163 const char* data,
164 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100166 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
168
kwiberg3ec46792016-04-27 07:22:53 -0700169 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700171 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.org269fb4b2014-10-28 22:20:11 +0000186
Mirko Bonadei0cb1cfa2022-02-25 10:45:32 +0000187 webrtc::ScopedTaskSafety network_safety_;
188
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 friend class TCPPort;
190};
191
192} // namespace cricket
193
Steve Anton10542f22019-01-11 09:11:00 -0800194#endif // P2P_BASE_TCP_PORT_H_