Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [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 | #ifndef API_CANDIDATE_H_ |
| 12 | #define API_CANDIDATE_H_ |
| 13 | |
Dor Hen | aefed55 | 2024-06-18 13:20:35 | [diff] [blame] | 14 | #include <stddef.h> |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 21 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "rtc_base/socket_address.h" |
Mirko Bonadei | 3b56ee7 | 2018-10-15 15:15:12 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 24 | |
Tommi | 32f2a30 | 2024-02-08 18:16:49 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | enum class IceCandidateType : int { kHost, kSrflx, kPrflx, kRelay }; |
Tommi | c7a4b2a | 2024-02-25 22:34:39 | [diff] [blame] | 27 | RTC_EXPORT absl::string_view IceCandidateTypeToString(IceCandidateType); |
Tommi | 32f2a30 | 2024-02-08 18:16:49 | [diff] [blame] | 28 | } // namespace webrtc |
| 29 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 30 | namespace cricket { |
| 31 | |
Tommi | d1e577d | 2024-03-21 14:02:31 | [diff] [blame] | 32 | // TODO(tommi): Remove. No usage in WebRTC now, remove once downstream projects |
| 33 | // don't have reliance. |
| 34 | [[deprecated("Use IceCandidateType")]] static constexpr char LOCAL_PORT_TYPE[] = |
| 35 | "local"; |
| 36 | [[deprecated("Use IceCandidateType")]] static constexpr char STUN_PORT_TYPE[] = |
| 37 | "stun"; |
| 38 | [[deprecated("Use IceCandidateType")]] static constexpr char PRFLX_PORT_TYPE[] = |
| 39 | "prflx"; |
| 40 | [[deprecated("Use IceCandidateType")]] static constexpr char RELAY_PORT_TYPE[] = |
| 41 | "relay"; |
Tommi | 3b2b2af | 2024-01-17 13:19:41 | [diff] [blame] | 42 | |
Philipp Hancke | 41a8357 | 2022-11-28 13:47:45 | [diff] [blame] | 43 | // TURN servers are limited to 32 in accordance with |
| 44 | // https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers |
| 45 | static constexpr size_t kMaxTurnServers = 32; |
| 46 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 47 | // Candidate for ICE based connection discovery. |
Mirko Bonadei | 3b56ee7 | 2018-10-15 15:15:12 | [diff] [blame] | 48 | class RTC_EXPORT Candidate { |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 49 | public: |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 50 | Candidate(); |
Tommi | c7a4b2a | 2024-02-25 22:34:39 | [diff] [blame] | 51 | Candidate(int component, |
| 52 | absl::string_view protocol, |
| 53 | const rtc::SocketAddress& address, |
| 54 | uint32_t priority, |
| 55 | absl::string_view username, |
| 56 | absl::string_view password, |
| 57 | webrtc::IceCandidateType type, |
| 58 | uint32_t generation, |
| 59 | absl::string_view foundation, |
| 60 | uint16_t network_id = 0, |
| 61 | uint16_t network_cost = 0); |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 62 | Candidate(const Candidate&); |
| 63 | ~Candidate(); |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 64 | |
Tommi | bde80e3 | 2024-02-17 14:52:57 | [diff] [blame] | 65 | // 8 character long randomized ID string for logging purposes. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 66 | const std::string& id() const { return id_; } |
Tommi | bde80e3 | 2024-02-17 14:52:57 | [diff] [blame] | 67 | // Generates a new, 8 character long, id. |
| 68 | void generate_id(); |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 69 | |
| 70 | int component() const { return component_; } |
| 71 | void set_component(int component) { component_ = component; } |
| 72 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 73 | const std::string& protocol() const { return protocol_; } |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 74 | |
| 75 | // Valid protocol values are: |
| 76 | // UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME, SSLTCP_PROTOCOL_NAME, |
| 77 | // TLS_PROTOCOL_NAME. |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 78 | void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 79 | |
| 80 | // The protocol used to talk to relay. |
| 81 | const std::string& relay_protocol() const { return relay_protocol_; } |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 82 | |
| 83 | // Valid protocol values are: |
| 84 | // UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME, SSLTCP_PROTOCOL_NAME, |
| 85 | // TLS_PROTOCOL_NAME. |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 86 | void set_relay_protocol(absl::string_view protocol) { |
| 87 | Assign(relay_protocol_, protocol); |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 88 | } |
| 89 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 90 | const rtc::SocketAddress& address() const { return address_; } |
| 91 | void set_address(const rtc::SocketAddress& address) { address_ = address; } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 92 | |
| 93 | uint32_t priority() const { return priority_; } |
| 94 | void set_priority(const uint32_t priority) { priority_ = priority; } |
| 95 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 96 | // TODO(honghaiz): Change to usernameFragment or ufrag. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 97 | const std::string& username() const { return username_; } |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 98 | void set_username(absl::string_view username) { Assign(username_, username); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 99 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 100 | const std::string& password() const { return password_; } |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 101 | void set_password(absl::string_view password) { Assign(password_, password); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 102 | |
Tommi | c7a4b2a | 2024-02-25 22:34:39 | [diff] [blame] | 103 | webrtc::IceCandidateType type() const { return type_; } |
Tommi | 6bf2d31 | 2023-09-25 12:05:31 | [diff] [blame] | 104 | |
Tommi | f7b22c6 | 2024-02-15 08:18:34 | [diff] [blame] | 105 | // Returns the name of the candidate type as specified in |
| 106 | // https://datatracker.ietf.org/doc/html/rfc5245#section-15.1 |
| 107 | absl::string_view type_name() const; |
| 108 | |
Tommi | 6bf2d31 | 2023-09-25 12:05:31 | [diff] [blame] | 109 | // Setting the type requires a constant string (e.g. |
| 110 | // cricket::LOCAL_PORT_TYPE). The type should really be an enum rather than a |
| 111 | // string, but until we make that change the lifetime attribute helps us lock |
| 112 | // things down. See also the `Port` class. |
Tommi | c7a4b2a | 2024-02-25 22:34:39 | [diff] [blame] | 113 | void set_type(webrtc::IceCandidateType type) { type_ = type; } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 114 | |
Tommi | c7a4b2a | 2024-02-25 22:34:39 | [diff] [blame] | 115 | // Simple checkers for checking the candidate type without dependency on the |
| 116 | // IceCandidateType enum. The `is_local()` and `is_stun()` names are legacy |
| 117 | // names and should now more accurately be `is_host()` and `is_srflx()`. |
Tommi | 3b2b2af | 2024-01-17 13:19:41 | [diff] [blame] | 118 | bool is_local() const; |
| 119 | bool is_stun() const; |
| 120 | bool is_prflx() const; |
| 121 | bool is_relay() const; |
| 122 | |
Tommi | be2786c | 2024-01-19 16:33:00 | [diff] [blame] | 123 | // Returns the type preference, a value between 0-126 inclusive, with 0 being |
| 124 | // the lowest preference value, as described in RFC 5245. |
| 125 | // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 |
| 126 | int type_preference() const { |
| 127 | // From https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.4 : |
| 128 | // It is RECOMMENDED that default candidates be chosen based on the |
| 129 | // likelihood of those candidates to work with the peer that is being |
| 130 | // contacted. |
| 131 | // I.e. it is recommended that relayed > reflexive > host. |
| 132 | if (is_local()) |
| 133 | return 1; // Host. |
| 134 | if (is_stun()) |
| 135 | return 2; // Reflexive. |
| 136 | if (is_relay()) |
| 137 | return 3; // Relayed. |
| 138 | return 0; // Unknown, lowest preference. |
| 139 | } |
| 140 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 141 | const std::string& network_name() const { return network_name_; } |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 142 | void set_network_name(absl::string_view network_name) { |
| 143 | Assign(network_name_, network_name); |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | rtc::AdapterType network_type() const { return network_type_; } |
| 147 | void set_network_type(rtc::AdapterType network_type) { |
| 148 | network_type_ = network_type; |
| 149 | } |
| 150 | |
Jonas Oreland | 0d13bbd | 2022-03-02 10:17:36 | [diff] [blame] | 151 | rtc::AdapterType underlying_type_for_vpn() const { |
| 152 | return underlying_type_for_vpn_; |
| 153 | } |
| 154 | void set_underlying_type_for_vpn(rtc::AdapterType network_type) { |
| 155 | underlying_type_for_vpn_ = network_type; |
| 156 | } |
| 157 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 158 | // Candidates in a new generation replace those in the old generation. |
| 159 | uint32_t generation() const { return generation_; } |
| 160 | void set_generation(uint32_t generation) { generation_ = generation; } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 161 | |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 162 | // `network_cost` measures the cost/penalty of using this candidate. A network |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 163 | // cost of 0 indicates this candidate can be used freely. A value of |
| 164 | // rtc::kNetworkCostMax indicates it should be used only as the last resort. |
| 165 | void set_network_cost(uint16_t network_cost) { |
| 166 | RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax); |
| 167 | network_cost_ = network_cost; |
| 168 | } |
| 169 | uint16_t network_cost() const { return network_cost_; } |
| 170 | |
| 171 | // An ID assigned to the network hosting the candidate. |
| 172 | uint16_t network_id() const { return network_id_; } |
| 173 | void set_network_id(uint16_t network_id) { network_id_ = network_id; } |
| 174 | |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 175 | // From RFC 5245, section-7.2.1.3: |
| 176 | // The foundation of the candidate is set to an arbitrary value, different |
| 177 | // from the foundation for all other remote candidates. |
| 178 | // Note: Use ComputeFoundation to populate this value. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 179 | const std::string& foundation() const { return foundation_; } |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 180 | |
| 181 | // TODO(tommi): Deprecate in favor of ComputeFoundation. |
| 182 | // For situations where serializing/deserializing a candidate is needed, |
| 183 | // the constructor can be used to inject a value for the foundation. |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 184 | void set_foundation(absl::string_view foundation) { |
| 185 | Assign(foundation_, foundation); |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 186 | } |
| 187 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 188 | const rtc::SocketAddress& related_address() const { return related_address_; } |
| 189 | void set_related_address(const rtc::SocketAddress& related_address) { |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 190 | related_address_ = related_address; |
| 191 | } |
| 192 | const std::string& tcptype() const { return tcptype_; } |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 193 | void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 194 | |
| 195 | // The name of the transport channel of this candidate. |
| 196 | // TODO(phoglund): remove. |
| 197 | const std::string& transport_name() const { return transport_name_; } |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 198 | void set_transport_name(absl::string_view transport_name) { |
| 199 | Assign(transport_name_, transport_name); |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // The URL of the ICE server which this candidate is gathered from. |
| 203 | const std::string& url() const { return url_; } |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 204 | void set_url(absl::string_view url) { Assign(url_, url); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 205 | |
| 206 | // Determines whether this candidate is equivalent to the given one. |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 207 | bool IsEquivalent(const Candidate& c) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 208 | |
| 209 | // Determines whether this candidate can be considered equivalent to the |
| 210 | // given one when looking for a matching candidate to remove. |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 211 | bool MatchesForRemoval(const Candidate& c) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 212 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 213 | std::string ToString() const { return ToStringInternal(false); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 214 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 215 | std::string ToSensitiveString() const { return ToStringInternal(true); } |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 216 | |
| 217 | uint32_t GetPriority(uint32_t type_preference, |
| 218 | int network_adapter_preference, |
Philipp Hancke | 17ec056 | 2023-06-16 11:58:45 | [diff] [blame] | 219 | int relay_preference, |
| 220 | bool adjust_local_preference) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 221 | |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 222 | bool operator==(const Candidate& o) const; |
| 223 | bool operator!=(const Candidate& o) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 224 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 225 | // Returns a sanitized copy configured by the given booleans. If |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 226 | // `use_host_address` is true, the returned copy has its IP removed from |
| 227 | // `address()`, which leads `address()` to be a hostname address. If |
| 228 | // `filter_related_address`, the returned copy has its related address reset |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 229 | // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that |
| 230 | // setting both booleans to false returns an identical copy to the original |
| 231 | // candidate. |
| 232 | Candidate ToSanitizedCopy(bool use_hostname_address, |
| 233 | bool filter_related_address) const; |
| 234 | |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 235 | // Computes and populates the `foundation()` field. |
| 236 | // Foundation: An arbitrary string that is the same for two candidates |
| 237 | // that have the same type, base IP address, protocol (UDP, TCP, |
| 238 | // etc.), and STUN or TURN server. If any of these are different, |
| 239 | // then the foundation will be different. Two candidate pairs with |
| 240 | // the same foundation pairs are likely to have similar network |
| 241 | // characteristics. Foundations are used in the frozen algorithm. |
Christoffer Dewerin | 7098d11 | 2024-02-26 14:38:44 | [diff] [blame] | 242 | // A session wide (peerconnection) tie-breaker is applied to the foundation, |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 243 | // adds additional randomness and must be the same for all candidates. |
Christoffer Dewerin | 7098d11 | 2024-02-26 14:38:44 | [diff] [blame] | 244 | void ComputeFoundation(const rtc::SocketAddress& base_address, |
| 245 | uint64_t tie_breaker); |
Tommi | f2431a9 | 2024-02-20 14:27:44 | [diff] [blame] | 246 | |
| 247 | // https://www.rfc-editor.org/rfc/rfc5245#section-7.2.1.3 |
| 248 | // Call to populate the foundation field for a new peer reflexive remote |
| 249 | // candidate. The type of the candidate must be "prflx". |
| 250 | // The foundation of the candidate is set to an arbitrary value, different |
| 251 | // from the foundation for all other remote candidates. |
| 252 | void ComputePrflxFoundation(); |
| 253 | |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 254 | private: |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 255 | // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment |
| 256 | // operator accepting any object implicitly convertible to std::string_view, |
| 257 | // and then we don't need this workaround. |
| 258 | static void Assign(std::string& s, absl::string_view view); |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 259 | std::string ToStringInternal(bool sensitive) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 260 | |
| 261 | std::string id_; |
| 262 | int component_; |
| 263 | std::string protocol_; |
| 264 | std::string relay_protocol_; |
| 265 | rtc::SocketAddress address_; |
| 266 | uint32_t priority_; |
| 267 | std::string username_; |
| 268 | std::string password_; |
Tommi | c7a4b2a | 2024-02-25 22:34:39 | [diff] [blame] | 269 | webrtc::IceCandidateType type_ = webrtc::IceCandidateType::kHost; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 270 | std::string network_name_; |
| 271 | rtc::AdapterType network_type_; |
Jonas Oreland | 0d13bbd | 2022-03-02 10:17:36 | [diff] [blame] | 272 | rtc::AdapterType underlying_type_for_vpn_; |
Patrik Höglund | e2d6a06 | 2017-10-05 12:53:33 | [diff] [blame] | 273 | uint32_t generation_; |
| 274 | std::string foundation_; |
| 275 | rtc::SocketAddress related_address_; |
| 276 | std::string tcptype_; |
| 277 | std::string transport_name_; |
| 278 | uint16_t network_id_; |
| 279 | uint16_t network_cost_; |
| 280 | std::string url_; |
| 281 | }; |
| 282 | |
| 283 | } // namespace cricket |
| 284 | |
| 285 | #endif // API_CANDIDATE_H_ |