2004-09-01 23:55:58 +04:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 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"
|
|
|
|
#include "nsAutoPtr.h"
|
2015-02-19 08:14:00 +03:00
|
|
|
#include "nsPrintfCString.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"
|
2005-09-23 01:38:12 +04:00
|
|
|
#include "nsURLHelper.h"
|
2012-06-12 21:06:20 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
#include "GetAddrInfo.h"
|
2017-01-05 18:34:26 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2018-02-01 12:20:49 +03:00
|
|
|
#include "TRR.h"
|
|
|
|
#include "TRRService.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"
|
2010-05-20 03:22:19 +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
|
|
|
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.
|
2017-07-06 15:00:35 +03:00
|
|
|
// In particular, thread creation results in a res_init() call from libc which is
|
2008-11-08 02:00:26 +03:00
|
|
|
// 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
|
2017-07-06 15:00:35 +03:00
|
|
|
// the new request is at a lower priority a new thread will only be created if
|
2008-11-08 02:00:26 +03:00
|
|
|
// 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
|
2017-07-06 15:00:35 +03:00
|
|
|
// ShortIdleTimeoutSeconds of idle time. Smaller pools use LongIdleTimeoutSeconds for a
|
2008-11-08 02:00:26 +03:00
|
|
|
// 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
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
LazyLogModule gHostResolverLog("nsHostResolver");
|
|
|
|
#define LOG(args) MOZ_LOG(mozilla::net::gHostResolverLog, mozilla::LogLevel::Debug, args)
|
|
|
|
#define LOG_ENABLED() MOZ_LOG_TEST(mozilla::net::gHostResolverLog, mozilla::LogLevel::Debug)
|
|
|
|
}
|
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2015-02-19 08:14:00 +03:00
|
|
|
#define LOG_HOST(host, interface) host, \
|
|
|
|
(interface && interface[0] != '\0') ? " on interface " : "", \
|
|
|
|
(interface && interface[0] != '\0') ? interface : ""
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if (PR_IntervalToSeconds(PR_IntervalNow() - mLastReset) < 1)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2012-07-17 02:37:28 +04:00
|
|
|
LOG(("Calling 'res_ninit'.\n"));
|
2003-09-12 00:32:33 +04:00
|
|
|
|
|
|
|
mLastReset = PR_IntervalNow();
|
|
|
|
return (res_ninit(&_res) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRIntervalTime mLastReset;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RES_RETRY_ON_FAILURE
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2013-07-20 02:40:00 +04:00
|
|
|
static inline bool
|
|
|
|
IsHighPriority(uint16_t flags)
|
|
|
|
{
|
|
|
|
return !(flags & (nsHostResolver::RES_PRIORITY_LOW | nsHostResolver::RES_PRIORITY_MEDIUM));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
IsMediumPriority(uint16_t flags)
|
|
|
|
{
|
|
|
|
return flags & nsHostResolver::RES_PRIORITY_MEDIUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
IsLowPriority(uint16_t flags)
|
|
|
|
{
|
|
|
|
return flags & nsHostResolver::RES_PRIORITY_LOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2004-09-01 23:55:58 +04:00
|
|
|
// this macro filters out any flags that are not used when constructing the
|
|
|
|
// host key. the significant flags are those that would affect the resulting
|
|
|
|
// host record (i.e., the flags that are passed down to PR_GetAddrInfoByName).
|
|
|
|
#define RES_KEY_FLAGS(_f) ((_f) & nsHostResolver::RES_CANON_NAME)
|
|
|
|
|
2017-12-23 01:08:17 +03:00
|
|
|
bool
|
|
|
|
nsHostKey::operator==(const nsHostKey& other) const
|
|
|
|
{
|
|
|
|
return host == other.host &&
|
|
|
|
RES_KEY_FLAGS (flags) == RES_KEY_FLAGS(other.flags) &&
|
|
|
|
af == other.af &&
|
|
|
|
netInterface == other.netInterface &&
|
|
|
|
originSuffix == other.originSuffix;
|
|
|
|
}
|
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
PLDHashNumber
|
|
|
|
nsHostKey::Hash() const
|
|
|
|
{
|
|
|
|
return AddToHash(HashString(host.get()), RES_KEY_FLAGS(flags), af,
|
|
|
|
HashString(netInterface.get()), HashString(originSuffix.get()));
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:08:17 +03:00
|
|
|
size_t
|
|
|
|
nsHostKey::SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
n += host.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
n += netInterface.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
n += originSuffix.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHostRecord::nsHostRecord(const nsHostKey& key)
|
|
|
|
: nsHostKey(key)
|
|
|
|
, addr_info_lock("nsHostRecord.addr_info_lock")
|
2011-07-21 17:18:01 +04:00
|
|
|
, addr_info_gencnt(0)
|
2016-09-08 01:04:07 +03:00
|
|
|
, addr_info(nullptr)
|
|
|
|
, addr(nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
, negative(false)
|
2018-02-01 12:20:49 +03:00
|
|
|
, mResolving(0)
|
|
|
|
, mNative(false)
|
|
|
|
, mTRRSuccess(0)
|
|
|
|
, mTRRUsed(false)
|
|
|
|
, mNativeUsed(false)
|
|
|
|
, mNativeSuccess(false)
|
|
|
|
, mFirstTRR(nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
, onQueue(false)
|
|
|
|
, usingAnyThread(false)
|
2013-07-20 02:40:00 +04:00
|
|
|
, mDoomed(false)
|
2018-02-01 12:20:49 +03:00
|
|
|
, mDidCallbacks(false)
|
2014-09-21 02:10:18 +04:00
|
|
|
, mGetTtl(false)
|
2018-02-01 12:20:49 +03:00
|
|
|
, mTrrAUsed(INIT)
|
|
|
|
, mTrrAAAAUsed(INIT)
|
2018-02-22 05:22:00 +03:00
|
|
|
, mTrrLock("nsHostRecord.mTrrLock")
|
2014-09-21 02:10:18 +04:00
|
|
|
, mBlacklistedCount(0)
|
2014-09-25 07:14:00 +04:00
|
|
|
, mResolveAgain(false)
|
2011-07-21 17:18:01 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
void
|
|
|
|
nsHostRecord::Cancel()
|
|
|
|
{
|
2018-02-22 05:22:00 +03:00
|
|
|
MutexAutoLock trrlock(mTrrLock);
|
2018-02-01 12:20:49 +03:00
|
|
|
if (mTrrA) {
|
|
|
|
mTrrA->Cancel();
|
2018-02-22 05:22:00 +03:00
|
|
|
mTrrA = nullptr;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
if (mTrrAAAA) {
|
|
|
|
mTrrAAAA->Cancel();
|
2018-02-22 05:22:00 +03:00
|
|
|
mTrrAAAA = nullptr;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
void
|
|
|
|
nsHostRecord::SetExpiration(const mozilla::TimeStamp& now, unsigned int valid, unsigned int grace)
|
|
|
|
{
|
|
|
|
mValidStart = now;
|
|
|
|
mGraceStart = now + TimeDuration::FromSeconds(valid);
|
|
|
|
mValidEnd = now + TimeDuration::FromSeconds(valid + grace);
|
|
|
|
}
|
|
|
|
|
2015-02-03 08:16:00 +03:00
|
|
|
void
|
2015-02-10 16:45:00 +03:00
|
|
|
nsHostRecord::CopyExpirationTimesAndFlagsFrom(const nsHostRecord *aFromHostRecord)
|
2015-02-03 08:16:00 +03:00
|
|
|
{
|
2015-02-10 16:45:00 +03:00
|
|
|
// This is used to copy information from a cache entry to a record. All
|
|
|
|
// information necessary for HasUsableRecord needs to be copied.
|
2015-02-03 08:16:00 +03:00
|
|
|
mValidStart = aFromHostRecord->mValidStart;
|
|
|
|
mValidEnd = aFromHostRecord->mValidEnd;
|
|
|
|
mGraceStart = aFromHostRecord->mGraceStart;
|
2015-02-10 16:45:00 +03:00
|
|
|
mDoomed = aFromHostRecord->mDoomed;
|
2015-02-03 08:16:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
void
|
|
|
|
nsHostRecord::ResolveComplete()
|
|
|
|
{
|
|
|
|
if (mNativeUsed) {
|
|
|
|
if (mNativeSuccess) {
|
|
|
|
uint32_t millis = static_cast<uint32_t>(mNativeDuration.ToMilliseconds());
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_NATIVE_LOOKUP_TIME, millis);
|
|
|
|
}
|
|
|
|
AccumulateCategorical(mNativeSuccess ?
|
|
|
|
Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::osOK :
|
|
|
|
Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::osFail);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTRRUsed) {
|
|
|
|
if (mTRRSuccess) {
|
|
|
|
uint32_t millis = static_cast<uint32_t>(mTrrDuration.ToMilliseconds());
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_TRR_LOOKUP_TIME, millis);
|
|
|
|
}
|
|
|
|
AccumulateCategorical(mTRRSuccess ?
|
|
|
|
Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::trrOK :
|
|
|
|
Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::trrFail);
|
|
|
|
|
|
|
|
if (mTrrAUsed == OK) {
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::trrAOK);
|
|
|
|
} else if (mTrrAUsed == FAILED) {
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::trrAFail);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTrrAAAAUsed == OK) {
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::trrAAAAOK);
|
|
|
|
} else if (mTrrAAAAUsed == FAILED) {
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_DISPOSITION::trrAAAAFail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTRRUsed && mNativeUsed && mNativeSuccess && mTRRSuccess) { // race or shadow!
|
|
|
|
static const TimeDuration k50ms = TimeDuration::FromMilliseconds(50);
|
|
|
|
if (mTrrDuration <= mNativeDuration) {
|
|
|
|
AccumulateCategorical(((mNativeDuration - mTrrDuration) > k50ms) ?
|
|
|
|
Telemetry::LABELS_DNS_TRR_RACE::TRRFasterBy50 :
|
|
|
|
Telemetry::LABELS_DNS_TRR_RACE::TRRFaster);
|
|
|
|
LOG(("nsHostRecord::Complete %s Dns Race: TRR\n", host.get()));
|
|
|
|
} else {
|
|
|
|
AccumulateCategorical(((mTrrDuration - mNativeDuration) > k50ms) ?
|
|
|
|
Telemetry::LABELS_DNS_TRR_RACE::NativeFasterBy50 :
|
|
|
|
Telemetry::LABELS_DNS_TRR_RACE::NativeFaster);
|
|
|
|
LOG(("nsHostRecord::Complete %s Dns Race: NATIVE\n", host.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(mResolverMode) {
|
|
|
|
case MODE_NATIVEONLY:
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_ALGORITHM::nativeOnly);
|
|
|
|
break;
|
|
|
|
case MODE_PARALLEL:
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_ALGORITHM::trrRace);
|
|
|
|
break;
|
|
|
|
case MODE_TRRFIRST:
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_ALGORITHM::trrFirst);
|
|
|
|
break;
|
|
|
|
case MODE_TRRONLY:
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_ALGORITHM::trrOnly);
|
|
|
|
break;
|
|
|
|
case MODE_SHADOW:
|
|
|
|
AccumulateCategorical(Telemetry::LABELS_DNS_LOOKUP_ALGORITHM::trrShadow);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTRRUsed && !mTRRSuccess && mNativeSuccess && gTRRService) {
|
|
|
|
gTRRService->TRRBlacklist(nsCString(host), pb, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-07 09:11:41 +04:00
|
|
|
nsHostRecord::~nsHostRecord()
|
|
|
|
{
|
2017-12-20 03:13:46 +03:00
|
|
|
mCallbacks.clear();
|
|
|
|
|
2014-10-20 21:26:33 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_BLACKLIST_COUNT, mBlacklistedCount);
|
2017-12-04 18:02:59 +03:00
|
|
|
delete addr_info;
|
2003-10-07 09:11:41 +04:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-12-24 01:08:43 +04:00
|
|
|
nsHostRecord::Blacklisted(NetAddr *aQuery)
|
2011-07-21 17:18:01 +04:00
|
|
|
{
|
|
|
|
// must call locked
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Checking blacklist for host [%s%s%s], host record [%p].\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(host.get(), netInterface.get()), this));
|
2011-07-21 17:18:01 +04:00
|
|
|
|
|
|
|
// skip the string conversion for the common case of no blacklist
|
2012-07-17 02:37:28 +04:00
|
|
|
if (!mBlacklistedItems.Length()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-17 02:37:28 +04:00
|
|
|
}
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (!NetAddrToString(aQuery, buf, sizeof(buf))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-17 02:37:28 +04:00
|
|
|
}
|
2011-07-21 17:18:01 +04:00
|
|
|
nsDependentCString strQuery(buf);
|
2012-07-17 02:37:28 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mBlacklistedItems.Length(); i++) {
|
2011-07-21 17:18:01 +04:00
|
|
|
if (mBlacklistedItems.ElementAt(i).Equals(strQuery)) {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Address [%s] is blacklisted for host [%s%s%s].\n", buf,
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(host.get(), netInterface.get())));
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-07-21 17:18:01 +04:00
|
|
|
}
|
2012-07-17 02:37:28 +04:00
|
|
|
}
|
2011-07-21 17:18:01 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-07-21 17:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-24 01:08:43 +04:00
|
|
|
nsHostRecord::ReportUnusable(NetAddr *aAddress)
|
2011-07-21 17:18:01 +04:00
|
|
|
{
|
|
|
|
// must call locked
|
2018-02-01 12:20:49 +03:00
|
|
|
LOG(("Adding address to blacklist for host [%s%s%s], host record [%p]."
|
|
|
|
"used trr=%d\n", LOG_HOST(host.get(), netInterface.get()),
|
|
|
|
this, mTRRSuccess));
|
2011-07-21 17:18:01 +04:00
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
++mBlacklistedCount;
|
|
|
|
|
2013-07-20 02:40:00 +04:00
|
|
|
if (negative)
|
|
|
|
mDoomed = true;
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(aAddress, buf, sizeof(buf))) {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Successfully adding address [%s] to blacklist for host "
|
2017-12-23 01:08:17 +03:00
|
|
|
"[%s%s%s].\n", buf, LOG_HOST(host.get(), netInterface.get())));
|
2011-07-21 17:18:01 +04:00
|
|
|
mBlacklistedItems.AppendElement(nsCString(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostRecord::ResetBlacklist()
|
|
|
|
{
|
|
|
|
// must call locked
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Resetting blacklist for host [%s%s%s], host record [%p].\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(host.get(), netInterface.get()), this));
|
2011-07-21 17:18:01 +04:00
|
|
|
mBlacklistedItems.Clear();
|
|
|
|
}
|
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
nsHostRecord::ExpirationStatus
|
|
|
|
nsHostRecord::CheckExpiration(const mozilla::TimeStamp& now) const {
|
|
|
|
if (!mGraceStart.IsNull() && now >= mGraceStart
|
|
|
|
&& !mValidEnd.IsNull() && now < mValidEnd) {
|
|
|
|
return nsHostRecord::EXP_GRACE;
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
|
|
|
if (!mValidEnd.IsNull() && now < mValidEnd) {
|
2014-09-21 02:10:18 +04:00
|
|
|
return nsHostRecord::EXP_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsHostRecord::EXP_EXPIRED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-20 02:40:00 +04:00
|
|
|
bool
|
2014-09-21 02:10:18 +04:00
|
|
|
nsHostRecord::HasUsableResult(const mozilla::TimeStamp& now, uint16_t queryFlags) const
|
2013-07-20 02:40:00 +04:00
|
|
|
{
|
2014-09-21 02:10:18 +04:00
|
|
|
if (mDoomed) {
|
2013-07-20 02:40:00 +04:00
|
|
|
return false;
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2013-07-20 02:40:00 +04:00
|
|
|
|
|
|
|
// don't use cached negative results for high priority queries.
|
2014-09-21 02:10:18 +04:00
|
|
|
if (negative && IsHighPriority(queryFlags)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CheckExpiration(now) == EXP_EXPIRED) {
|
2013-07-20 02:40:00 +04:00
|
|
|
return false;
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
2013-07-20 02:40:00 +04:00
|
|
|
|
2016-09-08 01:04:07 +03:00
|
|
|
return addr_info || addr || negative;
|
2013-07-20 02:40:00 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 03:24:12 +04:00
|
|
|
static size_t
|
2017-12-20 03:13:46 +03:00
|
|
|
SizeOfResolveHostCallbackListExcludingHead(const mozilla::LinkedList<RefPtr<nsResolveHostCallback>>& aCallbacks,
|
2013-11-25 03:24:12 +04:00
|
|
|
MallocSizeOf mallocSizeOf)
|
|
|
|
{
|
2017-12-20 03:13:53 +03:00
|
|
|
size_t n = aCallbacks.sizeOfExcludingThis(mallocSizeOf);
|
2017-12-20 03:13:46 +03:00
|
|
|
|
|
|
|
for (const nsResolveHostCallback* t = aCallbacks.getFirst(); t; t = t->getNext()) {
|
|
|
|
n += t->SizeOfIncludingThis(mallocSizeOf);
|
2013-11-25 03:24:12 +04:00
|
|
|
}
|
2017-12-20 03:13:46 +03:00
|
|
|
|
2013-11-25 03:24:12 +04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsHostRecord::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = mallocSizeOf(this);
|
|
|
|
|
2017-12-23 01:08:17 +03:00
|
|
|
n += nsHostKey::SizeOfExcludingThis(mallocSizeOf);
|
2017-12-20 03:13:46 +03:00
|
|
|
n += SizeOfResolveHostCallbackListExcludingHead(mCallbacks, mallocSizeOf);
|
2013-11-25 03:24:12 +04:00
|
|
|
n += addr_info ? addr_info->SizeOfIncludingThis(mallocSizeOf) : 0;
|
2017-11-26 10:37:24 +03:00
|
|
|
n += mallocSizeOf(addr.get());
|
2013-11-25 03:24:12 +04:00
|
|
|
|
2015-07-29 09:24:24 +03:00
|
|
|
n += mBlacklistedItems.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2013-11-25 03:24:12 +04:00
|
|
|
for (size_t i = 0; i < mBlacklistedItems.Length(); i++) {
|
2015-12-02 02:36:26 +03:00
|
|
|
n += mBlacklistedItems[i].SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
2013-11-25 03:24:12 +04:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
nsHostRecord::DnsPriority
|
|
|
|
nsHostRecord::GetPriority(uint16_t aFlags)
|
|
|
|
{
|
|
|
|
if (IsHighPriority(aFlags)){
|
|
|
|
return nsHostRecord::DNS_PRIORITY_HIGH;
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
|
|
|
if (IsMediumPriority(aFlags)) {
|
2014-09-21 02:10:18 +04:00
|
|
|
return nsHostRecord::DNS_PRIORITY_MEDIUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsHostRecord::DNS_PRIORITY_LOW;
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:07:00 +04:00
|
|
|
// Returns true if the entry can be removed, or false if it should be left.
|
|
|
|
// Sets mResolveAgain true for entries being resolved right now.
|
2014-09-25 07:14:00 +04:00
|
|
|
bool
|
|
|
|
nsHostRecord::RemoveOrRefresh()
|
|
|
|
{
|
2018-02-01 12:20:49 +03:00
|
|
|
// no need to flush TRRed names, they're not resolved "locally"
|
2018-02-19 11:03:09 +03:00
|
|
|
MutexAutoLock lock(addr_info_lock);
|
2018-02-01 12:20:49 +03:00
|
|
|
if (addr_info && addr_info->IsTRR()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mNative) {
|
2014-10-14 01:07:00 +04:00
|
|
|
if (!onQueue) {
|
|
|
|
// The request has been passed to the OS resolver. The resultant DNS
|
|
|
|
// record should be considered stale and not trusted; set a flag to
|
|
|
|
// ensure it is called again.
|
|
|
|
mResolveAgain = true;
|
|
|
|
}
|
|
|
|
// if Onqueue is true, the host entry is already added to the cache
|
|
|
|
// but is still pending to get resolved: just leave it in hash.
|
2014-09-25 07:14:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
// Already resolved; not in a pending state; remove from cache
|
2014-10-14 01:07:00 +04:00
|
|
|
return true;
|
2014-09-25 07:14:00 +04:00
|
|
|
}
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
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";
|
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
|
|
|
|
2014-10-20 21:26:33 +04:00
|
|
|
static void DnsPrefChanged(const char* aPref, void* aClosure)
|
2014-09-21 02:10:18 +04:00
|
|
|
{
|
2014-09-24 20:56:13 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Should be getting pref changed notification on main thread!");
|
|
|
|
|
2017-08-24 20:03:19 +03:00
|
|
|
DebugOnly<nsHostResolver*> self = static_cast<nsHostResolver*>(aClosure);
|
2014-09-24 20:56:13 +04:00
|
|
|
MOZ_ASSERT(self);
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (!strcmp(aPref, kPrefGetTtl)) {
|
|
|
|
sGetTtlEnabled = Preferences::GetBool(kPrefGetTtl);
|
|
|
|
} 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)
|
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
|
|
|
, mLock("nsHostResolver.mLock")
|
|
|
|
, mIdleThreadCV(mLock, "nsHostResolver.mIdleThreadCV")
|
2015-08-20 19:14:40 +03:00
|
|
|
, mEvictionQSize(0)
|
|
|
|
, mShutdown(true)
|
2008-11-08 02:00:26 +03:00
|
|
|
, mNumIdleThreads(0)
|
2003-09-12 00:32:33 +04:00
|
|
|
, mThreadCount(0)
|
2009-01-14 20:18:27 +03:00
|
|
|
, mActiveAnyThreadCount(0)
|
2008-11-08 02:00:26 +03:00
|
|
|
, mPendingCount(0)
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
|
|
|
mCreationTime = PR_Now();
|
2008-11-08 02:00:26 +03:00
|
|
|
|
|
|
|
mLongIdleTimeout = PR_SecondsToInterval(LongIdleTimeoutSeconds);
|
|
|
|
mShortIdleTimeout = PR_SecondsToInterval(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
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::Init()
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:16:00 +03:00
|
|
|
LOG(("nsHostResolver::Init this=%p", this));
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdown = false;
|
2006-05-25 20:49:48 +04: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) {
|
2012-07-17 02:37:28 +04:00
|
|
|
LOG(("Calling 'res_ninit'.\n"));
|
2006-05-25 20:49:48 +04:00
|
|
|
res_ninit(&_res);
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
void
|
2018-02-20 19:41:50 +03:00
|
|
|
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()) {
|
|
|
|
for (RefPtr<nsHostRecord> rec : aPendingQ) {
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->Cancel();
|
|
|
|
CompleteLookup(rec, NS_ERROR_ABORT, nullptr, rec->pb);
|
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
|
|
|
|
// cache that have 'Resolve' set true but not 'onQueue' are being resolved
|
|
|
|
// right now, so we need to mark them to get re-resolved on completion!
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::FlushCache()
|
|
|
|
{
|
2015-06-11 03:09:32 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mEvictionQSize = 0;
|
|
|
|
|
|
|
|
// Clear the evictionQ and remove all its corresponding entries from
|
|
|
|
// the cache first
|
2018-02-20 19:41:50 +03:00
|
|
|
if (!mEvictionQ.isEmpty()) {
|
|
|
|
for (RefPtr<nsHostRecord> rec : mEvictionQ) {
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->Cancel();
|
2018-01-08 01:22:01 +03:00
|
|
|
mRecordDB.Remove(*static_cast<nsHostKey *>(rec));
|
2015-06-11 03:09:32 +03:00
|
|
|
}
|
2018-02-20 19:41:50 +03:00
|
|
|
mEvictionQ.clear();
|
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-01-08 01:22:01 +03:00
|
|
|
if (record->RemoveOrRefresh()) {
|
2018-02-20 19:41:50 +03:00
|
|
|
if (record->isInList()) {
|
|
|
|
record->remove();
|
|
|
|
}
|
2015-06-11 04:28:03 +03:00
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
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 = Preferences::UnregisterCallback(
|
2014-10-20 21:26:33 +04:00
|
|
|
&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
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
// Move queues to temporary lists.
|
|
|
|
pendingQHigh = mozilla::Move(mHighQ);
|
|
|
|
pendingQMed = mozilla::Move(mMediumQ);
|
|
|
|
pendingQLow = mozilla::Move(mLowQ);
|
|
|
|
evictionQ = mozilla::Move(mEvictionQ);
|
|
|
|
|
2006-04-29 08:37:07 +04:00
|
|
|
mEvictionQSize = 0;
|
2008-11-08 02:00:26 +03:00
|
|
|
mPendingCount = 0;
|
2014-09-25 07:14:00 +04:00
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
if (mNumIdleThreads)
|
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
|
|
|
mIdleThreadCV.NotifyAll();
|
2014-09-25 07:14:00 +04:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
// empty host database
|
2018-01-08 01:22:01 +03:00
|
|
|
mRecordDB.Clear();
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2014-09-25 07:14:00 +04:00
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
ClearPendingQueue(pendingQHigh);
|
|
|
|
ClearPendingQueue(pendingQMed);
|
|
|
|
ClearPendingQueue(pendingQLow);
|
2006-04-29 08:37:07 +04:00
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
if (!evictionQ.isEmpty()) {
|
|
|
|
for (RefPtr<nsHostRecord> rec : evictionQ) {
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->Cancel();
|
2006-04-29 08:37:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
pendingQHigh.clear();
|
|
|
|
pendingQMed.clear();
|
|
|
|
pendingQLow.clear();
|
|
|
|
evictionQ.clear();
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
for (auto iter = mRecordDB.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->Cancel();
|
|
|
|
}
|
2008-11-08 02:00:26 +03:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
// Logically join the outstanding worker threads with a timeout.
|
2017-07-06 15:00:35 +03:00
|
|
|
// Use this approach instead of PR_JoinThread() because that does
|
|
|
|
// not allow a timeout which may be necessary for a semi-responsive
|
2008-11-08 02:00:26 +03:00
|
|
|
// shutdown if the thread is blocked on a very slow DNS resolution.
|
2017-07-06 15:00:35 +03:00
|
|
|
// mThreadCount is read outside of mLock, but the worst case
|
2008-11-08 02:00:26 +03:00
|
|
|
// scenario for that race is one extra 25ms sleep.
|
|
|
|
|
|
|
|
PRIntervalTime delay = PR_MillisecondsToInterval(25);
|
|
|
|
PRIntervalTime stopTime = PR_IntervalNow() + PR_SecondsToInterval(20);
|
|
|
|
while (mThreadCount && PR_IntervalNow() < stopTime)
|
|
|
|
PR_Sleep(delay);
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsresult
|
|
|
|
nsHostResolver::GetHostRecord(const char *host,
|
|
|
|
uint16_t flags, uint16_t af, bool pb,
|
|
|
|
const nsCString &netInterface,
|
|
|
|
const nsCString &originSuffix,
|
|
|
|
nsHostRecord **result)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
nsHostKey key(nsCString(host), flags, af, pb,
|
|
|
|
netInterface, originSuffix);
|
|
|
|
|
|
|
|
RefPtr<nsHostRecord>& entry = mRecordDB.GetOrInsert(key);
|
|
|
|
if (!entry) {
|
|
|
|
entry = new nsHostRecord(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsHostRecord> rec = entry;
|
|
|
|
if (rec->addr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (rec->mResolving) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*result = rec.forget().take();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
nsresult
|
2017-02-14 07:25:35 +03:00
|
|
|
nsHostResolver::ResolveHost(const char *host,
|
|
|
|
const OriginAttributes &aOriginAttributes,
|
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af,
|
|
|
|
const char *netInterface,
|
2017-12-20 03:13:46 +03:00
|
|
|
nsResolveHostCallback *aCallback)
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(host && *host, NS_ERROR_UNEXPECTED);
|
2015-02-19 08:14:00 +03:00
|
|
|
NS_ENSURE_TRUE(netInterface, NS_ERROR_UNEXPECTED);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Resolving host [%s%s%s]%s.\n", LOG_HOST(host, netInterface),
|
|
|
|
flags & RES_BYPASS_CACHE ? " - bypassing cache" : ""));
|
2003-10-26 02:48:33 +03:00
|
|
|
|
2005-09-23 01:38:12 +04:00
|
|
|
// ensure that we are working with a valid hostname before proceeding. see
|
|
|
|
// bug 304904 for details.
|
|
|
|
if (!net_IsValidHostName(nsDependentCString(host)))
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
|
2017-12-20 03:13:46 +03:00
|
|
|
RefPtr<nsResolveHostCallback> callback(aCallback);
|
2003-10-07 09:11:41 +04:00
|
|
|
// if result is set inside the lock, then we need to issue the
|
|
|
|
// callback before returning.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHostRecord> result;
|
2003-10-07 09:11:41 +04:00
|
|
|
nsresult status = NS_OK, rv = NS_OK;
|
|
|
|
{
|
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-02-01 12:20:49 +03:00
|
|
|
if (mShutdown) {
|
2003-10-07 09:11:41 +04:00
|
|
|
rv = NS_ERROR_NOT_INITIALIZED;
|
2018-02-01 12:20:49 +03:00
|
|
|
} else {
|
2012-12-24 01:08:43 +04:00
|
|
|
// Used to try to parse to an IP address literal.
|
2003-10-07 09:11:41 +04:00
|
|
|
PRNetAddr tempAddr;
|
2012-12-24 01:08:43 +04:00
|
|
|
// Unfortunately, PR_StringToNetAddr does not properly initialize
|
|
|
|
// the output buffer in the case of IPv6 input. See bug 223145.
|
2003-11-03 11:54:49 +03:00
|
|
|
memset(&tempAddr, 0, sizeof(PRNetAddr));
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2003-10-07 09:11:41 +04: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.
|
2017-02-14 07:25:35 +03:00
|
|
|
nsAutoCString originSuffix;
|
|
|
|
aOriginAttributes.CreateSuffix(originSuffix);
|
2003-10-07 09:11:41 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsHostKey key(nsCString(host), flags, af,
|
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0),
|
|
|
|
nsCString(netInterface),
|
2017-12-23 01:08:17 +03:00
|
|
|
originSuffix);
|
2018-01-08 01:22:01 +03:00
|
|
|
RefPtr<nsHostRecord>& entry = mRecordDB.GetOrInsert(key);
|
|
|
|
if (!entry) {
|
|
|
|
entry = new nsHostRecord(key);
|
2013-11-22 01:35:44 +04:00
|
|
|
}
|
2018-01-08 01:22:01 +03:00
|
|
|
|
|
|
|
RefPtr<nsHostRecord> rec = entry;
|
|
|
|
MOZ_ASSERT(rec, "Record should not be null");
|
|
|
|
if (!(flags & RES_BYPASS_CACHE) &&
|
|
|
|
rec->HasUsableResult(TimeStamp::NowLoRes(), flags)) {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" Using cached record for host [%s%s%s].\n",
|
|
|
|
LOG_HOST(host, netInterface)));
|
2003-10-07 09:11:41 +04:00
|
|
|
// put reference to host record on stack...
|
2018-01-08 01:22:01 +03:00
|
|
|
result = rec;
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
|
2011-12-17 06:24:12 +04:00
|
|
|
|
2014-03-09 19:00:18 +04:00
|
|
|
// For entries that are in the grace period
|
2013-08-16 22:49:57 +04:00
|
|
|
// or all cached negative entries, use the cache but start a new
|
|
|
|
// lookup in the background
|
2018-01-08 01:22:01 +03:00
|
|
|
ConditionallyRefreshRecord(rec, host);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
if (rec->negative) {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" Negative cache entry for host [%s%s%s].\n",
|
|
|
|
LOG_HOST(host, netInterface)));
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-17 06:24:17 +04:00
|
|
|
METHOD_NEGATIVE_HIT);
|
2008-10-14 01:04:09 +04:00
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (rec->addr) {
|
|
|
|
// if the host name is an IP address literal and has been parsed,
|
|
|
|
// go ahead and use it.
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG((" Using cached address for IP Literal [%s].\n", host));
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-17 06:24:17 +04:00
|
|
|
METHOD_LITERAL);
|
2018-01-08 01:22:01 +03:00
|
|
|
result = rec;
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (PR_StringToNetAddr(host, &tempAddr) == PR_SUCCESS) {
|
|
|
|
// 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.)
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG((" Host is IP Literal [%s].\n", host));
|
2003-10-07 09:11:41 +04:00
|
|
|
// ok, just copy the result into the host record, and be done
|
|
|
|
// with it! ;-)
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->addr = MakeUnique<NetAddr>();
|
|
|
|
PRNetAddrToNetAddr(&tempAddr, rec->addr.get());
|
2003-10-07 09:11:41 +04:00
|
|
|
// put reference to host record on stack...
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-17 06:24:17 +04:00
|
|
|
METHOD_LITERAL);
|
2018-01-08 01:22:01 +03:00
|
|
|
result = rec;
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (mPendingCount >= MAX_NON_PRIORITY_REQUESTS &&
|
|
|
|
!IsHighPriority(flags) &&
|
|
|
|
!rec->mResolving) {
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG((" Lookup queue full: dropping %s priority request for "
|
2015-02-19 08:14:00 +03:00
|
|
|
"host [%s%s%s].\n",
|
|
|
|
IsMediumPriority(flags) ? "medium" : "low",
|
|
|
|
LOG_HOST(host, netInterface)));
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-17 06:24:17 +04:00
|
|
|
METHOD_OVERFLOW);
|
2008-11-08 02:00:26 +03:00
|
|
|
// This is a lower priority request and we are swamped, so refuse it.
|
|
|
|
rv = NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (flags & RES_OFFLINE) {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" Offline request for host [%s%s%s]; ignoring.\n",
|
|
|
|
LOG_HOST(host, netInterface)));
|
2012-09-18 03:45:10 +04:00
|
|
|
rv = NS_ERROR_OFFLINE;
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (!rec->mResolving) {
|
|
|
|
// If this is an IPV4 or IPV6 specific request, check if there is
|
|
|
|
// an AF_UNSPEC entry we can use. Otherwise, hit the resolver...
|
2012-09-18 03:45:10 +04:00
|
|
|
|
2013-08-16 22:49:57 +04:00
|
|
|
if (!(flags & RES_BYPASS_CACHE) &&
|
|
|
|
((af == PR_AF_INET) || (af == PR_AF_INET6))) {
|
|
|
|
// First, search for an entry with AF_UNSPEC
|
2017-12-23 01:08:17 +03:00
|
|
|
const nsHostKey unspecKey(nsCString(host), flags, PR_AF_UNSPEC,
|
2018-02-01 12:20:49 +03:00
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0),
|
2017-12-23 01:08:17 +03:00
|
|
|
nsCString(netInterface), originSuffix);
|
2018-01-08 01:22:01 +03:00
|
|
|
RefPtr<nsHostRecord> unspecRec = mRecordDB.Get(unspecKey);
|
|
|
|
|
2015-02-10 16:45:00 +03:00
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
2018-01-08 01:22:01 +03:00
|
|
|
if (unspecRec && unspecRec->HasUsableResult(now, flags)) {
|
2013-11-22 23:15:52 +04:00
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
MOZ_ASSERT(unspecRec->addr_info || unspecRec->negative,
|
2013-11-22 23:15:52 +04:00
|
|
|
"Entry should be resolved or negative.");
|
|
|
|
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" Trying AF_UNSPEC entry for host [%s%s%s] af: %s.\n",
|
|
|
|
LOG_HOST(host, netInterface),
|
|
|
|
(af == PR_AF_INET) ? "AF_INET" : "AF_INET6"));
|
2015-07-23 05:12:29 +03:00
|
|
|
|
2017-12-01 18:05:16 +03:00
|
|
|
// We need to lock in case any other thread is reading
|
|
|
|
// addr_info.
|
2018-01-08 01:22:01 +03:00
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
2017-12-01 18:05:16 +03:00
|
|
|
|
2017-12-04 18:03:02 +03:00
|
|
|
// XXX: note that this actually leaks addr_info.
|
|
|
|
// For some reason, freeing the memory causes a crash in
|
|
|
|
// nsDNSRecord::GetNextAddr - see bug 1422173
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->addr_info = nullptr;
|
|
|
|
if (unspecRec->negative) {
|
|
|
|
rec->negative = unspecRec->negative;
|
|
|
|
rec->CopyExpirationTimesAndFlagsFrom(unspecRec);
|
|
|
|
} else if (unspecRec->addr_info) {
|
2013-11-22 23:15:52 +04:00
|
|
|
// Search for any valid address in the AF_UNSPEC entry
|
|
|
|
// in the cache (not blacklisted and from the right
|
|
|
|
// family).
|
|
|
|
NetAddrElement *addrIter =
|
2018-01-08 01:22:01 +03:00
|
|
|
unspecRec->addr_info->mAddresses.getFirst();
|
2013-11-22 23:15:52 +04:00
|
|
|
while (addrIter) {
|
|
|
|
if ((af == addrIter->mAddress.inet.family) &&
|
2018-01-08 01:22:01 +03:00
|
|
|
!unspecRec->Blacklisted(&addrIter->mAddress)) {
|
|
|
|
if (!rec->addr_info) {
|
|
|
|
rec->addr_info = new AddrInfo(
|
|
|
|
unspecRec->addr_info->mHostName,
|
|
|
|
unspecRec->addr_info->mCanonicalName);
|
|
|
|
rec->CopyExpirationTimesAndFlagsFrom(unspecRec);
|
2013-11-22 23:15:52 +04:00
|
|
|
}
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->addr_info->AddAddress(
|
2013-11-22 23:15:52 +04:00
|
|
|
new NetAddrElement(*addrIter));
|
2013-08-16 22:49:57 +04:00
|
|
|
}
|
2013-11-22 23:15:52 +04:00
|
|
|
addrIter = addrIter->getNext();
|
2013-08-16 22:49:57 +04:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 16:45:00 +03:00
|
|
|
// Now check if we have a new record.
|
2018-01-08 01:22:01 +03:00
|
|
|
if (rec->HasUsableResult(now, flags)) {
|
|
|
|
result = rec;
|
|
|
|
if (rec->negative) {
|
2015-02-10 16:45:00 +03:00
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2013-08-16 22:49:57 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
|
|
|
METHOD_HIT);
|
2018-01-08 01:22:01 +03:00
|
|
|
ConditionallyRefreshRecord(rec, host);
|
2018-02-01 12:20:49 +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.
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG((" No AF_INET6 in AF_UNSPEC entry: "
|
2015-02-19 08:14:00 +03:00
|
|
|
"host [%s%s%s] unknown host.",
|
|
|
|
LOG_HOST(host, netInterface)));
|
2018-01-08 01:22:01 +03:00
|
|
|
result = rec;
|
|
|
|
rec->negative = true;
|
2013-08-16 22:49:57 +04:00
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
|
|
|
METHOD_NEGATIVE_HIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If no valid address was found in the cache or this is an
|
|
|
|
// AF_UNSPEC request, then start a new lookup.
|
|
|
|
if (!result) {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" No usable address in cache for host [%s%s%s].",
|
|
|
|
LOG_HOST(host, netInterface)));
|
|
|
|
|
2013-08-16 22:49:57 +04:00
|
|
|
// Add callback to the list of pending callbacks.
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->mCallbacks.insertBack(callback);
|
|
|
|
rec->flags = flags;
|
2018-02-01 12:20:49 +03:00
|
|
|
rv = NameLookup(rec);
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-17 06:24:17 +04:00
|
|
|
METHOD_NETWORK_FIRST);
|
2017-12-20 03:13:46 +03:00
|
|
|
if (NS_FAILED(rv) && callback->isInList()) {
|
|
|
|
callback->remove();
|
2018-02-01 12:20:49 +03:00
|
|
|
} else {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" DNS lookup for host [%s%s%s] blocking "
|
|
|
|
"pending 'getaddrinfo' query: callback [%p]",
|
2017-12-20 03:13:46 +03:00
|
|
|
LOG_HOST(host, netInterface), callback.get()));
|
2013-08-16 22:49:57 +04:00
|
|
|
}
|
2003-10-07 09:11:41 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
} else {
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG((" Host [%s%s%s] is being resolved. Appending callback "
|
2017-12-20 03:13:46 +03:00
|
|
|
"[%p].", LOG_HOST(host, netInterface), callback.get()));
|
2015-02-19 08:14:00 +03:00
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->mCallbacks.insertBack(callback);
|
|
|
|
if (rec->onQueue) {
|
2012-06-20 04:38:44 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-17 06:24:17 +04:00
|
|
|
METHOD_NETWORK_SHARED);
|
|
|
|
|
2013-08-16 22:49:57 +04:00
|
|
|
// Consider the case where we are on a pending queue of
|
2008-11-08 02:00:26 +03:00
|
|
|
// lower priority than the request is being made at.
|
|
|
|
// In that case we should upgrade to the higher queue.
|
|
|
|
|
2013-08-16 22:49:57 +04:00
|
|
|
if (IsHighPriority(flags) &&
|
2018-01-08 01:22:01 +03:00
|
|
|
!IsHighPriority(rec->flags)) {
|
2008-11-08 02:00:26 +03:00
|
|
|
// Move from (low|med) to high.
|
2018-02-20 19:41:50 +03:00
|
|
|
NS_ASSERTION(rec->onQueue, "Moving Host Record Not Currently Queued");
|
|
|
|
rec->remove();
|
|
|
|
mHighQ.insertBack(rec);
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->flags = flags;
|
|
|
|
ConditionallyCreateThread(rec);
|
2013-08-16 22:49:57 +04:00
|
|
|
} else if (IsMediumPriority(flags) &&
|
2018-01-08 01:22:01 +03:00
|
|
|
IsLowPriority(rec->flags)) {
|
2008-11-08 02:00:26 +03:00
|
|
|
// Move from low to med.
|
2018-02-20 19:41:50 +03:00
|
|
|
NS_ASSERTION(rec->onQueue, "Moving Host Record Not Currently Queued");
|
|
|
|
rec->remove();
|
|
|
|
mMediumQ.insertBack(rec);
|
2018-01-08 01:22:01 +03:00
|
|
|
rec->flags = flags;
|
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
|
|
|
mIdleThreadCV.Notify();
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
}
|
2003-10-07 09:11:41 +04:00
|
|
|
}
|
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2017-12-20 03:13:46 +03:00
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
if (result) {
|
2017-12-20 03:13:46 +03:00
|
|
|
if (callback->isInList()) {
|
|
|
|
callback->remove();
|
|
|
|
}
|
2017-12-08 01:19:48 +03:00
|
|
|
callback->OnResolveHostComplete(this, result, status);
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-02-14 07:25:35 +03:00
|
|
|
nsHostResolver::DetachCallback(const char *host,
|
|
|
|
const OriginAttributes &aOriginAttributes,
|
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af,
|
|
|
|
const char *netInterface,
|
2017-12-20 03:13:46 +03:00
|
|
|
nsResolveHostCallback *aCallback,
|
2017-02-14 07:25:35 +03:00
|
|
|
nsresult status)
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
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);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2017-02-14 07:25:35 +03:00
|
|
|
nsAutoCString originSuffix;
|
|
|
|
aOriginAttributes.CreateSuffix(originSuffix);
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsHostKey key(nsCString(host), flags, af,
|
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0),
|
|
|
|
nsCString(netInterface),
|
2017-12-23 01:08:17 +03:00
|
|
|
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!
|
2017-12-20 03:13:46 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-11-08 02:00:26 +03:00
|
|
|
nsHostResolver::ConditionallyCreateThread(nsHostRecord *rec)
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
2008-11-08 02:00:26 +03:00
|
|
|
if (mNumIdleThreads) {
|
2003-09-12 00:32:33 +04:00
|
|
|
// wake up idle thread to process this lookup
|
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
|
|
|
mIdleThreadCV.Notify();
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2008-11-08 02:00:26 +03:00
|
|
|
else if ((mThreadCount < HighThreadThreshold) ||
|
|
|
|
(IsHighPriority(rec->flags) && mThreadCount < MAX_RESOLVER_THREADS)) {
|
2003-09-12 00:32:33 +04:00
|
|
|
// dispatch new worker thread
|
|
|
|
NS_ADDREF_THIS(); // owning reference passed to thread
|
2008-11-08 02:00:26 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
mThreadCount++;
|
|
|
|
PRThread *thr = PR_CreateThread(PR_SYSTEM_THREAD,
|
|
|
|
ThreadFunc,
|
2009-01-14 20:18:27 +03:00
|
|
|
this,
|
2003-09-12 00:32:33 +04:00
|
|
|
PR_PRIORITY_NORMAL,
|
|
|
|
PR_GLOBAL_THREAD,
|
|
|
|
PR_UNJOINABLE_THREAD,
|
|
|
|
0);
|
|
|
|
if (!thr) {
|
|
|
|
mThreadCount--;
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 08:14:00 +03:00
|
|
|
else {
|
|
|
|
LOG((" Unable to find a thread for looking up host [%s%s%s].\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get())));
|
2015-02-19 08:14:00 +03:00
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-22 05:22:00 +03:00
|
|
|
// make sure the mTrrLock is held when this is used!
|
2018-02-01 12:20:49 +03:00
|
|
|
#define TRROutstanding() ((rec->mTrrA || rec->mTrrAAAA))
|
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
nsresult
|
2018-02-01 12:20:49 +03:00
|
|
|
nsHostResolver::TrrLookup_unlocked(nsHostRecord *rec, TRR *pushedTRR)
|
2008-11-08 02:00:26 +03:00
|
|
|
{
|
2018-02-01 12:20:49 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return TrrLookup(rec, pushedTRR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns error if no TRR resolve is issued
|
|
|
|
// it is impt this is not called while a native lookup is going on
|
|
|
|
nsresult
|
2018-02-20 19:41:50 +03:00
|
|
|
nsHostResolver::TrrLookup(nsHostRecord *aRec, TRR *pushedTRR)
|
2018-02-01 12:20:49 +03:00
|
|
|
{
|
2018-02-20 19:41:50 +03:00
|
|
|
RefPtr<nsHostRecord> rec(aRec);
|
2018-02-01 12:20:49 +03:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
2018-02-22 05:22:00 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
MutexAutoLock trrlock(rec->mTrrLock);
|
|
|
|
MOZ_ASSERT(!TRROutstanding());
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(!rec->mResolving);
|
|
|
|
|
|
|
|
if (!gTRRService || !gTRRService->Enabled()) {
|
|
|
|
LOG(("TrrLookup:: %s service not enabled\n", rec->host.get()));
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
if (rec->isInList()) {
|
2018-02-01 12:20:49 +03:00
|
|
|
// we're already on the eviction queue. This is a renewal
|
|
|
|
MOZ_ASSERT(mEvictionQSize);
|
2018-02-20 19:41:50 +03:00
|
|
|
AssertOnQ(rec, mEvictionQ);
|
|
|
|
|
|
|
|
rec->remove();
|
2018-02-01 12:20:49 +03:00
|
|
|
mEvictionQSize--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec->mTRRSuccess = 0; // bump for each successful TRR response
|
|
|
|
rec->mTrrAUsed = nsHostRecord::INIT;
|
|
|
|
rec->mTrrAAAAUsed = nsHostRecord::INIT;
|
|
|
|
|
|
|
|
if (gTRRService && gTRRService->IsTRRBlacklisted(rec->host, rec->pb, true)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_TRR_BLACKLISTED, true);
|
|
|
|
MOZ_ASSERT(!rec->mTRRUsed);
|
|
|
|
// not really an error but no TRR is issued
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_TRR_BLACKLISTED, false);
|
|
|
|
|
|
|
|
rec->mTrrStart = TimeStamp::Now();
|
|
|
|
rec->mTRRUsed = true; // this record gets TRR treatment
|
|
|
|
|
|
|
|
// If asking for AF_UNSPEC, issue both A and AAAA.
|
|
|
|
// If asking for AF_INET6 or AF_INET, do only that single type
|
|
|
|
enum TrrType rectype = (rec->af == AF_INET6)? TRRTYPE_AAAA : TRRTYPE_A;
|
|
|
|
if (pushedTRR) {
|
|
|
|
rectype = pushedTRR->Type();
|
|
|
|
}
|
|
|
|
bool sendAgain;
|
|
|
|
|
|
|
|
bool madeQuery = false;
|
|
|
|
do {
|
|
|
|
sendAgain = false;
|
|
|
|
LOG(("TRR Resolve %s type %d\n", rec->host.get(), (int)rectype));
|
|
|
|
RefPtr<TRR> trr;
|
2018-02-22 05:22:00 +03:00
|
|
|
MutexAutoLock trrlock(rec->mTrrLock);
|
2018-02-01 12:20:49 +03:00
|
|
|
trr = pushedTRR ? pushedTRR : new TRR(this, rec, rectype);
|
|
|
|
if (pushedTRR || NS_SUCCEEDED(NS_DispatchToMainThread(trr))) {
|
|
|
|
rec->mResolving++;
|
|
|
|
if (rectype == TRRTYPE_A) {
|
|
|
|
MOZ_ASSERT(!rec->mTrrA);
|
|
|
|
rec->mTrrA = trr;
|
|
|
|
rec->mTrrAUsed = nsHostRecord::STARTED;
|
|
|
|
} else if (rectype == TRRTYPE_AAAA) {
|
|
|
|
MOZ_ASSERT(!rec->mTrrAAAA);
|
|
|
|
rec->mTrrAAAA = trr;
|
|
|
|
rec->mTrrAAAAUsed = nsHostRecord::STARTED;
|
|
|
|
} else {
|
|
|
|
LOG(("TrrLookup called with bad type set: %d\n", rectype));
|
|
|
|
MOZ_ASSERT(0);
|
|
|
|
}
|
|
|
|
madeQuery = true;
|
|
|
|
if (!pushedTRR && (rec->af == AF_UNSPEC) && (rectype == TRRTYPE_A)) {
|
|
|
|
rectype = TRRTYPE_AAAA;
|
|
|
|
sendAgain = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (sendAgain);
|
|
|
|
|
|
|
|
return madeQuery ? NS_OK : NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-20 19:41:50 +03:00
|
|
|
nsHostResolver::AssertOnQ(nsHostRecord *rec, LinkedList<RefPtr<nsHostRecord>>& q)
|
2018-02-01 12:20:49 +03:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2018-02-20 19:41:50 +03:00
|
|
|
MOZ_ASSERT(!q.isEmpty());
|
|
|
|
MOZ_ASSERT(rec->isInList());
|
|
|
|
for (RefPtr<nsHostRecord> r: q) {
|
|
|
|
if (rec == r) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-02-20 19:41:50 +03:00
|
|
|
MOZ_ASSERT(false, "Did not find element");
|
2018-02-01 12:20:49 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2018-02-20 19:41:50 +03:00
|
|
|
nsHostResolver::NativeLookup(nsHostRecord *aRec)
|
2018-02-01 12:20:49 +03:00
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2018-02-20 19:41:50 +03:00
|
|
|
RefPtr<nsHostRecord> rec(aRec);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
rec->mNativeStart = TimeStamp::Now();
|
2008-11-08 02:00:26 +03:00
|
|
|
|
|
|
|
// Add rec to one of the pending queues, possibly removing it from mEvictionQ.
|
2018-02-20 19:41:50 +03:00
|
|
|
if (rec->isInList()) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(mEvictionQSize);
|
2018-02-20 19:41:50 +03:00
|
|
|
AssertOnQ(rec, mEvictionQ);
|
|
|
|
rec->remove(); // was on the eviction queue
|
2008-11-08 02:00:26 +03:00
|
|
|
mEvictionQSize--;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
switch (nsHostRecord::GetPriority(rec->flags)) {
|
|
|
|
case nsHostRecord::DNS_PRIORITY_HIGH:
|
2018-02-20 19:41:50 +03:00
|
|
|
mHighQ.insertBack(rec);
|
2014-09-21 02:10:18 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsHostRecord::DNS_PRIORITY_MEDIUM:
|
2018-02-20 19:41:50 +03:00
|
|
|
mMediumQ.insertBack(rec);
|
2014-09-21 02:10:18 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsHostRecord::DNS_PRIORITY_LOW:
|
2018-02-20 19:41:50 +03:00
|
|
|
mLowQ.insertBack(rec);
|
2014-09-21 02:10:18 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-11-08 02:00:26 +03:00
|
|
|
mPendingCount++;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->mNative = true;
|
|
|
|
rec->mNativeUsed = true;
|
2011-10-17 18:59:28 +04:00
|
|
|
rec->onQueue = true;
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->mResolving++;
|
2008-11-08 02:00:26 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsresult rv = ConditionallyCreateThread(rec);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG ((" DNS thread counters: total=%d any-live=%d idle=%d pending=%d\n",
|
2015-08-20 19:14:40 +03:00
|
|
|
static_cast<uint32_t>(mThreadCount),
|
|
|
|
static_cast<uint32_t>(mActiveAnyThreadCount),
|
|
|
|
static_cast<uint32_t>(mNumIdleThreads),
|
|
|
|
static_cast<uint32_t>(mPendingCount)));
|
2008-11-08 02:00:26 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
ResolverMode
|
|
|
|
nsHostResolver::Mode()
|
|
|
|
{
|
|
|
|
if (gTRRService) {
|
|
|
|
return static_cast<ResolverMode>(gTRRService->Mode());
|
|
|
|
}
|
|
|
|
|
|
|
|
return MODE_NATIVEONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kick-off a name resolve operation, using native resolver and/or TRR
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::NameLookup(nsHostRecord *rec)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
if (rec->mResolving) {
|
|
|
|
LOG(("NameLookup %s while already resolving\n", rec->host.get()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolverMode mode = rec->mResolverMode = Mode();
|
|
|
|
|
|
|
|
rec->mNativeUsed = false;
|
|
|
|
rec->mTRRUsed = false;
|
|
|
|
rec->mNativeSuccess = false;
|
|
|
|
rec->mTRRSuccess = 0;
|
|
|
|
rec->mDidCallbacks = false;
|
|
|
|
rec->mTrrAUsed = nsHostRecord::INIT;
|
|
|
|
rec->mTrrAAAAUsed = nsHostRecord::INIT;
|
|
|
|
|
|
|
|
if (rec->flags & RES_DISABLE_TRR) {
|
|
|
|
if (mode == MODE_TRRONLY) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mode = MODE_NATIVEONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != MODE_NATIVEONLY) {
|
|
|
|
rv = TrrLookup(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mode == MODE_PARALLEL) ||
|
|
|
|
(mode == MODE_NATIVEONLY) ||
|
|
|
|
(mode == MODE_SHADOW) ||
|
|
|
|
((mode == MODE_TRRFIRST) && NS_FAILED(rv))) {
|
|
|
|
rv = NativeLookup(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-08-16 22:49:57 +04:00
|
|
|
nsresult
|
|
|
|
nsHostResolver::ConditionallyRefreshRecord(nsHostRecord *rec, const char *host)
|
|
|
|
{
|
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) && !rec->mResolving) {
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG((" Using %s cache entry for host [%s] but starting async renewal.",
|
2013-08-16 22:49:57 +04:00
|
|
|
rec->negative ? "negative" :"positive", host));
|
2018-02-01 12:20:49 +03:00
|
|
|
NameLookup(rec);
|
2013-08-16 22:49:57 +04:00
|
|
|
|
|
|
|
if (!rec->negative) {
|
|
|
|
// negative entries are constantly being refreshed, only
|
|
|
|
// track positive grace period induced renewals
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
|
|
|
METHOD_RENEWAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
void
|
2018-02-20 19:41:50 +03:00
|
|
|
nsHostResolver::DeQueue(LinkedList<RefPtr<nsHostRecord>>& aQ, nsHostRecord **aResult)
|
2008-11-08 02:00:26 +03:00
|
|
|
{
|
2018-02-20 19:41:50 +03:00
|
|
|
RefPtr<nsHostRecord> rec = aQ.popFirst();
|
2008-11-08 02:00:26 +03:00
|
|
|
mPendingCount--;
|
2018-02-20 19:41:50 +03:00
|
|
|
rec.forget(aResult);
|
2011-10-17 18:59:28 +04:00
|
|
|
(*aResult)->onQueue = false;
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-01-14 20:18:27 +03:00
|
|
|
nsHostResolver::GetHostToLookup(nsHostRecord **result)
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool timedOut = false;
|
2009-01-14 20:18:27 +03:00
|
|
|
PRIntervalTime epoch, now, timeout;
|
2015-08-20 19:14:40 +03: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);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2009-01-14 20:18:27 +03:00
|
|
|
timeout = (mNumIdleThreads >= HighThreadThreshold) ? mShortIdleTimeout : mLongIdleTimeout;
|
|
|
|
epoch = PR_IntervalNow();
|
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
while (!mShutdown) {
|
|
|
|
// remove next record from Q; hand over owning reference. Check high, then med, then low
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
#define SET_GET_TTL(var, val) (var)->mGetTtl = sGetTtlEnabled && (val)
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
if (!mHighQ.isEmpty()) {
|
2008-11-08 02:00:26 +03:00
|
|
|
DeQueue (mHighQ, result);
|
2014-09-21 02:10:18 +04:00
|
|
|
SET_GET_TTL(*result, false);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
|
2009-01-14 20:18:27 +03:00
|
|
|
if (mActiveAnyThreadCount < HighThreadThreshold) {
|
2018-02-20 19:41:50 +03:00
|
|
|
if (!mMediumQ.isEmpty()) {
|
2008-11-08 02:00:26 +03:00
|
|
|
DeQueue (mMediumQ, result);
|
2009-01-14 20:18:27 +03:00
|
|
|
mActiveAnyThreadCount++;
|
2011-10-17 18:59:28 +04:00
|
|
|
(*result)->usingAnyThread = true;
|
2014-09-21 02:10:18 +04:00
|
|
|
SET_GET_TTL(*result, true);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-02-20 19:41:50 +03:00
|
|
|
if (!mLowQ.isEmpty()) {
|
2008-11-08 02:00:26 +03:00
|
|
|
DeQueue (mLowQ, result);
|
2009-01-14 20:18:27 +03:00
|
|
|
mActiveAnyThreadCount++;
|
2011-10-17 18:59:28 +04:00
|
|
|
(*result)->usingAnyThread = true;
|
2014-09-21 02:10:18 +04:00
|
|
|
SET_GET_TTL(*result, true);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-11-08 02:00:26 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2009-01-14 20:18:27 +03:00
|
|
|
// Determining timeout is racy, so allow one cycle through checking the queues
|
|
|
|
// before exiting.
|
|
|
|
if (timedOut)
|
|
|
|
break;
|
|
|
|
|
2008-11-08 02:00:26 +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
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-11-08 02:00:26 +03:00
|
|
|
mNumIdleThreads++;
|
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
|
|
|
mIdleThreadCV.Wait(timeout);
|
2008-11-08 02:00:26 +03:00
|
|
|
mNumIdleThreads--;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2009-01-14 20:18:27 +03:00
|
|
|
now = PR_IntervalNow();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2009-01-14 20:18:27 +03:00
|
|
|
if ((PRIntervalTime)(now - epoch) >= timeout)
|
2011-10-17 18:59:28 +04:00
|
|
|
timedOut = true;
|
2009-01-14 20:18:27 +03:00
|
|
|
else {
|
|
|
|
// It is possible that PR_WaitCondVar() 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 -= (PRIntervalTime)(now - epoch);
|
|
|
|
epoch = now;
|
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
// tell thread to exit...
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-21 02:10:18 +04:00
|
|
|
nsHostResolver::PrepareRecordExpiration(nsHostRecord* 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);
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Caching host [%s%s%s] negative record for %u seconds.\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get()),
|
2014-09-21 02:10:18 +04:00
|
|
|
NEGATIVE_RECORD_LIFETIME));
|
|
|
|
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;
|
|
|
|
if (sGetTtlEnabled) {
|
2014-09-21 02:10:18 +04:00
|
|
|
if (rec->addr_info && rec->addr_info->ttl != AddrInfo::NO_TTL_DATA) {
|
|
|
|
ttl = rec->addr_info->ttl;
|
|
|
|
}
|
2014-10-20 21:26:33 +04:00
|
|
|
lifetime = ttl;
|
|
|
|
grace = 0;
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rec->SetExpiration(TimeStamp::NowLoRes(), lifetime, grace);
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("Caching host [%s%s%s] record for %u seconds (grace %d).",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get()), lifetime, grace));
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
static nsresult
|
|
|
|
merge_rrset(AddrInfo *rrto, AddrInfo *rrfrom)
|
|
|
|
{
|
|
|
|
if (!rrto || !rrfrom) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
NetAddrElement *element;
|
|
|
|
while ((element = rrfrom->mAddresses.getFirst())) {
|
|
|
|
element->remove(); // unlist from old
|
|
|
|
rrto->AddAddress(element); // enlist on new
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-08 04:04:36 +03:00
|
|
|
static bool
|
|
|
|
different_rrset(AddrInfo *rrset1, AddrInfo *rrset2)
|
|
|
|
{
|
|
|
|
if (!rrset1 || !rrset2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-04 18:02:58 +03:00
|
|
|
LOG(("different_rrset %s\n", rrset1->mHostName));
|
2016-07-08 04:04:36 +03:00
|
|
|
nsTArray<NetAddr> orderedSet1;
|
|
|
|
nsTArray<NetAddr> orderedSet2;
|
|
|
|
|
|
|
|
for (NetAddrElement *element = rrset1->mAddresses.getFirst();
|
|
|
|
element; element = element->getNext()) {
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
char buf[128];
|
|
|
|
NetAddrToString(&element->mAddress, buf, 128);
|
|
|
|
LOG(("different_rrset add to set 1 %s\n", buf));
|
|
|
|
}
|
|
|
|
orderedSet1.InsertElementAt(orderedSet1.Length(), element->mAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (NetAddrElement *element = rrset2->mAddresses.getFirst();
|
|
|
|
element; element = element->getNext()) {
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
char buf[128];
|
|
|
|
NetAddrToString(&element->mAddress, buf, 128);
|
|
|
|
LOG(("different_rrset add to set 2 %s\n", buf));
|
|
|
|
}
|
|
|
|
orderedSet2.InsertElementAt(orderedSet2.Length(), element->mAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (orderedSet1.Length() != orderedSet2.Length()) {
|
|
|
|
LOG(("different_rrset true due to length change\n"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
orderedSet1.Sort();
|
|
|
|
orderedSet2.Sort();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < orderedSet1.Length(); ++i) {
|
|
|
|
if (!(orderedSet1[i] == orderedSet2[i])) {
|
|
|
|
LOG(("different_rrset true due to content change\n"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(("different_rrset false\n"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.
|
2016-07-08 04:04:36 +03:00
|
|
|
// takes ownership of AddrInfo parameter
|
2014-09-25 07:14:00 +04:00
|
|
|
nsHostResolver::LookupStatus
|
2018-02-01 12:20:49 +03:00
|
|
|
nsHostResolver::CompleteLookup(nsHostRecord* rec, nsresult status, AddrInfo* aNewRRSet, bool pb)
|
2003-09-12 00:32:33 +04:00
|
|
|
{
|
2018-02-01 12:20:49 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MOZ_ASSERT(rec);
|
|
|
|
MOZ_ASSERT(rec->pb == pb);
|
|
|
|
|
|
|
|
// newRRSet needs to be taken into the hostrecord (which will then own it)
|
|
|
|
// or deleted on early return.
|
|
|
|
nsAutoPtr<AddrInfo> newRRSet(aNewRRSet);
|
|
|
|
|
2018-02-20 01:33:40 +03:00
|
|
|
bool trrResult = newRRSet && newRRSet->IsTRR();
|
|
|
|
|
|
|
|
if (rec->mResolveAgain && (status != NS_ERROR_ABORT) && !trrResult) {
|
|
|
|
LOG(("nsHostResolver record %p resolve again due to flushcache\n", rec));
|
|
|
|
rec->mResolveAgain = false;
|
|
|
|
return LOOKUP_RESOLVEAGAIN;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(rec->mResolving);
|
|
|
|
rec->mResolving--;
|
|
|
|
LOG(("nsHostResolver::CompleteLookup %s %p %X trr=%d stillResolving=%d\n",
|
|
|
|
rec->host.get(), aNewRRSet, (unsigned int)status,
|
|
|
|
aNewRRSet ? aNewRRSet->IsTRR() : 0, rec->mResolving));
|
|
|
|
|
|
|
|
if (trrResult) {
|
2018-02-22 05:22:00 +03:00
|
|
|
MutexAutoLock trrlock(rec->mTrrLock);
|
2018-02-01 12:20:49 +03:00
|
|
|
LOG(("TRR lookup Complete (%d) %s %s\n",
|
|
|
|
newRRSet->IsTRR(), newRRSet->mHostName,
|
|
|
|
NS_SUCCEEDED(status) ? "OK" : "FAILED"));
|
|
|
|
MOZ_ASSERT(TRROutstanding());
|
|
|
|
if (newRRSet->IsTRR() == TRRTYPE_A) {
|
|
|
|
MOZ_ASSERT(rec->mTrrA);
|
|
|
|
rec->mTrrA = nullptr;
|
|
|
|
rec->mTrrAUsed = NS_SUCCEEDED(status) ? nsHostRecord::OK : nsHostRecord::FAILED;
|
|
|
|
} else if (newRRSet->IsTRR() == TRRTYPE_AAAA) {
|
|
|
|
MOZ_ASSERT(rec->mTrrAAAA);
|
|
|
|
rec->mTrrAAAA = nullptr;
|
|
|
|
rec->mTrrAAAAUsed = NS_SUCCEEDED(status) ? nsHostRecord::OK : nsHostRecord::FAILED;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(0);
|
|
|
|
}
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (NS_SUCCEEDED(status)) {
|
|
|
|
if (rec->mTRRSuccess == 0) { // first one
|
|
|
|
rec->mTrrDuration = TimeStamp::Now() - rec->mTrrStart;
|
|
|
|
}
|
|
|
|
rec->mTRRSuccess++;
|
2014-09-25 07:14:00 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (TRROutstanding()) {
|
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
return LOOKUP_OK; // wait for outstanding
|
|
|
|
}
|
2003-10-07 09:11:41 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
// There's another TRR complete pending. Wait for it and keep
|
|
|
|
// this RRset around until then.
|
|
|
|
MOZ_ASSERT(!rec->mFirstTRR && newRRSet);
|
|
|
|
rec->mFirstTRR = newRRSet; // autoPtr.swap()
|
|
|
|
MOZ_ASSERT(rec->mFirstTRR && !newRRSet);
|
|
|
|
|
|
|
|
if (rec->mDidCallbacks || rec->mResolverMode == MODE_SHADOW) {
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can do some callbacks with this partial result which requires
|
|
|
|
// a deep copy
|
|
|
|
newRRSet = new AddrInfo(rec->mFirstTRR);
|
|
|
|
MOZ_ASSERT(rec->mFirstTRR && newRRSet);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// no more outstanding TRRs
|
|
|
|
// If mFirstTRR is set, merge those addresses into current set!
|
|
|
|
if (rec->mFirstTRR) {
|
|
|
|
if (NS_SUCCEEDED(status)) {
|
|
|
|
merge_rrset(newRRSet, rec->mFirstTRR);
|
2016-07-08 04:04:36 +03:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
else {
|
|
|
|
newRRSet = rec->mFirstTRR; // transfers
|
|
|
|
}
|
|
|
|
rec->mFirstTRR = nullptr;
|
2016-07-08 04:04:36 +03:00
|
|
|
}
|
2012-12-24 01:08:43 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (!rec->mTRRSuccess) {
|
|
|
|
// no TRR success
|
|
|
|
newRRSet = nullptr;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (!rec->mTRRSuccess && rec->mResolverMode == MODE_TRRFIRST) {
|
|
|
|
MOZ_ASSERT(!rec->mResolving);
|
|
|
|
NativeLookup(rec);
|
|
|
|
MOZ_ASSERT(rec->mResolving);
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
} else { // native resolve completed
|
2014-08-21 18:53:11 +04:00
|
|
|
if (rec->usingAnyThread) {
|
|
|
|
mActiveAnyThreadCount--;
|
|
|
|
rec->usingAnyThread = false;
|
|
|
|
}
|
2009-01-14 20:18:27 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
rec->mNative = false;
|
|
|
|
rec->mNativeSuccess = newRRSet ? true : false;
|
|
|
|
if (rec->mNativeSuccess) {
|
|
|
|
rec->mNativeDuration = TimeStamp::Now() - rec->mNativeStart;
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 18:53:11 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
// update record fields. We might have a rec->addr_info already if a
|
|
|
|
// previous lookup result expired and we're reresolving it or we get
|
|
|
|
// a late second TRR response.
|
|
|
|
// note that we don't update the addr_info if this is trr shadow results
|
|
|
|
if (!mShutdown &&
|
|
|
|
!(trrResult && rec->mResolverMode == MODE_SHADOW)) {
|
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
|
|
|
nsAutoPtr<AddrInfo> old_addr_info;
|
|
|
|
if (different_rrset(rec->addr_info, newRRSet)) {
|
|
|
|
LOG(("nsHostResolver record %p new gencnt\n", rec));
|
|
|
|
old_addr_info = rec->addr_info;
|
|
|
|
rec->addr_info = newRRSet.forget();
|
|
|
|
rec->addr_info_gencnt++;
|
|
|
|
} else {
|
|
|
|
if (rec->addr_info && newRRSet) {
|
|
|
|
rec->addr_info->ttl = newRRSet->ttl;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
old_addr_info = newRRSet.forget();
|
|
|
|
}
|
|
|
|
rec->negative = !rec->addr_info;
|
|
|
|
PrepareRecordExpiration(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool doCallbacks = true;
|
|
|
|
|
|
|
|
if (trrResult && (rec->mResolverMode == MODE_SHADOW) && !rec->mDidCallbacks) {
|
|
|
|
// don't report result based only on suppressed TRR info
|
|
|
|
doCallbacks = false;
|
|
|
|
LOG(("nsHostResolver Suppressing TRR %s because it is first shadow result\n",
|
|
|
|
rec->host.get()));
|
|
|
|
} else if(trrResult && rec->mDidCallbacks) {
|
|
|
|
// already callback'ed on the first TRR response
|
|
|
|
LOG(("nsHostResolver Suppressing callback for second TRR response for %s\n",
|
|
|
|
rec->host.get()));
|
|
|
|
doCallbacks = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
|
|
|
NetAddrElement *element;
|
|
|
|
if (rec->addr_info) {
|
|
|
|
for (element = rec->addr_info->mAddresses.getFirst();
|
|
|
|
element; element = element->getNext()) {
|
|
|
|
char buf[128];
|
|
|
|
NetAddrToString(&element->mAddress, buf, sizeof(buf));
|
|
|
|
LOG(("CompleteLookup: %s has %s\n", rec->host.get(), buf));
|
2014-10-14 01:07:00 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
} else {
|
|
|
|
LOG(("CompleteLookup: %s has NO address\n", rec->host.get()));
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (doCallbacks) {
|
|
|
|
// get the list of pending callbacks for this lookup, and notify
|
|
|
|
// them that the lookup is complete.
|
|
|
|
mozilla::LinkedList<RefPtr<nsResolveHostCallback>> cbs = mozilla::Move(rec->mCallbacks);
|
|
|
|
|
|
|
|
LOG(("nsHostResolver record %p calling back dns users\n", rec));
|
|
|
|
|
|
|
|
for (nsResolveHostCallback* c = cbs.getFirst(); c; c = c->removeAndGetNext()) {
|
|
|
|
c->OnResolveHostComplete(this, rec, status);
|
|
|
|
}
|
|
|
|
rec->mDidCallbacks = true;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (!rec->mResolving && !mShutdown) {
|
|
|
|
rec->ResolveComplete();
|
|
|
|
|
|
|
|
// add to mEvictionQ
|
2018-02-20 19:41:50 +03:00
|
|
|
MOZ_ASSERT(!rec->isInList());
|
|
|
|
mEvictionQ.insertBack(rec);
|
2018-02-01 12:20:49 +03:00
|
|
|
if (mEvictionQSize < mMaxCacheEntries) {
|
|
|
|
mEvictionQSize++;
|
|
|
|
} else {
|
|
|
|
// remove first element on mEvictionQ
|
2018-02-20 19:41:50 +03:00
|
|
|
RefPtr<nsHostRecord> head = mEvictionQ.popFirst();
|
|
|
|
mRecordDB.Remove(*static_cast<nsHostKey *>(head.get()));
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
if (!head->negative) {
|
|
|
|
// record the age of the entry upon eviction.
|
|
|
|
TimeDuration age = TimeStamp::NowLoRes() - head->mValidStart;
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_CLEANUP_AGE,
|
|
|
|
static_cast<uint32_t>(age.ToSeconds() / 60));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
|
|
|
// Unless the result is from TRR, resolve again to get TTL
|
|
|
|
bool fromTRR = false;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
|
|
|
if(rec->addr_info && rec->addr_info->IsTRR()) {
|
|
|
|
fromTRR = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fromTRR &&
|
|
|
|
!mShutdown && !rec->mGetTtl && !rec->mResolving && sGetTtlEnabled) {
|
|
|
|
LOG(("Issuing second async lookup for TTL for host [%s%s%s].",
|
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get())));
|
|
|
|
rec->flags =
|
|
|
|
(rec->flags & ~RES_PRIORITY_MEDIUM) | RES_PRIORITY_LOW |
|
|
|
|
RES_DISABLE_TRR;
|
|
|
|
DebugOnly<nsresult> rv = NameLookup(rec);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rv),
|
|
|
|
"Could not issue second async lookup for TTL.");
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-25 07:14:00 +04:00
|
|
|
return LOOKUP_OK;
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2012-01-21 03:14:46 +04:00
|
|
|
void
|
2017-02-14 07:25:35 +03:00
|
|
|
nsHostResolver::CancelAsyncRequest(const char *host,
|
|
|
|
const OriginAttributes &aOriginAttributes,
|
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af,
|
|
|
|
const char *netInterface,
|
|
|
|
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
|
|
|
|
|
|
|
nsHostKey key(nsCString(host), flags, af,
|
|
|
|
(aOriginAttributes.mPrivateBrowsingId > 0),
|
|
|
|
nsCString(netInterface),
|
2017-12-23 01:08:17 +03:00
|
|
|
originSuffix);
|
2018-01-08 01:22:01 +03:00
|
|
|
RefPtr<nsHostRecord> rec = mRecordDB.Get(key);
|
|
|
|
if (rec) {
|
2013-09-19 23:28:26 +04:00
|
|
|
nsHostRecord* recPtr = nullptr;
|
2017-12-20 03:13:46 +03:00
|
|
|
|
2018-01-08 01:22:01 +03:00
|
|
|
for (RefPtr<nsResolveHostCallback> c : rec->mCallbacks) {
|
2017-12-20 03:13:46 +03:00
|
|
|
if (c->EqualsAsyncListener(aListener)) {
|
|
|
|
c->remove();
|
2018-01-08 01:22:01 +03:00
|
|
|
recPtr = rec;
|
2017-12-20 03:13:46 +03:00
|
|
|
c->OnResolveHostComplete(this, recPtr, status);
|
2012-01-21 03:14:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no more callbacks, remove the hash table entry
|
2017-12-20 03:13:46 +03:00
|
|
|
if (recPtr && recPtr->mCallbacks.isEmpty()) {
|
2018-01-08 01:22:01 +03:00
|
|
|
mRecordDB.Remove(*static_cast<nsHostKey *>(recPtr));
|
2012-01-21 03:14:46 +04:00
|
|
|
// If record is on a Queue, remove it and then deref it
|
2018-02-20 19:41:50 +03:00
|
|
|
if (recPtr->isInList()) {
|
|
|
|
recPtr->remove();
|
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);
|
|
|
|
for (auto iter = mRecordDB.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = iter.UserData();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
void
|
2003-09-12 00:32:33 +04:00
|
|
|
nsHostResolver::ThreadFunc(void *arg)
|
|
|
|
{
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG(("DNS lookup thread - starting execution.\n"));
|
2012-06-12 21:06:20 +04:00
|
|
|
|
|
|
|
static nsThreadPoolNaming naming;
|
2016-12-20 16:21:34 +03:00
|
|
|
nsCString name = naming.GetNextThreadName("DNS Resolver");
|
|
|
|
|
2017-10-04 01:11:18 +03:00
|
|
|
AUTO_PROFILER_REGISTER_THREAD(name.BeginReading());
|
2017-02-07 13:57:23 +03:00
|
|
|
NS_SetCurrentThreadName(name.BeginReading());
|
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
|
2017-12-08 00:53:42 +03:00
|
|
|
RefPtr<nsHostResolver> resolver = dont_AddRef((nsHostResolver *)arg);
|
2018-01-08 01:00:07 +03:00
|
|
|
RefPtr<nsHostRecord> rec;
|
2017-12-04 18:02:59 +03:00
|
|
|
AddrInfo *ai = nullptr;
|
2014-09-25 07:14:00 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
do {
|
|
|
|
if (!rec) {
|
|
|
|
RefPtr<nsHostRecord> tmpRec;
|
|
|
|
if (!resolver->GetHostToLookup(getter_AddRefs(tmpRec))) {
|
|
|
|
break; // thread shutdown signal
|
|
|
|
}
|
|
|
|
// GetHostToLookup() returns an owning reference
|
|
|
|
MOZ_ASSERT(tmpRec);
|
|
|
|
rec.swap(tmpRec);
|
|
|
|
}
|
|
|
|
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("DNS lookup thread - Calling getaddrinfo for host [%s%s%s].\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get())));
|
2004-09-01 23:55:58 +04:00
|
|
|
|
2011-12-17 06:24:17 +04:00
|
|
|
TimeStamp startTime = TimeStamp::Now();
|
2014-09-21 02:10:18 +04:00
|
|
|
bool getTtl = rec->mGetTtl;
|
2017-12-23 01:08:17 +03:00
|
|
|
nsresult status = GetAddrInfo(rec->host.get(), rec->af,
|
|
|
|
rec->flags,
|
|
|
|
rec->netInterface.get(), &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()) {
|
2017-12-23 01:08:17 +03:00
|
|
|
status = GetAddrInfo(rec->host.get(), rec->af,
|
|
|
|
rec->flags, rec->netInterface.get(), &ai,
|
2015-02-19 08:14:00 +03:00
|
|
|
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(resolver->mLock);
|
|
|
|
|
|
|
|
if (!resolver->mShutdown) {
|
|
|
|
TimeDuration elapsed = TimeStamp::Now() - startTime;
|
|
|
|
uint32_t millis = static_cast<uint32_t>(elapsed.ToMilliseconds());
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(status)) {
|
2017-02-15 22:15:15 +03:00
|
|
|
Telemetry::HistogramID histogramID;
|
2015-08-20 19:14:40 +03:00
|
|
|
if (!rec->addr_info_gencnt) {
|
|
|
|
// Time for initial lookup.
|
|
|
|
histogramID = Telemetry::DNS_LOOKUP_TIME;
|
|
|
|
} else if (!getTtl) {
|
|
|
|
// Time for renewal; categorized by expiration strategy.
|
|
|
|
histogramID = Telemetry::DNS_RENEWAL_TIME;
|
|
|
|
} else {
|
|
|
|
// Time to get TTL; categorized by expiration strategy.
|
|
|
|
histogramID = Telemetry::DNS_RENEWAL_TIME_FOR_TTL;
|
|
|
|
}
|
|
|
|
Telemetry::Accumulate(histogramID, millis);
|
|
|
|
} else {
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_FAILED_LOOKUP_TIME, millis);
|
|
|
|
}
|
2014-10-08 01:57:48 +04:00
|
|
|
}
|
2011-12-17 06:24:17 +04:00
|
|
|
}
|
2012-10-26 16:12:53 +04:00
|
|
|
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("DNS lookup thread - lookup completed for host [%s%s%s]: %s.\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get()),
|
2015-02-19 08:14:00 +03:00
|
|
|
ai ? "success" : "failure: unknown host"));
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (LOOKUP_RESOLVEAGAIN == resolver->CompleteLookup(rec, status, ai, rec->pb)) {
|
2014-09-25 07:14:00 +04:00
|
|
|
// leave 'rec' assigned and loop to make a renewed host resolve
|
2015-02-19 08:14:00 +03:00
|
|
|
LOG(("DNS lookup thread - Re-resolving host [%s%s%s].\n",
|
2017-12-23 01:08:17 +03:00
|
|
|
LOG_HOST(rec->host.get(), rec->netInterface.get())));
|
2014-09-25 07:14:00 +04:00
|
|
|
} else {
|
|
|
|
rec = nullptr;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
} while(true);
|
|
|
|
|
2015-08-20 19:14:40 +03:00
|
|
|
resolver->mThreadCount--;
|
2017-12-08 00:53:42 +03:00
|
|
|
resolver = nullptr;
|
2013-11-22 01:35:44 +04:00
|
|
|
LOG(("DNS lookup thread - queue empty, thread finished.\n"));
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-09-21 02:10:18 +04:00
|
|
|
nsHostResolver::Create(uint32_t maxCacheEntries,
|
|
|
|
uint32_t defaultCacheEntryLifetime,
|
|
|
|
uint32_t defaultGracePeriod,
|
2003-09-12 00:32:33 +04:00
|
|
|
nsHostResolver **result)
|
|
|
|
{
|
2016-11-01 15:44:09 +03:00
|
|
|
auto *res = new nsHostResolver(maxCacheEntries, defaultCacheEntryLifetime,
|
2018-02-01 12:20:49 +03:00
|
|
|
defaultGracePeriod);
|
2015-11-28 02:20:01 +03:00
|
|
|
NS_ADDREF(res);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
|
|
|
nsresult rv = res->Init();
|
2015-11-28 02:20:01 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_RELEASE(res);
|
2003-09-12 00:32:33 +04:00
|
|
|
|
2015-11-28 02:20:01 +03:00
|
|
|
*result = res;
|
2003-09-12 00:32:33 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
void
|
|
|
|
nsHostResolver::GetDNSCacheEntries(nsTArray<DNSCacheEntries> *args)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2018-01-08 01:22:01 +03:00
|
|
|
for (auto iter = mRecordDB.Iter(); !iter.Done(); iter.Next()) {
|
2015-06-11 04:28:03 +03:00
|
|
|
// We don't pay attention to address literals, only resolved domains.
|
|
|
|
// Also require a host.
|
2018-01-08 01:22:01 +03:00
|
|
|
nsHostRecord* rec = iter.UserData();
|
2015-06-11 04:28:03 +03:00
|
|
|
MOZ_ASSERT(rec, "rec should never be null here!");
|
2017-12-23 01:08:17 +03:00
|
|
|
if (!rec || !rec->addr_info) {
|
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
|
|
|
DNSCacheEntries info;
|
|
|
|
info.hostname = rec->host;
|
|
|
|
info.family = rec->af;
|
|
|
|
info.netInterface = rec->netInterface;
|
|
|
|
info.expiration =
|
|
|
|
(int64_t)(rec->mValidEnd - TimeStamp::NowLoRes()).ToSeconds();
|
|
|
|
if (info.expiration <= 0) {
|
|
|
|
// We only need valid DNS cache entries
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
NetAddr *addr = nullptr;
|
|
|
|
NetAddrElement *addrElement = rec->addr_info->mAddresses.getFirst();
|
2012-12-24 01:08:43 +04:00
|
|
|
if (addrElement) {
|
|
|
|
addr = &addrElement->mAddress;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
2015-06-11 04:28:03 +03:00
|
|
|
while (addr) {
|
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(addr, buf, sizeof(buf))) {
|
|
|
|
info.hostaddr.AppendElement(buf);
|
|
|
|
}
|
|
|
|
addr = nullptr;
|
|
|
|
addrElement = addrElement->getNext();
|
|
|
|
if (addrElement) {
|
|
|
|
addr = &addrElement->mAddress;
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 11:57:22 +03:00
|
|
|
info.TRR = rec->addr_info->IsTRR();
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2015-06-11 04:28:03 +03:00
|
|
|
args->AppendElement(info);
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
#undef LOG_ENABLED
|