You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.4 KiB

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