xref: /aosp_15_r20/external/cronet/net/dns/dns_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 <cstdint>
9 #include <string>
10 #include <vector>
11 
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/public/dns_over_https_server_config.h"
18 #include "net/dns/public/dns_query_type.h"
19 #include "net/dns/public/secure_dns_mode.h"
20 
21 namespace net {
22 
23 // Return the expanded template when no variables have corresponding values.
24 NET_EXPORT_PRIVATE std::string GetURLFromTemplateWithoutParameters(
25     const std::string& server_template);
26 
27 NET_EXPORT_PRIVATE
28 base::TimeDelta GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
29     const char* field_trial_name,
30     base::TimeDelta default_delta,
31     NetworkChangeNotifier::ConnectionType connection_type);
32 
33 // Creates a 2-byte string that represents the name pointer defined in Section
34 // 4.1.1 of RFC 1035 for the given offset. The first two bits in the first byte
35 // of the name pointer are ones, and the rest 14 bits are given to `offset`,
36 // which specifies an offset from the start of the message for the pointed name.
37 // Note that `offset` must be less than 2^14 - 1 by definition.
38 NET_EXPORT std::string CreateNamePointer(uint16_t offset);
39 
40 // Convert a DnsQueryType enum to the wire format integer representation.
41 NET_EXPORT_PRIVATE uint16_t DnsQueryTypeToQtype(DnsQueryType dns_query_type);
42 
43 NET_EXPORT DnsQueryType
44 AddressFamilyToDnsQueryType(AddressFamily address_family);
45 
46 // Uses the hardcoded upgrade mapping to discover DoH service(s) associated with
47 // a DoT hostname. Providers with a disabled `base::Feature` are not eligible
48 // for upgrade.
49 NET_EXPORT_PRIVATE std::vector<DnsOverHttpsServerConfig>
50 GetDohUpgradeServersFromDotHostname(const std::string& dot_server);
51 
52 // Uses the hardcoded upgrade mapping to discover DoH service(s) associated with
53 // a list of insecure DNS servers. Server ordering is preserved across the
54 // mapping. Providers with a disabled `base::Feature` are not eligible for
55 // upgrade.
56 NET_EXPORT_PRIVATE std::vector<DnsOverHttpsServerConfig>
57 GetDohUpgradeServersFromNameservers(const std::vector<IPEndPoint>& dns_servers);
58 
59 // Returns the provider id to use in UMA histogram names. If there is no
60 // provider id that matches `doh_server`, returns "Other".
61 NET_EXPORT_PRIVATE std::string GetDohProviderIdForHistogramFromServerConfig(
62     const DnsOverHttpsServerConfig& doh_server);
63 
64 // Returns the provider id to use in UMA histogram names. If there is no
65 // provider id that matches `nameserver`, returns "Other".
66 NET_EXPORT_PRIVATE std::string GetDohProviderIdForHistogramFromNameserver(
67     const IPEndPoint& nameserver);
68 
69 NET_EXPORT_PRIVATE std::string SecureDnsModeToString(
70     const SecureDnsMode secure_dns_mode);
71 
72 }  // namespace net
73 
74 #endif  // NET_DNS_DNS_UTIL_H_
75