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