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