Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / dns / DNS.h
blob64dd7a7336014f6fdc9c024ee362d6d138854802
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DNS_h_
8 #define DNS_h_
10 #include "nscore.h"
11 #include "nsString.h"
12 #include "prio.h"
13 #include "prnetdb.h"
14 #include "nsISupportsImpl.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "nsTArray.h"
18 #if !defined(XP_WIN)
19 # include <arpa/inet.h>
20 #endif
22 #ifdef XP_WIN
23 # include "winsock2.h"
24 #endif
26 #ifndef AF_LOCAL
27 # define AF_LOCAL 1 // used for named pipe
28 #endif
30 #define IPv6ADDR_IS_LOOPBACK(a) \
31 (((a)->u32[0] == 0) && ((a)->u32[1] == 0) && ((a)->u32[2] == 0) && \
32 ((a)->u8[12] == 0) && ((a)->u8[13] == 0) && ((a)->u8[14] == 0) && \
33 ((a)->u8[15] == 0x1U))
35 #define IPv6ADDR_IS_V4MAPPED(a) \
36 (((a)->u32[0] == 0) && ((a)->u32[1] == 0) && ((a)->u8[8] == 0) && \
37 ((a)->u8[9] == 0) && ((a)->u8[10] == 0xff) && ((a)->u8[11] == 0xff))
39 #define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3])
41 #define IPv6ADDR_IS_UNSPECIFIED(a) \
42 (((a)->u32[0] == 0) && ((a)->u32[1] == 0) && ((a)->u32[2] == 0) && \
43 ((a)->u32[3] == 0))
45 namespace mozilla {
46 namespace net {
48 // IMPORTANT: when adding new values, always add them to the end, otherwise
49 // it will mess up telemetry.
50 // Stage_0: Receive the record before the http transaction is created.
51 // Stage_1: Receive the record after the http transaction is created and the
52 // transaction is not dispatched.
53 // Stage_2: Receive the record after the http transaction is dispatched.
54 enum HTTPSSVC_RECEIVED_STAGE : uint32_t {
55 HTTPSSVC_NOT_PRESENT = 0,
56 HTTPSSVC_WITH_IPHINT_RECEIVED_STAGE_0 = 1,
57 HTTPSSVC_WITHOUT_IPHINT_RECEIVED_STAGE_0 = 2,
58 HTTPSSVC_WITH_IPHINT_RECEIVED_STAGE_1 = 3,
59 HTTPSSVC_WITHOUT_IPHINT_RECEIVED_STAGE_1 = 4,
60 HTTPSSVC_WITH_IPHINT_RECEIVED_STAGE_2 = 5,
61 HTTPSSVC_WITHOUT_IPHINT_RECEIVED_STAGE_2 = 6,
62 HTTPSSVC_NOT_USED = 7,
63 HTTPSSVC_NO_USABLE_RECORD = 8,
66 #define HTTPS_RR_IS_USED(s) \
67 (s > HTTPSSVC_NOT_PRESENT && s < HTTPSSVC_WITH_IPHINT_RECEIVED_STAGE_2)
69 // Required buffer size for text form of an IP address.
70 // Includes space for null termination. We make our own contants
71 // because we don't want higher-level code depending on things
72 // like INET6_ADDRSTRLEN and having to include the associated
73 // platform-specific headers.
74 #ifdef XP_WIN
75 // Windows requires longer buffers for some reason.
76 const int kIPv4CStrBufSize = 22;
77 const int kIPv6CStrBufSize = 65;
78 const int kNetAddrMaxCStrBufSize = kIPv6CStrBufSize;
79 #else
80 const int kIPv4CStrBufSize = 16;
81 const int kIPv6CStrBufSize = 46;
82 const int kLocalCStrBufSize = 108;
83 const int kNetAddrMaxCStrBufSize = kLocalCStrBufSize;
84 #endif
86 // This was all created at a time in which we were using NSPR for host
87 // resolution and we were propagating NSPR types like "PRAddrInfo" and
88 // "PRNetAddr" all over Gecko. This made it hard to use another host
89 // resolver -- we were locked into NSPR. The goal here is to get away
90 // from that. We'll translate what we get from NSPR or any other host
91 // resolution library into the types below and use them in Gecko.
93 union IPv6Addr {
94 uint8_t u8[16];
95 uint16_t u16[8];
96 uint32_t u32[4];
97 uint64_t u64[2];
100 // This struct is similar to operating system structs like "sockaddr", used for
101 // things like "connect" and "getsockname". When tempted to cast or do dumb
102 // copies of this struct to another struct, bear compiler-computed padding
103 // in mind. The size of this struct, and the layout of the data in it, may
104 // not be what you expect.
105 union NetAddr {
106 struct {
107 uint16_t family; /* address family (0x00ff maskable) */
108 char data[14]; /* raw address data */
109 } raw{};
110 struct {
111 uint16_t family; /* address family (AF_INET) */
112 uint16_t port; /* port number */
113 uint32_t ip; /* The actual 32 bits of address */
114 } inet;
115 struct {
116 uint16_t family; /* address family (AF_INET6) */
117 uint16_t port; /* port number */
118 uint32_t flowinfo; /* routing information */
119 IPv6Addr ip; /* the actual 128 bits of address */
120 uint32_t scope_id; /* set of interfaces for a scope */
121 } inet6;
122 #if defined(XP_UNIX) || defined(XP_WIN)
123 struct { /* Unix domain socket or
124 Windows Named Pipes address */
125 uint16_t family; /* address family (AF_UNIX) */
126 char path[104]; /* null-terminated pathname */
127 } local;
128 #endif
129 // introduced to support nsTArray<NetAddr> comparisons and sorting
130 bool operator==(const NetAddr& other) const;
131 bool operator<(const NetAddr& other) const;
133 inline NetAddr& operator=(const NetAddr& other) {
134 memcpy(this, &other, sizeof(NetAddr));
135 return *this;
138 NetAddr() { memset(this, 0, sizeof(NetAddr)); }
139 explicit NetAddr(const PRNetAddr* prAddr);
141 // Will parse aString into a NetAddr using PR_StringToNetAddr.
142 // Returns an error code if parsing fails.
143 // If aPort is non-0 will set the NetAddr's port to (the network endian
144 // value of) that.
145 nsresult InitFromString(const nsACString& aString, uint16_t aPort = 0);
147 bool IsIPAddrAny() const;
148 bool IsLoopbackAddr() const;
149 bool IsLoopBackAddressWithoutIPv6Mapping() const;
150 bool IsIPAddrV4() const;
151 bool IsIPAddrV4Mapped() const;
152 bool IsIPAddrLocal() const;
153 bool IsIPAddrShared() const;
154 nsresult GetPort(uint16_t* aResult) const;
155 bool ToStringBuffer(char* buf, uint32_t bufSize) const;
156 nsCString ToString() const;
159 enum class DNSResolverType : uint32_t { Native = 0, TRR };
161 class AddrInfo {
162 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AddrInfo)
164 public:
165 static const uint32_t NO_TTL_DATA = (uint32_t)-1;
167 // Creates an AddrInfo object.
168 explicit AddrInfo(const nsACString& host, const PRAddrInfo* prAddrInfo,
169 bool disableIPv4, bool filterNameCollision,
170 const nsACString& cname);
172 // Creates a basic AddrInfo object (initialize only the host, cname and TRR
173 // type).
174 explicit AddrInfo(const nsACString& host, const nsACString& cname,
175 DNSResolverType aResolverType, unsigned int aTRRType,
176 nsTArray<NetAddr>&& addresses);
178 // Creates a basic AddrInfo object (initialize only the host and TRR status).
179 explicit AddrInfo(const nsACString& host, DNSResolverType aResolverType,
180 unsigned int aTRRType, nsTArray<NetAddr>&& addresses,
181 uint32_t aTTL = NO_TTL_DATA);
183 explicit AddrInfo(const AddrInfo* src); // copy
185 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
187 bool IsTRR() const { return mResolverType == DNSResolverType::TRR; }
188 DNSResolverType ResolverType() const { return mResolverType; }
189 unsigned int TRRType() { return mTRRType; }
191 double GetTrrFetchDuration() { return mTrrFetchDuration; }
192 double GetTrrFetchDurationNetworkOnly() {
193 return mTrrFetchDurationNetworkOnly;
196 const nsTArray<NetAddr>& Addresses() { return mAddresses; }
197 const nsCString& Hostname() { return mHostName; }
198 const nsCString& CanonicalHostname() { return mCanonicalName; }
199 uint32_t TTL() { return ttl; }
201 class MOZ_STACK_CLASS AddrInfoBuilder {
202 public:
203 explicit AddrInfoBuilder(AddrInfo* aInfo) {
204 mInfo = new AddrInfo(aInfo); // Clone it
207 void SetTrrFetchDurationNetworkOnly(double aTime) {
208 mInfo->mTrrFetchDurationNetworkOnly = aTime;
211 void SetTrrFetchDuration(double aTime) { mInfo->mTrrFetchDuration = aTime; }
213 void SetTTL(uint32_t aTTL) { mInfo->ttl = aTTL; }
215 void SetAddresses(nsTArray<NetAddr>&& addresses) {
216 mInfo->mAddresses = std::move(addresses);
219 template <class Comparator>
220 void SortAddresses(const Comparator& aComp) {
221 mInfo->mAddresses.Sort(aComp);
224 void SetCanonicalHostname(const nsACString& aCname) {
225 mInfo->mCanonicalName = aCname;
228 already_AddRefed<AddrInfo> Finish() { return mInfo.forget(); }
230 private:
231 RefPtr<AddrInfo> mInfo;
234 AddrInfoBuilder Build() { return AddrInfoBuilder(this); }
236 private:
237 ~AddrInfo();
238 uint32_t ttl = NO_TTL_DATA;
240 nsCString mHostName;
241 nsCString mCanonicalName;
242 DNSResolverType mResolverType = DNSResolverType::Native;
243 unsigned int mTRRType = 0;
244 double mTrrFetchDuration = 0;
245 double mTrrFetchDurationNetworkOnly = 0;
247 nsTArray<NetAddr> mAddresses;
250 // Copies the contents of a PRNetAddr to a NetAddr.
251 // Does not do a ptr safety check!
252 void PRNetAddrToNetAddr(const PRNetAddr* prAddr, NetAddr* addr);
254 // Copies the contents of a NetAddr to a PRNetAddr.
255 // Does not do a ptr safety check!
256 void NetAddrToPRNetAddr(const NetAddr* addr, PRNetAddr* prAddr);
258 bool IsLoopbackHostname(const nsACString& aAsciiHost);
260 bool HostIsIPLiteral(const nsACString& aAsciiHost);
262 } // namespace net
263 } // namespace mozilla
265 #endif // DNS_h_