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