xref: /aosp_15_r20/external/webrtc/api/candidate.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef API_CANDIDATE_H_
12*d9f75844SAndroid Build Coastguard Worker #define API_CANDIDATE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <limits.h>
15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
18*d9f75844SAndroid Build Coastguard Worker #include <string>
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
21*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network_constants.h"
23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket_address.h"
24*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker namespace cricket {
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker // TURN servers are limited to 32 in accordance with
29*d9f75844SAndroid Build Coastguard Worker // https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers
30*d9f75844SAndroid Build Coastguard Worker static constexpr size_t kMaxTurnServers = 32;
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker // Candidate for ICE based connection discovery.
33*d9f75844SAndroid Build Coastguard Worker // TODO(phoglund): remove things in here that are not needed in the public API.
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT Candidate {
36*d9f75844SAndroid Build Coastguard Worker  public:
37*d9f75844SAndroid Build Coastguard Worker   Candidate();
38*d9f75844SAndroid Build Coastguard Worker   // TODO(pthatcher): Match the ordering and param list as per RFC 5245
39*d9f75844SAndroid Build Coastguard Worker   // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
40*d9f75844SAndroid Build Coastguard Worker   Candidate(int component,
41*d9f75844SAndroid Build Coastguard Worker             absl::string_view protocol,
42*d9f75844SAndroid Build Coastguard Worker             const rtc::SocketAddress& address,
43*d9f75844SAndroid Build Coastguard Worker             uint32_t priority,
44*d9f75844SAndroid Build Coastguard Worker             absl::string_view username,
45*d9f75844SAndroid Build Coastguard Worker             absl::string_view password,
46*d9f75844SAndroid Build Coastguard Worker             absl::string_view type,
47*d9f75844SAndroid Build Coastguard Worker             uint32_t generation,
48*d9f75844SAndroid Build Coastguard Worker             absl::string_view foundation,
49*d9f75844SAndroid Build Coastguard Worker             uint16_t network_id = 0,
50*d9f75844SAndroid Build Coastguard Worker             uint16_t network_cost = 0);
51*d9f75844SAndroid Build Coastguard Worker   Candidate(const Candidate&);
52*d9f75844SAndroid Build Coastguard Worker   ~Candidate();
53*d9f75844SAndroid Build Coastguard Worker 
id()54*d9f75844SAndroid Build Coastguard Worker   const std::string& id() const { return id_; }
set_id(absl::string_view id)55*d9f75844SAndroid Build Coastguard Worker   void set_id(absl::string_view id) { Assign(id_, id); }
56*d9f75844SAndroid Build Coastguard Worker 
component()57*d9f75844SAndroid Build Coastguard Worker   int component() const { return component_; }
set_component(int component)58*d9f75844SAndroid Build Coastguard Worker   void set_component(int component) { component_ = component; }
59*d9f75844SAndroid Build Coastguard Worker 
protocol()60*d9f75844SAndroid Build Coastguard Worker   const std::string& protocol() const { return protocol_; }
set_protocol(absl::string_view protocol)61*d9f75844SAndroid Build Coastguard Worker   void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); }
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker   // The protocol used to talk to relay.
relay_protocol()64*d9f75844SAndroid Build Coastguard Worker   const std::string& relay_protocol() const { return relay_protocol_; }
set_relay_protocol(absl::string_view protocol)65*d9f75844SAndroid Build Coastguard Worker   void set_relay_protocol(absl::string_view protocol) {
66*d9f75844SAndroid Build Coastguard Worker     Assign(relay_protocol_, protocol);
67*d9f75844SAndroid Build Coastguard Worker   }
68*d9f75844SAndroid Build Coastguard Worker 
address()69*d9f75844SAndroid Build Coastguard Worker   const rtc::SocketAddress& address() const { return address_; }
set_address(const rtc::SocketAddress & address)70*d9f75844SAndroid Build Coastguard Worker   void set_address(const rtc::SocketAddress& address) { address_ = address; }
71*d9f75844SAndroid Build Coastguard Worker 
priority()72*d9f75844SAndroid Build Coastguard Worker   uint32_t priority() const { return priority_; }
set_priority(const uint32_t priority)73*d9f75844SAndroid Build Coastguard Worker   void set_priority(const uint32_t priority) { priority_ = priority; }
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker   // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
76*d9f75844SAndroid Build Coastguard Worker   // doesn't use it.
77*d9f75844SAndroid Build Coastguard Worker   // Maps old preference (which was 0.0-1.0) to match priority (which
78*d9f75844SAndroid Build Coastguard Worker   // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1.  Also see
79*d9f75844SAndroid Build Coastguard Worker   // https://docs.google.com/a/google.com/document/d/
80*d9f75844SAndroid Build Coastguard Worker   // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
preference()81*d9f75844SAndroid Build Coastguard Worker   float preference() const {
82*d9f75844SAndroid Build Coastguard Worker     // The preference value is clamped to two decimal precision.
83*d9f75844SAndroid Build Coastguard Worker     return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
84*d9f75844SAndroid Build Coastguard Worker   }
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker   // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
87*d9f75844SAndroid Build Coastguard Worker   // doesn't use it.
set_preference(float preference)88*d9f75844SAndroid Build Coastguard Worker   void set_preference(float preference) {
89*d9f75844SAndroid Build Coastguard Worker     // Limiting priority to UINT_MAX when value exceeds uint32_t max.
90*d9f75844SAndroid Build Coastguard Worker     // This can happen for e.g. when preference = 3.
91*d9f75844SAndroid Build Coastguard Worker     uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
92*d9f75844SAndroid Build Coastguard Worker     priority_ = static_cast<uint32_t>(
93*d9f75844SAndroid Build Coastguard Worker         std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
94*d9f75844SAndroid Build Coastguard Worker   }
95*d9f75844SAndroid Build Coastguard Worker 
96*d9f75844SAndroid Build Coastguard Worker   // TODO(honghaiz): Change to usernameFragment or ufrag.
username()97*d9f75844SAndroid Build Coastguard Worker   const std::string& username() const { return username_; }
set_username(absl::string_view username)98*d9f75844SAndroid Build Coastguard Worker   void set_username(absl::string_view username) { Assign(username_, username); }
99*d9f75844SAndroid Build Coastguard Worker 
password()100*d9f75844SAndroid Build Coastguard Worker   const std::string& password() const { return password_; }
set_password(absl::string_view password)101*d9f75844SAndroid Build Coastguard Worker   void set_password(absl::string_view password) { Assign(password_, password); }
102*d9f75844SAndroid Build Coastguard Worker 
type()103*d9f75844SAndroid Build Coastguard Worker   const std::string& type() const { return type_; }
set_type(absl::string_view type)104*d9f75844SAndroid Build Coastguard Worker   void set_type(absl::string_view type) { Assign(type_, type); }
105*d9f75844SAndroid Build Coastguard Worker 
network_name()106*d9f75844SAndroid Build Coastguard Worker   const std::string& network_name() const { return network_name_; }
set_network_name(absl::string_view network_name)107*d9f75844SAndroid Build Coastguard Worker   void set_network_name(absl::string_view network_name) {
108*d9f75844SAndroid Build Coastguard Worker     Assign(network_name_, network_name);
109*d9f75844SAndroid Build Coastguard Worker   }
110*d9f75844SAndroid Build Coastguard Worker 
network_type()111*d9f75844SAndroid Build Coastguard Worker   rtc::AdapterType network_type() const { return network_type_; }
set_network_type(rtc::AdapterType network_type)112*d9f75844SAndroid Build Coastguard Worker   void set_network_type(rtc::AdapterType network_type) {
113*d9f75844SAndroid Build Coastguard Worker     network_type_ = network_type;
114*d9f75844SAndroid Build Coastguard Worker   }
115*d9f75844SAndroid Build Coastguard Worker 
underlying_type_for_vpn()116*d9f75844SAndroid Build Coastguard Worker   rtc::AdapterType underlying_type_for_vpn() const {
117*d9f75844SAndroid Build Coastguard Worker     return underlying_type_for_vpn_;
118*d9f75844SAndroid Build Coastguard Worker   }
set_underlying_type_for_vpn(rtc::AdapterType network_type)119*d9f75844SAndroid Build Coastguard Worker   void set_underlying_type_for_vpn(rtc::AdapterType network_type) {
120*d9f75844SAndroid Build Coastguard Worker     underlying_type_for_vpn_ = network_type;
121*d9f75844SAndroid Build Coastguard Worker   }
122*d9f75844SAndroid Build Coastguard Worker 
123*d9f75844SAndroid Build Coastguard Worker   // Candidates in a new generation replace those in the old generation.
generation()124*d9f75844SAndroid Build Coastguard Worker   uint32_t generation() const { return generation_; }
set_generation(uint32_t generation)125*d9f75844SAndroid Build Coastguard Worker   void set_generation(uint32_t generation) { generation_ = generation; }
126*d9f75844SAndroid Build Coastguard Worker 
127*d9f75844SAndroid Build Coastguard Worker   // `network_cost` measures the cost/penalty of using this candidate. A network
128*d9f75844SAndroid Build Coastguard Worker   // cost of 0 indicates this candidate can be used freely. A value of
129*d9f75844SAndroid Build Coastguard Worker   // rtc::kNetworkCostMax indicates it should be used only as the last resort.
set_network_cost(uint16_t network_cost)130*d9f75844SAndroid Build Coastguard Worker   void set_network_cost(uint16_t network_cost) {
131*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
132*d9f75844SAndroid Build Coastguard Worker     network_cost_ = network_cost;
133*d9f75844SAndroid Build Coastguard Worker   }
network_cost()134*d9f75844SAndroid Build Coastguard Worker   uint16_t network_cost() const { return network_cost_; }
135*d9f75844SAndroid Build Coastguard Worker 
136*d9f75844SAndroid Build Coastguard Worker   // An ID assigned to the network hosting the candidate.
network_id()137*d9f75844SAndroid Build Coastguard Worker   uint16_t network_id() const { return network_id_; }
set_network_id(uint16_t network_id)138*d9f75844SAndroid Build Coastguard Worker   void set_network_id(uint16_t network_id) { network_id_ = network_id; }
139*d9f75844SAndroid Build Coastguard Worker 
foundation()140*d9f75844SAndroid Build Coastguard Worker   const std::string& foundation() const { return foundation_; }
set_foundation(absl::string_view foundation)141*d9f75844SAndroid Build Coastguard Worker   void set_foundation(absl::string_view foundation) {
142*d9f75844SAndroid Build Coastguard Worker     Assign(foundation_, foundation);
143*d9f75844SAndroid Build Coastguard Worker   }
144*d9f75844SAndroid Build Coastguard Worker 
related_address()145*d9f75844SAndroid Build Coastguard Worker   const rtc::SocketAddress& related_address() const { return related_address_; }
set_related_address(const rtc::SocketAddress & related_address)146*d9f75844SAndroid Build Coastguard Worker   void set_related_address(const rtc::SocketAddress& related_address) {
147*d9f75844SAndroid Build Coastguard Worker     related_address_ = related_address;
148*d9f75844SAndroid Build Coastguard Worker   }
tcptype()149*d9f75844SAndroid Build Coastguard Worker   const std::string& tcptype() const { return tcptype_; }
set_tcptype(absl::string_view tcptype)150*d9f75844SAndroid Build Coastguard Worker   void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); }
151*d9f75844SAndroid Build Coastguard Worker 
152*d9f75844SAndroid Build Coastguard Worker   // The name of the transport channel of this candidate.
153*d9f75844SAndroid Build Coastguard Worker   // TODO(phoglund): remove.
transport_name()154*d9f75844SAndroid Build Coastguard Worker   const std::string& transport_name() const { return transport_name_; }
set_transport_name(absl::string_view transport_name)155*d9f75844SAndroid Build Coastguard Worker   void set_transport_name(absl::string_view transport_name) {
156*d9f75844SAndroid Build Coastguard Worker     Assign(transport_name_, transport_name);
157*d9f75844SAndroid Build Coastguard Worker   }
158*d9f75844SAndroid Build Coastguard Worker 
159*d9f75844SAndroid Build Coastguard Worker   // The URL of the ICE server which this candidate is gathered from.
url()160*d9f75844SAndroid Build Coastguard Worker   const std::string& url() const { return url_; }
set_url(absl::string_view url)161*d9f75844SAndroid Build Coastguard Worker   void set_url(absl::string_view url) { Assign(url_, url); }
162*d9f75844SAndroid Build Coastguard Worker 
163*d9f75844SAndroid Build Coastguard Worker   // Determines whether this candidate is equivalent to the given one.
164*d9f75844SAndroid Build Coastguard Worker   bool IsEquivalent(const Candidate& c) const;
165*d9f75844SAndroid Build Coastguard Worker 
166*d9f75844SAndroid Build Coastguard Worker   // Determines whether this candidate can be considered equivalent to the
167*d9f75844SAndroid Build Coastguard Worker   // given one when looking for a matching candidate to remove.
168*d9f75844SAndroid Build Coastguard Worker   bool MatchesForRemoval(const Candidate& c) const;
169*d9f75844SAndroid Build Coastguard Worker 
ToString()170*d9f75844SAndroid Build Coastguard Worker   std::string ToString() const { return ToStringInternal(false); }
171*d9f75844SAndroid Build Coastguard Worker 
ToSensitiveString()172*d9f75844SAndroid Build Coastguard Worker   std::string ToSensitiveString() const { return ToStringInternal(true); }
173*d9f75844SAndroid Build Coastguard Worker 
174*d9f75844SAndroid Build Coastguard Worker   uint32_t GetPriority(uint32_t type_preference,
175*d9f75844SAndroid Build Coastguard Worker                        int network_adapter_preference,
176*d9f75844SAndroid Build Coastguard Worker                        int relay_preference) const;
177*d9f75844SAndroid Build Coastguard Worker 
178*d9f75844SAndroid Build Coastguard Worker   bool operator==(const Candidate& o) const;
179*d9f75844SAndroid Build Coastguard Worker   bool operator!=(const Candidate& o) const;
180*d9f75844SAndroid Build Coastguard Worker 
181*d9f75844SAndroid Build Coastguard Worker   // Returns a sanitized copy configured by the given booleans. If
182*d9f75844SAndroid Build Coastguard Worker   // `use_host_address` is true, the returned copy has its IP removed from
183*d9f75844SAndroid Build Coastguard Worker   // `address()`, which leads `address()` to be a hostname address. If
184*d9f75844SAndroid Build Coastguard Worker   // `filter_related_address`, the returned copy has its related address reset
185*d9f75844SAndroid Build Coastguard Worker   // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that
186*d9f75844SAndroid Build Coastguard Worker   // setting both booleans to false returns an identical copy to the original
187*d9f75844SAndroid Build Coastguard Worker   // candidate.
188*d9f75844SAndroid Build Coastguard Worker   Candidate ToSanitizedCopy(bool use_hostname_address,
189*d9f75844SAndroid Build Coastguard Worker                             bool filter_related_address) const;
190*d9f75844SAndroid Build Coastguard Worker 
191*d9f75844SAndroid Build Coastguard Worker  private:
192*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment
193*d9f75844SAndroid Build Coastguard Worker   // operator accepting any object implicitly convertible to std::string_view,
194*d9f75844SAndroid Build Coastguard Worker   // and then we don't need this workaround.
195*d9f75844SAndroid Build Coastguard Worker   static void Assign(std::string& s, absl::string_view view);
196*d9f75844SAndroid Build Coastguard Worker   std::string ToStringInternal(bool sensitive) const;
197*d9f75844SAndroid Build Coastguard Worker 
198*d9f75844SAndroid Build Coastguard Worker   std::string id_;
199*d9f75844SAndroid Build Coastguard Worker   int component_;
200*d9f75844SAndroid Build Coastguard Worker   std::string protocol_;
201*d9f75844SAndroid Build Coastguard Worker   std::string relay_protocol_;
202*d9f75844SAndroid Build Coastguard Worker   rtc::SocketAddress address_;
203*d9f75844SAndroid Build Coastguard Worker   uint32_t priority_;
204*d9f75844SAndroid Build Coastguard Worker   std::string username_;
205*d9f75844SAndroid Build Coastguard Worker   std::string password_;
206*d9f75844SAndroid Build Coastguard Worker   std::string type_;
207*d9f75844SAndroid Build Coastguard Worker   std::string network_name_;
208*d9f75844SAndroid Build Coastguard Worker   rtc::AdapterType network_type_;
209*d9f75844SAndroid Build Coastguard Worker   rtc::AdapterType underlying_type_for_vpn_;
210*d9f75844SAndroid Build Coastguard Worker   uint32_t generation_;
211*d9f75844SAndroid Build Coastguard Worker   std::string foundation_;
212*d9f75844SAndroid Build Coastguard Worker   rtc::SocketAddress related_address_;
213*d9f75844SAndroid Build Coastguard Worker   std::string tcptype_;
214*d9f75844SAndroid Build Coastguard Worker   std::string transport_name_;
215*d9f75844SAndroid Build Coastguard Worker   uint16_t network_id_;
216*d9f75844SAndroid Build Coastguard Worker   uint16_t network_cost_;
217*d9f75844SAndroid Build Coastguard Worker   std::string url_;
218*d9f75844SAndroid Build Coastguard Worker };
219*d9f75844SAndroid Build Coastguard Worker 
220*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
221*d9f75844SAndroid Build Coastguard Worker 
222*d9f75844SAndroid Build Coastguard Worker #endif  // API_CANDIDATE_H_
223