Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 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 | |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 13 | #include "rtc_base/helpers.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include "rtc_base/ipaddress.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 15 | #include "rtc_base/strings/string_builder.h" |
| 16 | |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 17 | namespace cricket { |
| 18 | |
| 19 | Candidate::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 | |
| 28 | Candidate::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 | |
| 53 | Candidate::Candidate(const Candidate&) = default; |
| 54 | |
| 55 | Candidate::~Candidate() = default; |
| 56 | |
| 57 | bool 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 | |
| 69 | bool Candidate::MatchesForRemoval(const Candidate& c) const { |
| 70 | return component_ == c.component_ && protocol_ == c.protocol_ && |
| 71 | address_ == c.address_; |
| 72 | } |
| 73 | |
| 74 | std::string Candidate::ToStringInternal(bool sensitive) const { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 75 | rtc::StringBuilder ost; |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 76 | std::string address = |
| 77 | sensitive ? address_.ToSensitiveString() : address_.ToString(); |
| 78 | ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_ |
| 79 | << ":" << protocol_ << ":" << priority_ << ":" << address << ":" << type_ |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 80 | << ":" << related_address_.ToString() << ":" << username_ << ":" |
| 81 | << password_ << ":" << network_id_ << ":" << network_cost_ << ":" |
| 82 | << generation_ << "]"; |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 83 | return ost.Release(); |
Steve Anton | 36b28db | 2017-10-26 18:27:17 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | uint32_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 | |
| 112 | bool 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 | |
| 124 | bool Candidate::operator!=(const Candidate& o) const { |
| 125 | return !(*this == o); |
| 126 | } |
| 127 | |
| 128 | } // namespace cricket |