2007-03-22 10:30:00 -07:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if defined(MOZ_LOGGING)
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_RES_NINIT)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#define RES_RETRY_ON_FAILURE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsHostResolver.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsISupportsBase.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "pratom.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "prlong.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsURLHelper.h"
|
2012-06-12 10:06:20 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2011-12-16 18:24:17 -08:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2010-05-19 16:22:19 -07: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-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
2012-12-10 06:13:55 -08: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-03-31 21:29:02 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
// Use a persistent thread pool in order to avoid spinning up new threads all the time.
|
|
|
|
// In particular, thread creation results in a res_init() call from libc which is
|
|
|
|
// quite expensive.
|
|
|
|
//
|
|
|
|
// The pool dynamically grows between 0 and MAX_RESOLVER_THREADS in size. New requests
|
|
|
|
// go first to an idle thread. If that cannot be found and there are fewer than MAX_RESOLVER_THREADS
|
|
|
|
// currently in the pool a new thread is created for high priority requests. If
|
|
|
|
// the new request is at a lower priority a new thread will only be created if
|
|
|
|
// there are fewer than HighThreadThreshold currently outstanding. If a thread cannot be
|
|
|
|
// created or an idle thread located for the request it is queued.
|
|
|
|
//
|
|
|
|
// When the pool is greater than HighThreadThreshold in size a thread will be destroyed after
|
|
|
|
// ShortIdleTimeoutSeconds of idle time. Smaller pools use LongIdleTimeoutSeconds for a
|
|
|
|
// timeout period.
|
|
|
|
|
|
|
|
#define HighThreadThreshold MAX_RESOLVER_THREADS_FOR_ANY_PRIORITY
|
|
|
|
#define LongIdleTimeoutSeconds 300 // for threads 1 -> HighThreadThreshold
|
|
|
|
#define ShortIdleTimeoutSeconds 60 // for threads HighThreadThreshold+1 -> MAX_RESOLVER_THREADS
|
|
|
|
|
|
|
|
PR_STATIC_ASSERT (HighThreadThreshold <= MAX_RESOLVER_THREADS);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
2012-07-30 07:20:58 -07:00
|
|
|
static PRLogModuleInfo *gHostResolverLog = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
#define LOG(args) PR_LOG(gHostResolverLog, PR_LOG_DEBUG, args)
|
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
MoveCList(PRCList &from, PRCList &to)
|
|
|
|
{
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&from)) {
|
|
|
|
to.next = from.next;
|
|
|
|
to.prev = from.prev;
|
|
|
|
to.next->prev = &to;
|
|
|
|
to.prev->next = &to;
|
|
|
|
PR_INIT_CLIST(&from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
NowInMinutes()
|
|
|
|
{
|
2012-10-12 10:29:11 -07:00
|
|
|
return uint32_t(PR_Now() / int64_t(60 * PR_USEC_PER_SEC));
|
2007-03-22 10:30:00 -07: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-28 23:19:26 -07:00
|
|
|
bool Reset()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// reset no more than once per second
|
|
|
|
if (PR_IntervalToSeconds(PR_IntervalNow() - mLastReset) < 1)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Calling 'res_ninit'.\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mLastReset = PR_IntervalNow();
|
|
|
|
return (res_ninit(&_res) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRIntervalTime mLastReset;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RES_RETRY_ON_FAILURE
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2011-07-21 06:18:01 -07:00
|
|
|
nsHostRecord::nsHostRecord(const nsHostKey *key)
|
2012-07-10 07:14:01 -07:00
|
|
|
: addr_info_lock("nsHostRecord.addr_info_lock")
|
2011-07-21 06:18:01 -07:00
|
|
|
, addr_info_gencnt(0)
|
2012-07-30 07:20:58 -07:00
|
|
|
, addr_info(nullptr)
|
|
|
|
, addr(nullptr)
|
2011-10-17 07:59:28 -07:00
|
|
|
, negative(false)
|
|
|
|
, resolving(false)
|
|
|
|
, onQueue(false)
|
|
|
|
, usingAnyThread(false)
|
2011-07-21 06:18:01 -07:00
|
|
|
{
|
|
|
|
host = ((char *) this) + sizeof(nsHostRecord);
|
|
|
|
memcpy((char *) host, key->host, strlen(key->host) + 1);
|
|
|
|
flags = key->flags;
|
|
|
|
af = key->af;
|
|
|
|
|
|
|
|
expiration = NowInMinutes();
|
|
|
|
|
|
|
|
PR_INIT_CLIST(this);
|
|
|
|
PR_INIT_CLIST(&callbacks);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsHostRecord::Create(const nsHostKey *key, nsHostRecord **result)
|
|
|
|
{
|
|
|
|
size_t hostLen = strlen(key->host) + 1;
|
|
|
|
size_t size = hostLen + sizeof(nsHostRecord);
|
|
|
|
|
2011-07-21 06:18:01 -07:00
|
|
|
// Use placement new to create the object with room for the hostname
|
|
|
|
// allocated after it.
|
|
|
|
void *place = ::operator new(size);
|
|
|
|
*result = new(place) nsHostRecord(key);
|
2012-07-10 07:14:01 -07:00
|
|
|
NS_ADDREF(*result);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHostRecord::~nsHostRecord()
|
|
|
|
{
|
|
|
|
if (addr)
|
|
|
|
free(addr);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-12-20 11:47:07 -08:00
|
|
|
nsHostRecord::Blacklisted(NetAddr *aQuery)
|
2011-07-21 06:18:01 -07:00
|
|
|
{
|
|
|
|
// must call locked
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Checking blacklist for host [%s], host record [%p].\n", host, this));
|
2011-07-21 06:18:01 -07:00
|
|
|
|
|
|
|
// skip the string conversion for the common case of no blacklist
|
2012-07-16 15:37:28 -07:00
|
|
|
if (!mBlacklistedItems.Length()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2012-07-16 15:37:28 -07:00
|
|
|
}
|
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (!NetAddrToString(aQuery, buf, sizeof(buf))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2012-07-16 15:37:28 -07:00
|
|
|
}
|
2011-07-21 06:18:01 -07:00
|
|
|
nsDependentCString strQuery(buf);
|
2012-07-16 15:37:28 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mBlacklistedItems.Length(); i++) {
|
2011-07-21 06:18:01 -07:00
|
|
|
if (mBlacklistedItems.ElementAt(i).Equals(strQuery)) {
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Address [%s] is blacklisted for host [%s].\n", buf, host));
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-07-21 06:18:01 -07:00
|
|
|
}
|
2012-07-16 15:37:28 -07:00
|
|
|
}
|
2011-07-21 06:18:01 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-07-21 06:18:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-20 11:47:07 -08:00
|
|
|
nsHostRecord::ReportUnusable(NetAddr *aAddress)
|
2011-07-21 06:18:01 -07:00
|
|
|
{
|
|
|
|
// must call locked
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Adding address to blacklist for host [%s], host record [%p].\n", host, this));
|
2011-07-21 06:18:01 -07:00
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(aAddress, buf, sizeof(buf))) {
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Successfully adding address [%s] to blacklist for host [%s].\n", buf, host));
|
2011-07-21 06:18:01 -07:00
|
|
|
mBlacklistedItems.AppendElement(nsCString(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostRecord::ResetBlacklist()
|
|
|
|
{
|
|
|
|
// must call locked
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Resetting blacklist for host [%s], host record [%p].\n", host, this));
|
2011-07-21 06:18:01 -07:00
|
|
|
mBlacklistedItems.Clear();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct nsHostDBEnt : PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
nsHostRecord *rec;
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
HostDB_HashKey(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
const nsHostKey *hk = static_cast<const nsHostKey *>(key);
|
2012-03-12 15:53:18 -07:00
|
|
|
return AddToHash(HashString(hk->host), RES_KEY_FLAGS(hk->flags), hk->af);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
HostDB_MatchEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
const nsHostDBEnt *he = static_cast<const nsHostDBEnt *>(entry);
|
|
|
|
const nsHostKey *hk = static_cast<const nsHostKey *>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return !strcmp(he->rec->host, hk->host) &&
|
2008-11-04 08:05:24 -08:00
|
|
|
RES_KEY_FLAGS (he->rec->flags) == RES_KEY_FLAGS(hk->flags) &&
|
2007-03-22 10:30:00 -07:00
|
|
|
he->rec->af == hk->af;
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
HostDB_MoveEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsHostDBEnt *>(to)->rec =
|
|
|
|
static_cast<const nsHostDBEnt *>(from)->rec;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
HostDB_ClearEntry(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *entry)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>(entry);
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Clearing cache db entry for host [%s].\n", he->rec->host));
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(DEBUG) && defined(PR_LOGGING)
|
2012-12-20 11:47:07 -08:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(he->rec->addr_info_lock);
|
|
|
|
if (!he->rec->addr_info) {
|
|
|
|
LOG(("No address info for host [%s].\n", he->rec->host));
|
|
|
|
} else {
|
|
|
|
int32_t now = (int32_t) NowInMinutes();
|
|
|
|
int32_t diff = (int32_t) he->rec->expiration - now;
|
|
|
|
LOG(("Record for [%s] expires in %d minute(s).\n", he->rec->host, diff));
|
|
|
|
NetAddrElement *addrElement;
|
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
while ((addrElement = he->rec->addr_info->mAddresses.popLast())) {
|
|
|
|
NetAddrToString(&addrElement->mAddress, buf, sizeof(buf));
|
|
|
|
LOG((" [%s]\n", buf));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
NS_RELEASE(he->rec);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
HostDB_InitEntry(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>(entry);
|
|
|
|
nsHostRecord::Create(static_cast<const nsHostKey *>(key), &he->rec);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashTableOps gHostDB_ops =
|
|
|
|
{
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
HostDB_HashKey,
|
|
|
|
HostDB_MatchEntry,
|
|
|
|
HostDB_MoveEntry,
|
|
|
|
HostDB_ClearEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
|
|
|
HostDB_InitEntry,
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
HostDB_RemoveEntry(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number,
|
2007-03-22 10:30:00 -07:00
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHostResolver::nsHostResolver(uint32_t maxCacheEntries,
|
|
|
|
uint32_t maxCacheLifetime,
|
|
|
|
uint32_t lifetimeGracePeriod)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mMaxCacheEntries(maxCacheEntries)
|
|
|
|
, mMaxCacheLifetime(maxCacheLifetime)
|
2011-12-16 18:24:12 -08:00
|
|
|
, mGracePeriod(lifetimeGracePeriod)
|
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-03-31 21:29:02 -07:00
|
|
|
, mLock("nsHostResolver.mLock")
|
|
|
|
, mIdleThreadCV(mLock, "nsHostResolver.mIdleThreadCV")
|
2008-11-07 15:00:26 -08:00
|
|
|
, mNumIdleThreads(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mThreadCount(0)
|
2009-01-14 09:18:27 -08:00
|
|
|
, mActiveAnyThreadCount(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mEvictionQSize(0)
|
2008-11-07 15:00:26 -08:00
|
|
|
, mPendingCount(0)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mShutdown(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mCreationTime = PR_Now();
|
2008-11-07 15:00:26 -08:00
|
|
|
PR_INIT_CLIST(&mHighQ);
|
|
|
|
PR_INIT_CLIST(&mMediumQ);
|
|
|
|
PR_INIT_CLIST(&mLowQ);
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_INIT_CLIST(&mEvictionQ);
|
2008-11-07 15:00:26 -08:00
|
|
|
|
|
|
|
mLongIdleTimeout = PR_SecondsToInterval(LongIdleTimeoutSeconds);
|
|
|
|
mShortIdleTimeout = PR_SecondsToInterval(ShortIdleTimeoutSeconds);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHostResolver::~nsHostResolver()
|
|
|
|
{
|
|
|
|
PL_DHashTableFinish(&mDB);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::Init()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
PL_DHashTableInit(&mDB, &gHostDB_ops, nullptr, sizeof(nsHostDBEnt), 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mShutdown = false;
|
2007-03-22 10:30:00 -07: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-16 15:37:28 -07:00
|
|
|
LOG(("Calling 'res_ninit'.\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
res_ninit(&_res);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
void
|
|
|
|
nsHostResolver::ClearPendingQueue(PRCList *aPendingQ)
|
|
|
|
{
|
|
|
|
// loop through pending queue, erroring out pending lookups.
|
|
|
|
if (!PR_CLIST_IS_EMPTY(aPendingQ)) {
|
|
|
|
PRCList *node = aPendingQ->next;
|
|
|
|
while (node != aPendingQ) {
|
|
|
|
nsHostRecord *rec = static_cast<nsHostRecord *>(node);
|
|
|
|
node = node->next;
|
2012-07-30 07:20:58 -07:00
|
|
|
OnLookupComplete(rec, NS_ERROR_ABORT, nullptr);
|
2008-11-07 15:00:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsHostResolver::Shutdown()
|
|
|
|
{
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Shutting down host resolver.\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
PRCList pendingQHigh, pendingQMed, pendingQLow, evictionQ;
|
|
|
|
PR_INIT_CLIST(&pendingQHigh);
|
|
|
|
PR_INIT_CLIST(&pendingQMed);
|
|
|
|
PR_INIT_CLIST(&pendingQLow);
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_INIT_CLIST(&evictionQ);
|
|
|
|
|
|
|
|
{
|
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-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mShutdown = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
MoveCList(mHighQ, pendingQHigh);
|
|
|
|
MoveCList(mMediumQ, pendingQMed);
|
|
|
|
MoveCList(mLowQ, pendingQLow);
|
2007-03-22 10:30:00 -07:00
|
|
|
MoveCList(mEvictionQ, evictionQ);
|
|
|
|
mEvictionQSize = 0;
|
2008-11-07 15:00:26 -08:00
|
|
|
mPendingCount = 0;
|
|
|
|
|
|
|
|
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-03-31 21:29:02 -07:00
|
|
|
mIdleThreadCV.NotifyAll();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// empty host database
|
2012-07-30 07:20:58 -07:00
|
|
|
PL_DHashTableEnumerate(&mDB, HostDB_RemoveEntry, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-11-07 15:00:26 -08:00
|
|
|
|
|
|
|
ClearPendingQueue(&pendingQHigh);
|
|
|
|
ClearPendingQueue(&pendingQMed);
|
|
|
|
ClearPendingQueue(&pendingQLow);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&evictionQ)) {
|
|
|
|
PRCList *node = evictionQ.next;
|
|
|
|
while (node != &evictionQ) {
|
2007-07-08 00:08:04 -07:00
|
|
|
nsHostRecord *rec = static_cast<nsHostRecord *>(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
node = node->next;
|
|
|
|
NS_RELEASE(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
|
|
|
|
// Logically join the outstanding worker threads with a timeout.
|
|
|
|
// Use this approach instead of PR_JoinThread() because that does
|
|
|
|
// not allow a timeout which may be necessary for a semi-responsive
|
|
|
|
// shutdown if the thread is blocked on a very slow DNS resolution.
|
|
|
|
// mThreadCount is read outside of mLock, but the worst case
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static inline bool
|
2012-08-22 08:56:38 -07:00
|
|
|
IsHighPriority(uint16_t flags)
|
2008-11-07 15:00:26 -08:00
|
|
|
{
|
|
|
|
return !(flags & (nsHostResolver::RES_PRIORITY_LOW | nsHostResolver::RES_PRIORITY_MEDIUM));
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static inline bool
|
2012-08-22 08:56:38 -07:00
|
|
|
IsMediumPriority(uint16_t flags)
|
2008-11-07 15:00:26 -08:00
|
|
|
{
|
|
|
|
return flags & nsHostResolver::RES_PRIORITY_MEDIUM;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static inline bool
|
2012-08-22 08:56:38 -07:00
|
|
|
IsLowPriority(uint16_t flags)
|
2008-11-07 15:00:26 -08:00
|
|
|
{
|
|
|
|
return flags & nsHostResolver::RES_PRIORITY_LOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::MoveQueue(nsHostRecord *aRec, PRCList &aDestQ)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aRec->onQueue, "Moving Host Record Not Currently Queued");
|
|
|
|
|
|
|
|
PR_REMOVE_LINK(aRec);
|
|
|
|
PR_APPEND_LINK(aRec, &aDestQ);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::ResolveHost(const char *host,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsResolveHostCallback *callback)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(host && *host, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Resolving host [%s].\n", host));
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
|
|
|
|
// if result is set inside the lock, then we need to issue the
|
|
|
|
// callback before returning.
|
|
|
|
nsRefPtr<nsHostRecord> result;
|
|
|
|
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-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mShutdown)
|
|
|
|
rv = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
else {
|
2012-12-20 11:47:07 -08:00
|
|
|
// Used to try to parse to an IP address literal.
|
2007-03-22 10:30:00 -07:00
|
|
|
PRNetAddr tempAddr;
|
2012-12-20 11:47:07 -08:00
|
|
|
// Unfortunately, PR_StringToNetAddr does not properly initialize
|
|
|
|
// the output buffer in the case of IPv6 input. See bug 223145.
|
2007-03-22 10:30:00 -07:00
|
|
|
memset(&tempAddr, 0, sizeof(PRNetAddr));
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
nsHostKey key = { host, flags, af };
|
2007-07-08 00:08:04 -07:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>
|
|
|
|
(PL_DHashTableOperate(&mDB, &key, PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if the record is null, then HostDB_InitEntry failed.
|
|
|
|
if (!he || !he->rec)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
// do we have a cached result that we can reuse?
|
|
|
|
else if (!(flags & RES_BYPASS_CACHE) &&
|
|
|
|
he->rec->HasResult() &&
|
2011-12-16 18:24:12 -08:00
|
|
|
NowInMinutes() <= he->rec->expiration + mGracePeriod) {
|
|
|
|
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Using cached record for host [%s].\n", host));
|
2007-03-22 10:30:00 -07:00
|
|
|
// put reference to host record on stack...
|
|
|
|
result = he->rec;
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
|
2011-12-16 18:24:12 -08:00
|
|
|
|
|
|
|
// For entries that are in the grace period, or all cached
|
|
|
|
// negative entries, use the cache but start a new lookup in
|
|
|
|
// the background
|
|
|
|
if (((NowInMinutes() > he->rec->expiration) ||
|
|
|
|
he->rec->negative) && !he->rec->resolving) {
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Using %s cache entry for host [%s] but starting async renewal.",
|
|
|
|
he->rec->negative ? "negative" :"positive", host));
|
2011-12-16 18:24:12 -08:00
|
|
|
IssueLookup(he->rec);
|
2011-12-16 18:24:17 -08:00
|
|
|
|
|
|
|
if (!he->rec->negative) {
|
|
|
|
// negative entries are constantly being refreshed, only
|
|
|
|
// track positive grace period induced renewals
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_RENEWAL);
|
|
|
|
}
|
2011-12-16 18:24:12 -08:00
|
|
|
}
|
|
|
|
|
2008-10-13 14:04:09 -07:00
|
|
|
if (he->rec->negative) {
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_NEGATIVE_HIT);
|
2008-10-13 14:04:09 -07:00
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-02-26 16:27:33 -08:00
|
|
|
// if the host name is an IP address literal and has been parsed,
|
|
|
|
// go ahead and use it.
|
|
|
|
else if (he->rec->addr) {
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_LITERAL);
|
2008-02-26 16:27:33 -08:00
|
|
|
result = he->rec;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
// try parsing the host name as an IP address literal to short
|
|
|
|
// circuit full host resolution. (this is necessary on some
|
|
|
|
// platforms like Win9x. see bug 219376 for more details.)
|
|
|
|
else if (PR_StringToNetAddr(host, &tempAddr) == PR_SUCCESS) {
|
|
|
|
// ok, just copy the result into the host record, and be done
|
|
|
|
// with it! ;-)
|
2012-12-20 11:47:07 -08:00
|
|
|
he->rec->addr = new NetAddr();
|
|
|
|
PRNetAddrToNetAddr(&tempAddr, he->rec->addr);
|
2007-03-22 10:30:00 -07:00
|
|
|
// put reference to host record on stack...
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_LITERAL);
|
2007-03-22 10:30:00 -07:00
|
|
|
result = he->rec;
|
|
|
|
}
|
2008-11-07 15:00:26 -08:00
|
|
|
else if (mPendingCount >= MAX_NON_PRIORITY_REQUESTS &&
|
|
|
|
!IsHighPriority(flags) &&
|
|
|
|
!he->rec->resolving) {
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_OVERFLOW);
|
2008-11-07 15:00:26 -08:00
|
|
|
// This is a lower priority request and we are swamped, so refuse it.
|
|
|
|
rv = NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
|
|
|
}
|
2012-09-17 16:45:10 -07:00
|
|
|
else if (flags & RES_OFFLINE) {
|
|
|
|
rv = NS_ERROR_OFFLINE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// otherwise, hit the resolver...
|
|
|
|
else {
|
2008-11-07 15:00:26 -08:00
|
|
|
// Add callback to the list of pending callbacks.
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_APPEND_LINK(callback, &he->rec->callbacks);
|
|
|
|
|
|
|
|
if (!he->rec->resolving) {
|
2008-11-04 08:05:24 -08:00
|
|
|
he->rec->flags = flags;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = IssueLookup(he->rec);
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_NETWORK_FIRST);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
PR_REMOVE_AND_INIT_LINK(callback);
|
2011-12-16 18:24:12 -08:00
|
|
|
else
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("DNS lookup for host [%s] blocking pending 'getaddrinfo' query.", host));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-11-07 15:00:26 -08:00
|
|
|
else if (he->rec->onQueue) {
|
2012-06-19 17:38:44 -07:00
|
|
|
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
|
2011-12-16 18:24:17 -08:00
|
|
|
METHOD_NETWORK_SHARED);
|
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
// Consider the case where we are on a pending queue of
|
|
|
|
// lower priority than the request is being made at.
|
|
|
|
// In that case we should upgrade to the higher queue.
|
|
|
|
|
|
|
|
if (IsHighPriority(flags) && !IsHighPriority(he->rec->flags)) {
|
|
|
|
// Move from (low|med) to high.
|
|
|
|
MoveQueue(he->rec, mHighQ);
|
|
|
|
he->rec->flags = flags;
|
|
|
|
ConditionallyCreateThread(he->rec);
|
|
|
|
} else if (IsMediumPriority(flags) && IsLowPriority(he->rec->flags)) {
|
|
|
|
// Move from low to med.
|
|
|
|
MoveQueue(he->rec, mMediumQ);
|
|
|
|
he->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-03-31 21:29:02 -07:00
|
|
|
mIdleThreadCV.Notify();
|
2008-11-07 15:00:26 -08:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result)
|
|
|
|
callback->OnLookupComplete(this, result, status);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::DetachCallback(const char *host,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsResolveHostCallback *callback,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsHostRecord> rec;
|
|
|
|
{
|
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-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsHostKey key = { host, flags, af };
|
2007-07-08 00:08:04 -07:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>
|
|
|
|
(PL_DHashTableOperate(&mDB, &key, PL_DHASH_LOOKUP));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (he && he->rec) {
|
|
|
|
// walk list looking for |callback|... we cannot assume
|
|
|
|
// that it will be there!
|
|
|
|
PRCList *node = he->rec->callbacks.next;
|
|
|
|
while (node != &he->rec->callbacks) {
|
2007-07-08 00:08:04 -07:00
|
|
|
if (static_cast<nsResolveHostCallback *>(node) == callback) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_REMOVE_LINK(callback);
|
|
|
|
rec = he->rec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// complete callback with the given status code; this would only be done if
|
|
|
|
// the record was in the process of being resolved.
|
|
|
|
if (rec)
|
|
|
|
callback->OnLookupComplete(this, rec, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-11-07 15:00:26 -08:00
|
|
|
nsHostResolver::ConditionallyCreateThread(nsHostRecord *rec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-11-07 15:00:26 -08:00
|
|
|
if (mNumIdleThreads) {
|
2007-03-22 10:30:00 -07: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-03-31 21:29:02 -07:00
|
|
|
mIdleThreadCV.Notify();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-11-07 15:00:26 -08:00
|
|
|
else if ((mThreadCount < HighThreadThreshold) ||
|
|
|
|
(IsHighPriority(rec->flags) && mThreadCount < MAX_RESOLVER_THREADS)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// dispatch new worker thread
|
|
|
|
NS_ADDREF_THIS(); // owning reference passed to thread
|
2008-11-07 15:00:26 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mThreadCount++;
|
|
|
|
PRThread *thr = PR_CreateThread(PR_SYSTEM_THREAD,
|
|
|
|
ThreadFunc,
|
2009-01-14 09:18:27 -08:00
|
|
|
this,
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_PRIORITY_NORMAL,
|
|
|
|
PR_GLOBAL_THREAD,
|
|
|
|
PR_UNJOINABLE_THREAD,
|
|
|
|
0);
|
|
|
|
if (!thr) {
|
|
|
|
mThreadCount--;
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
else
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Unable to find a thread for looking up host [%s].\n", rec->host));
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
nsresult
|
|
|
|
nsHostResolver::IssueLookup(nsHostRecord *rec)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
NS_ASSERTION(!rec->resolving, "record is already being resolved");
|
|
|
|
|
|
|
|
// Add rec to one of the pending queues, possibly removing it from mEvictionQ.
|
|
|
|
// If rec is on mEvictionQ, then we can just move the owning
|
|
|
|
// reference over to the new active queue.
|
|
|
|
if (rec->next == rec)
|
|
|
|
NS_ADDREF(rec);
|
|
|
|
else {
|
|
|
|
PR_REMOVE_LINK(rec);
|
|
|
|
mEvictionQSize--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsHighPriority(rec->flags))
|
|
|
|
PR_APPEND_LINK(rec, &mHighQ);
|
|
|
|
else if (IsMediumPriority(rec->flags))
|
|
|
|
PR_APPEND_LINK(rec, &mMediumQ);
|
|
|
|
else
|
|
|
|
PR_APPEND_LINK(rec, &mLowQ);
|
|
|
|
mPendingCount++;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
rec->resolving = true;
|
|
|
|
rec->onQueue = true;
|
2008-11-07 15:00:26 -08:00
|
|
|
|
|
|
|
rv = ConditionallyCreateThread(rec);
|
|
|
|
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG (("DNS thread counters: total=%d any-live=%d idle=%d pending=%d\n",
|
2008-11-07 15:00:26 -08:00
|
|
|
mThreadCount,
|
2009-01-14 09:18:27 -08:00
|
|
|
mActiveAnyThreadCount,
|
2008-11-07 15:00:26 -08:00
|
|
|
mNumIdleThreads,
|
|
|
|
mPendingCount));
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::DeQueue(PRCList &aQ, nsHostRecord **aResult)
|
|
|
|
{
|
|
|
|
*aResult = static_cast<nsHostRecord *>(aQ.next);
|
|
|
|
PR_REMOVE_AND_INIT_LINK(*aResult);
|
|
|
|
mPendingCount--;
|
2011-10-17 07:59:28 -07:00
|
|
|
(*aResult)->onQueue = false;
|
2008-11-07 15:00:26 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-01-14 09:18:27 -08:00
|
|
|
nsHostResolver::GetHostToLookup(nsHostRecord **result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool timedOut = false;
|
2009-01-14 09:18:27 -08:00
|
|
|
PRIntervalTime epoch, now, timeout;
|
|
|
|
|
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-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-14 09:18:27 -08:00
|
|
|
timeout = (mNumIdleThreads >= HighThreadThreshold) ? mShortIdleTimeout : mLongIdleTimeout;
|
|
|
|
epoch = PR_IntervalNow();
|
|
|
|
|
2008-11-07 15:00:26 -08:00
|
|
|
while (!mShutdown) {
|
|
|
|
// remove next record from Q; hand over owning reference. Check high, then med, then low
|
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&mHighQ)) {
|
|
|
|
DeQueue (mHighQ, result);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-11-07 15:00:26 -08:00
|
|
|
}
|
|
|
|
|
2009-01-14 09:18:27 -08:00
|
|
|
if (mActiveAnyThreadCount < HighThreadThreshold) {
|
2008-11-07 15:00:26 -08:00
|
|
|
if (!PR_CLIST_IS_EMPTY(&mMediumQ)) {
|
|
|
|
DeQueue (mMediumQ, result);
|
2009-01-14 09:18:27 -08:00
|
|
|
mActiveAnyThreadCount++;
|
2011-10-17 07:59:28 -07:00
|
|
|
(*result)->usingAnyThread = true;
|
|
|
|
return true;
|
2008-11-07 15:00:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&mLowQ)) {
|
|
|
|
DeQueue (mLowQ, result);
|
2009-01-14 09:18:27 -08:00
|
|
|
mActiveAnyThreadCount++;
|
2011-10-17 07:59:28 -07:00
|
|
|
(*result)->usingAnyThread = true;
|
|
|
|
return true;
|
2008-11-07 15:00:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 09:18:27 -08:00
|
|
|
// Determining timeout is racy, so allow one cycle through checking the queues
|
|
|
|
// before exiting.
|
|
|
|
if (timedOut)
|
|
|
|
break;
|
|
|
|
|
2008-11-07 15:00:26 -08: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
|
|
|
|
|
|
|
|
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-03-31 21:29:02 -07:00
|
|
|
mIdleThreadCV.Wait(timeout);
|
2008-11-07 15:00:26 -08:00
|
|
|
mNumIdleThreads--;
|
2009-01-14 09:18:27 -08:00
|
|
|
|
|
|
|
now = PR_IntervalNow();
|
|
|
|
|
|
|
|
if ((PRIntervalTime)(now - epoch) >= timeout)
|
2011-10-17 07:59:28 -07:00
|
|
|
timedOut = true;
|
2009-01-14 09:18:27 -08: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;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-01-14 09:18:27 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// tell thread to exit...
|
|
|
|
mThreadCount--;
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-20 11:47:07 -08:00
|
|
|
nsHostResolver::OnLookupComplete(nsHostRecord *rec, nsresult status, AddrInfo *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// get the list of pending callbacks for this lookup, and notify
|
|
|
|
// them that the lookup is complete.
|
|
|
|
PRCList cbs;
|
|
|
|
PR_INIT_CLIST(&cbs);
|
|
|
|
{
|
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-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// grab list of callbacks to notify
|
|
|
|
MoveCList(rec->callbacks, cbs);
|
|
|
|
|
2007-09-28 07:43:03 -07:00
|
|
|
// update record fields. We might have a rec->addr_info already if a
|
|
|
|
// previous lookup result expired and we're reresolving it..
|
2012-12-20 11:47:07 -08:00
|
|
|
AddrInfo *old_addr_info;
|
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-03-31 21:29:02 -07:00
|
|
|
{
|
2011-07-21 06:18:01 -07:00
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
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-03-31 21:29:02 -07:00
|
|
|
old_addr_info = rec->addr_info;
|
|
|
|
rec->addr_info = result;
|
|
|
|
rec->addr_info_gencnt++;
|
|
|
|
}
|
2012-12-20 11:47:07 -08:00
|
|
|
delete old_addr_info;
|
|
|
|
|
2008-04-08 17:42:52 -07:00
|
|
|
rec->expiration = NowInMinutes();
|
2008-10-13 14:04:09 -07:00
|
|
|
if (result) {
|
2008-04-08 17:42:52 -07:00
|
|
|
rec->expiration += mMaxCacheLifetime;
|
2011-10-17 07:59:28 -07:00
|
|
|
rec->negative = false;
|
2008-10-13 14:04:09 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rec->expiration += 1; /* one minute for negative cache */
|
2011-10-17 07:59:28 -07:00
|
|
|
rec->negative = true;
|
2008-10-13 14:04:09 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
rec->resolving = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-14 09:18:27 -08:00
|
|
|
if (rec->usingAnyThread) {
|
|
|
|
mActiveAnyThreadCount--;
|
2011-10-17 07:59:28 -07:00
|
|
|
rec->usingAnyThread = false;
|
2009-01-14 09:18:27 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rec->addr_info && !mShutdown) {
|
|
|
|
// add to mEvictionQ
|
|
|
|
PR_APPEND_LINK(rec, &mEvictionQ);
|
|
|
|
NS_ADDREF(rec);
|
|
|
|
if (mEvictionQSize < mMaxCacheEntries)
|
|
|
|
mEvictionQSize++;
|
|
|
|
else {
|
|
|
|
// remove first element on mEvictionQ
|
|
|
|
nsHostRecord *head =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsHostRecord *>(PR_LIST_HEAD(&mEvictionQ));
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_REMOVE_AND_INIT_LINK(head);
|
|
|
|
PL_DHashTableOperate(&mDB, (nsHostKey *) head, PL_DHASH_REMOVE);
|
2011-12-16 18:24:17 -08:00
|
|
|
|
|
|
|
if (!head->negative) {
|
|
|
|
// record the age of the entry upon eviction.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t age =
|
2011-12-16 18:24:17 -08:00
|
|
|
NowInMinutes() - (head->expiration - mMaxCacheLifetime);
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_CLEANUP_AGE, age);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// release reference to rec owned by mEvictionQ
|
|
|
|
NS_RELEASE(head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&cbs)) {
|
|
|
|
PRCList *node = cbs.next;
|
|
|
|
while (node != &cbs) {
|
|
|
|
nsResolveHostCallback *callback =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsResolveHostCallback *>(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
node = node->next;
|
|
|
|
callback->OnLookupComplete(this, rec, status);
|
|
|
|
// NOTE: callback must not be dereferenced after this point!!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(rec);
|
|
|
|
}
|
|
|
|
|
2012-01-20 15:14:46 -08:00
|
|
|
void
|
|
|
|
nsHostResolver::CancelAsyncRequest(const char *host,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af,
|
2012-01-20 15:14:46 -08:00
|
|
|
nsIDNSListener *aListener,
|
|
|
|
nsresult status)
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// Lookup the host record associated with host, flags & address family
|
|
|
|
nsHostKey key = { host, flags, af };
|
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>
|
|
|
|
(PL_DHashTableOperate(&mDB, &key, PL_DHASH_LOOKUP));
|
|
|
|
if (he && he->rec) {
|
|
|
|
nsHostRecord* recPtr = NULL;
|
|
|
|
PRCList *node = he->rec->callbacks.next;
|
|
|
|
// Remove the first nsDNSAsyncRequest callback which matches the
|
|
|
|
// supplied listener object
|
|
|
|
while (node != &he->rec->callbacks) {
|
|
|
|
nsResolveHostCallback *callback
|
|
|
|
= static_cast<nsResolveHostCallback *>(node);
|
|
|
|
if (callback && (callback->EqualsAsyncListener(aListener))) {
|
|
|
|
// Remove from the list of callbacks
|
|
|
|
PR_REMOVE_LINK(callback);
|
|
|
|
recPtr = he->rec;
|
|
|
|
callback->OnLookupComplete(this, recPtr, status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no more callbacks, remove the hash table entry
|
|
|
|
if (recPtr && PR_CLIST_IS_EMPTY(&recPtr->callbacks)) {
|
|
|
|
PL_DHashTableOperate(&mDB, (nsHostKey *)recPtr, PL_DHASH_REMOVE);
|
|
|
|
// If record is on a Queue, remove it and then deref it
|
|
|
|
if (recPtr->next != recPtr) {
|
|
|
|
PR_REMOVE_LINK(recPtr);
|
|
|
|
NS_RELEASE(recPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHostResolver::ThreadFunc(void *arg)
|
|
|
|
{
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("DNS lookup thread starting execution.\n"));
|
2012-06-12 10:06:20 -07:00
|
|
|
|
|
|
|
static nsThreadPoolNaming naming;
|
|
|
|
naming.SetThreadPoolName(NS_LITERAL_CSTRING("DNS Resolver"));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
|
|
|
nsResState rs;
|
|
|
|
#endif
|
2009-01-14 09:18:27 -08:00
|
|
|
nsHostResolver *resolver = (nsHostResolver *)arg;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHostRecord *rec;
|
2012-12-20 11:47:07 -08:00
|
|
|
PRAddrInfo *prai = nullptr;
|
2009-01-14 09:18:27 -08:00
|
|
|
while (resolver->GetHostToLookup(&rec)) {
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Calling getaddrinfo for host [%s].\n", rec->host));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-09 00:09:40 -07:00
|
|
|
int flags = PR_AI_ADDRCONFIG;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!(rec->flags & RES_CANON_NAME))
|
|
|
|
flags |= PR_AI_NOCANONNAME;
|
|
|
|
|
2011-12-16 18:24:17 -08:00
|
|
|
TimeStamp startTime = TimeStamp::Now();
|
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
prai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
2012-12-20 11:47:07 -08:00
|
|
|
if (!prai && rs.Reset())
|
|
|
|
prai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2011-12-16 18:24:17 -08:00
|
|
|
TimeDuration elapsed = TimeStamp::Now() - startTime;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t millis = static_cast<uint32_t>(elapsed.ToMilliseconds());
|
2011-12-16 18:24:17 -08:00
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
// convert error code to nsresult
|
2011-12-16 18:24:17 -08:00
|
|
|
nsresult status;
|
2012-12-20 11:47:07 -08:00
|
|
|
AddrInfo *ai = nullptr;
|
|
|
|
if (prai) {
|
|
|
|
ai = new AddrInfo(rec->host, prai);
|
|
|
|
|
2011-12-16 18:24:17 -08:00
|
|
|
status = NS_OK;
|
|
|
|
|
|
|
|
Telemetry::Accumulate(!rec->addr_info_gencnt ?
|
|
|
|
Telemetry::DNS_LOOKUP_TIME :
|
|
|
|
Telemetry::DNS_RENEWAL_TIME,
|
|
|
|
millis);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_FAILED_LOOKUP_TIME, millis);
|
|
|
|
}
|
2012-10-26 05:12:53 -07:00
|
|
|
|
|
|
|
// OnLookupComplete may release "rec", log before we lose it.
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("Lookup completed for host [%s].\n", rec->host));
|
2012-10-26 05:12:53 -07:00
|
|
|
resolver->OnLookupComplete(rec, status, ai);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
NS_RELEASE(resolver);
|
2012-07-16 15:37:28 -07:00
|
|
|
LOG(("DNS lookup thread ending execution.\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHostResolver::Create(uint32_t maxCacheEntries,
|
|
|
|
uint32_t maxCacheLifetime,
|
|
|
|
uint32_t lifetimeGracePeriod,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHostResolver **result)
|
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gHostResolverLog)
|
|
|
|
gHostResolverLog = PR_NewLogModule("nsHostResolver");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsHostResolver *res = new nsHostResolver(maxCacheEntries,
|
2011-12-16 18:24:12 -08:00
|
|
|
maxCacheLifetime,
|
|
|
|
lifetimeGracePeriod);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!res)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(res);
|
|
|
|
|
|
|
|
nsresult rv = res->Init();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_RELEASE(res);
|
|
|
|
|
|
|
|
*result = res;
|
|
|
|
return rv;
|
|
|
|
}
|
2012-12-10 06:13:55 -08:00
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
CacheEntryEnumerator(PLDHashTable *table, PLDHashEntryHdr *entry,
|
|
|
|
uint32_t number, void *arg)
|
|
|
|
{
|
2012-12-20 11:47:07 -08:00
|
|
|
// We don't pay attention to address literals, only resolved domains.
|
|
|
|
// Also require a host.
|
|
|
|
nsHostRecord *rec = static_cast<nsHostDBEnt*>(entry)->rec;
|
|
|
|
if (!rec->addr_info || !rec->host) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
2012-12-10 06:13:55 -08:00
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
DNSCacheEntries info;
|
|
|
|
info.hostname = rec->host;
|
|
|
|
info.family = rec->af;
|
|
|
|
info.expiration = ((int64_t)rec->expiration - NowInMinutes()) * 60;
|
|
|
|
if (info.expiration <= 0) {
|
2012-12-10 06:13:55 -08:00
|
|
|
// We only need valid DNS cache entries
|
2012-12-20 11:47:07 -08:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
2012-12-10 06:13:55 -08:00
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(rec->addr_info_lock);
|
2012-12-10 06:13:55 -08:00
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
NetAddr *addr = nullptr;
|
|
|
|
NetAddrElement *addrElement = rec->addr_info->mAddresses.getFirst();
|
|
|
|
if (addrElement) {
|
|
|
|
addr = &addrElement->mAddress;
|
|
|
|
}
|
|
|
|
while (addr) {
|
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(addr, buf, sizeof(buf))) {
|
|
|
|
info.hostaddr.AppendElement(buf);
|
|
|
|
}
|
|
|
|
addrElement = addrElement->getNext();
|
|
|
|
if (addrElement) {
|
|
|
|
addr = &addrElement->mAddress;
|
2012-12-10 06:13:55 -08:00
|
|
|
}
|
|
|
|
}
|
2012-12-14 05:00:19 -08:00
|
|
|
}
|
2012-12-13 06:18:33 -08:00
|
|
|
|
2012-12-20 11:47:07 -08:00
|
|
|
nsTArray<DNSCacheEntries> *args = static_cast<nsTArray<DNSCacheEntries> *>(arg);
|
|
|
|
args->AppendElement(info);
|
|
|
|
|
2012-12-10 06:13:55 -08:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::GetDNSCacheEntries(nsTArray<DNSCacheEntries> *args)
|
|
|
|
{
|
|
|
|
PL_DHashTableEnumerate(&mDB, CacheEntryEnumerator, args);
|
|
|
|
}
|