2013-12-11 11:03:51 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et tw=80 : */
|
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
|
|
|
|
|
|
|
#include "nsDNSService2.h"
|
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsIDNSListener.h"
|
|
|
|
#include "nsICancelable.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2013-06-26 07:54:05 -07:00
|
|
|
#include "nsIXPConnect.h"
|
2011-12-30 09:53:44 -08:00
|
|
|
#include "nsProxyRelease.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsNetCID.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2008-11-07 15:00:26 -08:00
|
|
|
#include "nsDNSPrefetch.h"
|
2011-08-19 08:47:43 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2010-06-17 11:33:42 -07:00
|
|
|
#include "nsIProtocolProxyService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prsystem.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "prmon.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "plstr.h"
|
2010-11-24 21:20:11 -08:00
|
|
|
#include "nsIOService.h"
|
2012-05-11 12:59:19 -07:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2013-01-10 10:44:17 -08:00
|
|
|
#include "nsNetAddr.h"
|
2013-03-18 20:39:53 -07:00
|
|
|
#include "nsProxyRelease.h"
|
2013-08-31 09:06:14 -07:00
|
|
|
#include "nsIObserverService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-05 20:18:25 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-03-28 10:38:04 -07:00
|
|
|
#include "mozilla/VisualEventTracer.h"
|
2013-12-11 11:03:51 -08:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
|
|
|
#include "mozilla/net/ChildDNSService.h"
|
|
|
|
#include "mozilla/net/DNSListenerProxy.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-23 13:08:43 -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
|
|
|
static const char kPrefDnsCacheEntries[] = "network.dnsCacheEntries";
|
|
|
|
static const char kPrefDnsCacheExpiration[] = "network.dnsCacheExpiration";
|
2011-12-16 18:24:12 -08:00
|
|
|
static const char kPrefDnsCacheGrace[] = "network.dnsCacheExpirationGracePeriod";
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char kPrefIPv4OnlyDomains[] = "network.dns.ipv4OnlyDomains";
|
|
|
|
static const char kPrefDisableIPv6[] = "network.dns.disableIPv6";
|
2008-11-07 15:00:26 -08:00
|
|
|
static const char kPrefDisablePrefetch[] = "network.dns.disablePrefetch";
|
2012-05-11 12:59:19 -07:00
|
|
|
static const char kPrefDnsLocalDomains[] = "network.dns.localDomains";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsDNSRecord : public nsIDNSRecord
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIDNSRECORD
|
|
|
|
|
|
|
|
nsDNSRecord(nsHostRecord *hostRecord)
|
|
|
|
: mHostRecord(hostRecord)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mIter(nullptr)
|
2007-12-11 13:45:42 -08:00
|
|
|
, mIterGenCnt(-1)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mDone(false) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsDNSRecord() {}
|
|
|
|
|
|
|
|
nsRefPtr<nsHostRecord> mHostRecord;
|
2012-12-23 13:08:43 -08:00
|
|
|
NetAddrElement *mIter;
|
2007-12-11 13:45:42 -08:00
|
|
|
int mIterGenCnt; // the generation count of
|
|
|
|
// mHostRecord->addr_info when we
|
|
|
|
// start iterating
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDone;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDNSRecord, nsIDNSRecord)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::GetCanonicalName(nsACString &result)
|
|
|
|
{
|
|
|
|
// this method should only be called if we have a CNAME
|
|
|
|
NS_ENSURE_TRUE(mHostRecord->flags & nsHostResolver::RES_CANON_NAME,
|
|
|
|
NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
// if the record is for an IP address literal, then the canonical
|
|
|
|
// host name is the IP address literal.
|
|
|
|
const char *cname;
|
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(mHostRecord->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
|
|
|
if (mHostRecord->addr_info)
|
2013-04-08 09:02:41 -07:00
|
|
|
cname = mHostRecord->addr_info->mCanonicalName ?
|
|
|
|
mHostRecord->addr_info->mCanonicalName :
|
|
|
|
mHostRecord->addr_info->mHostName;
|
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
|
|
|
else
|
|
|
|
cname = mHostRecord->host;
|
|
|
|
result.Assign(cname);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-23 13:08:43 -08:00
|
|
|
nsDNSRecord::GetNextAddr(uint16_t port, NetAddr *addr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-23 13:08:43 -08:00
|
|
|
if (mDone) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-12-23 13:08:43 -08:00
|
|
|
}
|
2011-07-21 06:18:01 -07:00
|
|
|
|
2012-12-14 05:00:19 -08:00
|
|
|
mHostRecord->addr_info_lock.Lock();
|
|
|
|
if (mHostRecord->addr_info) {
|
2012-12-23 13:08:43 -08:00
|
|
|
if (mIterGenCnt != mHostRecord->addr_info_gencnt) {
|
|
|
|
// mHostRecord->addr_info has changed, restart the iteration.
|
2012-07-30 07:20:58 -07:00
|
|
|
mIter = nullptr;
|
2007-12-11 13:45:42 -08:00
|
|
|
mIterGenCnt = mHostRecord->addr_info_gencnt;
|
2011-07-21 06:18:01 -07:00
|
|
|
}
|
|
|
|
|
2012-12-23 13:08:43 -08:00
|
|
|
bool startedFresh = !mIter;
|
|
|
|
|
2011-07-21 06:18:01 -07:00
|
|
|
do {
|
2012-12-23 13:08:43 -08:00
|
|
|
if (!mIter) {
|
|
|
|
mIter = mHostRecord->addr_info->mAddresses.getFirst();
|
|
|
|
} else {
|
|
|
|
mIter = mIter->getNext();
|
|
|
|
}
|
2011-07-21 06:18:01 -07:00
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
while (mIter && mHostRecord->Blacklisted(&mIter->mAddress));
|
2011-07-21 06:18:01 -07:00
|
|
|
|
2012-12-23 13:08:43 -08:00
|
|
|
if (!mIter && startedFresh) {
|
|
|
|
// If everything was blacklisted we want to reset the blacklist (and
|
2011-07-21 06:18:01 -07:00
|
|
|
// likely relearn it) and return the first address. That is better
|
2012-12-23 13:08:43 -08:00
|
|
|
// than nothing.
|
2011-07-21 06:18:01 -07:00
|
|
|
mHostRecord->ResetBlacklist();
|
2012-12-23 13:08:43 -08:00
|
|
|
mIter = mHostRecord->addr_info->mAddresses.getFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIter) {
|
|
|
|
memcpy(addr, &mIter->mAddress, sizeof(NetAddr));
|
2007-12-11 13:45:42 -08:00
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
|
2011-07-21 06:18:01 -07:00
|
|
|
mHostRecord->addr_info_lock.Unlock();
|
2012-12-23 13:08:43 -08:00
|
|
|
|
2007-12-11 13:45:42 -08:00
|
|
|
if (!mIter) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mDone = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2007-12-11 13:45:42 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-12-14 05:00:19 -08:00
|
|
|
mHostRecord->addr_info_lock.Unlock();
|
2012-12-23 13:08:43 -08:00
|
|
|
|
2008-04-08 17:42:52 -07:00
|
|
|
if (!mHostRecord->addr) {
|
|
|
|
// Both mHostRecord->addr_info and mHostRecord->addr are null.
|
|
|
|
// This can happen if mHostRecord->addr_info expired and the
|
|
|
|
// attempt to reresolve it failed.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
memcpy(addr, mHostRecord->addr, sizeof(NetAddr));
|
|
|
|
mDone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set given port
|
|
|
|
port = htons(port);
|
|
|
|
if (addr->raw.family == AF_INET) {
|
|
|
|
addr->inet.port = port;
|
2012-12-20 11:47:07 -08:00
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
else if (addr->raw.family == AF_INET6) {
|
|
|
|
addr->inet6.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-10 10:44:17 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::GetScriptableNextAddr(uint16_t port, nsINetAddr * *result)
|
|
|
|
{
|
|
|
|
NetAddr addr;
|
|
|
|
nsresult rv = GetNextAddr(port, &addr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*result = new nsNetAddr(&addr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::GetNextAddrAsString(nsACString &result)
|
|
|
|
{
|
2012-12-23 13:08:43 -08:00
|
|
|
NetAddr addr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = GetNextAddr(0, &addr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-12-23 13:08:43 -08:00
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(&addr, buf, sizeof(buf))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
result.Assign(buf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
NS_ERROR("NetAddrToString failed unexpectedly");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE; // conversion failed for some reason
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDNSRecord::HasMore(bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-23 13:08:43 -08:00
|
|
|
if (mDone) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2012-12-23 13:08:43 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
|
|
|
|
NetAddrElement *iterCopy = mIter;
|
|
|
|
|
|
|
|
NetAddr addr;
|
|
|
|
*result = NS_SUCCEEDED(GetNextAddr(0, &addr));
|
|
|
|
|
|
|
|
mIter = iterCopy;
|
|
|
|
mDone = false;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::Rewind()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mIter = nullptr;
|
2007-12-11 13:45:42 -08:00
|
|
|
mIterGenCnt = -1;
|
2011-10-17 07:59:28 -07:00
|
|
|
mDone = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-21 06:18:01 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDNSRecord::ReportUnusable(uint16_t aPort)
|
2011-07-21 06:18:01 -07:00
|
|
|
{
|
|
|
|
// right now we don't use the port in the blacklist
|
|
|
|
|
2012-12-23 13:08:43 -08:00
|
|
|
MutexAutoLock lock(mHostRecord->addr_info_lock);
|
2011-07-21 06:18:01 -07:00
|
|
|
|
|
|
|
// Check that we are using a real addr_info (as opposed to a single
|
|
|
|
// constant address), and that the generation count is valid. Otherwise,
|
|
|
|
// ignore the report.
|
|
|
|
|
|
|
|
if (mHostRecord->addr_info &&
|
2013-05-14 16:35:18 -07:00
|
|
|
mIterGenCnt == mHostRecord->addr_info_gencnt &&
|
|
|
|
mIter) {
|
2012-12-23 13:08:43 -08:00
|
|
|
mHostRecord->ReportUnusable(&mIter->mAddress);
|
2011-07-21 06:18:01 -07:00
|
|
|
}
|
2012-12-23 13:08:43 -08:00
|
|
|
|
2011-07-21 06:18:01 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-05 20:18:25 -07:00
|
|
|
class nsDNSAsyncRequest MOZ_FINAL : public nsResolveHostCallback
|
|
|
|
, public nsICancelable
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSICANCELABLE
|
|
|
|
|
|
|
|
nsDNSAsyncRequest(nsHostResolver *res,
|
|
|
|
const nsACString &host,
|
|
|
|
nsIDNSListener *listener,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mResolver(res)
|
|
|
|
, mHost(host)
|
2013-06-26 07:54:05 -07:00
|
|
|
, mListener(listener)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mFlags(flags)
|
|
|
|
, mAF(af) {}
|
|
|
|
~nsDNSAsyncRequest() {}
|
|
|
|
|
|
|
|
void OnLookupComplete(nsHostResolver *, nsHostRecord *, nsresult);
|
2012-01-20 15:14:46 -08:00
|
|
|
// Returns TRUE if the DNS listener arg is the same as the member listener
|
|
|
|
// Used in Cancellations to remove DNS requests associated with a
|
|
|
|
// particular hostname and nsIDNSListener
|
|
|
|
bool EqualsAsyncListener(nsIDNSListener *aListener);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-24 15:24:12 -08:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf) const;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<nsHostResolver> mResolver;
|
|
|
|
nsCString mHost; // hostname we're resolving
|
2013-06-26 07:54:05 -07:00
|
|
|
nsCOMPtr<nsIDNSListener> mListener;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t mFlags;
|
|
|
|
uint16_t mAF;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDNSAsyncRequest::OnLookupComplete(nsHostResolver *resolver,
|
|
|
|
nsHostRecord *hostRecord,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
// need to have an owning ref when we issue the callback to enable
|
|
|
|
// the caller to be able to addref/release multiple times without
|
|
|
|
// destroying the record prematurely.
|
|
|
|
nsCOMPtr<nsIDNSRecord> rec;
|
|
|
|
if (NS_SUCCEEDED(status)) {
|
|
|
|
NS_ASSERTION(hostRecord, "no host record");
|
|
|
|
rec = new nsDNSRecord(hostRecord);
|
|
|
|
if (!rec)
|
|
|
|
status = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-03-28 10:38:04 -07:00
|
|
|
MOZ_EVENT_TRACER_DONE(this, "net::dns::lookup");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mListener->OnLookupComplete(this, rec, status);
|
2013-06-26 07:54:05 -07:00
|
|
|
mListener = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// release the reference to ourselves that was added before we were
|
|
|
|
// handed off to the host resolver.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
|
2012-01-20 15:14:46 -08:00
|
|
|
bool
|
|
|
|
nsDNSAsyncRequest::EqualsAsyncListener(nsIDNSListener *aListener)
|
|
|
|
{
|
|
|
|
return (aListener == mListener);
|
|
|
|
}
|
|
|
|
|
2013-11-24 15:24:12 -08:00
|
|
|
size_t
|
|
|
|
nsDNSAsyncRequest::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = mallocSizeOf(this);
|
|
|
|
|
|
|
|
// The following fields aren't measured.
|
|
|
|
// - mHost, because it's a non-owning pointer
|
|
|
|
// - mResolver, because it's a non-owning pointer
|
|
|
|
// - mListener, because it's a non-owning pointer
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDNSAsyncRequest, nsICancelable)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSAsyncRequest::Cancel(nsresult reason)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(NS_FAILED(reason));
|
|
|
|
mResolver->DetachCallback(mHost.get(), mFlags, mAF, this, reason);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsDNSSyncRequest : public nsResolveHostCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDNSSyncRequest(PRMonitor *mon)
|
2011-10-17 07:59:28 -07:00
|
|
|
: mDone(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mStatus(NS_OK)
|
|
|
|
, mMonitor(mon) {}
|
|
|
|
virtual ~nsDNSSyncRequest() {}
|
|
|
|
|
|
|
|
void OnLookupComplete(nsHostResolver *, nsHostRecord *, nsresult);
|
2012-01-20 15:14:46 -08:00
|
|
|
bool EqualsAsyncListener(nsIDNSListener *aListener);
|
2013-11-24 15:24:12 -08:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDone;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult mStatus;
|
|
|
|
nsRefPtr<nsHostRecord> mHostRecord;
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRMonitor *mMonitor;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDNSSyncRequest::OnLookupComplete(nsHostResolver *resolver,
|
|
|
|
nsHostRecord *hostRecord,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
// store results, and wake up nsDNSService::Resolve to process results.
|
|
|
|
PR_EnterMonitor(mMonitor);
|
2011-10-17 07:59:28 -07:00
|
|
|
mDone = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mStatus = status;
|
|
|
|
mHostRecord = hostRecord;
|
|
|
|
PR_Notify(mMonitor);
|
|
|
|
PR_ExitMonitor(mMonitor);
|
|
|
|
}
|
|
|
|
|
2012-01-20 15:14:46 -08:00
|
|
|
bool
|
|
|
|
nsDNSSyncRequest::EqualsAsyncListener(nsIDNSListener *aListener)
|
|
|
|
{
|
|
|
|
// Sync request: no listener to compare
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-24 15:24:12 -08:00
|
|
|
size_t
|
|
|
|
nsDNSSyncRequest::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = mallocSizeOf(this);
|
|
|
|
|
|
|
|
// The following fields aren't measured.
|
|
|
|
// - mHostRecord, because it's a non-owning pointer
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
|
|
|
// - mMonitor
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsDNSService::nsDNSService()
|
2013-12-07 22:09:10 -08:00
|
|
|
: mLock("nsDNSServer.mLock")
|
2011-10-17 07:59:28 -07:00
|
|
|
, mFirstTime(true)
|
2012-09-17 16:45:10 -07:00
|
|
|
, mOffline(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDNSService::~nsDNSService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-07 22:09:10 -08:00
|
|
|
NS_IMPL_ISUPPORTS4(nsDNSService, nsIDNSService, nsPIDNSService, nsIObserver,
|
|
|
|
nsIMemoryReporter)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-11 11:03:51 -08:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDNSService impl:
|
|
|
|
* singleton instance ctor/dtor methods
|
|
|
|
******************************************************************************/
|
|
|
|
static nsDNSService *gDNSService;
|
|
|
|
|
|
|
|
nsIDNSService*
|
|
|
|
nsDNSService::GetXPCOMSingleton()
|
|
|
|
{
|
|
|
|
if (IsNeckoChild()) {
|
|
|
|
return ChildDNSService::GetSingleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetSingleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDNSService*
|
|
|
|
nsDNSService::GetSingleton()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!IsNeckoChild(), "not a parent process");
|
|
|
|
|
|
|
|
if (gDNSService) {
|
|
|
|
NS_ADDREF(gDNSService);
|
|
|
|
return gDNSService;
|
|
|
|
}
|
|
|
|
|
|
|
|
gDNSService = new nsDNSService();
|
|
|
|
if (gDNSService) {
|
|
|
|
NS_ADDREF(gDNSService);
|
|
|
|
if (NS_FAILED(gDNSService->Init())) {
|
|
|
|
NS_RELEASE(gDNSService);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gDNSService;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::Init()
|
|
|
|
{
|
2012-09-17 16:45:10 -07:00
|
|
|
if (mResolver)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(!mResolver, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
// prefs
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t maxCacheEntries = 400;
|
2014-03-09 03:01:56 -07:00
|
|
|
uint32_t maxCacheLifetime = 120; // seconds
|
|
|
|
uint32_t lifetimeGracePeriod = 60; // seconds
|
2011-09-28 23:19:26 -07:00
|
|
|
bool disableIPv6 = false;
|
|
|
|
bool disablePrefetch = false;
|
2010-06-17 11:33:42 -07:00
|
|
|
int proxyType = nsIProtocolProxyService::PROXYCONFIG_DIRECT;
|
2013-11-24 15:24:12 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAdoptingCString ipv4OnlyDomains;
|
2012-05-11 12:59:19 -07:00
|
|
|
nsAdoptingCString localDomains;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// read prefs
|
2012-01-16 17:48:29 -08:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prefs) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t val;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(prefs->GetIntPref(kPrefDnsCacheEntries, &val)))
|
2012-08-22 08:56:38 -07:00
|
|
|
maxCacheEntries = (uint32_t) val;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(prefs->GetIntPref(kPrefDnsCacheExpiration, &val)))
|
2014-03-09 03:01:56 -07:00
|
|
|
maxCacheLifetime = val;
|
2011-12-16 18:24:12 -08:00
|
|
|
if (NS_SUCCEEDED(prefs->GetIntPref(kPrefDnsCacheGrace, &val)))
|
2014-03-09 03:01:56 -07:00
|
|
|
lifetimeGracePeriod = val;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// ASSUMPTION: pref branch does not modify out params on failure
|
|
|
|
prefs->GetBoolPref(kPrefDisableIPv6, &disableIPv6);
|
|
|
|
prefs->GetCharPref(kPrefIPv4OnlyDomains, getter_Copies(ipv4OnlyDomains));
|
2012-05-11 12:59:19 -07:00
|
|
|
prefs->GetCharPref(kPrefDnsLocalDomains, getter_Copies(localDomains));
|
2008-11-07 15:00:26 -08:00
|
|
|
prefs->GetBoolPref(kPrefDisablePrefetch, &disablePrefetch);
|
2009-05-06 14:26:33 -07:00
|
|
|
|
|
|
|
// If a manual proxy is in use, disable prefetch implicitly
|
|
|
|
prefs->GetIntPref("network.proxy.type", &proxyType);
|
2007-03-22 10:30:00 -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
|
|
|
if (mFirstTime) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mFirstTime = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// register as prefs observer
|
|
|
|
if (prefs) {
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(kPrefDnsCacheEntries, this, false);
|
|
|
|
prefs->AddObserver(kPrefDnsCacheExpiration, this, false);
|
2011-12-16 18:24:12 -08:00
|
|
|
prefs->AddObserver(kPrefDnsCacheGrace, this, false);
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(kPrefIPv4OnlyDomains, this, false);
|
2012-05-11 12:59:19 -07:00
|
|
|
prefs->AddObserver(kPrefDnsLocalDomains, this, false);
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver(kPrefDisableIPv6, this, false);
|
|
|
|
prefs->AddObserver(kPrefDisablePrefetch, this, false);
|
2009-05-06 14:26:33 -07:00
|
|
|
|
|
|
|
// Monitor these to see if there is a change in proxy configuration
|
|
|
|
// If a manual proxy is in use, disable prefetch implicitly
|
2011-10-17 07:59:28 -07:00
|
|
|
prefs->AddObserver("network.proxy.type", this, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-08-31 09:06:14 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
observerService->AddObserver(this, "last-pb-context-exited", false);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-24 21:20:11 -08:00
|
|
|
nsDNSPrefetch::Initialize(this);
|
|
|
|
|
|
|
|
// Don't initialize the resolver if we're in offline mode.
|
|
|
|
// Later on, the IO service will reinitialize us when going online.
|
|
|
|
if (gIOService->IsOffline() && !gIOService->IsComingOnline())
|
|
|
|
return NS_OK;
|
|
|
|
|
2013-03-03 08:36:37 -08:00
|
|
|
nsCOMPtr<nsIIDNService> idn = do_GetService(NS_IDNSERVICE_CONTRACTID);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsresult rv = nsHostResolver::Create(maxCacheEntries,
|
|
|
|
maxCacheLifetime,
|
2011-12-16 18:24:12 -08:00
|
|
|
lifetimeGracePeriod,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(res));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// now, set all of our member variables while holding the 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
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
mResolver = res;
|
|
|
|
mIDN = idn;
|
|
|
|
mIPv4OnlyDomains = ipv4OnlyDomains; // exchanges buffer ownership
|
|
|
|
mDisableIPv6 = disableIPv6;
|
2009-05-06 14:26:33 -07:00
|
|
|
|
|
|
|
// Disable prefetching either by explicit preference or if a manual proxy is configured
|
2010-06-17 11:33:42 -07:00
|
|
|
mDisablePrefetch = disablePrefetch || (proxyType == nsIProtocolProxyService::PROXYCONFIG_MANUAL);
|
2012-05-11 12:59:19 -07:00
|
|
|
|
|
|
|
mLocalDomains.Clear();
|
|
|
|
if (localDomains) {
|
|
|
|
nsAdoptingString domains;
|
|
|
|
domains.AssignASCII(nsDependentCString(localDomains).get());
|
|
|
|
nsCharSeparatedTokenizer tokenizer(domains, ',',
|
|
|
|
nsCharSeparatedTokenizerTemplate<>::SEPARATOR_OPTIONAL);
|
2013-11-24 15:24:12 -08:00
|
|
|
|
2012-05-11 12:59:19 -07:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsSubstring& domain = tokenizer.nextToken();
|
|
|
|
mLocalDomains.PutEntry(nsDependentCString(NS_ConvertUTF16toUTF8(domain).get()));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-11-24 15:24:12 -08:00
|
|
|
|
2013-11-06 21:35:30 -08:00
|
|
|
RegisterWeakMemoryReporter(this);
|
2013-11-24 15:24:12 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::Shutdown()
|
|
|
|
{
|
2013-11-06 21:35:30 -08:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2013-11-24 15:24:12 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
{
|
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
|
|
|
res = mResolver;
|
2012-07-30 07:20:58 -07:00
|
|
|
mResolver = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (res)
|
|
|
|
res->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-17 16:45:10 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::GetOffline(bool *offline)
|
|
|
|
{
|
|
|
|
*offline = mOffline;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::SetOffline(bool offline)
|
|
|
|
{
|
|
|
|
mOffline = offline;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-14 13:27:46 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::GetPrefetchEnabled(bool *outVal)
|
|
|
|
{
|
|
|
|
*outVal = !mDisablePrefetch;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::SetPrefetchEnabled(bool inVal)
|
|
|
|
{
|
|
|
|
mDisablePrefetch = !inVal;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-19 08:47:43 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::AsyncResolve(const nsACString &hostname,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDNSListener *listener,
|
2013-06-26 07:54:05 -07:00
|
|
|
nsIEventTarget *target_,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsICancelable **result)
|
|
|
|
{
|
|
|
|
// grab reference to global host resolver and IDN service. beware
|
|
|
|
// simultaneous shutdown!!
|
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsCOMPtr<nsIIDNService> idn;
|
2013-06-26 07:54:05 -07:00
|
|
|
nsCOMPtr<nsIEventTarget> target = target_;
|
2012-05-11 12:59:19 -07:00
|
|
|
bool localDomain = false;
|
2007-03-22 10:30:00 -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
|
|
|
MutexAutoLock lock(mLock);
|
2008-11-07 15:00:26 -08:00
|
|
|
|
2009-05-06 14:26:33 -07:00
|
|
|
if (mDisablePrefetch && (flags & RESOLVE_SPECULATE))
|
2008-11-07 15:00:26 -08:00
|
|
|
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mResolver;
|
|
|
|
idn = mIDN;
|
2012-05-11 12:59:19 -07:00
|
|
|
localDomain = mLocalDomains.GetEntry(hostname);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-02-24 11:59:26 -08:00
|
|
|
if (!res)
|
|
|
|
return NS_ERROR_OFFLINE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-17 16:45:10 -07:00
|
|
|
if (mOffline)
|
|
|
|
flags |= RESOLVE_OFFLINE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsACString *hostPtr = &hostname;
|
|
|
|
|
2012-05-11 12:59:19 -07:00
|
|
|
if (localDomain) {
|
|
|
|
hostPtr = &(NS_LITERAL_CSTRING("localhost"));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostACE;
|
2012-05-11 12:59:19 -07:00
|
|
|
if (idn && !IsASCII(*hostPtr)) {
|
|
|
|
if (NS_SUCCEEDED(idn->ConvertUTF8toACE(*hostPtr, hostACE)))
|
2007-03-22 10:30:00 -07:00
|
|
|
hostPtr = &hostACE;
|
|
|
|
}
|
|
|
|
|
2013-12-11 11:03:51 -08:00
|
|
|
// make sure JS callers get notification on the main thread
|
2013-06-26 07:54:05 -07:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> wrappedListener = do_QueryInterface(listener);
|
|
|
|
if (wrappedListener && !target) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
target = do_QueryInterface(mainThread);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (target) {
|
2011-08-19 08:47:43 -07:00
|
|
|
listener = new DNSListenerProxy(listener, target);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t af = GetAFForLookup(*hostPtr, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDNSAsyncRequest *req =
|
|
|
|
new nsDNSAsyncRequest(res, *hostPtr, listener, flags, af);
|
|
|
|
if (!req)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*result = req);
|
|
|
|
|
2013-03-28 10:38:04 -07:00
|
|
|
MOZ_EVENT_TRACER_NAME_OBJECT(req, hostname.BeginReading());
|
|
|
|
MOZ_EVENT_TRACER_WAIT(req, "net::dns::lookup");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// addref for resolver; will be released when OnLookupComplete is called.
|
|
|
|
NS_ADDREF(req);
|
|
|
|
rv = res->ResolveHost(req->mHost.get(), flags, af, req);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(req);
|
|
|
|
NS_RELEASE(*result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-20 15:14:46 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::CancelAsyncResolve(const nsACString &aHostname,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags,
|
2012-01-20 15:14:46 -08:00
|
|
|
nsIDNSListener *aListener,
|
|
|
|
nsresult aReason)
|
|
|
|
{
|
|
|
|
// grab reference to global host resolver and IDN service. beware
|
|
|
|
// simultaneous shutdown!!
|
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsCOMPtr<nsIIDNService> idn;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mDisablePrefetch && (aFlags & RESOLVE_SPECULATE))
|
|
|
|
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
|
|
|
|
|
|
|
res = mResolver;
|
|
|
|
idn = mIDN;
|
|
|
|
}
|
|
|
|
if (!res)
|
|
|
|
return NS_ERROR_OFFLINE;
|
|
|
|
|
|
|
|
nsCString hostname(aHostname);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostACE;
|
2012-01-20 15:14:46 -08:00
|
|
|
if (idn && !IsASCII(aHostname)) {
|
|
|
|
if (NS_SUCCEEDED(idn->ConvertUTF8toACE(aHostname, hostACE)))
|
|
|
|
hostname = hostACE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t af = GetAFForLookup(hostname, aFlags);
|
2012-01-20 15:14:46 -08:00
|
|
|
|
|
|
|
res->CancelAsyncRequest(hostname.get(), aFlags, af, aListener, aReason);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::Resolve(const nsACString &hostname,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDNSRecord **result)
|
|
|
|
{
|
|
|
|
// grab reference to global host resolver and IDN service. beware
|
|
|
|
// simultaneous shutdown!!
|
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsCOMPtr<nsIIDNService> idn;
|
2012-05-11 12:59:19 -07:00
|
|
|
bool localDomain = false;
|
2007-03-22 10:30:00 -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
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mResolver;
|
|
|
|
idn = mIDN;
|
2012-05-11 12:59:19 -07:00
|
|
|
localDomain = mLocalDomains.GetEntry(hostname);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(res, NS_ERROR_OFFLINE);
|
|
|
|
|
2012-09-17 16:45:10 -07:00
|
|
|
if (mOffline)
|
|
|
|
flags |= RESOLVE_OFFLINE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsACString *hostPtr = &hostname;
|
|
|
|
|
2012-05-11 12:59:19 -07:00
|
|
|
if (localDomain) {
|
|
|
|
hostPtr = &(NS_LITERAL_CSTRING("localhost"));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostACE;
|
2012-05-11 12:59:19 -07:00
|
|
|
if (idn && !IsASCII(*hostPtr)) {
|
|
|
|
if (NS_SUCCEEDED(idn->ConvertUTF8toACE(*hostPtr, hostACE)))
|
2007-03-22 10:30:00 -07:00
|
|
|
hostPtr = &hostACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// sync resolve: since the host resolver only works asynchronously, we need
|
|
|
|
// to use a mutex and a condvar to wait for the result. however, since the
|
|
|
|
// result may be in the resolvers cache, we might get called back recursively
|
|
|
|
// on the same thread. so, our mutex needs to be re-entrant. in other words,
|
|
|
|
// we need to use a monitor! ;-)
|
|
|
|
//
|
|
|
|
|
|
|
|
PRMonitor *mon = PR_NewMonitor();
|
|
|
|
if (!mon)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
PR_EnterMonitor(mon);
|
|
|
|
nsDNSSyncRequest syncReq(mon);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t af = GetAFForLookup(*hostPtr, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = res->ResolveHost(PromiseFlatCString(*hostPtr).get(), flags, af, &syncReq);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// wait for result
|
|
|
|
while (!syncReq.mDone)
|
|
|
|
PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
|
|
|
|
|
|
|
|
if (NS_FAILED(syncReq.mStatus))
|
|
|
|
rv = syncReq.mStatus;
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(syncReq.mHostRecord, "no host record");
|
|
|
|
nsDNSRecord *rec = new nsDNSRecord(syncReq.mHostRecord);
|
|
|
|
if (!rec)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else
|
|
|
|
NS_ADDREF(*result = rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_ExitMonitor(mon);
|
|
|
|
PR_DestroyMonitor(mon);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::GetMyHostName(nsACString &result)
|
|
|
|
{
|
|
|
|
char name[100];
|
|
|
|
if (PR_GetSystemInfo(PR_SI_HOSTNAME, name, sizeof(name)) == PR_SUCCESS) {
|
|
|
|
result = name;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
nsDNSService::Observe(nsISupports *subject, const char *topic, const char16_t *data)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// we are only getting called if a preference has changed.
|
2013-08-31 09:06:14 -07:00
|
|
|
NS_ASSERTION(strcmp(topic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0 ||
|
|
|
|
strcmp(topic, "last-pb-context-exited") == 0,
|
2007-03-22 10:30:00 -07:00
|
|
|
"unexpected observe call");
|
|
|
|
|
|
|
|
//
|
|
|
|
// Shutdown and this function are both only called on the UI thread, so we don't
|
|
|
|
// have to worry about mResolver being cleared out from under us.
|
|
|
|
//
|
|
|
|
// NOTE Shutting down and reinitializing the service like this is obviously
|
|
|
|
// suboptimal if Observe gets called several times in a row, but we don't
|
|
|
|
// expect that to be the case.
|
|
|
|
//
|
|
|
|
|
|
|
|
if (mResolver) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
2010-11-24 21:20:11 -08:00
|
|
|
Init();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t
|
|
|
|
nsDNSService::GetAFForLookup(const nsACString &host, uint32_t flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-07-04 02:47:09 -07:00
|
|
|
if (mDisableIPv6 || (flags & RESOLVE_DISABLE_IPV6))
|
2007-03-22 10:30:00 -07:00
|
|
|
return PR_AF_INET;
|
|
|
|
|
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
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t af = PR_AF_UNSPEC;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mIPv4OnlyDomains.IsEmpty()) {
|
|
|
|
const char *domain, *domainEnd, *end;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t hostLen, domainLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// see if host is in one of the IPv4-only domains
|
|
|
|
domain = mIPv4OnlyDomains.BeginReading();
|
|
|
|
domainEnd = mIPv4OnlyDomains.EndReading();
|
|
|
|
|
|
|
|
nsACString::const_iterator hostStart;
|
|
|
|
host.BeginReading(hostStart);
|
|
|
|
hostLen = host.Length();
|
|
|
|
|
|
|
|
do {
|
|
|
|
// skip any whitespace
|
|
|
|
while (*domain == ' ' || *domain == '\t')
|
|
|
|
++domain;
|
|
|
|
|
|
|
|
// find end of this domain in the string
|
|
|
|
end = strchr(domain, ',');
|
|
|
|
if (!end)
|
|
|
|
end = domainEnd;
|
|
|
|
|
|
|
|
// to see if the hostname is in the domain, check if the domain
|
|
|
|
// matches the end of the hostname.
|
|
|
|
domainLen = end - domain;
|
|
|
|
if (domainLen && hostLen >= domainLen) {
|
|
|
|
const char *hostTail = hostStart.get() + hostLen - domainLen;
|
|
|
|
if (PL_strncasecmp(domain, hostTail, domainLen) == 0) {
|
|
|
|
// now, make sure either that the hostname is a direct match or
|
|
|
|
// that the hostname begins with a dot.
|
|
|
|
if (hostLen == domainLen ||
|
|
|
|
*hostTail == '.' || *(hostTail - 1) == '.') {
|
|
|
|
af = PR_AF_INET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
domain = end + 1;
|
|
|
|
} while (*end);
|
|
|
|
}
|
|
|
|
|
2013-01-29 14:49:35 -08:00
|
|
|
if ((af != PR_AF_INET) && (flags & RESOLVE_DISABLE_IPV4))
|
|
|
|
af = PR_AF_INET6;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return af;
|
|
|
|
}
|
2012-12-10 06:13:55 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::GetDNSCacheEntries(nsTArray<mozilla::net::DNSCacheEntries> *args)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mResolver, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
mResolver->GetDNSCacheEntries(args);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-24 15:24:12 -08:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
SizeOfLocalDomainsEntryExcludingThis(nsCStringHashKey* entry,
|
|
|
|
MallocSizeOf mallocSizeOf, void*)
|
|
|
|
{
|
|
|
|
return entry->GetKey().SizeOfExcludingThisMustBeUnshared(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2013-11-06 21:35:30 -08:00
|
|
|
nsDNSService::SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
2013-11-24 15:24:12 -08:00
|
|
|
{
|
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
|
|
|
// - mIDN
|
|
|
|
// - mLock
|
|
|
|
|
|
|
|
size_t n = mallocSizeOf(this);
|
|
|
|
n += mResolver->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
n += mIPv4OnlyDomains.SizeOfExcludingThisMustBeUnshared(mallocSizeOf);
|
|
|
|
n += mLocalDomains.SizeOfExcludingThis(SizeOfLocalDomainsEntryExcludingThis,
|
|
|
|
mallocSizeOf);
|
|
|
|
return n;
|
|
|
|
}
|
2013-12-07 22:09:10 -08:00
|
|
|
|
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(DNSServiceMallocSizeOf)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData)
|
|
|
|
{
|
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/network/dns-service", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(DNSServiceMallocSizeOf),
|
|
|
|
"Memory used for the DNS service.");
|
|
|
|
}
|
|
|
|
|