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/. */
6 
7 #ifndef nsDNSService2_h__
8 #define nsDNSService2_h__
9 
10 #include "nsPIDNSService.h"
11 #include "nsIIDNService.h"
12 #include "nsIMemoryReporter.h"
13 #include "nsIObserver.h"
14 #include "nsHostResolver.h"
15 #include "nsString.h"
16 #include "nsTHashtable.h"
17 #include "nsHashKeys.h"
18 #include "mozilla/Mutex.h"
19 #include "mozilla/Attributes.h"
20 #include "TRRService.h"
21 
22 class nsAuthSSPI;
23 
24 class nsDNSService final : public nsPIDNSService,
25                            public nsIObserver,
26                            public nsIMemoryReporter {
27  public:
28   NS_DECL_THREADSAFE_ISUPPORTS
29   NS_DECL_NSPIDNSSERVICE
30   NS_DECL_NSIDNSSERVICE
31   NS_DECL_NSIOBSERVER
32   NS_DECL_NSIMEMORYREPORTER
33 
34   nsDNSService();
35 
36   static already_AddRefed<nsIDNSService> GetXPCOMSingleton();
37 
38   size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
39 
40   bool GetOffline() const;
41 
42  protected:
43   friend class nsAuthSSPI;
44 
45   nsresult DeprecatedSyncResolve(
46       const nsACString& aHostname, uint32_t flags,
47       const mozilla::OriginAttributes& aOriginAttributes,
48       nsIDNSRecord** result);
49 
50  private:
51   ~nsDNSService();
52 
53   nsresult ReadPrefs(const char* name);
54   static already_AddRefed<nsDNSService> GetSingleton();
55 
56   uint16_t GetAFForLookup(const nsACString& host, uint32_t flags);
57 
58   nsresult PreprocessHostname(bool aLocalDomain, const nsACString& aInput,
59                               nsIIDNService* aIDN, nsACString& aACE);
60 
61   nsresult AsyncResolveInternal(
62       const nsACString& aHostname, const nsACString& aTrrServer, uint16_t type,
63       uint32_t flags, nsIDNSListener* aListener, nsIEventTarget* target_,
64       const mozilla::OriginAttributes& aOriginAttributes,
65       nsICancelable** result);
66 
67   nsresult CancelAsyncResolveInternal(
68       const nsACString& aHostname, const nsACString& aTrrServer, uint16_t aType,
69       uint32_t aFlags, nsIDNSListener* aListener, nsresult aReason,
70       const mozilla::OriginAttributes& aOriginAttributes);
71 
72   nsresult ResolveInternal(const nsACString& aHostname, uint32_t flags,
73                            const mozilla::OriginAttributes& aOriginAttributes,
74                            nsIDNSRecord** result);
75 
76   // Locks the mutex and returns an addreffed resolver. May return null.
77   already_AddRefed<nsHostResolver> GetResolverLocked();
78 
79   RefPtr<nsHostResolver> mResolver;
80   nsCOMPtr<nsIIDNService> mIDN;
81 
82   // mLock protects access to mResolver, mLocalDomains and mIPv4OnlyDomains
83   mozilla::Mutex mLock;
84 
85   // mIPv4OnlyDomains is a comma-separated list of domains for which only
86   // IPv4 DNS lookups are performed. This allows the user to disable IPv6 on
87   // a per-domain basis and work around broken DNS servers. See bug 68796.
88   nsCString mIPv4OnlyDomains;
89   nsCString mForceResolve;
90   bool mDisableIPv6;
91   bool mDisablePrefetch;
92   bool mBlockDotOnion;
93   bool mNotifyResolution;
94   bool mOfflineLocalhost;
95   bool mForceResolveOn;
96   uint32_t mProxyType;
97   nsTHashtable<nsCStringHashKey> mLocalDomains;
98   RefPtr<mozilla::net::TRRService> mTrrService;
99 
100   uint32_t mResCacheEntries;
101   uint32_t mResCacheExpiration;
102   uint32_t mResCacheGrace;
103   bool mResolverPrefsUpdated;
104 };
105 
106 #endif  // nsDNSService2_h__
107