bug 1439067 - let TRR access TRRService through the null-checked global. r?valentin draft
authorDaniel Stenberg <daniel@haxx.se>
Mon, 19 Feb 2018 22:54:14 +0100
changeset 757100 587dac1d8f303803e187114e1780b4a7be8c0b93
parent 756852 ad133cd410a719c0b67e61b8d3b1c77a32fd80a9
push id99656
push userbmo:daniel@haxx.se
push dateMon, 19 Feb 2018 21:57:37 +0000
reviewersvalentin
bugs1439067
milestone60.0a1
bug 1439067 - let TRR access TRRService through the null-checked global. r?valentin ... and also store allow-rfc1918 bool locally to remove later accesses to TRRservice. MozReview-Commit-ID: KkO4u2N9gfE
netwerk/dns/TRR.cpp
netwerk/dns/TRR.h
--- a/netwerk/dns/TRR.cpp
+++ b/netwerk/dns/TRR.cpp
@@ -117,18 +117,17 @@ TRR::DohEncode(nsCString &aBody)
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 TRR::Run()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  MOZ_ASSERT(mTRRService);
-  if (NS_FAILED(SendHTTPRequest())) {
+  if ((gTRRService == nullptr) || NS_FAILED(SendHTTPRequest())) {
     FailData();
     // The dtor will now be run
   }
   return NS_OK;
 }
 
 nsresult
 TRR::SendHTTPRequest()
@@ -141,17 +140,17 @@ TRR::SendHTTPRequest()
     // these types
     return NS_ERROR_FAILURE;
   }
 
   nsresult rv;
   nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  bool useGet = mTRRService->UseGET();
+  bool useGet = gTRRService->UseGET();
   nsAutoCString body;
   nsCOMPtr<nsIURI> dnsURI;
 
   LOG(("TRR::SendHTTPRequest resolve %s type %u\n", mHost.get(), mType));
 
   if (useGet) {
     nsAutoCString tmp;
     rv = DohEncode(tmp);
@@ -159,26 +158,26 @@ TRR::SendHTTPRequest()
 
     /* For GET requests, the outgoing packet needs to be Base64url-encoded and
        then appended to the end of the URI. */
     rv = Base64URLEncode(tmp.Length(), reinterpret_cast<const unsigned char *>(tmp.get()),
                          Base64URLEncodePaddingPolicy::Omit, body);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsAutoCString uri;
-    mTRRService->GetURI(uri);
+    gTRRService->GetURI(uri);
     uri.Append(NS_LITERAL_CSTRING("?ct&dns="));
     uri.Append(body);
     rv = NS_NewURI(getter_AddRefs(dnsURI), uri);
   } else {
     rv = DohEncode(body);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsAutoCString uri;
-    mTRRService->GetURI(uri);
+    gTRRService->GetURI(uri);
     rv = NS_NewURI(getter_AddRefs(dnsURI), uri);
   }
   if (NS_FAILED(rv)) {
     LOG(("TRR:SendHTTPRequest: NewURI failed!\n"));
     return rv;
   }
 
   rv = NS_NewChannel(getter_AddRefs(mChannel),
@@ -201,17 +200,17 @@ TRR::SendHTTPRequest()
   }
 
   rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
                                      NS_LITERAL_CSTRING("application/dns-udpwireformat"),
                                      false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsAutoCString cred;
-  mTRRService->GetCredentials(cred);
+  gTRRService->GetCredentials(cred);
   if (!cred.IsEmpty()){
     rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Authorization"), cred, false);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   nsCOMPtr<nsIHttpChannelInternal> internalChannel = do_QueryInterface(mChannel);
   if (!internalChannel) {
     return NS_ERROR_UNEXPECTED;
@@ -219,16 +218,18 @@ TRR::SendHTTPRequest()
 
   // setting a small stream window means the h2 stack won't pipeline a window update
   // with each HEADERS or reply to a DATA with a WINDOW UPDATE
   rv = internalChannel->SetInitialRwin(127 * 1024);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = internalChannel->SetTrr(true);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  mAllowRFC1918 = gTRRService->AllowRFC1918();
+
   if (useGet) {
     rv = httpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET"));
     NS_ENSURE_SUCCESS(rv, rv);
   } else {
     rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Cache-Control"),
                                        NS_LITERAL_CSTRING("no-store"), false);
     NS_ENSURE_SUCCESS(rv, rv);
     nsCOMPtr<nsIUploadChannel2> uploadChannel = do_QueryInterface(httpChannel);
@@ -247,17 +248,17 @@ TRR::SendHTTPRequest()
   }
 
   // set the *default* response content type
   if (NS_FAILED(httpChannel->SetContentType(NS_LITERAL_CSTRING("application/dns-udpwireformat")))) {
     LOG(("TRR::SendHTTPRequest: couldn't set content-type!\n"));
   }
   if (NS_SUCCEEDED(httpChannel->AsyncOpen2(this))) {
     NS_NewTimerWithCallback(getter_AddRefs(mTimeout),
-                            this, mTRRService->GetRequestTimeout(),
+                            this, gTRRService->GetRequestTimeout(),
                             nsITimer::TYPE_ONE_SHOT);
     return NS_OK;
   }
   mChannel = nullptr;
   return NS_ERROR_UNEXPECTED;
 }
 
 NS_IMETHODIMP
@@ -592,29 +593,29 @@ TRR::DohDecode()
     nsresult rv;
     switch(TYPE) {
     case TRRTYPE_A:
       if (RDLENGTH != 4) {
         LOG(("TRR bad length for A (%u)\n", RDLENGTH));
         return NS_ERROR_UNEXPECTED;
       }
       rv = mDNS.Add(TTL, mResponse, index, RDLENGTH,
-                    mTRRService->AllowRFC1918());
+                    mAllowRFC1918);
       if (NS_FAILED(rv)) {
         LOG(("TRR:DohDecode failed: local IP addresses or unknown IP family\n"));
         return rv;
       }
       break;
     case TRRTYPE_AAAA:
       if (RDLENGTH != 16) {
         LOG(("TRR bad length for AAAA (%u)\n", RDLENGTH));
         return NS_ERROR_UNEXPECTED;
       }
       rv = mDNS.Add(TTL, mResponse, index, RDLENGTH,
-                    mTRRService->AllowRFC1918());
+                    mAllowRFC1918);
       if (NS_FAILED(rv)) {
         LOG(("TRR got unique/local IPv6 address!\n"));
         return rv;
       }
       break;
 
     case TRRTYPE_NS:
       break;
--- a/netwerk/dns/TRR.h
+++ b/netwerk/dns/TRR.h
@@ -68,81 +68,80 @@ public:
 
   // when firing off a normal A or AAAA query
   explicit TRR(AHostResolver *aResolver,
                nsHostRecord *aRec,
                enum TrrType aType)
     : mozilla::Runnable("TRR")
     , mRec(aRec)
     , mHostResolver(aResolver)
-    , mTRRService(gTRRService)
     , mType(aType)
     , mBodySize(0)
     , mFailed(false)
     , mCnameLoop(kCnameChaseMax)
+    , mAllowRFC1918(false)
   {
     mHost = aRec->host;
     mPB = aRec->pb;
   }
 
   // when following CNAMEs
   explicit TRR(AHostResolver *aResolver,
                nsHostRecord *aRec,
                nsCString &aHost,
                enum TrrType & aType,
                unsigned int aLoopCount,
                bool aPB)
     : mozilla::Runnable("TRR")
     , mHost(aHost)
     , mRec(aRec)
     , mHostResolver(aResolver)
-    , mTRRService(gTRRService)
     , mType(aType)
     , mBodySize(0)
     , mFailed(false)
     , mPB(aPB)
     , mCnameLoop(aLoopCount)
+    , mAllowRFC1918(false)
   {
 
   }
 
   // used on push
   explicit TRR(AHostResolver *aResolver, bool aPB)
     : mozilla::Runnable("TRR")
     , mHostResolver(aResolver)
-    , mTRRService(gTRRService)
     , mBodySize(0)
     , mFailed(false)
     , mPB(aPB)
     , mCnameLoop(kCnameChaseMax)
+    , mAllowRFC1918(false)
   { }
 
   // to verify a domain
   explicit TRR(AHostResolver *aResolver,
                nsACString &aHost,
                enum TrrType aType,
                bool aPB)
     : mozilla::Runnable("TRR")
     , mHost(aHost)
     , mHostResolver(aResolver)
-    , mTRRService(gTRRService)
     , mType(aType)
     , mBodySize(0)
     , mFailed(false)
     , mPB(aPB)
     , mCnameLoop(kCnameChaseMax)
+    , mAllowRFC1918(false)
   { }
 
   NS_IMETHOD Run() override;
   void Cancel();
   enum TrrType Type() { return mType; }
   nsCString mHost;
   RefPtr<nsHostRecord> mRec;
   RefPtr<AHostResolver> mHostResolver;
-  TRRService *mTRRService;
 
 private:
   ~TRR() = default;
   nsresult SendHTTPRequest();
   nsresult DohEncode(nsCString &target);
   nsresult DohDecode();
   nsresult ReturnData();
   nsresult FailData();
@@ -157,14 +156,15 @@ private:
   unsigned char mResponse[kMaxSize];
   unsigned int mBodySize;
   bool mFailed;
   bool mPB;
   DOHresp mDNS;
   nsCOMPtr<nsITimer> mTimeout;
   nsCString mCname;
   uint32_t mCnameLoop; // loop detection counter
+  bool mAllowRFC1918;
 };
 
 } // namespace net
 } // namespace mozilla
 
 #endif // include guard