1 // Copyright 2018 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 #include "net/dns/public/util.h"
6
7 #include <stdint.h>
8
9 #include <string_view>
10
11 #include "base/check.h"
12 #include "base/notreached.h"
13 #include "base/strings/strcat.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "build/build_config.h"
16 #include "net/base/ip_address.h"
17 #include "net/dns/public/dns_protocol.h"
18 #include "url/scheme_host_port.h"
19 #include "url/url_constants.h"
20
21 namespace net {
22
23 namespace {
24
GetMdnsIPEndPoint(const char * address)25 IPEndPoint GetMdnsIPEndPoint(const char* address) {
26 IPAddress multicast_group_number;
27 bool success = multicast_group_number.AssignFromIPLiteral(address);
28 DCHECK(success);
29 return IPEndPoint(multicast_group_number,
30 dns_protocol::kDefaultPortMulticast);
31 }
32
33 } // namespace
34
35 namespace dns_util {
36
GetMdnsGroupEndPoint(AddressFamily address_family)37 IPEndPoint GetMdnsGroupEndPoint(AddressFamily address_family) {
38 switch (address_family) {
39 case ADDRESS_FAMILY_IPV4:
40 return GetMdnsIPEndPoint(dns_protocol::kMdnsMulticastGroupIPv4);
41 case ADDRESS_FAMILY_IPV6:
42 return GetMdnsIPEndPoint(dns_protocol::kMdnsMulticastGroupIPv6);
43 default:
44 NOTREACHED();
45 return IPEndPoint();
46 }
47 }
48
GetMdnsReceiveEndPoint(AddressFamily address_family)49 IPEndPoint GetMdnsReceiveEndPoint(AddressFamily address_family) {
50 // TODO(qingsi): MacOS should follow other POSIX platforms in the else-branch
51 // after addressing crbug.com/899310. We have encountered a conflicting issue on
52 // CrOS as described in crbug.com/931916, and the following is a temporary
53 // mitigation to reconcile the two issues. Remove this after closing
54 // crbug.com/899310.
55 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE)
56 // With Windows, binding to a mulitcast group address is not allowed.
57 // Multicast messages will be received appropriate to the multicast groups the
58 // socket has joined. Sockets intending to receive multicast messages should
59 // bind to a wildcard address (e.g. 0.0.0.0).
60 switch (address_family) {
61 case ADDRESS_FAMILY_IPV4:
62 return IPEndPoint(IPAddress::IPv4AllZeros(),
63 dns_protocol::kDefaultPortMulticast);
64 case ADDRESS_FAMILY_IPV6:
65 return IPEndPoint(IPAddress::IPv6AllZeros(),
66 dns_protocol::kDefaultPortMulticast);
67 default:
68 NOTREACHED();
69 return IPEndPoint();
70 }
71 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
72 // With POSIX/Fuchsia, any socket can receive messages for multicast groups
73 // joined by any socket on the system. Sockets intending to receive messages
74 // for a specific multicast group should bind to that group address.
75 return GetMdnsGroupEndPoint(address_family);
76 #else
77 #error Platform not supported.
78 #endif
79 }
80
GetNameForHttpsQuery(const url::SchemeHostPort & scheme_host_port,uint16_t * out_port)81 std::string GetNameForHttpsQuery(const url::SchemeHostPort& scheme_host_port,
82 uint16_t* out_port) {
83 DCHECK(!scheme_host_port.host().empty() &&
84 scheme_host_port.host().front() != '.');
85
86 // Normalize ws/wss schemes to http/https. Note that this behavior is not
87 // indicated by the draft-ietf-dnsop-svcb-https-08 spec.
88 std::string_view normalized_scheme = scheme_host_port.scheme();
89 if (normalized_scheme == url::kWsScheme) {
90 normalized_scheme = url::kHttpScheme;
91 } else if (normalized_scheme == url::kWssScheme) {
92 normalized_scheme = url::kHttpsScheme;
93 }
94
95 // For http-schemed hosts, request the corresponding upgraded https host
96 // per the rules in draft-ietf-dnsop-svcb-https-08, Section 9.5.
97 uint16_t port = scheme_host_port.port();
98 if (normalized_scheme == url::kHttpScheme) {
99 normalized_scheme = url::kHttpsScheme;
100 if (port == 80)
101 port = 443;
102 }
103
104 // Scheme should always end up normalized to "https" to create HTTPS
105 // transactions.
106 DCHECK_EQ(normalized_scheme, url::kHttpsScheme);
107
108 if (out_port != nullptr)
109 *out_port = port;
110
111 // Per the rules in draft-ietf-dnsop-svcb-https-08, Section 9.1 and 2.3,
112 // encode scheme and port in the transaction hostname, unless the port is
113 // the default 443.
114 if (port == 443)
115 return scheme_host_port.host();
116 return base::StrCat({"_", base::NumberToString(scheme_host_port.port()),
117 "._https.", scheme_host_port.host()});
118 }
119
120 } // namespace dns_util
121 } // namespace net
122