3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_P2P_BASE_CANDIDATE_H_
29 #define TALK_P2P_BASE_CANDIDATE_H_
37 #include "talk/base/basictypes.h"
38 #include "talk/base/socketaddress.h"
39 #include "talk/p2p/base/constants.h"
43 // Candidate for ICE based connection discovery.
47 // TODO: Match the ordering and param list as per RFC 5245
48 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
49 Candidate() : component_(0), priority_(0), generation_(0) {}
50 Candidate(const std::string& id, int component, const std::string& protocol,
51 const talk_base::SocketAddress& address, uint32 priority,
52 const std::string& username, const std::string& password,
53 const std::string& type, const std::string& network_name,
54 uint32 generation, const std::string& foundation)
55 : id_(id), component_(component), protocol_(protocol), address_(address),
56 priority_(priority), username_(username), password_(password),
57 type_(type), network_name_(network_name), generation_(generation),
58 foundation_(foundation) {
61 const std::string & id() const { return id_; }
62 void set_id(const std::string & id) { id_ = id; }
64 int component() const { return component_; }
65 void set_component(int component) { component_ = component; }
67 const std::string & protocol() const { return protocol_; }
68 void set_protocol(const std::string & protocol) { protocol_ = protocol; }
70 const talk_base::SocketAddress & address() const { return address_; }
71 void set_address(const talk_base::SocketAddress & address) {
75 uint32 priority() const { return priority_; }
76 void set_priority(const uint32 priority) { priority_ = priority; }
78 // void set_type_preference(uint32 type_preference) {
79 // priority_ = GetPriority(type_preference);
82 // Maps old preference (which was 0.0-1.0) to match priority (which
83 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
84 // https://docs.google.com/a/google.com/document/d/
85 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
86 float preference() const {
87 // The preference value is clamped to two decimal precision.
88 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
91 void set_preference(float preference) {
92 // Limiting priority to UINT_MAX when value exceeds uint32 max.
93 // This can happen for e.g. when preference = 3.
94 uint64 prio_val = static_cast<uint64>(preference * 127) << 24;
95 priority_ = static_cast<uint32>(
96 talk_base::_min(prio_val, static_cast<uint64>(UINT_MAX)));
99 const std::string & username() const { return username_; }
100 void set_username(const std::string & username) { username_ = username; }
102 const std::string & password() const { return password_; }
103 void set_password(const std::string & password) { password_ = password; }
105 const std::string & type() const { return type_; }
106 void set_type(const std::string & type) { type_ = type; }
108 const std::string & network_name() const { return network_name_; }
109 void set_network_name(const std::string & network_name) {
110 network_name_ = network_name;
113 // Candidates in a new generation replace those in the old generation.
114 uint32 generation() const { return generation_; }
115 void set_generation(uint32 generation) { generation_ = generation; }
116 const std::string generation_str() const {
117 std::ostringstream ost;
121 void set_generation_str(const std::string& str) {
122 std::istringstream ist(str);
126 const std::string& foundation() const {
130 void set_foundation(const std::string& foundation) {
131 foundation_ = foundation;
134 const talk_base::SocketAddress & related_address() const {
135 return related_address_;
137 void set_related_address(
138 const talk_base::SocketAddress & related_address) {
139 related_address_ = related_address;
142 // Determines whether this candidate is equivalent to the given one.
143 bool IsEquivalent(const Candidate& c) const {
144 // We ignore the network name, since that is just debug information, and
145 // the priority, since that should be the same if the rest is (and it's
146 // a float so equality checking is always worrisome).
147 return (id_ == c.id_) &&
148 (component_ == c.component_) &&
149 (protocol_ == c.protocol_) &&
150 (address_ == c.address_) &&
151 (username_ == c.username_) &&
152 (password_ == c.password_) &&
153 (type_ == c.type_) &&
154 (generation_ == c.generation_) &&
155 (foundation_ == c.foundation_) &&
156 (related_address_ == c.related_address_);
159 std::string ToString() const {
160 return ToStringInternal(false);
163 std::string ToSensitiveString() const {
164 return ToStringInternal(true);
167 uint32 GetPriority(uint32 type_preference,
168 int network_adapter_preference) const {
169 // RFC 5245 - 4.1.2.1.
170 // priority = (2^24)*(type preference) +
171 // (2^8)*(local preference) +
172 // (2^0)*(256 - component ID)
174 // |local_preference| length is 2 bytes, 0-65535 inclusive.
175 // In our implemenation we will partion local_preference into
177 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
178 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179 // | NIC Pref | Addr Pref |
180 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181 // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired.
182 // Addr Pref - Address preference value as per RFC 3484.
183 // local preference is calculated as - NIC Type << 8 | Addr_Pref.
185 int addr_pref = IPAddressPrecedence(address_.ipaddr());
186 int local_preference = (network_adapter_preference << 8) | addr_pref;
188 return (type_preference << 24) |
189 (local_preference << 8) |
194 std::string ToStringInternal(bool sensitive) const {
195 std::ostringstream ost;
196 std::string address = sensitive ? address_.ToSensitiveString() :
198 ost << "Cand[" << foundation_ << ":" << component_ << ":"
199 << protocol_ << ":" << priority_ << ":"
200 << address << ":" << type_ << ":" << related_address_ << ":"
201 << username_ << ":" << password_ << "]";
207 std::string protocol_;
208 talk_base::SocketAddress address_;
210 std::string username_;
211 std::string password_;
213 std::string network_name_;
215 std::string foundation_;
216 talk_base::SocketAddress related_address_;
219 } // namespace cricket
221 #endif // TALK_P2P_BASE_CANDIDATE_H_