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