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