Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [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 | |
| 14 | #include <limits.h> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <string> |
| 19 | |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 20 | #include "absl/strings/string_view.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 22 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/socket_address.h" |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 24 | #include "rtc_base/system/rtc_export.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 25 | |
| 26 | namespace cricket { |
| 27 | |
Philipp Hancke | 41a8357 | 2022-11-28 14:47:45 +0100 | [diff] [blame] | 28 | // TURN servers are limited to 32 in accordance with |
| 29 | // https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers |
| 30 | static constexpr size_t kMaxTurnServers = 32; |
| 31 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 32 | // Candidate for ICE based connection discovery. |
| 33 | // TODO(phoglund): remove things in here that are not needed in the public API. |
| 34 | |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 35 | class RTC_EXPORT Candidate { |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 36 | public: |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 37 | Candidate(); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 38 | // TODO(pthatcher): Match the ordering and param list as per RFC 5245 |
| 39 | // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1 |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 40 | Candidate(int component, |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 41 | absl::string_view protocol, |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 42 | const rtc::SocketAddress& address, |
| 43 | uint32_t priority, |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 44 | absl::string_view username, |
| 45 | absl::string_view password, |
| 46 | absl::string_view type, |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 47 | uint32_t generation, |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 48 | absl::string_view foundation, |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 49 | uint16_t network_id = 0, |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 50 | uint16_t network_cost = 0); |
| 51 | Candidate(const Candidate&); |
| 52 | ~Candidate(); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 53 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 54 | const std::string& id() const { return id_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 55 | void set_id(absl::string_view id) { Assign(id_, id); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 56 | |
| 57 | int component() const { return component_; } |
| 58 | void set_component(int component) { component_ = component; } |
| 59 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | const std::string& protocol() const { return protocol_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 61 | void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 62 | |
| 63 | // The protocol used to talk to relay. |
| 64 | const std::string& relay_protocol() const { return relay_protocol_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 65 | void set_relay_protocol(absl::string_view protocol) { |
| 66 | Assign(relay_protocol_, protocol); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 69 | const rtc::SocketAddress& address() const { return address_; } |
| 70 | void set_address(const rtc::SocketAddress& address) { address_ = address; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 71 | |
| 72 | uint32_t priority() const { return priority_; } |
| 73 | void set_priority(const uint32_t priority) { priority_ = priority; } |
| 74 | |
| 75 | // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc |
| 76 | // doesn't use it. |
| 77 | // Maps old preference (which was 0.0-1.0) to match priority (which |
| 78 | // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see |
| 79 | // https://docs.google.com/a/google.com/document/d/ |
| 80 | // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit |
| 81 | float preference() const { |
| 82 | // The preference value is clamped to two decimal precision. |
| 83 | return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0); |
| 84 | } |
| 85 | |
| 86 | // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc |
| 87 | // doesn't use it. |
| 88 | void set_preference(float preference) { |
| 89 | // Limiting priority to UINT_MAX when value exceeds uint32_t max. |
| 90 | // This can happen for e.g. when preference = 3. |
| 91 | uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24; |
| 92 | priority_ = static_cast<uint32_t>( |
| 93 | std::min(prio_val, static_cast<uint64_t>(UINT_MAX))); |
| 94 | } |
| 95 | |
| 96 | // TODO(honghaiz): Change to usernameFragment or ufrag. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 97 | const std::string& username() const { return username_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 98 | void set_username(absl::string_view username) { Assign(username_, username); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 99 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | const std::string& password() const { return password_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 101 | void set_password(absl::string_view password) { Assign(password_, password); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 102 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | const std::string& type() const { return type_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 104 | void set_type(absl::string_view type) { Assign(type_, type); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 105 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 106 | const std::string& network_name() const { return network_name_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 107 | void set_network_name(absl::string_view network_name) { |
| 108 | Assign(network_name_, network_name); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | rtc::AdapterType network_type() const { return network_type_; } |
| 112 | void set_network_type(rtc::AdapterType network_type) { |
| 113 | network_type_ = network_type; |
| 114 | } |
| 115 | |
Jonas Oreland | 0d13bbd | 2022-03-02 11:17:36 +0100 | [diff] [blame] | 116 | rtc::AdapterType underlying_type_for_vpn() const { |
| 117 | return underlying_type_for_vpn_; |
| 118 | } |
| 119 | void set_underlying_type_for_vpn(rtc::AdapterType network_type) { |
| 120 | underlying_type_for_vpn_ = network_type; |
| 121 | } |
| 122 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 123 | // Candidates in a new generation replace those in the old generation. |
| 124 | uint32_t generation() const { return generation_; } |
| 125 | void set_generation(uint32_t generation) { generation_ = generation; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 126 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 127 | // `network_cost` measures the cost/penalty of using this candidate. A network |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 128 | // cost of 0 indicates this candidate can be used freely. A value of |
| 129 | // rtc::kNetworkCostMax indicates it should be used only as the last resort. |
| 130 | void set_network_cost(uint16_t network_cost) { |
| 131 | RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax); |
| 132 | network_cost_ = network_cost; |
| 133 | } |
| 134 | uint16_t network_cost() const { return network_cost_; } |
| 135 | |
| 136 | // An ID assigned to the network hosting the candidate. |
| 137 | uint16_t network_id() const { return network_id_; } |
| 138 | void set_network_id(uint16_t network_id) { network_id_ = network_id; } |
| 139 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 140 | const std::string& foundation() const { return foundation_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 141 | void set_foundation(absl::string_view foundation) { |
| 142 | Assign(foundation_, foundation); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 145 | const rtc::SocketAddress& related_address() const { return related_address_; } |
| 146 | void set_related_address(const rtc::SocketAddress& related_address) { |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 147 | related_address_ = related_address; |
| 148 | } |
| 149 | const std::string& tcptype() const { return tcptype_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 150 | void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 151 | |
| 152 | // The name of the transport channel of this candidate. |
| 153 | // TODO(phoglund): remove. |
| 154 | const std::string& transport_name() const { return transport_name_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 155 | void set_transport_name(absl::string_view transport_name) { |
| 156 | Assign(transport_name_, transport_name); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // The URL of the ICE server which this candidate is gathered from. |
| 160 | const std::string& url() const { return url_; } |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 161 | void set_url(absl::string_view url) { Assign(url_, url); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 162 | |
| 163 | // Determines whether this candidate is equivalent to the given one. |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 164 | bool IsEquivalent(const Candidate& c) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 165 | |
| 166 | // Determines whether this candidate can be considered equivalent to the |
| 167 | // given one when looking for a matching candidate to remove. |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 168 | bool MatchesForRemoval(const Candidate& c) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 169 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 170 | std::string ToString() const { return ToStringInternal(false); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 171 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 172 | std::string ToSensitiveString() const { return ToStringInternal(true); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 173 | |
| 174 | uint32_t GetPriority(uint32_t type_preference, |
| 175 | int network_adapter_preference, |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 176 | int relay_preference) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 177 | |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 178 | bool operator==(const Candidate& o) const; |
| 179 | bool operator!=(const Candidate& o) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 180 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 181 | // Returns a sanitized copy configured by the given booleans. If |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 182 | // `use_host_address` is true, the returned copy has its IP removed from |
| 183 | // `address()`, which leads `address()` to be a hostname address. If |
| 184 | // `filter_related_address`, the returned copy has its related address reset |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 185 | // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that |
| 186 | // setting both booleans to false returns an identical copy to the original |
| 187 | // candidate. |
| 188 | Candidate ToSanitizedCopy(bool use_hostname_address, |
| 189 | bool filter_related_address) const; |
| 190 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 191 | private: |
Niels Möller | d4aa3a3 | 2021-09-29 13:23:01 +0200 | [diff] [blame] | 192 | // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment |
| 193 | // operator accepting any object implicitly convertible to std::string_view, |
| 194 | // and then we don't need this workaround. |
| 195 | static void Assign(std::string& s, absl::string_view view); |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 196 | std::string ToStringInternal(bool sensitive) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 197 | |
| 198 | std::string id_; |
| 199 | int component_; |
| 200 | std::string protocol_; |
| 201 | std::string relay_protocol_; |
| 202 | rtc::SocketAddress address_; |
| 203 | uint32_t priority_; |
| 204 | std::string username_; |
| 205 | std::string password_; |
| 206 | std::string type_; |
| 207 | std::string network_name_; |
| 208 | rtc::AdapterType network_type_; |
Jonas Oreland | 0d13bbd | 2022-03-02 11:17:36 +0100 | [diff] [blame] | 209 | rtc::AdapterType underlying_type_for_vpn_; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 210 | uint32_t generation_; |
| 211 | std::string foundation_; |
| 212 | rtc::SocketAddress related_address_; |
| 213 | std::string tcptype_; |
| 214 | std::string transport_name_; |
| 215 | uint16_t network_id_; |
| 216 | uint16_t network_cost_; |
| 217 | std::string url_; |
| 218 | }; |
| 219 | |
| 220 | } // namespace cricket |
| 221 | |
| 222 | #endif // API_CANDIDATE_H_ |