candidate.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2004 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. #ifndef API_CANDIDATE_H_
  11. #define API_CANDIDATE_H_
  12. #include <limits.h>
  13. #include <stdint.h>
  14. #include <algorithm>
  15. #include <string>
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/network_constants.h"
  18. #include "rtc_base/socket_address.h"
  19. #include "rtc_base/system/rtc_export.h"
  20. namespace cricket {
  21. // Candidate for ICE based connection discovery.
  22. // TODO(phoglund): remove things in here that are not needed in the public API.
  23. class RTC_EXPORT Candidate {
  24. public:
  25. Candidate();
  26. // TODO(pthatcher): Match the ordering and param list as per RFC 5245
  27. // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
  28. 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 = 0,
  38. uint16_t network_cost = 0);
  39. Candidate(const Candidate&);
  40. ~Candidate();
  41. const std::string& id() const { return id_; }
  42. void set_id(const std::string& id) { id_ = id; }
  43. int component() const { return component_; }
  44. void set_component(int component) { component_ = component; }
  45. const std::string& protocol() const { return protocol_; }
  46. void set_protocol(const std::string& protocol) { protocol_ = protocol; }
  47. // The protocol used to talk to relay.
  48. const std::string& relay_protocol() const { return relay_protocol_; }
  49. void set_relay_protocol(const std::string& protocol) {
  50. relay_protocol_ = protocol;
  51. }
  52. const rtc::SocketAddress& address() const { return address_; }
  53. void set_address(const rtc::SocketAddress& address) { address_ = address; }
  54. uint32_t priority() const { return priority_; }
  55. void set_priority(const uint32_t priority) { priority_ = priority; }
  56. // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
  57. // doesn't use it.
  58. // Maps old preference (which was 0.0-1.0) to match priority (which
  59. // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
  60. // https://docs.google.com/a/google.com/document/d/
  61. // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
  62. float preference() const {
  63. // The preference value is clamped to two decimal precision.
  64. return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
  65. }
  66. // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
  67. // doesn't use it.
  68. void set_preference(float preference) {
  69. // Limiting priority to UINT_MAX when value exceeds uint32_t max.
  70. // This can happen for e.g. when preference = 3.
  71. uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
  72. priority_ = static_cast<uint32_t>(
  73. std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
  74. }
  75. // TODO(honghaiz): Change to usernameFragment or ufrag.
  76. const std::string& username() const { return username_; }
  77. void set_username(const std::string& username) { username_ = username; }
  78. const std::string& password() const { return password_; }
  79. void set_password(const std::string& password) { password_ = password; }
  80. const std::string& type() const { return type_; }
  81. void set_type(const std::string& type) { type_ = type; }
  82. const std::string& network_name() const { return network_name_; }
  83. void set_network_name(const std::string& network_name) {
  84. network_name_ = network_name;
  85. }
  86. rtc::AdapterType network_type() const { return network_type_; }
  87. void set_network_type(rtc::AdapterType network_type) {
  88. network_type_ = network_type;
  89. }
  90. // Candidates in a new generation replace those in the old generation.
  91. uint32_t generation() const { return generation_; }
  92. void set_generation(uint32_t generation) { generation_ = generation; }
  93. // |network_cost| measures the cost/penalty of using this candidate. A network
  94. // cost of 0 indicates this candidate can be used freely. A value of
  95. // rtc::kNetworkCostMax indicates it should be used only as the last resort.
  96. void set_network_cost(uint16_t network_cost) {
  97. RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
  98. network_cost_ = network_cost;
  99. }
  100. uint16_t network_cost() const { return network_cost_; }
  101. // An ID assigned to the network hosting the candidate.
  102. uint16_t network_id() const { return network_id_; }
  103. void set_network_id(uint16_t network_id) { network_id_ = network_id; }
  104. const std::string& foundation() const { return foundation_; }
  105. void set_foundation(const std::string& foundation) {
  106. foundation_ = foundation;
  107. }
  108. const rtc::SocketAddress& related_address() const { return related_address_; }
  109. void set_related_address(const rtc::SocketAddress& related_address) {
  110. related_address_ = related_address;
  111. }
  112. const std::string& tcptype() const { return tcptype_; }
  113. void set_tcptype(const std::string& tcptype) { tcptype_ = tcptype; }
  114. // The name of the transport channel of this candidate.
  115. // TODO(phoglund): remove.
  116. const std::string& transport_name() const { return transport_name_; }
  117. void set_transport_name(const std::string& transport_name) {
  118. transport_name_ = transport_name;
  119. }
  120. // The URL of the ICE server which this candidate is gathered from.
  121. const std::string& url() const { return url_; }
  122. void set_url(const std::string& url) { url_ = url; }
  123. // Determines whether this candidate is equivalent to the given one.
  124. bool IsEquivalent(const Candidate& c) const;
  125. // Determines whether this candidate can be considered equivalent to the
  126. // given one when looking for a matching candidate to remove.
  127. bool MatchesForRemoval(const Candidate& c) const;
  128. std::string ToString() const { return ToStringInternal(false); }
  129. std::string ToSensitiveString() const { return ToStringInternal(true); }
  130. uint32_t GetPriority(uint32_t type_preference,
  131. int network_adapter_preference,
  132. int relay_preference) const;
  133. bool operator==(const Candidate& o) const;
  134. bool operator!=(const Candidate& o) const;
  135. // Returns a sanitized copy configured by the given booleans. If
  136. // |use_host_address| is true, the returned copy has its IP removed from
  137. // |address()|, which leads |address()| to be a hostname address. If
  138. // |filter_related_address|, the returned copy has its related address reset
  139. // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that
  140. // setting both booleans to false returns an identical copy to the original
  141. // candidate.
  142. Candidate ToSanitizedCopy(bool use_hostname_address,
  143. bool filter_related_address) const;
  144. private:
  145. std::string ToStringInternal(bool sensitive) const;
  146. std::string id_;
  147. int component_;
  148. std::string protocol_;
  149. std::string relay_protocol_;
  150. rtc::SocketAddress address_;
  151. uint32_t priority_;
  152. std::string username_;
  153. std::string password_;
  154. std::string type_;
  155. std::string network_name_;
  156. rtc::AdapterType network_type_;
  157. uint32_t generation_;
  158. std::string foundation_;
  159. rtc::SocketAddress related_address_;
  160. std::string tcptype_;
  161. std::string transport_name_;
  162. uint16_t network_id_;
  163. uint16_t network_cost_;
  164. std::string url_;
  165. };
  166. } // namespace cricket
  167. #endif // API_CANDIDATE_H_