blob: 76a73f8f474fdf3600c0902d5f6d0c504cf4d15c [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"
19#include "webrtc/base/asyncpacketsocket.h"
20
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,
36 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +020037 uint16_t min_port,
38 uint16_t max_port,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 const std::string& username,
40 const std::string& password,
41 bool allow_listen) {
pkasting@chromium.org332331f2014-11-06 20:19:22 +000042 TCPPort* port = new TCPPort(thread, factory, network, ip, min_port,
43 max_port, username, password, allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044 if (!port->Init()) {
45 delete port;
46 port = NULL;
47 }
48 return port;
49 }
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
Stefan Holmer55674ff2016-01-14 15:49:16 +010057 int GetOption(rtc::Socket::Option opt, int* value) override;
58 int SetOption(rtc::Socket::Option opt, int value) override;
59 int GetError() override;
60 bool SupportsProtocol(const std::string& protocol) const override {
Honghai Zhangf9945b22015-12-15 12:20:13 -080061 return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME;
62 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070064 ProtocolType GetProtocol() const override { return PROTO_TCP; }
65
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,
69 rtc::Network* network,
70 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +020071 uint16_t min_port,
72 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000073 const std::string& username,
74 const std::string& password,
75 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 bool Init();
77
78 // Handles sending using the local TCP socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +010079 int SendTo(const void* data,
80 size_t size,
81 const rtc::SocketAddress& addr,
82 const rtc::PacketOptions& options,
83 bool payload) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084
85 // Accepts incoming TCP connection.
86 void OnNewConnection(rtc::AsyncPacketSocket* socket,
87 rtc::AsyncPacketSocket* new_socket);
88
89 private:
90 struct Incoming {
91 rtc::SocketAddress addr;
92 rtc::AsyncPacketSocket* socket;
93 };
94
95 rtc::AsyncPacketSocket* GetIncoming(
96 const rtc::SocketAddress& addr, bool remove = false);
97
98 // Receives packet signal from the local TCP Socket.
99 void OnReadPacket(rtc::AsyncPacketSocket* socket,
100 const char* data, size_t size,
101 const rtc::SocketAddress& remote_addr,
102 const rtc::PacketTime& packet_time);
103
Stefan Holmer55674ff2016-01-14 15:49:16 +0100104 void OnSentPacket(rtc::AsyncPacketSocket* socket,
105 const rtc::SentPacket& sent_packet) override;
106
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
108
109 void OnAddressReady(rtc::AsyncPacketSocket* socket,
110 const rtc::SocketAddress& address);
111
112 // TODO: Is this still needed?
113 bool incoming_only_;
114 bool allow_listen_;
115 rtc::AsyncPacketSocket* socket_;
116 int error_;
117 std::list<Incoming> incoming_;
118
119 friend class TCPConnection;
120};
121
122class TCPConnection : public Connection {
123 public:
124 // Connection is outgoing unless socket is specified
125 TCPConnection(TCPPort* port, const Candidate& candidate,
126 rtc::AsyncPacketSocket* socket = 0);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100127 ~TCPConnection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
Stefan Holmer55674ff2016-01-14 15:49:16 +0100129 int Send(const void* data,
130 size_t size,
131 const rtc::PacketOptions& options) override;
132 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700134 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
135
Stefan Holmer55674ff2016-01-14 15:49:16 +0100136 void OnMessage(rtc::Message* pmsg) override;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700137
138 // Allow test cases to overwrite the default timeout period.
139 int reconnection_timeout() const { return reconnection_timeout_; }
140 void set_reconnection_timeout(int timeout_in_ms) {
141 reconnection_timeout_ = timeout_in_ms;
142 }
143
144 protected:
145 enum {
146 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
147 };
148
149 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
150 // addition to what Port::OnConnectionRequestResponse does.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100151 void OnConnectionRequestResponse(ConnectionRequest* req,
152 StunMessage* response) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153
154 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700155 // Helper function to handle the case when Ping or Send fails with error
156 // related to socket close.
157 void MaybeReconnect();
158
159 void CreateOutgoingTcpSocket();
160
161 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
162
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 void OnConnect(rtc::AsyncPacketSocket* socket);
164 void OnClose(rtc::AsyncPacketSocket* socket, int error);
165 void OnReadPacket(rtc::AsyncPacketSocket* socket,
166 const char* data, size_t size,
167 const rtc::SocketAddress& remote_addr,
168 const rtc::PacketTime& packet_time);
169 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
170
kwiberg3ec46792016-04-27 07:22:53 -0700171 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700173 bool outgoing_;
174
175 // Guard against multiple outgoing tcp connection during a reconnect.
176 bool connection_pending_;
177
178 // Guard against data packets sent when we reconnect a TCP connection. During
179 // reconnecting, when a new tcp connection has being made, we can't send data
180 // packets out until the STUN binding is completed (i.e. the write state is
181 // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC
182 // socket, when receiving data packets before that, will trigger OnError which
183 // will terminate the newly created connection.
184 bool pretending_to_be_writable_;
185
186 // Allow test case to overwrite the default timeout period.
187 int reconnection_timeout_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188
189 friend class TCPPort;
190};
191
192} // namespace cricket
193
194#endif // WEBRTC_P2P_BASE_TCPPORT_H_