blob: a1f45c215e16d548ce1f361c10ab96f98b5d4a30 [file] [log] [blame]
Patrik Höglunde2d6a062017-10-05 14:53:33 +02001/*
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:
Steve Anton36b28db2017-10-26 11:27:17 -070032 Candidate();
Patrik Höglunde2d6a062017-10-05 14:53:33 +020033 // TODO(pthatcher): Match the ordering and param list as per RFC 5245
34 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
Patrik Höglunde2d6a062017-10-05 14:53:33 +020035 Candidate(int component,
36 const std::string& protocol,
37 const rtc::SocketAddress& address,
38 uint32_t priority,
39 const std::string& username,
40 const std::string& password,
41 const std::string& type,
42 uint32_t generation,
43 const std::string& foundation,
44 uint16_t network_id = 0,
Steve Anton36b28db2017-10-26 11:27:17 -070045 uint16_t network_cost = 0);
46 Candidate(const Candidate&);
47 ~Candidate();
Patrik Höglunde2d6a062017-10-05 14:53:33 +020048
49 const std::string & id() const { return id_; }
50 void set_id(const std::string & id) { id_ = id; }
51
52 int component() const { return component_; }
53 void set_component(int component) { component_ = component; }
54
55 const std::string & protocol() const { return protocol_; }
56 void set_protocol(const std::string & protocol) { protocol_ = protocol; }
57
58 // The protocol used to talk to relay.
59 const std::string& relay_protocol() const { return relay_protocol_; }
60 void set_relay_protocol(const std::string& protocol) {
61 relay_protocol_ = protocol;
62 }
63
64 const rtc::SocketAddress & address() const { return address_; }
65 void set_address(const rtc::SocketAddress & address) {
66 address_ = address;
67 }
68
69 uint32_t priority() const { return priority_; }
70 void set_priority(const uint32_t priority) { priority_ = priority; }
71
72 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
73 // doesn't use it.
74 // Maps old preference (which was 0.0-1.0) to match priority (which
75 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
76 // https://docs.google.com/a/google.com/document/d/
77 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
78 float preference() const {
79 // The preference value is clamped to two decimal precision.
80 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
81 }
82
83 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
84 // doesn't use it.
85 void set_preference(float preference) {
86 // Limiting priority to UINT_MAX when value exceeds uint32_t max.
87 // This can happen for e.g. when preference = 3.
88 uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
89 priority_ = static_cast<uint32_t>(
90 std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
91 }
92
93 // TODO(honghaiz): Change to usernameFragment or ufrag.
94 const std::string & username() const { return username_; }
95 void set_username(const std::string & username) { username_ = username; }
96
97 const std::string & password() const { return password_; }
98 void set_password(const std::string & password) { password_ = password; }
99
100 const std::string & type() const { return type_; }
101 void set_type(const std::string & type) { type_ = type; }
102
103 const std::string & network_name() const { return network_name_; }
104 void set_network_name(const std::string & network_name) {
105 network_name_ = network_name;
106 }
107
108 rtc::AdapterType network_type() const { return network_type_; }
109 void set_network_type(rtc::AdapterType network_type) {
110 network_type_ = network_type;
111 }
112
113 // Candidates in a new generation replace those in the old generation.
114 uint32_t generation() const { return generation_; }
115 void set_generation(uint32_t generation) { generation_ = generation; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200116
117 // |network_cost| measures the cost/penalty of using this candidate. A network
118 // cost of 0 indicates this candidate can be used freely. A value of
119 // rtc::kNetworkCostMax indicates it should be used only as the last resort.
120 void set_network_cost(uint16_t network_cost) {
121 RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
122 network_cost_ = network_cost;
123 }
124 uint16_t network_cost() const { return network_cost_; }
125
126 // An ID assigned to the network hosting the candidate.
127 uint16_t network_id() const { return network_id_; }
128 void set_network_id(uint16_t network_id) { network_id_ = network_id; }
129
130 const std::string& foundation() const {
131 return foundation_;
132 }
133 void set_foundation(const std::string& foundation) {
134 foundation_ = foundation;
135 }
136
137 const rtc::SocketAddress & related_address() const {
138 return related_address_;
139 }
140 void set_related_address(
141 const rtc::SocketAddress & related_address) {
142 related_address_ = related_address;
143 }
144 const std::string& tcptype() const { return tcptype_; }
145 void set_tcptype(const std::string& tcptype) {
146 tcptype_ = tcptype;
147 }
148
149 // The name of the transport channel of this candidate.
150 // TODO(phoglund): remove.
151 const std::string& transport_name() const { return transport_name_; }
152 void set_transport_name(const std::string& transport_name) {
153 transport_name_ = transport_name;
154 }
155
156 // The URL of the ICE server which this candidate is gathered from.
157 const std::string& url() const { return url_; }
158 void set_url(const std::string& url) { url_ = url; }
159
160 // Determines whether this candidate is equivalent to the given one.
Steve Anton36b28db2017-10-26 11:27:17 -0700161 bool IsEquivalent(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200162
163 // Determines whether this candidate can be considered equivalent to the
164 // given one when looking for a matching candidate to remove.
Steve Anton36b28db2017-10-26 11:27:17 -0700165 bool MatchesForRemoval(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200166
167 std::string ToString() const {
168 return ToStringInternal(false);
169 }
170
171 std::string ToSensitiveString() const {
172 return ToStringInternal(true);
173 }
174
175 uint32_t GetPriority(uint32_t type_preference,
176 int network_adapter_preference,
Steve Anton36b28db2017-10-26 11:27:17 -0700177 int relay_preference) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200178
Steve Anton36b28db2017-10-26 11:27:17 -0700179 bool operator==(const Candidate& o) const;
180 bool operator!=(const Candidate& o) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200181
182 private:
Steve Anton36b28db2017-10-26 11:27:17 -0700183 std::string ToStringInternal(bool sensitive) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200184
185 std::string id_;
186 int component_;
187 std::string protocol_;
188 std::string relay_protocol_;
189 rtc::SocketAddress address_;
190 uint32_t priority_;
191 std::string username_;
192 std::string password_;
193 std::string type_;
194 std::string network_name_;
195 rtc::AdapterType network_type_;
196 uint32_t generation_;
197 std::string foundation_;
198 rtc::SocketAddress related_address_;
199 std::string tcptype_;
200 std::string transport_name_;
201 uint16_t network_id_;
202 uint16_t network_cost_;
203 std::string url_;
204};
205
206} // namespace cricket
207
208#endif // API_CANDIDATE_H_