blob: 1ac898e490283918042b5bdfb1e4b26b38862e71 [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//
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#ifndef P2P_BASE_P2PTRANSPORTCHANNEL_H_
21#define P2P_BASE_P2PTRANSPORTCHANNEL_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
Zach Steine20867f2018-08-02 13:20:15 -070030#include "api/asyncresolverfactory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020031#include "api/candidate.h"
Qingsi Wangdea68892018-03-27 10:55:21 -070032#include "api/rtcerror.h"
Qingsi Wang93a84392018-01-30 17:13:09 -080033#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
34#include "logging/rtc_event_log/icelogger.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "p2p/base/candidatepairinterface.h"
36#include "p2p/base/icetransportinternal.h"
Qingsi Wang866e08d2018-03-22 17:54:23 -070037#include "p2p/base/p2pconstants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/portallocator.h"
39#include "p2p/base/portinterface.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070040#include "p2p/base/regatheringcontroller.h"
Qingsi Wang502db3d2018-05-16 17:01:37 -070041#include "rtc_base/asyncinvoker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/asyncpacketsocket.h"
43#include "rtc_base/constructormagic.h"
Artem Titove41c4332018-07-25 15:04:28 +020044#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045
Qingsi Wang93a84392018-01-30 17:13:09 -080046namespace webrtc {
47class RtcEventLog;
48} // namespace webrtc
49
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050namespace cricket {
51
Honghai Zhangd93f50c2016-10-05 11:47:22 -070052// Enum for UMA metrics, used to record whether the channel is
53// connected/connecting/disconnected when ICE restart happens.
54enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE };
55
honghaiz524ecc22016-05-25 12:48:31 -070056static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3;
guoweisb0bb77f2015-10-26 15:10:01 -070057
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080058bool IceCredentialsChanged(const std::string& old_ufrag,
59 const std::string& old_pwd,
60 const std::string& new_ufrag,
61 const std::string& new_pwd);
62
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063// Adds the port on which the candidate originated.
64class RemoteCandidate : public Candidate {
65 public:
66 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
67 : Candidate(c), origin_port_(origin_port) {}
68
69 PortInterface* origin_port() { return origin_port_; }
70
71 private:
72 PortInterface* origin_port_;
73};
74
75// P2PTransportChannel manages the candidates and connection process to keep
76// two P2P clients connected to each other.
Qingsi Wang502db3d2018-05-16 17:01:37 -070077class P2PTransportChannel : public IceTransportInternal {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078 public:
Zach Steine20867f2018-08-02 13:20:15 -070079 // For testing only.
80 // TODO(zstein): Remove once AsyncResolverFactory is required.
81 P2PTransportChannel(const std::string& transport_name,
82 int component,
83 PortAllocator* allocator);
deadbeefcbecd352015-09-23 11:50:27 -070084 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085 int component,
Qingsi Wang93a84392018-01-30 17:13:09 -080086 PortAllocator* allocator,
Zach Steine20867f2018-08-02 13:20:15 -070087 webrtc::AsyncResolverFactory* async_resolver_factory,
Qingsi Wang93a84392018-01-30 17:13:09 -080088 webrtc::RtcEventLog* event_log = nullptr);
Steve Anton33f69db2017-10-30 10:01:15 -070089 ~P2PTransportChannel() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090
91 // From TransportChannelImpl:
zhihuangd06adf62017-01-12 15:58:31 -080092 IceTransportState GetState() const override;
Steve Anton33f69db2017-10-30 10:01:15 -070093 const std::string& transport_name() const override;
94 int component() const override;
95 bool writable() const override;
96 bool receiving() const override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020097 void SetIceRole(IceRole role) override;
Steve Anton33f69db2017-10-30 10:01:15 -070098 IceRole GetIceRole() const override;
Peter Boström0c4e06b2015-10-07 12:23:21 +020099 void SetIceTiebreaker(uint64_t tiebreaker) override;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700100 void SetIceParameters(const IceParameters& ice_params) override;
101 void SetRemoteIceParameters(const IceParameters& ice_params) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200102 void SetRemoteIceMode(IceMode mode) override;
deadbeef886815b2016-06-29 15:21:04 -0700103 // TODO(deadbeef): Deprecated. Remove when Chromium's
104 // IceTransportChannel does not depend on this.
105 void Connect() {}
deadbeefcbecd352015-09-23 11:50:27 -0700106 void MaybeStartGathering() override;
Steve Anton33f69db2017-10-30 10:01:15 -0700107 IceGatheringState gathering_state() const override;
deadbeefcbecd352015-09-23 11:50:27 -0700108 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700109 void RemoveRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800110 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
111 // only update the parameter if it is considered set in |config|. For example,
112 // a negative value of receiving_timeout will be considered "not set" and we
113 // will not use it to update the respective parameter in |config_|.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200114 // TODO(deadbeef): Use absl::optional instead of negative values.
honghaiz1f429e32015-09-28 07:57:34 -0700115 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -0800116 const IceConfig& config() const;
Qingsi Wangdea68892018-03-27 10:55:21 -0700117 static webrtc::RTCError ValidateIceConfig(const IceConfig& config);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118
119 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700120 int SendPacket(const char* data,
121 size_t len,
122 const rtc::PacketOptions& options,
123 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200124 int SetOption(rtc::Socket::Option opt, int value) override;
125 bool GetOption(rtc::Socket::Option opt, int* value) override;
Steve Anton33f69db2017-10-30 10:01:15 -0700126 int GetError() override;
Qingsi Wang72a43a12018-02-20 16:03:18 -0800127 bool GetStats(std::vector<ConnectionInfo>* candidate_pair_stats_list,
128 std::vector<CandidateStats>* candidate_stats_list) override;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200129 absl::optional<int> GetRttEstimate() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130
Honghai Zhang8cd7f222016-06-23 13:44:34 -0700131 // TODO(honghaiz): Remove this method once the reference of it in
132 // Chromoting is removed.
133 const Connection* best_connection() const { return selected_connection_; }
134
Honghai Zhang572b0942016-06-23 12:26:57 -0700135 const Connection* selected_connection() const { return selected_connection_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136 void set_incoming_only(bool value) { incoming_only_ = value; }
137
Honghai Zhanga74363c2016-07-28 18:06:15 -0700138 // Note: These are only for testing purpose.
139 // |ports_| and |pruned_ports| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700140 const std::vector<PortInterface*>& ports() { return ports_; }
Honghai Zhanga74363c2016-07-28 18:06:15 -0700141 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142
143 IceMode remote_ice_mode() const { return remote_ice_mode_; }
144
Honghai Zhanga74363c2016-07-28 18:06:15 -0700145 void PruneAllPorts();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700146 int check_receiving_interval() const;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200147 absl::optional<rtc::NetworkRoute> network_route() const override;
Zhi Huang942bc2e2017-11-13 13:26:07 -0800148
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149 // Helper method used only in unittest.
150 rtc::DiffServCodePoint DefaultDscpValue() const;
151
Peter Thatcher7351f462015-04-02 16:39:16 -0700152 // Public for unit tests.
153 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800154 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700155
honghaiz77d0d6e2015-10-27 11:34:45 -0700156 // Public for unit tests.
157 const std::vector<Connection*>& connections() const { return connections_; }
158
honghaiz9b669572015-11-04 12:07:44 -0800159 // Public for unit tests.
Qingsi Wang1b368942018-06-13 13:54:08 -0700160 PortAllocatorSession* allocator_session() const {
161 if (allocator_sessions_.empty()) {
162 return nullptr;
163 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700164 return allocator_sessions_.back().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 }
166
honghaiz112fe432015-12-30 13:32:47 -0800167 // Public for unit tests.
168 const std::vector<RemoteCandidate>& remote_candidates() const {
169 return remote_candidates_;
170 }
171
zhihuangd06adf62017-01-12 15:58:31 -0800172 std::string ToString() const {
173 const char RECEIVING_ABBREV[2] = {'_', 'R'};
174 const char WRITABLE_ABBREV[2] = {'_', 'W'};
175 std::stringstream ss;
176 ss << "Channel[" << transport_name_ << "|" << component_ << "|"
177 << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]";
178 return ss.str();
179 }
180
honghaiz9b669572015-11-04 12:07:44 -0800181 private:
johan0fd22ef2016-09-29 01:19:20 -0700182 rtc::Thread* thread() const { return network_thread_; }
Qingsi Wang1b368942018-06-13 13:54:08 -0700183
honghaiz9b669572015-11-04 12:07:44 -0800184 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
185
honghaiza58ea782015-09-24 08:13:36 -0700186 // A transport channel is weak if the current best connection is either
187 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700188 bool weak() const;
skvlad51072462017-02-02 11:50:14 -0800189
190 int weak_ping_interval() const {
Qingsi Wang866e08d2018-03-22 17:54:23 -0700191 return std::max(config_.ice_check_interval_weak_connectivity_or_default(),
192 config_.ice_check_min_interval_or_default());
skvlad51072462017-02-02 11:50:14 -0800193 }
194
195 int strong_ping_interval() const {
Qingsi Wang866e08d2018-03-22 17:54:23 -0700196 return std::max(config_.ice_check_interval_strong_connectivity_or_default(),
197 config_.ice_check_min_interval_or_default());
skvlad51072462017-02-02 11:50:14 -0800198 }
199
Honghai Zhange05bcc22016-08-16 18:19:14 -0700200 // Returns true if it's possible to send packets on |connection|.
201 bool ReadyToSend(Connection* connection) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 void UpdateConnectionStates();
Qingsi Wang10a0e512018-05-16 13:37:03 -0700203 void RequestSortAndStateUpdate(const std::string& reason_to_sort);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700204 // Start pinging if we haven't already started, and we now have a connection
205 // that's pingable.
206 void MaybeStartPinging();
deadbeef14f97f52016-06-22 17:14:15 -0700207
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800208 int CompareCandidatePairNetworks(
209 const Connection* a,
210 const Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200211 absl::optional<rtc::AdapterType> network_preference) const;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800212
honghaiz9ad0db52016-07-14 19:30:28 -0700213 // The methods below return a positive value if |a| is preferable to |b|,
214 // a negative value if |b| is preferable, and 0 if they're equally preferable.
215 // If |receiving_unchanged_threshold| is set, then when |b| is receiving and
216 // |a| is not, returns a negative value only if |b| has been in receiving
217 // state and |a| has been in not receiving state since
218 // |receiving_unchanged_threshold| and sets
219 // |missed_receiving_unchanged_threshold| to true otherwise.
220 int CompareConnectionStates(
221 const cricket::Connection* a,
222 const cricket::Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200223 absl::optional<int64_t> receiving_unchanged_threshold,
honghaiz9ad0db52016-07-14 19:30:28 -0700224 bool* missed_receiving_unchanged_threshold) const;
deadbeef14f97f52016-06-22 17:14:15 -0700225 int CompareConnectionCandidates(const cricket::Connection* a,
226 const cricket::Connection* b) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700227 // Compares two connections based on the connection states
228 // (writable/receiving/connected), nomination states, last data received time,
229 // and static preferences. Does not include latency. Used by both sorting
230 // and ShouldSwitchSelectedConnection().
231 // Returns a positive value if |a| is better than |b|.
deadbeef14f97f52016-06-22 17:14:15 -0700232 int CompareConnections(const cricket::Connection* a,
honghaiz9ad0db52016-07-14 19:30:28 -0700233 const cricket::Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200234 absl::optional<int64_t> receiving_unchanged_threshold,
honghaiz9ad0db52016-07-14 19:30:28 -0700235 bool* missed_receiving_unchanged_threshold) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700236
deadbeef14f97f52016-06-22 17:14:15 -0700237 bool PresumedWritable(const cricket::Connection* conn) const;
238
Qingsi Wang10a0e512018-05-16 13:37:03 -0700239 void SortConnectionsAndUpdateState(const std::string& reason_to_sort);
Honghai Zhang572b0942016-06-23 12:26:57 -0700240 void SwitchSelectedConnection(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800241 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000242 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800243 void MaybeStopPortAllocatorSessions();
zhihuangd06adf62017-01-12 15:58:31 -0800244 IceTransportState ComputeState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245
sprang716978d2016-10-11 06:43:28 -0700246 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700247 bool CreateConnections(const Candidate& remote_candidate,
248 PortInterface* origin_port);
249 bool CreateConnection(PortInterface* port,
250 const Candidate& remote_candidate,
251 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 bool FindConnection(cricket::Connection* connection) const;
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
256 void RememberRemoteCandidate(const Candidate& remote_candidate,
257 PortInterface* origin_port);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700258 bool IsPingable(const Connection* conn, int64_t now) const;
honghaiz7252a002016-11-08 20:04:09 -0800259 // Whether a writable connection is past its ping interval and needs to be
260 // pinged again.
261 bool WritableConnectionPastPingInterval(const Connection* conn,
262 int64_t now) const;
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700263 int CalculateActiveWritablePingInterval(const Connection* conn,
264 int64_t now) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000265 void PingConnection(Connection* conn);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700266 void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000267 void AddConnection(Connection* connection);
268
Yves Gerey665174f2018-06-19 15:03:05 +0200269 void OnPortReady(PortAllocatorSession* session, PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700270 void OnPortsPruned(PortAllocatorSession* session,
271 const std::vector<PortInterface*>& ports);
Yves Gerey665174f2018-06-19 15:03:05 +0200272 void OnCandidatesReady(PortAllocatorSession* session,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000273 const std::vector<Candidate>& candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700274 void OnCandidatesRemoved(PortAllocatorSession* session,
275 const std::vector<Candidate>& candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276 void OnCandidatesAllocationDone(PortAllocatorSession* session);
277 void OnUnknownAddress(PortInterface* port,
278 const rtc::SocketAddress& addr,
279 ProtocolType proto,
280 IceMessage* stun_msg,
281 const std::string& remote_username,
282 bool port_muxed);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700283
284 // When a port is destroyed, remove it from both lists |ports_|
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700285 // and |pruned_ports_|.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000286 void OnPortDestroyed(PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700287 // When pruning a port, move it from |ports_| to |pruned_ports_|.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700288 // Returns true if the port is found and removed from |ports_|.
Honghai Zhanga74363c2016-07-28 18:06:15 -0700289 bool PrunePort(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000290 void OnRoleConflict(PortInterface* port);
291
292 void OnConnectionStateChange(Connection* connection);
Yves Gerey665174f2018-06-19 15:03:05 +0200293 void OnReadPacket(Connection* connection,
294 const char* data,
295 size_t len,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296 const rtc::PacketTime& packet_time);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100297 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000298 void OnReadyToSend(Connection* connection);
Yves Gerey665174f2018-06-19 15:03:05 +0200299 void OnConnectionDestroyed(Connection* connection);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000300
honghaiz5a3acd82015-08-20 15:53:17 -0700301 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302
Qingsi Wang502db3d2018-05-16 17:01:37 -0700303 void CheckAndPing();
Peter Thatcher54360512015-07-08 11:08:35 -0700304
Bjorn Terelius59b4e3e2018-05-30 17:14:08 +0200305 void LogCandidatePairConfig(Connection* conn,
306 webrtc::IceCandidatePairConfigType type);
Qingsi Wang93a84392018-01-30 17:13:09 -0800307
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700308 uint32_t GetNominationAttr(Connection* conn) const;
309 bool GetUseCandidateAttr(Connection* conn, NominationMode mode) const;
310
honghaiz9ad0db52016-07-14 19:30:28 -0700311 // Returns true if we should switch to the new connection.
312 // sets |missed_receiving_unchanged_threshold| to true if either
313 // the selected connection or the new connection missed its
314 // receiving-unchanged-threshold.
315 bool ShouldSwitchSelectedConnection(
316 Connection* new_connection,
317 bool* missed_receiving_unchanged_threshold) const;
318 // Returns true if the new_connection is selected for transmission.
319 bool MaybeSwitchSelectedConnection(Connection* new_connection,
320 const std::string& reason);
honghaiz7252a002016-11-08 20:04:09 -0800321 // Gets the best connection for each network.
322 std::map<rtc::Network*, Connection*> GetBestConnectionByNetwork() const;
323 std::vector<Connection*> GetBestWritableConnectionPerNetwork() const;
honghaiz5a3acd82015-08-20 15:53:17 -0700324 void PruneConnections();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700325 bool IsBackupConnection(const Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700326
honghaiz34b11eb2016-03-16 08:55:44 -0700327 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800328 // Between |conn1| and |conn2|, this function returns the one which should
329 // be pinged first.
honghaiz7252a002016-11-08 20:04:09 -0800330 Connection* MorePingable(Connection* conn1, Connection* conn2);
guoweis36f01372016-03-02 18:02:40 -0800331 // Select the connection which is Relay/Relay. If both of them are,
332 // UDP relay protocol takes precedence.
333 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
334 // Compare the last_ping_sent time and return the one least recently pinged.
335 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
336
honghaiza54a0802015-12-16 18:37:23 -0800337 // Returns the latest remote ICE parameters or nullptr if there are no remote
338 // ICE parameters yet.
339 IceParameters* remote_ice() {
340 return remote_ice_parameters_.empty() ? nullptr
341 : &remote_ice_parameters_.back();
342 }
honghaiz112fe432015-12-30 13:32:47 -0800343 // Returns the remote IceParameters and generation that match |ufrag|
344 // if found, and returns nullptr otherwise.
345 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
346 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800347 // Returns the index of the latest remote ICE parameters, or 0 if no remote
348 // ICE parameters have been received.
349 uint32_t remote_ice_generation() {
350 return remote_ice_parameters_.empty()
351 ? 0
352 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
353 }
354
Steve Anton300bf8e2017-07-14 10:13:10 -0700355 // Indicates if the given local port has been pruned.
356 bool IsPortPruned(const Port* port) const;
357
358 // Indicates if the given remote candidate has been pruned.
359 bool IsRemoteCandidatePruned(const Candidate& cand) const;
360
zhihuangd06adf62017-01-12 15:58:31 -0800361 // Sets the writable state, signaling if necessary.
362 void set_writable(bool writable);
363 // Sets the receiving state, signaling if necessary.
364 void set_receiving(bool receiving);
365
366 std::string transport_name_;
367 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700368 PortAllocator* allocator_;
Zach Steine20867f2018-08-02 13:20:15 -0700369 webrtc::AsyncResolverFactory* async_resolver_factory_;
johan0fd22ef2016-09-29 01:19:20 -0700370 rtc::Thread* network_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000372 int error_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700373 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_;
deadbeefdfc42442016-06-21 14:19:48 -0700374 // |ports_| contains ports that are used to form new connections when
375 // new remote candidates are added.
376 std::vector<PortInterface*> ports_;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700377 // |pruned_ports_| contains ports that have been removed from |ports_| and
deadbeefdfc42442016-06-21 14:19:48 -0700378 // are not being used to form new connections, but that aren't yet destroyed.
379 // They may have existing connections, and they still fire signals such as
380 // SignalUnknownAddress.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700381 std::vector<PortInterface*> pruned_ports_;
guoweis36f01372016-03-02 18:02:40 -0800382
383 // |connections_| is a sorted list with the first one always be the
Honghai Zhang572b0942016-06-23 12:26:57 -0700384 // |selected_connection_| when it's not nullptr. The combination of
guoweis36f01372016-03-02 18:02:40 -0800385 // |pinged_connections_| and |unpinged_connections_| has the same
386 // connections as |connections_|. These 2 sets maintain whether a
387 // connection should be pinged next or not.
Yves Gerey665174f2018-06-19 15:03:05 +0200388 std::vector<Connection*> connections_;
guoweis36f01372016-03-02 18:02:40 -0800389 std::set<Connection*> pinged_connections_;
390 std::set<Connection*> unpinged_connections_;
391
Honghai Zhang572b0942016-06-23 12:26:57 -0700392 Connection* selected_connection_ = nullptr;
guoweis36f01372016-03-02 18:02:40 -0800393
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 std::vector<RemoteCandidate> remote_candidates_;
395 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700396 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000397 typedef std::map<rtc::Socket::Option, int> OptionMap;
398 OptionMap options_;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700399 IceParameters ice_parameters_;
honghaiza54a0802015-12-16 18:37:23 -0800400 std::vector<IceParameters> remote_ice_parameters_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401 IceMode remote_ice_mode_;
402 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 uint64_t tiebreaker_;
deadbeefcbecd352015-09-23 11:50:27 -0700404 IceGatheringState gathering_state_;
Qingsi Wang1b368942018-06-13 13:54:08 -0700405 std::unique_ptr<webrtc::BasicRegatheringController> regathering_controller_;
honghaiz34b11eb2016-03-16 08:55:44 -0700406 int64_t last_ping_sent_ms_ = 0;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800407 int weak_ping_interval_ = WEAK_PING_INTERVAL;
zhihuangd06adf62017-01-12 15:58:31 -0800408 IceTransportState state_ = IceTransportState::STATE_INIT;
guoweis36f01372016-03-02 18:02:40 -0800409 IceConfig config_;
Honghai Zhang52dce732016-03-31 12:37:31 -0700410 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before.
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700411 bool started_pinging_ = false;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700412 // The value put in the "nomination" attribute for the next nominated
413 // connection. A zero-value indicates the connection will not be nominated.
414 uint32_t nomination_ = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800415 bool receiving_ = false;
416 bool writable_ = false;
Peter Thatcher54360512015-07-08 11:08:35 -0700417
Qingsi Wang502db3d2018-05-16 17:01:37 -0700418 rtc::AsyncInvoker invoker_;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200419 absl::optional<rtc::NetworkRoute> network_route_;
Qingsi Wang93a84392018-01-30 17:13:09 -0800420 webrtc::IceEventLog ice_event_log_;
421
henrikg3c089d72015-09-16 05:37:44 -0700422 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000423};
424
425} // namespace cricket
426
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200427#endif // P2P_BASE_P2PTRANSPORTCHANNEL_H_