blob: 67716f73b2baa7ce1aa752c17dd85d44b58b54c5 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/async_packet_socket.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021
22namespace cricket {
23
24class TCPConnection;
25
26// Communicates using a local TCP port.
27//
28// This class is designed to allow subclasses to take advantage of the
29// connection management provided by this class. A subclass should take of all
30// packet sending and preparation, but when a packet is received, it should
31// call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection.
32class TCPPort : public Port {
33 public:
Steve Antona8f1e562018-10-10 11:29:44 -070034 static std::unique_ptr<TCPPort> Create(rtc::Thread* thread,
35 rtc::PacketSocketFactory* factory,
36 rtc::Network* network,
37 uint16_t min_port,
38 uint16_t max_port,
39 const std::string& username,
40 const std::string& password,
41 bool allow_listen) {
42 // Using `new` to access a non-public constructor.
43 return absl::WrapUnique(new TCPPort(thread, factory, network, min_port,
44 max_port, username, password,
45 allow_listen));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010047 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048
Stefan Holmer55674ff2016-01-14 15:49:16 +010049 Connection* CreateConnection(const Candidate& address,
50 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051
Stefan Holmer55674ff2016-01-14 15:49:16 +010052 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053
Stefan Holmer55674ff2016-01-14 15:49:16 +010054 int GetOption(rtc::Socket::Option opt, int* value) override;
55 int SetOption(rtc::Socket::Option opt, int value) override;
56 int GetError() override;
Steve Anton1cf1b7d2017-10-30 10:00:15 -070057 bool SupportsProtocol(const std::string& protocol) const override;
58 ProtocolType GetProtocol() const override;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070059
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000061 TCPPort(rtc::Thread* thread,
62 rtc::PacketSocketFactory* factory,
63 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020064 uint16_t min_port,
65 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000066 const std::string& username,
67 const std::string& password,
68 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069
70 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +010071 int SendTo(const void* data,
72 size_t size,
73 const rtc::SocketAddress& addr,
74 const rtc::PacketOptions& options,
75 bool payload) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076
77 // Accepts incoming TCP connection.
78 void OnNewConnection(rtc::AsyncPacketSocket* socket,
79 rtc::AsyncPacketSocket* new_socket);
80
81 private:
82 struct Incoming {
83 rtc::SocketAddress addr;
84 rtc::AsyncPacketSocket* socket;
85 };
86
deadbeef1ee21252017-06-13 15:49:45 -070087 void TryCreateServerSocket();
88
Yves Gerey665174f2018-06-19 15:03:05 +020089 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
90 bool remove = false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091
92 // Receives packet signal from the local TCP Socket.
93 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +020094 const char* data,
95 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010097 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098
Stefan Holmer55674ff2016-01-14 15:49:16 +010099 void OnSentPacket(rtc::AsyncPacketSocket* socket,
100 const rtc::SentPacket& sent_packet) override;
101
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
103
104 void OnAddressReady(rtc::AsyncPacketSocket* socket,
105 const rtc::SocketAddress& address);
106
Steve Anton6c38cc72017-11-29 10:25:58 -0800107 // TODO(?): Is this still needed?
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 bool incoming_only_;
109 bool allow_listen_;
110 rtc::AsyncPacketSocket* socket_;
111 int error_;
112 std::list<Incoming> incoming_;
113
114 friend class TCPConnection;
115};
116
117class TCPConnection : public Connection {
118 public:
119 // Connection is outgoing unless socket is specified
Yves Gerey665174f2018-06-19 15:03:05 +0200120 TCPConnection(TCPPort* port,
121 const Candidate& candidate,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 rtc::AsyncPacketSocket* socket = 0);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100123 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124
Stefan Holmer55674ff2016-01-14 15:49:16 +0100125 int Send(const void* data,
126 size_t size,
127 const rtc::PacketOptions& options) override;
128 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700130 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
131
Stefan Holmer55674ff2016-01-14 15:49:16 +0100132 void OnMessage(rtc::Message* pmsg) override;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700133
134 // Allow test cases to overwrite the default timeout period.
135 int reconnection_timeout() const { return reconnection_timeout_; }
136 void set_reconnection_timeout(int timeout_in_ms) {
137 reconnection_timeout_ = timeout_in_ms;
138 }
139
140 protected:
141 enum {
142 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
143 };
144
145 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
146 // addition to what Port::OnConnectionRequestResponse does.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100147 void OnConnectionRequestResponse(ConnectionRequest* req,
148 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149
150 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700151 // Helper function to handle the case when Ping or Send fails with error
152 // related to socket close.
153 void MaybeReconnect();
154
155 void CreateOutgoingTcpSocket();
156
157 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
158
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159 void OnConnect(rtc::AsyncPacketSocket* socket);
160 void OnClose(rtc::AsyncPacketSocket* socket, int error);
161 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200162 const char* data,
163 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000164 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100165 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
167
kwiberg3ec46792016-04-27 07:22:53 -0700168 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700170 bool outgoing_;
171
172 // Guard against multiple outgoing tcp connection during a reconnect.
173 bool connection_pending_;
174
175 // Guard against data packets sent when we reconnect a TCP connection. During
176 // reconnecting, when a new tcp connection has being made, we can't send data
177 // packets out until the STUN binding is completed (i.e. the write state is
178 // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC
179 // socket, when receiving data packets before that, will trigger OnError which
180 // will terminate the newly created connection.
181 bool pretending_to_be_writable_;
182
183 // Allow test case to overwrite the default timeout period.
184 int reconnection_timeout_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185
186 friend class TCPPort;
187};
188
189} // namespace cricket
190
Steve Anton10542f22019-01-11 09:11:00 -0800191#endif // P2P_BASE_TCP_PORT_H_