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 {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TRRQuery,override)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, bool aUseODoHProxy = false);
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       uint32_t aTtl, bool pb) override;
GetHostRecord(const nsACString & host,const nsACString & aTrrServer,uint16_t type,uint16_t flags,uint16_t af,bool pb,const nsCString & originSuffix,nsHostRecord ** result)44   virtual nsresult GetHostRecord(const nsACString& host,
45                                  const nsACString& aTrrServer, uint16_t type,
46                                  uint16_t flags, uint16_t af, bool pb,
47                                  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   }
MaybeRenewHostRecord(nsHostRecord * aRec)62   virtual void MaybeRenewHostRecord(nsHostRecord* aRec) override {
63     if (!mHostResolver) {
64       return;
65     }
66     mHostResolver->MaybeRenewHostRecord(aRec);
67   }
68 
Duration()69   mozilla::TimeDuration Duration() { return mTrrDuration; }
70 
71  private:
72   nsresult DispatchByTypeLookup(TRR* pushedTRR = nullptr,
73                                 bool aUseODoHProxy = false);
74 
75  private:
76   ~TRRQuery() = default;
77 
78   void MarkSendingTRR(TRR* trr, TrrType rectype, MutexAutoLock&);
79   void PrepareQuery(bool aUseODoH, TrrType aRecType,
80                     nsTArray<RefPtr<TRR>>& aRequestsToSend);
81   bool SendQueries(nsTArray<RefPtr<TRR>>& aRequestsToSend);
82 
83   RefPtr<nsHostResolver> mHostResolver;
84   RefPtr<nsHostRecord> mRecord;
85 
86   Mutex mTrrLock;  // lock when accessing the mTrrA[AAA] pointers
87   RefPtr<mozilla::net::TRR> mTrrA;
88   RefPtr<mozilla::net::TRR> mTrrAAAA;
89   RefPtr<mozilla::net::TRR> mTrrByType;
90   // |mTRRRequestCounter| indicates the number of TRR requests that were
91   // dispatched sucessfully. Generally, this counter is increased to 2 after
92   // mTrrA and mTrrAAAA are dispatched, and is decreased by 1 when
93   // CompleteLookup is called. Note that nsHostResolver::CompleteLookup is only
94   // called when this counter equals to 0.
95   Atomic<uint32_t> mTRRRequestCounter{0};
96 
97   uint8_t mTRRSuccess = 0;  // number of successful TRR responses
98   bool mUsingODoH = false;
99   bool mCalledCompleteLookup = false;
100 
101   mozilla::TimeDuration mTrrDuration;
102   mozilla::TimeStamp mTrrStart;
103 
104   RefPtr<mozilla::net::AddrInfo> mAddrInfoA;
105   RefPtr<mozilla::net::AddrInfo> mAddrInfoAAAA;
106   nsresult mAResult = NS_OK;
107   nsresult mAAAAResult = NS_OK;
108 };
109 
110 }  // namespace net
111 }  // namespace mozilla
112 
113 #endif  // mozilla_net_TRRQuery_h
114