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> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 24 | #include <memory> |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 25 | #include <set> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 28 | |
| 29 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | #include "webrtc/p2p/base/candidate.h" |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 31 | #include "webrtc/p2p/base/candidatepairinterface.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 32 | #include "webrtc/p2p/base/p2ptransport.h" |
| 33 | #include "webrtc/p2p/base/portallocator.h" |
| 34 | #include "webrtc/p2p/base/portinterface.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 35 | #include "webrtc/p2p/base/transportchannelimpl.h" |
| 36 | #include "webrtc/base/asyncpacketsocket.h" |
| 37 | #include "webrtc/base/sigslot.h" |
| 38 | |
| 39 | namespace cricket { |
| 40 | |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 41 | extern const int WEAK_PING_INTERVAL; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 42 | extern const int STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL; |
| 43 | extern const int STABLE_WRITABLE_CONNECTION_PING_INTERVAL; |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 44 | static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 45 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 46 | struct IceParameters { |
| 47 | std::string ufrag; |
| 48 | std::string pwd; |
| 49 | IceParameters(const std::string& ice_ufrag, const std::string& ice_pwd) |
| 50 | : ufrag(ice_ufrag), pwd(ice_pwd) {} |
| 51 | |
| 52 | bool operator==(const IceParameters& other) { |
| 53 | return ufrag == other.ufrag && pwd == other.pwd; |
| 54 | } |
| 55 | bool operator!=(const IceParameters& other) { return !(*this == other); } |
| 56 | }; |
| 57 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | // Adds the port on which the candidate originated. |
| 59 | class RemoteCandidate : public Candidate { |
| 60 | public: |
| 61 | RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 62 | : Candidate(c), origin_port_(origin_port) {} |
| 63 | |
| 64 | PortInterface* origin_port() { return origin_port_; } |
| 65 | |
| 66 | private: |
| 67 | PortInterface* origin_port_; |
| 68 | }; |
| 69 | |
| 70 | // P2PTransportChannel manages the candidates and connection process to keep |
| 71 | // two P2P clients connected to each other. |
| 72 | class P2PTransportChannel : public TransportChannelImpl, |
| 73 | public rtc::MessageHandler { |
| 74 | public: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 75 | P2PTransportChannel(const std::string& transport_name, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | int component, |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 77 | PortAllocator* allocator); |
| 78 | // TODO(mikescarlett): Deprecated. Remove when Chromium's |
| 79 | // IceTransportChannel does not depend on this. |
| 80 | P2PTransportChannel(const std::string& transport_name, |
| 81 | int component, |
guidou | 74db777 | 2016-02-18 01:57:49 -0800 | [diff] [blame] | 82 | P2PTransport* transport, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 83 | PortAllocator* allocator); |
| 84 | virtual ~P2PTransportChannel(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 85 | |
| 86 | // From TransportChannelImpl: |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 87 | TransportChannelState GetState() const override; |
| 88 | void SetIceRole(IceRole role) override; |
| 89 | IceRole GetIceRole() const override { return ice_role_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 90 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 91 | void SetIceCredentials(const std::string& ice_ufrag, |
| 92 | const std::string& ice_pwd) override; |
| 93 | void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 94 | const std::string& ice_pwd) override; |
| 95 | void SetRemoteIceMode(IceMode mode) override; |
deadbeef | 886815b | 2016-06-29 15:21:04 -0700 | [diff] [blame] | 96 | // TODO(deadbeef): Deprecated. Remove when Chromium's |
| 97 | // IceTransportChannel does not depend on this. |
| 98 | void Connect() {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 99 | void MaybeStartGathering() override; |
| 100 | IceGatheringState gathering_state() const override { |
| 101 | return gathering_state_; |
| 102 | } |
| 103 | void AddRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 104 | void RemoveRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 105 | // Sets the parameters in IceConfig. We do not set them blindly. Instead, we |
| 106 | // only update the parameter if it is considered set in |config|. For example, |
| 107 | // a negative value of receiving_timeout will be considered "not set" and we |
| 108 | // will not use it to update the respective parameter in |config_|. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 109 | // TODO(deadbeef): Use rtc::Optional instead of negative values. |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 110 | void SetIceConfig(const IceConfig& config) override; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 111 | const IceConfig& config() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 112 | |
| 113 | // From TransportChannel: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 114 | int SendPacket(const char* data, |
| 115 | size_t len, |
| 116 | const rtc::PacketOptions& options, |
| 117 | int flags) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 118 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 119 | bool GetOption(rtc::Socket::Option opt, int* value) override; |
| 120 | int GetError() override { return error_; } |
| 121 | bool GetStats(std::vector<ConnectionInfo>* stats) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 122 | |
Honghai Zhang | 8cd7f22 | 2016-06-23 13:44:34 -0700 | [diff] [blame] | 123 | // TODO(honghaiz): Remove this method once the reference of it in |
| 124 | // Chromoting is removed. |
| 125 | const Connection* best_connection() const { return selected_connection_; } |
| 126 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 127 | const Connection* selected_connection() const { return selected_connection_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 128 | void set_incoming_only(bool value) { incoming_only_ = value; } |
| 129 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame^] | 130 | // Note: These are only for testing purpose. |
| 131 | // |ports_| and |pruned_ports| should not be changed from outside. |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 132 | const std::vector<PortInterface*>& ports() { return ports_; } |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame^] | 133 | const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | |
| 135 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 136 | |
| 137 | // DTLS methods. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 138 | bool IsDtlsActive() const override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 139 | |
| 140 | // Default implementation. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 141 | bool GetSslRole(rtc::SSLRole* role) const override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 142 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 143 | bool SetSslRole(rtc::SSLRole role) override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 144 | |
| 145 | // Set up the ciphers to use for DTLS-SRTP. |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 146 | bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 150 | // Find out which DTLS-SRTP cipher was negotiated. |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 151 | bool GetSrtpCryptoSuite(int* cipher) override { return false; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 152 | |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 153 | // Find out which DTLS cipher was negotiated. |
Guo-wei Shieh | 6caafbe | 2015-10-05 12:43:27 -0700 | [diff] [blame] | 154 | bool GetSslCipherSuite(int* cipher) override { return false; } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 155 | |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 156 | // Returns null because the channel is not encrypted by default. |
| 157 | rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override { |
| 158 | return nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | } |
| 160 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 161 | std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate() |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 162 | const override { |
| 163 | return nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Allows key material to be extracted for external encryption. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 167 | bool ExportKeyingMaterial(const std::string& label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 168 | const uint8_t* context, |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 169 | size_t context_len, |
| 170 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 171 | uint8_t* result, |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 172 | size_t result_len) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 176 | bool SetLocalCertificate( |
| 177 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // Set DTLS Remote fingerprint. Must be after local identity set. |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 182 | bool SetRemoteFingerprint(const std::string& digest_alg, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 183 | const uint8_t* digest, |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 184 | size_t digest_len) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame^] | 188 | void PruneAllPorts(); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 189 | int receiving_timeout() const { return config_.receiving_timeout; } |
| 190 | int check_receiving_interval() const { return check_receiving_interval_; } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 191 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 192 | // Helper method used only in unittest. |
| 193 | rtc::DiffServCodePoint DefaultDscpValue() const; |
| 194 | |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 195 | // Public for unit tests. |
| 196 | Connection* FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 197 | void MarkConnectionPinged(Connection* conn); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 198 | |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 199 | // Public for unit tests. |
| 200 | const std::vector<Connection*>& connections() const { return connections_; } |
| 201 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 202 | // Public for unit tests. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 203 | PortAllocatorSession* allocator_session() { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 204 | return allocator_sessions_.back().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 205 | } |
| 206 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 207 | // Public for unit tests. |
| 208 | const std::vector<RemoteCandidate>& remote_candidates() const { |
| 209 | return remote_candidates_; |
| 210 | } |
| 211 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 212 | private: |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 213 | rtc::Thread* thread() const { return worker_thread_; } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 214 | bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } |
| 215 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 216 | // A transport channel is weak if the current best connection is either |
| 217 | // 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] | 218 | bool weak() const; |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 219 | // Returns true if it's possible to send packets on this channel. |
| 220 | bool ReadyToSend() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 221 | void UpdateConnectionStates(); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 222 | void RequestSortAndStateUpdate(); |
| 223 | // Start pinging if we haven't already started, and we now have a connection |
| 224 | // that's pingable. |
| 225 | void MaybeStartPinging(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 226 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 227 | // The methods below return a positive value if |a| is preferable to |b|, |
| 228 | // a negative value if |b| is preferable, and 0 if they're equally preferable. |
| 229 | // If |receiving_unchanged_threshold| is set, then when |b| is receiving and |
| 230 | // |a| is not, returns a negative value only if |b| has been in receiving |
| 231 | // state and |a| has been in not receiving state since |
| 232 | // |receiving_unchanged_threshold| and sets |
| 233 | // |missed_receiving_unchanged_threshold| to true otherwise. |
| 234 | int CompareConnectionStates( |
| 235 | const cricket::Connection* a, |
| 236 | const cricket::Connection* b, |
| 237 | rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 238 | bool* missed_receiving_unchanged_threshold) const; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 239 | int CompareConnectionCandidates(const cricket::Connection* a, |
| 240 | const cricket::Connection* b) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 241 | // Compares two connections based on the connection states |
| 242 | // (writable/receiving/connected), nomination states, last data received time, |
| 243 | // and static preferences. Does not include latency. Used by both sorting |
| 244 | // and ShouldSwitchSelectedConnection(). |
| 245 | // Returns a positive value if |a| is better than |b|. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 246 | int CompareConnections(const cricket::Connection* a, |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 247 | const cricket::Connection* b, |
| 248 | rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 249 | bool* missed_receiving_unchanged_threshold) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 250 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 251 | bool PresumedWritable(const cricket::Connection* conn) const; |
| 252 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 253 | void SortConnectionsAndUpdateState(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 254 | void SwitchSelectedConnection(Connection* conn); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 255 | void UpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 256 | void HandleAllTimedOut(); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 257 | void MaybeStopPortAllocatorSessions(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 258 | TransportChannelState ComputeState() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 259 | |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 260 | Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 261 | bool CreateConnections(const Candidate& remote_candidate, |
| 262 | PortInterface* origin_port); |
| 263 | bool CreateConnection(PortInterface* port, |
| 264 | const Candidate& remote_candidate, |
| 265 | PortInterface* origin_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 266 | bool FindConnection(cricket::Connection* connection) const; |
| 267 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 268 | uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 269 | bool IsDuplicateRemoteCandidate(const Candidate& candidate); |
| 270 | void RememberRemoteCandidate(const Candidate& remote_candidate, |
| 271 | PortInterface* origin_port); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 272 | bool IsPingable(const Connection* conn, int64_t now) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 273 | bool IsSelectedConnectionPingable(int64_t now); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 274 | int CalculateActiveWritablePingInterval(const Connection* conn, |
| 275 | int64_t now) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 276 | void PingConnection(Connection* conn); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 277 | void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 278 | void AddConnection(Connection* connection); |
| 279 | |
| 280 | void OnPortReady(PortAllocatorSession *session, PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 281 | void OnPortsPruned(PortAllocatorSession* session, |
| 282 | const std::vector<PortInterface*>& ports); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 283 | void OnCandidatesReady(PortAllocatorSession *session, |
| 284 | const std::vector<Candidate>& candidates); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 285 | void OnCandidatesRemoved(PortAllocatorSession* session, |
| 286 | const std::vector<Candidate>& candidates); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 287 | void OnCandidatesAllocationDone(PortAllocatorSession* session); |
| 288 | void OnUnknownAddress(PortInterface* port, |
| 289 | const rtc::SocketAddress& addr, |
| 290 | ProtocolType proto, |
| 291 | IceMessage* stun_msg, |
| 292 | const std::string& remote_username, |
| 293 | bool port_muxed); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 294 | |
| 295 | // When a port is destroyed, remove it from both lists |ports_| |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 296 | // and |pruned_ports_|. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 297 | void OnPortDestroyed(PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 298 | // When pruning a port, move it from |ports_| to |pruned_ports_|. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 299 | // Returns true if the port is found and removed from |ports_|. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame^] | 300 | bool PrunePort(PortInterface* port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | void OnRoleConflict(PortInterface* port); |
| 302 | |
| 303 | void OnConnectionStateChange(Connection* connection); |
| 304 | void OnReadPacket(Connection *connection, const char *data, size_t len, |
| 305 | const rtc::PacketTime& packet_time); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 306 | void OnSentPacket(const rtc::SentPacket& sent_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 307 | void OnReadyToSend(Connection* connection); |
| 308 | void OnConnectionDestroyed(Connection *connection); |
| 309 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 310 | void OnNominated(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 311 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 312 | void OnMessage(rtc::Message* pmsg) override; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 313 | void OnCheckAndPing(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 314 | void OnRegatherOnFailedNetworks(); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 315 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 316 | // Returns true if we should switch to the new connection. |
| 317 | // sets |missed_receiving_unchanged_threshold| to true if either |
| 318 | // the selected connection or the new connection missed its |
| 319 | // receiving-unchanged-threshold. |
| 320 | bool ShouldSwitchSelectedConnection( |
| 321 | Connection* new_connection, |
| 322 | bool* missed_receiving_unchanged_threshold) const; |
| 323 | // Returns true if the new_connection is selected for transmission. |
| 324 | bool MaybeSwitchSelectedConnection(Connection* new_connection, |
| 325 | const std::string& reason); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 326 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 327 | void PruneConnections(); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 328 | bool IsBackupConnection(const Connection* conn) const; |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 329 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 330 | Connection* FindConnectionToPing(int64_t now); |
| 331 | Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 332 | // Between |conn1| and |conn2|, this function returns the one which should |
| 333 | // be pinged first. |
| 334 | Connection* SelectMostPingableConnection(Connection* conn1, |
| 335 | Connection* conn2); |
| 336 | // Select the connection which is Relay/Relay. If both of them are, |
| 337 | // UDP relay protocol takes precedence. |
| 338 | Connection* MostLikelyToWork(Connection* conn1, Connection* conn2); |
| 339 | // Compare the last_ping_sent time and return the one least recently pinged. |
| 340 | Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2); |
| 341 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 342 | // Returns the latest remote ICE parameters or nullptr if there are no remote |
| 343 | // ICE parameters yet. |
| 344 | IceParameters* remote_ice() { |
| 345 | return remote_ice_parameters_.empty() ? nullptr |
| 346 | : &remote_ice_parameters_.back(); |
| 347 | } |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 348 | // Returns the remote IceParameters and generation that match |ufrag| |
| 349 | // if found, and returns nullptr otherwise. |
| 350 | const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, |
| 351 | uint32_t* generation); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 352 | // Returns the index of the latest remote ICE parameters, or 0 if no remote |
| 353 | // ICE parameters have been received. |
| 354 | uint32_t remote_ice_generation() { |
| 355 | return remote_ice_parameters_.empty() |
| 356 | ? 0 |
| 357 | : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); |
| 358 | } |
| 359 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 360 | PortAllocator* allocator_; |
| 361 | rtc::Thread* worker_thread_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 362 | bool incoming_only_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 363 | int error_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 364 | std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 365 | // |ports_| contains ports that are used to form new connections when |
| 366 | // new remote candidates are added. |
| 367 | std::vector<PortInterface*> ports_; |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 368 | // |pruned_ports_| contains ports that have been removed from |ports_| and |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 369 | // are not being used to form new connections, but that aren't yet destroyed. |
| 370 | // They may have existing connections, and they still fire signals such as |
| 371 | // SignalUnknownAddress. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 372 | std::vector<PortInterface*> pruned_ports_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 373 | |
| 374 | // |connections_| is a sorted list with the first one always be the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 375 | // |selected_connection_| when it's not nullptr. The combination of |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 376 | // |pinged_connections_| and |unpinged_connections_| has the same |
| 377 | // connections as |connections_|. These 2 sets maintain whether a |
| 378 | // connection should be pinged next or not. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 379 | std::vector<Connection *> connections_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 380 | std::set<Connection*> pinged_connections_; |
| 381 | std::set<Connection*> unpinged_connections_; |
| 382 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 383 | Connection* selected_connection_ = nullptr; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 384 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 385 | std::vector<RemoteCandidate> remote_candidates_; |
| 386 | bool sort_dirty_; // indicates whether another sort is needed right now |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 387 | bool had_connection_ = false; // if connections_ has ever been nonempty |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 388 | typedef std::map<rtc::Socket::Option, int> OptionMap; |
| 389 | OptionMap options_; |
| 390 | std::string ice_ufrag_; |
| 391 | std::string ice_pwd_; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 392 | std::vector<IceParameters> remote_ice_parameters_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 393 | IceMode remote_ice_mode_; |
| 394 | IceRole ice_role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 395 | uint64_t tiebreaker_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 396 | IceGatheringState gathering_state_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 397 | |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 398 | int check_receiving_interval_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 399 | int64_t last_ping_sent_ms_ = 0; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 400 | int weak_ping_interval_ = WEAK_PING_INTERVAL; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 401 | TransportChannelState state_ = TransportChannelState::STATE_INIT; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 402 | IceConfig config_; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 403 | int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 404 | bool started_pinging_ = false; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 405 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 406 | RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | } // namespace cricket |
| 410 | |
| 411 | #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |