[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tfarina | 77021d6 | 2015-10-11 20:19:03 | [diff] [blame] | 5 | #ifndef NET_DNS_DNS_UTIL_H_ |
| 6 | #define NET_DNS_DNS_UTIL_H_ |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 10 | #include "base/strings/string_piece.h" |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 11 | #include "base/time/time.h" |
Eric Orth | 192e3bb | 2018-11-14 19:30:32 | [diff] [blame] | 12 | #include "net/base/address_family.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 13 | #include "net/base/net_export.h" |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 14 | #include "net/base/network_change_notifier.h" |
Eric Orth | 192e3bb | 2018-11-14 19:30:32 | [diff] [blame] | 15 | #include "net/dns/public/dns_query_type.h" |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 16 | |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 17 | namespace net { |
| 18 | |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 19 | class AddressList; |
| 20 | |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 21 | // DNSDomainFromDot - convert a domain string to DNS format. From DJB's |
| 22 | // public domain DNS library. |
| 23 | // |
| 24 | // dotted: a string in dotted form: "www.google.com" |
| 25 | // out: a result in DNS form: "\x03www\x06google\x03com\x00" |
Qingsi Wang | c2213d5d | 2018-11-02 01:28:12 | [diff] [blame] | 26 | NET_EXPORT bool DNSDomainFromDot(const base::StringPiece& dotted, |
| 27 | std::string* out); |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 28 | |
mmenke | d6e9161aff | 2016-09-09 21:55:44 | [diff] [blame] | 29 | // Checks that a hostname is valid. Simple wrapper around DNSDomainFromDot. |
| 30 | NET_EXPORT_PRIVATE bool IsValidDNSDomain(const base::StringPiece& dotted); |
| 31 | |
palmer | 6170ab9 | 2017-03-23 20:57:54 | [diff] [blame] | 32 | // Returns true if the character is valid in a DNS hostname label, whether in |
| 33 | // the first position or later in the label. |
| 34 | // |
| 35 | // This function asserts a looser form of the restrictions in RFC 7719 (section |
| 36 | // 2; https://tools.ietf.org/html/rfc7719#section-2): hostnames can include |
| 37 | // characters a-z, A-Z, 0-9, -, and _, and any of those characters (except -) |
| 38 | // are legal in the first position. The looser rules are necessary to support |
| 39 | // service records (initial _), and non-compliant but attested hostnames that |
| 40 | // include _. These looser rules also allow Punycode and hence IDN. |
| 41 | // |
| 42 | // TODO(palmer): In the future, when we can remove support for invalid names, |
| 43 | // this can be a private implementation detail of |DNSDomainFromDot|, and need |
| 44 | // not be NET_EXPORT_PRIVATE. |
| 45 | NET_EXPORT_PRIVATE bool IsValidHostLabelCharacter(char c, bool is_first_char); |
| 46 | |
[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 47 | // DNSDomainToString converts a domain in DNS format to a dotted string. |
| 48 | // Excludes the dot at the end. |
Qingsi Wang | c2213d5d | 2018-11-02 01:28:12 | [diff] [blame] | 49 | NET_EXPORT std::string DNSDomainToString(const base::StringPiece& domain); |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 50 | |
dalyk | b34631f | 2018-08-24 19:59:48 | [diff] [blame] | 51 | // Return the expanded template when no variables have corresponding values. |
| 52 | NET_EXPORT_PRIVATE std::string GetURLFromTemplateWithoutParameters( |
| 53 | const std::string& server_template); |
| 54 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 55 | #if !defined(OS_NACL) |
| 56 | NET_EXPORT_PRIVATE |
| 57 | base::TimeDelta GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 58 | const char* field_trial_name, |
| 59 | base::TimeDelta default_delta, |
| 60 | NetworkChangeNotifier::ConnectionType connection_type); |
| 61 | #endif // !defined(OS_NACL) |
| 62 | |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 63 | // How similar or different two AddressLists are (see values for details). |
| 64 | // Used in histograms; do not modify existing values. |
| 65 | enum AddressListDeltaType { |
| 66 | // Both lists contain the same addresses in the same order. |
| 67 | DELTA_IDENTICAL = 0, |
| 68 | // Both lists contain the same addresses in a different order. |
| 69 | DELTA_REORDERED = 1, |
| 70 | // The two lists have at least one address in common, but not all of them. |
| 71 | DELTA_OVERLAP = 2, |
| 72 | // The two lists have no addresses in common. |
| 73 | DELTA_DISJOINT = 3, |
| 74 | MAX_DELTA_TYPE |
| 75 | }; |
| 76 | |
| 77 | // Compares two AddressLists to see how similar or different their addresses |
| 78 | // are. (See |AddressListDeltaType| for details of exactly what's checked.) |
Wez | 115d431 | 2018-02-17 04:05:19 | [diff] [blame] | 79 | NET_EXPORT |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 80 | AddressListDeltaType FindAddressListDeltaType(const AddressList& a, |
| 81 | const AddressList& b); |
| 82 | |
Qingsi Wang | c2213d5d | 2018-11-02 01:28:12 | [diff] [blame] | 83 | // Creates a 2-byte string that represents the name pointer defined in Section |
| 84 | // 4.1.1 of RFC 1035 for the given offset. The first two bits in the first byte |
| 85 | // of the name pointer are ones, and the rest 14 bits are given to |offset|, |
| 86 | // which specifies an offset from the start of the message for the pointed name. |
| 87 | // Note that |offset| must be less than 2^14 - 1 by definition. |
| 88 | NET_EXPORT std::string CreateNamePointer(uint16_t offset); |
| 89 | |
Eric Orth | 192e3bb | 2018-11-14 19:30:32 | [diff] [blame] | 90 | // Convert a DnsQueryType enum to the wire format integer representation. |
| 91 | uint16_t DnsQueryTypeToQtype(DnsQueryType dns_query_type); |
| 92 | |
| 93 | NET_EXPORT DnsQueryType |
| 94 | AddressFamilyToDnsQueryType(AddressFamily address_family); |
| 95 | |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 96 | } // namespace net |
| 97 | |
tfarina | 77021d6 | 2015-10-11 20:19:03 | [diff] [blame] | 98 | #endif // NET_DNS_DNS_UTIL_H_ |