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