2018-11-30 18:39:55 +03:00
|
|
|
/* vim:set ts=4 sw=2 sts=2 et cin: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-09-12 00:32:33 +04:00
|
|
|
|
|
|
|
#if defined(HAVE_RES_NINIT)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <netinet/in.h>
|
2017-02-09 13:21:38 +03:00
|
|
|
# include <arpa/inet.h>
|
2003-09-12 00:32:33 +04:00
|
|
|
# include <arpa/nameser.h>
|
|
|
|
# include <resolv.h>
|
|
|
|
# define RES_RETRY_ON_FAILURE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-09-21 02:10:18 +04:00
|
|
|
#include <ctime>
|
2003-09-12 00:32:33 +04:00
|
|
|
#include "nsHostResolver.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2003-09-12 00:32:33 +04:00
|
|
|
#include "nsISupportsBase.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
2018-06-27 22:29:12 +03:00
|
|
|
#include "nsIThreadManager.h"
|
2018-07-04 21:36:58 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2021-01-28 13:50:35 +03:00
|
|
|
#include "nsNetUtil.h"
|
2015-02-19 08:14:00 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2018-07-04 21:36:58 +03:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
2003-09-12 00:32:33 +04:00
|
|
|
#include "prthread.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "prtime.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2003-09-12 00:32:33 +04:00
|
|
|
#include "plstr.h"
|
2018-11-19 18:23:39 +03:00
|
|
|
#include "nsQueryObject.h"
|
2005-09-23 01:38:12 +04:00
|
|
|
#include "nsURLHelper.h"
|
2012-06-12 21:06:20 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2018-09-15 11:10:21 +03:00
|
|
|
#include "nsThreadPool.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
#include "GetAddrInfo.h"
|
2018-02-01 12:20:49 +03:00
|
|
|
#include "TRR.h"
|
2020-11-03 13:41:51 +03:00
|
|
|
#include "TRRQuery.h"
|
2018-02-01 12:20:49 +03:00
|
|
|
#include "TRRService.h"
|
2021-01-26 17:19:09 +03:00
|
|
|
#include "ODoHService.h"
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2011-12-17 06:24:17 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2021-06-16 11:14:10 +03:00
|
|
|
// Put DNSLogging.h at the end to avoid LOG being overwritten by other headers.
|
|
|
|
#include "DNSLogging.h"
|
2010-05-20 03:22:19 +04:00
|
|
|
|
2021-10-14 15:57:58 +03:00
|
|
|
#define IS_ADDR_TYPE(_type) ((_type) == nsIDNSService::RESOLVE_TYPE_DEFAULT)
|
|
|
|
#define IS_OTHER_TYPE(_type) ((_type) != nsIDNSService::RESOLVE_TYPE_DEFAULT)
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
2012-12-10 18:13:55 +04:00
|
|
|
using namespace mozilla::net;
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
// None of our implementations expose a TTL for negative responses, so we use a
|
|
|
|
// constant always.
|
|
|
|
static const unsigned int NEGATIVE_RECORD_LIFETIME = 60;
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
// Use a persistent thread pool in order to avoid spinning up new threads all
|
|
|
|
// the time. In particular, thread creation results in a res_init() call from
|
|
|
|
// libc which is quite expensive.
|
|
|
|
//
|
|
|
|
// The pool dynamically grows between 0 and MAX_RESOLVER_THREADS in size. New
|
|
|
|
// requests go first to an idle thread. If that cannot be found and there are
|
|
|
|
// fewer than MAX_RESOLVER_THREADS currently in the pool a new thread is created
|
|
|
|
// for high priority requests. If the new request is at a lower priority a new
|
|
|
|
// thread will only be created if there are fewer than HighThreadThreshold
|
|
|
|
// currently outstanding. If a thread cannot be created or an idle thread
|
|
|
|
// located for the request it is queued.
|
|
|
|
//
|
|
|
|
// When the pool is greater than HighThreadThreshold in size a thread will be
|
|
|
|
// destroyed after ShortIdleTimeoutSeconds of idle time. Smaller pools use
|
|
|
|
// LongIdleTimeoutSeconds for a timeout period.
|
|
|
|
|
|
|
|
#define HighThreadThreshold MAX_RESOLVER_THREADS_FOR_ANY_PRIORITY
|
|
|
|
#define LongIdleTimeoutSeconds 300 // for threads 1 -> HighThreadThreshold
|
|
|
|
#define ShortIdleTimeoutSeconds \
|
|
|
|
60 // for threads HighThreadThreshold+1 -> MAX_RESOLVER_THREADS
|
|
|
|
|
2016-08-23 18:24:54 +03:00
|
|
|
static_assert(
|
|
|
|
HighThreadThreshold <= MAX_RESOLVER_THREADS,
|
|
|
|
"High Thread Threshold should be less equal Maximum allowed thread");
|
2003-09-12 00:32:33 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
namespace mozilla::net {
|
2018-02-01 12:20:49 +03:00
|
|
|
LazyLogModule gHostResolverLog("nsHostResolver");
|
2020-01-18 16:48:34 +03:00
|
|
|
} // namespace mozilla::net
|
2003-09-12 00:32:33 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
|
|
|
|
|
|
|
// this class represents the resolver state for a given thread. if we
|
|
|
|
// encounter a lookup failure, then we can invoke the Reset method on an
|
|
|
|
// instance of this class to reset the resolver (in case /etc/resolv.conf
|
|
|
|
// for example changed). this is mainly an issue on GNU systems since glibc
|
|
|
|
// only reads in /etc/resolv.conf once per thread. it may be an issue on
|
|
|
|
// other systems as well.
|
|
|
|
|
|
|
|
class nsResState {
|
|
|
|
public:
|
|
|
|
nsResState()
|
|
|
|
// initialize mLastReset to the time when this object
|
|
|
|
// is created. this means that a reset will not occur
|
|
|
|
// if a thread is too young. the alternative would be
|
|
|
|
// to initialize this to the beginning of time, so that
|
|
|
|
// the first failure would cause a reset, but since the
|
|
|
|
// thread would have just started up, it likely would
|
|
|
|
// already have current /etc/resolv.conf info.
|
|
|
|
: mLastReset(PR_IntervalNow()) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Reset() {
|
2003-09-12 00:32:33 +04:00
|
|
|
// reset no more than once per second
|
2020-09-02 18:39:00 +03:00
|
|
|
if (PR_IntervalToSeconds(PR_IntervalNow() - mLastReset) < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
mLastReset = PR_IntervalNow();
|
2021-08-03 12:15:05 +03:00
|
|
|
auto result = res_ninit(&_res);
|
|
|
|
|
|
|
|
LOG(("nsResState::Reset() > 'res_ninit' returned %d", result));
|
|
|
|
return (result == 0);
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRIntervalTime mLastReset;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RES_RETRY_ON_FAILURE
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2014-10-20 21:26:33 +04:00
|
|
|
static const char kPrefGetTtl[] = "network.dns.get-ttl";
|
2018-02-01 12:20:49 +03:00
|
|
|
static const char kPrefNativeIsLocalhost[] = "network.dns.native-is-localhost";
|
2018-07-04 22:25:28 +03:00
|
|
|
static const char kPrefThreadIdleTime[] =
|
|
|
|
"network.dns.resolver-thread-extra-idle-time-seconds";
|
2014-10-20 21:26:33 +04:00
|
|
|
static bool sGetTtlEnabled = false;
|
2018-02-01 12:20:49 +03:00
|
|
|
mozilla::Atomic<bool, mozilla::Relaxed> gNativeIsLocalhost;
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2019-11-01 05:57:20 +03:00
|
|
|
static void DnsPrefChanged(const char* aPref, void* aSelf) {
|
2014-09-24 20:56:13 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Should be getting pref changed notification on main thread!");
|
|
|
|
|
2018-07-06 22:24:41 +03:00
|
|
|
MOZ_ASSERT(aSelf);
|
2014-09-24 20:56:13 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (!strcmp(aPref, kPrefGetTtl)) {
|
2018-06-26 16:36:26 +03:00
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
2018-02-01 12:20:49 +03:00
|
|
|
sGetTtlEnabled = Preferences::GetBool(kPrefGetTtl);
|
2018-06-26 16:36:26 +03:00
|
|
|
#endif
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (!strcmp(aPref, kPrefNativeIsLocalhost)) {
|
|
|
|
gNativeIsLocalhost = Preferences::GetBool(kPrefNativeIsLocalhost);
|
|
|
|
}
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(nsHostResolver)
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHostResolver::nsHostResolver(uint32_t maxCacheEntries,
|
2014-09-21 02:10:18 +04:00
|
|
|
uint32_t defaultCacheEntryLifetime,
|
|
|
|
uint32_t defaultGracePeriod)
|
2003-10-07 09:11:41 +04:00
|
|
|
: mMaxCacheEntries(maxCacheEntries),
|
2014-09-21 02:10:18 +04:00
|
|
|
mDefaultCacheLifetime(defaultCacheEntryLifetime),
|
|
|
|
mDefaultGracePeriod(defaultGracePeriod),
|
2021-04-07 10:29:54 +03:00
|
|
|
mIdleTaskCV(mLock, "nsHostResolver.mIdleTaskCV") {
|
2003-09-12 00:32:33 +04:00
|
|
|
mCreationTime = PR_Now();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-09 23:17:26 +03:00
|
|
|
mLongIdleTimeout = TimeDuration::FromSeconds(LongIdleTimeoutSeconds);
|
|
|
|
mShortIdleTimeout = TimeDuration::FromSeconds(ShortIdleTimeoutSeconds);
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:44:09 +03:00
|
|
|
nsHostResolver::~nsHostResolver() = default;
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2022-05-13 00:55:01 +03:00
|
|
|
nsresult nsHostResolver::Init() NO_THREAD_SAFETY_ANALYSIS {
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-21 02:10:18 +04:00
|
|
|
if (NS_FAILED(GetAddrInfoInit())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2006-05-25 20:49:48 +04:00
|
|
|
|
2017-08-17 19:16:00 +03:00
|
|
|
LOG(("nsHostResolver::Init this=%p", this));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdown = false;
|
2019-06-05 23:39:12 +03:00
|
|
|
mNCS = NetworkConnectivityService::GetSingleton();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
// The preferences probably haven't been loaded from the disk yet, so we
|
|
|
|
// need to register a callback that will set up the experiment once they
|
|
|
|
// are. We also need to explicitly set a value for the props otherwise the
|
|
|
|
// callback won't be called.
|
|
|
|
{
|
2014-10-20 21:26:33 +04:00
|
|
|
DebugOnly<nsresult> rv = Preferences::RegisterCallbackAndCall(
|
|
|
|
&DnsPrefChanged, kPrefGetTtl, this);
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Could not register DNS TTL pref callback.");
|
2018-02-01 12:20:49 +03:00
|
|
|
rv = Preferences::RegisterCallbackAndCall(&DnsPrefChanged,
|
|
|
|
kPrefNativeIsLocalhost, this);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Could not register DNS pref callback.");
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2006-05-25 20:49:48 +04:00
|
|
|
#if defined(HAVE_RES_NINIT)
|
|
|
|
// We want to make sure the system is using the correct resolver settings,
|
|
|
|
// so we force it to reload those settings whenever we startup a subsequent
|
|
|
|
// nsHostResolver instance. We assume that there is no reason to do this
|
|
|
|
// for the first nsHostResolver instance since that is usually created
|
|
|
|
// during application startup.
|
|
|
|
static int initCount = 0;
|
|
|
|
if (initCount++ > 0) {
|
2021-08-03 12:15:05 +03:00
|
|
|
auto result = res_ninit(&_res);
|
|
|
|
LOG(("nsHostResolver::Init > 'res_ninit' returned %d", result));
|
2006-05-25 20:49:48 +04:00
|
|
|
}
|
|
|
|
#endif
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2018-07-04 22:25:28 +03:00
|
|
|
// We can configure the threadpool to keep threads alive for a while after
|
2018-08-09 19:56:02 +03:00
|
|
|
// the last ThreadFunc task has been executed.
|
2018-07-04 22:25:28 +03:00
|
|
|
int32_t poolTimeoutSecs = Preferences::GetInt(kPrefThreadIdleTime, 60);
|
|
|
|
uint32_t poolTimeoutMs;
|
|
|
|
if (poolTimeoutSecs < 0) {
|
|
|
|
// This means never shut down the idle threads
|
|
|
|
poolTimeoutMs = UINT32_MAX;
|
|
|
|
} else {
|
|
|
|
// We clamp down the idle time between 0 and one hour.
|
|
|
|
poolTimeoutMs =
|
|
|
|
mozilla::clamped<uint32_t>(poolTimeoutSecs * 1000, 0, 3600 * 1000);
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2018-09-15 11:10:21 +03:00
|
|
|
nsCOMPtr<nsIThreadPool> threadPool = new nsThreadPool();
|
2018-07-04 21:36:58 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(threadPool->SetThreadLimit(MAX_RESOLVER_THREADS));
|
2008-11-08 02:00:26 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(threadPool->SetIdleThreadLimit(MAX_RESOLVER_THREADS));
|
2018-07-04 21:36:58 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(threadPool->SetIdleThreadTimeout(poolTimeoutMs));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
threadPool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(threadPool->SetName("DNS Resolver"_ns));
|
2020-02-13 17:38:48 +03:00
|
|
|
mResolverThreads = ToRefPtr(std::move(threadPool));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
void nsHostResolver::ClearPendingQueue(
|
|
|
|
LinkedList<RefPtr<nsHostRecord>>& aPendingQ) {
|
2008-11-08 02:00:26 +03:00
|
|
|
// loop through pending queue, erroring out pending lookups.
|
2018-02-20 19:41:50 +03:00
|
|
|
if (!aPendingQ.isEmpty()) {
|
2020-09-02 18:39:00 +03:00
|
|
|
for (const RefPtr<nsHostRecord>& rec : aPendingQ) {
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->Cancel();
|
2018-09-27 12:28:36 +03:00
|
|
|
if (rec->IsAddrRecord()) {
|
2020-07-11 22:32:05 +03:00
|
|
|
CompleteLookup(rec, NS_ERROR_ABORT, nullptr, rec->pb, rec->originSuffix,
|
2021-02-24 19:37:03 +03:00
|
|
|
rec->mTRRSkippedReason, nullptr);
|
2018-09-22 23:54:11 +03:00
|
|
|
} else {
|
2020-05-04 22:56:51 +03:00
|
|
|
mozilla::net::TypeRecordResultType empty(Nothing{});
|
|
|
|
CompleteLookupByType(rec, NS_ERROR_ABORT, empty, 0, rec->pb);
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
|
2014-09-25 07:14:00 +04:00
|
|
|
//
|
|
|
|
// FlushCache() is what we call when the network has changed. We must not
|
|
|
|
// trust names that were resolved before this change. They may resolve
|
|
|
|
// differently now.
|
|
|
|
//
|
|
|
|
// This function removes all existing resolved host entries from the hash.
|
|
|
|
// Names that are in the pending queues can be left there. Entries in the
|
2020-12-08 19:39:42 +03:00
|
|
|
// cache that have 'Resolve' set true but not 'OnQueue' are being resolved
|
2014-09-25 07:14:00 +04:00
|
|
|
// right now, so we need to mark them to get re-resolved on completion!
|
|
|
|
|
2019-03-21 15:41:39 +03:00
|
|
|
void nsHostResolver::FlushCache(bool aTrrToo) {
|
2015-06-11 03:09:32 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.FlushEvictionQ(mRecordDB, lock);
|
2015-06-11 03:09:32 +03:00
|
|
|
|
|
|
|
// Refresh the cache entries that are resolving RIGHT now, remove the rest.
|
2018-01-08 01:22:01 +03:00
|
|
|
for (auto iter = mRecordDB.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsHostRecord* record = iter.UserData();
|
2015-06-11 04:28:03 +03:00
|
|
|
// Try to remove the record, or mark it for refresh.
|
2018-09-27 12:28:36 +03:00
|
|
|
// By-type records are from TRR. We do not need to flush those entry
|
|
|
|
// when the network has change, because they are not local.
|
|
|
|
if (record->IsAddrRecord()) {
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(record);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(addrRec);
|
2019-03-21 15:41:39 +03:00
|
|
|
if (addrRec->RemoveOrRefresh(aTrrToo)) {
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.MaybeRemoveFromQ(record, lock);
|
2021-04-07 10:29:54 +03:00
|
|
|
LOG(("Removing (%s) Addr record from mRecordDB", record->host.get()));
|
2018-09-27 12:28:36 +03:00
|
|
|
iter.Remove();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2021-04-07 10:29:54 +03:00
|
|
|
} else if (aTrrToo) {
|
|
|
|
// remove by type records
|
|
|
|
LOG(("Removing (%s) type record from mRecordDB", record->host.get()));
|
|
|
|
iter.Remove();
|
2015-06-11 04:28:03 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-09-25 07:14:00 +04:00
|
|
|
}
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
void nsHostResolver::Shutdown() {
|
2012-07-17 02:37:28 +04:00
|
|
|
LOG(("Shutting down host resolver.\n"));
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
{
|
|
|
|
DebugOnly<nsresult> rv =
|
2014-10-20 21:26:33 +04:00
|
|
|
Preferences::UnregisterCallback(&DnsPrefChanged, kPrefGetTtl, this);
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Could not unregister DNS TTL pref callback.");
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
LinkedList<RefPtr<nsHostRecord>> pendingQHigh, pendingQMed, pendingQLow,
|
|
|
|
evictionQ;
|
2006-04-29 08:37:07 +04:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-09-25 07:14:00 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdown = true;
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2020-09-02 18:39:00 +03:00
|
|
|
if (mNumIdleTasks) {
|
|
|
|
mIdleTaskCV.NotifyAll();
|
|
|
|
}
|
2018-08-09 19:56:02 +03:00
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.ClearAll(
|
|
|
|
[&](nsHostRecord* aRec) {
|
2021-12-22 15:46:50 +03:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2021-06-16 11:14:11 +03:00
|
|
|
if (aRec->IsAddrRecord()) {
|
|
|
|
CompleteLookupLocked(aRec, NS_ERROR_ABORT, nullptr, aRec->pb,
|
|
|
|
aRec->originSuffix, aRec->mTRRSkippedReason,
|
|
|
|
nullptr, lock);
|
|
|
|
} else {
|
|
|
|
mozilla::net::TypeRecordResultType empty(Nothing{});
|
|
|
|
CompleteLookupByTypeLocked(aRec, NS_ERROR_ABORT, empty, 0, aRec->pb,
|
|
|
|
lock);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lock);
|
|
|
|
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& data : mRecordDB.Values()) {
|
|
|
|
data->Cancel();
|
2019-02-08 12:34:34 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
// empty host database
|
2018-01-08 01:22:01 +03:00
|
|
|
mRecordDB.Clear();
|
2019-06-05 23:39:12 +03:00
|
|
|
|
|
|
|
mNCS = nullptr;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2014-09-25 07:14:00 +04:00
|
|
|
|
2019-04-12 20:04:05 +03:00
|
|
|
// Shutdown the resolver threads, but with a timeout of 2 seconds (prefable).
|
2018-10-26 21:46:00 +03:00
|
|
|
// If the timeout is exceeded, any stuck threads will be leaked.
|
2019-04-12 20:04:05 +03:00
|
|
|
mResolverThreads->ShutdownWithTimeout(
|
|
|
|
StaticPrefs::network_dns_resolver_shutdown_timeout_ms());
|
2014-09-21 02:10:18 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::DebugOnly<nsresult> rv = GetAddrInfoShutdown();
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to shutdown GetAddrInfo");
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 22:35:34 +03:00
|
|
|
nsresult nsHostResolver::GetHostRecord(const nsACString& host,
|
2020-02-07 12:42:00 +03:00
|
|
|
const nsACString& aTrrServer,
|
|
|
|
uint16_t type, uint16_t flags,
|
|
|
|
uint16_t af, bool pb,
|
2018-02-01 12:20:49 +03:00
|
|
|
const nsCString& originSuffix,
|
|
|
|
nsHostRecord** result) {
|
|
|
|
MutexAutoLock lock(mLock);
|
2020-02-04 22:35:34 +03:00
|
|
|
nsHostKey key(host, aTrrServer, type, flags, af, pb, originSuffix);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2021-02-15 19:37:52 +03:00
|
|
|
RefPtr<nsHostRecord> rec =
|
2021-02-26 12:11:45 +03:00
|
|
|
mRecordDB.LookupOrInsertWith(key, [&] { return InitRecord(key); });
|
2018-09-27 12:28:36 +03:00
|
|
|
if (rec->IsAddrRecord()) {
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
2018-09-27 12:28:36 +03:00
|
|
|
if (addrRec->addr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (rec->mResolving) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2020-02-07 12:42:00 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
*result = rec.forget().take();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
nsHostRecord* nsHostResolver::InitRecord(const nsHostKey& key) {
|
|
|
|
if (IS_ADDR_TYPE(key.type)) {
|
|
|
|
return new AddrHostRecord(key);
|
|
|
|
}
|
|
|
|
return new TypeHostRecord(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsHostRecord> nsHostResolver::InitLoopbackRecord(
|
|
|
|
const nsHostKey& key, nsresult* aRv) {
|
|
|
|
MOZ_ASSERT(aRv);
|
|
|
|
MOZ_ASSERT(IS_ADDR_TYPE(key.type));
|
|
|
|
|
|
|
|
*aRv = NS_ERROR_FAILURE;
|
|
|
|
RefPtr<nsHostRecord> rec = InitRecord(key);
|
|
|
|
|
2020-10-27 16:25:22 +03:00
|
|
|
nsTArray<NetAddr> addresses;
|
2021-05-10 12:26:01 +03:00
|
|
|
NetAddr addr;
|
2020-10-27 16:25:22 +03:00
|
|
|
if (key.af == PR_AF_INET || key.af == PR_AF_UNSPEC) {
|
2021-05-10 12:26:01 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(addr.InitFromString("127.0.0.1"_ns)));
|
|
|
|
addresses.AppendElement(addr);
|
2020-10-27 16:25:22 +03:00
|
|
|
}
|
|
|
|
if (key.af == PR_AF_INET6 || key.af == PR_AF_UNSPEC) {
|
2021-05-10 12:26:01 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(addr.InitFromString("::1"_ns)));
|
|
|
|
addresses.AppendElement(addr);
|
2020-10-22 10:36:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 15:50:02 +03:00
|
|
|
RefPtr<AddrInfo> ai =
|
|
|
|
new AddrInfo(rec->host, DNSResolverType::Native, 0, std::move(addresses));
|
2020-10-22 10:36:15 +03:00
|
|
|
|
2020-10-27 16:25:22 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
|
|
|
MutexAutoLock lock(addrRec->addr_info_lock);
|
2020-10-22 10:36:15 +03:00
|
|
|
addrRec->addr_info = ai;
|
|
|
|
addrRec->SetExpiration(TimeStamp::NowLoRes(), mDefaultCacheLifetime,
|
|
|
|
mDefaultGracePeriod);
|
|
|
|
addrRec->negative = false;
|
|
|
|
|
|
|
|
*aRv = NS_OK;
|
|
|
|
return rec.forget();
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:35:34 +03:00
|
|
|
nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
|
2020-02-07 12:42:00 +03:00
|
|
|
const nsACString& aTrrServer,
|
2022-03-25 15:15:03 +03:00
|
|
|
int32_t aPort, uint16_t type,
|
2017-02-14 07:25:35 +03:00
|
|
|
const OriginAttributes& aOriginAttributes,
|
|
|
|
uint16_t flags, uint16_t af,
|
2017-12-20 03:13:46 +03:00
|
|
|
nsResolveHostCallback* aCallback) {
|
2018-09-27 12:28:36 +03:00
|
|
|
nsAutoCString host(aHost);
|
|
|
|
NS_ENSURE_TRUE(!host.IsEmpty(), NS_ERROR_UNEXPECTED);
|
|
|
|
|
2020-05-18 17:55:21 +03:00
|
|
|
nsAutoCString originSuffix;
|
|
|
|
aOriginAttributes.CreateSuffix(originSuffix);
|
|
|
|
LOG(("Resolving host [%s]<%s>%s%s type %d. [this=%p]\n", host.get(),
|
|
|
|
originSuffix.get(), flags & RES_BYPASS_CACHE ? " - bypassing cache" : "",
|
2018-09-27 12:28:36 +03:00
|
|
|
flags & RES_REFRESH_CACHE ? " - refresh cache" : "", type, this));
|
|
|
|
|
|
|
|
// ensure that we are working with a valid hostname before proceeding. see
|
|
|
|
// bug 304904 for details.
|
2020-09-02 18:39:00 +03:00
|
|
|
if (!net_IsValidHostName(host)) {
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2003-10-07 09:11:41 +04:00
|
|
|
// By-Type requests use only TRR. If TRR is disabled we can return
|
|
|
|
// immediately.
|
2021-01-25 15:22:43 +03:00
|
|
|
if (IS_OTHER_TYPE(type) && Mode() == nsIDNSService::MODE_TRROFF) {
|
2015-10-18 08:24:48 +03:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
// Used to try to parse to an IP address literal.
|
2021-05-10 12:26:01 +03:00
|
|
|
NetAddr tempAddr;
|
|
|
|
if (IS_OTHER_TYPE(type) && (NS_SUCCEEDED(tempAddr.InitFromString(host)))) {
|
2003-10-07 09:11:41 +04:00
|
|
|
// For by-type queries the host cannot be IP literal.
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2003-10-07 09:11:41 +04:00
|
|
|
RefPtr<nsResolveHostCallback> callback(aCallback);
|
|
|
|
// if result is set inside the lock, then we need to issue the
|
|
|
|
// callback before returning.
|
2018-01-08 01:22:01 +03:00
|
|
|
RefPtr<nsHostRecord> result;
|
2018-09-27 12:28:36 +03:00
|
|
|
nsresult status = NS_OK, rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2018-01-08 01:22:01 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (mShutdown) {
|
2020-10-22 10:36:15 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
// check to see if there is already an entry for this |host|
|
|
|
|
// in the hash table. if so, then check to see if we can't
|
|
|
|
// just reuse the lookup result. otherwise, if there are
|
|
|
|
// any pending callbacks, then add to pending callbacks queue,
|
|
|
|
// and return. otherwise, add ourselves as first pending
|
|
|
|
// callback, and proceed to do the lookup.
|
|
|
|
|
2022-03-25 15:15:03 +03:00
|
|
|
if (StaticPrefs::network_dns_port_prefixed_qname_https_rr() &&
|
|
|
|
type == nsIDNSService::RESOLVE_TYPE_HTTPSSVC && aPort != -1 &&
|
|
|
|
aPort != 443) {
|
|
|
|
host = nsPrintfCString("_%d._https.%s", aPort, host.get());
|
|
|
|
LOG((" Using port prefixed host name [%s]", host.get()));
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
bool excludedFromTRR = false;
|
2021-07-26 12:37:37 +03:00
|
|
|
if (TRRService::Get() && TRRService::Get()->IsExcludedFromTRR(host)) {
|
2020-10-22 10:36:15 +03:00
|
|
|
flags |= RES_DISABLE_TRR;
|
|
|
|
excludedFromTRR = true;
|
|
|
|
|
|
|
|
if (!aTrrServer.IsEmpty()) {
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
2020-07-11 22:32:05 +03:00
|
|
|
}
|
2020-10-22 10:36:15 +03:00
|
|
|
}
|
2020-07-11 22:32:05 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
nsHostKey key(host, aTrrServer, type, flags, af,
|
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0), originSuffix);
|
2011-12-17 06:24:12 +04:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
// Check if we have a localhost domain, if so hardcode to loopback
|
|
|
|
if (IS_ADDR_TYPE(type) && IsLoopbackHostname(host)) {
|
|
|
|
nsresult rv;
|
|
|
|
RefPtr<nsHostRecord> result = InitLoopbackRecord(key, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2020-10-21 13:00:01 +03:00
|
|
|
}
|
2020-10-22 10:36:15 +03:00
|
|
|
MOZ_ASSERT(result);
|
|
|
|
aCallback->OnResolveHostComplete(this, result, NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2021-02-15 19:37:52 +03:00
|
|
|
RefPtr<nsHostRecord> rec =
|
2021-02-26 12:11:45 +03:00
|
|
|
mRecordDB.LookupOrInsertWith(key, [&] { return InitRecord(key); });
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
|
|
|
MOZ_ASSERT(rec, "Record should not be null");
|
|
|
|
MOZ_ASSERT((IS_ADDR_TYPE(type) && rec->IsAddrRecord() && addrRec) ||
|
|
|
|
(IS_OTHER_TYPE(type) && !rec->IsAddrRecord()));
|
2020-10-21 10:42:23 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
if (excludedFromTRR) {
|
2021-02-24 19:37:03 +03:00
|
|
|
rec->RecordReason(TRRSkippedReason::TRR_EXCLUDED);
|
2020-10-22 10:36:15 +03:00
|
|
|
}
|
2020-10-21 10:42:23 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
if (!(flags & RES_BYPASS_CACHE) &&
|
|
|
|
rec->HasUsableResult(TimeStamp::NowLoRes(), flags)) {
|
2021-06-16 11:14:11 +03:00
|
|
|
result = FromCache(rec, host, type, status, lock);
|
2020-10-22 10:36:15 +03:00
|
|
|
} else if (addrRec && addrRec->addr) {
|
|
|
|
// if the host name is an IP address literal and has been
|
|
|
|
// parsed, go ahead and use it.
|
|
|
|
LOG((" Using cached address for IP Literal [%s].\n", host.get()));
|
2021-06-16 11:14:10 +03:00
|
|
|
result = FromCachedIPLiteral(rec);
|
2021-05-10 12:26:01 +03:00
|
|
|
} else if (addrRec && NS_SUCCEEDED(tempAddr.InitFromString(host))) {
|
2020-10-22 10:36:15 +03:00
|
|
|
// try parsing the host name as an IP address literal to short
|
|
|
|
// circuit full host resolution. (this is necessary on some
|
|
|
|
// platforms like Win9x. see bug 219376 for more details.)
|
|
|
|
LOG((" Host is IP Literal [%s].\n", host.get()));
|
2021-06-16 11:14:10 +03:00
|
|
|
result = FromIPLiteral(addrRec, tempAddr);
|
2021-06-16 11:14:11 +03:00
|
|
|
} else if (mQueue.PendingCount() >= MAX_NON_PRIORITY_REQUESTS &&
|
2020-10-22 10:36:15 +03:00
|
|
|
!IsHighPriority(flags) && !rec->mResolving) {
|
|
|
|
LOG(
|
|
|
|
(" Lookup queue full: dropping %s priority request for "
|
|
|
|
"host [%s].\n",
|
|
|
|
IsMediumPriority(flags) ? "medium" : "low", host.get()));
|
|
|
|
if (IS_ADDR_TYPE(type)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_OVERFLOW);
|
|
|
|
}
|
|
|
|
// This is a lower priority request and we are swamped, so refuse it.
|
|
|
|
rv = NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
|
|
|
|
|
|
|
// Check if the offline flag is set.
|
|
|
|
} else if (flags & RES_OFFLINE) {
|
|
|
|
LOG((" Offline request for host [%s]; ignoring.\n", host.get()));
|
|
|
|
rv = NS_ERROR_OFFLINE;
|
|
|
|
|
|
|
|
// We do not have a valid result till here.
|
|
|
|
// A/AAAA request can check for an alternative entry like AF_UNSPEC.
|
|
|
|
// Otherwise we need to start a new query.
|
|
|
|
} else if (!rec->mResolving) {
|
2021-06-16 11:14:10 +03:00
|
|
|
result =
|
|
|
|
FromUnspecEntry(rec, host, aTrrServer, originSuffix, type, flags, af,
|
|
|
|
aOriginAttributes.mPrivateBrowsingId > 0, status);
|
2020-10-22 10:36:15 +03:00
|
|
|
// If this is a by-type request or if no valid record was found
|
|
|
|
// in the cache or this is an AF_UNSPEC request, then start a
|
|
|
|
// new lookup.
|
|
|
|
if (!result) {
|
|
|
|
LOG((" No usable record in cache for host [%s] type %d.", host.get(),
|
|
|
|
type));
|
2020-10-21 10:42:23 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
if (flags & RES_REFRESH_CACHE) {
|
|
|
|
rec->Invalidate();
|
|
|
|
}
|
2020-10-21 13:00:01 +03:00
|
|
|
|
2020-10-22 10:36:15 +03:00
|
|
|
// Add callback to the list of pending callbacks.
|
2020-10-21 13:00:01 +03:00
|
|
|
rec->mCallbacks.insertBack(callback);
|
2020-10-22 10:36:15 +03:00
|
|
|
rec->flags = flags;
|
2021-06-16 11:14:11 +03:00
|
|
|
rv = NameLookup(rec, lock);
|
2020-10-22 10:36:15 +03:00
|
|
|
if (IS_ADDR_TYPE(type)) {
|
2020-10-21 13:00:01 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2020-10-22 10:36:15 +03:00
|
|
|
METHOD_NETWORK_FIRST);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) && callback->isInList()) {
|
|
|
|
callback->remove();
|
|
|
|
} else {
|
|
|
|
LOG(
|
|
|
|
(" DNS lookup for host [%s] blocking "
|
|
|
|
"pending 'getaddrinfo' or trr query: "
|
|
|
|
"callback [%p]",
|
|
|
|
host.get(), callback.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(
|
|
|
|
(" Host [%s] is being resolved. Appending callback "
|
|
|
|
"[%p].",
|
|
|
|
host.get(), callback.get()));
|
|
|
|
|
|
|
|
rec->mCallbacks.insertBack(callback);
|
|
|
|
|
|
|
|
// Only A/AAAA records are place in a queue. The queues are for
|
|
|
|
// the native resolver, therefore by-type request are never put
|
|
|
|
// into a queue.
|
2020-12-04 13:01:05 +03:00
|
|
|
if (addrRec && addrRec->onQueue()) {
|
2020-10-22 10:36:15 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
|
|
|
METHOD_NETWORK_SHARED);
|
|
|
|
|
|
|
|
// Consider the case where we are on a pending queue of
|
|
|
|
// lower priority than the request is being made at.
|
|
|
|
// In that case we should upgrade to the higher queue.
|
|
|
|
|
|
|
|
if (IsHighPriority(flags) && !IsHighPriority(rec->flags)) {
|
|
|
|
// Move from (low|med) to high.
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.MoveToAnotherPendingQ(rec, flags, lock);
|
2020-10-22 10:36:15 +03:00
|
|
|
rec->flags = flags;
|
|
|
|
ConditionallyCreateThread(rec);
|
|
|
|
} else if (IsMediumPriority(flags) && IsLowPriority(rec->flags)) {
|
|
|
|
// Move from low to med.
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.MoveToAnotherPendingQ(rec, flags, lock);
|
2020-10-22 10:36:15 +03:00
|
|
|
rec->flags = flags;
|
|
|
|
mIdleTaskCV.Notify();
|
2017-12-20 03:13:46 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 18:58:18 +03:00
|
|
|
if (result && callback->isInList()) {
|
2017-12-08 01:19:48 +03:00
|
|
|
callback->remove();
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2019-10-09 18:58:18 +03:00
|
|
|
} // lock
|
|
|
|
|
|
|
|
if (result) {
|
2017-12-08 01:19:48 +03:00
|
|
|
callback->OnResolveHostComplete(this, result, status);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-06-16 11:14:10 +03:00
|
|
|
already_AddRefed<nsHostRecord> nsHostResolver::FromCache(
|
|
|
|
nsHostRecord* aRec, const nsACString& aHost, uint16_t aType,
|
2021-06-16 11:14:11 +03:00
|
|
|
nsresult& aStatus, const MutexAutoLock& aLock) {
|
2021-06-16 11:14:10 +03:00
|
|
|
LOG((" Using cached record for host [%s].\n",
|
|
|
|
nsPromiseFlatCString(aHost).get()));
|
|
|
|
|
|
|
|
// put reference to host record on stack...
|
|
|
|
RefPtr<nsHostRecord> result = aRec;
|
|
|
|
if (IS_ADDR_TYPE(aType)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For entries that are in the grace period
|
|
|
|
// or all cached negative entries, use the cache but start a new
|
|
|
|
// lookup in the background
|
2021-06-16 11:14:11 +03:00
|
|
|
ConditionallyRefreshRecord(aRec, aHost, aLock);
|
2021-06-16 11:14:10 +03:00
|
|
|
|
|
|
|
if (aRec->negative) {
|
|
|
|
LOG((" Negative cache entry for host [%s].\n",
|
|
|
|
nsPromiseFlatCString(aHost).get()));
|
|
|
|
if (IS_ADDR_TYPE(aType)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_NEGATIVE_HIT);
|
|
|
|
}
|
|
|
|
aStatus = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsHostRecord> nsHostResolver::FromCachedIPLiteral(
|
|
|
|
nsHostRecord* aRec) {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
|
|
|
|
RefPtr<nsHostRecord> result = aRec;
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsHostRecord> nsHostResolver::FromIPLiteral(
|
|
|
|
AddrHostRecord* aAddrRec, const NetAddr& aAddr) {
|
|
|
|
// ok, just copy the result into the host record, and be
|
|
|
|
// done with it! ;-)
|
|
|
|
aAddrRec->addr = MakeUnique<NetAddr>(aAddr);
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
|
|
|
|
// put reference to host record on stack...
|
|
|
|
RefPtr<nsHostRecord> result = aAddrRec;
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsHostRecord> nsHostResolver::FromUnspecEntry(
|
|
|
|
nsHostRecord* aRec, const nsACString& aHost, const nsACString& aTrrServer,
|
|
|
|
const nsACString& aOriginSuffix, uint16_t aType, uint16_t aFlags,
|
|
|
|
uint16_t af, bool aPb, nsresult& aStatus) {
|
|
|
|
RefPtr<nsHostRecord> result = nullptr;
|
|
|
|
// If this is an IPV4 or IPV6 specific request, check if there is
|
|
|
|
// an AF_UNSPEC entry we can use. Otherwise, hit the resolver...
|
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(aRec);
|
|
|
|
if (addrRec && !(aFlags & RES_BYPASS_CACHE) &&
|
|
|
|
((af == PR_AF_INET) || (af == PR_AF_INET6))) {
|
|
|
|
// Check for an AF_UNSPEC entry.
|
|
|
|
|
|
|
|
const nsHostKey unspecKey(aHost, aTrrServer,
|
|
|
|
nsIDNSService::RESOLVE_TYPE_DEFAULT, aFlags,
|
|
|
|
PR_AF_UNSPEC, aPb, aOriginSuffix);
|
|
|
|
RefPtr<nsHostRecord> unspecRec = mRecordDB.Get(unspecKey);
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
if (unspecRec && unspecRec->HasUsableResult(now, aFlags)) {
|
|
|
|
MOZ_ASSERT(unspecRec->IsAddrRecord());
|
|
|
|
|
|
|
|
RefPtr<AddrHostRecord> addrUnspecRec = do_QueryObject(unspecRec);
|
|
|
|
MOZ_ASSERT(addrUnspecRec);
|
|
|
|
MOZ_ASSERT(addrUnspecRec->addr_info || addrUnspecRec->negative,
|
|
|
|
"Entry should be resolved or negative.");
|
|
|
|
|
|
|
|
LOG((" Trying AF_UNSPEC entry for host [%s] af: %s.\n",
|
|
|
|
PromiseFlatCString(aHost).get(),
|
|
|
|
(af == PR_AF_INET) ? "AF_INET" : "AF_INET6"));
|
|
|
|
|
|
|
|
// We need to lock in case any other thread is reading
|
|
|
|
// addr_info.
|
|
|
|
MutexAutoLock lock(addrRec->addr_info_lock);
|
|
|
|
|
|
|
|
addrRec->addr_info = nullptr;
|
|
|
|
addrRec->addr_info_gencnt++;
|
|
|
|
if (unspecRec->negative) {
|
|
|
|
aRec->negative = unspecRec->negative;
|
|
|
|
aRec->CopyExpirationTimesAndFlagsFrom(unspecRec);
|
|
|
|
} else if (addrUnspecRec->addr_info) {
|
|
|
|
MutexAutoLock lock(addrUnspecRec->addr_info_lock);
|
|
|
|
if (addrUnspecRec->addr_info) {
|
|
|
|
// Search for any valid address in the AF_UNSPEC entry
|
|
|
|
// in the cache (not blocklisted and from the right
|
|
|
|
// family).
|
|
|
|
nsTArray<NetAddr> addresses;
|
|
|
|
for (const auto& addr : addrUnspecRec->addr_info->Addresses()) {
|
|
|
|
if ((af == addr.inet.family) &&
|
|
|
|
!addrUnspecRec->Blocklisted(&addr)) {
|
|
|
|
addresses.AppendElement(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!addresses.IsEmpty()) {
|
|
|
|
addrRec->addr_info = new AddrInfo(
|
|
|
|
addrUnspecRec->addr_info->Hostname(),
|
|
|
|
addrUnspecRec->addr_info->CanonicalHostname(),
|
|
|
|
addrUnspecRec->addr_info->ResolverType(),
|
|
|
|
addrUnspecRec->addr_info->TRRType(), std::move(addresses));
|
|
|
|
addrRec->addr_info_gencnt++;
|
|
|
|
aRec->CopyExpirationTimesAndFlagsFrom(unspecRec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now check if we have a new record.
|
|
|
|
if (aRec->HasUsableResult(now, aFlags)) {
|
|
|
|
result = aRec;
|
|
|
|
if (aRec->negative) {
|
|
|
|
aStatus = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
|
2021-06-16 11:14:11 +03:00
|
|
|
ConditionallyRefreshRecord(aRec, aHost, lock);
|
2021-06-16 11:14:10 +03:00
|
|
|
} else if (af == PR_AF_INET6) {
|
|
|
|
// For AF_INET6, a new lookup means another AF_UNSPEC
|
|
|
|
// lookup. We have already iterated through the
|
|
|
|
// AF_UNSPEC addresses, so we mark this record as
|
|
|
|
// negative.
|
|
|
|
LOG(
|
|
|
|
(" No AF_INET6 in AF_UNSPEC entry: "
|
|
|
|
"host [%s] unknown host.",
|
|
|
|
nsPromiseFlatCString(aHost).get()));
|
|
|
|
result = aRec;
|
|
|
|
aRec->negative = true;
|
|
|
|
aStatus = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
|
|
|
METHOD_NEGATIVE_HIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2020-02-07 12:42:00 +03:00
|
|
|
void nsHostResolver::DetachCallback(
|
|
|
|
const nsACString& host, const nsACString& aTrrServer, uint16_t aType,
|
|
|
|
const OriginAttributes& aOriginAttributes, uint16_t flags, uint16_t af,
|
|
|
|
nsResolveHostCallback* aCallback, nsresult status) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHostRecord> rec;
|
2017-12-20 03:13:46 +03:00
|
|
|
RefPtr<nsResolveHostCallback> callback(aCallback);
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-14 07:25:35 +03:00
|
|
|
nsAutoCString originSuffix;
|
|
|
|
aOriginAttributes.CreateSuffix(originSuffix);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-04 22:35:34 +03:00
|
|
|
nsHostKey key(host, aTrrServer, aType, flags, af,
|
2018-02-01 12:20:49 +03:00
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0), originSuffix);
|
2018-01-08 01:22:01 +03:00
|
|
|
RefPtr<nsHostRecord> entry = mRecordDB.Get(key);
|
|
|
|
if (entry) {
|
2003-09-12 00:32:33 +04:00
|
|
|
// walk list looking for |callback|... we cannot assume
|
|
|
|
// that it will be there!
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
for (nsResolveHostCallback* c : entry->mCallbacks) {
|
2017-12-20 03:13:46 +03:00
|
|
|
if (c == callback) {
|
2018-01-08 01:22:01 +03:00
|
|
|
rec = entry;
|
2017-12-20 03:13:46 +03:00
|
|
|
c->remove();
|
2003-09-12 00:32:33 +04:00
|
|
|
break;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2005-04-06 05:33:28 +04:00
|
|
|
// complete callback with the given status code; this would only be done if
|
|
|
|
// the record was in the process of being resolved.
|
2017-12-20 03:13:46 +03:00
|
|
|
if (rec) {
|
2017-12-08 01:19:48 +03:00
|
|
|
callback->OnResolveHostComplete(this, rec, status);
|
2017-12-20 03:13:46 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
nsresult nsHostResolver::ConditionallyCreateThread(nsHostRecord* rec) {
|
|
|
|
if (mNumIdleTasks) {
|
|
|
|
// wake up idle tasks to process this lookup
|
|
|
|
mIdleTaskCV.Notify();
|
|
|
|
} else if ((mActiveTaskCount < HighThreadThreshold) ||
|
|
|
|
(IsHighPriority(rec->flags) &&
|
|
|
|
mActiveTaskCount < MAX_RESOLVER_THREADS)) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = mozilla::NewRunnableMethod(
|
|
|
|
"nsHostResolver::ThreadFunc", this, &nsHostResolver::ThreadFunc);
|
|
|
|
mActiveTaskCount++;
|
|
|
|
nsresult rv =
|
|
|
|
mResolverThreads->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mActiveTaskCount--;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2018-08-09 19:56:02 +03:00
|
|
|
LOG((" Unable to find a thread for looking up host [%s].\n",
|
|
|
|
rec->host.get()));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-08-09 19:56:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsresult nsHostResolver::TrrLookup_unlocked(nsHostRecord* rec, TRR* pushedTRR) {
|
|
|
|
MutexAutoLock lock(mLock);
|
2021-06-16 11:14:11 +03:00
|
|
|
return TrrLookup(rec, lock, pushedTRR);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2020-09-14 17:47:29 +03:00
|
|
|
void nsHostResolver::MaybeRenewHostRecord(nsHostRecord* aRec) {
|
2020-10-05 17:23:22 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2021-06-16 11:14:11 +03:00
|
|
|
MaybeRenewHostRecordLocked(aRec, lock);
|
2020-10-05 17:23:22 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
void nsHostResolver::MaybeRenewHostRecordLocked(nsHostRecord* aRec,
|
|
|
|
const MutexAutoLock& aLock) {
|
|
|
|
mQueue.MaybeRenewHostRecord(aRec, aLock);
|
2020-09-14 17:47:29 +03:00
|
|
|
}
|
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
bool nsHostResolver::TRRServiceEnabledForRecord(nsHostRecord* aRec) {
|
|
|
|
MOZ_ASSERT(aRec, "Record must not be empty");
|
|
|
|
MOZ_ASSERT(aRec->mEffectiveTRRMode != nsIRequest::TRR_DEFAULT_MODE,
|
|
|
|
"effective TRR mode must be computed before this call");
|
|
|
|
if (!TRRService::Get()) {
|
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_NO_GSERVICE);
|
|
|
|
return false;
|
2020-01-07 23:20:38 +03:00
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
// We always try custom resolvers.
|
|
|
|
if (!aRec->mTrrServer.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
nsIRequest::TRRMode reqMode = aRec->mEffectiveTRRMode;
|
|
|
|
if (TRRService::Get()->Enabled(reqMode)) {
|
|
|
|
return true;
|
2018-09-27 12:28:36 +03:00
|
|
|
}
|
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
if (NS_IsOffline()) {
|
|
|
|
// If we are in the NOT_CONFIRMED state _because_ we lack connectivity,
|
|
|
|
// then we should report that the browser is offline instead.
|
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_IS_OFFLINE);
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2021-01-28 13:51:57 +03:00
|
|
|
auto hasConnectivity = [this]() -> bool {
|
2021-12-22 15:46:50 +03:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2021-01-28 13:51:57 +03:00
|
|
|
if (!mNCS) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
nsINetworkConnectivityService::ConnectivityState ipv4 = mNCS->GetIPv4();
|
|
|
|
nsINetworkConnectivityService::ConnectivityState ipv6 = mNCS->GetIPv6();
|
|
|
|
|
|
|
|
if (ipv4 == nsINetworkConnectivityService::OK ||
|
|
|
|
ipv6 == nsINetworkConnectivityService::OK) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ipv4 == nsINetworkConnectivityService::UNKNOWN ||
|
|
|
|
ipv6 == nsINetworkConnectivityService::UNKNOWN) {
|
|
|
|
// One of the checks hasn't completed yet. Optimistically assume we'll
|
|
|
|
// have network connectivity.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
if (!hasConnectivity()) {
|
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_NO_CONNECTIVITY);
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-28 13:50:35 +03:00
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
if (!TRRService::Get()->IsConfirmed()) {
|
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_NOT_CONFIRMED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns error if no TRR resolve is issued
|
|
|
|
// it is impt this is not called while a native lookup is going on
|
|
|
|
nsresult nsHostResolver::TrrLookup(nsHostRecord* aRec,
|
|
|
|
const MutexAutoLock& aLock, TRR* pushedTRR) {
|
|
|
|
if (Mode() == nsIDNSService::MODE_TRROFF ||
|
|
|
|
StaticPrefs::network_dns_disabled()) {
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
LOG(("TrrLookup host:%s af:%" PRId16, aRec->host.get(), aRec->af));
|
|
|
|
|
|
|
|
RefPtr<nsHostRecord> rec(aRec);
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
RefPtr<AddrHostRecord> addrRec;
|
|
|
|
RefPtr<TypeHostRecord> typeRec;
|
|
|
|
|
|
|
|
if (rec->IsAddrRecord()) {
|
|
|
|
addrRec = do_QueryObject(rec);
|
|
|
|
MOZ_ASSERT(addrRec);
|
|
|
|
} else {
|
|
|
|
typeRec = do_QueryObject(rec);
|
|
|
|
MOZ_ASSERT(typeRec);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!rec->mResolving);
|
|
|
|
|
|
|
|
if (!TRRServiceEnabledForRecord(aRec)) {
|
2018-02-01 12:20:49 +03:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
MaybeRenewHostRecordLocked(rec, aLock);
|
2018-09-22 23:54:11 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
RefPtr<TRRQuery> query = new TRRQuery(this, rec);
|
2021-01-26 17:19:09 +03:00
|
|
|
bool useODoH = gODoHService->Enabled() &&
|
|
|
|
!((rec->flags & nsIDNSService::RESOLVE_DISABLE_ODOH));
|
|
|
|
nsresult rv = query->DispatchLookup(pushedTRR, useODoH);
|
2020-11-03 13:41:51 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2021-02-24 19:37:03 +03:00
|
|
|
rec->RecordReason(TRRSkippedReason::TRR_DID_NOT_MAKE_QUERY);
|
2020-11-03 13:41:51 +03:00
|
|
|
return rv;
|
2020-07-11 22:32:05 +03:00
|
|
|
}
|
2020-11-03 11:08:30 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
{
|
|
|
|
auto lock = rec->mTRRQuery.Lock();
|
|
|
|
MOZ_ASSERT(!lock.ref(), "TRR already in progress");
|
|
|
|
lock.ref() = query;
|
2020-11-03 12:23:35 +03:00
|
|
|
}
|
2020-11-03 13:41:51 +03:00
|
|
|
|
|
|
|
rec->mResolving++;
|
2021-12-02 16:21:53 +03:00
|
|
|
rec->mTrrAttempts++;
|
2020-11-03 13:41:51 +03:00
|
|
|
return NS_OK;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
nsresult nsHostResolver::NativeLookup(nsHostRecord* aRec,
|
|
|
|
const MutexAutoLock& aLock) {
|
2020-05-14 21:26:17 +03:00
|
|
|
if (StaticPrefs::network_dns_disabled()) {
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2020-10-01 13:05:03 +03:00
|
|
|
LOG(("NativeLookup host:%s af:%" PRId16, aRec->host.get(), aRec->af));
|
2020-05-14 21:26:17 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
// Only A/AAAA request are resolve natively.
|
|
|
|
MOZ_ASSERT(aRec->IsAddrRecord());
|
2018-02-01 12:20:49 +03:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2018-09-27 12:28:36 +03:00
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
RefPtr<nsHostRecord> rec(aRec);
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec;
|
|
|
|
addrRec = do_QueryObject(rec);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(addrRec);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
addrRec->mNativeStart = TimeStamp::Now();
|
2008-11-08 02:00:26 +03:00
|
|
|
|
|
|
|
// Add rec to one of the pending queues, possibly removing it from mEvictionQ.
|
2021-06-16 11:14:11 +03:00
|
|
|
MaybeRenewHostRecordLocked(aRec, aLock);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.InsertRecord(rec, rec->flags, aLock);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2021-06-10 17:29:19 +03:00
|
|
|
addrRec->StoreNative(true);
|
2020-12-09 21:14:55 +03:00
|
|
|
addrRec->StoreNativeUsed(true);
|
2018-09-27 12:28:36 +03:00
|
|
|
addrRec->mResolving++;
|
2008-11-08 02:00:26 +03:00
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
nsresult rv = ConditionallyCreateThread(rec);
|
|
|
|
|
|
|
|
LOG((" DNS thread counters: total=%d any-live=%d idle=%d pending=%d\n",
|
2018-07-04 21:36:58 +03:00
|
|
|
static_cast<uint32_t>(mActiveTaskCount),
|
2015-08-20 19:14:40 +03:00
|
|
|
static_cast<uint32_t>(mActiveAnyThreadCount),
|
2021-06-16 11:14:11 +03:00
|
|
|
static_cast<uint32_t>(mNumIdleTasks), mQueue.PendingCount()));
|
2008-11-08 02:00:26 +03:00
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
return rv;
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 22:32:05 +03:00
|
|
|
// static
|
2021-01-25 15:22:43 +03:00
|
|
|
nsIDNSService::ResolverMode nsHostResolver::Mode() {
|
2021-07-26 12:37:37 +03:00
|
|
|
if (TRRService::Get()) {
|
|
|
|
return TRRService::Get()->Mode();
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 12:42:00 +03:00
|
|
|
// If we don't have a TRR service just return MODE_TRROFF so we don't make
|
|
|
|
// any TRR requests by mistake.
|
2021-01-25 15:22:43 +03:00
|
|
|
return nsIDNSService::MODE_TRROFF;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 23:20:38 +03:00
|
|
|
nsIRequest::TRRMode nsHostRecord::TRRMode() {
|
|
|
|
return nsIDNSService::GetTRRModeFromFlags(flags);
|
|
|
|
}
|
|
|
|
|
2020-07-11 22:32:05 +03:00
|
|
|
// static
|
|
|
|
void nsHostResolver::ComputeEffectiveTRRMode(nsHostRecord* aRec) {
|
2021-01-25 15:22:43 +03:00
|
|
|
nsIDNSService::ResolverMode resolverMode = nsHostResolver::Mode();
|
2020-07-11 22:32:05 +03:00
|
|
|
nsIRequest::TRRMode requestMode = aRec->TRRMode();
|
|
|
|
|
2020-01-07 23:20:38 +03:00
|
|
|
// For domains that are excluded from TRR or when parental control is enabled,
|
|
|
|
// we fallback to NativeLookup. This happens even in MODE_TRRONLY. By default
|
|
|
|
// localhost and local are excluded (so we cover *.local hosts) See the
|
|
|
|
// network.trr.excluded-domains pref.
|
|
|
|
|
2021-07-26 12:37:37 +03:00
|
|
|
if (!TRRService::Get()) {
|
2021-02-24 19:37:03 +03:00
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_NO_GSERVICE);
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = requestMode;
|
|
|
|
return;
|
2020-01-07 23:20:38 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 22:32:05 +03:00
|
|
|
if (!aRec->mTrrServer.IsEmpty()) {
|
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_ONLY_MODE;
|
|
|
|
return;
|
2020-01-07 23:20:38 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 12:37:37 +03:00
|
|
|
if (TRRService::Get()->IsExcludedFromTRR(aRec->host)) {
|
2021-02-24 19:37:03 +03:00
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_EXCLUDED);
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
|
|
|
|
return;
|
2020-01-07 23:20:38 +03:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:49:59 +03:00
|
|
|
if (StaticPrefs::network_dns_skipTRR_when_parental_control_enabled() &&
|
2021-07-26 12:37:37 +03:00
|
|
|
TRRService::Get()->ParentalControlEnabled()) {
|
2021-02-24 19:37:03 +03:00
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_PARENTAL_CONTROL);
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:22:43 +03:00
|
|
|
if (resolverMode == nsIDNSService::MODE_TRROFF) {
|
2021-02-24 19:37:03 +03:00
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_OFF_EXPLICIT);
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
|
|
|
|
return;
|
2020-01-07 23:20:38 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 22:32:05 +03:00
|
|
|
if (requestMode == nsIRequest::TRR_DISABLED_MODE) {
|
2021-02-24 19:37:03 +03:00
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_REQ_MODE_DISABLED);
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((requestMode == nsIRequest::TRR_DEFAULT_MODE &&
|
2021-01-25 15:22:43 +03:00
|
|
|
resolverMode == nsIDNSService::MODE_NATIVEONLY)) {
|
2021-02-24 19:37:03 +03:00
|
|
|
aRec->RecordReason(TRRSkippedReason::TRR_MODE_NOT_ENABLED);
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_DISABLED_MODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestMode == nsIRequest::TRR_DEFAULT_MODE &&
|
2021-01-25 15:22:43 +03:00
|
|
|
resolverMode == nsIDNSService::MODE_TRRFIRST) {
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_FIRST_MODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestMode == nsIRequest::TRR_DEFAULT_MODE &&
|
2021-01-25 15:22:43 +03:00
|
|
|
resolverMode == nsIDNSService::MODE_TRRONLY) {
|
2020-07-11 22:32:05 +03:00
|
|
|
aRec->mEffectiveTRRMode = nsIRequest::TRR_ONLY_MODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRec->mEffectiveTRRMode = requestMode;
|
2020-01-07 23:20:38 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
// Kick-off a name resolve operation, using native resolver and/or TRR
|
2021-06-16 11:14:11 +03:00
|
|
|
nsresult nsHostResolver::NameLookup(nsHostRecord* rec,
|
|
|
|
const mozilla::MutexAutoLock& aLock) {
|
2020-05-28 09:58:04 +03:00
|
|
|
LOG(("NameLookup host:%s af:%" PRId16, rec->host.get(), rec->af));
|
2022-05-13 00:55:01 +03:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2020-05-28 09:58:04 +03:00
|
|
|
|
2020-09-14 17:47:29 +03:00
|
|
|
if (rec->flags & RES_IP_HINT) {
|
|
|
|
LOG(("Skip lookup if RES_IP_HINT is set\n"));
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsresult rv = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
if (rec->mResolving) {
|
|
|
|
LOG(("NameLookup %s while already resolving\n", rec->host.get()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:11:00 +03:00
|
|
|
// Make sure we reset the reason each time we attempt to do a new lookup
|
2021-12-02 16:21:54 +03:00
|
|
|
// so we don't wrongly report the reason for the previous one.
|
2021-02-24 19:37:03 +03:00
|
|
|
rec->mTRRSkippedReason = TRRSkippedReason::TRR_UNSET;
|
2021-12-02 16:21:54 +03:00
|
|
|
rec->mFirstTRRSkippedReason = TRRSkippedReason::TRR_UNSET;
|
2021-12-02 16:21:53 +03:00
|
|
|
rec->mTrrAttempts = 0;
|
|
|
|
|
2020-07-11 22:32:05 +03:00
|
|
|
ComputeEffectiveTRRMode(rec);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
if (rec->IsAddrRecord()) {
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(addrRec);
|
2018-09-22 23:54:11 +03:00
|
|
|
|
2020-12-09 21:14:55 +03:00
|
|
|
addrRec->StoreNativeUsed(false);
|
2021-02-18 15:50:02 +03:00
|
|
|
addrRec->mResolverType = DNSResolverType::Native;
|
2018-09-27 12:28:36 +03:00
|
|
|
addrRec->mNativeSuccess = false;
|
2018-09-22 23:54:11 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2020-02-04 22:35:34 +03:00
|
|
|
if (!rec->mTrrServer.IsEmpty()) {
|
|
|
|
LOG(("NameLookup: %s use trr:%s", rec->host.get(), rec->mTrrServer.get()));
|
2020-07-11 22:32:05 +03:00
|
|
|
if (rec->mEffectiveTRRMode != nsIRequest::TRR_ONLY_MODE) {
|
2020-02-04 22:35:34 +03:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2020-06-04 13:43:47 +03:00
|
|
|
|
|
|
|
if (rec->flags & RES_DISABLE_TRR) {
|
|
|
|
LOG(("TRR with server and DISABLE_TRR flag. Returning error."));
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2021-06-16 11:14:11 +03:00
|
|
|
return TrrLookup(rec, aLock);
|
2020-02-04 22:35:34 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:58:04 +03:00
|
|
|
LOG(("NameLookup: %s effectiveTRRmode: %d flags: %X", rec->host.get(),
|
|
|
|
rec->mEffectiveTRRMode, rec->flags));
|
2020-01-07 22:32:32 +03:00
|
|
|
|
2020-08-03 15:26:54 +03:00
|
|
|
if (rec->flags & RES_DISABLE_TRR) {
|
2021-02-24 19:37:03 +03:00
|
|
|
rec->RecordReason(TRRSkippedReason::TRR_DISABLED_FLAG);
|
2020-08-03 15:26:54 +03:00
|
|
|
}
|
|
|
|
|
2021-10-11 10:22:37 +03:00
|
|
|
bool serviceNotReady = !TRRServiceEnabledForRecord(rec);
|
|
|
|
|
2020-02-07 12:42:00 +03:00
|
|
|
if (rec->mEffectiveTRRMode != nsIRequest::TRR_DISABLED_MODE &&
|
2021-07-26 12:37:37 +03:00
|
|
|
!((rec->flags & RES_DISABLE_TRR)) && !serviceNotReady) {
|
2021-06-16 11:14:11 +03:00
|
|
|
rv = TrrLookup(rec, aLock);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 12:42:00 +03:00
|
|
|
if (rec->mEffectiveTRRMode == nsIRequest::TRR_DISABLED_MODE ||
|
|
|
|
(rec->mEffectiveTRRMode == nsIRequest::TRR_FIRST_MODE &&
|
2020-06-04 14:12:19 +03:00
|
|
|
(rec->flags & RES_DISABLE_TRR || serviceNotReady || NS_FAILED(rv)))) {
|
2018-09-27 12:28:36 +03:00
|
|
|
if (!rec->IsAddrRecord()) {
|
2018-09-22 23:54:11 +03:00
|
|
|
return rv;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2020-07-07 13:08:28 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// If we use this branch then the mTRRUsed flag should not be set
|
|
|
|
// Even if we did call TrrLookup above, the fact that it failed sync-ly
|
|
|
|
// means that we didn't actually succeed in opening the channel.
|
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
2021-02-18 15:50:02 +03:00
|
|
|
MOZ_ASSERT(addrRec && addrRec->mResolverType == DNSResolverType::Native);
|
2020-07-07 13:08:28 +03:00
|
|
|
#endif
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
rv = NativeLookup(rec, aLock);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
nsresult nsHostResolver::ConditionallyRefreshRecord(
|
|
|
|
nsHostRecord* rec, const nsACString& host, const MutexAutoLock& aLock) {
|
2014-09-21 02:10:18 +04:00
|
|
|
if ((rec->CheckExpiration(TimeStamp::NowLoRes()) != nsHostRecord::EXP_VALID ||
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->negative) &&
|
2021-04-19 16:56:50 +03:00
|
|
|
!rec->mResolving && rec->RefreshForNegativeResponse()) {
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG((" Using %s cache entry for host [%s] but starting async renewal.",
|
2018-06-15 02:15:13 +03:00
|
|
|
rec->negative ? "negative" : "positive", host.BeginReading()));
|
2021-06-16 11:14:11 +03:00
|
|
|
NameLookup(rec, aLock);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
if (rec->IsAddrRecord() && !rec->negative) {
|
2013-08-16 22:49:57 +04:00
|
|
|
// negative entries are constantly being refreshed, only
|
|
|
|
// track positive grace period induced renewals
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_RENEWAL);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-08-16 22:49:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
bool nsHostResolver::GetHostToLookup(AddrHostRecord** result) {
|
2018-08-09 19:56:02 +03:00
|
|
|
bool timedOut = false;
|
|
|
|
TimeDuration timeout;
|
|
|
|
TimeStamp epoch, now;
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
timeout = (mNumIdleTasks >= HighThreadThreshold) ? mShortIdleTimeout
|
|
|
|
: mLongIdleTimeout;
|
|
|
|
epoch = TimeStamp::Now();
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
while (!mShutdown) {
|
|
|
|
// remove next record from Q; hand over owning reference. Check high, then
|
|
|
|
// med, then low
|
|
|
|
|
2020-12-09 21:14:55 +03:00
|
|
|
#define SET_GET_TTL(var, val) (var)->StoreGetTtl(sGetTtlEnabled && (val))
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = mQueue.Dequeue(true, lock);
|
|
|
|
if (addrRec) {
|
|
|
|
SET_GET_TTL(addrRec, false);
|
|
|
|
addrRec.forget(result);
|
2018-08-07 10:03:57 +03:00
|
|
|
return true;
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
if (mActiveAnyThreadCount < HighThreadThreshold) {
|
2021-06-16 11:14:11 +03:00
|
|
|
addrRec = mQueue.Dequeue(false, lock);
|
|
|
|
if (addrRec) {
|
|
|
|
MOZ_ASSERT(IsMediumPriority(addrRec->flags) ||
|
|
|
|
IsLowPriority(addrRec->flags));
|
2018-08-09 19:56:02 +03:00
|
|
|
mActiveAnyThreadCount++;
|
2021-06-16 11:14:11 +03:00
|
|
|
addrRec->StoreUsingAnyThread(true);
|
|
|
|
SET_GET_TTL(addrRec, true);
|
|
|
|
addrRec.forget(result);
|
2018-08-09 19:56:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determining timeout is racy, so allow one cycle through checking the
|
|
|
|
// queues before exiting.
|
2020-09-02 18:39:00 +03:00
|
|
|
if (timedOut) {
|
|
|
|
break;
|
|
|
|
}
|
2018-08-09 19:56:02 +03:00
|
|
|
|
|
|
|
// wait for one or more of the following to occur:
|
|
|
|
// (1) the pending queue has a host record to process
|
|
|
|
// (2) the shutdown flag has been set
|
|
|
|
// (3) the thread has been idle for too long
|
|
|
|
|
|
|
|
mNumIdleTasks++;
|
|
|
|
mIdleTaskCV.Wait(timeout);
|
|
|
|
mNumIdleTasks--;
|
|
|
|
|
|
|
|
now = TimeStamp::Now();
|
|
|
|
|
|
|
|
if (now - epoch >= timeout) {
|
|
|
|
timedOut = true;
|
|
|
|
} else {
|
|
|
|
// It is possible that CondVar::Wait() was interrupted and returned
|
|
|
|
// early, in which case we will loop back and re-enter it. In that
|
|
|
|
// case we want to do so with the new timeout reduced to reflect
|
|
|
|
// time already spent waiting.
|
|
|
|
timeout -= now - epoch;
|
|
|
|
epoch = now;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
// tell thread to exit...
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
void nsHostResolver::PrepareRecordExpirationAddrRecord(
|
|
|
|
AddrHostRecord* rec) const {
|
2018-02-01 12:20:49 +03:00
|
|
|
// NOTE: rec->addr_info_lock is already held by parent
|
2014-09-21 02:10:18 +04:00
|
|
|
MOZ_ASSERT(((bool)rec->addr_info) != rec->negative);
|
2018-02-01 12:20:49 +03:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2014-09-21 02:10:18 +04:00
|
|
|
if (!rec->addr_info) {
|
|
|
|
rec->SetExpiration(TimeStamp::NowLoRes(), NEGATIVE_RECORD_LIFETIME, 0);
|
2018-05-16 23:05:03 +03:00
|
|
|
LOG(("Caching host [%s] negative record for %u seconds.\n", rec->host.get(),
|
|
|
|
NEGATIVE_RECORD_LIFETIME));
|
2014-09-21 02:10:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-20 21:26:33 +04:00
|
|
|
unsigned int lifetime = mDefaultCacheLifetime;
|
|
|
|
unsigned int grace = mDefaultGracePeriod;
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2014-10-20 21:26:33 +04:00
|
|
|
unsigned int ttl = mDefaultCacheLifetime;
|
2021-02-18 15:50:02 +03:00
|
|
|
if (sGetTtlEnabled || rec->addr_info->IsTRROrODoH()) {
|
2020-09-01 10:22:14 +03:00
|
|
|
if (rec->addr_info && rec->addr_info->TTL() != AddrInfo::NO_TTL_DATA) {
|
|
|
|
ttl = rec->addr_info->TTL();
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2014-10-20 21:26:33 +04:00
|
|
|
lifetime = ttl;
|
|
|
|
grace = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-09-21 02:10:18 +04:00
|
|
|
|
|
|
|
rec->SetExpiration(TimeStamp::NowLoRes(), lifetime, grace);
|
2018-05-16 23:05:03 +03:00
|
|
|
LOG(("Caching host [%s] record for %u seconds (grace %d).", rec->host.get(),
|
|
|
|
lifetime, grace));
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2016-07-08 04:04:36 +03:00
|
|
|
static bool different_rrset(AddrInfo* rrset1, AddrInfo* rrset2) {
|
|
|
|
if (!rrset1 || !rrset2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:22:14 +03:00
|
|
|
LOG(("different_rrset %s\n", rrset1->Hostname().get()));
|
2016-07-08 04:04:36 +03:00
|
|
|
|
2021-02-18 15:50:02 +03:00
|
|
|
if (rrset1->ResolverType() != rrset2->ResolverType()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rrset1->TRRType() != rrset2->TRRType()) {
|
2018-04-05 17:30:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:22:14 +03:00
|
|
|
if (rrset1->Addresses().Length() != rrset2->Addresses().Length()) {
|
2016-07-08 04:04:36 +03:00
|
|
|
LOG(("different_rrset true due to length change\n"));
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-01 10:22:14 +03:00
|
|
|
|
|
|
|
nsTArray<NetAddr> orderedSet1 = rrset1->Addresses().Clone();
|
|
|
|
nsTArray<NetAddr> orderedSet2 = rrset2->Addresses().Clone();
|
2016-07-08 04:04:36 +03:00
|
|
|
orderedSet1.Sort();
|
|
|
|
orderedSet2.Sort();
|
|
|
|
|
2020-09-01 10:38:03 +03:00
|
|
|
bool eq = orderedSet1 == orderedSet2;
|
|
|
|
if (!eq) {
|
|
|
|
LOG(("different_rrset true due to content change\n"));
|
|
|
|
} else {
|
|
|
|
LOG(("different_rrset false\n"));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2020-09-01 10:38:03 +03:00
|
|
|
return !eq;
|
2016-07-08 04:04:36 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
void nsHostResolver::AddToEvictionQ(nsHostRecord* rec,
|
|
|
|
const MutexAutoLock& aLock) {
|
|
|
|
mQueue.AddToEvictionQ(rec, mMaxCacheEntries, mRecordDB, aLock);
|
2018-09-22 23:54:11 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 16:21:53 +03:00
|
|
|
// After a first lookup attempt with TRR in mode 2, we may:
|
|
|
|
// - If we are not in strict mode, retry with native.
|
|
|
|
// - If we are in strict mode:
|
|
|
|
// - Retry with native if the first attempt failed because we got NXDOMAIN, an
|
|
|
|
// unreachable address (TRR_DISABLED_FLAG), or we skipped TRR because
|
|
|
|
// Confirmation failed.
|
|
|
|
// - Trigger a "strict mode" Confirmation which will start a fresh
|
|
|
|
// connection for TRR, and then retry the lookup with TRR.
|
|
|
|
// Returns true if we retried with either TRR or Native.
|
|
|
|
bool nsHostResolver::MaybeRetryTRRLookup(
|
|
|
|
AddrHostRecord* aAddrRec, nsresult aFirstAttemptStatus,
|
|
|
|
TRRSkippedReason aFirstAttemptSkipReason, const MutexAutoLock& aLock) {
|
|
|
|
if (NS_SUCCEEDED(aFirstAttemptStatus) ||
|
|
|
|
aAddrRec->mEffectiveTRRMode != nsIRequest::TRR_FIRST_MODE ||
|
|
|
|
aFirstAttemptStatus == NS_ERROR_DEFINITIVE_UNKNOWN_HOST) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!aAddrRec->mResolving);
|
|
|
|
if (!StaticPrefs::network_trr_strict_native_fallback()) {
|
|
|
|
LOG(("nsHostResolver::MaybeRetryTRRLookup retrying with native"));
|
2022-01-18 12:49:21 +03:00
|
|
|
return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
|
2021-12-02 16:21:53 +03:00
|
|
|
}
|
|
|
|
|
2022-02-25 13:25:56 +03:00
|
|
|
if (aFirstAttemptSkipReason == TRRSkippedReason::TRR_RCODE_FAIL ||
|
|
|
|
aFirstAttemptSkipReason == TRRSkippedReason::TRR_NO_ANSWERS ||
|
|
|
|
aFirstAttemptSkipReason == TRRSkippedReason::TRR_NXDOMAIN ||
|
2021-12-02 16:21:53 +03:00
|
|
|
aFirstAttemptSkipReason == TRRSkippedReason::TRR_DISABLED_FLAG ||
|
|
|
|
aFirstAttemptSkipReason == TRRSkippedReason::TRR_NOT_CONFIRMED) {
|
|
|
|
LOG(
|
|
|
|
("nsHostResolver::MaybeRetryTRRLookup retrying with native in strict "
|
|
|
|
"mode, skip reason was %d",
|
|
|
|
static_cast<uint32_t>(aFirstAttemptSkipReason)));
|
2022-01-18 12:49:21 +03:00
|
|
|
return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
|
2021-12-02 16:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aAddrRec->mTrrAttempts > 1) {
|
2022-02-07 12:04:45 +03:00
|
|
|
if (aFirstAttemptSkipReason == TRRSkippedReason::TRR_TIMEOUT &&
|
|
|
|
StaticPrefs::network_trr_strict_native_fallback_allow_timeouts()) {
|
|
|
|
LOG(
|
|
|
|
("nsHostResolver::MaybeRetryTRRLookup retry timed out. Using "
|
|
|
|
"native."));
|
|
|
|
return NS_SUCCEEDED(NativeLookup(aAddrRec, aLock));
|
|
|
|
}
|
2021-12-02 16:21:53 +03:00
|
|
|
LOG(("nsHostResolver::MaybeRetryTRRLookup mTrrAttempts>1, not retrying."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("nsHostResolver::MaybeRetryTRRLookup triggering Confirmation and "
|
|
|
|
"retrying with TRR, skip reason was %d",
|
|
|
|
static_cast<uint32_t>(aFirstAttemptSkipReason)));
|
|
|
|
TRRService::Get()->StrictModeConfirm();
|
|
|
|
|
|
|
|
{
|
|
|
|
// Clear out the old query
|
|
|
|
auto trrQuery = aAddrRec->mTRRQuery.Lock();
|
|
|
|
trrQuery.ref() = nullptr;
|
|
|
|
}
|
2022-01-18 12:49:21 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(TrrLookup(aAddrRec, aLock, nullptr /* pushedTRR */))) {
|
|
|
|
aAddrRec->NotifyRetryingTrr();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-12-02 16:21:53 +03:00
|
|
|
}
|
|
|
|
|
2014-09-25 07:14:00 +04:00
|
|
|
//
|
2017-12-08 01:19:48 +03:00
|
|
|
// CompleteLookup() checks if the resolving should be redone and if so it
|
2014-09-25 07:14:00 +04:00
|
|
|
// returns LOOKUP_RESOLVEAGAIN, but only if 'status' is not NS_ERROR_ABORT.
|
2018-10-30 16:06:24 +03:00
|
|
|
nsHostResolver::LookupStatus nsHostResolver::CompleteLookup(
|
|
|
|
nsHostRecord* rec, nsresult status, AddrInfo* aNewRRSet, bool pb,
|
2021-02-24 19:37:03 +03:00
|
|
|
const nsACString& aOriginsuffix, TRRSkippedReason aReason,
|
2021-02-17 13:26:41 +03:00
|
|
|
mozilla::net::TRR* aTRRRequest) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2021-06-16 11:14:11 +03:00
|
|
|
return CompleteLookupLocked(rec, status, aNewRRSet, pb, aOriginsuffix,
|
|
|
|
aReason, aTRRRequest, lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHostResolver::LookupStatus nsHostResolver::CompleteLookupLocked(
|
|
|
|
nsHostRecord* rec, nsresult status, AddrInfo* aNewRRSet, bool pb,
|
|
|
|
const nsACString& aOriginsuffix, TRRSkippedReason aReason,
|
|
|
|
mozilla::net::TRR* aTRRRequest, const mozilla::MutexAutoLock& aLock) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(rec);
|
|
|
|
MOZ_ASSERT(rec->pb == pb);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(rec->IsAddrRecord());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(addrRec);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-19 15:22:12 +03:00
|
|
|
RefPtr<AddrInfo> newRRSet(aNewRRSet);
|
2020-11-03 13:41:51 +03:00
|
|
|
MOZ_ASSERT(NS_FAILED(status) || newRRSet->Addresses().Length() > 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-02-18 15:50:02 +03:00
|
|
|
DNSResolverType type =
|
|
|
|
newRRSet ? newRRSet->ResolverType() : DNSResolverType::Native;
|
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
newRRSet = nullptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-02-18 15:50:02 +03:00
|
|
|
if (addrRec->LoadResolveAgain() && (status != NS_ERROR_ABORT) &&
|
|
|
|
type == DNSResolverType::Native) {
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(("nsHostResolver record %p resolve again due to flushcache\n",
|
|
|
|
addrRec.get()));
|
2020-12-09 21:14:55 +03:00
|
|
|
addrRec->StoreResolveAgain(false);
|
2018-02-20 01:33:40 +03:00
|
|
|
return LOOKUP_RESOLVEAGAIN;
|
|
|
|
}
|
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(addrRec->mResolving);
|
|
|
|
addrRec->mResolving--;
|
2021-02-18 15:50:02 +03:00
|
|
|
LOG((
|
|
|
|
"nsHostResolver::CompleteLookup %s %p %X resolver=%d stillResolving=%d\n",
|
2021-02-18 19:39:30 +03:00
|
|
|
addrRec->host.get(), aNewRRSet, (unsigned int)status, (int)type,
|
2021-02-18 15:50:02 +03:00
|
|
|
int(addrRec->mResolving)));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-02-18 15:50:02 +03:00
|
|
|
if (type != DNSResolverType::Native) {
|
2020-11-03 13:41:51 +03:00
|
|
|
if (NS_FAILED(status) && status != NS_ERROR_UNKNOWN_HOST &&
|
|
|
|
status != NS_ERROR_DEFINITIVE_UNKNOWN_HOST) {
|
|
|
|
// the errors are not failed resolves, that means
|
|
|
|
// something else failed, consider this as *TRR not used*
|
|
|
|
// for actually trying to resolve the host
|
2021-02-18 15:50:02 +03:00
|
|
|
addrRec->mResolverType = DNSResolverType::Native;
|
2020-11-03 11:08:30 +03:00
|
|
|
}
|
2009-01-14 20:18:27 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
if (NS_FAILED(status)) {
|
2021-02-24 19:37:03 +03:00
|
|
|
if (aReason != TRRSkippedReason::TRR_UNSET) {
|
2020-11-03 13:41:51 +03:00
|
|
|
addrRec->RecordReason(aReason);
|
2020-11-03 12:23:35 +03:00
|
|
|
} else {
|
2020-11-03 13:41:51 +03:00
|
|
|
// Unknown failed reason.
|
2021-02-24 19:37:03 +03:00
|
|
|
addrRec->RecordReason(TRRSkippedReason::TRR_FAILED);
|
2020-11-03 12:23:35 +03:00
|
|
|
}
|
2020-11-03 13:41:51 +03:00
|
|
|
} else {
|
|
|
|
addrRec->mTRRSuccess++;
|
2021-02-24 19:37:03 +03:00
|
|
|
addrRec->RecordReason(TRRSkippedReason::TRR_OK);
|
2020-11-03 13:41:51 +03:00
|
|
|
}
|
2020-11-03 12:23:35 +03:00
|
|
|
|
2021-12-02 16:21:53 +03:00
|
|
|
if (MaybeRetryTRRLookup(addrRec, status, aReason, aLock)) {
|
2020-11-03 13:41:51 +03:00
|
|
|
MOZ_ASSERT(addrRec->mResolving);
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
2020-11-03 12:23:35 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
if (!addrRec->mTRRSuccess) {
|
|
|
|
// no TRR success
|
|
|
|
newRRSet = nullptr;
|
2020-11-03 11:08:30 +03:00
|
|
|
}
|
2020-11-03 12:23:35 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
// This is the error that consumers expect.
|
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
} else { // native resolve completed
|
2020-12-09 21:14:55 +03:00
|
|
|
if (addrRec->LoadUsingAnyThread()) {
|
2018-09-27 12:28:36 +03:00
|
|
|
mActiveAnyThreadCount--;
|
2020-12-09 21:14:55 +03:00
|
|
|
addrRec->StoreUsingAnyThread(false);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2014-08-21 18:53:11 +04:00
|
|
|
|
2020-09-02 18:39:00 +03:00
|
|
|
addrRec->mNativeSuccess = static_cast<bool>(newRRSet);
|
2018-09-27 12:28:36 +03:00
|
|
|
if (addrRec->mNativeSuccess) {
|
|
|
|
addrRec->mNativeDuration = TimeStamp::Now() - addrRec->mNativeStart;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2020-12-04 13:01:05 +03:00
|
|
|
// This should always be cleared when a request is completed.
|
2020-12-09 21:14:55 +03:00
|
|
|
addrRec->StoreNative(false);
|
2020-12-04 13:01:05 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
// update record fields. We might have a addrRec->addr_info already if a
|
|
|
|
// previous lookup result expired and we're reresolving it or we get
|
|
|
|
// a late second TRR response.
|
2019-06-01 12:44:20 +03:00
|
|
|
if (!mShutdown) {
|
2018-09-27 12:28:36 +03:00
|
|
|
MutexAutoLock lock(addrRec->addr_info_lock);
|
2019-03-19 15:22:12 +03:00
|
|
|
RefPtr<AddrInfo> old_addr_info;
|
2018-09-27 12:28:36 +03:00
|
|
|
if (different_rrset(addrRec->addr_info, newRRSet)) {
|
2018-02-01 12:20:49 +03:00
|
|
|
LOG(("nsHostResolver record %p new gencnt\n", addrRec.get()));
|
|
|
|
old_addr_info = addrRec->addr_info;
|
2020-02-13 17:38:48 +03:00
|
|
|
addrRec->addr_info = std::move(newRRSet);
|
2018-09-27 12:28:36 +03:00
|
|
|
addrRec->addr_info_gencnt++;
|
|
|
|
} else {
|
|
|
|
if (addrRec->addr_info && newRRSet) {
|
2020-09-01 10:22:14 +03:00
|
|
|
auto builder = addrRec->addr_info->Build();
|
|
|
|
builder.SetTTL(newRRSet->TTL());
|
2020-02-07 13:46:58 +03:00
|
|
|
// Update trr timings
|
2020-09-01 10:22:14 +03:00
|
|
|
builder.SetTrrFetchDuration(newRRSet->GetTrrFetchDuration());
|
|
|
|
builder.SetTrrFetchDurationNetworkOnly(
|
2020-02-07 13:46:58 +03:00
|
|
|
newRRSet->GetTrrFetchDurationNetworkOnly());
|
2020-09-01 10:22:14 +03:00
|
|
|
|
|
|
|
addrRec->addr_info = builder.Finish();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2020-02-13 17:38:48 +03:00
|
|
|
old_addr_info = std::move(newRRSet);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
addrRec->negative = !addrRec->addr_info;
|
|
|
|
PrepareRecordExpirationAddrRecord(addrRec);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
if (LOG_ENABLED()) {
|
2018-09-27 12:28:36 +03:00
|
|
|
MutexAutoLock lock(addrRec->addr_info_lock);
|
|
|
|
if (addrRec->addr_info) {
|
2020-09-01 10:22:14 +03:00
|
|
|
for (const auto& elem : addrRec->addr_info->Addresses()) {
|
2018-02-01 12:20:49 +03:00
|
|
|
char buf[128];
|
2020-09-15 13:39:56 +03:00
|
|
|
elem.ToStringBuffer(buf, sizeof(buf));
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(("CompleteLookup: %s has %s\n", addrRec->host.get(), buf));
|
2014-10-14 01:07:00 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
} else {
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(("CompleteLookup: %s has NO address\n", addrRec->host.get()));
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
// get the list of pending callbacks for this lookup, and notify
|
|
|
|
// them that the lookup is complete.
|
|
|
|
mozilla::LinkedList<RefPtr<nsResolveHostCallback>> cbs =
|
|
|
|
std::move(rec->mCallbacks);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
LOG(("nsHostResolver record %p calling back dns users status:%X\n",
|
|
|
|
addrRec.get(), int(status)));
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
for (nsResolveHostCallback* c = cbs.getFirst(); c;
|
|
|
|
c = c->removeAndGetNext()) {
|
|
|
|
c->OnResolveHostComplete(this, rec, status);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
if (!addrRec->mResolving && !mShutdown) {
|
2020-11-03 13:41:51 +03:00
|
|
|
{
|
|
|
|
auto trrQuery = addrRec->mTRRQuery.Lock();
|
|
|
|
if (trrQuery.ref()) {
|
|
|
|
addrRec->mTrrDuration = trrQuery.ref()->Duration();
|
|
|
|
}
|
|
|
|
trrQuery.ref() = nullptr;
|
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
addrRec->ResolveComplete();
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
AddToEvictionQ(rec, aLock);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
|
|
|
// Unless the result is from TRR, resolve again to get TTL
|
2020-06-04 13:43:47 +03:00
|
|
|
bool hasNativeResult = false;
|
2018-02-01 12:20:49 +03:00
|
|
|
{
|
2018-09-27 12:28:36 +03:00
|
|
|
MutexAutoLock lock(addrRec->addr_info_lock);
|
2021-02-18 15:50:02 +03:00
|
|
|
if (addrRec->addr_info && !addrRec->addr_info->IsTRROrODoH()) {
|
2020-06-04 13:43:47 +03:00
|
|
|
hasNativeResult = true;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2020-12-09 21:14:55 +03:00
|
|
|
if (hasNativeResult && !mShutdown && !addrRec->LoadGetTtl() &&
|
2020-12-08 19:39:42 +03:00
|
|
|
!rec->mResolving && sGetTtlEnabled) {
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(("Issuing second async lookup for TTL for host [%s].",
|
|
|
|
addrRec->host.get()));
|
2020-07-11 22:32:07 +03:00
|
|
|
addrRec->flags = (addrRec->flags & ~RES_PRIORITY_MEDIUM) | RES_PRIORITY_LOW;
|
2021-06-16 11:14:11 +03:00
|
|
|
DebugOnly<nsresult> rv = NativeLookup(rec, aLock);
|
2018-02-01 12:20:49 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Could not issue second async lookup for TTL.");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
#endif
|
2014-09-25 07:14:00 +04:00
|
|
|
return LOOKUP_OK;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsHostResolver::LookupStatus nsHostResolver::CompleteLookupByType(
|
2020-05-04 22:56:51 +03:00
|
|
|
nsHostRecord* rec, nsresult status,
|
|
|
|
mozilla::net::TypeRecordResultType& aResult, uint32_t aTtl, bool pb) {
|
2018-09-22 23:54:11 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2021-06-16 11:14:11 +03:00
|
|
|
return CompleteLookupByTypeLocked(rec, status, aResult, aTtl, pb, lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHostResolver::LookupStatus nsHostResolver::CompleteLookupByTypeLocked(
|
|
|
|
nsHostRecord* rec, nsresult status,
|
|
|
|
mozilla::net::TypeRecordResultType& aResult, uint32_t aTtl, bool pb,
|
|
|
|
const mozilla::MutexAutoLock& aLock) {
|
2018-09-22 23:54:11 +03:00
|
|
|
MOZ_ASSERT(rec);
|
|
|
|
MOZ_ASSERT(rec->pb == pb);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(!rec->IsAddrRecord());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<TypeHostRecord> typeRec = do_QueryObject(rec);
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(typeRec);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
MOZ_ASSERT(typeRec->mResolving);
|
|
|
|
typeRec->mResolving--;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-11-03 13:41:51 +03:00
|
|
|
{
|
|
|
|
auto lock = rec->mTRRQuery.Lock();
|
|
|
|
lock.ref() = nullptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
uint32_t duration = static_cast<uint32_t>(
|
|
|
|
(TimeStamp::Now() - typeRec->mStart).ToMilliseconds());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
if (NS_FAILED(status)) {
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(("nsHostResolver::CompleteLookupByType record %p [%s] status %x\n",
|
|
|
|
typeRec.get(), typeRec->host.get(), (unsigned int)status));
|
2021-04-19 16:56:50 +03:00
|
|
|
typeRec->SetExpiration(
|
|
|
|
TimeStamp::NowLoRes(),
|
|
|
|
StaticPrefs::network_dns_negative_ttl_for_type_record(), 0);
|
2020-05-04 22:56:51 +03:00
|
|
|
MOZ_ASSERT(aResult.is<TypeRecordEmpty>());
|
2018-09-22 23:54:11 +03:00
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
2018-09-27 12:28:36 +03:00
|
|
|
typeRec->negative = true;
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_BY_TYPE_FAILED_LOOKUP_TIME, duration);
|
2018-09-22 23:54:11 +03:00
|
|
|
} else {
|
2020-05-04 22:56:51 +03:00
|
|
|
size_t recordCount = 0;
|
|
|
|
if (aResult.is<TypeRecordTxt>()) {
|
|
|
|
recordCount = aResult.as<TypeRecordTxt>().Length();
|
|
|
|
} else if (aResult.is<TypeRecordHTTPSSVC>()) {
|
|
|
|
recordCount = aResult.as<TypeRecordHTTPSSVC>().Length();
|
|
|
|
}
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(
|
|
|
|
("nsHostResolver::CompleteLookupByType record %p [%s], number of "
|
|
|
|
"records %zu\n",
|
2020-05-04 22:56:51 +03:00
|
|
|
typeRec.get(), typeRec->host.get(), recordCount));
|
2018-09-27 12:28:36 +03:00
|
|
|
MutexAutoLock typeLock(typeRec->mResultsLock);
|
2020-05-04 22:56:51 +03:00
|
|
|
typeRec->mResults = aResult;
|
2018-09-27 12:28:36 +03:00
|
|
|
typeRec->SetExpiration(TimeStamp::NowLoRes(), aTtl, mDefaultGracePeriod);
|
|
|
|
typeRec->negative = false;
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_BY_TYPE_SUCCEEDED_LOOKUP_TIME,
|
|
|
|
duration);
|
2018-09-22 23:54:11 +03:00
|
|
|
}
|
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
mozilla::LinkedList<RefPtr<nsResolveHostCallback>> cbs =
|
|
|
|
std::move(typeRec->mCallbacks);
|
2018-09-22 23:54:11 +03:00
|
|
|
|
2018-09-27 12:28:36 +03:00
|
|
|
LOG(
|
|
|
|
("nsHostResolver::CompleteLookupByType record %p calling back dns "
|
|
|
|
"users\n",
|
|
|
|
typeRec.get()));
|
2018-09-22 23:54:11 +03:00
|
|
|
|
|
|
|
for (nsResolveHostCallback* c = cbs.getFirst(); c;
|
|
|
|
c = c->removeAndGetNext()) {
|
|
|
|
c->OnResolveHostComplete(this, rec, status);
|
|
|
|
}
|
|
|
|
|
2021-06-16 11:14:11 +03:00
|
|
|
AddToEvictionQ(rec, aLock);
|
2018-09-22 23:54:11 +03:00
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:15:13 +03:00
|
|
|
void nsHostResolver::CancelAsyncRequest(
|
2020-02-04 22:35:34 +03:00
|
|
|
const nsACString& host, const nsACString& aTrrServer, uint16_t aType,
|
2017-02-14 07:25:35 +03:00
|
|
|
const OriginAttributes& aOriginAttributes, uint16_t flags, uint16_t af,
|
|
|
|
nsIDNSListener* aListener, nsresult status)
|
2012-01-21 03:14:46 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsAutoCString originSuffix;
|
2017-02-14 07:25:35 +03:00
|
|
|
aOriginAttributes.CreateSuffix(originSuffix);
|
|
|
|
|
2012-01-21 03:14:46 +04:00
|
|
|
// Lookup the host record associated with host, flags & address family
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2020-02-04 22:35:34 +03:00
|
|
|
nsHostKey key(host, aTrrServer, aType, flags, af,
|
2018-02-01 12:20:49 +03:00
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0), originSuffix);
|
2018-01-08 01:22:01 +03:00
|
|
|
RefPtr<nsHostRecord> rec = mRecordDB.Get(key);
|
2021-04-07 10:29:53 +03:00
|
|
|
if (!rec) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RefPtr<nsResolveHostCallback> c : rec->mCallbacks) {
|
|
|
|
if (c->EqualsAsyncListener(aListener)) {
|
|
|
|
c->remove();
|
|
|
|
c->OnResolveHostComplete(this, rec.get(), status);
|
|
|
|
break;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2021-04-07 10:29:53 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-04-07 10:29:53 +03:00
|
|
|
// If there are no more callbacks, remove the hash table entry
|
|
|
|
if (rec->mCallbacks.isEmpty()) {
|
|
|
|
mRecordDB.Remove(*static_cast<nsHostKey*>(rec.get()));
|
|
|
|
// If record is on a Queue, remove it
|
2021-06-16 11:14:11 +03:00
|
|
|
mQueue.MaybeRemoveFromQ(rec, lock);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-01-21 03:14:46 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 03:24:12 +04:00
|
|
|
size_t nsHostResolver::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
size_t n = mallocSizeOf(this);
|
2015-07-30 08:28:20 +03:00
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
n += mRecordDB.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& entry : mRecordDB.Values()) {
|
2018-01-08 01:22:01 +03:00
|
|
|
n += entry->SizeOfIncludingThis(mallocSizeOf);
|
2015-07-30 08:28:20 +03:00
|
|
|
}
|
2013-11-25 03:24:12 +04:00
|
|
|
|
|
|
|
// The following fields aren't measured.
|
|
|
|
// - mHighQ, mMediumQ, mLowQ, mEvictionQ, because they just point to
|
2018-01-08 01:22:01 +03:00
|
|
|
// nsHostRecords that also pointed to by entries |mRecordDB|, and
|
|
|
|
// measured when |mRecordDB| is measured.
|
2013-11-25 03:24:12 +04:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:56:02 +03:00
|
|
|
void nsHostResolver::ThreadFunc() {
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG(("DNS lookup thread - starting execution.\n"));
|
2012-06-12 21:06:20 +04:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
|
|
|
nsResState rs;
|
|
|
|
#endif
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> rec;
|
2019-03-19 15:22:12 +03:00
|
|
|
RefPtr<AddrInfo> ai;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-07 10:03:57 +03:00
|
|
|
do {
|
2018-08-09 19:56:02 +03:00
|
|
|
if (!rec) {
|
2018-11-19 18:23:39 +03:00
|
|
|
RefPtr<AddrHostRecord> tmpRec;
|
2018-08-09 19:56:02 +03:00
|
|
|
if (!GetHostToLookup(getter_AddRefs(tmpRec))) {
|
|
|
|
break; // thread shutdown signal
|
|
|
|
}
|
|
|
|
// GetHostToLookup() returns an owning reference
|
|
|
|
MOZ_ASSERT(tmpRec);
|
|
|
|
rec.swap(tmpRec);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-16 19:35:52 +03:00
|
|
|
LOG1(("DNS lookup thread - Calling getaddrinfo for host [%s].\n",
|
|
|
|
rec->host.get()));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-12-17 06:24:17 +04:00
|
|
|
TimeStamp startTime = TimeStamp::Now();
|
2020-12-09 21:14:55 +03:00
|
|
|
bool getTtl = rec->LoadGetTtl();
|
2018-06-25 09:03:44 +03:00
|
|
|
TimeDuration inQueue = startTime - rec->mNativeStart;
|
|
|
|
uint32_t ms = static_cast<uint32_t>(inQueue.ToMilliseconds());
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_NATIVE_QUEUING, ms);
|
2019-03-19 15:22:12 +03:00
|
|
|
nsresult status =
|
|
|
|
GetAddrInfo(rec->host, rec->af, rec->flags, getter_AddRefs(ai), getTtl);
|
2003-09-12 00:32:33 +04:00
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
2014-09-21 02:10:18 +04:00
|
|
|
if (NS_FAILED(status) && rs.Reset()) {
|
2019-03-19 15:22:12 +03:00
|
|
|
status = GetAddrInfo(rec->host, rec->af, rec->flags, getter_AddRefs(ai),
|
|
|
|
getTtl);
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
#endif
|
2015-08-20 19:14:40 +03:00
|
|
|
|
|
|
|
{ // obtain lock to check shutdown and manage inter-module telemetry
|
|
|
|
MutexAutoLock lock(mLock);
|
2012-10-26 16:12:53 +04:00
|
|
|
|
2018-06-18 19:35:16 +03:00
|
|
|
if (!mShutdown) {
|
2018-11-16 19:35:52 +03:00
|
|
|
TimeDuration elapsed = TimeStamp::Now() - startTime;
|
|
|
|
uint32_t millis = static_cast<uint32_t>(elapsed.ToMilliseconds());
|
2015-02-19 08:14:00 +03:00
|
|
|
|
2018-10-30 16:06:24 +03:00
|
|
|
if (NS_SUCCEEDED(status)) {
|
2017-02-15 22:15:15 +03:00
|
|
|
Telemetry::HistogramID histogramID;
|
2018-10-30 16:06:24 +03:00
|
|
|
if (!rec->addr_info_gencnt) {
|
|
|
|
// Time for initial lookup.
|
|
|
|
histogramID = Telemetry::DNS_LOOKUP_TIME;
|
|
|
|
} else if (!getTtl) {
|
2014-09-25 07:14:00 +04:00
|
|
|
// Time for renewal; categorized by expiration strategy.
|
|
|
|
histogramID = Telemetry::DNS_RENEWAL_TIME;
|
|
|
|
} else {
|
|
|
|
// Time to get TTL; categorized by expiration strategy.
|
2018-05-16 23:05:03 +03:00
|
|
|
histogramID = Telemetry::DNS_RENEWAL_TIME_FOR_TTL;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-05-16 23:05:03 +03:00
|
|
|
Telemetry::Accumulate(histogramID, millis);
|
2014-09-25 07:14:00 +04:00
|
|
|
} else {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_FAILED_LOOKUP_TIME, millis);
|
|
|
|
}
|
2018-08-09 19:56:02 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2018-11-16 19:35:52 +03:00
|
|
|
LOG1(("DNS lookup thread - lookup completed for host [%s]: %s.\n",
|
|
|
|
rec->host.get(), ai ? "success" : "failure: unknown host"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-02-17 13:26:41 +03:00
|
|
|
if (LOOKUP_RESOLVEAGAIN ==
|
|
|
|
CompleteLookup(rec, status, ai, rec->pb, rec->originSuffix,
|
2021-02-24 19:37:03 +03:00
|
|
|
rec->mTRRSkippedReason, nullptr)) {
|
2014-09-25 07:14:00 +04:00
|
|
|
// leave 'rec' assigned and loop to make a renewed host resolve
|
2018-05-16 23:05:03 +03:00
|
|
|
LOG(("DNS lookup thread - Re-resolving host [%s].\n", rec->host.get()));
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2018-09-27 12:28:36 +03:00
|
|
|
rec = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-08-09 19:56:02 +03:00
|
|
|
} while (true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-12-22 15:46:50 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2018-07-04 21:36:58 +03:00
|
|
|
mActiveTaskCount--;
|
|
|
|
LOG(("DNS lookup thread - queue empty, task finished.\n"));
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2018-03-21 16:35:53 +03:00
|
|
|
void nsHostResolver::SetCacheLimits(uint32_t aMaxCacheEntries,
|
|
|
|
uint32_t aDefaultCacheEntryLifetime,
|
|
|
|
uint32_t aDefaultGracePeriod) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mMaxCacheEntries = aMaxCacheEntries;
|
|
|
|
mDefaultCacheLifetime = aDefaultCacheEntryLifetime;
|
|
|
|
mDefaultGracePeriod = aDefaultGracePeriod;
|
|
|
|
}
|
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
nsresult nsHostResolver::Create(uint32_t maxCacheEntries,
|
|
|
|
uint32_t defaultCacheEntryLifetime,
|
|
|
|
uint32_t defaultGracePeriod,
|
2003-09-12 00:32:33 +04:00
|
|
|
nsHostResolver** result) {
|
2018-07-03 00:46:16 +03:00
|
|
|
RefPtr<nsHostResolver> res = new nsHostResolver(
|
|
|
|
maxCacheEntries, defaultCacheEntryLifetime, defaultGracePeriod);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
|
|
|
nsresult rv = res->Init();
|
2018-07-03 00:46:16 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2018-07-03 00:46:16 +03:00
|
|
|
res.forget(result);
|
|
|
|
return NS_OK;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
void nsHostResolver::GetDNSCacheEntries(nsTArray<DNSCacheEntries>* args) {
|
2018-05-22 18:29:34 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& recordEntry : mRecordDB) {
|
2015-06-11 04:28:03 +03:00
|
|
|
// We don't pay attention to address literals, only resolved domains.
|
|
|
|
// Also require a host.
|
2021-03-17 18:49:46 +03:00
|
|
|
nsHostRecord* rec = recordEntry.GetWeak();
|
2015-06-11 04:28:03 +03:00
|
|
|
MOZ_ASSERT(rec, "rec should never be null here!");
|
2018-09-27 12:28:36 +03:00
|
|
|
|
|
|
|
if (!rec) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-19 18:23:39 +03:00
|
|
|
// For now we only show A/AAAA records.
|
2018-09-27 12:28:36 +03:00
|
|
|
if (!rec->IsAddrRecord()) {
|
2015-06-11 04:28:03 +03:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
RefPtr<AddrHostRecord> addrRec = do_QueryObject(rec);
|
|
|
|
MOZ_ASSERT(addrRec);
|
|
|
|
if (!addrRec || !addrRec->addr_info) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
DNSCacheEntries info;
|
|
|
|
info.hostname = rec->host;
|
2018-09-27 12:28:36 +03:00
|
|
|
info.family = rec->af;
|
|
|
|
info.expiration =
|
|
|
|
(int64_t)(rec->mValidEnd - TimeStamp::NowLoRes()).ToSeconds();
|
2015-06-11 04:28:03 +03:00
|
|
|
if (info.expiration <= 0) {
|
|
|
|
// We only need valid DNS cache entries
|
2018-09-27 12:28:36 +03:00
|
|
|
continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2018-09-27 12:28:36 +03:00
|
|
|
MutexAutoLock lock(addrRec->addr_info_lock);
|
2020-09-01 10:22:14 +03:00
|
|
|
for (const auto& addr : addrRec->addr_info->Addresses()) {
|
2015-06-11 04:28:03 +03:00
|
|
|
char buf[kIPv6CStrBufSize];
|
2020-09-15 13:39:56 +03:00
|
|
|
if (addr.ToStringBuffer(buf, sizeof(buf))) {
|
2015-06-11 04:28:03 +03:00
|
|
|
info.hostaddr.AppendElement(buf);
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2021-02-18 15:50:02 +03:00
|
|
|
info.TRR = addrRec->addr_info->IsTRROrODoH();
|
2015-06-11 04:28:03 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-03-17 18:49:46 +03:00
|
|
|
info.originAttributesSuffix = recordEntry.GetKey().originSuffix;
|
2021-10-20 17:21:16 +03:00
|
|
|
info.flags = nsPrintfCString("%u|0x%x|%u|%d|%s", rec->type, rec->flags,
|
|
|
|
rec->af, rec->pb, rec->mTrrServer.get());
|
2020-05-20 19:46:26 +03:00
|
|
|
|
2020-05-05 13:40:41 +03:00
|
|
|
args->AppendElement(std::move(info));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
#undef LOG_ENABLED
|