blob: 180b1c915b261f65977499dd8ebed5174d788356 [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
Dor Henaefed552024-06-18 13:20:3514#include <stddef.h>
Patrik Höglunde2d6a062017-10-05 12:53:3315#include <stdint.h>
16
Patrik Höglunde2d6a062017-10-05 12:53:3317#include <string>
18
Niels Möllerd4aa3a32021-09-29 11:23:0119#include "absl/strings/string_view.h"
Patrik Höglunde2d6a062017-10-05 12:53:3320#include "rtc_base/checks.h"
Patrik Höglunde2d6a062017-10-05 12:53:3321#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 17:11:0022#include "rtc_base/socket_address.h"
Mirko Bonadei3b56ee72018-10-15 15:15:1223#include "rtc_base/system/rtc_export.h"
Patrik Höglunde2d6a062017-10-05 12:53:3324
Tommi32f2a302024-02-08 18:16:4925namespace webrtc {
26enum class IceCandidateType : int { kHost, kSrflx, kPrflx, kRelay };
Tommic7a4b2a2024-02-25 22:34:3927RTC_EXPORT absl::string_view IceCandidateTypeToString(IceCandidateType);
Tommi32f2a302024-02-08 18:16:4928} // namespace webrtc
29
Patrik Höglunde2d6a062017-10-05 12:53:3330namespace cricket {
31
Tommid1e577d2024-03-21 14:02:3132// TODO(tommi): Remove. No usage in WebRTC now, remove once downstream projects
33// don't have reliance.
34[[deprecated("Use IceCandidateType")]] static constexpr char LOCAL_PORT_TYPE[] =
35 "local";
36[[deprecated("Use IceCandidateType")]] static constexpr char STUN_PORT_TYPE[] =
37 "stun";
38[[deprecated("Use IceCandidateType")]] static constexpr char PRFLX_PORT_TYPE[] =
39 "prflx";
40[[deprecated("Use IceCandidateType")]] static constexpr char RELAY_PORT_TYPE[] =
41 "relay";
Tommi3b2b2af2024-01-17 13:19:4142
Philipp Hancke41a83572022-11-28 13:47:4543// TURN servers are limited to 32 in accordance with
44// https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers
45static constexpr size_t kMaxTurnServers = 32;
46
Patrik Höglunde2d6a062017-10-05 12:53:3347// Candidate for ICE based connection discovery.
Mirko Bonadei3b56ee72018-10-15 15:15:1248class RTC_EXPORT Candidate {
Patrik Höglunde2d6a062017-10-05 12:53:3349 public:
Steve Anton36b28db2017-10-26 18:27:1750 Candidate();
Tommic7a4b2a2024-02-25 22:34:3951 Candidate(int component,
52 absl::string_view protocol,
53 const rtc::SocketAddress& address,
54 uint32_t priority,
55 absl::string_view username,
56 absl::string_view password,
57 webrtc::IceCandidateType type,
58 uint32_t generation,
59 absl::string_view foundation,
60 uint16_t network_id = 0,
61 uint16_t network_cost = 0);
Steve Anton36b28db2017-10-26 18:27:1762 Candidate(const Candidate&);
63 ~Candidate();
Patrik Höglunde2d6a062017-10-05 12:53:3364
Tommibde80e32024-02-17 14:52:5765 // 8 character long randomized ID string for logging purposes.
Yves Gerey665174f2018-06-19 13:03:0566 const std::string& id() const { return id_; }
Tommibde80e32024-02-17 14:52:5767 // Generates a new, 8 character long, id.
68 void generate_id();
Patrik Höglunde2d6a062017-10-05 12:53:3369
70 int component() const { return component_; }
71 void set_component(int component) { component_ = component; }
72
Yves Gerey665174f2018-06-19 13:03:0573 const std::string& protocol() const { return protocol_; }
Tommif2431a92024-02-20 14:27:4474
75 // Valid protocol values are:
76 // UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME, SSLTCP_PROTOCOL_NAME,
77 // TLS_PROTOCOL_NAME.
Niels Möllerd4aa3a32021-09-29 11:23:0178 void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); }
Patrik Höglunde2d6a062017-10-05 12:53:3379
80 // The protocol used to talk to relay.
81 const std::string& relay_protocol() const { return relay_protocol_; }
Tommif2431a92024-02-20 14:27:4482
83 // Valid protocol values are:
84 // UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME, SSLTCP_PROTOCOL_NAME,
85 // TLS_PROTOCOL_NAME.
Niels Möllerd4aa3a32021-09-29 11:23:0186 void set_relay_protocol(absl::string_view protocol) {
87 Assign(relay_protocol_, protocol);
Patrik Höglunde2d6a062017-10-05 12:53:3388 }
89
Yves Gerey665174f2018-06-19 13:03:0590 const rtc::SocketAddress& address() const { return address_; }
91 void set_address(const rtc::SocketAddress& address) { address_ = address; }
Patrik Höglunde2d6a062017-10-05 12:53:3392
93 uint32_t priority() const { return priority_; }
94 void set_priority(const uint32_t priority) { priority_ = priority; }
95
Patrik Höglunde2d6a062017-10-05 12:53:3396 // TODO(honghaiz): Change to usernameFragment or ufrag.
Yves Gerey665174f2018-06-19 13:03:0597 const std::string& username() const { return username_; }
Niels Möllerd4aa3a32021-09-29 11:23:0198 void set_username(absl::string_view username) { Assign(username_, username); }
Patrik Höglunde2d6a062017-10-05 12:53:3399
Yves Gerey665174f2018-06-19 13:03:05100 const std::string& password() const { return password_; }
Niels Möllerd4aa3a32021-09-29 11:23:01101 void set_password(absl::string_view password) { Assign(password_, password); }
Patrik Höglunde2d6a062017-10-05 12:53:33102
Tommic7a4b2a2024-02-25 22:34:39103 webrtc::IceCandidateType type() const { return type_; }
Tommi6bf2d312023-09-25 12:05:31104
Tommif7b22c62024-02-15 08:18:34105 // Returns the name of the candidate type as specified in
106 // https://datatracker.ietf.org/doc/html/rfc5245#section-15.1
107 absl::string_view type_name() const;
108
Tommi6bf2d312023-09-25 12:05:31109 // Setting the type requires a constant string (e.g.
110 // cricket::LOCAL_PORT_TYPE). The type should really be an enum rather than a
111 // string, but until we make that change the lifetime attribute helps us lock
112 // things down. See also the `Port` class.
Tommic7a4b2a2024-02-25 22:34:39113 void set_type(webrtc::IceCandidateType type) { type_ = type; }
Patrik Höglunde2d6a062017-10-05 12:53:33114
Tommic7a4b2a2024-02-25 22:34:39115 // Simple checkers for checking the candidate type without dependency on the
116 // IceCandidateType enum. The `is_local()` and `is_stun()` names are legacy
117 // names and should now more accurately be `is_host()` and `is_srflx()`.
Tommi3b2b2af2024-01-17 13:19:41118 bool is_local() const;
119 bool is_stun() const;
120 bool is_prflx() const;
121 bool is_relay() const;
122
Tommibe2786c2024-01-19 16:33:00123 // Returns the type preference, a value between 0-126 inclusive, with 0 being
124 // the lowest preference value, as described in RFC 5245.
125 // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1
126 int type_preference() const {
127 // From https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.4 :
128 // It is RECOMMENDED that default candidates be chosen based on the
129 // likelihood of those candidates to work with the peer that is being
130 // contacted.
131 // I.e. it is recommended that relayed > reflexive > host.
132 if (is_local())
133 return 1; // Host.
134 if (is_stun())
135 return 2; // Reflexive.
136 if (is_relay())
137 return 3; // Relayed.
138 return 0; // Unknown, lowest preference.
139 }
140
Yves Gerey665174f2018-06-19 13:03:05141 const std::string& network_name() const { return network_name_; }
Niels Möllerd4aa3a32021-09-29 11:23:01142 void set_network_name(absl::string_view network_name) {
143 Assign(network_name_, network_name);
Patrik Höglunde2d6a062017-10-05 12:53:33144 }
145
146 rtc::AdapterType network_type() const { return network_type_; }
147 void set_network_type(rtc::AdapterType network_type) {
148 network_type_ = network_type;
149 }
150
Jonas Oreland0d13bbd2022-03-02 10:17:36151 rtc::AdapterType underlying_type_for_vpn() const {
152 return underlying_type_for_vpn_;
153 }
154 void set_underlying_type_for_vpn(rtc::AdapterType network_type) {
155 underlying_type_for_vpn_ = network_type;
156 }
157
Patrik Höglunde2d6a062017-10-05 12:53:33158 // Candidates in a new generation replace those in the old generation.
159 uint32_t generation() const { return generation_; }
160 void set_generation(uint32_t generation) { generation_ = generation; }
Patrik Höglunde2d6a062017-10-05 12:53:33161
Artem Titov0e61fdd2021-07-25 19:50:14162 // `network_cost` measures the cost/penalty of using this candidate. A network
Patrik Höglunde2d6a062017-10-05 12:53:33163 // cost of 0 indicates this candidate can be used freely. A value of
164 // rtc::kNetworkCostMax indicates it should be used only as the last resort.
165 void set_network_cost(uint16_t network_cost) {
166 RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
167 network_cost_ = network_cost;
168 }
169 uint16_t network_cost() const { return network_cost_; }
170
171 // An ID assigned to the network hosting the candidate.
172 uint16_t network_id() const { return network_id_; }
173 void set_network_id(uint16_t network_id) { network_id_ = network_id; }
174
Tommif2431a92024-02-20 14:27:44175 // From RFC 5245, section-7.2.1.3:
176 // The foundation of the candidate is set to an arbitrary value, different
177 // from the foundation for all other remote candidates.
178 // Note: Use ComputeFoundation to populate this value.
Yves Gerey665174f2018-06-19 13:03:05179 const std::string& foundation() const { return foundation_; }
Tommif2431a92024-02-20 14:27:44180
181 // TODO(tommi): Deprecate in favor of ComputeFoundation.
182 // For situations where serializing/deserializing a candidate is needed,
183 // the constructor can be used to inject a value for the foundation.
Niels Möllerd4aa3a32021-09-29 11:23:01184 void set_foundation(absl::string_view foundation) {
185 Assign(foundation_, foundation);
Patrik Höglunde2d6a062017-10-05 12:53:33186 }
187
Yves Gerey665174f2018-06-19 13:03:05188 const rtc::SocketAddress& related_address() const { return related_address_; }
189 void set_related_address(const rtc::SocketAddress& related_address) {
Patrik Höglunde2d6a062017-10-05 12:53:33190 related_address_ = related_address;
191 }
192 const std::string& tcptype() const { return tcptype_; }
Niels Möllerd4aa3a32021-09-29 11:23:01193 void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); }
Patrik Höglunde2d6a062017-10-05 12:53:33194
195 // The name of the transport channel of this candidate.
196 // TODO(phoglund): remove.
197 const std::string& transport_name() const { return transport_name_; }
Niels Möllerd4aa3a32021-09-29 11:23:01198 void set_transport_name(absl::string_view transport_name) {
199 Assign(transport_name_, transport_name);
Patrik Höglunde2d6a062017-10-05 12:53:33200 }
201
202 // The URL of the ICE server which this candidate is gathered from.
203 const std::string& url() const { return url_; }
Niels Möllerd4aa3a32021-09-29 11:23:01204 void set_url(absl::string_view url) { Assign(url_, url); }
Patrik Höglunde2d6a062017-10-05 12:53:33205
206 // Determines whether this candidate is equivalent to the given one.
Steve Anton36b28db2017-10-26 18:27:17207 bool IsEquivalent(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 12:53:33208
209 // Determines whether this candidate can be considered equivalent to the
210 // given one when looking for a matching candidate to remove.
Steve Anton36b28db2017-10-26 18:27:17211 bool MatchesForRemoval(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 12:53:33212
Yves Gerey665174f2018-06-19 13:03:05213 std::string ToString() const { return ToStringInternal(false); }
Patrik Höglunde2d6a062017-10-05 12:53:33214
Yves Gerey665174f2018-06-19 13:03:05215 std::string ToSensitiveString() const { return ToStringInternal(true); }
Patrik Höglunde2d6a062017-10-05 12:53:33216
217 uint32_t GetPriority(uint32_t type_preference,
218 int network_adapter_preference,
Philipp Hancke17ec0562023-06-16 11:58:45219 int relay_preference,
220 bool adjust_local_preference) const;
Patrik Höglunde2d6a062017-10-05 12:53:33221
Steve Anton36b28db2017-10-26 18:27:17222 bool operator==(const Candidate& o) const;
223 bool operator!=(const Candidate& o) const;
Patrik Höglunde2d6a062017-10-05 12:53:33224
Qingsi Wang1dac6d82018-12-12 23:28:47225 // Returns a sanitized copy configured by the given booleans. If
Artem Titov0e61fdd2021-07-25 19:50:14226 // `use_host_address` is true, the returned copy has its IP removed from
227 // `address()`, which leads `address()` to be a hostname address. If
228 // `filter_related_address`, the returned copy has its related address reset
Qingsi Wang1dac6d82018-12-12 23:28:47229 // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that
230 // setting both booleans to false returns an identical copy to the original
231 // candidate.
232 Candidate ToSanitizedCopy(bool use_hostname_address,
233 bool filter_related_address) const;
234
Tommif2431a92024-02-20 14:27:44235 // Computes and populates the `foundation()` field.
236 // Foundation: An arbitrary string that is the same for two candidates
237 // that have the same type, base IP address, protocol (UDP, TCP,
238 // etc.), and STUN or TURN server. If any of these are different,
239 // then the foundation will be different. Two candidate pairs with
240 // the same foundation pairs are likely to have similar network
241 // characteristics. Foundations are used in the frozen algorithm.
Christoffer Dewerin7098d112024-02-26 14:38:44242 // A session wide (peerconnection) tie-breaker is applied to the foundation,
Tommif2431a92024-02-20 14:27:44243 // adds additional randomness and must be the same for all candidates.
Christoffer Dewerin7098d112024-02-26 14:38:44244 void ComputeFoundation(const rtc::SocketAddress& base_address,
245 uint64_t tie_breaker);
Tommif2431a92024-02-20 14:27:44246
247 // https://www.rfc-editor.org/rfc/rfc5245#section-7.2.1.3
248 // Call to populate the foundation field for a new peer reflexive remote
249 // candidate. The type of the candidate must be "prflx".
250 // The foundation of the candidate is set to an arbitrary value, different
251 // from the foundation for all other remote candidates.
252 void ComputePrflxFoundation();
253
Patrik Höglunde2d6a062017-10-05 12:53:33254 private:
Niels Möllerd4aa3a32021-09-29 11:23:01255 // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment
256 // operator accepting any object implicitly convertible to std::string_view,
257 // and then we don't need this workaround.
258 static void Assign(std::string& s, absl::string_view view);
Steve Anton36b28db2017-10-26 18:27:17259 std::string ToStringInternal(bool sensitive) const;
Patrik Höglunde2d6a062017-10-05 12:53:33260
261 std::string id_;
262 int component_;
263 std::string protocol_;
264 std::string relay_protocol_;
265 rtc::SocketAddress address_;
266 uint32_t priority_;
267 std::string username_;
268 std::string password_;
Tommic7a4b2a2024-02-25 22:34:39269 webrtc::IceCandidateType type_ = webrtc::IceCandidateType::kHost;
Patrik Höglunde2d6a062017-10-05 12:53:33270 std::string network_name_;
271 rtc::AdapterType network_type_;
Jonas Oreland0d13bbd2022-03-02 10:17:36272 rtc::AdapterType underlying_type_for_vpn_;
Patrik Höglunde2d6a062017-10-05 12:53:33273 uint32_t generation_;
274 std::string foundation_;
275 rtc::SocketAddress related_address_;
276 std::string tcptype_;
277 std::string transport_name_;
278 uint16_t network_id_;
279 uint16_t network_cost_;
280 std::string url_;
281};
282
283} // namespace cricket
284
285#endif // API_CANDIDATE_H_