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 28 #ifndef TALK_P2P_BASE_CANDIDATE_H_ 29 #define TALK_P2P_BASE_CANDIDATE_H_ 30 31 #include <limits.h> 32 #include <math.h> 33 34 #include <iomanip> 35 #include <sstream> 36 #include <string> 37 38 #include "talk/p2p/base/constants.h" 39 #include "webrtc/base/basictypes.h" 40 #include "webrtc/base/socketaddress.h" 41 42 namespace cricket { 43 44 // Candidate for ICE based connection discovery. 45 46 class Candidate { 47 public: 48 // TODO: Match the ordering and param list as per RFC 5245 49 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1 Candidate()50 Candidate() : component_(0), priority_(0), generation_(0) {} Candidate(const std::string & id,int component,const std::string & protocol,const rtc::SocketAddress & address,uint32 priority,const std::string & username,const std::string & password,const std::string & type,const std::string & network_name,uint32 generation,const std::string & foundation)51 Candidate(const std::string& id, int component, const std::string& protocol, 52 const rtc::SocketAddress& address, uint32 priority, 53 const std::string& username, const std::string& password, 54 const std::string& type, const std::string& network_name, 55 uint32 generation, const std::string& foundation) 56 : id_(id), component_(component), protocol_(protocol), address_(address), 57 priority_(priority), username_(username), password_(password), 58 type_(type), network_name_(network_name), generation_(generation), 59 foundation_(foundation) { 60 } 61 id()62 const std::string & id() const { return id_; } set_id(const std::string & id)63 void set_id(const std::string & id) { id_ = id; } 64 component()65 int component() const { return component_; } set_component(int component)66 void set_component(int component) { component_ = component; } 67 protocol()68 const std::string & protocol() const { return protocol_; } set_protocol(const std::string & protocol)69 void set_protocol(const std::string & protocol) { protocol_ = protocol; } 70 address()71 const rtc::SocketAddress & address() const { return address_; } set_address(const rtc::SocketAddress & address)72 void set_address(const rtc::SocketAddress & address) { 73 address_ = address; 74 } 75 priority()76 uint32 priority() const { return priority_; } set_priority(const uint32 priority)77 void set_priority(const uint32 priority) { priority_ = priority; } 78 79 // void set_type_preference(uint32 type_preference) { 80 // priority_ = GetPriority(type_preference); 81 // } 82 83 // Maps old preference (which was 0.0-1.0) to match priority (which 84 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see 85 // https://docs.google.com/a/google.com/document/d/ 86 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit preference()87 float preference() const { 88 // The preference value is clamped to two decimal precision. 89 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0); 90 } 91 set_preference(float preference)92 void set_preference(float preference) { 93 // Limiting priority to UINT_MAX when value exceeds uint32 max. 94 // This can happen for e.g. when preference = 3. 95 uint64 prio_val = static_cast<uint64>(preference * 127) << 24; 96 priority_ = static_cast<uint32>( 97 rtc::_min(prio_val, static_cast<uint64>(UINT_MAX))); 98 } 99 username()100 const std::string & username() const { return username_; } set_username(const std::string & username)101 void set_username(const std::string & username) { username_ = username; } 102 password()103 const std::string & password() const { return password_; } set_password(const std::string & password)104 void set_password(const std::string & password) { password_ = password; } 105 type()106 const std::string & type() const { return type_; } set_type(const std::string & type)107 void set_type(const std::string & type) { type_ = type; } 108 network_name()109 const std::string & network_name() const { return network_name_; } set_network_name(const std::string & network_name)110 void set_network_name(const std::string & network_name) { 111 network_name_ = network_name; 112 } 113 114 // Candidates in a new generation replace those in the old generation. generation()115 uint32 generation() const { return generation_; } set_generation(uint32 generation)116 void set_generation(uint32 generation) { generation_ = generation; } generation_str()117 const std::string generation_str() const { 118 std::ostringstream ost; 119 ost << generation_; 120 return ost.str(); 121 } set_generation_str(const std::string & str)122 void set_generation_str(const std::string& str) { 123 std::istringstream ist(str); 124 ist >> generation_; 125 } 126 foundation()127 const std::string& foundation() const { 128 return foundation_; 129 } 130 set_foundation(const std::string & foundation)131 void set_foundation(const std::string& foundation) { 132 foundation_ = foundation; 133 } 134 related_address()135 const rtc::SocketAddress & related_address() const { 136 return related_address_; 137 } set_related_address(const rtc::SocketAddress & related_address)138 void set_related_address( 139 const rtc::SocketAddress & related_address) { 140 related_address_ = related_address; 141 } tcptype()142 const std::string& tcptype() const { return tcptype_; } set_tcptype(const std::string & tcptype)143 void set_tcptype(const std::string& tcptype){ 144 tcptype_ = tcptype; 145 } 146 147 // Determines whether this candidate is equivalent to the given one. IsEquivalent(const Candidate & c)148 bool IsEquivalent(const Candidate& c) const { 149 // We ignore the network name, since that is just debug information, and 150 // the priority, since that should be the same if the rest is (and it's 151 // a float so equality checking is always worrisome). 152 return (id_ == c.id_) && 153 (component_ == c.component_) && 154 (protocol_ == c.protocol_) && 155 (address_ == c.address_) && 156 (username_ == c.username_) && 157 (password_ == c.password_) && 158 (type_ == c.type_) && 159 (generation_ == c.generation_) && 160 (foundation_ == c.foundation_) && 161 (related_address_ == c.related_address_); 162 } 163 ToString()164 std::string ToString() const { 165 return ToStringInternal(false); 166 } 167 ToSensitiveString()168 std::string ToSensitiveString() const { 169 return ToStringInternal(true); 170 } 171 GetPriority(uint32 type_preference,int network_adapter_preference,int relay_preference)172 uint32 GetPriority(uint32 type_preference, 173 int network_adapter_preference, 174 int relay_preference) const { 175 // RFC 5245 - 4.1.2.1. 176 // priority = (2^24)*(type preference) + 177 // (2^8)*(local preference) + 178 // (2^0)*(256 - component ID) 179 180 // |local_preference| length is 2 bytes, 0-65535 inclusive. 181 // In our implemenation we will partion local_preference into 182 // 0 1 183 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 184 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 185 // | NIC Pref | Addr Pref | 186 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 187 // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired. 188 // Addr Pref - Address preference value as per RFC 3484. 189 // local preference = (NIC Type << 8 | Addr_Pref) - relay preference. 190 191 int addr_pref = IPAddressPrecedence(address_.ipaddr()); 192 int local_preference = ((network_adapter_preference << 8) | addr_pref) + 193 relay_preference; 194 195 return (type_preference << 24) | 196 (local_preference << 8) | 197 (256 - component_); 198 } 199 200 private: ToStringInternal(bool sensitive)201 std::string ToStringInternal(bool sensitive) const { 202 std::ostringstream ost; 203 std::string address = sensitive ? address_.ToSensitiveString() : 204 address_.ToString(); 205 ost << "Cand[" << foundation_ << ":" << component_ << ":" 206 << protocol_ << ":" << priority_ << ":" 207 << address << ":" << type_ << ":" << related_address_ << ":" 208 << username_ << ":" << password_ << "]"; 209 return ost.str(); 210 } 211 212 std::string id_; 213 int component_; 214 std::string protocol_; 215 rtc::SocketAddress address_; 216 uint32 priority_; 217 std::string username_; 218 std::string password_; 219 std::string type_; 220 std::string network_name_; 221 uint32 generation_; 222 std::string foundation_; 223 rtc::SocketAddress related_address_; 224 std::string tcptype_; 225 }; 226 227 } // namespace cricket 228 229 #endif // TALK_P2P_BASE_CANDIDATE_H_ 230