1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_DNS_UTIL_H_ 6 #define NET_DNS_DNS_UTIL_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/strings/string_piece.h" 12 #include "base/time/time.h" 13 #include "net/base/address_family.h" 14 #include "net/base/ip_endpoint.h" 15 #include "net/base/net_export.h" 16 #include "net/base/network_change_notifier.h" 17 #include "net/dns/dns_config.h" 18 #include "net/dns/public/dns_over_https_server_config.h" 19 #include "net/dns/public/dns_query_type.h" 20 21 namespace net { 22 23 class AddressList; 24 25 // DNSDomainFromDot - convert a domain string to DNS format. From DJB's 26 // public domain DNS library. |dotted| may include only characters a-z, A-Z, 27 // 0-9, -, and _. 28 // 29 // dotted: a string in dotted form: "www.google.com" 30 // out: a result in DNS form: "\x03www\x06google\x03com\x00" 31 NET_EXPORT bool DNSDomainFromDot(const base::StringPiece& dotted, 32 std::string* out); 33 34 // DNSDomainFromUnrestrictedDot - convert a domain string to DNS format. Adapted 35 // from DJB's public domain DNS library. No validation of the characters in 36 // |dotted| is performed. 37 // 38 // dotted: a string in dotted form: "Foo Printer._tcp.local" 39 // out: a result in DNS form: "\x0bFoo Printer\x04_tcp\x05local\x00" 40 NET_EXPORT bool DNSDomainFromUnrestrictedDot(const base::StringPiece& dotted, 41 std::string* out); 42 43 // Checks that a hostname is valid. Simple wrapper around DNSDomainFromDot. 44 NET_EXPORT_PRIVATE bool IsValidDNSDomain(const base::StringPiece& dotted); 45 46 // Checks that a hostname is valid. Simple wrapper around 47 // DNSDomainFromUnrestrictedDot. 48 NET_EXPORT_PRIVATE bool IsValidUnrestrictedDNSDomain( 49 const base::StringPiece& dotted); 50 51 // Returns true if the character is valid in a DNS hostname label, whether in 52 // the first position or later in the label. 53 // 54 // This function asserts a looser form of the restrictions in RFC 7719 (section 55 // 2; https://tools.ietf.org/html/rfc7719#section-2): hostnames can include 56 // characters a-z, A-Z, 0-9, -, and _, and any of those characters (except -) 57 // are legal in the first position. The looser rules are necessary to support 58 // service records (initial _), and non-compliant but attested hostnames that 59 // include _. These looser rules also allow Punycode and hence IDN. 60 // 61 // TODO(palmer): In the future, when we can remove support for invalid names, 62 // this can be a private implementation detail of |DNSDomainFromDot|, and need 63 // not be NET_EXPORT_PRIVATE. 64 NET_EXPORT_PRIVATE bool IsValidHostLabelCharacter(char c, bool is_first_char); 65 66 // DNSDomainToString converts a domain in DNS format to a dotted string. 67 // Excludes the dot at the end. 68 NET_EXPORT std::string DNSDomainToString(const base::StringPiece& domain); 69 70 // Return the expanded template when no variables have corresponding values. 71 NET_EXPORT_PRIVATE std::string GetURLFromTemplateWithoutParameters( 72 const std::string& server_template); 73 74 #if !defined(OS_NACL) 75 NET_EXPORT_PRIVATE 76 base::TimeDelta GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( 77 const char* field_trial_name, 78 base::TimeDelta default_delta, 79 NetworkChangeNotifier::ConnectionType connection_type); 80 #endif // !defined(OS_NACL) 81 82 // How similar or different two AddressLists are (see values for details). 83 // Used in histograms; do not modify existing values. 84 enum AddressListDeltaType { 85 // Both lists contain the same addresses in the same order. 86 DELTA_IDENTICAL = 0, 87 // Both lists contain the same addresses in a different order. 88 DELTA_REORDERED = 1, 89 // The two lists have at least one address in common, but not all of them. 90 DELTA_OVERLAP = 2, 91 // The two lists have no addresses in common. 92 DELTA_DISJOINT = 3, 93 MAX_DELTA_TYPE 94 }; 95 96 // Compares two AddressLists to see how similar or different their addresses 97 // are. (See |AddressListDeltaType| for details of exactly what's checked.) 98 NET_EXPORT 99 AddressListDeltaType FindAddressListDeltaType(const AddressList& a, 100 const AddressList& b); 101 102 // Creates a 2-byte string that represents the name pointer defined in Section 103 // 4.1.1 of RFC 1035 for the given offset. The first two bits in the first byte 104 // of the name pointer are ones, and the rest 14 bits are given to |offset|, 105 // which specifies an offset from the start of the message for the pointed name. 106 // Note that |offset| must be less than 2^14 - 1 by definition. 107 NET_EXPORT std::string CreateNamePointer(uint16_t offset); 108 109 // Convert a DnsQueryType enum to the wire format integer representation. 110 NET_EXPORT_PRIVATE uint16_t DnsQueryTypeToQtype(DnsQueryType dns_query_type); 111 112 NET_EXPORT DnsQueryType 113 AddressFamilyToDnsQueryType(AddressFamily address_family); 114 115 // Uses the hardcoded upgrade mapping to discover DoH service(s) associated 116 // with a DoT hostname. Providers listed in |excluded_providers| are not 117 // eligible for upgrade. 118 NET_EXPORT_PRIVATE std::vector<DnsOverHttpsServerConfig> 119 GetDohUpgradeServersFromDotHostname( 120 const std::string& dot_server, 121 const std::vector<std::string>& excluded_providers); 122 123 // Uses the hardcoded upgrade mapping to discover DoH service(s) associated 124 // with a list of insecure DNS servers. Server ordering is preserved across 125 // the mapping. Providers listed in |excluded_providers| are not 126 // eligible for upgrade. 127 NET_EXPORT_PRIVATE std::vector<DnsOverHttpsServerConfig> 128 GetDohUpgradeServersFromNameservers( 129 const std::vector<IPEndPoint>& dns_servers, 130 const std::vector<std::string>& excluded_providers); 131 132 // Returns the provider id to use in UMA histogram names. If there is no 133 // provider id that matches |doh_server|, returns "Other". 134 NET_EXPORT_PRIVATE std::string GetDohProviderIdForHistogramFromDohConfig( 135 const DnsOverHttpsServerConfig& doh_server); 136 137 // Returns the provider id to use in UMA histogram names. If there is no 138 // provider id that matches |nameserver|, returns "Other". 139 NET_EXPORT_PRIVATE std::string GetDohProviderIdForHistogramFromNameserver( 140 const IPEndPoint& nameserver); 141 142 NET_EXPORT_PRIVATE std::string SecureDnsModeToString( 143 const DnsConfig::SecureDnsMode secure_dns_mode); 144 145 } // namespace net 146 147 #endif // NET_DNS_DNS_UTIL_H_ 148