blob: 6e7d00feb24588bff6391c25fd409a0c4058aef1 [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"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022
23namespace cricket {
24
25class TCPConnection;
26
27// Communicates using a local TCP port.
28//
29// This class is designed to allow subclasses to take advantage of the
30// connection management provided by this class. A subclass should take of all
31// packet sending and preparation, but when a packet is received, it should
32// call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection.
33class TCPPort : public Port {
34 public:
Steve Antona8f1e562018-10-10 11:29:44 -070035 static std::unique_ptr<TCPPort> Create(rtc::Thread* thread,
36 rtc::PacketSocketFactory* factory,
37 rtc::Network* network,
38 uint16_t min_port,
39 uint16_t max_port,
40 const std::string& username,
41 const std::string& password,
42 bool allow_listen) {
43 // Using `new` to access a non-public constructor.
44 return absl::WrapUnique(new TCPPort(thread, factory, network, min_port,
45 max_port, username, password,
46 allow_listen));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010048 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049
Stefan Holmer55674ff2016-01-14 15:49:16 +010050 Connection* CreateConnection(const Candidate& address,
51 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052
Stefan Holmer55674ff2016-01-14 15:49:16 +010053 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054
Stefan Holmer55674ff2016-01-14 15:49:16 +010055 int GetOption(rtc::Socket::Option opt, int* value) override;
56 int SetOption(rtc::Socket::Option opt, int value) override;
57 int GetError() override;
Steve Anton1cf1b7d2017-10-30 10:00:15 -070058 bool SupportsProtocol(const std::string& protocol) const override;
59 ProtocolType GetProtocol() const override;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070060
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000062 TCPPort(rtc::Thread* thread,
63 rtc::PacketSocketFactory* factory,
64 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020065 uint16_t min_port,
66 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000067 const std::string& username,
68 const std::string& password,
69 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070
71 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +010072 int SendTo(const void* data,
73 size_t size,
74 const rtc::SocketAddress& addr,
75 const rtc::PacketOptions& options,
76 bool payload) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077
78 // Accepts incoming TCP connection.
79 void OnNewConnection(rtc::AsyncPacketSocket* socket,
80 rtc::AsyncPacketSocket* new_socket);
81
82 private:
83 struct Incoming {
84 rtc::SocketAddress addr;
85 rtc::AsyncPacketSocket* socket;
86 };
87
deadbeef1ee21252017-06-13 15:49:45 -070088 void TryCreateServerSocket();
89
Yves Gerey665174f2018-06-19 15:03:05 +020090 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
91 bool remove = false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092
93 // Receives packet signal from the local TCP Socket.
94 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +020095 const char* data,
96 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010098 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099
Stefan Holmer55674ff2016-01-14 15:49:16 +0100100 void OnSentPacket(rtc::AsyncPacketSocket* socket,
101 const rtc::SentPacket& sent_packet) override;
102
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
104
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105 bool allow_listen_;
106 rtc::AsyncPacketSocket* socket_;
107 int error_;
108 std::list<Incoming> incoming_;
109
110 friend class TCPConnection;
111};
112
113class TCPConnection : public Connection {
114 public:
115 // Connection is outgoing unless socket is specified
Yves Gerey665174f2018-06-19 15:03:05 +0200116 TCPConnection(TCPPort* port,
117 const Candidate& candidate,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118 rtc::AsyncPacketSocket* socket = 0);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100119 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120
Stefan Holmer55674ff2016-01-14 15:49:16 +0100121 int Send(const void* data,
122 size_t size,
123 const rtc::PacketOptions& options) override;
124 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700126 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
127
Stefan Holmer55674ff2016-01-14 15:49:16 +0100128 void OnMessage(rtc::Message* pmsg) override;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700129
130 // Allow test cases to overwrite the default timeout period.
131 int reconnection_timeout() const { return reconnection_timeout_; }
132 void set_reconnection_timeout(int timeout_in_ms) {
133 reconnection_timeout_ = timeout_in_ms;
134 }
135
136 protected:
137 enum {
138 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
Jonas Oreland7a284e12020-01-28 09:21:54 +0100139 MSG_TCPCONNECTION_FAILED_CREATE_SOCKET,
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700140 };
141
142 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
143 // addition to what Port::OnConnectionRequestResponse does.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100144 void OnConnectionRequestResponse(ConnectionRequest* req,
145 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146
147 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700148 // Helper function to handle the case when Ping or Send fails with error
149 // related to socket close.
150 void MaybeReconnect();
151
152 void CreateOutgoingTcpSocket();
153
154 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
155
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000156 void OnConnect(rtc::AsyncPacketSocket* socket);
157 void OnClose(rtc::AsyncPacketSocket* socket, int error);
158 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200159 const char* data,
160 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100162 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
164
kwiberg3ec46792016-04-27 07:22:53 -0700165 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700167 bool outgoing_;
168
169 // Guard against multiple outgoing tcp connection during a reconnect.
170 bool connection_pending_;
171
172 // Guard against data packets sent when we reconnect a TCP connection. During
173 // reconnecting, when a new tcp connection has being made, we can't send data
174 // packets out until the STUN binding is completed (i.e. the write state is
175 // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC
176 // socket, when receiving data packets before that, will trigger OnError which
177 // will terminate the newly created connection.
178 bool pretending_to_be_writable_;
179
180 // Allow test case to overwrite the default timeout period.
181 int reconnection_timeout_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000182
183 friend class TCPPort;
184};
185
186} // namespace cricket
187
Steve Anton10542f22019-01-11 09:11:00 -0800188#endif // P2P_BASE_TCP_PORT_H_