blob: 6a71d083abf13e935135e06ea59eb0b0158799ca [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_TCPPORT_H_
12#define P2P_BASE_TCPPORT_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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/port.h"
19#include "rtc_base/asyncpacketsocket.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020
21namespace cricket {
22
23class TCPConnection;
24
25// Communicates using a local TCP port.
26//
27// This class is designed to allow subclasses to take advantage of the
28// connection management provided by this class. A subclass should take of all
29// packet sending and preparation, but when a packet is received, it should
30// call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection.
31class TCPPort : public Port {
32 public:
Steve Antona8f1e562018-10-10 11:29:44 -070033 static std::unique_ptr<TCPPort> Create(rtc::Thread* thread,
34 rtc::PacketSocketFactory* factory,
35 rtc::Network* network,
36 uint16_t min_port,
37 uint16_t max_port,
38 const std::string& username,
39 const std::string& password,
40 bool allow_listen) {
41 // Using `new` to access a non-public constructor.
42 return absl::WrapUnique(new TCPPort(thread, factory, network, min_port,
43 max_port, username, password,
44 allow_listen));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010046 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047
Stefan Holmer55674ff2016-01-14 15:49:16 +010048 Connection* CreateConnection(const Candidate& address,
49 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050
Stefan Holmer55674ff2016-01-14 15:49:16 +010051 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052
Stefan Holmer55674ff2016-01-14 15:49:16 +010053 int GetOption(rtc::Socket::Option opt, int* value) override;
54 int SetOption(rtc::Socket::Option opt, int value) override;
55 int GetError() override;
Steve Anton1cf1b7d2017-10-30 10:00:15 -070056 bool SupportsProtocol(const std::string& protocol) const override;
57 ProtocolType GetProtocol() const override;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070058
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000060 TCPPort(rtc::Thread* thread,
61 rtc::PacketSocketFactory* factory,
62 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020063 uint16_t min_port,
64 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000065 const std::string& username,
66 const std::string& password,
67 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068
69 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +010070 int SendTo(const void* data,
71 size_t size,
72 const rtc::SocketAddress& addr,
73 const rtc::PacketOptions& options,
74 bool payload) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075
76 // Accepts incoming TCP connection.
77 void OnNewConnection(rtc::AsyncPacketSocket* socket,
78 rtc::AsyncPacketSocket* new_socket);
79
80 private:
81 struct Incoming {
82 rtc::SocketAddress addr;
83 rtc::AsyncPacketSocket* socket;
84 };
85
deadbeef1ee21252017-06-13 15:49:45 -070086 void TryCreateServerSocket();
87
Yves Gerey665174f2018-06-19 15:03:05 +020088 rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
89 bool remove = false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090
91 // Receives packet signal from the local TCP Socket.
92 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +020093 const char* data,
94 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 const rtc::SocketAddress& remote_addr,
96 const rtc::PacketTime& packet_time);
97
Stefan Holmer55674ff2016-01-14 15:49:16 +010098 void OnSentPacket(rtc::AsyncPacketSocket* socket,
99 const rtc::SentPacket& sent_packet) override;
100
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
102
103 void OnAddressReady(rtc::AsyncPacketSocket* socket,
104 const rtc::SocketAddress& address);
105
Steve Anton6c38cc72017-11-29 10:25:58 -0800106 // TODO(?): Is this still needed?
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 bool incoming_only_;
108 bool allow_listen_;
109 rtc::AsyncPacketSocket* socket_;
110 int error_;
111 std::list<Incoming> incoming_;
112
113 friend class TCPConnection;
114};
115
116class TCPConnection : public Connection {
117 public:
118 // Connection is outgoing unless socket is specified
Yves Gerey665174f2018-06-19 15:03:05 +0200119 TCPConnection(TCPPort* port,
120 const Candidate& candidate,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121 rtc::AsyncPacketSocket* socket = 0);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100122 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123
Stefan Holmer55674ff2016-01-14 15:49:16 +0100124 int Send(const void* data,
125 size_t size,
126 const rtc::PacketOptions& options) override;
127 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700129 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
130
Stefan Holmer55674ff2016-01-14 15:49:16 +0100131 void OnMessage(rtc::Message* pmsg) override;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700132
133 // Allow test cases to overwrite the default timeout period.
134 int reconnection_timeout() const { return reconnection_timeout_; }
135 void set_reconnection_timeout(int timeout_in_ms) {
136 reconnection_timeout_ = timeout_in_ms;
137 }
138
139 protected:
140 enum {
141 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
142 };
143
144 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
145 // addition to what Port::OnConnectionRequestResponse does.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100146 void OnConnectionRequestResponse(ConnectionRequest* req,
147 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148
149 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700150 // Helper function to handle the case when Ping or Send fails with error
151 // related to socket close.
152 void MaybeReconnect();
153
154 void CreateOutgoingTcpSocket();
155
156 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
157
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 void OnConnect(rtc::AsyncPacketSocket* socket);
159 void OnClose(rtc::AsyncPacketSocket* socket, int error);
160 void OnReadPacket(rtc::AsyncPacketSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200161 const char* data,
162 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 const rtc::SocketAddress& remote_addr,
164 const rtc::PacketTime& packet_time);
165 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
166
kwiberg3ec46792016-04-27 07:22:53 -0700167 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700169 bool outgoing_;
170
171 // Guard against multiple outgoing tcp connection during a reconnect.
172 bool connection_pending_;
173
174 // Guard against data packets sent when we reconnect a TCP connection. During
175 // reconnecting, when a new tcp connection has being made, we can't send data
176 // packets out until the STUN binding is completed (i.e. the write state is
177 // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC
178 // socket, when receiving data packets before that, will trigger OnError which
179 // will terminate the newly created connection.
180 bool pretending_to_be_writable_;
181
182 // Allow test case to overwrite the default timeout period.
183 int reconnection_timeout_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184
185 friend class TCPPort;
186};
187
188} // namespace cricket
189
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200190#endif // P2P_BASE_TCPPORT_H_