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 | // |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #ifndef P2P_BASE_P2P_TRANSPORT_CHANNEL_H_ |
| 21 | #define P2P_BASE_P2P_TRANSPORT_CHANNEL_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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "api/async_resolver_factory.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 31 | #include "api/candidate.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "api/rtc_error.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 33 | #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "logging/rtc_event_log/ice_logger.h" |
| 35 | #include "p2p/base/candidate_pair_interface.h" |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 36 | #include "p2p/base/ice_controller_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 37 | #include "p2p/base/ice_transport_internal.h" |
| 38 | #include "p2p/base/p2p_constants.h" |
| 39 | #include "p2p/base/port_allocator.h" |
| 40 | #include "p2p/base/port_interface.h" |
| 41 | #include "p2p/base/regathering_controller.h" |
| 42 | #include "rtc_base/async_invoker.h" |
| 43 | #include "rtc_base/async_packet_socket.h" |
| 44 | #include "rtc_base/constructor_magic.h" |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 45 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 46 | #include "rtc_base/system/rtc_export.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 47 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 48 | #include "rtc_base/thread_annotations.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 50 | namespace webrtc { |
| 51 | class RtcEventLog; |
| 52 | } // namespace webrtc |
| 53 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | namespace cricket { |
| 55 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 56 | // Enum for UMA metrics, used to record whether the channel is |
| 57 | // connected/connecting/disconnected when ICE restart happens. |
| 58 | enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE }; |
| 59 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 60 | static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 61 | |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 62 | bool IceCredentialsChanged(const std::string& old_ufrag, |
| 63 | const std::string& old_pwd, |
| 64 | const std::string& new_ufrag, |
| 65 | const std::string& new_pwd); |
| 66 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | // Adds the port on which the candidate originated. |
| 68 | class RemoteCandidate : public Candidate { |
| 69 | public: |
| 70 | RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 71 | : Candidate(c), origin_port_(origin_port) {} |
| 72 | |
| 73 | PortInterface* origin_port() { return origin_port_; } |
| 74 | |
| 75 | private: |
| 76 | PortInterface* origin_port_; |
| 77 | }; |
| 78 | |
Jonas Oreland | 4af7882 | 2019-10-11 08:17:06 +0200 | [diff] [blame] | 79 | struct IceFieldTrials { |
| 80 | bool skip_relay_to_non_relay_connections = false; |
Jonas Oreland | c6404a1 | 2019-10-14 15:52:15 +0200 | [diff] [blame] | 81 | absl::optional<int> max_outstanding_pings; |
Jonas Oreland | 1230fb7 | 2019-10-25 15:58:14 +0200 | [diff] [blame] | 82 | |
| 83 | // Wait X ms before selecting a connection when having none. |
| 84 | // This will make media slower, but will give us chance to find |
| 85 | // a better connection before starting. |
| 86 | absl::optional<int> initial_select_dampening; |
| 87 | |
| 88 | // If the connection has recevied a ping-request, delay by |
| 89 | // maximum this delay. This will make media slower, but will |
| 90 | // give us chance to find a better connection before starting. |
| 91 | absl::optional<int> initial_select_dampening_ping_received; |
Jonas Oreland | 4af7882 | 2019-10-11 08:17:06 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 94 | // P2PTransportChannel manages the candidates and connection process to keep |
| 95 | // two P2P clients connected to each other. |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 96 | class RTC_EXPORT P2PTransportChannel : public IceTransportInternal { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | public: |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 98 | // For testing only. |
| 99 | // TODO(zstein): Remove once AsyncResolverFactory is required. |
| 100 | P2PTransportChannel(const std::string& transport_name, |
| 101 | int component, |
| 102 | PortAllocator* allocator); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 103 | P2PTransportChannel(const std::string& transport_name, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 104 | int component, |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 105 | PortAllocator* allocator, |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 106 | webrtc::AsyncResolverFactory* async_resolver_factory, |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 107 | webrtc::RtcEventLog* event_log = nullptr); |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 108 | ~P2PTransportChannel() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 109 | |
| 110 | // From TransportChannelImpl: |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 111 | IceTransportState GetState() const override; |
Jonas Olsson | 81125f0 | 2018-10-09 10:52:04 +0200 | [diff] [blame] | 112 | webrtc::IceTransportState GetIceTransportState() const override; |
| 113 | |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 114 | const std::string& transport_name() const override; |
| 115 | int component() const override; |
| 116 | bool writable() const override; |
| 117 | bool receiving() const override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 118 | void SetIceRole(IceRole role) override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 119 | IceRole GetIceRole() const override; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 120 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 121 | void SetIceParameters(const IceParameters& ice_params) override; |
| 122 | void SetRemoteIceParameters(const IceParameters& ice_params) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 123 | void SetRemoteIceMode(IceMode mode) override; |
deadbeef | 886815b | 2016-06-29 15:21:04 -0700 | [diff] [blame] | 124 | // TODO(deadbeef): Deprecated. Remove when Chromium's |
| 125 | // IceTransportChannel does not depend on this. |
| 126 | void Connect() {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 127 | void MaybeStartGathering() override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 128 | IceGatheringState gathering_state() const override; |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 129 | void ResolveHostnameCandidate(const Candidate& candidate); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 130 | void AddRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 131 | void RemoveRemoteCandidate(const Candidate& candidate) override; |
Harald Alvestrand | 4241cf5 | 2019-01-21 09:29:42 +0100 | [diff] [blame] | 132 | void RemoveAllRemoteCandidates() override; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 133 | // Sets the parameters in IceConfig. We do not set them blindly. Instead, we |
| 134 | // only update the parameter if it is considered set in |config|. For example, |
| 135 | // a negative value of receiving_timeout will be considered "not set" and we |
| 136 | // will not use it to update the respective parameter in |config_|. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 137 | // TODO(deadbeef): Use absl::optional instead of negative values. |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 138 | void SetIceConfig(const IceConfig& config) override; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 139 | const IceConfig& config() const; |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 140 | static webrtc::RTCError ValidateIceConfig(const IceConfig& config); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 141 | |
| 142 | // From TransportChannel: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 143 | int SendPacket(const char* data, |
| 144 | size_t len, |
| 145 | const rtc::PacketOptions& options, |
| 146 | int flags) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 147 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 148 | bool GetOption(rtc::Socket::Option opt, int* value) override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 149 | int GetError() override; |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 150 | bool GetStats(IceTransportStats* ice_transport_stats) override; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 151 | absl::optional<int> GetRttEstimate() override; |
Harald Alvestrand | 4241cf5 | 2019-01-21 09:29:42 +0100 | [diff] [blame] | 152 | const Connection* selected_connection() const override; |
Qingsi Wang | e5defb1 | 2019-08-15 11:01:53 -0700 | [diff] [blame] | 153 | absl::optional<const CandidatePair> GetSelectedCandidatePair() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 154 | |
Honghai Zhang | 8cd7f22 | 2016-06-23 13:44:34 -0700 | [diff] [blame] | 155 | // TODO(honghaiz): Remove this method once the reference of it in |
| 156 | // Chromoting is removed. |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 157 | const Connection* best_connection() const { |
| 158 | RTC_DCHECK_RUN_ON(network_thread_); |
| 159 | return selected_connection_; |
| 160 | } |
Honghai Zhang | 8cd7f22 | 2016-06-23 13:44:34 -0700 | [diff] [blame] | 161 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 162 | void set_incoming_only(bool value) { |
| 163 | RTC_DCHECK_RUN_ON(network_thread_); |
| 164 | incoming_only_ = value; |
| 165 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 166 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 167 | // Note: These are only for testing purpose. |
| 168 | // |ports_| and |pruned_ports| should not be changed from outside. |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 169 | const std::vector<PortInterface*>& ports() { |
| 170 | RTC_DCHECK_RUN_ON(network_thread_); |
| 171 | return ports_; |
| 172 | } |
| 173 | const std::vector<PortInterface*>& pruned_ports() { |
| 174 | RTC_DCHECK_RUN_ON(network_thread_); |
| 175 | return pruned_ports_; |
| 176 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 177 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 178 | IceMode remote_ice_mode() const { |
| 179 | RTC_DCHECK_RUN_ON(network_thread_); |
| 180 | return remote_ice_mode_; |
| 181 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 182 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 183 | void PruneAllPorts(); |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 184 | int check_receiving_interval() const; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 185 | absl::optional<rtc::NetworkRoute> network_route() const override; |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 186 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 187 | // Helper method used only in unittest. |
| 188 | rtc::DiffServCodePoint DefaultDscpValue() const; |
| 189 | |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 190 | // Public for unit tests. |
| 191 | Connection* FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 192 | void MarkConnectionPinged(Connection* conn); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 193 | |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 194 | // Public for unit tests. |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 195 | rtc::ArrayView<Connection*> connections() const; |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 196 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 197 | // Public for unit tests. |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 198 | PortAllocatorSession* allocator_session() const { |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 199 | RTC_DCHECK_RUN_ON(network_thread_); |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 200 | if (allocator_sessions_.empty()) { |
| 201 | return nullptr; |
| 202 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 203 | return allocator_sessions_.back().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 204 | } |
| 205 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 206 | // Public for unit tests. |
| 207 | const std::vector<RemoteCandidate>& remote_candidates() const { |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 208 | RTC_DCHECK_RUN_ON(network_thread_); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 209 | return remote_candidates_; |
| 210 | } |
| 211 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 212 | std::string ToString() const { |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 213 | RTC_DCHECK_RUN_ON(network_thread_); |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 214 | const std::string RECEIVING_ABBREV[2] = {"_", "R"}; |
| 215 | const std::string WRITABLE_ABBREV[2] = {"_", "W"}; |
| 216 | rtc::StringBuilder ss; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 217 | ss << "Channel[" << transport_name_ << "|" << component_ << "|" |
| 218 | << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]"; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 219 | return ss.Release(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 220 | } |
| 221 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 222 | private: |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 223 | rtc::Thread* thread() const { return network_thread_; } |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 224 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 225 | bool IsGettingPorts() { |
| 226 | RTC_DCHECK_RUN_ON(network_thread_); |
| 227 | return allocator_session()->IsGettingPorts(); |
| 228 | } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 229 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 230 | // Returns true if it's possible to send packets on |connection|. |
| 231 | bool ReadyToSend(Connection* connection) const; |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 232 | bool PresumedWritable(const Connection* conn) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 233 | void UpdateConnectionStates(); |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 234 | void RequestSortAndStateUpdate(IceControllerEvent reason_to_sort); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 235 | // Start pinging if we haven't already started, and we now have a connection |
| 236 | // that's pingable. |
| 237 | void MaybeStartPinging(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 238 | |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 239 | void SortConnectionsAndUpdateState(IceControllerEvent reason_to_sort); |
| 240 | void SortConnections(); |
| 241 | void SortConnectionsIfNeeded(); |
| 242 | void SwitchSelectedConnection(Connection* conn, IceControllerEvent reason); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 243 | void UpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 244 | void HandleAllTimedOut(); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 245 | void MaybeStopPortAllocatorSessions(); |
Jonas Olsson | 81125f0 | 2018-10-09 10:52:04 +0200 | [diff] [blame] | 246 | |
| 247 | // ComputeIceTransportState computes the RTCIceTransportState as described in |
| 248 | // https://w3c.github.io/webrtc-pc/#dom-rtcicetransportstate. ComputeState |
| 249 | // computes the value we currently export as RTCIceTransportState. |
| 250 | // TODO(bugs.webrtc.org/9308): Remove ComputeState once it's no longer used. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 251 | IceTransportState ComputeState() const; |
Jonas Olsson | 81125f0 | 2018-10-09 10:52:04 +0200 | [diff] [blame] | 252 | webrtc::IceTransportState ComputeIceTransportState() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 253 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 254 | bool CreateConnections(const Candidate& remote_candidate, |
| 255 | PortInterface* origin_port); |
| 256 | bool CreateConnection(PortInterface* port, |
| 257 | const Candidate& remote_candidate, |
| 258 | PortInterface* origin_port); |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 259 | bool FindConnection(Connection* connection) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 260 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 261 | uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 262 | bool IsDuplicateRemoteCandidate(const Candidate& candidate); |
| 263 | void RememberRemoteCandidate(const Candidate& remote_candidate, |
| 264 | PortInterface* origin_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 265 | void PingConnection(Connection* conn); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 266 | void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 267 | void AddConnection(Connection* connection); |
| 268 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 269 | void OnPortReady(PortAllocatorSession* session, PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 270 | void OnPortsPruned(PortAllocatorSession* session, |
| 271 | const std::vector<PortInterface*>& ports); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 272 | void OnCandidatesReady(PortAllocatorSession* session, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 273 | const std::vector<Candidate>& candidates); |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 274 | void OnCandidateError(PortAllocatorSession* session, |
| 275 | const IceCandidateErrorEvent& event); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 276 | void OnCandidatesRemoved(PortAllocatorSession* session, |
| 277 | const std::vector<Candidate>& candidates); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 278 | void OnCandidatesAllocationDone(PortAllocatorSession* session); |
| 279 | void OnUnknownAddress(PortInterface* port, |
| 280 | const rtc::SocketAddress& addr, |
| 281 | ProtocolType proto, |
| 282 | IceMessage* stun_msg, |
| 283 | const std::string& remote_username, |
| 284 | bool port_muxed); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 285 | void OnCandidateFilterChanged(uint32_t prev_filter, uint32_t cur_filter); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 286 | |
| 287 | // When a port is destroyed, remove it from both lists |ports_| |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 288 | // and |pruned_ports_|. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 289 | void OnPortDestroyed(PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 290 | // When pruning a port, move it from |ports_| to |pruned_ports_|. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 291 | // Returns true if the port is found and removed from |ports_|. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 292 | bool PrunePort(PortInterface* port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 293 | void OnRoleConflict(PortInterface* port); |
| 294 | |
| 295 | void OnConnectionStateChange(Connection* connection); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 296 | void OnReadPacket(Connection* connection, |
| 297 | const char* data, |
| 298 | size_t len, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 299 | int64_t packet_time_us); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 300 | void OnSentPacket(const rtc::SentPacket& sent_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | void OnReadyToSend(Connection* connection); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 302 | void OnConnectionDestroyed(Connection* connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 303 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 304 | void OnNominated(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 305 | |
Qingsi Wang | 502db3d | 2018-05-16 17:01:37 -0700 | [diff] [blame] | 306 | void CheckAndPing(); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 307 | |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 308 | void LogCandidatePairConfig(Connection* conn, |
| 309 | webrtc::IceCandidatePairConfigType type); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 310 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 311 | uint32_t GetNominationAttr(Connection* conn) const; |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 312 | bool GetUseCandidateAttr(Connection* conn) const; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 313 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 314 | // Returns true if the new_connection is selected for transmission. |
| 315 | bool MaybeSwitchSelectedConnection(Connection* new_connection, |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 316 | IceControllerEvent reason); |
| 317 | bool MaybeSwitchSelectedConnection( |
| 318 | IceControllerEvent reason, |
| 319 | IceControllerInterface::SwitchResult result); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 320 | void PruneConnections(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 321 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 322 | // Returns the latest remote ICE parameters or nullptr if there are no remote |
| 323 | // ICE parameters yet. |
| 324 | IceParameters* remote_ice() { |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 325 | RTC_DCHECK_RUN_ON(network_thread_); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 326 | return remote_ice_parameters_.empty() ? nullptr |
| 327 | : &remote_ice_parameters_.back(); |
| 328 | } |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 329 | // Returns the remote IceParameters and generation that match |ufrag| |
| 330 | // if found, and returns nullptr otherwise. |
| 331 | const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, |
| 332 | uint32_t* generation); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 333 | // Returns the index of the latest remote ICE parameters, or 0 if no remote |
| 334 | // ICE parameters have been received. |
| 335 | uint32_t remote_ice_generation() { |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 336 | RTC_DCHECK_RUN_ON(network_thread_); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 337 | return remote_ice_parameters_.empty() |
| 338 | ? 0 |
| 339 | : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); |
| 340 | } |
| 341 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 342 | // Indicates if the given local port has been pruned. |
| 343 | bool IsPortPruned(const Port* port) const; |
| 344 | |
| 345 | // Indicates if the given remote candidate has been pruned. |
| 346 | bool IsRemoteCandidatePruned(const Candidate& cand) const; |
| 347 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 348 | // Sets the writable state, signaling if necessary. |
Jonas Olsson | 7a6739e | 2019-01-15 16:31:55 +0100 | [diff] [blame] | 349 | void SetWritable(bool writable); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 350 | // Sets the receiving state, signaling if necessary. |
Jonas Olsson | 7a6739e | 2019-01-15 16:31:55 +0100 | [diff] [blame] | 351 | void SetReceiving(bool receiving); |
Qingsi Wang | 7627fdd | 2019-08-19 16:07:40 -0700 | [diff] [blame] | 352 | // Clears the address and the related address fields of a local candidate to |
| 353 | // avoid IP leakage. This is applicable in several scenarios as commented in |
| 354 | // |PortAllocator::SanitizeCandidate|. |
| 355 | Candidate SanitizeLocalCandidate(const Candidate& c) const; |
| 356 | // Clears the address field of a remote candidate to avoid IP leakage. This is |
| 357 | // applicable in the following scenarios: |
| 358 | // 1. mDNS candidates are received. |
| 359 | // 2. Peer-reflexive remote candidates. |
| 360 | Candidate SanitizeRemoteCandidate(const Candidate& c) const; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 361 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 362 | std::string transport_name_ RTC_GUARDED_BY(network_thread_); |
| 363 | int component_ RTC_GUARDED_BY(network_thread_); |
| 364 | PortAllocator* allocator_ RTC_GUARDED_BY(network_thread_); |
| 365 | webrtc::AsyncResolverFactory* async_resolver_factory_ |
| 366 | RTC_GUARDED_BY(network_thread_); |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 367 | rtc::Thread* network_thread_; |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 368 | bool incoming_only_ RTC_GUARDED_BY(network_thread_); |
| 369 | int error_ RTC_GUARDED_BY(network_thread_); |
| 370 | std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_ |
| 371 | RTC_GUARDED_BY(network_thread_); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 372 | // |ports_| contains ports that are used to form new connections when |
| 373 | // new remote candidates are added. |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 374 | std::vector<PortInterface*> ports_ RTC_GUARDED_BY(network_thread_); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 375 | // |pruned_ports_| contains ports that have been removed from |ports_| and |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 376 | // are not being used to form new connections, but that aren't yet destroyed. |
| 377 | // They may have existing connections, and they still fire signals such as |
| 378 | // SignalUnknownAddress. |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 379 | std::vector<PortInterface*> pruned_ports_ RTC_GUARDED_BY(network_thread_); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 380 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 381 | Connection* selected_connection_ RTC_GUARDED_BY(network_thread_) = nullptr; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 382 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 383 | std::vector<RemoteCandidate> remote_candidates_ |
| 384 | RTC_GUARDED_BY(network_thread_); |
| 385 | bool sort_dirty_ RTC_GUARDED_BY( |
| 386 | network_thread_); // indicates whether another sort is needed right now |
| 387 | bool had_connection_ RTC_GUARDED_BY(network_thread_) = |
| 388 | false; // if connections_ has ever been nonempty |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 389 | typedef std::map<rtc::Socket::Option, int> OptionMap; |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 390 | OptionMap options_ RTC_GUARDED_BY(network_thread_); |
| 391 | IceParameters ice_parameters_ RTC_GUARDED_BY(network_thread_); |
| 392 | std::vector<IceParameters> remote_ice_parameters_ |
| 393 | RTC_GUARDED_BY(network_thread_); |
| 394 | IceMode remote_ice_mode_ RTC_GUARDED_BY(network_thread_); |
| 395 | IceRole ice_role_ RTC_GUARDED_BY(network_thread_); |
| 396 | uint64_t tiebreaker_ RTC_GUARDED_BY(network_thread_); |
| 397 | IceGatheringState gathering_state_ RTC_GUARDED_BY(network_thread_); |
| 398 | std::unique_ptr<webrtc::BasicRegatheringController> regathering_controller_ |
| 399 | RTC_GUARDED_BY(network_thread_); |
| 400 | int64_t last_ping_sent_ms_ RTC_GUARDED_BY(network_thread_) = 0; |
| 401 | int weak_ping_interval_ RTC_GUARDED_BY(network_thread_) = WEAK_PING_INTERVAL; |
Jonas Olsson | 81125f0 | 2018-10-09 10:52:04 +0200 | [diff] [blame] | 402 | // TODO(jonasolsson): Remove state_ and rename standardized_state_ once state_ |
| 403 | // is no longer used to compute the ICE connection state. |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 404 | IceTransportState state_ RTC_GUARDED_BY(network_thread_) = |
| 405 | IceTransportState::STATE_INIT; |
| 406 | webrtc::IceTransportState standardized_state_ |
| 407 | RTC_GUARDED_BY(network_thread_) = webrtc::IceTransportState::kNew; |
| 408 | IceConfig config_ RTC_GUARDED_BY(network_thread_); |
| 409 | int last_sent_packet_id_ RTC_GUARDED_BY(network_thread_) = |
| 410 | -1; // -1 indicates no packet was sent before. |
| 411 | bool started_pinging_ RTC_GUARDED_BY(network_thread_) = false; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 412 | // The value put in the "nomination" attribute for the next nominated |
| 413 | // connection. A zero-value indicates the connection will not be nominated. |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 414 | uint32_t nomination_ RTC_GUARDED_BY(network_thread_) = 0; |
| 415 | bool receiving_ RTC_GUARDED_BY(network_thread_) = false; |
| 416 | bool writable_ RTC_GUARDED_BY(network_thread_) = false; |
| 417 | bool has_been_writable_ RTC_GUARDED_BY(network_thread_) = |
| 418 | false; // if writable_ has ever been true |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 419 | |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 420 | rtc::AsyncInvoker invoker_ RTC_GUARDED_BY(network_thread_); |
| 421 | absl::optional<rtc::NetworkRoute> network_route_ |
| 422 | RTC_GUARDED_BY(network_thread_); |
| 423 | webrtc::IceEventLog ice_event_log_ RTC_GUARDED_BY(network_thread_); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 424 | |
Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [diff] [blame] | 425 | std::unique_ptr<IceControllerInterface> ice_controller_ |
| 426 | RTC_GUARDED_BY(network_thread_); |
| 427 | |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 428 | struct CandidateAndResolver final { |
| 429 | CandidateAndResolver(const Candidate& candidate, |
| 430 | rtc::AsyncResolverInterface* resolver); |
| 431 | ~CandidateAndResolver(); |
| 432 | Candidate candidate_; |
| 433 | rtc::AsyncResolverInterface* resolver_; |
| 434 | }; |
Harald Alvestrand | 36bc4f8 | 2019-05-27 15:49:31 +0200 | [diff] [blame] | 435 | std::vector<CandidateAndResolver> resolvers_ RTC_GUARDED_BY(network_thread_); |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 436 | void FinishAddingRemoteCandidate(const Candidate& new_remote_candidate); |
| 437 | void OnCandidateResolved(rtc::AsyncResolverInterface* resolver); |
| 438 | void AddRemoteCandidateWithResolver(Candidate candidate, |
| 439 | rtc::AsyncResolverInterface* resolver); |
| 440 | |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 441 | // Number of times the selected_connection_ has been modified. |
| 442 | uint32_t selected_candidate_pair_changes_ = 0; |
| 443 | |
Jonas Oreland | 4af7882 | 2019-10-11 08:17:06 +0200 | [diff] [blame] | 444 | IceFieldTrials field_trials_; |
| 445 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 446 | RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | } // namespace cricket |
| 450 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 451 | #endif // P2P_BASE_P2P_TRANSPORT_CHANNEL_H_ |