blob: 78b20827e505e9437f5efaf802d5ad37a99e847d [file] [log] [blame]
Patrik Höglunde2d6a062017-10-05 12:53:331/*
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 API_CANDIDATE_H_
12#define API_CANDIDATE_H_
13
14#include <limits.h>
15#include <stdint.h>
16
17#include <algorithm>
18#include <string>
19
20#include "rtc_base/checks.h"
21#include "rtc_base/helpers.h"
22#include "rtc_base/network_constants.h"
23#include "rtc_base/socketaddress.h"
24
25namespace cricket {
26
27// Candidate for ICE based connection discovery.
28// TODO(phoglund): remove things in here that are not needed in the public API.
29
30class Candidate {
31 public:
32 // TODO(pthatcher): Match the ordering and param list as per RFC 5245
33 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
34 Candidate()
35 : id_(rtc::CreateRandomString(8)),
36 component_(0),
37 priority_(0),
38 network_type_(rtc::ADAPTER_TYPE_UNKNOWN),
39 generation_(0),
40 network_id_(0),
41 network_cost_(0) {}
42
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 uint16_t network_id = 0,
53 uint16_t network_cost = 0)
54 : id_(rtc::CreateRandomString(8)),
55 component_(component),
56 protocol_(protocol),
57 address_(address),
58 priority_(priority),
59 username_(username),
60 password_(password),
61 type_(type),
62 network_type_(rtc::ADAPTER_TYPE_UNKNOWN),
63 generation_(generation),
64 foundation_(foundation),
65 network_id_(network_id),
66 network_cost_(network_cost) {}
67
68 const std::string & id() const { return id_; }
69 void set_id(const std::string & id) { id_ = id; }
70
71 int component() const { return component_; }
72 void set_component(int component) { component_ = component; }
73
74 const std::string & protocol() const { return protocol_; }
75 void set_protocol(const std::string & protocol) { protocol_ = protocol; }
76
77 // The protocol used to talk to relay.
78 const std::string& relay_protocol() const { return relay_protocol_; }
79 void set_relay_protocol(const std::string& protocol) {
80 relay_protocol_ = protocol;
81 }
82
83 const rtc::SocketAddress & address() const { return address_; }
84 void set_address(const rtc::SocketAddress & address) {
85 address_ = address;
86 }
87
88 uint32_t priority() const { return priority_; }
89 void set_priority(const uint32_t priority) { priority_ = priority; }
90
91 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
92 // doesn't use it.
93 // Maps old preference (which was 0.0-1.0) to match priority (which
94 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
95 // https://docs.google.com/a/google.com/document/d/
96 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
97 float preference() const {
98 // The preference value is clamped to two decimal precision.
99 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
100 }
101
102 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
103 // doesn't use it.
104 void set_preference(float preference) {
105 // Limiting priority to UINT_MAX when value exceeds uint32_t max.
106 // This can happen for e.g. when preference = 3.
107 uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
108 priority_ = static_cast<uint32_t>(
109 std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
110 }
111
112 // TODO(honghaiz): Change to usernameFragment or ufrag.
113 const std::string & username() const { return username_; }
114 void set_username(const std::string & username) { username_ = username; }
115
116 const std::string & password() const { return password_; }
117 void set_password(const std::string & password) { password_ = password; }
118
119 const std::string & type() const { return type_; }
120 void set_type(const std::string & type) { type_ = type; }
121
122 const std::string & network_name() const { return network_name_; }
123 void set_network_name(const std::string & network_name) {
124 network_name_ = network_name;
125 }
126
127 rtc::AdapterType network_type() const { return network_type_; }
128 void set_network_type(rtc::AdapterType network_type) {
129 network_type_ = network_type;
130 }
131
132 // Candidates in a new generation replace those in the old generation.
133 uint32_t generation() const { return generation_; }
134 void set_generation(uint32_t generation) { generation_ = generation; }
135 const std::string generation_str() const {
136 std::ostringstream ost;
137 ost << generation_;
138 return ost.str();
139 }
140 void set_generation_str(const std::string& str) {
141 std::istringstream ist(str);
142 ist >> generation_;
143 }
144
145 // |network_cost| measures the cost/penalty of using this candidate. A network
146 // cost of 0 indicates this candidate can be used freely. A value of
147 // rtc::kNetworkCostMax indicates it should be used only as the last resort.
148 void set_network_cost(uint16_t network_cost) {
149 RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
150 network_cost_ = network_cost;
151 }
152 uint16_t network_cost() const { return network_cost_; }
153
154 // An ID assigned to the network hosting the candidate.
155 uint16_t network_id() const { return network_id_; }
156 void set_network_id(uint16_t network_id) { network_id_ = network_id; }
157
158 const std::string& foundation() const {
159 return foundation_;
160 }
161 void set_foundation(const std::string& foundation) {
162 foundation_ = foundation;
163 }
164
165 const rtc::SocketAddress & related_address() const {
166 return related_address_;
167 }
168 void set_related_address(
169 const rtc::SocketAddress & related_address) {
170 related_address_ = related_address;
171 }
172 const std::string& tcptype() const { return tcptype_; }
173 void set_tcptype(const std::string& tcptype) {
174 tcptype_ = tcptype;
175 }
176
177 // The name of the transport channel of this candidate.
178 // TODO(phoglund): remove.
179 const std::string& transport_name() const { return transport_name_; }
180 void set_transport_name(const std::string& transport_name) {
181 transport_name_ = transport_name;
182 }
183
184 // The URL of the ICE server which this candidate is gathered from.
185 const std::string& url() const { return url_; }
186 void set_url(const std::string& url) { url_ = url; }
187
188 // Determines whether this candidate is equivalent to the given one.
189 bool IsEquivalent(const Candidate& c) const {
190 // We ignore the network name, since that is just debug information, and
191 // the priority and the network cost, since they should be the same if the
192 // rest are.
193 return (component_ == c.component_) && (protocol_ == c.protocol_) &&
194 (address_ == c.address_) && (username_ == c.username_) &&
195 (password_ == c.password_) && (type_ == c.type_) &&
196 (generation_ == c.generation_) && (foundation_ == c.foundation_) &&
197 (related_address_ == c.related_address_) &&
198 (network_id_ == c.network_id_);
199 }
200
201 // Determines whether this candidate can be considered equivalent to the
202 // given one when looking for a matching candidate to remove.
203 bool MatchesForRemoval(const Candidate& c) const {
204 return component_ == c.component_ && protocol_ == c.protocol_ &&
205 address_ == c.address_;
206 }
207
208 std::string ToString() const {
209 return ToStringInternal(false);
210 }
211
212 std::string ToSensitiveString() const {
213 return ToStringInternal(true);
214 }
215
216 uint32_t GetPriority(uint32_t type_preference,
217 int network_adapter_preference,
218 int relay_preference) const {
219 // RFC 5245 - 4.1.2.1.
220 // priority = (2^24)*(type preference) +
221 // (2^8)*(local preference) +
222 // (2^0)*(256 - component ID)
223
224 // |local_preference| length is 2 bytes, 0-65535 inclusive.
225 // In our implemenation we will partion local_preference into
226 // 0 1
227 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
228 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 // | NIC Pref | Addr Pref |
230 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231 // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired.
232 // Addr Pref - Address preference value as per RFC 3484.
233 // local preference = (NIC Type << 8 | Addr_Pref) - relay preference.
234
235 int addr_pref = IPAddressPrecedence(address_.ipaddr());
236 int local_preference = ((network_adapter_preference << 8) | addr_pref) +
237 relay_preference;
238
239 return (type_preference << 24) |
240 (local_preference << 8) |
241 (256 - component_);
242 }
243
244 bool operator==(const Candidate& o) const {
245 return id_ == o.id_ && component_ == o.component_ &&
246 protocol_ == o.protocol_ && relay_protocol_ == o.relay_protocol_ &&
247 address_ == o.address_ && priority_ == o.priority_ &&
248 username_ == o.username_ && password_ == o.password_ &&
249 type_ == o.type_ && network_name_ == o.network_name_ &&
250 network_type_ == o.network_type_ && generation_ == o.generation_ &&
251 foundation_ == o.foundation_ &&
252 related_address_ == o.related_address_ && tcptype_ == o.tcptype_ &&
253 transport_name_ == o.transport_name_ && network_id_ == o.network_id_;
254 }
255 bool operator!=(const Candidate& o) const { return !(*this == o); }
256
257 private:
258 std::string ToStringInternal(bool sensitive) const {
259 std::ostringstream ost;
260 std::string address = sensitive ? address_.ToSensitiveString() :
261 address_.ToString();
262 ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_
263 << ":" << protocol_ << ":" << priority_ << ":" << address << ":"
264 << type_ << ":" << related_address_ << ":" << username_ << ":"
265 << password_ << ":" << network_id_ << ":" << network_cost_ << ":"
266 << generation_ << "]";
267 return ost.str();
268 }
269
270 std::string id_;
271 int component_;
272 std::string protocol_;
273 std::string relay_protocol_;
274 rtc::SocketAddress address_;
275 uint32_t priority_;
276 std::string username_;
277 std::string password_;
278 std::string type_;
279 std::string network_name_;
280 rtc::AdapterType network_type_;
281 uint32_t generation_;
282 std::string foundation_;
283 rtc::SocketAddress related_address_;
284 std::string tcptype_;
285 std::string transport_name_;
286 uint16_t network_id_;
287 uint16_t network_cost_;
288 std::string url_;
289};
290
291} // namespace cricket
292
293#endif // API_CANDIDATE_H_