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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef P2P_BASE_TCPPORT_H_ |
| 12 | #define P2P_BASE_TCPPORT_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "p2p/base/port.h" |
| 19 | #include "rtc_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, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 36 | uint16_t min_port, |
| 37 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 38 | const std::string& username, |
| 39 | const std::string& password, |
| 40 | bool allow_listen) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 41 | return new TCPPort(thread, factory, network, min_port, max_port, username, |
| 42 | password, allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | } |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 44 | ~TCPPort() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 45 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 46 | Connection* CreateConnection(const Candidate& address, |
| 47 | CandidateOrigin origin) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 48 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 49 | void PrepareAddress() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 50 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 51 | int GetOption(rtc::Socket::Option opt, int* value) override; |
| 52 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 53 | int GetError() override; |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 54 | bool SupportsProtocol(const std::string& protocol) const override; |
| 55 | ProtocolType GetProtocol() const override; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 56 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 57 | protected: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 58 | TCPPort(rtc::Thread* thread, |
| 59 | rtc::PacketSocketFactory* factory, |
| 60 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 61 | uint16_t min_port, |
| 62 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 63 | const std::string& username, |
| 64 | const std::string& password, |
| 65 | bool allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 66 | |
| 67 | // Handles sending using the local TCP socket. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 68 | int SendTo(const void* data, |
| 69 | size_t size, |
| 70 | const rtc::SocketAddress& addr, |
| 71 | const rtc::PacketOptions& options, |
| 72 | bool payload) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | |
| 74 | // Accepts incoming TCP connection. |
| 75 | void OnNewConnection(rtc::AsyncPacketSocket* socket, |
| 76 | rtc::AsyncPacketSocket* new_socket); |
| 77 | |
| 78 | private: |
| 79 | struct Incoming { |
| 80 | rtc::SocketAddress addr; |
| 81 | rtc::AsyncPacketSocket* socket; |
| 82 | }; |
| 83 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 84 | void TryCreateServerSocket(); |
| 85 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 86 | rtc::AsyncPacketSocket* GetIncoming( |
| 87 | const rtc::SocketAddress& addr, bool remove = false); |
| 88 | |
| 89 | // Receives packet signal from the local TCP Socket. |
| 90 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 91 | const char* data, size_t size, |
| 92 | const rtc::SocketAddress& remote_addr, |
| 93 | const rtc::PacketTime& packet_time); |
| 94 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 95 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 96 | const rtc::SentPacket& sent_packet) override; |
| 97 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 98 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 99 | |
| 100 | void OnAddressReady(rtc::AsyncPacketSocket* socket, |
| 101 | const rtc::SocketAddress& address); |
| 102 | |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame^] | 103 | // TODO(?): Is this still needed? |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 104 | bool incoming_only_; |
| 105 | bool allow_listen_; |
| 106 | rtc::AsyncPacketSocket* socket_; |
| 107 | int error_; |
| 108 | std::list<Incoming> incoming_; |
| 109 | |
| 110 | friend class TCPConnection; |
| 111 | }; |
| 112 | |
| 113 | class TCPConnection : public Connection { |
| 114 | public: |
| 115 | // Connection is outgoing unless socket is specified |
| 116 | TCPConnection(TCPPort* port, const Candidate& candidate, |
| 117 | rtc::AsyncPacketSocket* socket = 0); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 118 | ~TCPConnection() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 120 | int Send(const void* data, |
| 121 | size_t size, |
| 122 | const rtc::PacketOptions& options) override; |
| 123 | int GetError() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 125 | rtc::AsyncPacketSocket* socket() { return socket_.get(); } |
| 126 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 127 | void OnMessage(rtc::Message* pmsg) override; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 128 | |
| 129 | // Allow test cases to overwrite the default timeout period. |
| 130 | int reconnection_timeout() const { return reconnection_timeout_; } |
| 131 | void set_reconnection_timeout(int timeout_in_ms) { |
| 132 | reconnection_timeout_ = timeout_in_ms; |
| 133 | } |
| 134 | |
| 135 | protected: |
| 136 | enum { |
| 137 | MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE, |
| 138 | }; |
| 139 | |
| 140 | // Set waiting_for_stun_binding_complete_ to false to allow data packets in |
| 141 | // addition to what Port::OnConnectionRequestResponse does. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 142 | void OnConnectionRequestResponse(ConnectionRequest* req, |
| 143 | StunMessage* response) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 144 | |
| 145 | private: |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 146 | // Helper function to handle the case when Ping or Send fails with error |
| 147 | // related to socket close. |
| 148 | void MaybeReconnect(); |
| 149 | |
| 150 | void CreateOutgoingTcpSocket(); |
| 151 | |
| 152 | void ConnectSocketSignals(rtc::AsyncPacketSocket* socket); |
| 153 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 154 | void OnConnect(rtc::AsyncPacketSocket* socket); |
| 155 | void OnClose(rtc::AsyncPacketSocket* socket, int error); |
| 156 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 157 | const char* data, size_t size, |
| 158 | const rtc::SocketAddress& remote_addr, |
| 159 | const rtc::PacketTime& packet_time); |
| 160 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 161 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 162 | std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 163 | int error_; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 164 | bool outgoing_; |
| 165 | |
| 166 | // Guard against multiple outgoing tcp connection during a reconnect. |
| 167 | bool connection_pending_; |
| 168 | |
| 169 | // Guard against data packets sent when we reconnect a TCP connection. During |
| 170 | // reconnecting, when a new tcp connection has being made, we can't send data |
| 171 | // packets out until the STUN binding is completed (i.e. the write state is |
| 172 | // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC |
| 173 | // socket, when receiving data packets before that, will trigger OnError which |
| 174 | // will terminate the newly created connection. |
| 175 | bool pretending_to_be_writable_; |
| 176 | |
| 177 | // Allow test case to overwrite the default timeout period. |
| 178 | int reconnection_timeout_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 179 | |
| 180 | friend class TCPPort; |
| 181 | }; |
| 182 | |
| 183 | } // namespace cricket |
| 184 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 185 | #endif // P2P_BASE_TCPPORT_H_ |