2004-09-01 19:55:58 +00:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
#include "nsDNSService2.h"
|
2005-04-06 01:33:28 +00:00
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsIDNSListener.h"
|
|
|
|
#include "nsICancelable.h"
|
2003-09-11 20:32:33 +00:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2011-12-30 17:53:44 +00:00
|
|
|
#include "nsProxyRelease.h"
|
2003-09-11 20:32:33 +00:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsNetCID.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2008-11-07 23:00:26 +00:00
|
|
|
#include "nsDNSPrefetch.h"
|
2011-08-19 15:47:43 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2010-06-17 18:33:42 +00:00
|
|
|
#include "nsIProtocolProxyService.h"
|
2003-09-11 20:32:33 +00:00
|
|
|
#include "prsystem.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "prmon.h"
|
|
|
|
#include "prio.h"
|
2004-03-09 20:31:54 +00:00
|
|
|
#include "plstr.h"
|
2010-11-25 05:20:11 +00:00
|
|
|
#include "nsIOService.h"
|
2012-05-11 19:59:19 +00:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2012-06-06 03:18:25 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-05-19 23:22:19 +00:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
using namespace mozilla;
|
2012-12-23 21:08:43 +00:00
|
|
|
using namespace mozilla::net;
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
static const char kPrefDnsCacheEntries[] = "network.dnsCacheEntries";
|
|
|
|
static const char kPrefDnsCacheExpiration[] = "network.dnsCacheExpiration";
|
2011-12-17 02:24:12 +00:00
|
|
|
static const char kPrefDnsCacheGrace[] = "network.dnsCacheExpirationGracePeriod";
|
2003-09-11 20:32:33 +00:00
|
|
|
static const char kPrefEnableIDN[] = "network.enableIDN";
|
2004-03-09 20:31:54 +00:00
|
|
|
static const char kPrefIPv4OnlyDomains[] = "network.dns.ipv4OnlyDomains";
|
|
|
|
static const char kPrefDisableIPv6[] = "network.dns.disableIPv6";
|
2008-11-07 23:00:26 +00:00
|
|
|
static const char kPrefDisablePrefetch[] = "network.dns.disablePrefetch";
|
2012-05-11 19:59:19 +00:00
|
|
|
static const char kPrefDnsLocalDomains[] = "network.dns.localDomains";
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsDNSRecord : public nsIDNSRecord
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDNSRECORD
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
nsDNSRecord(nsHostRecord *hostRecord)
|
|
|
|
: mHostRecord(hostRecord)
|
2012-07-30 14:20:58 +00:00
|
|
|
, mIter(nullptr)
|
2007-12-11 21:45:42 +00:00
|
|
|
, mIterGenCnt(-1)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mDone(false) {}
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsDNSRecord() {}
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
nsRefPtr<nsHostRecord> mHostRecord;
|
2012-12-23 21:08:43 +00:00
|
|
|
NetAddrElement *mIter;
|
2007-12-11 21:45:42 +00:00
|
|
|
int mIterGenCnt; // the generation count of
|
|
|
|
// mHostRecord->addr_info when we
|
|
|
|
// start iterating
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDone;
|
2003-09-11 20:32:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDNSRecord, nsIDNSRecord)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::GetCanonicalName(nsACString &result)
|
|
|
|
{
|
2004-09-01 19:55:58 +00:00
|
|
|
// this method should only be called if we have a CNAME
|
|
|
|
NS_ENSURE_TRUE(mHostRecord->flags & nsHostResolver::RES_CANON_NAME,
|
|
|
|
NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
// 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-04-01 04:29:02 +00:00
|
|
|
{
|
2011-07-21 13:18:01 +00: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-04-01 04:29:02 +00:00
|
|
|
if (mHostRecord->addr_info)
|
2012-12-23 21:08:43 +00:00
|
|
|
cname = 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-04-01 04:29:02 +00:00
|
|
|
else
|
|
|
|
cname = mHostRecord->host;
|
|
|
|
result.Assign(cname);
|
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-23 21:08:43 +00:00
|
|
|
nsDNSRecord::GetNextAddr(uint16_t port, NetAddr *addr)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
2012-12-23 21:08:43 +00:00
|
|
|
if (mDone) {
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-12-23 21:08:43 +00:00
|
|
|
}
|
2011-07-21 13:18:01 +00:00
|
|
|
|
2012-12-14 13:00:19 +00:00
|
|
|
mHostRecord->addr_info_lock.Lock();
|
|
|
|
if (mHostRecord->addr_info) {
|
2012-12-23 21:08:43 +00:00
|
|
|
if (mIterGenCnt != mHostRecord->addr_info_gencnt) {
|
|
|
|
// mHostRecord->addr_info has changed, restart the iteration.
|
2012-07-30 14:20:58 +00:00
|
|
|
mIter = nullptr;
|
2007-12-11 21:45:42 +00:00
|
|
|
mIterGenCnt = mHostRecord->addr_info_gencnt;
|
2011-07-21 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-23 21:08:43 +00:00
|
|
|
bool startedFresh = !mIter;
|
|
|
|
|
2011-07-21 13:18:01 +00:00
|
|
|
do {
|
2012-12-23 21:08:43 +00:00
|
|
|
if (!mIter) {
|
|
|
|
mIter = mHostRecord->addr_info->mAddresses.getFirst();
|
|
|
|
} else {
|
|
|
|
mIter = mIter->getNext();
|
|
|
|
}
|
2011-07-21 13:18:01 +00:00
|
|
|
}
|
2012-12-23 21:08:43 +00:00
|
|
|
while (mIter && mHostRecord->Blacklisted(&mIter->mAddress));
|
2011-07-21 13:18:01 +00:00
|
|
|
|
2012-12-23 21:08:43 +00:00
|
|
|
if (!mIter && startedFresh) {
|
|
|
|
// If everything was blacklisted we want to reset the blacklist (and
|
2011-07-21 13:18:01 +00:00
|
|
|
// likely relearn it) and return the first address. That is better
|
2012-12-23 21:08:43 +00:00
|
|
|
// than nothing.
|
2011-07-21 13:18:01 +00:00
|
|
|
mHostRecord->ResetBlacklist();
|
2012-12-23 21:08:43 +00:00
|
|
|
mIter = mHostRecord->addr_info->mAddresses.getFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIter) {
|
|
|
|
memcpy(addr, &mIter->mAddress, sizeof(NetAddr));
|
2007-12-11 21:45:42 +00:00
|
|
|
}
|
2012-12-23 21:08:43 +00:00
|
|
|
|
2011-07-21 13:18:01 +00:00
|
|
|
mHostRecord->addr_info_lock.Unlock();
|
2012-12-23 21:08:43 +00:00
|
|
|
|
2007-12-11 21:45:42 +00:00
|
|
|
if (!mIter) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mDone = true;
|
2003-10-07 05:11:41 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2007-12-11 21:45:42 +00:00
|
|
|
}
|
2003-10-07 05:11:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-12-14 13:00:19 +00:00
|
|
|
mHostRecord->addr_info_lock.Unlock();
|
2012-12-23 21:08:43 +00:00
|
|
|
|
2008-04-09 00:42:52 +00: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 21:08:43 +00: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 19:47:07 +00:00
|
|
|
}
|
2012-12-23 21:08:43 +00:00
|
|
|
else if (addr->raw.family == AF_INET6) {
|
|
|
|
addr->inet6.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::GetNextAddrAsString(nsACString &result)
|
|
|
|
{
|
2012-12-23 21:08:43 +00:00
|
|
|
NetAddr addr;
|
2003-09-11 20:32:33 +00:00
|
|
|
nsresult rv = GetNextAddr(0, &addr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-12-23 21:08:43 +00:00
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(&addr, buf, sizeof(buf))) {
|
2003-09-11 20:32:33 +00:00
|
|
|
result.Assign(buf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-23 21:08:43 +00:00
|
|
|
NS_ERROR("NetAddrToString failed unexpectedly");
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_ERROR_FAILURE; // conversion failed for some reason
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsDNSRecord::HasMore(bool *result)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
2012-12-23 21:08:43 +00:00
|
|
|
if (mDone) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*result = false;
|
2012-12-23 21:08:43 +00:00
|
|
|
return NS_OK;
|
2003-10-07 05:11:41 +00:00
|
|
|
}
|
2012-12-23 21:08:43 +00:00
|
|
|
|
|
|
|
NetAddrElement *iterCopy = mIter;
|
|
|
|
|
|
|
|
NetAddr addr;
|
|
|
|
*result = NS_SUCCEEDED(GetNextAddr(0, &addr));
|
|
|
|
|
|
|
|
mIter = iterCopy;
|
|
|
|
mDone = false;
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSRecord::Rewind()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
mIter = nullptr;
|
2007-12-11 21:45:42 +00:00
|
|
|
mIterGenCnt = -1;
|
2011-10-17 14:59:28 +00:00
|
|
|
mDone = false;
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-21 13:18:01 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDNSRecord::ReportUnusable(uint16_t aPort)
|
2011-07-21 13:18:01 +00:00
|
|
|
{
|
|
|
|
// right now we don't use the port in the blacklist
|
|
|
|
|
2012-12-23 21:08:43 +00:00
|
|
|
MutexAutoLock lock(mHostRecord->addr_info_lock);
|
2011-07-21 13:18:01 +00: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 &&
|
|
|
|
mIterGenCnt == mHostRecord->addr_info_gencnt) {
|
2012-12-23 21:08:43 +00:00
|
|
|
mHostRecord->ReportUnusable(&mIter->mAddress);
|
2011-07-21 13:18:01 +00:00
|
|
|
}
|
2012-12-23 21:08:43 +00:00
|
|
|
|
2011-07-21 13:18:01 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-06 03:18:25 +00:00
|
|
|
class nsDNSAsyncRequest MOZ_FINAL : public nsResolveHostCallback
|
|
|
|
, public nsICancelable
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2005-04-06 01:33:28 +00:00
|
|
|
NS_DECL_NSICANCELABLE
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
nsDNSAsyncRequest(nsHostResolver *res,
|
|
|
|
const nsACString &host,
|
2004-09-01 19:55:58 +00:00
|
|
|
nsIDNSListener *listener,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t flags,
|
|
|
|
uint16_t af)
|
2003-09-11 20:32:33 +00:00
|
|
|
: mResolver(res)
|
|
|
|
, mHost(host)
|
2004-09-01 19:55:58 +00:00
|
|
|
, mListener(listener)
|
|
|
|
, mFlags(flags)
|
|
|
|
, mAF(af) {}
|
|
|
|
~nsDNSAsyncRequest() {}
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
void OnLookupComplete(nsHostResolver *, nsHostRecord *, nsresult);
|
2012-01-20 23:14:46 +00: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);
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
nsRefPtr<nsHostResolver> mResolver;
|
|
|
|
nsCString mHost; // hostname we're resolving
|
|
|
|
nsCOMPtr<nsIDNSListener> mListener;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t mFlags;
|
|
|
|
uint16_t mAF;
|
2003-09-11 20:32:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDNSAsyncRequest::OnLookupComplete(nsHostResolver *resolver,
|
2003-10-07 05:11:41 +00:00
|
|
|
nsHostRecord *hostRecord,
|
|
|
|
nsresult status)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
2003-10-07 05:11:41 +00:00
|
|
|
// 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);
|
2003-09-11 20:32:33 +00:00
|
|
|
if (!rec)
|
|
|
|
status = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListener->OnLookupComplete(this, rec, status);
|
2012-07-30 14:20:58 +00:00
|
|
|
mListener = nullptr;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
// release the reference to ourselves that was added before we were
|
|
|
|
// handed off to the host resolver.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
|
2012-01-20 23:14:46 +00:00
|
|
|
bool
|
|
|
|
nsDNSAsyncRequest::EqualsAsyncListener(nsIDNSListener *aListener)
|
|
|
|
{
|
|
|
|
return (aListener == mListener);
|
|
|
|
}
|
|
|
|
|
2005-04-06 01:33:28 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDNSAsyncRequest, nsICancelable)
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-04-06 01:33:28 +00:00
|
|
|
nsDNSAsyncRequest::Cancel(nsresult reason)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
2005-04-06 01:33:28 +00:00
|
|
|
NS_ENSURE_ARG(NS_FAILED(reason));
|
|
|
|
mResolver->DetachCallback(mHost.get(), mFlags, mAF, this, reason);
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
class nsDNSSyncRequest : public nsResolveHostCallback
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDNSSyncRequest(PRMonitor *mon)
|
2011-10-17 14:59:28 +00:00
|
|
|
: mDone(false)
|
2003-09-11 20:32:33 +00:00
|
|
|
, mStatus(NS_OK)
|
|
|
|
, mMonitor(mon) {}
|
|
|
|
virtual ~nsDNSSyncRequest() {}
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
void OnLookupComplete(nsHostResolver *, nsHostRecord *, nsresult);
|
2012-01-20 23:14:46 +00:00
|
|
|
bool EqualsAsyncListener(nsIDNSListener *aListener);
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDone;
|
2003-10-07 05:11:41 +00:00
|
|
|
nsresult mStatus;
|
|
|
|
nsRefPtr<nsHostRecord> mHostRecord;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
private:
|
2003-10-07 05:11:41 +00:00
|
|
|
PRMonitor *mMonitor;
|
2003-09-11 20:32:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDNSSyncRequest::OnLookupComplete(nsHostResolver *resolver,
|
2003-10-07 05:11:41 +00:00
|
|
|
nsHostRecord *hostRecord,
|
|
|
|
nsresult status)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
// store results, and wake up nsDNSService::Resolve to process results.
|
|
|
|
PR_EnterMonitor(mMonitor);
|
2011-10-17 14:59:28 +00:00
|
|
|
mDone = true;
|
2003-09-11 20:32:33 +00:00
|
|
|
mStatus = status;
|
2003-10-07 05:11:41 +00:00
|
|
|
mHostRecord = hostRecord;
|
2003-09-11 20:32:33 +00:00
|
|
|
PR_Notify(mMonitor);
|
|
|
|
PR_ExitMonitor(mMonitor);
|
|
|
|
}
|
|
|
|
|
2012-01-20 23:14:46 +00:00
|
|
|
bool
|
|
|
|
nsDNSSyncRequest::EqualsAsyncListener(nsIDNSListener *aListener)
|
|
|
|
{
|
|
|
|
// Sync request: no listener to compare
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsDNSService::nsDNSService()
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
: mLock("nsDNSServer.mLock")
|
2011-10-17 14:59:28 +00:00
|
|
|
, mFirstTime(true)
|
2012-09-17 23:45:10 +00:00
|
|
|
, mOffline(false)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDNSService::~nsDNSService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-04-06 01:33:28 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsDNSService, nsIDNSService, nsPIDNSService,
|
|
|
|
nsIObserver)
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::Init()
|
|
|
|
{
|
2012-09-17 23:45:10 +00:00
|
|
|
if (mResolver)
|
|
|
|
return NS_OK;
|
2003-09-11 20:32:33 +00:00
|
|
|
NS_ENSURE_TRUE(!mResolver, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
// prefs
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t maxCacheEntries = 400;
|
|
|
|
uint32_t maxCacheLifetime = 2; // minutes
|
|
|
|
uint32_t lifetimeGracePeriod = 1;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool enableIDN = true;
|
|
|
|
bool disableIPv6 = false;
|
|
|
|
bool disablePrefetch = false;
|
2010-06-17 18:33:42 +00:00
|
|
|
int proxyType = nsIProtocolProxyService::PROXYCONFIG_DIRECT;
|
2008-11-07 23:00:26 +00:00
|
|
|
|
2004-03-09 20:31:54 +00:00
|
|
|
nsAdoptingCString ipv4OnlyDomains;
|
2012-05-11 19:59:19 +00:00
|
|
|
nsAdoptingCString localDomains;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
// read prefs
|
2012-01-17 01:48:29 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2003-09-11 20:32:33 +00:00
|
|
|
if (prefs) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t val;
|
2003-09-11 20:32:33 +00:00
|
|
|
if (NS_SUCCEEDED(prefs->GetIntPref(kPrefDnsCacheEntries, &val)))
|
2012-08-22 15:56:38 +00:00
|
|
|
maxCacheEntries = (uint32_t) val;
|
2003-09-11 20:32:33 +00:00
|
|
|
if (NS_SUCCEEDED(prefs->GetIntPref(kPrefDnsCacheExpiration, &val)))
|
2004-03-09 20:31:54 +00:00
|
|
|
maxCacheLifetime = val / 60; // convert from seconds to minutes
|
2011-12-17 02:24:12 +00:00
|
|
|
if (NS_SUCCEEDED(prefs->GetIntPref(kPrefDnsCacheGrace, &val)))
|
|
|
|
lifetimeGracePeriod = val / 60; // convert from seconds to minutes
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2004-03-09 20:31:54 +00:00
|
|
|
// ASSUMPTION: pref branch does not modify out params on failure
|
|
|
|
prefs->GetBoolPref(kPrefEnableIDN, &enableIDN);
|
|
|
|
prefs->GetBoolPref(kPrefDisableIPv6, &disableIPv6);
|
|
|
|
prefs->GetCharPref(kPrefIPv4OnlyDomains, getter_Copies(ipv4OnlyDomains));
|
2012-05-11 19:59:19 +00:00
|
|
|
prefs->GetCharPref(kPrefDnsLocalDomains, getter_Copies(localDomains));
|
2008-11-07 23:00:26 +00:00
|
|
|
prefs->GetBoolPref(kPrefDisablePrefetch, &disablePrefetch);
|
2009-05-06 21:26:33 +00:00
|
|
|
|
|
|
|
// If a manual proxy is in use, disable prefetch implicitly
|
|
|
|
prefs->GetIntPref("network.proxy.type", &proxyType);
|
2004-03-09 20:31:54 +00:00
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
if (mFirstTime) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mFirstTime = false;
|
2004-03-09 20:31:54 +00:00
|
|
|
|
2012-05-11 19:59:19 +00:00
|
|
|
mLocalDomains.Init();
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
// register as prefs observer
|
2006-05-10 17:30:15 +00:00
|
|
|
if (prefs) {
|
2011-10-17 14:59:28 +00:00
|
|
|
prefs->AddObserver(kPrefDnsCacheEntries, this, false);
|
|
|
|
prefs->AddObserver(kPrefDnsCacheExpiration, this, false);
|
2011-12-17 02:24:12 +00:00
|
|
|
prefs->AddObserver(kPrefDnsCacheGrace, this, false);
|
2011-10-17 14:59:28 +00:00
|
|
|
prefs->AddObserver(kPrefEnableIDN, this, false);
|
|
|
|
prefs->AddObserver(kPrefIPv4OnlyDomains, this, false);
|
2012-05-11 19:59:19 +00:00
|
|
|
prefs->AddObserver(kPrefDnsLocalDomains, this, false);
|
2011-10-17 14:59:28 +00:00
|
|
|
prefs->AddObserver(kPrefDisableIPv6, this, false);
|
|
|
|
prefs->AddObserver(kPrefDisablePrefetch, this, false);
|
2009-05-06 21:26:33 +00: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 14:59:28 +00:00
|
|
|
prefs->AddObserver("network.proxy.type", this, false);
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2004-03-09 20:31:54 +00:00
|
|
|
// we have to null out mIDN since we might be getting re-initialized
|
|
|
|
// as a result of a pref change.
|
|
|
|
nsCOMPtr<nsIIDNService> idn;
|
|
|
|
if (enableIDN)
|
|
|
|
idn = do_GetService(NS_IDNSERVICE_CONTRACTID);
|
|
|
|
|
2010-11-25 05:20:11 +00: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;
|
|
|
|
|
2004-03-09 20:31:54 +00:00
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsresult rv = nsHostResolver::Create(maxCacheEntries,
|
|
|
|
maxCacheLifetime,
|
2011-12-17 02:24:12 +00:00
|
|
|
lifetimeGracePeriod,
|
2004-03-09 20:31:54 +00: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-04-01 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2004-03-09 20:31:54 +00:00
|
|
|
mResolver = res;
|
|
|
|
mIDN = idn;
|
|
|
|
mIPv4OnlyDomains = ipv4OnlyDomains; // exchanges buffer ownership
|
|
|
|
mDisableIPv6 = disableIPv6;
|
2009-05-06 21:26:33 +00:00
|
|
|
|
|
|
|
// Disable prefetching either by explicit preference or if a manual proxy is configured
|
2010-06-17 18:33:42 +00:00
|
|
|
mDisablePrefetch = disablePrefetch || (proxyType == nsIProtocolProxyService::PROXYCONFIG_MANUAL);
|
2012-05-11 19:59:19 +00:00
|
|
|
|
|
|
|
mLocalDomains.Clear();
|
|
|
|
if (localDomains) {
|
|
|
|
nsAdoptingString domains;
|
|
|
|
domains.AssignASCII(nsDependentCString(localDomains).get());
|
|
|
|
nsCharSeparatedTokenizer tokenizer(domains, ',',
|
|
|
|
nsCharSeparatedTokenizerTemplate<>::SEPARATOR_OPTIONAL);
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsSubstring& domain = tokenizer.nextToken();
|
|
|
|
mLocalDomains.PutEntry(nsDependentCString(NS_ConvertUTF16toUTF8(domain).get()));
|
|
|
|
}
|
|
|
|
}
|
2004-03-09 20:31:54 +00:00
|
|
|
}
|
|
|
|
return rv;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::Shutdown()
|
|
|
|
{
|
|
|
|
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-04-01 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-09-11 20:32:33 +00:00
|
|
|
res = mResolver;
|
2012-07-30 14:20:58 +00:00
|
|
|
mResolver = nullptr;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
if (res)
|
|
|
|
res->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-17 23:45:10 +00: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 20:27:46 +00: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 15:47:43 +00:00
|
|
|
namespace {
|
|
|
|
|
2012-06-06 03:18:25 +00:00
|
|
|
class DNSListenerProxy MOZ_FINAL : public nsIDNSListener
|
2011-08-19 15:47:43 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DNSListenerProxy(nsIDNSListener* aListener, nsIEventTarget* aTargetThread)
|
|
|
|
: mListener(aListener)
|
|
|
|
, mTargetThread(aTargetThread)
|
|
|
|
{ }
|
|
|
|
|
2011-12-30 17:53:44 +00:00
|
|
|
~DNSListenerProxy()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> mainThread(do_GetMainThread());
|
|
|
|
NS_ProxyRelease(mainThread, mListener);
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:47:43 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDNSLISTENER
|
|
|
|
|
|
|
|
class OnLookupCompleteRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OnLookupCompleteRunnable(nsIDNSListener* aListener,
|
|
|
|
nsICancelable* aRequest,
|
|
|
|
nsIDNSRecord* aRecord,
|
|
|
|
nsresult aStatus)
|
|
|
|
: mListener(aListener)
|
|
|
|
, mRequest(aRequest)
|
|
|
|
, mRecord(aRecord)
|
|
|
|
, mStatus(aStatus)
|
|
|
|
{ }
|
|
|
|
|
2011-12-30 17:53:44 +00:00
|
|
|
~OnLookupCompleteRunnable()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> mainThread(do_GetMainThread());
|
|
|
|
NS_ProxyRelease(mainThread, mListener);
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:47:43 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDNSListener> mListener;
|
|
|
|
nsCOMPtr<nsICancelable> mRequest;
|
|
|
|
nsCOMPtr<nsIDNSRecord> mRecord;
|
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDNSListener> mListener;
|
|
|
|
nsCOMPtr<nsIEventTarget> mTargetThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(DNSListenerProxy, nsIDNSListener)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DNSListenerProxy::OnLookupComplete(nsICancelable* aRequest,
|
|
|
|
nsIDNSRecord* aRecord,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
nsRefPtr<OnLookupCompleteRunnable> r =
|
|
|
|
new OnLookupCompleteRunnable(mListener, aRequest, aRecord, aStatus);
|
|
|
|
return mTargetThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DNSListenerProxy::OnLookupCompleteRunnable::Run()
|
|
|
|
{
|
|
|
|
mListener->OnLookupComplete(mRequest, mRecord, mStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
NS_IMETHODIMP
|
2006-05-10 17:30:15 +00:00
|
|
|
nsDNSService::AsyncResolve(const nsACString &hostname,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags,
|
2006-05-10 17:30:15 +00:00
|
|
|
nsIDNSListener *listener,
|
|
|
|
nsIEventTarget *target,
|
|
|
|
nsICancelable **result)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
// grab reference to global host resolver and IDN service. beware
|
|
|
|
// simultaneous shutdown!!
|
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsCOMPtr<nsIIDNService> idn;
|
2012-05-11 19:59:19 +00:00
|
|
|
bool localDomain = false;
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2008-11-07 23:00:26 +00:00
|
|
|
|
2009-05-06 21:26:33 +00:00
|
|
|
if (mDisablePrefetch && (flags & RESOLVE_SPECULATE))
|
2008-11-07 23:00:26 +00:00
|
|
|
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
res = mResolver;
|
|
|
|
idn = mIDN;
|
2012-05-11 19:59:19 +00:00
|
|
|
localDomain = mLocalDomains.GetEntry(hostname);
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
2010-02-24 19:59:26 +00:00
|
|
|
if (!res)
|
|
|
|
return NS_ERROR_OFFLINE;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2012-09-17 23:45:10 +00:00
|
|
|
if (mOffline)
|
|
|
|
flags |= RESOLVE_OFFLINE;
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
const nsACString *hostPtr = &hostname;
|
|
|
|
|
2012-05-11 19:59:19 +00:00
|
|
|
if (localDomain) {
|
|
|
|
hostPtr = &(NS_LITERAL_CSTRING("localhost"));
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
nsresult rv;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString hostACE;
|
2012-05-11 19:59:19 +00:00
|
|
|
if (idn && !IsASCII(*hostPtr)) {
|
|
|
|
if (NS_SUCCEEDED(idn->ConvertUTF8toACE(*hostPtr, hostACE)))
|
2003-10-07 05:11:41 +00:00
|
|
|
hostPtr = &hostACE;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
if (target) {
|
2011-08-19 15:47:43 +00:00
|
|
|
listener = new DNSListenerProxy(listener, target);
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t af = GetAFForLookup(*hostPtr, flags);
|
2004-09-01 19:55:58 +00:00
|
|
|
|
|
|
|
nsDNSAsyncRequest *req =
|
|
|
|
new nsDNSAsyncRequest(res, *hostPtr, listener, flags, af);
|
2003-09-11 20:32:33 +00:00
|
|
|
if (!req)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*result = req);
|
|
|
|
|
|
|
|
// addref for resolver; will be released when OnLookupComplete is called.
|
|
|
|
NS_ADDREF(req);
|
2004-09-01 19:55:58 +00:00
|
|
|
rv = res->ResolveHost(req->mHost.get(), flags, af, req);
|
2003-09-11 20:32:33 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(req);
|
|
|
|
NS_RELEASE(*result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-20 23:14:46 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::CancelAsyncResolve(const nsACString &aHostname,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFlags,
|
2012-01-20 23:14:46 +00: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-02 02:35:17 +00:00
|
|
|
nsAutoCString hostACE;
|
2012-01-20 23:14:46 +00:00
|
|
|
if (idn && !IsASCII(aHostname)) {
|
|
|
|
if (NS_SUCCEEDED(idn->ConvertUTF8toACE(aHostname, hostACE)))
|
|
|
|
hostname = hostACE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t af = GetAFForLookup(hostname, aFlags);
|
2012-01-20 23:14:46 +00:00
|
|
|
|
|
|
|
res->CancelAsyncRequest(hostname.get(), aFlags, af, aListener, aReason);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::Resolve(const nsACString &hostname,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags,
|
2003-09-11 20:32:33 +00:00
|
|
|
nsIDNSRecord **result)
|
|
|
|
{
|
2012-09-29 04:50:59 +00:00
|
|
|
NS_WARNING("Do not use synchronous DNS resolution! This API may be removed soon.");
|
|
|
|
|
|
|
|
// We will not allow this to be called on the main thread. This is transitional
|
|
|
|
// and a bit of a test for removing the synchronous API entirely.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
NS_ERROR("Synchronous DNS resolve failing - not allowed on the main thread!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
// grab reference to global host resolver and IDN service. beware
|
|
|
|
// simultaneous shutdown!!
|
|
|
|
nsRefPtr<nsHostResolver> res;
|
|
|
|
nsCOMPtr<nsIIDNService> idn;
|
2012-05-11 19:59:19 +00:00
|
|
|
bool localDomain = false;
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-09-11 20:32:33 +00:00
|
|
|
res = mResolver;
|
|
|
|
idn = mIDN;
|
2012-05-11 19:59:19 +00:00
|
|
|
localDomain = mLocalDomains.GetEntry(hostname);
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(res, NS_ERROR_OFFLINE);
|
|
|
|
|
2012-09-17 23:45:10 +00:00
|
|
|
if (mOffline)
|
|
|
|
flags |= RESOLVE_OFFLINE;
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
const nsACString *hostPtr = &hostname;
|
|
|
|
|
2012-05-11 19:59:19 +00:00
|
|
|
if (localDomain) {
|
|
|
|
hostPtr = &(NS_LITERAL_CSTRING("localhost"));
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
nsresult rv;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString hostACE;
|
2012-05-11 19:59:19 +00:00
|
|
|
if (idn && !IsASCII(*hostPtr)) {
|
|
|
|
if (NS_SUCCEEDED(idn->ConvertUTF8toACE(*hostPtr, hostACE)))
|
2003-10-07 05:11:41 +00:00
|
|
|
hostPtr = &hostACE;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
2005-11-25 21:57:13 +00:00
|
|
|
// on the same thread. so, our mutex needs to be re-entrant. in other words,
|
2003-09-11 20:32:33 +00:00
|
|
|
// 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 15:56:38 +00:00
|
|
|
uint16_t af = GetAFForLookup(*hostPtr, flags);
|
2004-03-09 20:31:54 +00:00
|
|
|
|
2004-09-01 19:55:58 +00:00
|
|
|
rv = res->ResolveHost(PromiseFlatCString(*hostPtr).get(), flags, af, &syncReq);
|
2003-09-11 20:32:33 +00:00
|
|
|
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 {
|
2003-10-07 05:11:41 +00:00
|
|
|
NS_ASSERTION(syncReq.mHostRecord, "no host record");
|
|
|
|
nsDNSRecord *rec = new nsDNSRecord(syncReq.mHostRecord);
|
2003-09-11 20:32:33 +00:00
|
|
|
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
|
|
|
|
nsDNSService::Observe(nsISupports *subject, const char *topic, const PRUnichar *data)
|
|
|
|
{
|
|
|
|
// we are only getting called if a preference has changed.
|
|
|
|
NS_ASSERTION(strcmp(topic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
|
|
|
|
"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-25 05:20:11 +00:00
|
|
|
Init();
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-03-09 20:31:54 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t
|
|
|
|
nsDNSService::GetAFForLookup(const nsACString &host, uint32_t flags)
|
2004-03-09 20:31:54 +00:00
|
|
|
{
|
2011-07-04 09:47:09 +00:00
|
|
|
if (mDisableIPv6 || (flags & RESOLVE_DISABLE_IPV6))
|
2004-03-09 20:31:54 +00: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-04-01 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2004-03-09 20:31:54 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t af = PR_AF_UNSPEC;
|
2004-03-09 20:31:54 +00:00
|
|
|
|
|
|
|
if (!mIPv4OnlyDomains.IsEmpty()) {
|
|
|
|
const char *domain, *domainEnd, *end;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t hostLen, domainLen;
|
2004-03-09 20:31:54 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return af;
|
|
|
|
}
|
2012-12-10 14:13:55 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSService::GetDNSCacheEntries(nsTArray<mozilla::net::DNSCacheEntries> *args)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mResolver, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
mResolver->GetDNSCacheEntries(args);
|
|
|
|
return NS_OK;
|
|
|
|
}
|