1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef ODoHService_h_
7 #define ODoHService_h_
8 
9 #include "DNS.h"
10 #include "mozilla/Atomics.h"
11 #include "mozilla/Maybe.h"
12 #include "nsString.h"
13 #include "nsIDNSListener.h"
14 #include "nsIObserver.h"
15 #include "nsIStreamLoader.h"
16 #include "nsITimer.h"
17 #include "nsWeakReference.h"
18 
19 namespace mozilla {
20 namespace net {
21 
22 class ODoH;
23 
24 class ODoHService : public nsIDNSListener,
25                     public nsIObserver,
26                     public nsSupportsWeakReference,
27                     public nsITimerCallback,
28                     public nsIStreamLoaderObserver {
29  public:
30   NS_DECL_THREADSAFE_ISUPPORTS
31   NS_DECL_NSIDNSLISTENER
32   NS_DECL_NSIOBSERVER
33   NS_DECL_NSITIMERCALLBACK
34   NS_DECL_NSISTREAMLOADEROBSERVER
35 
36   ODoHService();
37   bool Init();
38   bool Enabled() const;
39 
40   const Maybe<nsTArray<ObliviousDoHConfig>>& ODoHConfigs();
41   void AppendPendingODoHRequest(ODoH* aRequest);
42   bool RemovePendingODoHRequest(ODoH* aRequest);
43   void GetRequestURI(nsACString& aResult);
44   // Send a DNS query to reterive the ODoHConfig.
45   nsresult UpdateODoHConfig();
46 
47  private:
48   virtual ~ODoHService();
49   nsresult ReadPrefs(const char* aName);
50   void OnODoHPrefsChange(bool aInit);
51   void BuildODoHRequestURI();
52   void StartTTLTimer(uint32_t aTTL);
53   void OnODohConfigsURIChanged();
54   void ODoHConfigUpdateDone(uint32_t aTTL, Span<const uint8_t> aRawConfig);
55   nsresult UpdateODoHConfigFromHTTPSRR();
56   nsresult UpdateODoHConfigFromURI();
57 
58   Mutex mLock;
59   Atomic<bool, Relaxed> mQueryODoHConfigInProgress;
60   nsCString mODoHProxyURI;
61   nsCString mODoHTargetHost;
62   nsCString mODoHTargetPath;
63   nsCString mODoHRequestURI;
64   nsCString mODoHConfigsUri;
65   Maybe<nsTArray<ObliviousDoHConfig>> mODoHConfigs;
66   nsTArray<RefPtr<ODoH>> mPendingRequests;
67   // This timer is always touched on main thread to avoid race conditions.
68   nsCOMPtr<nsITimer> mTTLTimer;
69   nsCOMPtr<nsIStreamLoader> mLoader;
70 };
71 
72 extern ODoHService* gODoHService;
73 
74 }  // namespace net
75 }  // namespace mozilla
76 
77 #endif
78