blob: b8aaebc14a4725444209f00e875b58b5700c95bd [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
Niels Möllerd4aa3a32021-09-29 11:23:0120#include "absl/strings/string_view.h"
Patrik Höglunde2d6a062017-10-05 12:53:3321#include "rtc_base/checks.h"
Patrik Höglunde2d6a062017-10-05 12:53:3322#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/socket_address.h"
Mirko Bonadei3b56ee72018-10-15 15:15:1224#include "rtc_base/system/rtc_export.h"
Patrik Höglunde2d6a062017-10-05 12:53:3325
26namespace cricket {
27
28// Candidate for ICE based connection discovery.
29// TODO(phoglund): remove things in here that are not needed in the public API.
30
Mirko Bonadei3b56ee72018-10-15 15:15:1231class RTC_EXPORT Candidate {
Patrik Höglunde2d6a062017-10-05 12:53:3332 public:
Steve Anton36b28db2017-10-26 18:27:1733 Candidate();
Patrik Höglunde2d6a062017-10-05 12:53:3334 // TODO(pthatcher): Match the ordering and param list as per RFC 5245
35 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
Patrik Höglunde2d6a062017-10-05 12:53:3336 Candidate(int component,
Niels Möllerd4aa3a32021-09-29 11:23:0137 absl::string_view protocol,
Patrik Höglunde2d6a062017-10-05 12:53:3338 const rtc::SocketAddress& address,
39 uint32_t priority,
Niels Möllerd4aa3a32021-09-29 11:23:0140 absl::string_view username,
41 absl::string_view password,
42 absl::string_view type,
Patrik Höglunde2d6a062017-10-05 12:53:3343 uint32_t generation,
Niels Möllerd4aa3a32021-09-29 11:23:0144 absl::string_view foundation,
Patrik Höglunde2d6a062017-10-05 12:53:3345 uint16_t network_id = 0,
Steve Anton36b28db2017-10-26 18:27:1746 uint16_t network_cost = 0);
47 Candidate(const Candidate&);
48 ~Candidate();
Patrik Höglunde2d6a062017-10-05 12:53:3349
Yves Gerey665174f2018-06-19 13:03:0550 const std::string& id() const { return id_; }
Niels Möllerd4aa3a32021-09-29 11:23:0151 void set_id(absl::string_view id) { Assign(id_, id); }
Patrik Höglunde2d6a062017-10-05 12:53:3352
53 int component() const { return component_; }
54 void set_component(int component) { component_ = component; }
55
Yves Gerey665174f2018-06-19 13:03:0556 const std::string& protocol() const { return protocol_; }
Niels Möllerd4aa3a32021-09-29 11:23:0157 void set_protocol(absl::string_view protocol) { Assign(protocol_, protocol); }
Patrik Höglunde2d6a062017-10-05 12:53:3358
59 // The protocol used to talk to relay.
60 const std::string& relay_protocol() const { return relay_protocol_; }
Niels Möllerd4aa3a32021-09-29 11:23:0161 void set_relay_protocol(absl::string_view protocol) {
62 Assign(relay_protocol_, protocol);
Patrik Höglunde2d6a062017-10-05 12:53:3363 }
64
Yves Gerey665174f2018-06-19 13:03:0565 const rtc::SocketAddress& address() const { return address_; }
66 void set_address(const rtc::SocketAddress& address) { address_ = address; }
Patrik Höglunde2d6a062017-10-05 12:53:3367
68 uint32_t priority() const { return priority_; }
69 void set_priority(const uint32_t priority) { priority_ = priority; }
70
71 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
72 // doesn't use it.
73 // Maps old preference (which was 0.0-1.0) to match priority (which
74 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
75 // https://docs.google.com/a/google.com/document/d/
76 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
77 float preference() const {
78 // The preference value is clamped to two decimal precision.
79 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
80 }
81
82 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
83 // doesn't use it.
84 void set_preference(float preference) {
85 // Limiting priority to UINT_MAX when value exceeds uint32_t max.
86 // This can happen for e.g. when preference = 3.
87 uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
88 priority_ = static_cast<uint32_t>(
89 std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
90 }
91
92 // TODO(honghaiz): Change to usernameFragment or ufrag.
Yves Gerey665174f2018-06-19 13:03:0593 const std::string& username() const { return username_; }
Niels Möllerd4aa3a32021-09-29 11:23:0194 void set_username(absl::string_view username) { Assign(username_, username); }
Patrik Höglunde2d6a062017-10-05 12:53:3395
Yves Gerey665174f2018-06-19 13:03:0596 const std::string& password() const { return password_; }
Niels Möllerd4aa3a32021-09-29 11:23:0197 void set_password(absl::string_view password) { Assign(password_, password); }
Patrik Höglunde2d6a062017-10-05 12:53:3398
Yves Gerey665174f2018-06-19 13:03:0599 const std::string& type() const { return type_; }
Niels Möllerd4aa3a32021-09-29 11:23:01100 void set_type(absl::string_view type) { Assign(type_, type); }
Patrik Höglunde2d6a062017-10-05 12:53:33101
Yves Gerey665174f2018-06-19 13:03:05102 const std::string& network_name() const { return network_name_; }
Niels Möllerd4aa3a32021-09-29 11:23:01103 void set_network_name(absl::string_view network_name) {
104 Assign(network_name_, network_name);
Patrik Höglunde2d6a062017-10-05 12:53:33105 }
106
107 rtc::AdapterType network_type() const { return network_type_; }
108 void set_network_type(rtc::AdapterType network_type) {
109 network_type_ = network_type;
110 }
111
Jonas Oreland0d13bbd2022-03-02 10:17:36112 rtc::AdapterType underlying_type_for_vpn() const {
113 return underlying_type_for_vpn_;
114 }
115 void set_underlying_type_for_vpn(rtc::AdapterType network_type) {
116 underlying_type_for_vpn_ = network_type;
117 }
118
Patrik Höglunde2d6a062017-10-05 12:53:33119 // Candidates in a new generation replace those in the old generation.
120 uint32_t generation() const { return generation_; }
121 void set_generation(uint32_t generation) { generation_ = generation; }
Patrik Höglunde2d6a062017-10-05 12:53:33122
Artem Titov0e61fdd2021-07-25 19:50:14123 // `network_cost` measures the cost/penalty of using this candidate. A network
Patrik Höglunde2d6a062017-10-05 12:53:33124 // cost of 0 indicates this candidate can be used freely. A value of
125 // rtc::kNetworkCostMax indicates it should be used only as the last resort.
126 void set_network_cost(uint16_t network_cost) {
127 RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
128 network_cost_ = network_cost;
129 }
130 uint16_t network_cost() const { return network_cost_; }
131
132 // An ID assigned to the network hosting the candidate.
133 uint16_t network_id() const { return network_id_; }
134 void set_network_id(uint16_t network_id) { network_id_ = network_id; }
135
Yves Gerey665174f2018-06-19 13:03:05136 const std::string& foundation() const { return foundation_; }
Niels Möllerd4aa3a32021-09-29 11:23:01137 void set_foundation(absl::string_view foundation) {
138 Assign(foundation_, foundation);
Patrik Höglunde2d6a062017-10-05 12:53:33139 }
140
Yves Gerey665174f2018-06-19 13:03:05141 const rtc::SocketAddress& related_address() const { return related_address_; }
142 void set_related_address(const rtc::SocketAddress& related_address) {
Patrik Höglunde2d6a062017-10-05 12:53:33143 related_address_ = related_address;
144 }
145 const std::string& tcptype() const { return tcptype_; }
Niels Möllerd4aa3a32021-09-29 11:23:01146 void set_tcptype(absl::string_view tcptype) { Assign(tcptype_, tcptype); }
Patrik Höglunde2d6a062017-10-05 12:53:33147
148 // The name of the transport channel of this candidate.
149 // TODO(phoglund): remove.
150 const std::string& transport_name() const { return transport_name_; }
Niels Möllerd4aa3a32021-09-29 11:23:01151 void set_transport_name(absl::string_view transport_name) {
152 Assign(transport_name_, transport_name);
Patrik Höglunde2d6a062017-10-05 12:53:33153 }
154
155 // The URL of the ICE server which this candidate is gathered from.
156 const std::string& url() const { return url_; }
Niels Möllerd4aa3a32021-09-29 11:23:01157 void set_url(absl::string_view url) { Assign(url_, url); }
Patrik Höglunde2d6a062017-10-05 12:53:33158
159 // Determines whether this candidate is equivalent to the given one.
Steve Anton36b28db2017-10-26 18:27:17160 bool IsEquivalent(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 12:53:33161
162 // Determines whether this candidate can be considered equivalent to the
163 // given one when looking for a matching candidate to remove.
Steve Anton36b28db2017-10-26 18:27:17164 bool MatchesForRemoval(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 12:53:33165
Yves Gerey665174f2018-06-19 13:03:05166 std::string ToString() const { return ToStringInternal(false); }
Patrik Höglunde2d6a062017-10-05 12:53:33167
Yves Gerey665174f2018-06-19 13:03:05168 std::string ToSensitiveString() const { return ToStringInternal(true); }
Patrik Höglunde2d6a062017-10-05 12:53:33169
170 uint32_t GetPriority(uint32_t type_preference,
171 int network_adapter_preference,
Steve Anton36b28db2017-10-26 18:27:17172 int relay_preference) const;
Patrik Höglunde2d6a062017-10-05 12:53:33173
Steve Anton36b28db2017-10-26 18:27:17174 bool operator==(const Candidate& o) const;
175 bool operator!=(const Candidate& o) const;
Patrik Höglunde2d6a062017-10-05 12:53:33176
Qingsi Wang1dac6d82018-12-12 23:28:47177 // Returns a sanitized copy configured by the given booleans. If
Artem Titov0e61fdd2021-07-25 19:50:14178 // `use_host_address` is true, the returned copy has its IP removed from
179 // `address()`, which leads `address()` to be a hostname address. If
180 // `filter_related_address`, the returned copy has its related address reset
Qingsi Wang1dac6d82018-12-12 23:28:47181 // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that
182 // setting both booleans to false returns an identical copy to the original
183 // candidate.
184 Candidate ToSanitizedCopy(bool use_hostname_address,
185 bool filter_related_address) const;
186
Patrik Höglunde2d6a062017-10-05 12:53:33187 private:
Niels Möllerd4aa3a32021-09-29 11:23:01188 // TODO(bugs.webrtc.org/13220): With C++17, we get a std::string assignment
189 // operator accepting any object implicitly convertible to std::string_view,
190 // and then we don't need this workaround.
191 static void Assign(std::string& s, absl::string_view view);
Steve Anton36b28db2017-10-26 18:27:17192 std::string ToStringInternal(bool sensitive) const;
Patrik Höglunde2d6a062017-10-05 12:53:33193
194 std::string id_;
195 int component_;
196 std::string protocol_;
197 std::string relay_protocol_;
198 rtc::SocketAddress address_;
199 uint32_t priority_;
200 std::string username_;
201 std::string password_;
202 std::string type_;
203 std::string network_name_;
204 rtc::AdapterType network_type_;
Jonas Oreland0d13bbd2022-03-02 10:17:36205 rtc::AdapterType underlying_type_for_vpn_;
Patrik Höglunde2d6a062017-10-05 12:53:33206 uint32_t generation_;
207 std::string foundation_;
208 rtc::SocketAddress related_address_;
209 std::string tcptype_;
210 std::string transport_name_;
211 uint16_t network_id_;
212 uint16_t network_cost_;
213 std::string url_;
214};
215
216} // namespace cricket
217
218#endif // API_CANDIDATE_H_