| 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 | #include "api/candidate.h" |
| 12 |
| 13 | #include "absl/base/attributes.h" |
| 14 | #include "rtc_base/helpers.h" |
| 15 | #include "rtc_base/ip_address.h" |
| 16 | #include "rtc_base/logging.h" |
| 17 | #include "rtc_base/strings/string_builder.h" |
| 18 |
| 19 | namespace cricket { |
| 20 |
| 21 | ABSL_CONST_INIT const absl::string_view LOCAL_PORT_TYPE = "local"; |
| 22 | ABSL_CONST_INIT const absl::string_view STUN_PORT_TYPE = "stun"; |
| 23 | ABSL_CONST_INIT const absl::string_view PRFLX_PORT_TYPE = "prflx"; |
| 24 | ABSL_CONST_INIT const absl::string_view RELAY_PORT_TYPE = "relay"; |
| 25 |
| 26 | Candidate::Candidate() |
| 27 | : id_(rtc::CreateRandomString(8)), |
| 28 | component_(0), |
| 29 | priority_(0), |
| 30 | type_(LOCAL_PORT_TYPE), |
| 31 | network_type_(rtc::ADAPTER_TYPE_UNKNOWN), |
| 32 | underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN), |
| 33 | generation_(0), |
| 34 | network_id_(0), |
| 35 | network_cost_(0) {} |
| 36 |
| 37 | Candidate::Candidate(int component, |
| 38 | absl::string_view protocol, |
| 39 | const rtc::SocketAddress& address, |
| 40 | uint32_t priority, |
| 41 | absl::string_view username, |
| 42 | absl::string_view password, |
| 43 | absl::string_view type, |
| 44 | uint32_t generation, |
| 45 | absl::string_view foundation, |
| 46 | uint16_t network_id, |
| 47 | uint16_t network_cost) |
| 48 | : id_(rtc::CreateRandomString(8)), |
| 49 | component_(component), |
| 50 | protocol_(protocol), |
| 51 | address_(address), |
| 52 | priority_(priority), |
| 53 | username_(username), |
| 54 | password_(password), |
| 55 | type_(type), |
| 56 | network_type_(rtc::ADAPTER_TYPE_UNKNOWN), |
| 57 | underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN), |
| 58 | generation_(generation), |
| 59 | foundation_(foundation), |
| 60 | network_id_(network_id), |
| 61 | network_cost_(network_cost) {} |
| 62 |
| 63 | Candidate::Candidate(const Candidate&) = default; |
| 64 |
| 65 | Candidate::~Candidate() = default; |
| 66 |
| 67 | void Candidate::generate_id() { |
| 68 | id_ = rtc::CreateRandomString(8); |
| 69 | } |
| 70 |
| 71 | bool Candidate::is_local() const { |
| 72 | return type_ == LOCAL_PORT_TYPE; |
| 73 | } |
| 74 | bool Candidate::is_stun() const { |
| 75 | return type_ == STUN_PORT_TYPE; |
| 76 | } |
| 77 | bool Candidate::is_prflx() const { |
| 78 | return type_ == PRFLX_PORT_TYPE; |
| 79 | } |
| 80 | bool Candidate::is_relay() const { |
| 81 | return type_ == RELAY_PORT_TYPE; |
| 82 | } |
| 83 |
| 84 | absl::string_view Candidate::type_name() const { |
| 85 | // The LOCAL_PORT_TYPE and STUN_PORT_TYPE constants are not the standard type |
| 86 | // names, so check for those specifically. For other types, `type_` will have |
| 87 | // the correct name. |
| 88 | if (is_local()) |
| 89 | return "host"; |
| 90 | if (is_stun()) |
| 91 | return "srflx"; |
| 92 | return type_; |
| 93 | } |
| 94 |
| 95 | bool Candidate::IsEquivalent(const Candidate& c) const { |
| 96 | // We ignore the network name, since that is just debug information, and |
| 97 | // the priority and the network cost, since they should be the same if the |
| 98 | // rest are. |
| 99 | return (component_ == c.component_) && (protocol_ == c.protocol_) && |
| 100 | (address_ == c.address_) && (username_ == c.username_) && |
| 101 | (password_ == c.password_) && (type_ == c.type_) && |
| 102 | (generation_ == c.generation_) && (foundation_ == c.foundation_) && |
| 103 | (related_address_ == c.related_address_) && |
| 104 | (network_id_ == c.network_id_); |
| 105 | } |
| 106 |
| 107 | bool Candidate::MatchesForRemoval(const Candidate& c) const { |
| 108 | return component_ == c.component_ && protocol_ == c.protocol_ && |
| 109 | address_ == c.address_; |
| 110 | } |
| 111 |
| 112 | std::string Candidate::ToStringInternal(bool sensitive) const { |
| 113 | rtc::StringBuilder ost; |
| 114 | std::string address = |
| 115 | sensitive ? address_.ToSensitiveString() : address_.ToString(); |
| 116 | std::string related_address = sensitive ? related_address_.ToSensitiveString() |
| 117 | : related_address_.ToString(); |
| 118 | ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_ |
| 119 | << ":" << protocol_ << ":" << priority_ << ":" << address << ":" |
| 120 | << type_name() << ":" << related_address << ":" << username_ << ":" |
| 121 | << password_ << ":" << network_id_ << ":" << network_cost_ << ":" |
| 122 | << generation_ << "]"; |
| 123 | return ost.Release(); |
| 124 | } |
| 125 |
| 126 | uint32_t Candidate::GetPriority(uint32_t type_preference, |
| 127 | int network_adapter_preference, |
| 128 | int relay_preference, |
| 129 | bool adjust_local_preference) const { |
| 130 | // RFC 5245 - 4.1.2.1. |
| 131 | // priority = (2^24)*(type preference) + |
| 132 | // (2^8)*(local preference) + |
| 133 | // (2^0)*(256 - component ID) |
| 134 |
| 135 | // `local_preference` length is 2 bytes, 0-65535 inclusive. |
| 136 | // In our implemenation we will partion local_preference into |
| 137 | // 0 1 |
| 138 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 139 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 140 | // | NIC Pref | Addr Pref | |
| 141 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 142 | // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired. |
| 143 | // Addr Pref - Address preference value as per RFC 3484. |
| 144 | // local preference = (NIC Type << 8 | Addr_Pref) + relay preference. |
| 145 | // The relay preference is based on the number of TURN servers, the |
| 146 | // first TURN server gets the highest preference. |
| 147 | int addr_pref = IPAddressPrecedence(address_.ipaddr()); |
| 148 | int local_preference = |
| 149 | ((network_adapter_preference << 8) | addr_pref) + relay_preference; |
| 150 |
| 151 | // Ensure that the added relay preference will not result in a relay candidate |
| 152 | // whose STUN priority attribute has a higher priority than a server-reflexive |
| 153 | // candidate. |
| 154 | // The STUN priority attribute is calculated as |
| 155 | // (peer-reflexive type preference) << 24 | (priority & 0x00FFFFFF) |
| 156 | // as described in |
| 157 | // https://www.rfc-editor.org/rfc/rfc5245#section-7.1.2.1 |
| 158 | // To satisfy that condition, add kMaxTurnServers to the local preference. |
| 159 | // This can not overflow the field width since the highest "NIC pref" |
| 160 | // assigned is kHighestNetworkPreference = 127 |
| 161 | RTC_DCHECK_LT(local_preference + kMaxTurnServers, 0x10000); |
| 162 | if (adjust_local_preference && relay_protocol_.empty()) { |
| 163 | local_preference += kMaxTurnServers; |
| 164 | } |
| 165 |
| 166 | return (type_preference << 24) | (local_preference << 8) | (256 - component_); |
| 167 | } |
| 168 |
| 169 | bool Candidate::operator==(const Candidate& o) const { |
| 170 | return id_ == o.id_ && component_ == o.component_ && |
| 171 | protocol_ == o.protocol_ && relay_protocol_ == o.relay_protocol_ && |
| 172 | address_ == o.address_ && priority_ == o.priority_ && |
| 173 | username_ == o.username_ && password_ == o.password_ && |
| 174 | type_ == o.type_ && network_name_ == o.network_name_ && |
| 175 | network_type_ == o.network_type_ && generation_ == o.generation_ && |
| 176 | foundation_ == o.foundation_ && |
| 177 | related_address_ == o.related_address_ && tcptype_ == o.tcptype_ && |
| 178 | transport_name_ == o.transport_name_ && network_id_ == o.network_id_; |
| 179 | } |
| 180 |
| 181 | bool Candidate::operator!=(const Candidate& o) const { |
| 182 | return !(*this == o); |
| 183 | } |
| 184 |
| 185 | Candidate Candidate::ToSanitizedCopy(bool use_hostname_address, |
| 186 | bool filter_related_address) const { |
| 187 | Candidate copy(*this); |
| 188 | if (use_hostname_address) { |
| 189 | rtc::IPAddress ip; |
| 190 | if (address().hostname().empty()) { |
| 191 | // IP needs to be redacted, but no hostname available. |
| 192 | rtc::SocketAddress redacted_addr("redacted-ip.invalid", address().port()); |
| 193 | copy.set_address(redacted_addr); |
| 194 | } else if (IPFromString(address().hostname(), &ip)) { |
| 195 | // The hostname is an IP literal, and needs to be redacted too. |
| 196 | rtc::SocketAddress redacted_addr("redacted-literal.invalid", |
| 197 | address().port()); |
| 198 | copy.set_address(redacted_addr); |
| 199 | } else { |
| 200 | rtc::SocketAddress hostname_only_addr(address().hostname(), |
| 201 | address().port()); |
| 202 | copy.set_address(hostname_only_addr); |
| 203 | } |
| 204 | } |
| 205 | if (filter_related_address) { |
| 206 | copy.set_related_address( |
| 207 | rtc::EmptySocketAddressWithFamily(copy.address().family())); |
| 208 | } |
| 209 | return copy; |
| 210 | } |
| 211 |
| 212 | void Candidate::Assign(std::string& s, absl::string_view view) { |
| 213 | // Assigning via a temporary object, like s = std::string(view), results in |
| 214 | // binary size bloat. To avoid that, extract pointer and size from the |
| 215 | // string view, and use std::string::assign method. |
| 216 | s.assign(view.data(), view.size()); |
| 217 | } |
| 218 |
| 219 | } // namespace cricket |
| 220 |