blob: 6e2974802fa60417d9cbcda806f559b4412c7ece [file] [log] [blame]
[email protected]d0d49dd82012-01-26 00:03:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c36f0642009-09-09 01:10:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tfarina77021d62015-10-11 20:19:035#ifndef NET_DNS_DNS_UTIL_H_
6#define NET_DNS_DNS_UTIL_H_
[email protected]c36f0642009-09-09 01:10:507
8#include <string>
9
[email protected]d069c11a2013-04-13 00:01:5510#include "base/strings/string_piece.h"
ttuttlecf1158bf2016-03-18 16:37:4411#include "base/time/time.h"
Eric Orth192e3bb2018-11-14 19:30:3212#include "net/base/address_family.h"
[email protected]172da1b2011-08-12 15:52:2613#include "net/base/net_export.h"
ttuttlecf1158bf2016-03-18 16:37:4414#include "net/base/network_change_notifier.h"
Eric Orth192e3bb2018-11-14 19:30:3215#include "net/dns/public/dns_query_type.h"
[email protected]b3ccac82010-08-04 15:54:4016
[email protected]c36f0642009-09-09 01:10:5017namespace net {
18
juliatuttle317860e2016-05-12 14:47:2219class AddressList;
20
[email protected]c36f0642009-09-09 01:10:5021// 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 Wangc2213d5d2018-11-02 01:28:1226NET_EXPORT bool DNSDomainFromDot(const base::StringPiece& dotted,
27 std::string* out);
[email protected]c36f0642009-09-09 01:10:5028
mmenked6e9161aff2016-09-09 21:55:4429// Checks that a hostname is valid. Simple wrapper around DNSDomainFromDot.
30NET_EXPORT_PRIVATE bool IsValidDNSDomain(const base::StringPiece& dotted);
31
palmer6170ab92017-03-23 20:57:5432// 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.
45NET_EXPORT_PRIVATE bool IsValidHostLabelCharacter(char c, bool is_first_char);
46
[email protected]d0d49dd82012-01-26 00:03:5947// DNSDomainToString converts a domain in DNS format to a dotted string.
48// Excludes the dot at the end.
Qingsi Wangc2213d5d2018-11-02 01:28:1249NET_EXPORT std::string DNSDomainToString(const base::StringPiece& domain);
[email protected]f060be32011-02-17 17:20:2850
dalykb34631f2018-08-24 19:59:4851// Return the expanded template when no variables have corresponding values.
52NET_EXPORT_PRIVATE std::string GetURLFromTemplateWithoutParameters(
53 const std::string& server_template);
54
ttuttlecf1158bf2016-03-18 16:37:4455#if !defined(OS_NACL)
56NET_EXPORT_PRIVATE
57base::TimeDelta GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
58 const char* field_trial_name,
59 base::TimeDelta default_delta,
60 NetworkChangeNotifier::ConnectionType connection_type);
61#endif // !defined(OS_NACL)
62
juliatuttle317860e2016-05-12 14:47:2263// How similar or different two AddressLists are (see values for details).
64// Used in histograms; do not modify existing values.
65enum 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.)
Wez115d4312018-02-17 04:05:1979NET_EXPORT
juliatuttle317860e2016-05-12 14:47:2280AddressListDeltaType FindAddressListDeltaType(const AddressList& a,
81 const AddressList& b);
82
Qingsi Wangc2213d5d2018-11-02 01:28:1283// 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.
88NET_EXPORT std::string CreateNamePointer(uint16_t offset);
89
Eric Orth192e3bb2018-11-14 19:30:3290// Convert a DnsQueryType enum to the wire format integer representation.
91uint16_t DnsQueryTypeToQtype(DnsQueryType dns_query_type);
92
93NET_EXPORT DnsQueryType
94AddressFamilyToDnsQueryType(AddressFamily address_family);
95
[email protected]c36f0642009-09-09 01:10:5096} // namespace net
97
tfarina77021d62015-10-11 20:19:0398#endif // NET_DNS_DNS_UTIL_H_