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
13 #include "rtc_base/helpers.h"
14 #include "rtc_base/ip_address.h"
15 #include "rtc_base/logging.h"
16 #include "rtc_base/strings/string_builder.h"
17
18 namespace cricket {
19
Candidate()20 Candidate::Candidate()
21 : id_(rtc::CreateRandomString(8)),
22 component_(0),
23 priority_(0),
24 network_type_(rtc::ADAPTER_TYPE_UNKNOWN),
25 underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN),
26 generation_(0),
27 network_id_(0),
28 network_cost_(0) {}
29
Candidate(int component,absl::string_view protocol,const rtc::SocketAddress & address,uint32_t priority,absl::string_view username,absl::string_view password,absl::string_view type,uint32_t generation,absl::string_view foundation,uint16_t network_id,uint16_t network_cost)30 Candidate::Candidate(int component,
31 absl::string_view protocol,
32 const rtc::SocketAddress& address,
33 uint32_t priority,
34 absl::string_view username,
35 absl::string_view password,
36 absl::string_view type,
37 uint32_t generation,
38 absl::string_view foundation,
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),
50 underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN),
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
IsEquivalent(const Candidate & c) const60 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
MatchesForRemoval(const Candidate & c) const72 bool Candidate::MatchesForRemoval(const Candidate& c) const {
73 return component_ == c.component_ && protocol_ == c.protocol_ &&
74 address_ == c.address_;
75 }
76
ToStringInternal(bool sensitive) const77 std::string Candidate::ToStringInternal(bool sensitive) const {
78 rtc::StringBuilder ost;
79 std::string address =
80 sensitive ? address_.ToSensitiveString() : address_.ToString();
81 ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_
82 << ":" << protocol_ << ":" << priority_ << ":" << address << ":" << type_
83 << ":" << related_address_.ToString() << ":" << username_ << ":"
84 << password_ << ":" << network_id_ << ":" << network_cost_ << ":"
85 << generation_ << "]";
86 return ost.Release();
87 }
88
GetPriority(uint32_t type_preference,int network_adapter_preference,int relay_preference) const89 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
97 // `local_preference` length is 2 bytes, 0-65535 inclusive.
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.
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.
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
operator ==(const Candidate & o) const117 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
operator !=(const Candidate & o) const129 bool Candidate::operator!=(const Candidate& o) const {
130 return !(*this == o);
131 }
132
ToSanitizedCopy(bool use_hostname_address,bool filter_related_address) const133 Candidate Candidate::ToSanitizedCopy(bool use_hostname_address,
134 bool filter_related_address) const {
135 Candidate copy(*this);
136 if (use_hostname_address) {
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 }
152 }
153 if (filter_related_address) {
154 copy.set_related_address(
155 rtc::EmptySocketAddressWithFamily(copy.address().family()));
156 }
157 return copy;
158 }
159
Assign(std::string & s,absl::string_view view)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
167 } // namespace cricket
168