File: dns_names_util.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (95 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
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
// 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_DNS_NAMES_UTIL_H_
#define NET_DNS_DNS_NAMES_UTIL_H_

#include <cstdint>
#include <string>
#include <vector>

#include "base/containers/span.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "net/base/net_export.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace base {
class BigEndianReader;
}  // namespace base

// Various utilities for converting, validating, and comparing DNS names.
namespace net::dns_names_util {

// Returns true iff `dotted` is acceptable to be encoded as a DNS name. That is
// that it is non-empty and fits size limitations. Also must match the expected
// structure of dot-separated labels, each non-empty and fitting within
// additional size limitations, and an optional dot at the end. See RFCs 1035
// and 2181.
//
// No validation is performed for correctness of characters within a label.
// As explained by RFC 2181, commonly cited rules for such characters are not
// DNS restrictions, but actually restrictions for Internet hostnames. For such
// validation, see IsCanonicalizedHostCompliant().
NET_EXPORT_PRIVATE bool IsValidDnsName(base::StringPiece dotted_form_name);

// Like IsValidDnsName() but further validates `dotted_form_name` is not an IP
// address (with or without surrounding []) or localhost, as such names would
// not be suitable for DNS queries or for use as DNS record names or alias
// target names.
NET_EXPORT_PRIVATE bool IsValidDnsRecordName(
    base::StringPiece dotted_form_name);

// Convert a dotted-form DNS name to network wire format. Returns nullopt if
// input is not valid for conversion (equivalent validity can be checked using
// IsValidDnsName()). If `require_valid_internet_hostname` is true, also returns
// nullopt if input is not a valid internet hostname (equivalent validity can be
// checked using net::IsCanonicalizedHostCompliant()).
NET_EXPORT_PRIVATE absl::optional<std::vector<uint8_t>> DottedNameToNetwork(
    base::StringPiece dotted_form_name,
    bool require_valid_internet_hostname = false);

// Converts a domain in DNS format to a dotted string. Excludes the dot at the
// end.  Returns nullopt on malformed input.
//
// If `require_complete` is true, input will be considered malformed if it does
// not contain a terminating zero-length label. If false, assumes the standard
// terminating zero-length label at the end if not included in the input.
//
// DNS name compression (see RFC 1035, section 4.1.4) is disallowed and
// considered malformed. To handle a potentially compressed name, in a
// DnsResponse object, use DnsRecordParser::ReadName().
NET_EXPORT_PRIVATE absl::optional<std::string> NetworkToDottedName(
    base::span<const uint8_t> dns_network_wire_name,
    bool require_complete = false);
NET_EXPORT_PRIVATE absl::optional<std::string> NetworkToDottedName(
    base::StringPiece dns_network_wire_name,
    bool require_complete = false);
NET_EXPORT_PRIVATE absl::optional<std::string> NetworkToDottedName(
    base::BigEndianReader& reader,
    bool require_complete = false);

// Canonicalize `name` as a URL hostname if able. If unable (typically if a name
// is not a valid URL hostname), returns `name` without change because such a
// name could still be a valid DNS name.
NET_EXPORT_PRIVATE std::string UrlCanonicalizeNameIfAble(
    base::StringPiece name);

// std::map-compliant Compare for two domain names. Works for any valid
// dotted-format or network-wire-format names. Returns true iff `lhs` is before
// `rhs` in strict weak ordering.
class NET_EXPORT_PRIVATE DomainNameComparator {
 public:
  bool operator()(base::StringPiece lhs, base::StringPiece rhs) const {
    // This works for dotted format or network-wire format as long as the names
    // are valid because valid network-wire names have labels of max 63 bytes
    // and thus will never have label length prefixes high enough to be
    // misinterpreted as capital letters ('A' is 65).
    return base::CompareCaseInsensitiveASCII(lhs, rhs) < 0;
  }
};

}  // namespace net::dns_names_util

#endif  // NET_DNS_DNS_NAMES_UTIL_H_