GetAddrInfo.h (4139B)
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/. */ 6 7 #ifndef netwerk_dns_GetAddrInfo_h 8 #define netwerk_dns_GetAddrInfo_h 9 10 #include "nsError.h" 11 #include "nscore.h" 12 #include "nsINativeDNSResolverOverride.h" 13 #include "nsHashKeys.h" 14 #include "nsTHashMap.h" 15 #include "mozilla/RWLock.h" 16 #include "nsTArray.h" 17 #include "prio.h" 18 #include "mozilla/net/DNS.h" 19 #include "nsIDNSByTypeRecord.h" 20 #include "mozilla/Logging.h" 21 #include "nsIDNSService.h" 22 23 #if defined(XP_WIN) 24 # define DNSQUERY_AVAILABLE 1 25 #else 26 # undef DNSQUERY_AVAILABLE 27 #endif 28 29 namespace mozilla { 30 namespace net { 31 32 extern LazyLogModule gGetAddrInfoLog; 33 class AddrInfo; 34 class DNSPacket; 35 36 /** 37 * Look up a host by name. Mostly equivalent to getaddrinfo(host, NULL, ...) of 38 * RFC 3493. 39 * 40 * @param aHost[in] Character string defining the host name of interest 41 * @param aAddressFamily[in] May be AF_INET, AF_INET6, or AF_UNSPEC. 42 * @param aFlags[in] See nsIDNSService::DNSFlags 43 * @param aAddrInfo[out] Will point to the results of the host lookup, or be 44 * null if the lookup failed. 45 * @param aGetTtl[in] If true, the TTL will be retrieved if DNS provides the 46 * answers.. 47 */ 48 nsresult GetAddrInfo(const nsACString& aHost, uint16_t aAddressFamily, 49 nsIDNSService::DNSFlags aFlags, AddrInfo** aAddrInfo, 50 bool aGetTtl); 51 52 /** 53 * Initialize the GetAddrInfo module. 54 * 55 * GetAddrInfoShutdown() should be called for every time this function is 56 * called. 57 */ 58 nsresult GetAddrInfoInit(); 59 60 /** 61 * Shutdown the GetAddrInfo module. 62 * 63 * This function should be called for every time GetAddrInfoInit() is called. 64 * An assertion may throw (but is not guarenteed) if this function is called 65 * too many times. 66 */ 67 nsresult GetAddrInfoShutdown(); 68 69 void DNSThreadShutdown(); 70 71 /** 72 * Resolves a HTTPS record. Will check overrides before calling the 73 * native OS implementation. 74 */ 75 nsresult ResolveHTTPSRecord(const nsACString& aHost, 76 nsIDNSService::DNSFlags aFlags, 77 TypeRecordResultType& aResult, uint32_t& aTTL); 78 79 /** 80 * The platform specific implementation of HTTPS resolution. 81 */ 82 nsresult ResolveHTTPSRecordImpl(const nsACString& aHost, 83 nsIDNSService::DNSFlags aFlags, 84 TypeRecordResultType& aResult, uint32_t& aTTL); 85 86 nsresult ParseHTTPSRecord(nsCString& aHost, DNSPacket& aDNSPacket, 87 TypeRecordResultType& aResult, uint32_t& aTTL); 88 89 // Use the provided aHost to create a mock HTTPS record. 90 nsresult CreateAndResolveMockHTTPSRecord(const nsACString& aHost, 91 nsIDNSService::DNSFlags aFlags, 92 TypeRecordResultType& aResult, 93 uint32_t& aTTL); 94 95 class NativeDNSResolverOverride : public nsINativeDNSResolverOverride { 96 NS_DECL_THREADSAFE_ISUPPORTS 97 NS_DECL_NSINATIVEDNSRESOLVEROVERRIDE 98 public: 99 NativeDNSResolverOverride() = default; 100 101 static already_AddRefed<nsINativeDNSResolverOverride> GetSingleton(); 102 103 private: 104 virtual ~NativeDNSResolverOverride() = default; 105 mozilla::RWLock mLock{"NativeDNSResolverOverride"}; 106 107 nsTHashMap<nsCStringHashKey, nsTArray<NetAddr>> mOverrides 108 MOZ_GUARDED_BY(mLock); 109 nsTHashMap<nsCStringHashKey, nsCString> mCnames MOZ_GUARDED_BY(mLock); 110 nsTHashMap<nsCStringHashKey, nsTArray<uint8_t>> mHTTPSRecordOverrides 111 MOZ_GUARDED_BY(mLock); 112 113 friend bool FindAddrOverride(const nsACString& aHost, uint16_t aAddressFamily, 114 nsIDNSService::DNSFlags aFlags, 115 AddrInfo** aAddrInfo); 116 friend bool FindHTTPSRecordOverride(const nsACString& aHost, 117 TypeRecordResultType& aResult); 118 }; 119 120 } // namespace net 121 } // namespace mozilla 122 123 #endif // netwerk_dns_GetAddrInfo_h