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 | // |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #ifndef P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 21 | #define P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 22 | |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 23 | #include <algorithm> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | #include <map> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 25 | #include <memory> |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 26 | #include <set> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <vector> |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 29 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 30 | #include "api/candidate.h" |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 31 | #include "api/rtcerror.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 32 | #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" |
| 33 | #include "logging/rtc_event_log/icelogger.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "p2p/base/candidatepairinterface.h" |
| 35 | #include "p2p/base/icetransportinternal.h" |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 36 | #include "p2p/base/p2pconstants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "p2p/base/portallocator.h" |
| 38 | #include "p2p/base/portinterface.h" |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 39 | #include "p2p/base/regatheringcontroller.h" |
Qingsi Wang | 502db3d | 2018-05-16 17:01:37 -0700 | [diff] [blame] | 40 | #include "rtc_base/asyncinvoker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "rtc_base/asyncpacketsocket.h" |
| 42 | #include "rtc_base/constructormagic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "rtc_base/sigslot.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 44 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 45 | namespace webrtc { |
| 46 | class RtcEventLog; |
| 47 | } // namespace webrtc |
| 48 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | namespace cricket { |
| 50 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 51 | // Enum for UMA metrics, used to record whether the channel is |
| 52 | // connected/connecting/disconnected when ICE restart happens. |
| 53 | enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE }; |
| 54 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 55 | static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 56 | |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 57 | bool IceCredentialsChanged(const std::string& old_ufrag, |
| 58 | const std::string& old_pwd, |
| 59 | const std::string& new_ufrag, |
| 60 | const std::string& new_pwd); |
| 61 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | // Adds the port on which the candidate originated. |
| 63 | class RemoteCandidate : public Candidate { |
| 64 | public: |
| 65 | RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 66 | : Candidate(c), origin_port_(origin_port) {} |
| 67 | |
| 68 | PortInterface* origin_port() { return origin_port_; } |
| 69 | |
| 70 | private: |
| 71 | PortInterface* origin_port_; |
| 72 | }; |
| 73 | |
| 74 | // P2PTransportChannel manages the candidates and connection process to keep |
| 75 | // two P2P clients connected to each other. |
Qingsi Wang | 502db3d | 2018-05-16 17:01:37 -0700 | [diff] [blame] | 76 | class P2PTransportChannel : public IceTransportInternal { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 77 | public: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 78 | P2PTransportChannel(const std::string& transport_name, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | int component, |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 80 | PortAllocator* allocator, |
| 81 | webrtc::RtcEventLog* event_log = nullptr); |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 82 | ~P2PTransportChannel() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 83 | |
| 84 | // From TransportChannelImpl: |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 85 | IceTransportState GetState() const override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 86 | const std::string& transport_name() const override; |
| 87 | int component() const override; |
| 88 | bool writable() const override; |
| 89 | bool receiving() const override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 90 | void SetIceRole(IceRole role) override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 91 | IceRole GetIceRole() const override; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 92 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 93 | void SetIceParameters(const IceParameters& ice_params) override; |
| 94 | void SetRemoteIceParameters(const IceParameters& ice_params) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 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; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 100 | IceGatheringState gathering_state() const override; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 101 | void AddRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 102 | void RemoveRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 103 | // Sets the parameters in IceConfig. We do not set them blindly. Instead, we |
| 104 | // only update the parameter if it is considered set in |config|. For example, |
| 105 | // a negative value of receiving_timeout will be considered "not set" and we |
| 106 | // will not use it to update the respective parameter in |config_|. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 107 | // TODO(deadbeef): Use absl::optional instead of negative values. |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 108 | void SetIceConfig(const IceConfig& config) override; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 109 | const IceConfig& config() const; |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 110 | static webrtc::RTCError ValidateIceConfig(const IceConfig& config); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 111 | void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override; |
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; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 120 | int GetError() override; |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 121 | bool GetStats(std::vector<ConnectionInfo>* candidate_pair_stats_list, |
| 122 | std::vector<CandidateStats>* candidate_stats_list) override; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 123 | absl::optional<int> GetRttEstimate() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | |
Honghai Zhang | 8cd7f22 | 2016-06-23 13:44:34 -0700 | [diff] [blame] | 125 | // TODO(honghaiz): Remove this method once the reference of it in |
| 126 | // Chromoting is removed. |
| 127 | const Connection* best_connection() const { return selected_connection_; } |
| 128 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 129 | const Connection* selected_connection() const { return selected_connection_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 130 | void set_incoming_only(bool value) { incoming_only_ = value; } |
| 131 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 132 | // Note: These are only for testing purpose. |
| 133 | // |ports_| and |pruned_ports| should not be changed from outside. |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 134 | const std::vector<PortInterface*>& ports() { return ports_; } |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 135 | const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 136 | |
| 137 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 138 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 139 | void PruneAllPorts(); |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 140 | int check_receiving_interval() const; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 141 | absl::optional<rtc::NetworkRoute> network_route() const override; |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 142 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 143 | // Helper method used only in unittest. |
| 144 | rtc::DiffServCodePoint DefaultDscpValue() const; |
| 145 | |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 146 | // Public for unit tests. |
| 147 | Connection* FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 148 | void MarkConnectionPinged(Connection* conn); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 149 | |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 150 | // Public for unit tests. |
| 151 | const std::vector<Connection*>& connections() const { return connections_; } |
| 152 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 153 | // Public for unit tests. |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 154 | PortAllocatorSession* allocator_session() const { |
| 155 | if (allocator_sessions_.empty()) { |
| 156 | return nullptr; |
| 157 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 158 | return allocator_sessions_.back().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | } |
| 160 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 161 | // Public for unit tests. |
| 162 | const std::vector<RemoteCandidate>& remote_candidates() const { |
| 163 | return remote_candidates_; |
| 164 | } |
| 165 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 166 | std::string ToString() const { |
| 167 | const char RECEIVING_ABBREV[2] = {'_', 'R'}; |
| 168 | const char WRITABLE_ABBREV[2] = {'_', 'W'}; |
| 169 | std::stringstream ss; |
| 170 | ss << "Channel[" << transport_name_ << "|" << component_ << "|" |
| 171 | << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]"; |
| 172 | return ss.str(); |
| 173 | } |
| 174 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 175 | private: |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 176 | rtc::Thread* thread() const { return network_thread_; } |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 177 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 178 | bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } |
| 179 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 180 | // A transport channel is weak if the current best connection is either |
| 181 | // 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] | 182 | bool weak() const; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 183 | |
| 184 | int weak_ping_interval() const { |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 185 | return std::max(config_.ice_check_interval_weak_connectivity_or_default(), |
| 186 | config_.ice_check_min_interval_or_default()); |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | int strong_ping_interval() const { |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 190 | return std::max(config_.ice_check_interval_strong_connectivity_or_default(), |
| 191 | config_.ice_check_min_interval_or_default()); |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 194 | // Returns true if it's possible to send packets on |connection|. |
| 195 | bool ReadyToSend(Connection* connection) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 196 | void UpdateConnectionStates(); |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 197 | void RequestSortAndStateUpdate(const std::string& reason_to_sort); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 198 | // Start pinging if we haven't already started, and we now have a connection |
| 199 | // that's pingable. |
| 200 | void MaybeStartPinging(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 201 | |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 202 | int CompareCandidatePairNetworks( |
| 203 | const Connection* a, |
| 204 | const Connection* b, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 205 | absl::optional<rtc::AdapterType> network_preference) const; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 206 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 207 | // The methods below return a positive value if |a| is preferable to |b|, |
| 208 | // a negative value if |b| is preferable, and 0 if they're equally preferable. |
| 209 | // If |receiving_unchanged_threshold| is set, then when |b| is receiving and |
| 210 | // |a| is not, returns a negative value only if |b| has been in receiving |
| 211 | // state and |a| has been in not receiving state since |
| 212 | // |receiving_unchanged_threshold| and sets |
| 213 | // |missed_receiving_unchanged_threshold| to true otherwise. |
| 214 | int CompareConnectionStates( |
| 215 | const cricket::Connection* a, |
| 216 | const cricket::Connection* b, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 217 | absl::optional<int64_t> receiving_unchanged_threshold, |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 218 | bool* missed_receiving_unchanged_threshold) const; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 219 | int CompareConnectionCandidates(const cricket::Connection* a, |
| 220 | const cricket::Connection* b) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 221 | // Compares two connections based on the connection states |
| 222 | // (writable/receiving/connected), nomination states, last data received time, |
| 223 | // and static preferences. Does not include latency. Used by both sorting |
| 224 | // and ShouldSwitchSelectedConnection(). |
| 225 | // Returns a positive value if |a| is better than |b|. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 226 | int CompareConnections(const cricket::Connection* a, |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 227 | const cricket::Connection* b, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 228 | absl::optional<int64_t> receiving_unchanged_threshold, |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 229 | bool* missed_receiving_unchanged_threshold) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 230 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 231 | bool PresumedWritable(const cricket::Connection* conn) const; |
| 232 | |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 233 | void SortConnectionsAndUpdateState(const std::string& reason_to_sort); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 234 | void SwitchSelectedConnection(Connection* conn); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 235 | void UpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 236 | void HandleAllTimedOut(); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 237 | void MaybeStopPortAllocatorSessions(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 238 | IceTransportState ComputeState() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 239 | |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 240 | Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 241 | bool CreateConnections(const Candidate& remote_candidate, |
| 242 | PortInterface* origin_port); |
| 243 | bool CreateConnection(PortInterface* port, |
| 244 | const Candidate& remote_candidate, |
| 245 | PortInterface* origin_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 246 | bool FindConnection(cricket::Connection* connection) const; |
| 247 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 248 | uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 249 | bool IsDuplicateRemoteCandidate(const Candidate& candidate); |
| 250 | void RememberRemoteCandidate(const Candidate& remote_candidate, |
| 251 | PortInterface* origin_port); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 252 | bool IsPingable(const Connection* conn, int64_t now) const; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 253 | // Whether a writable connection is past its ping interval and needs to be |
| 254 | // pinged again. |
| 255 | bool WritableConnectionPastPingInterval(const Connection* conn, |
| 256 | int64_t now) const; |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 257 | int CalculateActiveWritablePingInterval(const Connection* conn, |
| 258 | int64_t now) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 259 | void PingConnection(Connection* conn); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 260 | void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | void AddConnection(Connection* connection); |
| 262 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 263 | void OnPortReady(PortAllocatorSession* session, PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 264 | void OnPortsPruned(PortAllocatorSession* session, |
| 265 | const std::vector<PortInterface*>& ports); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 266 | void OnCandidatesReady(PortAllocatorSession* session, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 267 | const std::vector<Candidate>& candidates); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 268 | void OnCandidatesRemoved(PortAllocatorSession* session, |
| 269 | const std::vector<Candidate>& candidates); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 270 | void OnCandidatesAllocationDone(PortAllocatorSession* session); |
| 271 | void OnUnknownAddress(PortInterface* port, |
| 272 | const rtc::SocketAddress& addr, |
| 273 | ProtocolType proto, |
| 274 | IceMessage* stun_msg, |
| 275 | const std::string& remote_username, |
| 276 | bool port_muxed); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 277 | |
| 278 | // When a port is destroyed, remove it from both lists |ports_| |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 279 | // and |pruned_ports_|. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 280 | void OnPortDestroyed(PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 281 | // When pruning a port, move it from |ports_| to |pruned_ports_|. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 282 | // Returns true if the port is found and removed from |ports_|. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 283 | bool PrunePort(PortInterface* port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | void OnRoleConflict(PortInterface* port); |
| 285 | |
| 286 | void OnConnectionStateChange(Connection* connection); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 287 | void OnReadPacket(Connection* connection, |
| 288 | const char* data, |
| 289 | size_t len, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 290 | const rtc::PacketTime& packet_time); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 291 | void OnSentPacket(const rtc::SentPacket& sent_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 292 | void OnReadyToSend(Connection* connection); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 293 | void OnConnectionDestroyed(Connection* connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 294 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 295 | void OnNominated(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 296 | |
Qingsi Wang | 502db3d | 2018-05-16 17:01:37 -0700 | [diff] [blame] | 297 | void CheckAndPing(); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 298 | |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 299 | void LogCandidatePairConfig(Connection* conn, |
| 300 | webrtc::IceCandidatePairConfigType type); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 301 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 302 | uint32_t GetNominationAttr(Connection* conn) const; |
| 303 | bool GetUseCandidateAttr(Connection* conn, NominationMode mode) const; |
| 304 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 305 | // Returns true if we should switch to the new connection. |
| 306 | // sets |missed_receiving_unchanged_threshold| to true if either |
| 307 | // the selected connection or the new connection missed its |
| 308 | // receiving-unchanged-threshold. |
| 309 | bool ShouldSwitchSelectedConnection( |
| 310 | Connection* new_connection, |
| 311 | bool* missed_receiving_unchanged_threshold) const; |
| 312 | // Returns true if the new_connection is selected for transmission. |
| 313 | bool MaybeSwitchSelectedConnection(Connection* new_connection, |
| 314 | const std::string& reason); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 315 | // Gets the best connection for each network. |
| 316 | std::map<rtc::Network*, Connection*> GetBestConnectionByNetwork() const; |
| 317 | std::vector<Connection*> GetBestWritableConnectionPerNetwork() const; |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 318 | void PruneConnections(); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 319 | bool IsBackupConnection(const Connection* conn) const; |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 320 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 321 | Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 322 | // Between |conn1| and |conn2|, this function returns the one which should |
| 323 | // be pinged first. |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 324 | Connection* MorePingable(Connection* conn1, Connection* conn2); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 325 | // Select the connection which is Relay/Relay. If both of them are, |
| 326 | // UDP relay protocol takes precedence. |
| 327 | Connection* MostLikelyToWork(Connection* conn1, Connection* conn2); |
| 328 | // Compare the last_ping_sent time and return the one least recently pinged. |
| 329 | Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2); |
| 330 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 331 | // Returns the latest remote ICE parameters or nullptr if there are no remote |
| 332 | // ICE parameters yet. |
| 333 | IceParameters* remote_ice() { |
| 334 | return remote_ice_parameters_.empty() ? nullptr |
| 335 | : &remote_ice_parameters_.back(); |
| 336 | } |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 337 | // Returns the remote IceParameters and generation that match |ufrag| |
| 338 | // if found, and returns nullptr otherwise. |
| 339 | const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, |
| 340 | uint32_t* generation); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 341 | // Returns the index of the latest remote ICE parameters, or 0 if no remote |
| 342 | // ICE parameters have been received. |
| 343 | uint32_t remote_ice_generation() { |
| 344 | return remote_ice_parameters_.empty() |
| 345 | ? 0 |
| 346 | : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); |
| 347 | } |
| 348 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 349 | // Indicates if the given local port has been pruned. |
| 350 | bool IsPortPruned(const Port* port) const; |
| 351 | |
| 352 | // Indicates if the given remote candidate has been pruned. |
| 353 | bool IsRemoteCandidatePruned(const Candidate& cand) const; |
| 354 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 355 | // Sets the writable state, signaling if necessary. |
| 356 | void set_writable(bool writable); |
| 357 | // Sets the receiving state, signaling if necessary. |
| 358 | void set_receiving(bool receiving); |
| 359 | |
| 360 | std::string transport_name_; |
| 361 | int component_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 362 | PortAllocator* allocator_; |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 363 | rtc::Thread* network_thread_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 364 | bool incoming_only_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 365 | int error_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 366 | std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 367 | // |ports_| contains ports that are used to form new connections when |
| 368 | // new remote candidates are added. |
| 369 | std::vector<PortInterface*> ports_; |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 370 | // |pruned_ports_| contains ports that have been removed from |ports_| and |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 371 | // are not being used to form new connections, but that aren't yet destroyed. |
| 372 | // They may have existing connections, and they still fire signals such as |
| 373 | // SignalUnknownAddress. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 374 | std::vector<PortInterface*> pruned_ports_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 375 | |
| 376 | // |connections_| is a sorted list with the first one always be the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 377 | // |selected_connection_| when it's not nullptr. The combination of |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 378 | // |pinged_connections_| and |unpinged_connections_| has the same |
| 379 | // connections as |connections_|. These 2 sets maintain whether a |
| 380 | // connection should be pinged next or not. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 381 | std::vector<Connection*> connections_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 382 | std::set<Connection*> pinged_connections_; |
| 383 | std::set<Connection*> unpinged_connections_; |
| 384 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 385 | Connection* selected_connection_ = nullptr; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 386 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 387 | std::vector<RemoteCandidate> remote_candidates_; |
| 388 | bool sort_dirty_; // indicates whether another sort is needed right now |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 389 | bool had_connection_ = false; // if connections_ has ever been nonempty |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 390 | typedef std::map<rtc::Socket::Option, int> OptionMap; |
| 391 | OptionMap options_; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 392 | IceParameters ice_parameters_; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 393 | std::vector<IceParameters> remote_ice_parameters_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 394 | IceMode remote_ice_mode_; |
| 395 | IceRole ice_role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 396 | uint64_t tiebreaker_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 397 | IceGatheringState gathering_state_; |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 398 | std::unique_ptr<webrtc::BasicRegatheringController> regathering_controller_; |
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; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 401 | IceTransportState state_ = IceTransportState::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; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 405 | // The value put in the "nomination" attribute for the next nominated |
| 406 | // connection. A zero-value indicates the connection will not be nominated. |
| 407 | uint32_t nomination_ = 0; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 408 | bool receiving_ = false; |
| 409 | bool writable_ = false; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 410 | |
Qingsi Wang | 502db3d | 2018-05-16 17:01:37 -0700 | [diff] [blame] | 411 | rtc::AsyncInvoker invoker_; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 412 | webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 413 | absl::optional<rtc::NetworkRoute> network_route_; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 414 | webrtc::IceEventLog ice_event_log_; |
| 415 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 416 | RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | } // namespace cricket |
| 420 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 421 | #endif // P2P_BASE_P2PTRANSPORTCHANNEL_H_ |