blob: a14dda350c3c935d0010d8f55e9cc05503035419 [file] [log] [blame]
Steve Anton36b28db2017-10-26 18:27:171/*
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 Gerey2e00abc2018-10-05 13:39:2413#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 17:11:0014#include "rtc_base/ip_address.h"
Harald Alvestrande3ceb882021-06-15 13:02:4115#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 11:41:3016#include "rtc_base/strings/string_builder.h"
17
Steve Anton36b28db2017-10-26 18:27:1718namespace cricket {
19
20Candidate::Candidate()
21 : id_(rtc::CreateRandomString(8)),
22 component_(0),
23 priority_(0),
24 network_type_(rtc::ADAPTER_TYPE_UNKNOWN),
Byoungchan Lee5a925772022-10-18 17:43:1825 underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN),
Steve Anton36b28db2017-10-26 18:27:1726 generation_(0),
27 network_id_(0),
28 network_cost_(0) {}
29
30Candidate::Candidate(int component,
Niels Möllerd4aa3a32021-09-29 11:23:0131 absl::string_view protocol,
Steve Anton36b28db2017-10-26 18:27:1732 const rtc::SocketAddress& address,
33 uint32_t priority,
Niels Möllerd4aa3a32021-09-29 11:23:0134 absl::string_view username,
35 absl::string_view password,
36 absl::string_view type,
Steve Anton36b28db2017-10-26 18:27:1737 uint32_t generation,
Niels Möllerd4aa3a32021-09-29 11:23:0138 absl::string_view foundation,
Steve Anton36b28db2017-10-26 18:27:1739 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 Lee5a925772022-10-18 17:43:1850 underlying_type_for_vpn_(rtc::ADAPTER_TYPE_UNKNOWN),
Steve Anton36b28db2017-10-26 18:27:1751 generation_(generation),
52 foundation_(foundation),
53 network_id_(network_id),
54 network_cost_(network_cost) {}
55
56Candidate::Candidate(const Candidate&) = default;
57
58Candidate::~Candidate() = default;
59
60bool 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
72bool Candidate::MatchesForRemoval(const Candidate& c) const {
73 return component_ == c.component_ && protocol_ == c.protocol_ &&
74 address_ == c.address_;
75}
76
77std::string Candidate::ToStringInternal(bool sensitive) const {
Jonas Olsson366a50c2018-09-06 11:41:3078 rtc::StringBuilder ost;
Steve Anton36b28db2017-10-26 18:27:1779 std::string address =
80 sensitive ? address_.ToSensitiveString() : address_.ToString();
81 ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_
82 << ":" << protocol_ << ":" << priority_ << ":" << address << ":" << type_
Jonas Olssonabbe8412018-04-03 11:40:0583 << ":" << related_address_.ToString() << ":" << username_ << ":"
84 << password_ << ":" << network_id_ << ":" << network_cost_ << ":"
85 << generation_ << "]";
Jonas Olsson84df1c72018-09-14 14:59:3286 return ost.Release();
Steve Anton36b28db2017-10-26 18:27:1787}
88
89uint32_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 Titov0e61fdd2021-07-25 19:50:1497 // `local_preference` length is 2 bytes, 0-65535 inclusive.
Steve Anton36b28db2017-10-26 18:27:1798 // 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 Hanckeee4c9302021-09-28 21:19:47106 // 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 Anton36b28db2017-10-26 18:27:17109
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
117bool 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
129bool Candidate::operator!=(const Candidate& o) const {
130 return !(*this == o);
131}
132
Qingsi Wang1dac6d82018-12-12 23:28:47133Candidate Candidate::ToSanitizedCopy(bool use_hostname_address,
134 bool filter_related_address) const {
135 Candidate copy(*this);
136 if (use_hostname_address) {
Harald Alvestrande3ceb882021-06-15 13:02:41137 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 Wang1dac6d82018-12-12 23:28:47152 }
153 if (filter_related_address) {
154 copy.set_related_address(
155 rtc::EmptySocketAddressWithFamily(copy.address().family()));
156 }
157 return copy;
158}
159
Niels Möllerd4aa3a32021-09-29 11:23:01160void 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 Anton36b28db2017-10-26 18:27:17167} // namespace cricket