nsDNSService2.h (4505B)
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 "DNSServiceBase.h" 11 #include "nsClassHashtable.h" 12 #include "nsPIDNSService.h" 13 #include "nsIMemoryReporter.h" 14 #include "nsIObserver.h" 15 #include "nsHostResolver.h" 16 #include "nsString.h" 17 #include "nsTHashSet.h" 18 #include "nsHashKeys.h" 19 #include "mozilla/Atomics.h" 20 #include "mozilla/Mutex.h" 21 #include "TRRService.h" 22 23 class nsAuthSSPI; 24 25 class DNSServiceWrapper final : public nsPIDNSService { 26 public: 27 NS_DECL_THREADSAFE_ISUPPORTS 28 NS_FORWARD_NSPIDNSSERVICE(PIDNSService()->) 29 NS_FORWARD_NSIDNSSERVICE(DNSService()->) 30 31 DNSServiceWrapper() = default; 32 33 static already_AddRefed<nsIDNSService> GetSingleton(); 34 static void SwitchToBackupDNSService(); 35 36 private: 37 ~DNSServiceWrapper() = default; 38 nsIDNSService* DNSService(); 39 nsPIDNSService* PIDNSService(); 40 41 mozilla::Mutex mLock{"DNSServiceWrapper.mLock"}; 42 nsCOMPtr<nsIDNSService> mDNSServiceInUse MOZ_GUARDED_BY(mLock); 43 nsCOMPtr<nsIDNSService> mBackupDNSService; 44 }; 45 46 class nsDNSService final : public mozilla::net::DNSServiceBase, 47 public nsPIDNSService, 48 public nsIMemoryReporter { 49 public: 50 NS_DECL_ISUPPORTS_INHERITED 51 NS_DECL_NSPIDNSSERVICE 52 NS_DECL_NSIDNSSERVICE 53 NS_DECL_NSIOBSERVER 54 NS_DECL_NSIMEMORYREPORTER 55 56 nsDNSService() = default; 57 58 static already_AddRefed<nsIDNSService> GetXPCOMSingleton(); 59 60 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); 61 62 bool GetOffline() const; 63 64 protected: 65 friend class nsAuthSSPI; 66 friend class DNSServiceWrapper; 67 68 nsresult DeprecatedSyncResolve( 69 const nsACString& aHostname, nsIDNSService::DNSFlags flags, 70 const mozilla::OriginAttributes& aOriginAttributes, 71 nsIDNSRecord** result); 72 73 private: 74 ~nsDNSService() = default; 75 76 void ReadPrefs(const char* name) override; 77 static already_AddRefed<nsDNSService> GetSingleton(); 78 79 uint16_t GetAFForLookup(const nsACString& host, 80 nsIDNSService::DNSFlags flags); 81 82 nsresult PreprocessHostname(bool aLocalDomain, const nsACString& aInput, 83 nsACString& aACE); 84 85 bool IsLocalDomain(const nsACString& aHostname) const MOZ_REQUIRES(mLock); 86 87 nsresult AsyncResolveInternal( 88 const nsACString& aHostname, uint16_t type, nsIDNSService::DNSFlags flags, 89 nsIDNSAdditionalInfo* aInfo, nsIDNSListener* aListener, 90 nsIEventTarget* target_, 91 const mozilla::OriginAttributes& aOriginAttributes, 92 nsICancelable** result); 93 94 nsresult CancelAsyncResolveInternal( 95 const nsACString& aHostname, uint16_t aType, 96 nsIDNSService::DNSFlags aFlags, nsIDNSAdditionalInfo* aInfo, 97 nsIDNSListener* aListener, nsresult aReason, 98 const mozilla::OriginAttributes& aOriginAttributes); 99 100 nsresult ResolveInternal(const nsACString& aHostname, 101 nsIDNSService::DNSFlags flags, 102 const mozilla::OriginAttributes& aOriginAttributes, 103 nsIDNSRecord** result); 104 105 // Locks the mutex and returns an addreffed resolver. May return null. 106 already_AddRefed<nsHostResolver> GetResolverLocked(); 107 108 RefPtr<nsHostResolver> mResolver MOZ_GUARDED_BY(mLock); 109 110 // mLock protects access to mResolver, mLocalDomains, mIPv4OnlyDomains, 111 // mFailedSVCDomainNames, and mMockHTTPSRRDomain. 112 mozilla::Mutex mLock{"nsDNSServer.mLock"}; 113 114 // mIPv4OnlyDomains is a comma-separated list of domains for which only 115 // IPv4 DNS lookups are performed. This allows the user to disable IPv6 on 116 // a per-domain basis and work around broken DNS servers. See bug 68796. 117 nsCString mIPv4OnlyDomains MOZ_GUARDED_BY(mLock); 118 nsCString mForceResolve; 119 nsCString mMockHTTPSRRDomain MOZ_GUARDED_BY(mLock); 120 mozilla::Atomic<bool, mozilla::Relaxed> mHasMockHTTPSRRDomainSet{false}; 121 bool mNotifyResolution = false; 122 bool mForceResolveOn = false; 123 nsTHashSet<nsCString> mLocalDomains MOZ_GUARDED_BY(mLock); 124 RefPtr<mozilla::net::TRRService> mTrrService; 125 126 nsClassHashtable<nsCStringHashKey, nsTArray<nsCString>> mFailedSVCDomainNames 127 MOZ_GUARDED_BY(mLock); 128 ; 129 }; 130 131 already_AddRefed<nsIDNSService> GetOrInitDNSService(); 132 133 #endif // nsDNSService2_h__