Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 1 | /* |
| 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öglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 21 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/socket_address.h" |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 24 | |
| 25 | namespace 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 Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 30 | class RTC_EXPORT Candidate { |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 31 | public: |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 32 | Candidate(); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 33 | // 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öglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 35 | 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 Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 45 | uint16_t network_cost = 0); |
| 46 | Candidate(const Candidate&); |
| 47 | ~Candidate(); |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 48 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | const std::string& id() const { return id_; } |
| 50 | void set_id(const std::string& id) { id_ = id; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 51 | |
| 52 | int component() const { return component_; } |
| 53 | void set_component(int component) { component_ = component; } |
| 54 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | const std::string& protocol() const { return protocol_; } |
| 56 | void set_protocol(const std::string& protocol) { protocol_ = protocol; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | const rtc::SocketAddress& address() const { return address_; } |
| 65 | void set_address(const rtc::SocketAddress& address) { address_ = address; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 66 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | const std::string& username() const { return username_; } |
| 93 | void set_username(const std::string& username) { username_ = username; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 94 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 95 | const std::string& password() const { return password_; } |
| 96 | void set_password(const std::string& password) { password_ = password; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 97 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | const std::string& type() const { return type_; } |
| 99 | void set_type(const std::string& type) { type_ = type; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 100 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | const std::string& network_name() const { return network_name_; } |
| 102 | void set_network_name(const std::string& network_name) { |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 103 | 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öglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 114 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | const std::string& foundation() const { return foundation_; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 129 | void set_foundation(const std::string& foundation) { |
| 130 | foundation_ = foundation; |
| 131 | } |
| 132 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 133 | const rtc::SocketAddress& related_address() const { return related_address_; } |
| 134 | void set_related_address(const rtc::SocketAddress& related_address) { |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 135 | related_address_ = related_address; |
| 136 | } |
| 137 | const std::string& tcptype() const { return tcptype_; } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 138 | void set_tcptype(const std::string& tcptype) { tcptype_ = tcptype; } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 139 | |
| 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 Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 152 | bool IsEquivalent(const Candidate& c) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 153 | |
| 154 | // Determines whether this candidate can be considered equivalent to the |
| 155 | // given one when looking for a matching candidate to remove. |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 156 | bool MatchesForRemoval(const Candidate& c) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 157 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 158 | std::string ToString() const { return ToStringInternal(false); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 159 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | std::string ToSensitiveString() const { return ToStringInternal(true); } |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 161 | |
| 162 | uint32_t GetPriority(uint32_t type_preference, |
| 163 | int network_adapter_preference, |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 164 | int relay_preference) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 165 | |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 166 | bool operator==(const Candidate& o) const; |
| 167 | bool operator!=(const Candidate& o) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 168 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 169 | // 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öglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 179 | private: |
Steve Anton | 36b28db | 2017-10-26 11:27:17 -0700 | [diff] [blame] | 180 | std::string ToStringInternal(bool sensitive) const; |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 181 | |
| 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_ |