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