Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #include "api/candidate.h" |
| 12 | |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 13 | #include "rtc_base/helpers.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 14 | #include "rtc_base/ip_address.h" |
Harald Alvestrand | e3ceb88 | 2021-06-15 13:02:41 | [diff] [blame] | 15 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 16 | #include "rtc_base/strings/string_builder.h" |
| 17 | |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 18 | namespace cricket { |
| 19 | |
| 20 | Candidate::Candidate() |
| 21 | : id_(rtc::CreateRandomString(8)), |
| 22 | component_(0), |
| 23 | priority_(0), |
| 24 | network_type_(rtc::ADAPTER_TYPE_UNKNOWN), |
Byoungchan Lee | 5a92577 | 2022-10-18 17:43:18 | [diff] [blame] | 25 | underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN), |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 26 | generation_(0), |
| 27 | network_id_(0), |
| 28 | network_cost_(0) {} |
| 29 | |
| 30 | Candidate::Candidate(int component, |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 31 | absl::string_view protocol, |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 32 | const rtc::SocketAddress& address, |
| 33 | uint32_t priority, |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 34 | absl::string_view username, |
| 35 | absl::string_view password, |
| 36 | absl::string_view type, |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 37 | uint32_t generation, |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 38 | absl::string_view foundation, |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 39 | uint16_t network_id, |
| 40 | uint16_t network_cost) |
| 41 | : id_(rtc::CreateRandomString(8)), |
| 42 | component_(component), |
| 43 | protocol_(protocol), |
| 44 | address_(address), |
| 45 | priority_(priority), |
| 46 | username_(username), |
| 47 | password_(password), |
| 48 | type_(type), |
| 49 | network_type_(rtc::ADAPTER_TYPE_UNKNOWN), |
Byoungchan Lee | 5a92577 | 2022-10-18 17:43:18 | [diff] [blame] | 50 | underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN), |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 51 | generation_(generation), |
| 52 | foundation_(foundation), |
| 53 | network_id_(network_id), |
| 54 | network_cost_(network_cost) {} |
| 55 | |
| 56 | Candidate::Candidate(const Candidate&) = default; |
| 57 | |
| 58 | Candidate::~Candidate() = default; |
| 59 | |
| 60 | bool Candidate::IsEquivalent(const Candidate& c) const { |
| 61 | // We ignore the network name, since that is just debug information, and |
| 62 | // the priority and the network cost, since they should be the same if the |
| 63 | // rest are. |
| 64 | return (component_ == c.component_) && (protocol_ == c.protocol_) && |
| 65 | (address_ == c.address_) && (username_ == c.username_) && |
| 66 | (password_ == c.password_) && (type_ == c.type_) && |
| 67 | (generation_ == c.generation_) && (foundation_ == c.foundation_) && |
| 68 | (related_address_ == c.related_address_) && |
| 69 | (network_id_ == c.network_id_); |
| 70 | } |
| 71 | |
| 72 | bool Candidate::MatchesForRemoval(const Candidate& c) const { |
| 73 | return component_ == c.component_ && protocol_ == c.protocol_ && |
| 74 | address_ == c.address_; |
| 75 | } |
| 76 | |
| 77 | std::string Candidate::ToStringInternal(bool sensitive) const { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 78 | rtc::StringBuilder ost; |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 79 | std::string address = |
| 80 | sensitive ? address_.ToSensitiveString() : address_.ToString(); |
| 81 | ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_ |
| 82 | << ":" << protocol_ << ":" << priority_ << ":" << address << ":" << type_ |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 83 | << ":" << related_address_.ToString() << ":" << username_ << ":" |
| 84 | << password_ << ":" << network_id_ << ":" << network_cost_ << ":" |
| 85 | << generation_ << "]"; |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 86 | return ost.Release(); |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | uint32_t Candidate::GetPriority(uint32_t type_preference, |
| 90 | int network_adapter_preference, |
| 91 | int relay_preference) const { |
| 92 | // RFC 5245 - 4.1.2.1. |
| 93 | // priority = (2^24)*(type preference) + |
| 94 | // (2^8)*(local preference) + |
| 95 | // (2^0)*(256 - component ID) |
| 96 | |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 97 | // `local_preference` length is 2 bytes, 0-65535 inclusive. |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 98 | // In our implemenation we will partion local_preference into |
| 99 | // 0 1 |
| 100 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 101 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 102 | // | NIC Pref | Addr Pref | |
| 103 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 104 | // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired. |
| 105 | // Addr Pref - Address preference value as per RFC 3484. |
Philipp Hancke | ee4c930 | 2021-09-28 21:19:47 | [diff] [blame] | 106 | // local preference = (NIC Type << 8 | Addr_Pref) + relay preference. |
| 107 | // The relay preference is based on the number of TURN servers, the |
| 108 | // first TURN server gets the highest preference. |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 109 | |
| 110 | int addr_pref = IPAddressPrecedence(address_.ipaddr()); |
| 111 | int local_preference = |
| 112 | ((network_adapter_preference << 8) | addr_pref) + relay_preference; |
| 113 | |
| 114 | return (type_preference << 24) | (local_preference << 8) | (256 - component_); |
| 115 | } |
| 116 | |
| 117 | bool Candidate::operator==(const Candidate& o) const { |
| 118 | return id_ == o.id_ && component_ == o.component_ && |
| 119 | protocol_ == o.protocol_ && relay_protocol_ == o.relay_protocol_ && |
| 120 | address_ == o.address_ && priority_ == o.priority_ && |
| 121 | username_ == o.username_ && password_ == o.password_ && |
| 122 | type_ == o.type_ && network_name_ == o.network_name_ && |
| 123 | network_type_ == o.network_type_ && generation_ == o.generation_ && |
| 124 | foundation_ == o.foundation_ && |
| 125 | related_address_ == o.related_address_ && tcptype_ == o.tcptype_ && |
| 126 | transport_name_ == o.transport_name_ && network_id_ == o.network_id_; |
| 127 | } |
| 128 | |
| 129 | bool Candidate::operator!=(const Candidate& o) const { |
| 130 | return !(*this == o); |
| 131 | } |
| 132 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 133 | Candidate Candidate::ToSanitizedCopy(bool use_hostname_address, |
| 134 | bool filter_related_address) const { |
| 135 | Candidate copy(*this); |
| 136 | if (use_hostname_address) { |
Harald Alvestrand | e3ceb88 | 2021-06-15 13:02:41 | [diff] [blame] | 137 | rtc::IPAddress ip; |
| 138 | if (address().hostname().empty()) { |
| 139 | // IP needs to be redacted, but no hostname available. |
| 140 | rtc::SocketAddress redacted_addr("redacted-ip.invalid", address().port()); |
| 141 | copy.set_address(redacted_addr); |
| 142 | } else if (IPFromString(address().hostname(), &ip)) { |
| 143 | // The hostname is an IP literal, and needs to be redacted too. |
| 144 | rtc::SocketAddress redacted_addr("redacted-literal.invalid", |
| 145 | address().port()); |
| 146 | copy.set_address(redacted_addr); |
| 147 | } else { |
| 148 | rtc::SocketAddress hostname_only_addr(address().hostname(), |
| 149 | address().port()); |
| 150 | copy.set_address(hostname_only_addr); |
| 151 | } |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 152 | } |
| 153 | if (filter_related_address) { |
| 154 | copy.set_related_address( |
| 155 | rtc::EmptySocketAddressWithFamily(copy.address().family())); |
| 156 | } |
| 157 | return copy; |
| 158 | } |
| 159 | |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 160 | void Candidate::Assign(std::string& s, absl::string_view view) { |
| 161 | // Assigning via a temporary object, like s = std::string(view), results in |
| 162 | // binary size bloat. To avoid that, extract pointer and size from the |
| 163 | // string view, and use std::string::assign method. |
| 164 | s.assign(view.data(), view.size()); |
| 165 | } |
| 166 | |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 167 | } // namespace cricket |