TRRQuery.h (4060B)
1 /* -*- Mode: C++; tab-width: 2; 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 mozilla_net_TRRQuery_h 8 #define mozilla_net_TRRQuery_h 9 10 #include "nsHostResolver.h" 11 #include "DNSPacket.h" 12 13 namespace mozilla { 14 namespace net { 15 16 class TRRQuery : public AHostResolver { 17 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TRRQuery, override) 18 19 public: 20 TRRQuery(nsHostResolver* aHostResolver, nsHostRecord* aHostRecord) 21 : mHostResolver(aHostResolver), 22 mRecord(aHostRecord), 23 mTrrLock("TRRQuery.mTrrLock") {} 24 25 nsresult DispatchLookup(TRR* pushedTRR = nullptr); 26 27 void Cancel(nsresult aStatus); 28 29 enum TRRState { INIT, STARTED, OK, FAILED }; 30 TRRState mTrrAUsed = INIT; 31 TRRState mTrrAAAAUsed = INIT; 32 33 TRRSkippedReason mTRRAFailReason = TRRSkippedReason::TRR_UNSET; 34 TRRSkippedReason mTRRAAAAFailReason = TRRSkippedReason::TRR_UNSET; 35 36 virtual LookupStatus CompleteLookup(nsHostRecord*, nsresult, 37 mozilla::net::AddrInfo*, bool pb, 38 const nsACString& aOriginsuffix, 39 nsHostRecord::TRRSkippedReason aReason, 40 TRR* aTRRRequest) override; 41 virtual LookupStatus CompleteLookupByType( 42 nsHostRecord*, nsresult, mozilla::net::TypeRecordResultType& aResult, 43 mozilla::net::TRRSkippedReason aReason, uint32_t aTtl, bool pb) override; 44 virtual nsresult GetHostRecord(const nsACString& host, 45 const nsACString& aTrrServer, uint16_t type, 46 nsIDNSService::DNSFlags flags, uint16_t af, 47 bool pb, const nsCString& originSuffix, 48 nsHostRecord** result) override { 49 if (!mHostResolver) { 50 return NS_ERROR_FAILURE; 51 } 52 return mHostResolver->GetHostRecord(host, aTrrServer, type, flags, af, pb, 53 originSuffix, result); 54 } 55 virtual nsresult TrrLookup_unlocked( 56 nsHostRecord* rec, mozilla::net::TRR* pushedTRR = nullptr) override { 57 if (!mHostResolver) { 58 return NS_ERROR_FAILURE; 59 } 60 return mHostResolver->TrrLookup_unlocked(rec, pushedTRR); 61 } 62 virtual void MaybeRenewHostRecord(nsHostRecord* aRec) override { 63 if (!mHostResolver) { 64 return; 65 } 66 mHostResolver->MaybeRenewHostRecord(aRec); 67 } 68 69 mozilla::TimeDuration Duration() { return mTrrDuration; } 70 71 private: 72 nsresult DispatchByTypeLookup(TRR* pushedTRR = nullptr); 73 74 private: 75 ~TRRQuery() = default; 76 77 void MarkSendingTRR(TRR* trr, TrrType rectype, MutexAutoLock&); 78 void PrepareQuery(TrrType aRecType, nsTArray<RefPtr<TRR>>& aRequestsToSend); 79 bool SendQueries(nsTArray<RefPtr<TRR>>& aRequestsToSend); 80 81 RefPtr<nsHostResolver> mHostResolver; 82 RefPtr<nsHostRecord> mRecord; 83 84 Mutex mTrrLock 85 MOZ_UNANNOTATED; // lock when accessing the mTrrA[AAA] pointers 86 RefPtr<mozilla::net::TRR> mTrrA; 87 RefPtr<mozilla::net::TRR> mTrrAAAA; 88 RefPtr<mozilla::net::TRR> mTrrByType; 89 // |mTRRRequestCounter| indicates the number of TRR requests that were 90 // dispatched sucessfully. Generally, this counter is increased to 2 after 91 // mTrrA and mTrrAAAA are dispatched, and is decreased by 1 when 92 // CompleteLookup is called. Note that nsHostResolver::CompleteLookup is only 93 // called when this counter equals to 0. 94 Atomic<uint32_t> mTRRRequestCounter{0}; 95 96 uint8_t mTRRSuccess = 0; // number of successful TRR responses 97 bool mCalledCompleteLookup = false; 98 99 mozilla::TimeDuration mTrrDuration; 100 mozilla::TimeStamp mTrrStart; 101 102 RefPtr<mozilla::net::AddrInfo> mAddrInfoA; 103 RefPtr<mozilla::net::AddrInfo> mAddrInfoAAAA; 104 nsresult mAResult = NS_OK; 105 nsresult mAAAAResult = NS_OK; 106 }; 107 108 } // namespace net 109 } // namespace mozilla 110 111 #endif // mozilla_net_TRRQuery_h