| 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 |
| 20 | #include "absl/base/attributes.h" |
| 21 | #include "absl/strings/string_view.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/network_constants.h" |
| 24 | #include "rtc_base/socket_address.h" |
| 25 | #include "rtc_base/system/rtc_export.h" |
| 26 |
| 27 | namespace webrtc { |
| 28 | enum class IceCandidateType : int { kHost, kSrflx, kPrflx, kRelay }; |
| 29 | } // namespace webrtc |
| 30 |
| 31 | namespace cricket { |
| 32 |
| 33 | // TODO(tommi): These are temporarily here, moved from `port.h` and will |
| 34 | // eventually be removed once we use enums instead of strings for these values. |
| 35 | RTC_EXPORT extern const absl::string_view LOCAL_PORT_TYPE; |
| 36 | RTC_EXPORT extern const absl::string_view STUN_PORT_TYPE; |
| 37 | RTC_EXPORT extern const absl::string_view PRFLX_PORT_TYPE; |
| 38 | RTC_EXPORT extern const absl::string_view RELAY_PORT_TYPE; |
| 39 |
| 40 | // TURN servers are limited to 32 in accordance with |
| 41 | // https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers |
| 42 | static constexpr size_t kMaxTurnServers = 32; |
| 43 |
| 44 | // Candidate for ICE based connection discovery. |
| 45 | // TODO(phoglund): remove things in here that are not needed in the public API. |
| 46 |
| 47 | class RTC_EXPORT Candidate { |
| 48 | public: |
| 49 | Candidate(); |
| 50 | // TODO(pthatcher): Match the ordering and param list as per RFC 5245 |
| 51 | // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1 |
| 52 | Candidate(int component, |
| 53 | absl::string_view protocol, |
| 54 | const rtc::SocketAddress& address, |
| 55 | uint32_t priority, |
| 56 | absl::string_view username, |
| 57 | absl::string_view password, |
| 58 | absl::string_view type ABSL_ATTRIBUTE_LIFETIME_BOUND, |
| 59 | uint32_t generation, |
| 60 | absl::string_view foundation, |
| 61 | uint16_t network_id = 0, |
| 62 | uint16_t network_cost = 0); |
| 63 | Candidate(const Candidate&); |
| 64 | ~Candidate(); |
| 65 |
| 66 | // 8 character long randomized ID string for logging purposes. |
| 67 | const std::string& id() const { return id_; } |
| 68 | // Generates a new, 8 character long, id. |
| 69 | void generate_id(); |
| 70 | // TODO(tommi): Callers should use generate_id(). Remove. |
| 71 | [[deprecated]] void set_id(absl::string_view id) { Assign(id_, id); } |
| 72 |
| 73 | int component() const { return component_; } |
| 74 | void set_component(int component) { component_ = component; } |
| 75 |
| 76 | const std::string& protocol() const { return protocol_; } |
| 77 | void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); } |
| 78 |
| 79 | // The protocol used to talk to relay. |
| 80 | const std::string& relay_protocol() const { return relay_protocol_; } |
| 81 | void set_relay_protocol(absl::string_view protocol) { |
| 82 | Assign(relay_protocol_, protocol); |
| 83 | } |
| 84 |
| 85 | const rtc::SocketAddress& address() const { return address_; } |
| 86 | void set_address(const rtc::SocketAddress& address) { address_ = address; } |
| 87 |
| 88 | uint32_t priority() const { return priority_; } |
| 89 | void set_priority(const uint32_t priority) { priority_ = priority; } |
| 90 |
| 91 | // TODO(honghaiz): Change to usernameFragment or ufrag. |
| 92 | const std::string& username() const { return username_; } |
| 93 | void set_username(absl::string_view username) { Assign(username_, username); } |
| 94 |
| 95 | const std::string& password() const { return password_; } |
| 96 | void set_password(absl::string_view password) { Assign(password_, password); } |
| 97 |
| 98 | const std::string& type() const { return type_; } |
| 99 |
| 100 | // Returns the name of the candidate type as specified in |
| 101 | // https://datatracker.ietf.org/doc/html/rfc5245#section-15.1 |
| 102 | absl::string_view type_name() const; |
| 103 |
| 104 | // Setting the type requires a constant string (e.g. |
| 105 | // cricket::LOCAL_PORT_TYPE). The type should really be an enum rather than a |
| 106 | // string, but until we make that change the lifetime attribute helps us lock |
| 107 | // things down. See also the `Port` class. |
| 108 | void set_type(absl::string_view type ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
| 109 | Assign(type_, type); |
| 110 | } |
| 111 |
| 112 | // Provide these simple checkers to abstract away dependency on the port types |
| 113 | // that are currently defined outside of Candidate. This will ease the change |
| 114 | // from the string type to an enum. |
| 115 | bool is_local() const; |
| 116 | bool is_stun() const; |
| 117 | bool is_prflx() const; |
| 118 | bool is_relay() const; |
| 119 |
| 120 | // Returns the type preference, a value between 0-126 inclusive, with 0 being |
| 121 | // the lowest preference value, as described in RFC 5245. |
| 122 | // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 |
| 123 | int type_preference() const { |
| 124 | // From https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.4 : |
| 125 | // It is RECOMMENDED that default candidates be chosen based on the |
| 126 | // likelihood of those candidates to work with the peer that is being |
| 127 | // contacted. |
| 128 | // I.e. it is recommended that relayed > reflexive > host. |
| 129 | if (is_local()) |
| 130 | return 1; // Host. |
| 131 | if (is_stun()) |
| 132 | return 2; // Reflexive. |
| 133 | if (is_relay()) |
| 134 | return 3; // Relayed. |
| 135 | return 0; // Unknown, lowest preference. |
| 136 | } |
| 137 |
| 138 | const std::string& network_name() const { return network_name_; } |
| 139 | void set_network_name(absl::string_view network_name) { |
| 140 | Assign(network_name_, network_name); |
| 141 | } |
| 142 |
| 143 | rtc::AdapterType network_type() const { return network_type_; } |
| 144 | void set_network_type(rtc::AdapterType network_type) { |
| 145 | network_type_ = network_type; |
| 146 | } |
| 147 |
| 148 | rtc::AdapterType underlying_type_for_vpn() const { |
| 149 | return underlying_type_for_vpn_; |
| 150 | } |
| 151 | void set_underlying_type_for_vpn(rtc::AdapterType network_type) { |
| 152 | underlying_type_for_vpn_ = network_type; |
| 153 | } |
| 154 |
| 155 | // Candidates in a new generation replace those in the old generation. |
| 156 | uint32_t generation() const { return generation_; } |
| 157 | void set_generation(uint32_t generation) { generation_ = generation; } |
| 158 |
| 159 | // `network_cost` measures the cost/penalty of using this candidate. A network |
| 160 | // cost of 0 indicates this candidate can be used freely. A value of |
| 161 | // rtc::kNetworkCostMax indicates it should be used only as the last resort. |
| 162 | void set_network_cost(uint16_t network_cost) { |
| 163 | RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax); |
| 164 | network_cost_ = network_cost; |
| 165 | } |
| 166 | uint16_t network_cost() const { return network_cost_; } |
| 167 |
| 168 | // An ID assigned to the network hosting the candidate. |
| 169 | uint16_t network_id() const { return network_id_; } |
| 170 | void set_network_id(uint16_t network_id) { network_id_ = network_id; } |
| 171 |
| 172 | const std::string& foundation() const { return foundation_; } |
| 173 | void set_foundation(absl::string_view foundation) { |
| 174 | Assign(foundation_, foundation); |
| 175 | } |
| 176 |
| 177 | const rtc::SocketAddress& related_address() const { return related_address_; } |
| 178 | void set_related_address(const rtc::SocketAddress& related_address) { |
| 179 | related_address_ = related_address; |
| 180 | } |
| 181 | const std::string& tcptype() const { return tcptype_; } |
| 182 | void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); } |
| 183 |
| 184 | // The name of the transport channel of this candidate. |
| 185 | // TODO(phoglund): remove. |
| 186 | const std::string& transport_name() const { return transport_name_; } |
| 187 | void set_transport_name(absl::string_view transport_name) { |
| 188 | Assign(transport_name_, transport_name); |
| 189 | } |
| 190 |
| 191 | // The URL of the ICE server which this candidate is gathered from. |
| 192 | const std::string& url() const { return url_; } |
| 193 | void set_url(absl::string_view url) { Assign(url_, url); } |
| 194 |
| 195 | // Determines whether this candidate is equivalent to the given one. |
| 196 | bool IsEquivalent(const Candidate& c) const; |
| 197 |
| 198 | // Determines whether this candidate can be considered equivalent to the |
| 199 | // given one when looking for a matching candidate to remove. |
| 200 | bool MatchesForRemoval(const Candidate& c) const; |
| 201 |
| 202 | std::string ToString() const { return ToStringInternal(false); } |
| 203 |
| 204 | std::string ToSensitiveString() const { return ToStringInternal(true); } |
| 205 |
| 206 | uint32_t GetPriority(uint32_t type_preference, |
| 207 | int network_adapter_preference, |
| 208 | int relay_preference, |
| 209 | bool adjust_local_preference) const; |
| 210 |
| 211 | bool operator==(const Candidate& o) const; |
| 212 | bool operator!=(const Candidate& o) const; |
| 213 |
| 214 | // Returns a sanitized copy configured by the given booleans. If |
| 215 | // `use_host_address` is true, the returned copy has its IP removed from |
| 216 | // `address()`, which leads `address()` to be a hostname address. If |
| 217 | // `filter_related_address`, the returned copy has its related address reset |
| 218 | // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that |
| 219 | // setting both booleans to false returns an identical copy to the original |
| 220 | // candidate. |
| 221 | Candidate ToSanitizedCopy(bool use_hostname_address, |
| 222 | bool filter_related_address) const; |
| 223 |
| 224 | private: |
| 225 | // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment |
| 226 | // operator accepting any object implicitly convertible to std::string_view, |
| 227 | // and then we don't need this workaround. |
| 228 | static void Assign(std::string& s, absl::string_view view); |
| 229 | std::string ToStringInternal(bool sensitive) const; |
| 230 |
| 231 | std::string id_; |
| 232 | int component_; |
| 233 | std::string protocol_; |
| 234 | std::string relay_protocol_; |
| 235 | rtc::SocketAddress address_; |
| 236 | uint32_t priority_; |
| 237 | std::string username_; |
| 238 | std::string password_; |
| 239 | std::string type_; |
| 240 | std::string network_name_; |
| 241 | rtc::AdapterType network_type_; |
| 242 | rtc::AdapterType underlying_type_for_vpn_; |
| 243 | uint32_t generation_; |
| 244 | std::string foundation_; |
| 245 | rtc::SocketAddress related_address_; |
| 246 | std::string tcptype_; |
| 247 | std::string transport_name_; |
| 248 | uint16_t network_id_; |
| 249 | uint16_t network_cost_; |
| 250 | std::string url_; |
| 251 | }; |
| 252 |
| 253 | } // namespace cricket |
| 254 |
| 255 | #endif // API_CANDIDATE_H_ |
| 256 |