1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_DNS_HOST_RESOLVER_INTERNAL_RESULT_H_
#define NET_DNS_HOST_RESOLVER_INTERNAL_RESULT_H_
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <vector>
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/connection_endpoint_metadata.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_export.h"
#include "net/dns/https_record_rdata.h"
#include "net/dns/public/dns_query_type.h"
namespace net {
class HostResolverInternalDataResult;
class HostResolverInternalMetadataResult;
class HostResolverInternalErrorResult;
class HostResolverInternalAliasResult;
// Parsed and extracted result type for use internally to HostResolver code.
class NET_EXPORT_PRIVATE HostResolverInternalResult {
public:
enum class Type { kData, kMetadata, kError, kAlias };
enum class Source { kDns, kHosts, kUnknown };
// Nullptr if `value` is malformed to be deserialized.
static std::unique_ptr<HostResolverInternalResult> FromValue(
const base::Value& value);
virtual ~HostResolverInternalResult() = default;
const std::string& domain_name() const { return domain_name_; }
DnsQueryType query_type() const { return query_type_; }
Type type() const { return type_; }
Source source() const { return source_; }
std::optional<base::TimeTicks> expiration() const { return expiration_; }
std::optional<base::Time> timed_expiration() const {
return timed_expiration_;
}
const HostResolverInternalDataResult& AsData() const;
HostResolverInternalDataResult& AsData();
const HostResolverInternalMetadataResult& AsMetadata() const;
HostResolverInternalMetadataResult& AsMetadata();
const HostResolverInternalErrorResult& AsError() const;
HostResolverInternalErrorResult& AsError();
const HostResolverInternalAliasResult& AsAlias() const;
HostResolverInternalAliasResult& AsAlias();
virtual std::unique_ptr<HostResolverInternalResult> Clone() const = 0;
virtual base::Value ToValue() const = 0;
protected:
HostResolverInternalResult(std::string domain_name,
DnsQueryType query_type,
std::optional<base::TimeTicks> expiration,
std::optional<base::Time> timed_expiration,
Type type,
Source source);
// Expect to only be called with a `dict` well-formed for deserialization. Can
// be checked via ValidateValueBaseDict().
explicit HostResolverInternalResult(const base::Value::Dict& dict);
bool operator==(const HostResolverInternalResult& other) const {
return std::tie(domain_name_, query_type_, type_, source_, expiration_,
timed_expiration_) ==
std::tie(other.domain_name_, other.query_type_, other.type_,
other.source_, other.expiration_, other.timed_expiration_);
}
static bool ValidateValueBaseDict(const base::Value::Dict& dict,
bool require_timed_expiration);
base::Value::Dict ToValueBaseDict() const;
private:
const std::string domain_name_;
const DnsQueryType query_type_;
const Type type_;
const Source source_;
// Expiration logic should prefer to be based on `expiration_` for correctness
// through system time changes. But if result has been serialized to disk, it
// may be that only `timed_expiration_` is available.
const std::optional<base::TimeTicks> expiration_;
const std::optional<base::Time> timed_expiration_;
};
// Parsed and extracted result containing result data.
class NET_EXPORT_PRIVATE HostResolverInternalDataResult final
: public HostResolverInternalResult {
public:
static std::unique_ptr<HostResolverInternalDataResult> FromValue(
const base::Value& value);
// `domain_name` is dotted form.
HostResolverInternalDataResult(std::string domain_name,
DnsQueryType query_type,
std::optional<base::TimeTicks> expiration,
base::Time timed_expiration,
Source source,
std::vector<IPEndPoint> endpoints,
std::vector<std::string> strings,
std::vector<HostPortPair> hosts);
~HostResolverInternalDataResult() override;
HostResolverInternalDataResult(const HostResolverInternalDataResult&) =
delete;
HostResolverInternalDataResult& operator=(
const HostResolverInternalDataResult&) = delete;
bool operator==(const HostResolverInternalDataResult& other) const {
return HostResolverInternalResult::operator==(other) &&
std::tie(endpoints_, strings_, hosts_) ==
std::tie(other.endpoints_, other.strings_, other.hosts_);
}
const std::vector<IPEndPoint>& endpoints() const { return endpoints_; }
void set_endpoints(std::vector<IPEndPoint> endpoints) {
endpoints_ = std::move(endpoints);
}
const std::vector<std::string>& strings() const { return strings_; }
void set_strings(std::vector<std::string> strings) {
strings_ = std::move(strings);
}
const std::vector<HostPortPair>& hosts() const { return hosts_; }
void set_hosts(std::vector<HostPortPair> hosts) { hosts_ = std::move(hosts); }
std::unique_ptr<HostResolverInternalResult> Clone() const override;
base::Value ToValue() const override;
private:
HostResolverInternalDataResult(const base::Value::Dict& dict,
std::vector<IPEndPoint> endpoints,
std::vector<std::string> strings,
std::vector<HostPortPair> hosts);
// Corresponds to the `HostResolverEndpointResult::ip_endpoints` portion of
// `HostResolver::ResolveHostRequest::GetEndpointResults()`.
std::vector<IPEndPoint> endpoints_;
// Corresponds to `HostResolver::ResolveHostRequest::GetTextResults()`.
std::vector<std::string> strings_;
// Corresponds to `HostResolver::ResolveHostRequest::GetHostnameResults()`.
std::vector<HostPortPair> hosts_;
};
// Parsed and extracted connection metadata, but not usable on its own without
// being paired with separate HostResolverInternalDataResult data (for the
// domain name specified by `ConnectionEndpointMetadata::target_name`). An empty
// metadata result signifies that compatible HTTPS records were received but
// with no contained metadata of use to Chrome.
class NET_EXPORT_PRIVATE HostResolverInternalMetadataResult final
: public HostResolverInternalResult {
public:
static std::unique_ptr<HostResolverInternalMetadataResult> FromValue(
const base::Value& value);
// `domain_name` and `data_domain` are dotted form domain names.
HostResolverInternalMetadataResult(
std::string domain_name,
DnsQueryType query_type,
std::optional<base::TimeTicks> expiration,
base::Time timed_expiration,
Source source,
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata> metadatas);
~HostResolverInternalMetadataResult() override;
HostResolverInternalMetadataResult(
const HostResolverInternalMetadataResult&) = delete;
HostResolverInternalMetadataResult& operator=(
const HostResolverInternalMetadataResult&) = delete;
bool operator==(const HostResolverInternalMetadataResult& other) const {
return HostResolverInternalResult::operator==(other) &&
metadatas_ == other.metadatas_;
}
const std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>&
metadatas() const {
return metadatas_;
}
std::unique_ptr<HostResolverInternalResult> Clone() const override;
base::Value ToValue() const override;
private:
HostResolverInternalMetadataResult(
const base::Value::Dict& dict,
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata> metadatas);
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata> metadatas_;
};
// Parsed and extracted error.
class NET_EXPORT_PRIVATE HostResolverInternalErrorResult final
: public HostResolverInternalResult {
public:
static std::unique_ptr<HostResolverInternalErrorResult> FromValue(
const base::Value& value);
// `domain_name` is dotted form. `timed_expiration` may be `nullopt` for
// non-cacheable errors.
HostResolverInternalErrorResult(std::string domain_name,
DnsQueryType query_type,
std::optional<base::TimeTicks> expiration,
std::optional<base::Time> timed_expiration,
Source source,
int error);
~HostResolverInternalErrorResult() override = default;
HostResolverInternalErrorResult(const HostResolverInternalErrorResult&) =
delete;
HostResolverInternalErrorResult& operator=(
const HostResolverInternalErrorResult&) = delete;
bool operator==(const HostResolverInternalErrorResult& other) const {
return HostResolverInternalResult::operator==(other) &&
error_ == other.error_;
}
int error() const { return error_; }
std::unique_ptr<HostResolverInternalResult> Clone() const override;
base::Value ToValue() const override;
private:
HostResolverInternalErrorResult(const base::Value::Dict& dict, int error);
const int error_;
};
// Parsed and extracted alias (CNAME or alias-type HTTPS).
class NET_EXPORT_PRIVATE HostResolverInternalAliasResult final
: public HostResolverInternalResult {
public:
static std::unique_ptr<HostResolverInternalAliasResult> FromValue(
const base::Value& value);
// `domain_name` and `alias_target` are dotted form domain names.
HostResolverInternalAliasResult(std::string domain_name,
DnsQueryType query_type,
std::optional<base::TimeTicks> expiration,
base::Time timed_expiration,
Source source,
std::string alias_target);
~HostResolverInternalAliasResult() override = default;
HostResolverInternalAliasResult(const HostResolverInternalAliasResult&) =
delete;
HostResolverInternalAliasResult& operator=(
const HostResolverInternalAliasResult&) = delete;
bool operator==(const HostResolverInternalAliasResult& other) const {
return HostResolverInternalResult::operator==(other) &&
alias_target_ == other.alias_target_;
}
const std::string& alias_target() const { return alias_target_; }
std::unique_ptr<HostResolverInternalResult> Clone() const override;
base::Value ToValue() const override;
private:
HostResolverInternalAliasResult(const base::Value::Dict& dict,
std::string alias_target);
const std::string alias_target_;
};
} // namespace net
#endif // NET_DNS_HOST_RESOLVER_INTERNAL_RESULT_H_
|