blob: c857f89c3ccaa77fb658af7e35810656fa7ced01 [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"
Jonas Olsson366a50c2018-09-06 13:41:30 +020015#include "rtc_base/strings/string_builder.h"
16
Steve Anton36b28db2017-10-26 11:27:17 -070017namespace cricket {
18
19Candidate::Candidate()
20 : id_(rtc::CreateRandomString(8)),
21 component_(0),
22 priority_(0),
23 network_type_(rtc::ADAPTER_TYPE_UNKNOWN),
24 generation_(0),
25 network_id_(0),
26 network_cost_(0) {}
27
28Candidate::Candidate(int component,
29 const std::string& protocol,
30 const rtc::SocketAddress& address,
31 uint32_t priority,
32 const std::string& username,
33 const std::string& password,
34 const std::string& type,
35 uint32_t generation,
36 const std::string& foundation,
37 uint16_t network_id,
38 uint16_t network_cost)
39 : id_(rtc::CreateRandomString(8)),
40 component_(component),
41 protocol_(protocol),
42 address_(address),
43 priority_(priority),
44 username_(username),
45 password_(password),
46 type_(type),
47 network_type_(rtc::ADAPTER_TYPE_UNKNOWN),
48 generation_(generation),
49 foundation_(foundation),
50 network_id_(network_id),
51 network_cost_(network_cost) {}
52
53Candidate::Candidate(const Candidate&) = default;
54
55Candidate::~Candidate() = default;
56
57bool Candidate::IsEquivalent(const Candidate& c) const {
58 // We ignore the network name, since that is just debug information, and
59 // the priority and the network cost, since they should be the same if the
60 // rest are.
61 return (component_ == c.component_) && (protocol_ == c.protocol_) &&
62 (address_ == c.address_) && (username_ == c.username_) &&
63 (password_ == c.password_) && (type_ == c.type_) &&
64 (generation_ == c.generation_) && (foundation_ == c.foundation_) &&
65 (related_address_ == c.related_address_) &&
66 (network_id_ == c.network_id_);
67}
68
69bool Candidate::MatchesForRemoval(const Candidate& c) const {
70 return component_ == c.component_ && protocol_ == c.protocol_ &&
71 address_ == c.address_;
72}
73
74std::string Candidate::ToStringInternal(bool sensitive) const {
Jonas Olsson366a50c2018-09-06 13:41:30 +020075 rtc::StringBuilder ost;
Steve Anton36b28db2017-10-26 11:27:17 -070076 std::string address =
77 sensitive ? address_.ToSensitiveString() : address_.ToString();
78 ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_
79 << ":" << protocol_ << ":" << priority_ << ":" << address << ":" << type_
Jonas Olssonabbe8412018-04-03 13:40:05 +020080 << ":" << related_address_.ToString() << ":" << username_ << ":"
81 << password_ << ":" << network_id_ << ":" << network_cost_ << ":"
82 << generation_ << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +020083 return ost.Release();
Steve Anton36b28db2017-10-26 11:27:17 -070084}
85
86uint32_t Candidate::GetPriority(uint32_t type_preference,
87 int network_adapter_preference,
88 int relay_preference) const {
89 // RFC 5245 - 4.1.2.1.
90 // priority = (2^24)*(type preference) +
91 // (2^8)*(local preference) +
92 // (2^0)*(256 - component ID)
93
94 // |local_preference| length is 2 bytes, 0-65535 inclusive.
95 // In our implemenation we will partion local_preference into
96 // 0 1
97 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
98 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 // | NIC Pref | Addr Pref |
100 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired.
102 // Addr Pref - Address preference value as per RFC 3484.
103 // local preference = (NIC Type << 8 | Addr_Pref) - relay preference.
104
105 int addr_pref = IPAddressPrecedence(address_.ipaddr());
106 int local_preference =
107 ((network_adapter_preference << 8) | addr_pref) + relay_preference;
108
109 return (type_preference << 24) | (local_preference << 8) | (256 - component_);
110}
111
112bool Candidate::operator==(const Candidate& o) const {
113 return id_ == o.id_ && component_ == o.component_ &&
114 protocol_ == o.protocol_ && relay_protocol_ == o.relay_protocol_ &&
115 address_ == o.address_ && priority_ == o.priority_ &&
116 username_ == o.username_ && password_ == o.password_ &&
117 type_ == o.type_ && network_name_ == o.network_name_ &&
118 network_type_ == o.network_type_ && generation_ == o.generation_ &&
119 foundation_ == o.foundation_ &&
120 related_address_ == o.related_address_ && tcptype_ == o.tcptype_ &&
121 transport_name_ == o.transport_name_ && network_id_ == o.network_id_;
122}
123
124bool Candidate::operator!=(const Candidate& o) const {
125 return !(*this == o);
126}
127
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800128Candidate Candidate::ToSanitizedCopy(bool use_hostname_address,
129 bool filter_related_address) const {
130 Candidate copy(*this);
131 if (use_hostname_address) {
132 rtc::SocketAddress hostname_only_addr(address().hostname(),
133 address().port());
134 copy.set_address(hostname_only_addr);
135 }
136 if (filter_related_address) {
137 copy.set_related_address(
138 rtc::EmptySocketAddressWithFamily(copy.address().family()));
139 }
140 return copy;
141}
142
Steve Anton36b28db2017-10-26 11:27:17 -0700143} // namespace cricket