blob: a0a36df0e327bc1631c6ccfdf0398c0bffa8a307 [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
11#ifndef WEBRTC_P2P_BASE_TCPPORT_H_
12#define WEBRTC_P2P_BASE_TCPPORT_H_
13
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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include "webrtc/p2p/base/port.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/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:
33 static TCPPort* Create(rtc::Thread* thread,
34 rtc::PacketSocketFactory* factory,
35 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020036 uint16_t min_port,
37 uint16_t max_port,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038 const std::string& username,
39 const std::string& password,
40 bool allow_listen) {
deadbeef5c3c1042017-08-04 15:01:57 -070041 return new TCPPort(thread, factory, network, min_port, max_port, username,
42 password, allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010044 ~TCPPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045
Stefan Holmer55674ff2016-01-14 15:49:16 +010046 Connection* CreateConnection(const Candidate& address,
47 CandidateOrigin origin) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048
Stefan Holmer55674ff2016-01-14 15:49:16 +010049 void PrepareAddress() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050
Stefan Holmer55674ff2016-01-14 15:49:16 +010051 int GetOption(rtc::Socket::Option opt, int* value) override;
52 int SetOption(rtc::Socket::Option opt, int value) override;
53 int GetError() override;
54 bool SupportsProtocol(const std::string& protocol) const override {
Honghai Zhangf9945b22015-12-15 12:20:13 -080055 return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME;
56 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070058 ProtocolType GetProtocol() const override { return PROTO_TCP; }
59
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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089 rtc::AsyncPacketSocket* GetIncoming(
90 const rtc::SocketAddress& addr, bool remove = false);
91
92 // Receives packet signal from the local TCP Socket.
93 void OnReadPacket(rtc::AsyncPacketSocket* socket,
94 const char* data, size_t size,
95 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
106 // TODO: Is this still needed?
107 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
119 TCPConnection(TCPPort* port, const Candidate& candidate,
120 rtc::AsyncPacketSocket* socket = 0);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100121 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122
Stefan Holmer55674ff2016-01-14 15:49:16 +0100123 int Send(const void* data,
124 size_t size,
125 const rtc::PacketOptions& options) override;
126 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700128 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
129
Stefan Holmer55674ff2016-01-14 15:49:16 +0100130 void OnMessage(rtc::Message* pmsg) override;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700131
132 // Allow test cases to overwrite the default timeout period.
133 int reconnection_timeout() const { return reconnection_timeout_; }
134 void set_reconnection_timeout(int timeout_in_ms) {
135 reconnection_timeout_ = timeout_in_ms;
136 }
137
138 protected:
139 enum {
140 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
141 };
142
143 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
144 // addition to what Port::OnConnectionRequestResponse does.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100145 void OnConnectionRequestResponse(ConnectionRequest* req,
146 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147
148 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700149 // Helper function to handle the case when Ping or Send fails with error
150 // related to socket close.
151 void MaybeReconnect();
152
153 void CreateOutgoingTcpSocket();
154
155 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
156
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 void OnConnect(rtc::AsyncPacketSocket* socket);
158 void OnClose(rtc::AsyncPacketSocket* socket, int error);
159 void OnReadPacket(rtc::AsyncPacketSocket* socket,
160 const char* data, size_t size,
161 const rtc::SocketAddress& remote_addr,
162 const rtc::PacketTime& packet_time);
163 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
188#endif // WEBRTC_P2P_BASE_TCPPORT_H_