blob: 7ce0651c9a3e9e6fc017ca1adf2fd7c67a0beafd [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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//
Steve Anton10542f22019-01-11 09:11:00 -080020#ifndef P2P_BASE_P2P_TRANSPORT_CHANNEL_H_
21#define P2P_BASE_P2P_TRANSPORT_CHANNEL_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022
Qingsi Wange6826d22018-03-08 14:55:14 -080023#include <algorithm>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070025#include <memory>
guoweis36f01372016-03-02 18:02:40 -080026#include <set>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include <string>
28#include <vector>
kwiberg4485ffb2016-04-26 08:14:39 -070029
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/async_resolver_factory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020031#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "api/rtc_error.h"
Qingsi Wang93a84392018-01-30 17:13:09 -080033#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "logging/rtc_event_log/ice_logger.h"
35#include "p2p/base/candidate_pair_interface.h"
36#include "p2p/base/ice_transport_internal.h"
37#include "p2p/base/p2p_constants.h"
38#include "p2p/base/port_allocator.h"
39#include "p2p/base/port_interface.h"
40#include "p2p/base/regathering_controller.h"
41#include "rtc_base/async_invoker.h"
42#include "rtc_base/async_packet_socket.h"
43#include "rtc_base/constructor_magic.h"
Jonas Olsson941a07c2018-09-13 10:07:07 +020044#include "rtc_base/strings/string_builder.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020045#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020046#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand36bc4f82019-05-27 15:49:31 +020047#include "rtc_base/thread_annotations.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048
Qingsi Wang93a84392018-01-30 17:13:09 -080049namespace webrtc {
50class RtcEventLog;
51} // namespace webrtc
52
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053namespace cricket {
54
Honghai Zhangd93f50c2016-10-05 11:47:22 -070055// Enum for UMA metrics, used to record whether the channel is
56// connected/connecting/disconnected when ICE restart happens.
57enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE };
58
honghaiz524ecc22016-05-25 12:48:31 -070059static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3;
guoweisb0bb77f2015-10-26 15:10:01 -070060
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080061bool IceCredentialsChanged(const std::string& old_ufrag,
62 const std::string& old_pwd,
63 const std::string& new_ufrag,
64 const std::string& new_pwd);
65
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066// Adds the port on which the candidate originated.
67class RemoteCandidate : public Candidate {
68 public:
69 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
70 : Candidate(c), origin_port_(origin_port) {}
71
72 PortInterface* origin_port() { return origin_port_; }
73
74 private:
75 PortInterface* origin_port_;
76};
77
Jonas Oreland4af78822019-10-11 08:17:06 +020078struct IceFieldTrials {
79 bool skip_relay_to_non_relay_connections = false;
Jonas Orelandc6404a12019-10-14 15:52:15 +020080 absl::optional<int> max_outstanding_pings;
Jonas Oreland1230fb72019-10-25 15:58:14 +020081
82 // Wait X ms before selecting a connection when having none.
83 // This will make media slower, but will give us chance to find
84 // a better connection before starting.
85 absl::optional<int> initial_select_dampening;
86
87 // If the connection has recevied a ping-request, delay by
88 // maximum this delay. This will make media slower, but will
89 // give us chance to find a better connection before starting.
90 absl::optional<int> initial_select_dampening_ping_received;
Jonas Oreland4af78822019-10-11 08:17:06 +020091};
92
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093// P2PTransportChannel manages the candidates and connection process to keep
94// two P2P clients connected to each other.
Mirko Bonadeiac194142018-10-22 17:08:37 +020095class RTC_EXPORT P2PTransportChannel : public IceTransportInternal {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 public:
Zach Steine20867f2018-08-02 13:20:15 -070097 // For testing only.
98 // TODO(zstein): Remove once AsyncResolverFactory is required.
99 P2PTransportChannel(const std::string& transport_name,
100 int component,
101 PortAllocator* allocator);
deadbeefcbecd352015-09-23 11:50:27 -0700102 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 int component,
Qingsi Wang93a84392018-01-30 17:13:09 -0800104 PortAllocator* allocator,
Zach Steine20867f2018-08-02 13:20:15 -0700105 webrtc::AsyncResolverFactory* async_resolver_factory,
Qingsi Wang93a84392018-01-30 17:13:09 -0800106 webrtc::RtcEventLog* event_log = nullptr);
Steve Anton33f69db2017-10-30 10:01:15 -0700107 ~P2PTransportChannel() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108
109 // From TransportChannelImpl:
zhihuangd06adf62017-01-12 15:58:31 -0800110 IceTransportState GetState() const override;
Jonas Olsson81125f02018-10-09 10:52:04 +0200111 webrtc::IceTransportState GetIceTransportState() const override;
112
Steve Anton33f69db2017-10-30 10:01:15 -0700113 const std::string& transport_name() const override;
114 int component() const override;
115 bool writable() const override;
116 bool receiving() const override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200117 void SetIceRole(IceRole role) override;
Steve Anton33f69db2017-10-30 10:01:15 -0700118 IceRole GetIceRole() const override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200119 void SetIceTiebreaker(uint64_t tiebreaker) override;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700120 void SetIceParameters(const IceParameters& ice_params) override;
121 void SetRemoteIceParameters(const IceParameters& ice_params) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200122 void SetRemoteIceMode(IceMode mode) override;
deadbeef886815b2016-06-29 15:21:04 -0700123 // TODO(deadbeef): Deprecated. Remove when Chromium's
124 // IceTransportChannel does not depend on this.
125 void Connect() {}
deadbeefcbecd352015-09-23 11:50:27 -0700126 void MaybeStartGathering() override;
Steve Anton33f69db2017-10-30 10:01:15 -0700127 IceGatheringState gathering_state() const override;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700128 void ResolveHostnameCandidate(const Candidate& candidate);
deadbeefcbecd352015-09-23 11:50:27 -0700129 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700130 void RemoveRemoteCandidate(const Candidate& candidate) override;
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100131 void RemoveAllRemoteCandidates() override;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800132 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
133 // only update the parameter if it is considered set in |config|. For example,
134 // a negative value of receiving_timeout will be considered "not set" and we
135 // will not use it to update the respective parameter in |config_|.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200136 // TODO(deadbeef): Use absl::optional instead of negative values.
honghaiz1f429e32015-09-28 07:57:34 -0700137 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -0800138 const IceConfig& config() const;
Qingsi Wangdea68892018-03-27 10:55:21 -0700139 static webrtc::RTCError ValidateIceConfig(const IceConfig& config);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140
141 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700142 int SendPacket(const char* data,
143 size_t len,
144 const rtc::PacketOptions& options,
145 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200146 int SetOption(rtc::Socket::Option opt, int value) override;
147 bool GetOption(rtc::Socket::Option opt, int* value) override;
Steve Anton33f69db2017-10-30 10:01:15 -0700148 int GetError() override;
Jonas Oreland149dc722019-08-28 08:10:27 +0200149 bool GetStats(IceTransportStats* ice_transport_stats) override;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200150 absl::optional<int> GetRttEstimate() override;
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100151 const Connection* selected_connection() const override;
Qingsi Wange5defb12019-08-15 11:01:53 -0700152 absl::optional<const CandidatePair> GetSelectedCandidatePair() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153
Honghai Zhang8cd7f222016-06-23 13:44:34 -0700154 // TODO(honghaiz): Remove this method once the reference of it in
155 // Chromoting is removed.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200156 const Connection* best_connection() const {
157 RTC_DCHECK_RUN_ON(network_thread_);
158 return selected_connection_;
159 }
Honghai Zhang8cd7f222016-06-23 13:44:34 -0700160
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200161 void set_incoming_only(bool value) {
162 RTC_DCHECK_RUN_ON(network_thread_);
163 incoming_only_ = value;
164 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165
Honghai Zhanga74363c2016-07-28 18:06:15 -0700166 // Note: These are only for testing purpose.
167 // |ports_| and |pruned_ports| should not be changed from outside.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200168 const std::vector<PortInterface*>& ports() {
169 RTC_DCHECK_RUN_ON(network_thread_);
170 return ports_;
171 }
172 const std::vector<PortInterface*>& pruned_ports() {
173 RTC_DCHECK_RUN_ON(network_thread_);
174 return pruned_ports_;
175 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200177 IceMode remote_ice_mode() const {
178 RTC_DCHECK_RUN_ON(network_thread_);
179 return remote_ice_mode_;
180 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181
Honghai Zhanga74363c2016-07-28 18:06:15 -0700182 void PruneAllPorts();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700183 int check_receiving_interval() const;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200184 absl::optional<rtc::NetworkRoute> network_route() const override;
Zhi Huang942bc2e2017-11-13 13:26:07 -0800185
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 // Helper method used only in unittest.
187 rtc::DiffServCodePoint DefaultDscpValue() const;
188
Peter Thatcher7351f462015-04-02 16:39:16 -0700189 // Public for unit tests.
190 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800191 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700192
honghaiz77d0d6e2015-10-27 11:34:45 -0700193 // Public for unit tests.
194 const std::vector<Connection*>& connections() const { return connections_; }
195
honghaiz9b669572015-11-04 12:07:44 -0800196 // Public for unit tests.
Qingsi Wang1b368942018-06-13 13:54:08 -0700197 PortAllocatorSession* allocator_session() const {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200198 RTC_DCHECK_RUN_ON(network_thread_);
Qingsi Wang1b368942018-06-13 13:54:08 -0700199 if (allocator_sessions_.empty()) {
200 return nullptr;
201 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700202 return allocator_sessions_.back().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203 }
204
honghaiz112fe432015-12-30 13:32:47 -0800205 // Public for unit tests.
206 const std::vector<RemoteCandidate>& remote_candidates() const {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200207 RTC_DCHECK_RUN_ON(network_thread_);
honghaiz112fe432015-12-30 13:32:47 -0800208 return remote_candidates_;
209 }
210
zhihuangd06adf62017-01-12 15:58:31 -0800211 std::string ToString() const {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200212 RTC_DCHECK_RUN_ON(network_thread_);
Jonas Olsson941a07c2018-09-13 10:07:07 +0200213 const std::string RECEIVING_ABBREV[2] = {"_", "R"};
214 const std::string WRITABLE_ABBREV[2] = {"_", "W"};
215 rtc::StringBuilder ss;
zhihuangd06adf62017-01-12 15:58:31 -0800216 ss << "Channel[" << transport_name_ << "|" << component_ << "|"
217 << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200218 return ss.Release();
zhihuangd06adf62017-01-12 15:58:31 -0800219 }
220
honghaiz9b669572015-11-04 12:07:44 -0800221 private:
johan0fd22ef2016-09-29 01:19:20 -0700222 rtc::Thread* thread() const { return network_thread_; }
Qingsi Wang1b368942018-06-13 13:54:08 -0700223
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200224 bool IsGettingPorts() {
225 RTC_DCHECK_RUN_ON(network_thread_);
226 return allocator_session()->IsGettingPorts();
227 }
honghaiz9b669572015-11-04 12:07:44 -0800228
honghaiza58ea782015-09-24 08:13:36 -0700229 // A transport channel is weak if the current best connection is either
230 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700231 bool weak() const;
skvlad51072462017-02-02 11:50:14 -0800232
233 int weak_ping_interval() const {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200234 RTC_DCHECK_RUN_ON(network_thread_);
Qingsi Wang866e08d2018-03-22 17:54:23 -0700235 return std::max(config_.ice_check_interval_weak_connectivity_or_default(),
236 config_.ice_check_min_interval_or_default());
skvlad51072462017-02-02 11:50:14 -0800237 }
238
239 int strong_ping_interval() const {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200240 RTC_DCHECK_RUN_ON(network_thread_);
Qingsi Wang866e08d2018-03-22 17:54:23 -0700241 return std::max(config_.ice_check_interval_strong_connectivity_or_default(),
242 config_.ice_check_min_interval_or_default());
skvlad51072462017-02-02 11:50:14 -0800243 }
244
Honghai Zhange05bcc22016-08-16 18:19:14 -0700245 // Returns true if it's possible to send packets on |connection|.
246 bool ReadyToSend(Connection* connection) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 void UpdateConnectionStates();
Qingsi Wang10a0e512018-05-16 13:37:03 -0700248 void RequestSortAndStateUpdate(const std::string& reason_to_sort);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700249 // Start pinging if we haven't already started, and we now have a connection
250 // that's pingable.
251 void MaybeStartPinging();
deadbeef14f97f52016-06-22 17:14:15 -0700252
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800253 int CompareCandidatePairNetworks(
254 const Connection* a,
255 const Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200256 absl::optional<rtc::AdapterType> network_preference) const;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800257
honghaiz9ad0db52016-07-14 19:30:28 -0700258 // The methods below return a positive value if |a| is preferable to |b|,
259 // a negative value if |b| is preferable, and 0 if they're equally preferable.
260 // If |receiving_unchanged_threshold| is set, then when |b| is receiving and
261 // |a| is not, returns a negative value only if |b| has been in receiving
262 // state and |a| has been in not receiving state since
263 // |receiving_unchanged_threshold| and sets
264 // |missed_receiving_unchanged_threshold| to true otherwise.
265 int CompareConnectionStates(
266 const cricket::Connection* a,
267 const cricket::Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200268 absl::optional<int64_t> receiving_unchanged_threshold,
honghaiz9ad0db52016-07-14 19:30:28 -0700269 bool* missed_receiving_unchanged_threshold) const;
deadbeef14f97f52016-06-22 17:14:15 -0700270 int CompareConnectionCandidates(const cricket::Connection* a,
271 const cricket::Connection* b) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700272 // Compares two connections based on the connection states
273 // (writable/receiving/connected), nomination states, last data received time,
274 // and static preferences. Does not include latency. Used by both sorting
275 // and ShouldSwitchSelectedConnection().
276 // Returns a positive value if |a| is better than |b|.
deadbeef14f97f52016-06-22 17:14:15 -0700277 int CompareConnections(const cricket::Connection* a,
honghaiz9ad0db52016-07-14 19:30:28 -0700278 const cricket::Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200279 absl::optional<int64_t> receiving_unchanged_threshold,
honghaiz9ad0db52016-07-14 19:30:28 -0700280 bool* missed_receiving_unchanged_threshold) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700281
deadbeef14f97f52016-06-22 17:14:15 -0700282 bool PresumedWritable(const cricket::Connection* conn) const;
283
Qingsi Wang10a0e512018-05-16 13:37:03 -0700284 void SortConnectionsAndUpdateState(const std::string& reason_to_sort);
Alex Drake00c7ecf2019-08-06 10:54:47 -0700285 void SwitchSelectedConnection(Connection* conn, const std::string& reason);
Honghai Zhang381b4212015-12-04 12:24:03 -0800286 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000287 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800288 void MaybeStopPortAllocatorSessions();
Jonas Olsson81125f02018-10-09 10:52:04 +0200289
290 // ComputeIceTransportState computes the RTCIceTransportState as described in
291 // https://w3c.github.io/webrtc-pc/#dom-rtcicetransportstate. ComputeState
292 // computes the value we currently export as RTCIceTransportState.
293 // TODO(bugs.webrtc.org/9308): Remove ComputeState once it's no longer used.
zhihuangd06adf62017-01-12 15:58:31 -0800294 IceTransportState ComputeState() const;
Jonas Olsson81125f02018-10-09 10:52:04 +0200295 webrtc::IceTransportState ComputeIceTransportState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296
sprang716978d2016-10-11 06:43:28 -0700297 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700298 bool CreateConnections(const Candidate& remote_candidate,
299 PortInterface* origin_port);
300 bool CreateConnection(PortInterface* port,
301 const Candidate& remote_candidate,
302 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000303 bool FindConnection(cricket::Connection* connection) const;
304
Peter Boström0c4e06b2015-10-07 12:23:21 +0200305 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000306 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
307 void RememberRemoteCandidate(const Candidate& remote_candidate,
308 PortInterface* origin_port);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700309 bool IsPingable(const Connection* conn, int64_t now) const;
honghaiz7252a002016-11-08 20:04:09 -0800310 // Whether a writable connection is past its ping interval and needs to be
311 // pinged again.
312 bool WritableConnectionPastPingInterval(const Connection* conn,
313 int64_t now) const;
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700314 int CalculateActiveWritablePingInterval(const Connection* conn,
315 int64_t now) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000316 void PingConnection(Connection* conn);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700317 void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000318 void AddConnection(Connection* connection);
319
Yves Gerey665174f2018-06-19 15:03:05 +0200320 void OnPortReady(PortAllocatorSession* session, PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700321 void OnPortsPruned(PortAllocatorSession* session,
322 const std::vector<PortInterface*>& ports);
Yves Gerey665174f2018-06-19 15:03:05 +0200323 void OnCandidatesReady(PortAllocatorSession* session,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000324 const std::vector<Candidate>& candidates);
Eldar Relloda13ea22019-06-01 12:23:43 +0300325 void OnCandidateError(PortAllocatorSession* session,
326 const IceCandidateErrorEvent& event);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700327 void OnCandidatesRemoved(PortAllocatorSession* session,
328 const std::vector<Candidate>& candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000329 void OnCandidatesAllocationDone(PortAllocatorSession* session);
330 void OnUnknownAddress(PortInterface* port,
331 const rtc::SocketAddress& addr,
332 ProtocolType proto,
333 IceMessage* stun_msg,
334 const std::string& remote_username,
335 bool port_muxed);
Qingsi Wangc129c352019-04-18 10:41:58 -0700336 void OnCandidateFilterChanged(uint32_t prev_filter, uint32_t cur_filter);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700337
338 // When a port is destroyed, remove it from both lists |ports_|
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700339 // and |pruned_ports_|.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000340 void OnPortDestroyed(PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700341 // When pruning a port, move it from |ports_| to |pruned_ports_|.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700342 // Returns true if the port is found and removed from |ports_|.
Honghai Zhanga74363c2016-07-28 18:06:15 -0700343 bool PrunePort(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 void OnRoleConflict(PortInterface* port);
345
346 void OnConnectionStateChange(Connection* connection);
Yves Gerey665174f2018-06-19 15:03:05 +0200347 void OnReadPacket(Connection* connection,
348 const char* data,
349 size_t len,
Niels Möllere6933812018-11-05 13:01:41 +0100350 int64_t packet_time_us);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100351 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000352 void OnReadyToSend(Connection* connection);
Yves Gerey665174f2018-06-19 15:03:05 +0200353 void OnConnectionDestroyed(Connection* connection);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000354
honghaiz5a3acd82015-08-20 15:53:17 -0700355 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000356
Qingsi Wang502db3d2018-05-16 17:01:37 -0700357 void CheckAndPing();
Peter Thatcher54360512015-07-08 11:08:35 -0700358
Bjorn Terelius59b4e3e2018-05-30 17:14:08 +0200359 void LogCandidatePairConfig(Connection* conn,
360 webrtc::IceCandidatePairConfigType type);
Qingsi Wang93a84392018-01-30 17:13:09 -0800361
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700362 uint32_t GetNominationAttr(Connection* conn) const;
363 bool GetUseCandidateAttr(Connection* conn, NominationMode mode) const;
364
honghaiz9ad0db52016-07-14 19:30:28 -0700365 // Returns true if we should switch to the new connection.
366 // sets |missed_receiving_unchanged_threshold| to true if either
367 // the selected connection or the new connection missed its
368 // receiving-unchanged-threshold.
369 bool ShouldSwitchSelectedConnection(
370 Connection* new_connection,
371 bool* missed_receiving_unchanged_threshold) const;
372 // Returns true if the new_connection is selected for transmission.
373 bool MaybeSwitchSelectedConnection(Connection* new_connection,
374 const std::string& reason);
honghaiz7252a002016-11-08 20:04:09 -0800375 // Gets the best connection for each network.
376 std::map<rtc::Network*, Connection*> GetBestConnectionByNetwork() const;
377 std::vector<Connection*> GetBestWritableConnectionPerNetwork() const;
honghaiz5a3acd82015-08-20 15:53:17 -0700378 void PruneConnections();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700379 bool IsBackupConnection(const Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700380
honghaiz34b11eb2016-03-16 08:55:44 -0700381 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800382 // Between |conn1| and |conn2|, this function returns the one which should
383 // be pinged first.
honghaiz7252a002016-11-08 20:04:09 -0800384 Connection* MorePingable(Connection* conn1, Connection* conn2);
guoweis36f01372016-03-02 18:02:40 -0800385 // Select the connection which is Relay/Relay. If both of them are,
386 // UDP relay protocol takes precedence.
387 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
388 // Compare the last_ping_sent time and return the one least recently pinged.
389 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
390
honghaiza54a0802015-12-16 18:37:23 -0800391 // Returns the latest remote ICE parameters or nullptr if there are no remote
392 // ICE parameters yet.
393 IceParameters* remote_ice() {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200394 RTC_DCHECK_RUN_ON(network_thread_);
honghaiza54a0802015-12-16 18:37:23 -0800395 return remote_ice_parameters_.empty() ? nullptr
396 : &remote_ice_parameters_.back();
397 }
honghaiz112fe432015-12-30 13:32:47 -0800398 // Returns the remote IceParameters and generation that match |ufrag|
399 // if found, and returns nullptr otherwise.
400 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
401 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800402 // Returns the index of the latest remote ICE parameters, or 0 if no remote
403 // ICE parameters have been received.
404 uint32_t remote_ice_generation() {
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200405 RTC_DCHECK_RUN_ON(network_thread_);
honghaiza54a0802015-12-16 18:37:23 -0800406 return remote_ice_parameters_.empty()
407 ? 0
408 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
409 }
410
Steve Anton300bf8e2017-07-14 10:13:10 -0700411 // Indicates if the given local port has been pruned.
412 bool IsPortPruned(const Port* port) const;
413
414 // Indicates if the given remote candidate has been pruned.
415 bool IsRemoteCandidatePruned(const Candidate& cand) const;
416
zhihuangd06adf62017-01-12 15:58:31 -0800417 // Sets the writable state, signaling if necessary.
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100418 void SetWritable(bool writable);
zhihuangd06adf62017-01-12 15:58:31 -0800419 // Sets the receiving state, signaling if necessary.
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100420 void SetReceiving(bool receiving);
Qingsi Wang7627fdd2019-08-19 16:07:40 -0700421 // Clears the address and the related address fields of a local candidate to
422 // avoid IP leakage. This is applicable in several scenarios as commented in
423 // |PortAllocator::SanitizeCandidate|.
424 Candidate SanitizeLocalCandidate(const Candidate& c) const;
425 // Clears the address field of a remote candidate to avoid IP leakage. This is
426 // applicable in the following scenarios:
427 // 1. mDNS candidates are received.
428 // 2. Peer-reflexive remote candidates.
429 Candidate SanitizeRemoteCandidate(const Candidate& c) const;
zhihuangd06adf62017-01-12 15:58:31 -0800430
Jonas Oreland1230fb72019-10-25 15:58:14 +0200431 bool HandleInitialSelectDampening(Connection* new_connection,
432 const std::string& reason);
433
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200434 std::string transport_name_ RTC_GUARDED_BY(network_thread_);
435 int component_ RTC_GUARDED_BY(network_thread_);
436 PortAllocator* allocator_ RTC_GUARDED_BY(network_thread_);
437 webrtc::AsyncResolverFactory* async_resolver_factory_
438 RTC_GUARDED_BY(network_thread_);
johan0fd22ef2016-09-29 01:19:20 -0700439 rtc::Thread* network_thread_;
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200440 bool incoming_only_ RTC_GUARDED_BY(network_thread_);
441 int error_ RTC_GUARDED_BY(network_thread_);
442 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_
443 RTC_GUARDED_BY(network_thread_);
deadbeefdfc42442016-06-21 14:19:48 -0700444 // |ports_| contains ports that are used to form new connections when
445 // new remote candidates are added.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200446 std::vector<PortInterface*> ports_ RTC_GUARDED_BY(network_thread_);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700447 // |pruned_ports_| contains ports that have been removed from |ports_| and
deadbeefdfc42442016-06-21 14:19:48 -0700448 // are not being used to form new connections, but that aren't yet destroyed.
449 // They may have existing connections, and they still fire signals such as
450 // SignalUnknownAddress.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200451 std::vector<PortInterface*> pruned_ports_ RTC_GUARDED_BY(network_thread_);
guoweis36f01372016-03-02 18:02:40 -0800452
453 // |connections_| is a sorted list with the first one always be the
Honghai Zhang572b0942016-06-23 12:26:57 -0700454 // |selected_connection_| when it's not nullptr. The combination of
guoweis36f01372016-03-02 18:02:40 -0800455 // |pinged_connections_| and |unpinged_connections_| has the same
456 // connections as |connections_|. These 2 sets maintain whether a
457 // connection should be pinged next or not.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200458 std::vector<Connection*> connections_ RTC_GUARDED_BY(network_thread_);
459 std::set<Connection*> pinged_connections_ RTC_GUARDED_BY(network_thread_);
460 std::set<Connection*> unpinged_connections_ RTC_GUARDED_BY(network_thread_);
guoweis36f01372016-03-02 18:02:40 -0800461
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200462 Connection* selected_connection_ RTC_GUARDED_BY(network_thread_) = nullptr;
guoweis36f01372016-03-02 18:02:40 -0800463
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200464 std::vector<RemoteCandidate> remote_candidates_
465 RTC_GUARDED_BY(network_thread_);
466 bool sort_dirty_ RTC_GUARDED_BY(
467 network_thread_); // indicates whether another sort is needed right now
468 bool had_connection_ RTC_GUARDED_BY(network_thread_) =
469 false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000470 typedef std::map<rtc::Socket::Option, int> OptionMap;
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200471 OptionMap options_ RTC_GUARDED_BY(network_thread_);
472 IceParameters ice_parameters_ RTC_GUARDED_BY(network_thread_);
473 std::vector<IceParameters> remote_ice_parameters_
474 RTC_GUARDED_BY(network_thread_);
475 IceMode remote_ice_mode_ RTC_GUARDED_BY(network_thread_);
476 IceRole ice_role_ RTC_GUARDED_BY(network_thread_);
477 uint64_t tiebreaker_ RTC_GUARDED_BY(network_thread_);
478 IceGatheringState gathering_state_ RTC_GUARDED_BY(network_thread_);
479 std::unique_ptr<webrtc::BasicRegatheringController> regathering_controller_
480 RTC_GUARDED_BY(network_thread_);
481 int64_t last_ping_sent_ms_ RTC_GUARDED_BY(network_thread_) = 0;
482 int weak_ping_interval_ RTC_GUARDED_BY(network_thread_) = WEAK_PING_INTERVAL;
Jonas Olsson81125f02018-10-09 10:52:04 +0200483 // TODO(jonasolsson): Remove state_ and rename standardized_state_ once state_
484 // is no longer used to compute the ICE connection state.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200485 IceTransportState state_ RTC_GUARDED_BY(network_thread_) =
486 IceTransportState::STATE_INIT;
487 webrtc::IceTransportState standardized_state_
488 RTC_GUARDED_BY(network_thread_) = webrtc::IceTransportState::kNew;
489 IceConfig config_ RTC_GUARDED_BY(network_thread_);
490 int last_sent_packet_id_ RTC_GUARDED_BY(network_thread_) =
491 -1; // -1 indicates no packet was sent before.
492 bool started_pinging_ RTC_GUARDED_BY(network_thread_) = false;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700493 // The value put in the "nomination" attribute for the next nominated
494 // connection. A zero-value indicates the connection will not be nominated.
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200495 uint32_t nomination_ RTC_GUARDED_BY(network_thread_) = 0;
496 bool receiving_ RTC_GUARDED_BY(network_thread_) = false;
497 bool writable_ RTC_GUARDED_BY(network_thread_) = false;
498 bool has_been_writable_ RTC_GUARDED_BY(network_thread_) =
499 false; // if writable_ has ever been true
Peter Thatcher54360512015-07-08 11:08:35 -0700500
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200501 rtc::AsyncInvoker invoker_ RTC_GUARDED_BY(network_thread_);
502 absl::optional<rtc::NetworkRoute> network_route_
503 RTC_GUARDED_BY(network_thread_);
504 webrtc::IceEventLog ice_event_log_ RTC_GUARDED_BY(network_thread_);
Qingsi Wang93a84392018-01-30 17:13:09 -0800505
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700506 struct CandidateAndResolver final {
507 CandidateAndResolver(const Candidate& candidate,
508 rtc::AsyncResolverInterface* resolver);
509 ~CandidateAndResolver();
510 Candidate candidate_;
511 rtc::AsyncResolverInterface* resolver_;
512 };
Harald Alvestrand36bc4f82019-05-27 15:49:31 +0200513 std::vector<CandidateAndResolver> resolvers_ RTC_GUARDED_BY(network_thread_);
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700514 void FinishAddingRemoteCandidate(const Candidate& new_remote_candidate);
515 void OnCandidateResolved(rtc::AsyncResolverInterface* resolver);
516 void AddRemoteCandidateWithResolver(Candidate candidate,
517 rtc::AsyncResolverInterface* resolver);
518
Jonas Oreland149dc722019-08-28 08:10:27 +0200519 // Number of times the selected_connection_ has been modified.
520 uint32_t selected_candidate_pair_changes_ = 0;
521
Jonas Oreland4af78822019-10-11 08:17:06 +0200522 IceFieldTrials field_trials_;
523
Jonas Oreland1230fb72019-10-25 15:58:14 +0200524 // Timestamp for when we got the first selectable connection.
525 int64_t initial_select_timestamp_ms_ = 0;
526
henrikg3c089d72015-09-16 05:37:44 -0700527 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000528};
529
530} // namespace cricket
531
Steve Anton10542f22019-01-11 09:11:00 -0800532#endif // P2P_BASE_P2P_TRANSPORT_CHANNEL_H_