1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et 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 nsDNSService2_h__
8 #define nsDNSService2_h__
10 #include "DNSServiceBase.h"
11 #include "nsClassHashtable.h"
12 #include "nsPIDNSService.h"
13 #include "nsIMemoryReporter.h"
14 #include "nsIObserver.h"
15 #include "nsHostResolver.h"
17 #include "nsTHashSet.h"
18 #include "nsHashKeys.h"
19 #include "mozilla/Atomics.h"
20 #include "mozilla/Mutex.h"
21 #include "mozilla/Attributes.h"
22 #include "TRRService.h"
26 class DNSServiceWrapper final
: public nsPIDNSService
{
28 NS_DECL_THREADSAFE_ISUPPORTS
29 NS_FORWARD_NSPIDNSSERVICE(PIDNSService()->)
30 NS_FORWARD_NSIDNSSERVICE(DNSService()->)
32 DNSServiceWrapper() = default;
34 static already_AddRefed
<nsIDNSService
> GetSingleton();
35 static void SwitchToBackupDNSService();
38 ~DNSServiceWrapper() = default;
39 nsIDNSService
* DNSService();
40 nsPIDNSService
* PIDNSService();
42 mozilla::Mutex mLock
{"DNSServiceWrapper.mLock"};
43 nsCOMPtr
<nsIDNSService
> mDNSServiceInUse
;
44 nsCOMPtr
<nsIDNSService
> mBackupDNSService
;
47 class nsDNSService final
: public mozilla::net::DNSServiceBase
,
48 public nsPIDNSService
,
49 public nsIMemoryReporter
{
51 NS_DECL_ISUPPORTS_INHERITED
52 NS_DECL_NSPIDNSSERVICE
55 NS_DECL_NSIMEMORYREPORTER
57 nsDNSService() = default;
59 static already_AddRefed
<nsIDNSService
> GetXPCOMSingleton();
61 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
63 bool GetOffline() const;
66 friend class nsAuthSSPI
;
67 friend class DNSServiceWrapper
;
69 nsresult
DeprecatedSyncResolve(
70 const nsACString
& aHostname
, nsIDNSService::DNSFlags flags
,
71 const mozilla::OriginAttributes
& aOriginAttributes
,
72 nsIDNSRecord
** result
);
75 ~nsDNSService() = default;
77 void ReadPrefs(const char* name
) override
;
78 static already_AddRefed
<nsDNSService
> GetSingleton();
80 uint16_t GetAFForLookup(const nsACString
& host
,
81 nsIDNSService::DNSFlags flags
);
83 nsresult
PreprocessHostname(bool aLocalDomain
, const nsACString
& aInput
,
86 bool IsLocalDomain(const nsACString
& aHostname
) const;
88 nsresult
AsyncResolveInternal(
89 const nsACString
& aHostname
, uint16_t type
, nsIDNSService::DNSFlags flags
,
90 nsIDNSAdditionalInfo
* aInfo
, nsIDNSListener
* aListener
,
91 nsIEventTarget
* target_
,
92 const mozilla::OriginAttributes
& aOriginAttributes
,
93 nsICancelable
** result
);
95 nsresult
CancelAsyncResolveInternal(
96 const nsACString
& aHostname
, uint16_t aType
,
97 nsIDNSService::DNSFlags aFlags
, nsIDNSAdditionalInfo
* aInfo
,
98 nsIDNSListener
* aListener
, nsresult aReason
,
99 const mozilla::OriginAttributes
& aOriginAttributes
);
101 nsresult
ResolveInternal(const nsACString
& aHostname
,
102 nsIDNSService::DNSFlags flags
,
103 const mozilla::OriginAttributes
& aOriginAttributes
,
104 nsIDNSRecord
** result
);
106 // Locks the mutex and returns an addreffed resolver. May return null.
107 already_AddRefed
<nsHostResolver
> GetResolverLocked();
109 RefPtr
<nsHostResolver
> mResolver
;
111 // mLock protects access to mResolver, mLocalDomains, mIPv4OnlyDomains,
112 // mFailedSVCDomainNames, and mMockHTTPSRRDomain.
113 mozilla::Mutex mLock MOZ_UNANNOTATED
{"nsDNSServer.mLock"};
115 // mIPv4OnlyDomains is a comma-separated list of domains for which only
116 // IPv4 DNS lookups are performed. This allows the user to disable IPv6 on
117 // a per-domain basis and work around broken DNS servers. See bug 68796.
118 nsCString mIPv4OnlyDomains
;
119 nsCString mForceResolve
;
120 nsCString mMockHTTPSRRDomain
;
121 mozilla::Atomic
<bool, mozilla::Relaxed
> mHasMockHTTPSRRDomainSet
{false};
122 bool mNotifyResolution
= false;
123 bool mForceResolveOn
= false;
124 nsTHashSet
<nsCString
> mLocalDomains
;
125 RefPtr
<mozilla::net::TRRService
> mTrrService
;
127 nsClassHashtable
<nsCStringHashKey
, nsTArray
<nsCString
>> mFailedSVCDomainNames
;
130 already_AddRefed
<nsIDNSService
> GetOrInitDNSService();
132 #endif // nsDNSService2_h__