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.

193 lines
7.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_P2P_BASE_CANDIDATE_H_
  28. #define TALK_P2P_BASE_CANDIDATE_H_
  29. #include <climits>
  30. #include <cmath>
  31. #include <string>
  32. #include <sstream>
  33. #include <iomanip>
  34. #include "talk/base/basictypes.h"
  35. #include "talk/base/socketaddress.h"
  36. #include "talk/p2p/base/constants.h"
  37. namespace cricket {
  38. // Candidate for ICE based connection discovery.
  39. class Candidate {
  40. public:
  41. // TODO: Match the ordering and param list as per RFC 5245
  42. // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
  43. Candidate() : component_(0), priority_(0), generation_(0) {}
  44. Candidate(const std::string& id, int component, const std::string& protocol,
  45. const talk_base::SocketAddress& address, uint32 priority,
  46. const std::string& username, const std::string& password,
  47. const std::string& type, const std::string& network_name,
  48. uint32 generation, const std::string& foundation)
  49. : id_(id), component_(component), protocol_(protocol), address_(address),
  50. priority_(priority), username_(username), password_(password),
  51. type_(type), network_name_(network_name), generation_(generation),
  52. foundation_(foundation) {
  53. }
  54. const std::string & id() const { return id_; }
  55. void set_id(const std::string & id) { id_ = id; }
  56. int component() const { return component_; }
  57. void set_component(int component) { component_ = component; }
  58. const std::string & protocol() const { return protocol_; }
  59. void set_protocol(const std::string & protocol) { protocol_ = protocol; }
  60. const talk_base::SocketAddress & address() const { return address_; }
  61. void set_address(const talk_base::SocketAddress & address) {
  62. address_ = address;
  63. }
  64. uint32 priority() const { return priority_; }
  65. void set_priority(const uint32 priority) { priority_ = priority; }
  66. // void set_type_preference(uint32 type_preference) {
  67. // priority_ = GetPriority(type_preference);
  68. // }
  69. // Maps old preference (which was 0.0-1.0) to match priority (which
  70. // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
  71. // https://docs.google.com/a/google.com/document/d/
  72. // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
  73. float preference() const {
  74. // The preference value is clamped to two decimal precision.
  75. return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
  76. }
  77. void set_preference(float preference) {
  78. // Limiting priority to UINT_MAX when value exceeds uint32 max.
  79. // This can happen for e.g. when preference = 3.
  80. uint64 prio_val = static_cast<uint64>(preference * 127) << 24;
  81. priority_ = static_cast<uint32>(
  82. talk_base::_min(prio_val, static_cast<uint64>(UINT_MAX)));
  83. }
  84. const std::string & username() const { return username_; }
  85. void set_username(const std::string & username) { username_ = username; }
  86. const std::string & password() const { return password_; }
  87. void set_password(const std::string & password) { password_ = password; }
  88. const std::string & type() const { return type_; }
  89. void set_type(const std::string & type) { type_ = type; }
  90. const std::string & network_name() const { return network_name_; }
  91. void set_network_name(const std::string & network_name) {
  92. network_name_ = network_name;
  93. }
  94. // Candidates in a new generation replace those in the old generation.
  95. uint32 generation() const { return generation_; }
  96. void set_generation(uint32 generation) { generation_ = generation; }
  97. const std::string generation_str() const {
  98. std::ostringstream ost;
  99. ost << generation_;
  100. return ost.str();
  101. }
  102. void set_generation_str(const std::string& str) {
  103. std::istringstream ist(str);
  104. ist >> generation_;
  105. }
  106. const std::string& foundation() const {
  107. return foundation_;
  108. }
  109. void set_foundation(const std::string& foundation) {
  110. foundation_ = foundation;
  111. }
  112. const talk_base::SocketAddress & related_address() const {
  113. return related_address_;
  114. }
  115. void set_related_address(
  116. const talk_base::SocketAddress & related_address) {
  117. related_address_ = related_address;
  118. }
  119. // Determines whether this candidate is equivalent to the given one.
  120. bool IsEquivalent(const Candidate& c) const {
  121. // We ignore the network name, since that is just debug information, and
  122. // the priority, since that should be the same if the rest is (and it's
  123. // a float so equality checking is always worrisome).
  124. return (id_ == c.id_) &&
  125. (component_ == c.component_) &&
  126. (protocol_ == c.protocol_) &&
  127. (address_ == c.address_) &&
  128. (username_ == c.username_) &&
  129. (password_ == c.password_) &&
  130. (type_ == c.type_) &&
  131. (generation_ == c.generation_) &&
  132. (foundation_ == c.foundation_) &&
  133. (related_address_ == c.related_address_);
  134. }
  135. std::string ToString() const {
  136. std::ostringstream ost;
  137. ost << "Cand[" << id_ << ":" << component_ << ":"
  138. << type_ << ":" << protocol_ << ":"
  139. << network_name_ << ":" << address_.ToString() << ":"
  140. << username_ << ":" << password_ << "]";
  141. return ost.str();
  142. }
  143. uint32 GetPriority(uint32 type_preference) const {
  144. // RFC 5245 - 4.1.2.1.
  145. // priority = (2^24)*(type preference) +
  146. // (2^8)*(local preference) +
  147. // (2^0)*(256 - component ID)
  148. int addr_pref = IPAddressPrecedence(address_.ipaddr());
  149. return (type_preference << 24) | (addr_pref << 8) | (256 - component_);
  150. }
  151. private:
  152. std::string id_;
  153. int component_;
  154. std::string protocol_;
  155. talk_base::SocketAddress address_;
  156. uint32 priority_;
  157. std::string username_;
  158. std::string password_;
  159. std::string type_;
  160. std::string network_name_;
  161. uint32 generation_;
  162. std::string foundation_;
  163. talk_base::SocketAddress related_address_;
  164. };
  165. } // namespace cricket
  166. #endif // TALK_P2P_BASE_CANDIDATE_H_