blob: 745205575b0296c1c8fb32b94dfd2d1ab84aba43 [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"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020021#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/socket_address.h"
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020023#include "rtc_base/system/rtc_export.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020024
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
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020030class RTC_EXPORT Candidate {
Patrik Höglunde2d6a062017-10-05 14:53:33 +020031 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
Yves Gerey665174f2018-06-19 15:03:05 +020049 const std::string& id() const { return id_; }
50 void set_id(const std::string& id) { id_ = id; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020051
52 int component() const { return component_; }
53 void set_component(int component) { component_ = component; }
54
Yves Gerey665174f2018-06-19 15:03:05 +020055 const std::string& protocol() const { return protocol_; }
56 void set_protocol(const std::string& protocol) { protocol_ = protocol; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020057
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
Yves Gerey665174f2018-06-19 15:03:05 +020064 const rtc::SocketAddress& address() const { return address_; }
65 void set_address(const rtc::SocketAddress& address) { address_ = address; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020066
67 uint32_t priority() const { return priority_; }
68 void set_priority(const uint32_t priority) { priority_ = priority; }
69
70 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
71 // doesn't use it.
72 // Maps old preference (which was 0.0-1.0) to match priority (which
73 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
74 // https://docs.google.com/a/google.com/document/d/
75 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
76 float preference() const {
77 // The preference value is clamped to two decimal precision.
78 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
79 }
80
81 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
82 // doesn't use it.
83 void set_preference(float preference) {
84 // Limiting priority to UINT_MAX when value exceeds uint32_t max.
85 // This can happen for e.g. when preference = 3.
86 uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
87 priority_ = static_cast<uint32_t>(
88 std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
89 }
90
91 // TODO(honghaiz): Change to usernameFragment or ufrag.
Yves Gerey665174f2018-06-19 15:03:05 +020092 const std::string& username() const { return username_; }
93 void set_username(const std::string& username) { username_ = username; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020094
Yves Gerey665174f2018-06-19 15:03:05 +020095 const std::string& password() const { return password_; }
96 void set_password(const std::string& password) { password_ = password; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020097
Yves Gerey665174f2018-06-19 15:03:05 +020098 const std::string& type() const { return type_; }
99 void set_type(const std::string& type) { type_ = type; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200100
Yves Gerey665174f2018-06-19 15:03:05 +0200101 const std::string& network_name() const { return network_name_; }
102 void set_network_name(const std::string& network_name) {
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200103 network_name_ = network_name;
104 }
105
106 rtc::AdapterType network_type() const { return network_type_; }
107 void set_network_type(rtc::AdapterType network_type) {
108 network_type_ = network_type;
109 }
110
111 // Candidates in a new generation replace those in the old generation.
112 uint32_t generation() const { return generation_; }
113 void set_generation(uint32_t generation) { generation_ = generation; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200114
115 // |network_cost| measures the cost/penalty of using this candidate. A network
116 // cost of 0 indicates this candidate can be used freely. A value of
117 // rtc::kNetworkCostMax indicates it should be used only as the last resort.
118 void set_network_cost(uint16_t network_cost) {
119 RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
120 network_cost_ = network_cost;
121 }
122 uint16_t network_cost() const { return network_cost_; }
123
124 // An ID assigned to the network hosting the candidate.
125 uint16_t network_id() const { return network_id_; }
126 void set_network_id(uint16_t network_id) { network_id_ = network_id; }
127
Yves Gerey665174f2018-06-19 15:03:05 +0200128 const std::string& foundation() const { return foundation_; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200129 void set_foundation(const std::string& foundation) {
130 foundation_ = foundation;
131 }
132
Yves Gerey665174f2018-06-19 15:03:05 +0200133 const rtc::SocketAddress& related_address() const { return related_address_; }
134 void set_related_address(const rtc::SocketAddress& related_address) {
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200135 related_address_ = related_address;
136 }
137 const std::string& tcptype() const { return tcptype_; }
Yves Gerey665174f2018-06-19 15:03:05 +0200138 void set_tcptype(const std::string& tcptype) { tcptype_ = tcptype; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200139
140 // The name of the transport channel of this candidate.
141 // TODO(phoglund): remove.
142 const std::string& transport_name() const { return transport_name_; }
143 void set_transport_name(const std::string& transport_name) {
144 transport_name_ = transport_name;
145 }
146
147 // The URL of the ICE server which this candidate is gathered from.
148 const std::string& url() const { return url_; }
149 void set_url(const std::string& url) { url_ = url; }
150
151 // Determines whether this candidate is equivalent to the given one.
Steve Anton36b28db2017-10-26 11:27:17 -0700152 bool IsEquivalent(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200153
154 // Determines whether this candidate can be considered equivalent to the
155 // given one when looking for a matching candidate to remove.
Steve Anton36b28db2017-10-26 11:27:17 -0700156 bool MatchesForRemoval(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200157
Yves Gerey665174f2018-06-19 15:03:05 +0200158 std::string ToString() const { return ToStringInternal(false); }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200159
Yves Gerey665174f2018-06-19 15:03:05 +0200160 std::string ToSensitiveString() const { return ToStringInternal(true); }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200161
162 uint32_t GetPriority(uint32_t type_preference,
163 int network_adapter_preference,
Steve Anton36b28db2017-10-26 11:27:17 -0700164 int relay_preference) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200165
Steve Anton36b28db2017-10-26 11:27:17 -0700166 bool operator==(const Candidate& o) const;
167 bool operator!=(const Candidate& o) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200168
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800169 // Returns a sanitized copy configured by the given booleans. If
170 // |use_host_address| is true, the returned copy has its IP removed from
171 // |address()|, which leads |address()| to be a hostname address. If
172 // |filter_related_address|, the returned copy has its related address reset
173 // to the wildcard address (i.e. 0.0.0.0 for IPv4 and :: for IPv6). Note that
174 // setting both booleans to false returns an identical copy to the original
175 // candidate.
176 Candidate ToSanitizedCopy(bool use_hostname_address,
177 bool filter_related_address) const;
178
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200179 private:
Steve Anton36b28db2017-10-26 11:27:17 -0700180 std::string ToStringInternal(bool sensitive) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200181
182 std::string id_;
183 int component_;
184 std::string protocol_;
185 std::string relay_protocol_;
186 rtc::SocketAddress address_;
187 uint32_t priority_;
188 std::string username_;
189 std::string password_;
190 std::string type_;
191 std::string network_name_;
192 rtc::AdapterType network_type_;
193 uint32_t generation_;
194 std::string foundation_;
195 rtc::SocketAddress related_address_;
196 std::string tcptype_;
197 std::string transport_name_;
198 uint16_t network_id_;
199 uint16_t network_cost_;
200 std::string url_;
201};
202
203} // namespace cricket
204
205#endif // API_CANDIDATE_H_