Bug 1869043 add a main thread record of track audio outputs r=padenot
[gecko.git] / netwerk / dns / nsHostResolver.cpp
blob7af7c45579088a317b36c0e44a4442021c5975f2
1 /* vim:set ts=4 sw=2 sts=2 et cin: */
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/. */
6 #if defined(HAVE_RES_NINIT)
7 # include <sys/types.h>
8 # include <netinet/in.h>
9 # include <arpa/inet.h>
10 # include <arpa/nameser.h>
11 # include <resolv.h>
12 # define RES_RETRY_ON_FAILURE
13 #endif
15 #include <stdlib.h>
16 #include <ctime>
17 #include "nsHostResolver.h"
18 #include "nsError.h"
19 #include "nsISupports.h"
20 #include "nsISupportsUtils.h"
21 #include "nsIThreadManager.h"
22 #include "nsComponentManagerUtils.h"
23 #include "nsNetUtil.h"
24 #include "nsPrintfCString.h"
25 #include "nsXPCOMCIDInternal.h"
26 #include "prthread.h"
27 #include "prerror.h"
28 #include "prtime.h"
29 #include "mozilla/Logging.h"
30 #include "PLDHashTable.h"
31 #include "nsQueryObject.h"
32 #include "nsURLHelper.h"
33 #include "nsThreadUtils.h"
34 #include "nsThreadPool.h"
35 #include "GetAddrInfo.h"
36 #include "TRR.h"
37 #include "TRRQuery.h"
38 #include "TRRService.h"
40 #include "mozilla/Atomics.h"
41 #include "mozilla/glean/GleanMetrics.h"
42 #include "mozilla/HashFunctions.h"
43 #include "mozilla/TimeStamp.h"
44 #include "mozilla/Telemetry.h"
45 #include "mozilla/DebugOnly.h"
46 #include "mozilla/Preferences.h"
47 #include "mozilla/StaticPrefs_network.h"
48 // Put DNSLogging.h at the end to avoid LOG being overwritten by other headers.
49 #include "DNSLogging.h"
51 #ifdef XP_WIN
52 # include "mozilla/WindowsVersion.h"
53 #endif // XP_WIN
55 #ifdef MOZ_WIDGET_ANDROID
56 # include "mozilla/jni/Utils.h"
57 #endif
59 #define IS_ADDR_TYPE(_type) ((_type) == nsIDNSService::RESOLVE_TYPE_DEFAULT)
60 #define IS_OTHER_TYPE(_type) ((_type) != nsIDNSService::RESOLVE_TYPE_DEFAULT)
62 using namespace mozilla;
63 using namespace mozilla::net;
65 // None of our implementations expose a TTL for negative responses, so we use a
66 // constant always.
67 static const unsigned int NEGATIVE_RECORD_LIFETIME = 60;
69 //----------------------------------------------------------------------------
71 // Use a persistent thread pool in order to avoid spinning up new threads all
72 // the time. In particular, thread creation results in a res_init() call from
73 // libc which is quite expensive.
75 // The pool dynamically grows between 0 and MaxResolverThreads() in size. New
76 // requests go first to an idle thread. If that cannot be found and there are
77 // fewer than MaxResolverThreads() currently in the pool a new thread is created
78 // for high priority requests. If the new request is at a lower priority a new
79 // thread will only be created if there are fewer than
80 // MaxResolverThreadsAnyPriority() currently outstanding. If a thread cannot be
81 // created or an idle thread located for the request it is queued.
83 // When the pool is greater than MaxResolverThreadsAnyPriority() in size a
84 // thread will be destroyed after ShortIdleTimeoutSeconds of idle time. Smaller
85 // pools use LongIdleTimeoutSeconds for a timeout period.
87 // for threads 1 -> MaxResolverThreadsAnyPriority()
88 #define LongIdleTimeoutSeconds 300
89 // for threads MaxResolverThreadsAnyPriority() + 1 -> MaxResolverThreads()
90 #define ShortIdleTimeoutSeconds 60
92 //----------------------------------------------------------------------------
94 namespace mozilla::net {
95 LazyLogModule gHostResolverLog("nsHostResolver");
96 } // namespace mozilla::net
98 //----------------------------------------------------------------------------
100 #if defined(RES_RETRY_ON_FAILURE)
102 // this class represents the resolver state for a given thread. if we
103 // encounter a lookup failure, then we can invoke the Reset method on an
104 // instance of this class to reset the resolver (in case /etc/resolv.conf
105 // for example changed). this is mainly an issue on GNU systems since glibc
106 // only reads in /etc/resolv.conf once per thread. it may be an issue on
107 // other systems as well.
109 class nsResState {
110 public:
111 nsResState()
112 // initialize mLastReset to the time when this object
113 // is created. this means that a reset will not occur
114 // if a thread is too young. the alternative would be
115 // to initialize this to the beginning of time, so that
116 // the first failure would cause a reset, but since the
117 // thread would have just started up, it likely would
118 // already have current /etc/resolv.conf info.
119 : mLastReset(PR_IntervalNow()) {}
121 bool Reset() {
122 // reset no more than once per second
123 if (PR_IntervalToSeconds(PR_IntervalNow() - mLastReset) < 1) {
124 return false;
127 mLastReset = PR_IntervalNow();
128 auto result = res_ninit(&_res);
130 LOG(("nsResState::Reset() > 'res_ninit' returned %d", result));
131 return (result == 0);
134 private:
135 PRIntervalTime mLastReset;
138 #endif // RES_RETRY_ON_FAILURE
140 //----------------------------------------------------------------------------
142 static const char kPrefGetTtl[] = "network.dns.get-ttl";
143 static const char kPrefNativeIsLocalhost[] = "network.dns.native-is-localhost";
144 static const char kPrefThreadIdleTime[] =
145 "network.dns.resolver-thread-extra-idle-time-seconds";
146 static bool sGetTtlEnabled = false;
147 mozilla::Atomic<bool, mozilla::Relaxed> gNativeIsLocalhost;
148 mozilla::Atomic<bool, mozilla::Relaxed> sNativeHTTPSSupported{false};
150 static void DnsPrefChanged(const char* aPref, void* aSelf) {
151 MOZ_ASSERT(NS_IsMainThread(),
152 "Should be getting pref changed notification on main thread!");
154 MOZ_ASSERT(aSelf);
156 if (!strcmp(aPref, kPrefGetTtl)) {
157 #ifdef DNSQUERY_AVAILABLE
158 sGetTtlEnabled = Preferences::GetBool(kPrefGetTtl);
159 #endif
160 } else if (!strcmp(aPref, kPrefNativeIsLocalhost)) {
161 gNativeIsLocalhost = Preferences::GetBool(kPrefNativeIsLocalhost);
165 NS_IMPL_ISUPPORTS0(nsHostResolver)
167 nsHostResolver::nsHostResolver(uint32_t maxCacheEntries,
168 uint32_t defaultCacheEntryLifetime,
169 uint32_t defaultGracePeriod)
170 : mMaxCacheEntries(maxCacheEntries),
171 mDefaultCacheLifetime(defaultCacheEntryLifetime),
172 mDefaultGracePeriod(defaultGracePeriod),
173 mIdleTaskCV(mLock, "nsHostResolver.mIdleTaskCV") {
174 mCreationTime = PR_Now();
176 mLongIdleTimeout = TimeDuration::FromSeconds(LongIdleTimeoutSeconds);
177 mShortIdleTimeout = TimeDuration::FromSeconds(ShortIdleTimeoutSeconds);
180 nsHostResolver::~nsHostResolver() = default;
182 nsresult nsHostResolver::Init() MOZ_NO_THREAD_SAFETY_ANALYSIS {
183 MOZ_ASSERT(NS_IsMainThread());
184 if (NS_FAILED(GetAddrInfoInit())) {
185 return NS_ERROR_FAILURE;
188 LOG(("nsHostResolver::Init this=%p", this));
190 mShutdown = false;
191 mNCS = NetworkConnectivityService::GetSingleton();
193 // The preferences probably haven't been loaded from the disk yet, so we
194 // need to register a callback that will set up the experiment once they
195 // are. We also need to explicitly set a value for the props otherwise the
196 // callback won't be called.
198 DebugOnly<nsresult> rv = Preferences::RegisterCallbackAndCall(
199 &DnsPrefChanged, kPrefGetTtl, this);
200 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
201 "Could not register DNS TTL pref callback.");
202 rv = Preferences::RegisterCallbackAndCall(&DnsPrefChanged,
203 kPrefNativeIsLocalhost, this);
204 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
205 "Could not register DNS pref callback.");
208 #if defined(HAVE_RES_NINIT)
209 // We want to make sure the system is using the correct resolver settings,
210 // so we force it to reload those settings whenever we startup a subsequent
211 // nsHostResolver instance. We assume that there is no reason to do this
212 // for the first nsHostResolver instance since that is usually created
213 // during application startup.
214 static int initCount = 0;
215 if (initCount++ > 0) {
216 auto result = res_ninit(&_res);
217 LOG(("nsHostResolver::Init > 'res_ninit' returned %d", result));
219 #endif
221 // We can configure the threadpool to keep threads alive for a while after
222 // the last ThreadFunc task has been executed.
223 int32_t poolTimeoutSecs = Preferences::GetInt(kPrefThreadIdleTime, 60);
224 uint32_t poolTimeoutMs;
225 if (poolTimeoutSecs < 0) {
226 // This means never shut down the idle threads
227 poolTimeoutMs = UINT32_MAX;
228 } else {
229 // We clamp down the idle time between 0 and one hour.
230 poolTimeoutMs =
231 mozilla::clamped<uint32_t>(poolTimeoutSecs * 1000, 0, 3600 * 1000);
234 #if defined(XP_WIN)
235 // For some reason, the DNSQuery_A API doesn't work on Windows 10.
236 // It returns a success code, but no records. We only allow
237 // native HTTPS records on Win 11 for now.
238 sNativeHTTPSSupported = StaticPrefs::network_dns_native_https_query_win10() ||
239 mozilla::IsWin11OrLater();
240 #elif defined(MOZ_WIDGET_ANDROID)
241 // android_res_nquery only got added in API level 29
242 sNativeHTTPSSupported = jni::GetAPIVersion() >= 29;
243 #elif defined(XP_LINUX) || defined(XP_MACOSX)
244 sNativeHTTPSSupported = true;
245 #endif
246 LOG(("Native HTTPS records supported=%d", bool(sNativeHTTPSSupported)));
248 nsCOMPtr<nsIThreadPool> threadPool = new nsThreadPool();
249 MOZ_ALWAYS_SUCCEEDS(threadPool->SetThreadLimit(MaxResolverThreads()));
250 MOZ_ALWAYS_SUCCEEDS(threadPool->SetIdleThreadLimit(MaxResolverThreads()));
251 MOZ_ALWAYS_SUCCEEDS(threadPool->SetIdleThreadTimeout(poolTimeoutMs));
252 MOZ_ALWAYS_SUCCEEDS(
253 threadPool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize));
254 MOZ_ALWAYS_SUCCEEDS(threadPool->SetName("DNS Resolver"_ns));
255 mResolverThreads = ToRefPtr(std::move(threadPool));
257 return NS_OK;
260 void nsHostResolver::ClearPendingQueue(
261 LinkedList<RefPtr<nsHostRecord>>& aPendingQ) {
262 // loop through pending queue, erroring out pending lookups.
263 if (!aPendingQ.isEmpty()) {
264 for (const RefPtr<nsHostRecord>& rec : aPendingQ) {
265 rec->Cancel();
266 if (rec->IsAddrRecord()) {
267 CompleteLookup(rec, NS_ERROR_ABORT, nullptr, rec->pb, rec->originSuffix,
268 rec->mTRRSkippedReason, nullptr);
269 } else {
270 mozilla::net::TypeRecordResultType empty(Nothing{});
271 CompleteLookupByType(rec, NS_ERROR_ABORT, empty, rec->mTRRSkippedReason,
272 0, rec->pb);
279 // FlushCache() is what we call when the network has changed. We must not
280 // trust names that were resolved before this change. They may resolve
281 // differently now.
283 // This function removes all existing resolved host entries from the hash.
284 // Names that are in the pending queues can be left there. Entries in the
285 // cache that have 'Resolve' set true but not 'OnQueue' are being resolved
286 // right now, so we need to mark them to get re-resolved on completion!
288 void nsHostResolver::FlushCache(bool aTrrToo) {
289 MutexAutoLock lock(mLock);
291 mQueue.FlushEvictionQ(mRecordDB, lock);
293 // Refresh the cache entries that are resolving RIGHT now, remove the rest.
294 for (auto iter = mRecordDB.Iter(); !iter.Done(); iter.Next()) {
295 nsHostRecord* record = iter.UserData();
296 // Try to remove the record, or mark it for refresh.
297 // By-type records are from TRR. We do not need to flush those entry
298 // when the network has change, because they are not local.
299 if (record->IsAddrRecord()) {
300 RefPtr<AddrHostRecord> addrRec = do_QueryObject(record);
301 MOZ_ASSERT(addrRec);
302 if (addrRec->RemoveOrRefresh(aTrrToo)) {
303 mQueue.MaybeRemoveFromQ(record, lock);
304 LOG(("Removing (%s) Addr record from mRecordDB", record->host.get()));
305 iter.Remove();
307 } else if (aTrrToo) {
308 // remove by type records
309 LOG(("Removing (%s) type record from mRecordDB", record->host.get()));
310 iter.Remove();
315 void nsHostResolver::Shutdown() {
316 LOG(("Shutting down host resolver.\n"));
319 DebugOnly<nsresult> rv =
320 Preferences::UnregisterCallback(&DnsPrefChanged, kPrefGetTtl, this);
321 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
322 "Could not unregister DNS TTL pref callback.");
325 LinkedList<RefPtr<nsHostRecord>> pendingQHigh, pendingQMed, pendingQLow,
326 evictionQ;
329 MutexAutoLock lock(mLock);
331 mShutdown = true;
333 if (mNumIdleTasks) {
334 mIdleTaskCV.NotifyAll();
337 mQueue.ClearAll(
338 [&](nsHostRecord* aRec) {
339 mLock.AssertCurrentThreadOwns();
340 if (aRec->IsAddrRecord()) {
341 CompleteLookupLocked(aRec, NS_ERROR_ABORT, nullptr, aRec->pb,
342 aRec->originSuffix, aRec->mTRRSkippedReason,
343 nullptr, lock);
344 } else {
345 mozilla::net::TypeRecordResultType empty(Nothing{});
346 CompleteLookupByTypeLocked(aRec, NS_ERROR_ABORT, empty,
347 aRec->mTRRSkippedReason, 0, aRec->pb,
348 lock);
351 lock);
353 for (const auto& data : mRecordDB.Values()) {
354 data->Cancel();
356 // empty host database
357 mRecordDB.Clear();
359 mNCS = nullptr;
362 // Shutdown the resolver threads, but with a timeout of 2 seconds (prefable).
363 // If the timeout is exceeded, any stuck threads will be leaked.
364 mResolverThreads->ShutdownWithTimeout(
365 StaticPrefs::network_dns_resolver_shutdown_timeout_ms());
368 mozilla::DebugOnly<nsresult> rv = GetAddrInfoShutdown();
369 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to shutdown GetAddrInfo");
373 nsresult nsHostResolver::GetHostRecord(
374 const nsACString& host, const nsACString& aTrrServer, uint16_t type,
375 nsIDNSService::DNSFlags flags, uint16_t af, bool pb,
376 const nsCString& originSuffix, nsHostRecord** result) {
377 MutexAutoLock lock(mLock);
378 nsHostKey key(host, aTrrServer, type, flags, af, pb, originSuffix);
380 RefPtr<nsHostRecord> rec =
381 mRecordDB.LookupOrInsertWith(key, [&] { return InitRecord(key); });
382 if (rec->IsAddrRecord()) {
383 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
384 if (addrRec->addr) {
385 return NS_ERROR_FAILURE;
389 if (rec->mResolving) {
390 return NS_ERROR_FAILURE;
393 *result = rec.forget().take();
394 return NS_OK;
397 nsHostRecord* nsHostResolver::InitRecord(const nsHostKey& key) {
398 if (IS_ADDR_TYPE(key.type)) {
399 return new AddrHostRecord(key);
401 return new TypeHostRecord(key);
404 already_AddRefed<nsHostRecord> nsHostResolver::InitLoopbackRecord(
405 const nsHostKey& key, nsresult* aRv) {
406 MOZ_ASSERT(aRv);
407 MOZ_ASSERT(IS_ADDR_TYPE(key.type));
409 *aRv = NS_ERROR_FAILURE;
410 RefPtr<nsHostRecord> rec = InitRecord(key);
412 nsTArray<NetAddr> addresses;
413 NetAddr addr;
414 if (key.af == PR_AF_INET || key.af == PR_AF_UNSPEC) {
415 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(addr.InitFromString("127.0.0.1"_ns)));
416 addresses.AppendElement(addr);
418 if (key.af == PR_AF_INET6 || key.af == PR_AF_UNSPEC) {
419 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(addr.InitFromString("::1"_ns)));
420 addresses.AppendElement(addr);
423 RefPtr<AddrInfo> ai =
424 new AddrInfo(rec->host, DNSResolverType::Native, 0, std::move(addresses));
426 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
427 MutexAutoLock lock(addrRec->addr_info_lock);
428 addrRec->addr_info = ai;
429 addrRec->SetExpiration(TimeStamp::NowLoRes(), mDefaultCacheLifetime,
430 mDefaultGracePeriod);
431 addrRec->negative = false;
433 *aRv = NS_OK;
434 return rec.forget();
437 static bool IsNativeHTTPSEnabled() {
438 if (!StaticPrefs::network_dns_native_https_query()) {
439 return false;
441 return sNativeHTTPSSupported;
444 nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
445 const nsACString& aTrrServer,
446 int32_t aPort, uint16_t type,
447 const OriginAttributes& aOriginAttributes,
448 nsIDNSService::DNSFlags flags, uint16_t af,
449 nsResolveHostCallback* aCallback) {
450 nsAutoCString host(aHost);
451 NS_ENSURE_TRUE(!host.IsEmpty(), NS_ERROR_UNEXPECTED);
453 nsAutoCString originSuffix;
454 aOriginAttributes.CreateSuffix(originSuffix);
455 LOG(("Resolving host [%s]<%s>%s%s type %d. [this=%p]\n", host.get(),
456 originSuffix.get(), flags & RES_BYPASS_CACHE ? " - bypassing cache" : "",
457 flags & RES_REFRESH_CACHE ? " - refresh cache" : "", type, this));
459 // ensure that we are working with a valid hostname before proceeding. see
460 // bug 304904 for details.
461 if (!net_IsValidHostName(host)) {
462 return NS_ERROR_UNKNOWN_HOST;
465 // If TRR is disabled we can return immediately if the native API is disabled
466 if (!IsNativeHTTPSEnabled() && IS_OTHER_TYPE(type) &&
467 Mode() == nsIDNSService::MODE_TRROFF) {
468 return NS_ERROR_UNKNOWN_HOST;
471 // Used to try to parse to an IP address literal.
472 NetAddr tempAddr;
473 if (IS_OTHER_TYPE(type) && (NS_SUCCEEDED(tempAddr.InitFromString(host)))) {
474 // For by-type queries the host cannot be IP literal.
475 return NS_ERROR_UNKNOWN_HOST;
478 RefPtr<nsResolveHostCallback> callback(aCallback);
479 // if result is set inside the lock, then we need to issue the
480 // callback before returning.
481 RefPtr<nsHostRecord> result;
482 nsresult status = NS_OK, rv = NS_OK;
484 MutexAutoLock lock(mLock);
486 if (mShutdown) {
487 return NS_ERROR_NOT_INITIALIZED;
490 // check to see if there is already an entry for this |host|
491 // in the hash table. if so, then check to see if we can't
492 // just reuse the lookup result. otherwise, if there are
493 // any pending callbacks, then add to pending callbacks queue,
494 // and return. otherwise, add ourselves as first pending
495 // callback, and proceed to do the lookup.
497 Maybe<nsCString> originHost;
498 if (StaticPrefs::network_dns_port_prefixed_qname_https_rr() &&
499 type == nsIDNSService::RESOLVE_TYPE_HTTPSSVC && aPort != -1 &&
500 aPort != 443) {
501 originHost = Some(host);
502 host = nsPrintfCString("_%d._https.%s", aPort, host.get());
503 LOG((" Using port prefixed host name [%s]", host.get()));
506 bool excludedFromTRR = false;
507 if (TRRService::Get() && TRRService::Get()->IsExcludedFromTRR(host)) {
508 flags |= nsIDNSService::RESOLVE_DISABLE_TRR;
509 excludedFromTRR = true;
511 if (!aTrrServer.IsEmpty()) {
512 return NS_ERROR_UNKNOWN_HOST;
516 nsHostKey key(host, aTrrServer, type, flags, af,
517 (aOriginAttributes.mPrivateBrowsingId > 0), originSuffix);
519 // Check if we have a localhost domain, if so hardcode to loopback
520 if (IS_ADDR_TYPE(type) && IsLoopbackHostname(host)) {
521 nsresult rv;
522 RefPtr<nsHostRecord> result = InitLoopbackRecord(key, &rv);
523 if (NS_WARN_IF(NS_FAILED(rv))) {
524 return rv;
526 MOZ_ASSERT(result);
527 aCallback->OnResolveHostComplete(this, result, NS_OK);
528 return NS_OK;
531 RefPtr<nsHostRecord> rec =
532 mRecordDB.LookupOrInsertWith(key, [&] { return InitRecord(key); });
534 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
535 MOZ_ASSERT(rec, "Record should not be null");
536 MOZ_ASSERT((IS_ADDR_TYPE(type) && rec->IsAddrRecord() && addrRec) ||
537 (IS_OTHER_TYPE(type) && !rec->IsAddrRecord()));
539 if (IS_OTHER_TYPE(type) && originHost) {
540 RefPtr<TypeHostRecord> typeRec = do_QueryObject(rec);
541 typeRec->mOriginHost = std::move(originHost);
544 if (excludedFromTRR) {
545 rec->RecordReason(TRRSkippedReason::TRR_EXCLUDED);
548 if (!(flags & RES_BYPASS_CACHE) &&
549 rec->HasUsableResult(TimeStamp::NowLoRes(), flags)) {
550 result = FromCache(rec, host, type, status, lock);
551 } else if (addrRec && addrRec->addr) {
552 // if the host name is an IP address literal and has been
553 // parsed, go ahead and use it.
554 LOG((" Using cached address for IP Literal [%s].\n", host.get()));
555 result = FromCachedIPLiteral(rec);
556 } else if (addrRec && NS_SUCCEEDED(tempAddr.InitFromString(host))) {
557 // try parsing the host name as an IP address literal to short
558 // circuit full host resolution. (this is necessary on some
559 // platforms like Win9x. see bug 219376 for more details.)
560 LOG((" Host is IP Literal [%s].\n", host.get()));
561 result = FromIPLiteral(addrRec, tempAddr);
562 } else if (mQueue.PendingCount() >= MAX_NON_PRIORITY_REQUESTS &&
563 !IsHighPriority(flags) && !rec->mResolving) {
564 LOG(
565 (" Lookup queue full: dropping %s priority request for "
566 "host [%s].\n",
567 IsMediumPriority(flags) ? "medium" : "low", host.get()));
568 if (IS_ADDR_TYPE(type)) {
569 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_OVERFLOW);
571 // This is a lower priority request and we are swamped, so refuse it.
572 rv = NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
574 // Check if the offline flag is set.
575 } else if (flags & RES_OFFLINE) {
576 LOG((" Offline request for host [%s]; ignoring.\n", host.get()));
577 rv = NS_ERROR_OFFLINE;
579 // We do not have a valid result till here.
580 // A/AAAA request can check for an alternative entry like AF_UNSPEC.
581 // Otherwise we need to start a new query.
582 } else if (!rec->mResolving) {
583 result =
584 FromUnspecEntry(rec, host, aTrrServer, originSuffix, type, flags, af,
585 aOriginAttributes.mPrivateBrowsingId > 0, status);
586 // If this is a by-type request or if no valid record was found
587 // in the cache or this is an AF_UNSPEC request, then start a
588 // new lookup.
589 if (!result) {
590 LOG((" No usable record in cache for host [%s] type %d.", host.get(),
591 type));
593 if (flags & RES_REFRESH_CACHE) {
594 rec->Invalidate();
597 // Add callback to the list of pending callbacks.
598 rec->mCallbacks.insertBack(callback);
599 rec->flags = flags;
600 rv = NameLookup(rec, lock);
601 if (IS_ADDR_TYPE(type)) {
602 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
603 METHOD_NETWORK_FIRST);
605 if (NS_FAILED(rv) && callback->isInList()) {
606 callback->remove();
607 } else {
608 LOG(
609 (" DNS lookup for host [%s] blocking "
610 "pending 'getaddrinfo' or trr query: "
611 "callback [%p]",
612 host.get(), callback.get()));
615 } else {
616 LOG(
617 (" Host [%s] is being resolved. Appending callback "
618 "[%p].",
619 host.get(), callback.get()));
621 rec->mCallbacks.insertBack(callback);
623 // Only A/AAAA records are place in a queue. The queues are for
624 // the native resolver, therefore by-type request are never put
625 // into a queue.
626 if (addrRec && addrRec->onQueue()) {
627 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
628 METHOD_NETWORK_SHARED);
630 // Consider the case where we are on a pending queue of
631 // lower priority than the request is being made at.
632 // In that case we should upgrade to the higher queue.
634 if (IsHighPriority(flags) && !IsHighPriority(rec->flags)) {
635 // Move from (low|med) to high.
636 mQueue.MoveToAnotherPendingQ(rec, flags, lock);
637 rec->flags = flags;
638 ConditionallyCreateThread(rec);
639 } else if (IsMediumPriority(flags) && IsLowPriority(rec->flags)) {
640 // Move from low to med.
641 mQueue.MoveToAnotherPendingQ(rec, flags, lock);
642 rec->flags = flags;
643 mIdleTaskCV.Notify();
648 if (result && callback->isInList()) {
649 callback->remove();
651 } // lock
653 if (result) {
654 callback->OnResolveHostComplete(this, result, status);
657 return rv;
660 already_AddRefed<nsHostRecord> nsHostResolver::FromCache(
661 nsHostRecord* aRec, const nsACString& aHost, uint16_t aType,
662 nsresult& aStatus, const MutexAutoLock& aLock) {
663 LOG((" Using cached record for host [%s].\n",
664 nsPromiseFlatCString(aHost).get()));
666 // put reference to host record on stack...
667 RefPtr<nsHostRecord> result = aRec;
668 if (IS_ADDR_TYPE(aType)) {
669 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
672 // For entries that are in the grace period
673 // or all cached negative entries, use the cache but start a new
674 // lookup in the background
675 ConditionallyRefreshRecord(aRec, aHost, aLock);
677 if (aRec->negative) {
678 LOG((" Negative cache entry for host [%s].\n",
679 nsPromiseFlatCString(aHost).get()));
680 if (IS_ADDR_TYPE(aType)) {
681 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_NEGATIVE_HIT);
683 aStatus = NS_ERROR_UNKNOWN_HOST;
686 return result.forget();
689 already_AddRefed<nsHostRecord> nsHostResolver::FromCachedIPLiteral(
690 nsHostRecord* aRec) {
691 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
692 RefPtr<nsHostRecord> result = aRec;
693 return result.forget();
696 already_AddRefed<nsHostRecord> nsHostResolver::FromIPLiteral(
697 AddrHostRecord* aAddrRec, const NetAddr& aAddr) {
698 // ok, just copy the result into the host record, and be
699 // done with it! ;-)
700 aAddrRec->addr = MakeUnique<NetAddr>(aAddr);
701 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
702 // put reference to host record on stack...
703 RefPtr<nsHostRecord> result = aAddrRec;
704 return result.forget();
707 already_AddRefed<nsHostRecord> nsHostResolver::FromUnspecEntry(
708 nsHostRecord* aRec, const nsACString& aHost, const nsACString& aTrrServer,
709 const nsACString& aOriginSuffix, uint16_t aType,
710 nsIDNSService::DNSFlags aFlags, uint16_t af, bool aPb, nsresult& aStatus) {
711 RefPtr<nsHostRecord> result = nullptr;
712 // If this is an IPV4 or IPV6 specific request, check if there is
713 // an AF_UNSPEC entry we can use. Otherwise, hit the resolver...
714 RefPtr<AddrHostRecord> addrRec = do_QueryObject(aRec);
715 if (addrRec && !(aFlags & RES_BYPASS_CACHE) &&
716 ((af == PR_AF_INET) || (af == PR_AF_INET6))) {
717 // Check for an AF_UNSPEC entry.
719 const nsHostKey unspecKey(aHost, aTrrServer,
720 nsIDNSService::RESOLVE_TYPE_DEFAULT, aFlags,
721 PR_AF_UNSPEC, aPb, aOriginSuffix);
722 RefPtr<nsHostRecord> unspecRec = mRecordDB.Get(unspecKey);
724 TimeStamp now = TimeStamp::NowLoRes();
725 if (unspecRec && unspecRec->HasUsableResult(now, aFlags)) {
726 MOZ_ASSERT(unspecRec->IsAddrRecord());
728 RefPtr<AddrHostRecord> addrUnspecRec = do_QueryObject(unspecRec);
729 MOZ_ASSERT(addrUnspecRec);
730 MOZ_ASSERT(addrUnspecRec->addr_info || addrUnspecRec->negative,
731 "Entry should be resolved or negative.");
733 LOG((" Trying AF_UNSPEC entry for host [%s] af: %s.\n",
734 PromiseFlatCString(aHost).get(),
735 (af == PR_AF_INET) ? "AF_INET" : "AF_INET6"));
737 // We need to lock in case any other thread is reading
738 // addr_info.
739 MutexAutoLock lock(addrRec->addr_info_lock);
741 addrRec->addr_info = nullptr;
742 addrRec->addr_info_gencnt++;
743 if (unspecRec->negative) {
744 aRec->negative = unspecRec->negative;
745 aRec->CopyExpirationTimesAndFlagsFrom(unspecRec);
746 } else if (addrUnspecRec->addr_info) {
747 MutexAutoLock lock(addrUnspecRec->addr_info_lock);
748 if (addrUnspecRec->addr_info) {
749 // Search for any valid address in the AF_UNSPEC entry
750 // in the cache (not blocklisted and from the right
751 // family).
752 nsTArray<NetAddr> addresses;
753 for (const auto& addr : addrUnspecRec->addr_info->Addresses()) {
754 if ((af == addr.inet.family) &&
755 !addrUnspecRec->Blocklisted(&addr)) {
756 addresses.AppendElement(addr);
759 if (!addresses.IsEmpty()) {
760 addrRec->addr_info = new AddrInfo(
761 addrUnspecRec->addr_info->Hostname(),
762 addrUnspecRec->addr_info->CanonicalHostname(),
763 addrUnspecRec->addr_info->ResolverType(),
764 addrUnspecRec->addr_info->TRRType(), std::move(addresses));
765 addrRec->addr_info_gencnt++;
766 aRec->CopyExpirationTimesAndFlagsFrom(unspecRec);
770 // Now check if we have a new record.
771 if (aRec->HasUsableResult(now, aFlags)) {
772 result = aRec;
773 if (aRec->negative) {
774 aStatus = NS_ERROR_UNKNOWN_HOST;
776 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
777 ConditionallyRefreshRecord(aRec, aHost, lock);
778 } else if (af == PR_AF_INET6) {
779 // For AF_INET6, a new lookup means another AF_UNSPEC
780 // lookup. We have already iterated through the
781 // AF_UNSPEC addresses, so we mark this record as
782 // negative.
783 LOG(
784 (" No AF_INET6 in AF_UNSPEC entry: "
785 "host [%s] unknown host.",
786 nsPromiseFlatCString(aHost).get()));
787 result = aRec;
788 aRec->negative = true;
789 aStatus = NS_ERROR_UNKNOWN_HOST;
790 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
791 METHOD_NEGATIVE_HIT);
796 return result.forget();
799 void nsHostResolver::DetachCallback(
800 const nsACString& host, const nsACString& aTrrServer, uint16_t aType,
801 const OriginAttributes& aOriginAttributes, nsIDNSService::DNSFlags flags,
802 uint16_t af, nsResolveHostCallback* aCallback, nsresult status) {
803 RefPtr<nsHostRecord> rec;
804 RefPtr<nsResolveHostCallback> callback(aCallback);
807 MutexAutoLock lock(mLock);
809 nsAutoCString originSuffix;
810 aOriginAttributes.CreateSuffix(originSuffix);
812 nsHostKey key(host, aTrrServer, aType, flags, af,
813 (aOriginAttributes.mPrivateBrowsingId > 0), originSuffix);
814 RefPtr<nsHostRecord> entry = mRecordDB.Get(key);
815 if (entry) {
816 // walk list looking for |callback|... we cannot assume
817 // that it will be there!
819 for (nsResolveHostCallback* c : entry->mCallbacks) {
820 if (c == callback) {
821 rec = entry;
822 c->remove();
823 break;
829 // complete callback with the given status code; this would only be done if
830 // the record was in the process of being resolved.
831 if (rec) {
832 callback->OnResolveHostComplete(this, rec, status);
836 nsresult nsHostResolver::ConditionallyCreateThread(nsHostRecord* rec) {
837 if (mNumIdleTasks) {
838 // wake up idle tasks to process this lookup
839 mIdleTaskCV.Notify();
840 } else if ((mActiveTaskCount < MaxResolverThreadsAnyPriority()) ||
841 (IsHighPriority(rec->flags) &&
842 mActiveTaskCount < MaxResolverThreads())) {
843 nsCOMPtr<nsIRunnable> event = mozilla::NewRunnableMethod(
844 "nsHostResolver::ThreadFunc", this, &nsHostResolver::ThreadFunc);
845 mActiveTaskCount++;
846 nsresult rv =
847 mResolverThreads->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
848 if (NS_FAILED(rv)) {
849 mActiveTaskCount--;
851 } else {
852 LOG((" Unable to find a thread for looking up host [%s].\n",
853 rec->host.get()));
855 return NS_OK;
858 nsresult nsHostResolver::TrrLookup_unlocked(nsHostRecord* rec, TRR* pushedTRR) {
859 MutexAutoLock lock(mLock);
860 return TrrLookup(rec, lock, pushedTRR);
863 void nsHostResolver::MaybeRenewHostRecord(nsHostRecord* aRec) {
864 MutexAutoLock lock(mLock);
865 MaybeRenewHostRecordLocked(aRec, lock);
868 void nsHostResolver::MaybeRenewHostRecordLocked(nsHostRecord* aRec,
869 const MutexAutoLock& aLock) {
870 mQueue.MaybeRenewHostRecord(aRec, aLock);
873 bool nsHostResolver::TRRServiceEnabledForRecord(nsHostRecord* aRec) {
874 MOZ_ASSERT(aRec, "Record must not be empty");
875 MOZ_ASSERT(aRec->mEffectiveTRRMode != nsIRequest::TRR_DEFAULT_MODE,
876 "effective TRR mode must be computed before this call");
877 if (!TRRService::Get()) {
878 aRec->RecordReason(TRRSkippedReason::TRR_NO_GSERVICE);
879 return false;
882 // We always try custom resolvers.
883 if (!aRec->mTrrServer.IsEmpty()) {
884 return true;
887 nsIRequest::TRRMode reqMode = aRec->mEffectiveTRRMode;
888 if (TRRService::Get()->Enabled(reqMode)) {
889 return true;
892 if (NS_IsOffline()) {
893 // If we are in the NOT_CONFIRMED state _because_ we lack connectivity,
894 // then we should report that the browser is offline instead.
895 aRec->RecordReason(TRRSkippedReason::TRR_IS_OFFLINE);
896 return false;
899 auto hasConnectivity = [this]() -> bool {
900 mLock.AssertCurrentThreadOwns();
901 if (!mNCS) {
902 return true;
904 nsINetworkConnectivityService::ConnectivityState ipv4 = mNCS->GetIPv4();
905 nsINetworkConnectivityService::ConnectivityState ipv6 = mNCS->GetIPv6();
907 if (ipv4 == nsINetworkConnectivityService::OK ||
908 ipv6 == nsINetworkConnectivityService::OK) {
909 return true;
912 if (ipv4 == nsINetworkConnectivityService::UNKNOWN ||
913 ipv6 == nsINetworkConnectivityService::UNKNOWN) {
914 // One of the checks hasn't completed yet. Optimistically assume we'll
915 // have network connectivity.
916 return true;
919 return false;
922 if (!hasConnectivity()) {
923 aRec->RecordReason(TRRSkippedReason::TRR_NO_CONNECTIVITY);
924 return false;
927 bool isConfirmed = TRRService::Get()->IsConfirmed();
928 if (!isConfirmed) {
929 aRec->RecordReason(TRRSkippedReason::TRR_NOT_CONFIRMED);
932 return isConfirmed;
935 // returns error if no TRR resolve is issued
936 // it is impt this is not called while a native lookup is going on
937 nsresult nsHostResolver::TrrLookup(nsHostRecord* aRec,
938 const MutexAutoLock& aLock, TRR* pushedTRR) {
939 if (Mode() == nsIDNSService::MODE_TRROFF ||
940 StaticPrefs::network_dns_disabled()) {
941 return NS_ERROR_UNKNOWN_HOST;
943 LOG(("TrrLookup host:%s af:%" PRId16, aRec->host.get(), aRec->af));
945 RefPtr<nsHostRecord> rec(aRec);
946 mLock.AssertCurrentThreadOwns();
948 RefPtr<AddrHostRecord> addrRec;
949 RefPtr<TypeHostRecord> typeRec;
951 if (rec->IsAddrRecord()) {
952 addrRec = do_QueryObject(rec);
953 MOZ_ASSERT(addrRec);
954 } else {
955 typeRec = do_QueryObject(rec);
956 MOZ_ASSERT(typeRec);
959 MOZ_ASSERT(!rec->mResolving);
961 if (!TRRServiceEnabledForRecord(aRec)) {
962 return NS_ERROR_UNKNOWN_HOST;
965 MaybeRenewHostRecordLocked(rec, aLock);
967 RefPtr<TRRQuery> query = new TRRQuery(this, rec);
968 nsresult rv = query->DispatchLookup(pushedTRR);
969 if (NS_FAILED(rv)) {
970 rec->RecordReason(TRRSkippedReason::TRR_DID_NOT_MAKE_QUERY);
971 return rv;
975 auto lock = rec->mTRRQuery.Lock();
976 MOZ_ASSERT(!lock.ref(), "TRR already in progress");
977 lock.ref() = query;
980 rec->mResolving++;
981 rec->mTrrAttempts++;
982 rec->StoreNative(false);
983 return NS_OK;
986 nsresult nsHostResolver::NativeLookup(nsHostRecord* aRec,
987 const MutexAutoLock& aLock) {
988 if (StaticPrefs::network_dns_disabled()) {
989 return NS_ERROR_UNKNOWN_HOST;
991 LOG(("NativeLookup host:%s af:%" PRId16, aRec->host.get(), aRec->af));
993 // If this is not a A/AAAA request, make sure native HTTPS is enabled.
994 MOZ_ASSERT(aRec->IsAddrRecord() || IsNativeHTTPSEnabled());
995 mLock.AssertCurrentThreadOwns();
997 RefPtr<nsHostRecord> rec(aRec);
999 rec->mNativeStart = TimeStamp::Now();
1001 // Add rec to one of the pending queues, possibly removing it from mEvictionQ.
1002 MaybeRenewHostRecordLocked(aRec, aLock);
1004 mQueue.InsertRecord(rec, rec->flags, aLock);
1006 rec->StoreNative(true);
1007 rec->StoreNativeUsed(true);
1008 rec->mResolving++;
1010 nsresult rv = ConditionallyCreateThread(rec);
1012 LOG((" DNS thread counters: total=%d any-live=%d idle=%d pending=%d\n",
1013 static_cast<uint32_t>(mActiveTaskCount),
1014 static_cast<uint32_t>(mActiveAnyThreadCount),
1015 static_cast<uint32_t>(mNumIdleTasks), mQueue.PendingCount()));
1017 return rv;
1020 // static
1021 nsIDNSService::ResolverMode nsHostResolver::Mode() {
1022 if (TRRService::Get()) {
1023 return TRRService::Get()->Mode();
1026 // If we don't have a TRR service just return MODE_TRROFF so we don't make
1027 // any TRR requests by mistake.
1028 return nsIDNSService::MODE_TRROFF;
1031 nsIRequest::TRRMode nsHostRecord::TRRMode() {
1032 return nsIDNSService::GetTRRModeFromFlags(flags);
1035 // static
1036 void nsHostResolver::ComputeEffectiveTRRMode(nsHostRecord* aRec) {
1037 nsIDNSService::ResolverMode resolverMode = nsHostResolver::Mode();
1038 nsIRequest::TRRMode requestMode = aRec->TRRMode();
1040 // For domains that are excluded from TRR or when parental control is enabled,
1041 // we fallback to NativeLookup. This happens even in MODE_TRRONLY. By default
1042 // localhost and local are excluded (so we cover *.local hosts) See the
1043 // network.trr.excluded-domains pref.
1045 if (!TRRService::Get()) {
1046 aRec->RecordReason(TRRSkippedReason::TRR_NO_GSERVICE);
1047 aRec->mEffectiveTRRMode = requestMode;
1048 return;
1051 if (!aRec->mTrrServer.IsEmpty()) {
1052 aRec->mEffectiveTRRMode = nsIRequest::TRR_ONLY_MODE;
1053 return;
1056 if (TRRService::Get()->IsExcludedFromTRR(aRec->host)) {
1057 aRec->RecordReason(TRRSkippedReason::TRR_EXCLUDED);
1058 aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
1059 return;
1062 if (TRRService::Get()->ParentalControlEnabled()) {
1063 aRec->RecordReason(TRRSkippedReason::TRR_PARENTAL_CONTROL);
1064 aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
1065 return;
1068 if (resolverMode == nsIDNSService::MODE_TRROFF) {
1069 aRec->RecordReason(TRRSkippedReason::TRR_OFF_EXPLICIT);
1070 aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
1071 return;
1074 if (requestMode == nsIRequest::TRR_DISABLED_MODE) {
1075 aRec->RecordReason(TRRSkippedReason::TRR_REQ_MODE_DISABLED);
1076 aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
1077 return;
1080 if ((requestMode == nsIRequest::TRR_DEFAULT_MODE &&
1081 resolverMode == nsIDNSService::MODE_NATIVEONLY)) {
1082 if (StaticPrefs::network_trr_display_fallback_warning()) {
1083 TRRSkippedReason heuristicResult =
1084 TRRService::Get()->GetHeuristicDetectionResult();
1085 if (heuristicResult != TRRSkippedReason::TRR_UNSET &&
1086 heuristicResult != TRRSkippedReason::TRR_OK) {
1087 aRec->RecordReason(heuristicResult);
1088 aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
1089 return;
1092 aRec->RecordReason(TRRSkippedReason::TRR_MODE_NOT_ENABLED);
1093 aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
1094 return;
1097 if (requestMode == nsIRequest::TRR_DEFAULT_MODE &&
1098 resolverMode == nsIDNSService::MODE_TRRFIRST) {
1099 aRec->mEffectiveTRRMode = nsIRequest::TRR_FIRST_MODE;
1100 return;
1103 if (requestMode == nsIRequest::TRR_DEFAULT_MODE &&
1104 resolverMode == nsIDNSService::MODE_TRRONLY) {
1105 aRec->mEffectiveTRRMode = nsIRequest::TRR_ONLY_MODE;
1106 return;
1109 aRec->mEffectiveTRRMode = requestMode;
1112 // Kick-off a name resolve operation, using native resolver and/or TRR
1113 nsresult nsHostResolver::NameLookup(nsHostRecord* rec,
1114 const mozilla::MutexAutoLock& aLock) {
1115 LOG(("NameLookup host:%s af:%" PRId16, rec->host.get(), rec->af));
1116 mLock.AssertCurrentThreadOwns();
1118 if (rec->flags & RES_IP_HINT) {
1119 LOG(("Skip lookup if RES_IP_HINT is set\n"));
1120 return NS_ERROR_UNKNOWN_HOST;
1123 nsresult rv = NS_ERROR_UNKNOWN_HOST;
1124 if (rec->mResolving) {
1125 LOG(("NameLookup %s while already resolving\n", rec->host.get()));
1126 return NS_OK;
1129 // Make sure we reset the reason each time we attempt to do a new lookup
1130 // so we don't wrongly report the reason for the previous one.
1131 rec->Reset();
1133 ComputeEffectiveTRRMode(rec);
1135 if (!rec->mTrrServer.IsEmpty()) {
1136 LOG(("NameLookup: %s use trr:%s", rec->host.get(), rec->mTrrServer.get()));
1137 if (rec->mEffectiveTRRMode != nsIRequest::TRR_ONLY_MODE) {
1138 return NS_ERROR_UNKNOWN_HOST;
1141 if (rec->flags & nsIDNSService::RESOLVE_DISABLE_TRR) {
1142 LOG(("TRR with server and DISABLE_TRR flag. Returning error."));
1143 return NS_ERROR_UNKNOWN_HOST;
1145 return TrrLookup(rec, aLock);
1148 LOG(("NameLookup: %s effectiveTRRmode: %d flags: %X", rec->host.get(),
1149 static_cast<nsIRequest::TRRMode>(rec->mEffectiveTRRMode), rec->flags));
1151 if (rec->flags & nsIDNSService::RESOLVE_DISABLE_TRR) {
1152 rec->RecordReason(TRRSkippedReason::TRR_DISABLED_FLAG);
1155 bool serviceNotReady = !TRRServiceEnabledForRecord(rec);
1157 if (rec->mEffectiveTRRMode != nsIRequest::TRR_DISABLED_MODE &&
1158 !((rec->flags & nsIDNSService::RESOLVE_DISABLE_TRR)) &&
1159 !serviceNotReady) {
1160 rv = TrrLookup(rec, aLock);
1163 if (rec->mEffectiveTRRMode == nsIRequest::TRR_DISABLED_MODE ||
1164 (rec->mEffectiveTRRMode == nsIRequest::TRR_FIRST_MODE &&
1165 (rec->flags & nsIDNSService::RESOLVE_DISABLE_TRR || serviceNotReady ||
1166 NS_FAILED(rv)))) {
1167 if (!IsNativeHTTPSEnabled() && !rec->IsAddrRecord()) {
1168 return rv;
1171 #ifdef DEBUG
1172 // If we use this branch then the mTRRUsed flag should not be set
1173 // Even if we did call TrrLookup above, the fact that it failed sync-ly
1174 // means that we didn't actually succeed in opening the channel.
1175 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
1176 MOZ_ASSERT_IF(addrRec, addrRec->mResolverType == DNSResolverType::Native);
1177 #endif
1179 // We did not lookup via TRR - don't fallback to native if the
1180 // network.trr.display_fallback_warning pref is set and either
1181 // 1. we are in TRR first mode and confirmation failed
1182 // 2. the record has trr_disabled and a heuristic skip reason
1183 if (StaticPrefs::network_trr_display_fallback_warning() &&
1184 rec->mEffectiveTRRMode != nsIRequest::TRR_ONLY_MODE) {
1185 if ((rec->mEffectiveTRRMode == nsIRequest::TRR_FIRST_MODE &&
1186 rec->mTRRSkippedReason == TRRSkippedReason::TRR_NOT_CONFIRMED) ||
1187 (rec->mEffectiveTRRMode == nsIRequest::TRR_DISABLED_MODE &&
1188 rec->mTRRSkippedReason >=
1189 nsITRRSkipReason::TRR_HEURISTIC_TRIPPED_GOOGLE_SAFESEARCH &&
1190 rec->mTRRSkippedReason <=
1191 nsITRRSkipReason::TRR_HEURISTIC_TRIPPED_NRPT)) {
1192 LOG((
1193 "NameLookup: ResolveHostComplete with status NS_ERROR_UNKNOWN_HOST "
1194 "for: %s effectiveTRRmode: "
1195 "%d SkippedReason: %d",
1196 rec->host.get(),
1197 static_cast<nsIRequest::TRRMode>(rec->mEffectiveTRRMode),
1198 static_cast<int32_t>(rec->mTRRSkippedReason)));
1200 mozilla::LinkedList<RefPtr<nsResolveHostCallback>> cbs =
1201 std::move(rec->mCallbacks);
1202 for (nsResolveHostCallback* c = cbs.getFirst(); c;
1203 c = c->removeAndGetNext()) {
1204 c->OnResolveHostComplete(this, rec, NS_ERROR_UNKNOWN_HOST);
1207 return NS_OK;
1211 rv = NativeLookup(rec, aLock);
1214 return rv;
1217 nsresult nsHostResolver::ConditionallyRefreshRecord(
1218 nsHostRecord* rec, const nsACString& host, const MutexAutoLock& aLock) {
1219 if ((rec->CheckExpiration(TimeStamp::NowLoRes()) != nsHostRecord::EXP_VALID ||
1220 rec->negative) &&
1221 !rec->mResolving && rec->RefreshForNegativeResponse()) {
1222 LOG((" Using %s cache entry for host [%s] but starting async renewal.",
1223 rec->negative ? "negative" : "positive", host.BeginReading()));
1224 NameLookup(rec, aLock);
1226 if (rec->IsAddrRecord() && !rec->negative) {
1227 // negative entries are constantly being refreshed, only
1228 // track positive grace period induced renewals
1229 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_RENEWAL);
1232 return NS_OK;
1235 bool nsHostResolver::GetHostToLookup(nsHostRecord** result) {
1236 bool timedOut = false;
1237 TimeDuration timeout;
1238 TimeStamp epoch, now;
1240 MutexAutoLock lock(mLock);
1242 timeout = (mNumIdleTasks >= MaxResolverThreadsAnyPriority())
1243 ? mShortIdleTimeout
1244 : mLongIdleTimeout;
1245 epoch = TimeStamp::Now();
1247 while (!mShutdown) {
1248 // remove next record from Q; hand over owning reference. Check high, then
1249 // med, then low
1251 #define SET_GET_TTL(var, val) (var)->StoreGetTtl(sGetTtlEnabled && (val))
1253 RefPtr<nsHostRecord> rec = mQueue.Dequeue(true, lock);
1254 if (rec) {
1255 SET_GET_TTL(rec, false);
1256 rec.forget(result);
1257 return true;
1260 if (mActiveAnyThreadCount < MaxResolverThreadsAnyPriority()) {
1261 rec = mQueue.Dequeue(false, lock);
1262 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
1263 if (addrRec) {
1264 MOZ_ASSERT(IsMediumPriority(addrRec->flags) ||
1265 IsLowPriority(addrRec->flags));
1266 mActiveAnyThreadCount++;
1267 addrRec->StoreUsingAnyThread(true);
1268 SET_GET_TTL(addrRec, true);
1269 addrRec.forget(result);
1270 return true;
1274 // Determining timeout is racy, so allow one cycle through checking the
1275 // queues before exiting.
1276 if (timedOut) {
1277 break;
1280 // wait for one or more of the following to occur:
1281 // (1) the pending queue has a host record to process
1282 // (2) the shutdown flag has been set
1283 // (3) the thread has been idle for too long
1285 mNumIdleTasks++;
1286 mIdleTaskCV.Wait(timeout);
1287 mNumIdleTasks--;
1289 now = TimeStamp::Now();
1291 if (now - epoch >= timeout) {
1292 timedOut = true;
1293 } else {
1294 // It is possible that CondVar::Wait() was interrupted and returned
1295 // early, in which case we will loop back and re-enter it. In that
1296 // case we want to do so with the new timeout reduced to reflect
1297 // time already spent waiting.
1298 timeout -= now - epoch;
1299 epoch = now;
1303 // tell thread to exit...
1304 return false;
1307 void nsHostResolver::PrepareRecordExpirationAddrRecord(
1308 AddrHostRecord* rec) const {
1309 // NOTE: rec->addr_info_lock is already held by parent
1310 MOZ_ASSERT(((bool)rec->addr_info) != rec->negative);
1311 mLock.AssertCurrentThreadOwns();
1312 if (!rec->addr_info) {
1313 rec->SetExpiration(TimeStamp::NowLoRes(), NEGATIVE_RECORD_LIFETIME, 0);
1314 LOG(("Caching host [%s] negative record for %u seconds.\n", rec->host.get(),
1315 NEGATIVE_RECORD_LIFETIME));
1316 return;
1319 unsigned int lifetime = mDefaultCacheLifetime;
1320 unsigned int grace = mDefaultGracePeriod;
1322 unsigned int ttl = mDefaultCacheLifetime;
1323 if (sGetTtlEnabled || rec->addr_info->IsTRR()) {
1324 if (rec->addr_info && rec->addr_info->TTL() != AddrInfo::NO_TTL_DATA) {
1325 ttl = rec->addr_info->TTL();
1327 lifetime = ttl;
1328 grace = 0;
1331 rec->SetExpiration(TimeStamp::NowLoRes(), lifetime, grace);
1332 LOG(("Caching host [%s] record for %u seconds (grace %d).", rec->host.get(),
1333 lifetime, grace));
1336 static bool different_rrset(AddrInfo* rrset1, AddrInfo* rrset2) {
1337 if (!rrset1 || !rrset2) {
1338 return true;
1341 LOG(("different_rrset %s\n", rrset1->Hostname().get()));
1343 if (rrset1->ResolverType() != rrset2->ResolverType()) {
1344 return true;
1347 if (rrset1->TRRType() != rrset2->TRRType()) {
1348 return true;
1351 if (rrset1->Addresses().Length() != rrset2->Addresses().Length()) {
1352 LOG(("different_rrset true due to length change\n"));
1353 return true;
1356 nsTArray<NetAddr> orderedSet1 = rrset1->Addresses().Clone();
1357 nsTArray<NetAddr> orderedSet2 = rrset2->Addresses().Clone();
1358 orderedSet1.Sort();
1359 orderedSet2.Sort();
1361 bool eq = orderedSet1 == orderedSet2;
1362 if (!eq) {
1363 LOG(("different_rrset true due to content change\n"));
1364 } else {
1365 LOG(("different_rrset false\n"));
1367 return !eq;
1370 void nsHostResolver::AddToEvictionQ(nsHostRecord* rec,
1371 const MutexAutoLock& aLock) {
1372 mQueue.AddToEvictionQ(rec, mMaxCacheEntries, mRecordDB, aLock);
1375 // After a first lookup attempt with TRR in mode 2, we may:
1376 // - If network.trr.retry_on_recoverable_errors is false, retry with native.
1377 // - If network.trr.retry_on_recoverable_errors is true:
1378 // - Retry with native if the first attempt failed because we got NXDOMAIN, an
1379 // unreachable address (TRR_DISABLED_FLAG), or we skipped TRR because
1380 // Confirmation failed.
1381 // - Trigger a "RetryTRR" Confirmation which will start a fresh
1382 // connection for TRR, and then retry the lookup with TRR.
1383 // - If the second attempt failed, fallback to native if
1384 // network.trr.strict_native_fallback is false.
1385 // Returns true if we retried with either TRR or Native.
1386 bool nsHostResolver::MaybeRetryTRRLookup(
1387 AddrHostRecord* aAddrRec, nsresult aFirstAttemptStatus,
1388 TRRSkippedReason aFirstAttemptSkipReason, nsresult aChannelStatus,
1389 const MutexAutoLock& aLock) {
1390 if (NS_FAILED(aFirstAttemptStatus) &&
1391 (aChannelStatus == NS_ERROR_PROXY_UNAUTHORIZED ||
1392 aChannelStatus == NS_ERROR_PROXY_AUTHENTICATION_FAILED) &&
1393 aAddrRec->mEffectiveTRRMode == nsIRequest::TRR_ONLY_MODE) {
1394 LOG(("MaybeRetryTRRLookup retry because of proxy connect failed"));
1395 TRRService::Get()->DontUseTRRThread();
1396 return DoRetryTRR(aAddrRec, aLock);
1399 if (NS_SUCCEEDED(aFirstAttemptStatus) ||
1400 aAddrRec->mEffectiveTRRMode != nsIRequest::TRR_FIRST_MODE ||
1401 aFirstAttemptStatus == NS_ERROR_DEFINITIVE_UNKNOWN_HOST) {
1402 return false;
1405 MOZ_ASSERT(!aAddrRec->mResolving);
1406 if (!StaticPrefs::network_trr_retry_on_recoverable_errors()) {
1407 LOG(("nsHostResolver::MaybeRetryTRRLookup retrying with native"));
1408 return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
1411 if (IsFailedConfirmationOrNoConnectivity(aFirstAttemptSkipReason) ||
1412 IsNonRecoverableTRRSkipReason(aFirstAttemptSkipReason) ||
1413 IsBlockedTRRRequest(aFirstAttemptSkipReason)) {
1414 LOG(
1415 ("nsHostResolver::MaybeRetryTRRLookup retrying with native in strict "
1416 "mode, skip reason was %d",
1417 static_cast<uint32_t>(aFirstAttemptSkipReason)));
1418 return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
1421 if (aAddrRec->mTrrAttempts > 1) {
1422 if (!StaticPrefs::network_trr_strict_native_fallback()) {
1423 LOG(
1424 ("nsHostResolver::MaybeRetryTRRLookup retry failed. Using "
1425 "native."));
1426 return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
1429 if (aFirstAttemptSkipReason == TRRSkippedReason::TRR_TIMEOUT &&
1430 StaticPrefs::network_trr_strict_native_fallback_allow_timeouts()) {
1431 LOG(
1432 ("nsHostResolver::MaybeRetryTRRLookup retry timed out. Using "
1433 "native."));
1434 return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
1436 LOG(("nsHostResolver::MaybeRetryTRRLookup mTrrAttempts>1, not retrying."));
1437 return false;
1440 LOG(
1441 ("nsHostResolver::MaybeRetryTRRLookup triggering Confirmation and "
1442 "retrying with TRR, skip reason was %d",
1443 static_cast<uint32_t>(aFirstAttemptSkipReason)));
1444 TRRService::Get()->RetryTRRConfirm();
1446 return DoRetryTRR(aAddrRec, aLock);
1449 bool nsHostResolver::DoRetryTRR(AddrHostRecord* aAddrRec,
1450 const mozilla::MutexAutoLock& aLock) {
1452 // Clear out the old query
1453 auto trrQuery = aAddrRec->mTRRQuery.Lock();
1454 trrQuery.ref() = nullptr;
1457 if (NS_SUCCEEDED(TrrLookup(aAddrRec, aLock, nullptr /* pushedTRR */))) {
1458 aAddrRec->NotifyRetryingTrr();
1459 return true;
1462 return false;
1466 // CompleteLookup() checks if the resolving should be redone and if so it
1467 // returns LOOKUP_RESOLVEAGAIN, but only if 'status' is not NS_ERROR_ABORT.
1468 nsHostResolver::LookupStatus nsHostResolver::CompleteLookup(
1469 nsHostRecord* rec, nsresult status, AddrInfo* aNewRRSet, bool pb,
1470 const nsACString& aOriginsuffix, TRRSkippedReason aReason,
1471 mozilla::net::TRR* aTRRRequest) {
1472 MutexAutoLock lock(mLock);
1473 return CompleteLookupLocked(rec, status, aNewRRSet, pb, aOriginsuffix,
1474 aReason, aTRRRequest, lock);
1477 nsHostResolver::LookupStatus nsHostResolver::CompleteLookupLocked(
1478 nsHostRecord* rec, nsresult status, AddrInfo* aNewRRSet, bool pb,
1479 const nsACString& aOriginsuffix, TRRSkippedReason aReason,
1480 mozilla::net::TRR* aTRRRequest, const mozilla::MutexAutoLock& aLock) {
1481 MOZ_ASSERT(rec);
1482 MOZ_ASSERT(rec->pb == pb);
1483 MOZ_ASSERT(rec->IsAddrRecord());
1485 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
1486 MOZ_ASSERT(addrRec);
1488 RefPtr<AddrInfo> newRRSet(aNewRRSet);
1489 MOZ_ASSERT(NS_FAILED(status) || newRRSet->Addresses().Length() > 0);
1491 DNSResolverType type =
1492 newRRSet ? newRRSet->ResolverType() : DNSResolverType::Native;
1494 if (NS_FAILED(status)) {
1495 newRRSet = nullptr;
1498 if (addrRec->LoadResolveAgain() && (status != NS_ERROR_ABORT) &&
1499 type == DNSResolverType::Native) {
1500 LOG(("nsHostResolver record %p resolve again due to flushcache\n",
1501 addrRec.get()));
1502 addrRec->StoreResolveAgain(false);
1503 return LOOKUP_RESOLVEAGAIN;
1506 MOZ_ASSERT(addrRec->mResolving);
1507 addrRec->mResolving--;
1508 LOG((
1509 "nsHostResolver::CompleteLookup %s %p %X resolver=%d stillResolving=%d\n",
1510 addrRec->host.get(), aNewRRSet, (unsigned int)status, (int)type,
1511 int(addrRec->mResolving)));
1513 if (type != DNSResolverType::Native) {
1514 if (NS_FAILED(status) && status != NS_ERROR_UNKNOWN_HOST &&
1515 status != NS_ERROR_DEFINITIVE_UNKNOWN_HOST) {
1516 // the errors are not failed resolves, that means
1517 // something else failed, consider this as *TRR not used*
1518 // for actually trying to resolve the host
1519 addrRec->mResolverType = DNSResolverType::Native;
1522 if (NS_FAILED(status)) {
1523 if (aReason != TRRSkippedReason::TRR_UNSET) {
1524 addrRec->RecordReason(aReason);
1525 } else {
1526 // Unknown failed reason.
1527 addrRec->RecordReason(TRRSkippedReason::TRR_FAILED);
1529 } else {
1530 addrRec->mTRRSuccess = true;
1531 addrRec->RecordReason(TRRSkippedReason::TRR_OK);
1534 nsresult channelStatus = aTRRRequest->ChannelStatus();
1535 if (MaybeRetryTRRLookup(addrRec, status, aReason, channelStatus, aLock)) {
1536 MOZ_ASSERT(addrRec->mResolving);
1537 return LOOKUP_OK;
1540 if (!addrRec->mTRRSuccess) {
1541 // no TRR success
1542 newRRSet = nullptr;
1545 if (NS_FAILED(status)) {
1546 // This is the error that consumers expect.
1547 status = NS_ERROR_UNKNOWN_HOST;
1549 } else { // native resolve completed
1550 if (addrRec->LoadUsingAnyThread()) {
1551 mActiveAnyThreadCount--;
1552 addrRec->StoreUsingAnyThread(false);
1555 addrRec->mNativeSuccess = static_cast<bool>(newRRSet);
1556 if (addrRec->mNativeSuccess) {
1557 addrRec->mNativeDuration = TimeStamp::Now() - addrRec->mNativeStart;
1561 addrRec->OnCompleteLookup();
1563 // update record fields. We might have a addrRec->addr_info already if a
1564 // previous lookup result expired and we're reresolving it or we get
1565 // a late second TRR response.
1566 if (!mShutdown) {
1567 MutexAutoLock lock(addrRec->addr_info_lock);
1568 RefPtr<AddrInfo> old_addr_info;
1569 if (different_rrset(addrRec->addr_info, newRRSet)) {
1570 LOG(("nsHostResolver record %p new gencnt\n", addrRec.get()));
1571 old_addr_info = addrRec->addr_info;
1572 addrRec->addr_info = std::move(newRRSet);
1573 addrRec->addr_info_gencnt++;
1574 } else {
1575 if (addrRec->addr_info && newRRSet) {
1576 auto builder = addrRec->addr_info->Build();
1577 builder.SetTTL(newRRSet->TTL());
1578 // Update trr timings
1579 builder.SetTrrFetchDuration(newRRSet->GetTrrFetchDuration());
1580 builder.SetTrrFetchDurationNetworkOnly(
1581 newRRSet->GetTrrFetchDurationNetworkOnly());
1583 addrRec->addr_info = builder.Finish();
1585 old_addr_info = std::move(newRRSet);
1587 addrRec->negative = !addrRec->addr_info;
1588 PrepareRecordExpirationAddrRecord(addrRec);
1591 if (LOG_ENABLED()) {
1592 MutexAutoLock lock(addrRec->addr_info_lock);
1593 if (addrRec->addr_info) {
1594 for (const auto& elem : addrRec->addr_info->Addresses()) {
1595 char buf[128];
1596 elem.ToStringBuffer(buf, sizeof(buf));
1597 LOG(("CompleteLookup: %s has %s\n", addrRec->host.get(), buf));
1599 } else {
1600 LOG(("CompleteLookup: %s has NO address\n", addrRec->host.get()));
1604 // get the list of pending callbacks for this lookup, and notify
1605 // them that the lookup is complete.
1606 mozilla::LinkedList<RefPtr<nsResolveHostCallback>> cbs =
1607 std::move(rec->mCallbacks);
1609 LOG(("nsHostResolver record %p calling back dns users status:%X\n",
1610 addrRec.get(), int(status)));
1612 for (nsResolveHostCallback* c = cbs.getFirst(); c;
1613 c = c->removeAndGetNext()) {
1614 c->OnResolveHostComplete(this, rec, status);
1617 OnResolveComplete(rec, aLock);
1619 #ifdef DNSQUERY_AVAILABLE
1620 // Unless the result is from TRR, resolve again to get TTL
1621 bool hasNativeResult = false;
1623 MutexAutoLock lock(addrRec->addr_info_lock);
1624 if (addrRec->addr_info && !addrRec->addr_info->IsTRR()) {
1625 hasNativeResult = true;
1628 if (hasNativeResult && !mShutdown && !addrRec->LoadGetTtl() &&
1629 !rec->mResolving && sGetTtlEnabled) {
1630 LOG(("Issuing second async lookup for TTL for host [%s].",
1631 addrRec->host.get()));
1632 addrRec->flags =
1633 (addrRec->flags & ~nsIDNSService::RESOLVE_PRIORITY_MEDIUM) |
1634 nsIDNSService::RESOLVE_PRIORITY_LOW;
1635 DebugOnly<nsresult> rv = NativeLookup(rec, aLock);
1636 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
1637 "Could not issue second async lookup for TTL.");
1639 #endif
1640 return LOOKUP_OK;
1643 nsHostResolver::LookupStatus nsHostResolver::CompleteLookupByType(
1644 nsHostRecord* rec, nsresult status,
1645 mozilla::net::TypeRecordResultType& aResult, TRRSkippedReason aReason,
1646 uint32_t aTtl, bool pb) {
1647 MutexAutoLock lock(mLock);
1648 return CompleteLookupByTypeLocked(rec, status, aResult, aReason, aTtl, pb,
1649 lock);
1652 nsHostResolver::LookupStatus nsHostResolver::CompleteLookupByTypeLocked(
1653 nsHostRecord* rec, nsresult status,
1654 mozilla::net::TypeRecordResultType& aResult, TRRSkippedReason aReason,
1655 uint32_t aTtl, bool pb, const mozilla::MutexAutoLock& aLock) {
1656 MOZ_ASSERT(rec);
1657 MOZ_ASSERT(rec->pb == pb);
1658 MOZ_ASSERT(!rec->IsAddrRecord());
1660 RefPtr<TypeHostRecord> typeRec = do_QueryObject(rec);
1661 MOZ_ASSERT(typeRec);
1663 MOZ_ASSERT(typeRec->mResolving);
1664 typeRec->mResolving--;
1666 if (NS_FAILED(status)) {
1667 LOG(("nsHostResolver::CompleteLookupByType record %p [%s] status %x\n",
1668 typeRec.get(), typeRec->host.get(), (unsigned int)status));
1669 typeRec->SetExpiration(
1670 TimeStamp::NowLoRes(),
1671 StaticPrefs::network_dns_negative_ttl_for_type_record(), 0);
1672 MOZ_ASSERT(aResult.is<TypeRecordEmpty>());
1673 status = NS_ERROR_UNKNOWN_HOST;
1674 typeRec->negative = true;
1675 if (aReason != TRRSkippedReason::TRR_UNSET) {
1676 typeRec->RecordReason(aReason);
1677 } else {
1678 // Unknown failed reason.
1679 typeRec->RecordReason(TRRSkippedReason::TRR_FAILED);
1681 } else {
1682 size_t recordCount = 0;
1683 if (aResult.is<TypeRecordTxt>()) {
1684 recordCount = aResult.as<TypeRecordTxt>().Length();
1685 } else if (aResult.is<TypeRecordHTTPSSVC>()) {
1686 recordCount = aResult.as<TypeRecordHTTPSSVC>().Length();
1688 LOG(
1689 ("nsHostResolver::CompleteLookupByType record %p [%s], number of "
1690 "records %zu\n",
1691 typeRec.get(), typeRec->host.get(), recordCount));
1692 MutexAutoLock typeLock(typeRec->mResultsLock);
1693 typeRec->mResults = aResult;
1694 typeRec->SetExpiration(TimeStamp::NowLoRes(), aTtl, mDefaultGracePeriod);
1695 typeRec->negative = false;
1696 typeRec->mTRRSuccess = !rec->LoadNative();
1697 typeRec->mNativeSuccess = rec->LoadNative();
1698 MOZ_ASSERT(aReason != TRRSkippedReason::TRR_UNSET);
1699 typeRec->RecordReason(aReason);
1702 mozilla::LinkedList<RefPtr<nsResolveHostCallback>> cbs =
1703 std::move(typeRec->mCallbacks);
1705 LOG(
1706 ("nsHostResolver::CompleteLookupByType record %p calling back dns "
1707 "users\n",
1708 typeRec.get()));
1710 for (nsResolveHostCallback* c = cbs.getFirst(); c;
1711 c = c->removeAndGetNext()) {
1712 c->OnResolveHostComplete(this, rec, status);
1715 OnResolveComplete(rec, aLock);
1717 return LOOKUP_OK;
1720 void nsHostResolver::OnResolveComplete(nsHostRecord* aRec,
1721 const mozilla::MutexAutoLock& aLock) {
1722 if (!aRec->mResolving && !mShutdown) {
1724 auto trrQuery = aRec->mTRRQuery.Lock();
1725 if (trrQuery.ref()) {
1726 aRec->mTrrDuration = trrQuery.ref()->Duration();
1728 trrQuery.ref() = nullptr;
1730 aRec->ResolveComplete();
1732 AddToEvictionQ(aRec, aLock);
1736 void nsHostResolver::CancelAsyncRequest(
1737 const nsACString& host, const nsACString& aTrrServer, uint16_t aType,
1738 const OriginAttributes& aOriginAttributes, nsIDNSService::DNSFlags flags,
1739 uint16_t af, nsIDNSListener* aListener, nsresult status)
1742 MutexAutoLock lock(mLock);
1744 nsAutoCString originSuffix;
1745 aOriginAttributes.CreateSuffix(originSuffix);
1747 // Lookup the host record associated with host, flags & address family
1749 nsHostKey key(host, aTrrServer, aType, flags, af,
1750 (aOriginAttributes.mPrivateBrowsingId > 0), originSuffix);
1751 RefPtr<nsHostRecord> rec = mRecordDB.Get(key);
1752 if (!rec) {
1753 return;
1756 for (RefPtr<nsResolveHostCallback> c : rec->mCallbacks) {
1757 if (c->EqualsAsyncListener(aListener)) {
1758 c->remove();
1759 c->OnResolveHostComplete(this, rec.get(), status);
1760 break;
1764 // If there are no more callbacks, remove the hash table entry
1765 if (rec->mCallbacks.isEmpty()) {
1766 mRecordDB.Remove(*static_cast<nsHostKey*>(rec.get()));
1767 // If record is on a Queue, remove it
1768 mQueue.MaybeRemoveFromQ(rec, lock);
1772 size_t nsHostResolver::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const {
1773 MutexAutoLock lock(mLock);
1775 size_t n = mallocSizeOf(this);
1777 n += mRecordDB.ShallowSizeOfExcludingThis(mallocSizeOf);
1778 for (const auto& entry : mRecordDB.Values()) {
1779 n += entry->SizeOfIncludingThis(mallocSizeOf);
1782 // The following fields aren't measured.
1783 // - mHighQ, mMediumQ, mLowQ, mEvictionQ, because they just point to
1784 // nsHostRecords that also pointed to by entries |mRecordDB|, and
1785 // measured when |mRecordDB| is measured.
1787 return n;
1790 void nsHostResolver::ThreadFunc() {
1791 LOG(("DNS lookup thread - starting execution.\n"));
1793 #if defined(RES_RETRY_ON_FAILURE)
1794 nsResState rs;
1795 #endif
1796 RefPtr<nsHostRecord> rec;
1797 RefPtr<AddrInfo> ai;
1799 do {
1800 if (!rec) {
1801 RefPtr<nsHostRecord> tmpRec;
1802 if (!GetHostToLookup(getter_AddRefs(tmpRec))) {
1803 break; // thread shutdown signal
1805 // GetHostToLookup() returns an owning reference
1806 MOZ_ASSERT(tmpRec);
1807 rec.swap(tmpRec);
1810 LOG1(("DNS lookup thread - Calling getaddrinfo for host [%s].\n",
1811 rec->host.get()));
1813 TimeStamp startTime = TimeStamp::Now();
1814 bool getTtl = rec->LoadGetTtl();
1815 TimeDuration inQueue = startTime - rec->mNativeStart;
1816 uint32_t ms = static_cast<uint32_t>(inQueue.ToMilliseconds());
1817 Telemetry::Accumulate(Telemetry::DNS_NATIVE_QUEUING, ms);
1819 if (!rec->IsAddrRecord()) {
1820 LOG(("byType on DNS thread"));
1821 TypeRecordResultType result = AsVariant(mozilla::Nothing());
1822 uint32_t ttl = UINT32_MAX;
1823 nsresult status = ResolveHTTPSRecord(rec->host, rec->flags, result, ttl);
1824 CompleteLookupByType(rec, status, result, rec->mTRRSkippedReason, ttl,
1825 rec->pb);
1826 rec = nullptr;
1827 continue;
1830 nsresult status =
1831 GetAddrInfo(rec->host, rec->af, rec->flags, getter_AddRefs(ai), getTtl);
1832 #if defined(RES_RETRY_ON_FAILURE)
1833 if (NS_FAILED(status) && rs.Reset()) {
1834 status = GetAddrInfo(rec->host, rec->af, rec->flags, getter_AddRefs(ai),
1835 getTtl);
1837 #endif
1839 if (RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec)) {
1840 // obtain lock to check shutdown and manage inter-module telemetry
1841 MutexAutoLock lock(mLock);
1843 if (!mShutdown) {
1844 TimeDuration elapsed = TimeStamp::Now() - startTime;
1845 if (NS_SUCCEEDED(status)) {
1846 if (!addrRec->addr_info_gencnt) {
1847 // Time for initial lookup.
1848 glean::networking::dns_lookup_time.AccumulateRawDuration(elapsed);
1849 } else if (!getTtl) {
1850 // Time for renewal; categorized by expiration strategy.
1851 glean::networking::dns_renewal_time.AccumulateRawDuration(elapsed);
1852 } else {
1853 // Time to get TTL; categorized by expiration strategy.
1854 glean::networking::dns_renewal_time_for_ttl.AccumulateRawDuration(
1855 elapsed);
1857 } else {
1858 glean::networking::dns_failed_lookup_time.AccumulateRawDuration(
1859 elapsed);
1864 LOG1(("DNS lookup thread - lookup completed for host [%s]: %s.\n",
1865 rec->host.get(), ai ? "success" : "failure: unknown host"));
1867 if (LOOKUP_RESOLVEAGAIN ==
1868 CompleteLookup(rec, status, ai, rec->pb, rec->originSuffix,
1869 rec->mTRRSkippedReason, nullptr)) {
1870 // leave 'rec' assigned and loop to make a renewed host resolve
1871 LOG(("DNS lookup thread - Re-resolving host [%s].\n", rec->host.get()));
1872 } else {
1873 rec = nullptr;
1875 } while (true);
1877 MutexAutoLock lock(mLock);
1878 mActiveTaskCount--;
1879 LOG(("DNS lookup thread - queue empty, task finished.\n"));
1882 void nsHostResolver::SetCacheLimits(uint32_t aMaxCacheEntries,
1883 uint32_t aDefaultCacheEntryLifetime,
1884 uint32_t aDefaultGracePeriod) {
1885 MutexAutoLock lock(mLock);
1886 mMaxCacheEntries = aMaxCacheEntries;
1887 mDefaultCacheLifetime = aDefaultCacheEntryLifetime;
1888 mDefaultGracePeriod = aDefaultGracePeriod;
1891 nsresult nsHostResolver::Create(uint32_t maxCacheEntries,
1892 uint32_t defaultCacheEntryLifetime,
1893 uint32_t defaultGracePeriod,
1894 nsHostResolver** result) {
1895 RefPtr<nsHostResolver> res = new nsHostResolver(
1896 maxCacheEntries, defaultCacheEntryLifetime, defaultGracePeriod);
1898 nsresult rv = res->Init();
1899 if (NS_FAILED(rv)) {
1900 return rv;
1903 res.forget(result);
1904 return NS_OK;
1907 void nsHostResolver::GetDNSCacheEntries(nsTArray<DNSCacheEntries>* args) {
1908 MutexAutoLock lock(mLock);
1909 for (const auto& recordEntry : mRecordDB) {
1910 // We don't pay attention to address literals, only resolved domains.
1911 // Also require a host.
1912 nsHostRecord* rec = recordEntry.GetWeak();
1913 MOZ_ASSERT(rec, "rec should never be null here!");
1915 if (!rec) {
1916 continue;
1919 // For now we only show A/AAAA records.
1920 if (!rec->IsAddrRecord()) {
1921 continue;
1924 RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
1925 MOZ_ASSERT(addrRec);
1926 if (!addrRec || !addrRec->addr_info) {
1927 continue;
1930 DNSCacheEntries info;
1931 info.hostname = rec->host;
1932 info.family = rec->af;
1933 info.expiration =
1934 (int64_t)(rec->mValidEnd - TimeStamp::NowLoRes()).ToSeconds();
1935 if (info.expiration <= 0) {
1936 // We only need valid DNS cache entries
1937 continue;
1941 MutexAutoLock lock(addrRec->addr_info_lock);
1942 for (const auto& addr : addrRec->addr_info->Addresses()) {
1943 char buf[kIPv6CStrBufSize];
1944 if (addr.ToStringBuffer(buf, sizeof(buf))) {
1945 info.hostaddr.AppendElement(buf);
1948 info.TRR = addrRec->addr_info->IsTRR();
1951 info.originAttributesSuffix = recordEntry.GetKey().originSuffix;
1952 info.flags = nsPrintfCString("%u|0x%x|%u|%d|%s", rec->type, rec->flags,
1953 rec->af, rec->pb, rec->mTrrServer.get());
1955 args->AppendElement(std::move(info));
1959 #undef LOG
1960 #undef LOG_ENABLED