henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include <string> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 17 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 18 | #include "webrtc/p2p/base/port.h" |
Henrik Kjellander | a80c16a | 2017-07-01 16:48:15 +0200 | [diff] [blame] | 19 | #include "webrtc/base/asyncpacketsocket.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 20 | |
| 21 | namespace cricket { |
| 22 | |
| 23 | class 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. |
| 31 | class 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 37 | uint16_t min_port, |
| 38 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | const std::string& username, |
| 40 | const std::string& password, |
| 41 | bool allow_listen) { |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 42 | return new TCPPort(thread, factory, network, ip, min_port, max_port, |
| 43 | username, password, allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 44 | } |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 45 | ~TCPPort() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 47 | Connection* CreateConnection(const Candidate& address, |
| 48 | CandidateOrigin origin) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 50 | void PrepareAddress() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 52 | int GetOption(rtc::Socket::Option opt, int* value) override; |
| 53 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 54 | int GetError() override; |
| 55 | bool SupportsProtocol(const std::string& protocol) const override { |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 56 | return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME; |
| 57 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 59 | ProtocolType GetProtocol() const override { return PROTO_TCP; } |
| 60 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | protected: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 62 | TCPPort(rtc::Thread* thread, |
| 63 | rtc::PacketSocketFactory* factory, |
| 64 | rtc::Network* network, |
| 65 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 66 | uint16_t min_port, |
| 67 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 68 | const std::string& username, |
| 69 | const std::string& password, |
| 70 | bool allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 71 | |
| 72 | // Handles sending using the local TCP socket. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 73 | int SendTo(const void* data, |
| 74 | size_t size, |
| 75 | const rtc::SocketAddress& addr, |
| 76 | const rtc::PacketOptions& options, |
| 77 | bool payload) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 78 | |
| 79 | // Accepts incoming TCP connection. |
| 80 | void OnNewConnection(rtc::AsyncPacketSocket* socket, |
| 81 | rtc::AsyncPacketSocket* new_socket); |
| 82 | |
| 83 | private: |
| 84 | struct Incoming { |
| 85 | rtc::SocketAddress addr; |
| 86 | rtc::AsyncPacketSocket* socket; |
| 87 | }; |
| 88 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 89 | void TryCreateServerSocket(); |
| 90 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | rtc::AsyncPacketSocket* GetIncoming( |
| 92 | const rtc::SocketAddress& addr, bool remove = false); |
| 93 | |
| 94 | // Receives packet signal from the local TCP Socket. |
| 95 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 96 | const char* data, size_t size, |
| 97 | const rtc::SocketAddress& remote_addr, |
| 98 | const rtc::PacketTime& packet_time); |
| 99 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 100 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 101 | const rtc::SentPacket& sent_packet) override; |
| 102 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 103 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 104 | |
| 105 | void OnAddressReady(rtc::AsyncPacketSocket* socket, |
| 106 | const rtc::SocketAddress& address); |
| 107 | |
| 108 | // TODO: Is this still needed? |
| 109 | bool incoming_only_; |
| 110 | bool allow_listen_; |
| 111 | rtc::AsyncPacketSocket* socket_; |
| 112 | int error_; |
| 113 | std::list<Incoming> incoming_; |
| 114 | |
| 115 | friend class TCPConnection; |
| 116 | }; |
| 117 | |
| 118 | class TCPConnection : public Connection { |
| 119 | public: |
| 120 | // Connection is outgoing unless socket is specified |
| 121 | TCPConnection(TCPPort* port, const Candidate& candidate, |
| 122 | rtc::AsyncPacketSocket* socket = 0); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 123 | ~TCPConnection() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 125 | int Send(const void* data, |
| 126 | size_t size, |
| 127 | const rtc::PacketOptions& options) override; |
| 128 | int GetError() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 130 | rtc::AsyncPacketSocket* socket() { return socket_.get(); } |
| 131 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 132 | void OnMessage(rtc::Message* pmsg) override; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 133 | |
| 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 Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 147 | void OnConnectionRequestResponse(ConnectionRequest* req, |
| 148 | StunMessage* response) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 149 | |
| 150 | private: |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 151 | // 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | void OnConnect(rtc::AsyncPacketSocket* socket); |
| 160 | void OnClose(rtc::AsyncPacketSocket* socket, int error); |
| 161 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 162 | const char* data, size_t size, |
| 163 | const rtc::SocketAddress& remote_addr, |
| 164 | const rtc::PacketTime& packet_time); |
| 165 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 166 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 167 | std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 168 | int error_; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 169 | 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | |
| 185 | friend class TCPPort; |
| 186 | }; |
| 187 | |
| 188 | } // namespace cricket |
| 189 | |
| 190 | #endif // WEBRTC_P2P_BASE_TCPPORT_H_ |