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 WEBRTC_P2P_BASE_CANDIDATE_H_
29 #define WEBRTC_P2P_BASE_CANDIDATE_H_
38 #include "webrtc/p2p/base/constants.h"
39 #include "webrtc/base/basictypes.h"
40 #include "webrtc/base/socketaddress.h"
44 // Candidate for ICE based connection discovery.
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
50 Candidate() : component_(0), priority_(0), generation_(0) {}
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) {
62 const std::string & id() const { return id_; }
63 void set_id(const std::string & id) { id_ = id; }
65 int component() const { return component_; }
66 void set_component(int component) { component_ = component; }
68 const std::string & protocol() const { return protocol_; }
69 void set_protocol(const std::string & protocol) { protocol_ = protocol; }
71 const rtc::SocketAddress & address() const { return address_; }
72 void set_address(const rtc::SocketAddress & address) {
76 uint32 priority() const { return priority_; }
77 void set_priority(const uint32 priority) { priority_ = priority; }
79 // void set_type_preference(uint32 type_preference) {
80 // priority_ = GetPriority(type_preference);
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
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);
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)));
100 const std::string & username() const { return username_; }
101 void set_username(const std::string & username) { username_ = username; }
103 const std::string & password() const { return password_; }
104 void set_password(const std::string & password) { password_ = password; }
106 const std::string & type() const { return type_; }
107 void set_type(const std::string & type) { type_ = type; }
109 const std::string & network_name() const { return network_name_; }
110 void set_network_name(const std::string & network_name) {
111 network_name_ = network_name;
114 // Candidates in a new generation replace those in the old generation.
115 uint32 generation() const { return generation_; }
116 void set_generation(uint32 generation) { generation_ = generation; }
117 const std::string generation_str() const {
118 std::ostringstream ost;
122 void set_generation_str(const std::string& str) {
123 std::istringstream ist(str);
127 const std::string& foundation() const {
131 void set_foundation(const std::string& foundation) {
132 foundation_ = foundation;
135 const rtc::SocketAddress & related_address() const {
136 return related_address_;
138 void set_related_address(
139 const rtc::SocketAddress & related_address) {
140 related_address_ = related_address;
142 const std::string& tcptype() const { return tcptype_; }
143 void set_tcptype(const std::string& tcptype){
147 // Determines whether this candidate is equivalent to the given one.
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_);
164 std::string ToString() const {
165 return ToStringInternal(false);
168 std::string ToSensitiveString() const {
169 return ToStringInternal(true);
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)
180 // |local_preference| length is 2 bytes, 0-65535 inclusive.
181 // In our implemenation we will partion local_preference into
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.
191 int addr_pref = IPAddressPrecedence(address_.ipaddr());
192 int local_preference = ((network_adapter_preference << 8) | addr_pref) +
195 return (type_preference << 24) |
196 (local_preference << 8) |
201 std::string ToStringInternal(bool sensitive) const {
202 std::ostringstream ost;
203 std::string address = sensitive ? address_.ToSensitiveString() :
205 ost << "Cand[" << foundation_ << ":" << component_ << ":"
206 << protocol_ << ":" << priority_ << ":"
207 << address << ":" << type_ << ":" << related_address_ << ":"
208 << username_ << ":" << password_ << "]";
214 std::string protocol_;
215 rtc::SocketAddress address_;
217 std::string username_;
218 std::string password_;
220 std::string network_name_;
222 std::string foundation_;
223 rtc::SocketAddress related_address_;
224 std::string tcptype_;
227 } // namespace cricket
229 #endif // WEBRTC_P2P_BASE_CANDIDATE_H_