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 | // P2PTransportChannel wraps up the state management of the connection between |
| 12 | // two P2P clients. Clients have candidate ports for connecting, and |
| 13 | // connections which are combinations of candidates from each end (Alice and |
| 14 | // Bob each have candidates, one candidate from Alice and one candidate from |
| 15 | // Bob are used to make a connection, repeat to make many connections). |
| 16 | // |
| 17 | // When all of the available connections become invalid (non-writable), we |
| 18 | // kick off a process of determining more candidates and more connections. |
| 19 | // |
| 20 | #ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 21 | #define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 22 | |
| 23 | #include <map> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | #include "webrtc/p2p/base/candidate.h" |
| 27 | #include "webrtc/p2p/base/p2ptransport.h" |
| 28 | #include "webrtc/p2p/base/portallocator.h" |
| 29 | #include "webrtc/p2p/base/portinterface.h" |
| 30 | #include "webrtc/p2p/base/transport.h" |
| 31 | #include "webrtc/p2p/base/transportchannelimpl.h" |
| 32 | #include "webrtc/base/asyncpacketsocket.h" |
| 33 | #include "webrtc/base/sigslot.h" |
| 34 | |
| 35 | namespace cricket { |
| 36 | |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame^] | 37 | extern const uint32_t WEAK_PING_DELAY; |
| 38 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | // Adds the port on which the candidate originated. |
| 40 | class RemoteCandidate : public Candidate { |
| 41 | public: |
| 42 | RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 43 | : Candidate(c), origin_port_(origin_port) {} |
| 44 | |
| 45 | PortInterface* origin_port() { return origin_port_; } |
| 46 | |
| 47 | private: |
| 48 | PortInterface* origin_port_; |
| 49 | }; |
| 50 | |
| 51 | // P2PTransportChannel manages the candidates and connection process to keep |
| 52 | // two P2P clients connected to each other. |
| 53 | class P2PTransportChannel : public TransportChannelImpl, |
| 54 | public rtc::MessageHandler { |
| 55 | public: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 56 | P2PTransportChannel(const std::string& transport_name, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 57 | int component, |
| 58 | P2PTransport* transport, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 59 | PortAllocator* allocator); |
| 60 | virtual ~P2PTransportChannel(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | |
| 62 | // From TransportChannelImpl: |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 63 | Transport* GetTransport() override { return transport_; } |
| 64 | TransportChannelState GetState() const override; |
| 65 | void SetIceRole(IceRole role) override; |
| 66 | IceRole GetIceRole() const override { return ice_role_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 67 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 68 | void SetIceCredentials(const std::string& ice_ufrag, |
| 69 | const std::string& ice_pwd) override; |
| 70 | void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 71 | const std::string& ice_pwd) override; |
| 72 | void SetRemoteIceMode(IceMode mode) override; |
| 73 | void Connect() override; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 74 | void MaybeStartGathering() override; |
| 75 | IceGatheringState gathering_state() const override { |
| 76 | return gathering_state_; |
| 77 | } |
| 78 | void AddRemoteCandidate(const Candidate& candidate) override; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 79 | // Sets the receiving timeout and gather_continually. |
honghaiz | a03cd3f | 2015-07-13 17:08:08 -0700 | [diff] [blame] | 80 | // This also sets the check_receiving_delay proportionally. |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 81 | void SetIceConfig(const IceConfig& config) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | |
| 83 | // From TransportChannel: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 84 | int SendPacket(const char* data, |
| 85 | size_t len, |
| 86 | const rtc::PacketOptions& options, |
| 87 | int flags) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 88 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 89 | bool GetOption(rtc::Socket::Option opt, int* value) override; |
| 90 | int GetError() override { return error_; } |
| 91 | bool GetStats(std::vector<ConnectionInfo>* stats) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 92 | |
| 93 | const Connection* best_connection() const { return best_connection_; } |
| 94 | void set_incoming_only(bool value) { incoming_only_ = value; } |
| 95 | |
| 96 | // Note: This is only for testing purpose. |
| 97 | // |ports_| should not be changed from outside. |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 98 | const std::vector<PortInterface*>& ports() { return ports_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | |
| 100 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 101 | |
| 102 | // DTLS methods. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 103 | bool IsDtlsActive() const override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 104 | |
| 105 | // Default implementation. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 106 | bool GetSslRole(rtc::SSLRole* role) const override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 108 | bool SetSslRole(rtc::SSLRole role) override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 109 | |
| 110 | // Set up the ciphers to use for DTLS-SRTP. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 111 | bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 115 | // Find out which DTLS-SRTP cipher was negotiated. |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 116 | bool GetSrtpCryptoSuite(std::string* cipher) override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 117 | |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 118 | // Find out which DTLS cipher was negotiated. |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 119 | bool GetSslCipherSuite(int* cipher) override { return false; } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 120 | |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 121 | // Returns null because the channel is not encrypted by default. |
| 122 | rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override { |
| 123 | return nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 126 | bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // Allows key material to be extracted for external encryption. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 131 | bool ExportKeyingMaterial(const std::string& label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 132 | const uint8_t* context, |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 133 | size_t context_len, |
| 134 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 135 | uint8_t* result, |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 136 | size_t result_len) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 140 | bool SetLocalCertificate( |
| 141 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // Set DTLS Remote fingerprint. Must be after local identity set. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 146 | bool SetRemoteFingerprint(const std::string& digest_alg, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 147 | const uint8_t* digest, |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 148 | size_t digest_len) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 152 | int receiving_timeout() const { return receiving_timeout_; } |
| 153 | int check_receiving_delay() const { return check_receiving_delay_; } |
| 154 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 155 | // Helper method used only in unittest. |
| 156 | rtc::DiffServCodePoint DefaultDscpValue() const; |
| 157 | |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 158 | // Public for unit tests. |
| 159 | Connection* FindNextPingableConnection(); |
| 160 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 161 | private: |
| 162 | rtc::Thread* thread() { return worker_thread_; } |
| 163 | PortAllocatorSession* allocator_session() { |
| 164 | return allocator_sessions_.back(); |
| 165 | } |
| 166 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 167 | // A transport channel is weak if the current best connection is either |
| 168 | // not receiving or not writable, or if there is no best connection at all. |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 169 | bool weak() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 170 | void UpdateConnectionStates(); |
| 171 | void RequestSort(); |
| 172 | void SortConnections(); |
| 173 | void SwitchBestConnectionTo(Connection* conn); |
| 174 | void UpdateChannelState(); |
| 175 | void HandleWritable(); |
| 176 | void HandleNotWritable(); |
| 177 | void HandleAllTimedOut(); |
| 178 | |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 179 | Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 180 | bool CreateConnections(const Candidate& remote_candidate, |
| 181 | PortInterface* origin_port); |
| 182 | bool CreateConnection(PortInterface* port, |
| 183 | const Candidate& remote_candidate, |
| 184 | PortInterface* origin_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | bool FindConnection(cricket::Connection* connection) const; |
| 186 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 187 | uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 188 | bool IsDuplicateRemoteCandidate(const Candidate& candidate); |
| 189 | void RememberRemoteCandidate(const Candidate& remote_candidate, |
| 190 | PortInterface* origin_port); |
| 191 | bool IsPingable(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 192 | void PingConnection(Connection* conn); |
| 193 | void AddAllocatorSession(PortAllocatorSession* session); |
| 194 | void AddConnection(Connection* connection); |
| 195 | |
| 196 | void OnPortReady(PortAllocatorSession *session, PortInterface* port); |
| 197 | void OnCandidatesReady(PortAllocatorSession *session, |
| 198 | const std::vector<Candidate>& candidates); |
| 199 | void OnCandidatesAllocationDone(PortAllocatorSession* session); |
| 200 | void OnUnknownAddress(PortInterface* port, |
| 201 | const rtc::SocketAddress& addr, |
| 202 | ProtocolType proto, |
| 203 | IceMessage* stun_msg, |
| 204 | const std::string& remote_username, |
| 205 | bool port_muxed); |
| 206 | void OnPortDestroyed(PortInterface* port); |
| 207 | void OnRoleConflict(PortInterface* port); |
| 208 | |
| 209 | void OnConnectionStateChange(Connection* connection); |
| 210 | void OnReadPacket(Connection *connection, const char *data, size_t len, |
| 211 | const rtc::PacketTime& packet_time); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 212 | void OnSentPacket(PortInterface* port, const rtc::SentPacket& sent_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | void OnReadyToSend(Connection* connection); |
| 214 | void OnConnectionDestroyed(Connection *connection); |
| 215 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 216 | void OnNominated(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 217 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 218 | void OnMessage(rtc::Message* pmsg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 219 | void OnSort(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 220 | void OnCheckAndPing(); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 221 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 222 | void PruneConnections(); |
| 223 | Connection* best_nominated_connection() const; |
| 224 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 225 | P2PTransport* transport_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 226 | PortAllocator* allocator_; |
| 227 | rtc::Thread* worker_thread_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 228 | bool incoming_only_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | int error_; |
| 230 | std::vector<PortAllocatorSession*> allocator_sessions_; |
| 231 | std::vector<PortInterface *> ports_; |
| 232 | std::vector<Connection *> connections_; |
| 233 | Connection* best_connection_; |
| 234 | // Connection selected by the controlling agent. This should be used only |
| 235 | // at controlled side when protocol type is RFC5245. |
| 236 | Connection* pending_best_connection_; |
| 237 | std::vector<RemoteCandidate> remote_candidates_; |
| 238 | bool sort_dirty_; // indicates whether another sort is needed right now |
| 239 | bool was_writable_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 240 | bool had_connection_ = false; // if connections_ has ever been nonempty |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 241 | typedef std::map<rtc::Socket::Option, int> OptionMap; |
| 242 | OptionMap options_; |
| 243 | std::string ice_ufrag_; |
| 244 | std::string ice_pwd_; |
| 245 | std::string remote_ice_ufrag_; |
| 246 | std::string remote_ice_pwd_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 247 | IceMode remote_ice_mode_; |
| 248 | IceRole ice_role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 249 | uint64_t tiebreaker_; |
| 250 | uint32_t remote_candidate_generation_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 251 | IceGatheringState gathering_state_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 252 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 253 | int check_receiving_delay_; |
| 254 | int receiving_timeout_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 255 | uint32_t last_ping_sent_ms_ = 0; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 256 | bool gather_continually_ = false; |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame^] | 257 | int weak_ping_delay_ = WEAK_PING_DELAY; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 258 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 259 | RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | } // namespace cricket |
| 263 | |
| 264 | #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |