Bug 1659132 - Make AddrInfo immutable r=dragana,necko-reviewers

Also adds constructors for NetAddr to avoid it being uninitialized.

Differential Revision: https://phabricator.services.mozilla.com/D87091
This commit is contained in:
Valentin Gosu 2020-08-24 12:23:06 +00:00
parent 125efe18dd
commit ec0be3efef
17 changed files with 160 additions and 131 deletions

View File

@ -406,8 +406,7 @@ nsresult UDPSocket::InitLocal(const nsAString& aLocalAddress,
UDPSOCKET_LOG(("%s: %s:%u", __FUNCTION__,
NS_ConvertUTF16toUTF8(aLocalAddress).get(), aLocalPort));
mozilla::net::NetAddr addr;
PRNetAddrToNetAddr(&prAddr, &addr);
mozilla::net::NetAddr addr(&prAddr);
rv = sock->InitWithAddress(&addr, principal, mAddressReuse,
/* optionalArgc = */ 1);
}

View File

@ -139,8 +139,7 @@ nsresult UDPSocketParent::BindInternal(const nsCString& aHost,
return NS_ERROR_FAILURE;
}
mozilla::net::NetAddr addr;
PRNetAddrToNetAddr(&prAddr, &addr);
mozilla::net::NetAddr addr(&prAddr);
rv = sock->InitWithAddress(&addr, mPrincipal, aAddressReuse,
/* optional_argc = */ 1);
}
@ -288,9 +287,7 @@ nsresult UDPSocketParent::ConnectInternal(const nsCString& aHost,
return NS_ERROR_FAILURE;
}
mozilla::net::NetAddr addr;
PRNetAddrToNetAddr(&prAddr, &addr);
mozilla::net::NetAddr addr(&prAddr);
rv = mSocket->Connect(&addr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;

View File

@ -361,11 +361,7 @@ bool nsHTTPSOnlyUtils::LoopbackOrLocalException(nsIURI* aURI) {
return false;
}
// The linter wants this struct to get initialized,
// but PRNetAddrToNetAddr will do that.
mozilla::net::NetAddr addr; // NOLINT(cppcoreguidelines-pro-type-member-init)
PRNetAddrToNetAddr(&tempAddr, &addr);
mozilla::net::NetAddr addr(&tempAddr);
// Loopback IPs are always exempt
if (IsLoopBackAddress(&addr)) {
return true;

View File

@ -241,8 +241,7 @@ bool nsMixedContentBlocker::IsPotentiallyTrustworthyLoopbackHost(
}
using namespace mozilla::net;
NetAddr addr;
PRNetAddrToNetAddr(&tempAddr, &addr);
NetAddr addr(&tempAddr);
// Step 4 of
// https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy says

View File

@ -322,8 +322,7 @@ void WebrtcTCPSocket::OpenWithoutHttpProxy(nsIProxyInfo* aSocksProxyInfo) {
return;
}
mozilla::net::NetAddr addr;
PRNetAddrToNetAddr(&prAddr, &addr);
mozilla::net::NetAddr addr(&prAddr);
rv = mTransport->Bind(&addr);
if (NS_WARN_IF(NS_FAILED(rv))) {
CloseWithReason(rv);

View File

@ -738,8 +738,7 @@ nsresult nsIOService::RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan) {
return NS_OK;
}
NetAddr netAddr;
PRNetAddrToNetAddr(&prAddr, &netAddr);
NetAddr netAddr(&prAddr);
if (IsIPAddrLocal(&netAddr)) {
// Redirects to local IP addresses are probably captive portals
RecheckCaptivePortal();
@ -947,8 +946,7 @@ nsIOService::HostnameIsLocalIPAddress(nsIURI* aURI, bool* aResult) {
PRNetAddr addr;
PRStatus result = PR_StringToNetAddr(host.get(), &addr);
if (result == PR_SUCCESS) {
NetAddr netAddr;
PRNetAddrToNetAddr(&addr, &netAddr);
NetAddr netAddr(&addr);
if (IsIPAddrLocal(&netAddr)) {
*aResult = true;
}
@ -975,8 +973,7 @@ nsIOService::HostnameIsSharedIPAddress(nsIURI* aURI, bool* aResult) {
PRNetAddr addr;
PRStatus result = PR_StringToNetAddr(host.get(), &addr);
if (result == PR_SUCCESS) {
NetAddr netAddr;
PRNetAddrToNetAddr(&addr, &netAddr);
NetAddr netAddr(&addr);
if (IsIPAddrShared(&netAddr)) {
*aResult = true;
}

View File

@ -170,7 +170,6 @@ void nsServerSocket::OnSocketReady(PRFileDesc* fd, int16_t outFlags) {
PRFileDesc* clientFD;
PRNetAddr prClientAddr;
NetAddr clientAddr;
// NSPR doesn't tell us the peer address's length (as provided by the
// 'accept' system call), so we can't distinguish between named,
@ -180,13 +179,13 @@ void nsServerSocket::OnSocketReady(PRFileDesc* fd, int16_t outFlags) {
memset(&prClientAddr, 0, sizeof(prClientAddr));
clientFD = PR_Accept(mFD, &prClientAddr, PR_INTERVAL_NO_WAIT);
PRNetAddrToNetAddr(&prClientAddr, &clientAddr);
if (!clientFD) {
NS_WARNING("PR_Accept failed");
mCondition = NS_ERROR_UNEXPECTED;
return;
}
NetAddr clientAddr(&prClientAddr);
// Accept succeeded, create socket transport and notify consumer
CreateClientTransport(clientFD, clientAddr);
}

View File

@ -438,8 +438,7 @@ void nsUDPSocket::OnSocketReady(PRFileDesc* fd, int16_t outFlags) {
return;
}
NetAddr netAddr;
PRNetAddrToNetAddr(&prClientAddr, &netAddr);
NetAddr netAddr(&prClientAddr);
nsCOMPtr<nsIUDPMessage> message =
new UDPMessageProxy(&netAddr, pipeOut, std::move(data));
mListener->OnPacketReceived(this, message);

View File

@ -116,7 +116,7 @@ bool NetAddrToString(const NetAddr* addr, char* buf, uint32_t bufSize) {
return !!inet_ntop_internal(AF_INET6, &nativeAddr, buf, bufSize);
}
#if defined(XP_UNIX)
else if (addr->raw.family == AF_LOCAL) {
if (addr->raw.family == AF_LOCAL) {
if (bufSize < sizeof(addr->local.path)) {
// Many callers don't bother checking our return value, so
// null-terminate just in case.
@ -174,6 +174,8 @@ bool IsIPAddrAny(const NetAddr* addr) {
return false;
}
NetAddr::NetAddr(const PRNetAddr* prAddr) { PRNetAddrToNetAddr(prAddr, this); }
bool IsIPAddrV4(const NetAddr* addr) { return addr->raw.family == AF_INET; }
bool IsIPAddrV4Mapped(const NetAddr* addr) {
@ -301,19 +303,26 @@ AddrInfo::AddrInfo(const nsACString& host, const PRAddrInfo* prAddrInfo,
(!filterNameCollision || tmpAddr.raw.family != PR_AF_INET ||
(tmpAddr.inet.ip != nameCollisionAddr));
if (addIt) {
NetAddr elem;
PRNetAddrToNetAddr(&tmpAddr, &elem);
NetAddr elem(&tmpAddr);
mAddresses.AppendElement(elem);
}
} while (iter);
}
AddrInfo::AddrInfo(const nsACString& host, const nsACString& cname,
unsigned int aTRR)
: mHostName(host), mCanonicalName(cname), mFromTRR(aTRR) {}
unsigned int aTRR, nsTArray<NetAddr>&& addresses)
: mHostName(host),
mCanonicalName(cname),
mFromTRR(aTRR),
mAddresses(std::move(addresses)) {}
AddrInfo::AddrInfo(const nsACString& host, unsigned int aTRR)
: mHostName(host), mCanonicalName(EmptyCString()), mFromTRR(aTRR) {}
AddrInfo::AddrInfo(const nsACString& host, unsigned int aTRR,
nsTArray<NetAddr>&& addresses, uint32_t aTTL)
: ttl(aTTL),
mHostName(host),
mCanonicalName(EmptyCString()),
mFromTRR(aTRR),
mAddresses(std::move(addresses)) {}
// deep copy constructor
AddrInfo::AddrInfo(const AddrInfo* src) {
@ -329,12 +338,6 @@ AddrInfo::AddrInfo(const AddrInfo* src) {
AddrInfo::~AddrInfo() = default;
void AddrInfo::AddAddress(const PRNetAddr* address) {
NetAddr elem;
PRNetAddrToNetAddr(address, &elem);
mAddresses.AppendElement(elem);
}
size_t AddrInfo::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const {
size_t n = mallocSizeOf(this);
n += mHostName.SizeOfExcludingThisIfUnshared(mallocSizeOf);

View File

@ -87,7 +87,7 @@ union NetAddr {
struct {
uint16_t family; /* address family (0x00ff maskable) */
char data[14]; /* raw address data */
} raw;
} raw{};
struct {
uint16_t family; /* address family (AF_INET) */
uint16_t port; /* port number */
@ -115,12 +115,17 @@ union NetAddr {
memcpy(this, &other, sizeof(NetAddr));
return *this;
}
NetAddr() { memset(this, 0, sizeof(NetAddr)); }
explicit NetAddr(const PRNetAddr* prAddr);
};
class AddrInfo {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AddrInfo)
public:
static const uint32_t NO_TTL_DATA = (uint32_t)-1;
// Creates an AddrInfo object.
explicit AddrInfo(const nsACString& host, const PRAddrInfo* prAddrInfo,
bool disableIPv4, bool filterNameCollision,
@ -129,39 +134,66 @@ class AddrInfo {
// Creates a basic AddrInfo object (initialize only the host, cname and TRR
// type).
explicit AddrInfo(const nsACString& host, const nsACString& cname,
unsigned int TRRType);
unsigned int aTRR, nsTArray<NetAddr>&& addresses);
// Creates a basic AddrInfo object (initialize only the host and TRR status).
explicit AddrInfo(const nsACString& host, unsigned int TRRType);
explicit AddrInfo(const nsACString& host, unsigned int aTRR,
nsTArray<NetAddr>&& addresses, uint32_t aTTL = NO_TTL_DATA);
explicit AddrInfo(const AddrInfo* src); // copy
void AddAddress(const PRNetAddr* address);
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
nsCString mHostName;
nsCString mCanonicalName;
static const uint32_t NO_TTL_DATA = (uint32_t)-1;
uint32_t ttl = NO_TTL_DATA;
nsTArray<NetAddr> mAddresses;
unsigned int IsTRR() { return mFromTRR; }
double GetTrrFetchDuration() { return mTrrFetchDuration; }
double GetTrrFetchDurationNetworkOnly() {
return mTrrFetchDurationNetworkOnly;
}
void SetTrrFetchDuration(double aTime) { mTrrFetchDuration = aTime; }
void SetTrrFetchDurationNetworkOnly(double aTime) {
mTrrFetchDurationNetworkOnly = aTime;
const nsTArray<NetAddr>& Addresses() { return mAddresses; }
const nsCString& Hostname() { return mHostName; }
const nsCString& CanonicalHostname() { return mCanonicalName; }
uint32_t TTL() { return ttl; }
class MOZ_STACK_CLASS AddrInfoBuilder {
public:
explicit AddrInfoBuilder(AddrInfo* aInfo) {
mInfo = new AddrInfo(aInfo); // Clone it
}
void SetTrrFetchDurationNetworkOnly(double aTime) {
mInfo->mTrrFetchDurationNetworkOnly = aTime;
}
void SetTrrFetchDuration(double aTime) { mInfo->mTrrFetchDuration = aTime; }
void SetTTL(uint32_t aTTL) { mInfo->ttl = aTTL; }
void SetAddresses(nsTArray<NetAddr>&& addresses) {
mInfo->mAddresses = std::move(addresses);
}
already_AddRefed<AddrInfo> Finish() { return mInfo.forget(); }
private:
RefPtr<AddrInfo> mInfo;
};
AddrInfoBuilder Build() { return AddrInfoBuilder(this); }
private:
~AddrInfo();
uint32_t ttl = NO_TTL_DATA;
nsCString mHostName;
nsCString mCanonicalName;
unsigned int mFromTRR = 0;
double mTrrFetchDuration = 0;
double mTrrFetchDurationNetworkOnly = 0;
nsTArray<NetAddr> mAddresses;
};
// Copies the contents of a PRNetAddr to a NetAddr.

View File

@ -170,7 +170,7 @@ _GetAddrInfo_Portable(const nsACString& aCanonHost, uint16_t aAddressFamily,
RefPtr<AddrInfo> ai(new AddrInfo(aCanonHost, prai, disableIPv4,
filterNameCollision, canonName));
PR_FreeAddrInfo(prai);
if (ai->mAddresses.IsEmpty()) {
if (ai->Addresses().IsEmpty()) {
return NS_ERROR_UNKNOWN_HOST;
}
@ -210,17 +210,19 @@ bool FindAddrOverride(const nsACString& aHost, uint16_t aAddressFamily,
RefPtr<AddrInfo> ai;
if (!cname) {
ai = new AddrInfo(aHost, 0);
} else {
ai = new AddrInfo(aHost, *cname, 0);
}
nsTArray<NetAddr> addresses;
for (const auto& ip : *overrides) {
if (aAddressFamily != AF_UNSPEC && ip.raw.family != aAddressFamily) {
continue;
}
ai->AddAddress(&ip);
NetAddr addr(&ip);
addresses.AppendElement(addr);
}
if (!cname) {
ai = new AddrInfo(aHost, 0, std::move(addresses));
} else {
ai = new AddrInfo(aHost, *cname, 0, std::move(addresses));
}
ai.forget(aAddrInfo);

View File

@ -1262,9 +1262,10 @@ nsresult TRR::ParseSvcParam(unsigned int svcbIndex, uint16_t key,
field.mValue = AsVariant(SvcParamIpv4Hint());
auto& ipv4array = field.mValue.as<SvcParamIpv4Hint>().mValue;
while (length > 0) {
NetAddr addr = {.inet = {.family = AF_INET,
.port = 0,
.ip = ntohl(get32bit(mResponse, svcbIndex))}};
NetAddr addr;
addr.inet.family = AF_INET;
addr.inet.port = 0;
addr.inet.ip = ntohl(get32bit(mResponse, svcbIndex));
ipv4array.AppendElement(addr);
length -= 4;
svcbIndex += 4;
@ -1285,7 +1286,7 @@ nsresult TRR::ParseSvcParam(unsigned int svcbIndex, uint16_t key,
field.mValue = AsVariant(SvcParamIpv6Hint());
auto& ipv6array = field.mValue.as<SvcParamIpv6Hint>().mValue;
while (length > 0) {
NetAddr addr = {{.family = 0, .data = {0}}};
NetAddr addr;
addr.inet6.family = AF_INET6;
addr.inet6.port = 0; // unknown
addr.inet6.flowinfo = 0; // unknown
@ -1311,11 +1312,11 @@ nsresult TRR::ParseSvcParam(unsigned int svcbIndex, uint16_t key,
nsresult TRR::ReturnData(nsIChannel* aChannel) {
if (mType != TRRTYPE_TXT && mType != TRRTYPE_HTTPSSVC) {
// create and populate an AddrInfo instance to pass on
RefPtr<AddrInfo> ai(new AddrInfo(mHost, mType));
DOHaddr* item;
uint32_t ttl = AddrInfo::NO_TTL_DATA;
nsTArray<NetAddr> addresses;
while ((item = static_cast<DOHaddr*>(mDNS.mAddresses.popFirst()))) {
ai->mAddresses.AppendElement(item->mNet);
addresses.AppendElement(item->mNet);
if (item->mTtl < ttl) {
// While the DNS packet might return individual TTLs for each address,
// we can only return one value in the AddrInfo class so pick the
@ -1323,7 +1324,9 @@ nsresult TRR::ReturnData(nsIChannel* aChannel) {
ttl = item->mTtl;
}
}
ai->ttl = ttl;
RefPtr<AddrInfo> ai(new AddrInfo(mHost, mType, nsTArray<NetAddr>(), ttl));
auto builder = ai->Build();
builder.SetAddresses(std::move(addresses));
// Set timings.
nsCOMPtr<nsITimedChannel> timedChan = do_QueryInterface(aChannel);
@ -1331,15 +1334,16 @@ nsresult TRR::ReturnData(nsIChannel* aChannel) {
TimeStamp asyncOpen, start, end;
if (NS_SUCCEEDED(timedChan->GetAsyncOpen(&asyncOpen)) &&
!asyncOpen.IsNull()) {
ai->SetTrrFetchDuration(
builder.SetTrrFetchDuration(
(TimeStamp::Now() - asyncOpen).ToMilliseconds());
}
if (NS_SUCCEEDED(timedChan->GetRequestStart(&start)) &&
NS_SUCCEEDED(timedChan->GetResponseEnd(&end)) && !start.IsNull() &&
!end.IsNull()) {
ai->SetTrrFetchDurationNetworkOnly((end - start).ToMilliseconds());
builder.SetTrrFetchDurationNetworkOnly((end - start).ToMilliseconds());
}
}
ai = builder.Finish();
if (!mHostResolver) {
return NS_ERROR_FAILURE;
@ -1368,7 +1372,8 @@ nsresult TRR::FailData(nsresult error) {
} else {
// create and populate an TRR AddrInfo instance to pass on to signal that
// this comes from TRR
RefPtr<AddrInfo> ai = new AddrInfo(mHost, mType);
nsTArray<NetAddr> noAddresses;
RefPtr<AddrInfo> ai = new AddrInfo(mHost, mType, std::move(noAddresses));
(void)mHostResolver->CompleteLookup(mRec, error, ai, mPB, mOriginSuffix,
mTRRSkippedReason);

View File

@ -916,10 +916,10 @@ AHostResolver::LookupStatus TRRService::CompleteLookup(
// when called without a host record, this is a domain name check response.
if (NS_SUCCEEDED(status)) {
LOG(("TRR verified %s to be fine!\n", newRRSet->mHostName.get()));
LOG(("TRR verified %s to be fine!\n", newRRSet->Hostname().get()));
} else {
LOG(("TRR says %s doesn't resolve as NS!\n", newRRSet->mHostName.get()));
AddToBlocklist(newRRSet->mHostName, aOriginSuffix, pb, false);
LOG(("TRR says %s doesn't resolve as NS!\n", newRRSet->Hostname().get()));
AddToBlocklist(newRRSet->Hostname(), aOriginSuffix, pb, false);
}
return LOOKUP_OK;
}

View File

@ -80,8 +80,8 @@ class nsDNSRecord : public nsIDNSAddrRecord {
virtual ~nsDNSRecord() = default;
RefPtr<AddrHostRecord> mHostRecord;
nsTArray<NetAddr>::iterator mIter;
NetAddr* iter() {
nsTArray<NetAddr>::const_iterator mIter;
const NetAddr* iter() {
if (!mIter.GetArray()) {
return nullptr;
}
@ -113,10 +113,10 @@ nsDNSRecord::GetCanonicalName(nsACString& result) {
return NS_OK;
}
if (mHostRecord->addr_info->mCanonicalName.IsEmpty()) {
result = mHostRecord->addr_info->mHostName;
if (mHostRecord->addr_info->CanonicalHostname().IsEmpty()) {
result = mHostRecord->addr_info->Hostname();
} else {
result = mHostRecord->addr_info->mCanonicalName;
result = mHostRecord->addr_info->CanonicalHostname();
}
return NS_OK;
}
@ -164,7 +164,7 @@ nsDNSRecord::GetNextAddr(uint16_t port, NetAddr* addr) {
if (mHostRecord->addr_info) {
if (mIterGenCnt != mHostRecord->addr_info_gencnt) {
// mHostRecord->addr_info has changed, restart the iteration.
mIter = nsTArray<NetAddr>::iterator();
mIter = nsTArray<NetAddr>::const_iterator();
mIterGenCnt = mHostRecord->addr_info_gencnt;
}
@ -172,7 +172,7 @@ nsDNSRecord::GetNextAddr(uint16_t port, NetAddr* addr) {
do {
if (!iter()) {
mIter = mHostRecord->addr_info->mAddresses.begin();
mIter = mHostRecord->addr_info->Addresses().begin();
} else {
mIter++;
}
@ -183,7 +183,7 @@ nsDNSRecord::GetNextAddr(uint16_t port, NetAddr* addr) {
// likely relearn it) and return the first address. That is better
// than nothing.
mHostRecord->ResetBlacklist();
mIter = mHostRecord->addr_info->mAddresses.begin();
mIter = mHostRecord->addr_info->Addresses().begin();
}
if (iter()) {
@ -228,7 +228,7 @@ nsDNSRecord::GetAddresses(nsTArray<NetAddr>& aAddressArray) {
mHostRecord->addr_info_lock.Lock();
if (mHostRecord->addr_info) {
for (auto& address : mHostRecord->addr_info->mAddresses) {
for (const auto& address : mHostRecord->addr_info->Addresses()) {
if (mHostRecord->Blacklisted(&address)) {
continue;
}
@ -291,7 +291,7 @@ nsDNSRecord::HasMore(bool* result) {
return NS_OK;
}
nsTArray<NetAddr>::iterator iterCopy = mIter;
nsTArray<NetAddr>::const_iterator iterCopy = mIter;
int iterGenCntCopy = mIterGenCnt;
NetAddr addr;
@ -306,7 +306,7 @@ nsDNSRecord::HasMore(bool* result) {
NS_IMETHODIMP
nsDNSRecord::Rewind() {
mIter = nsTArray<NetAddr>::iterator();
mIter = nsTArray<NetAddr>::const_iterator();
mIterGenCnt = -1;
mDone = false;
return NS_OK;

View File

@ -297,7 +297,7 @@ AddrHostRecord::~AddrHostRecord() {
Telemetry::Accumulate(Telemetry::DNS_BLACKLIST_COUNT, mBlacklistedCount);
}
bool AddrHostRecord::Blacklisted(NetAddr* aQuery) {
bool AddrHostRecord::Blacklisted(const NetAddr* aQuery) {
// must call locked
LOG(("Checking blacklist for host [%s], host record [%p].\n", host.get(),
this));
@ -323,7 +323,7 @@ bool AddrHostRecord::Blacklisted(NetAddr* aQuery) {
return false;
}
void AddrHostRecord::ReportUnusable(NetAddr* aAddress) {
void AddrHostRecord::ReportUnusable(const NetAddr* aAddress) {
// must call locked
LOG(
("Adding address to blacklist for host [%s], host record [%p]."
@ -1023,8 +1023,7 @@ nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
// ok, just copy the result into the host record, and be
// done with it! ;-)
addrRec->addr = MakeUnique<NetAddr>();
PRNetAddrToNetAddr(&tempAddr, addrRec->addr.get());
addrRec->addr = MakeUnique<NetAddr>(&tempAddr);
// put reference to host record on stack...
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
result = rec;
@ -1088,20 +1087,21 @@ nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
// Search for any valid address in the AF_UNSPEC entry
// in the cache (not blacklisted and from the right
// family).
for (auto& addr : addrUnspecRec->addr_info->mAddresses) {
nsTArray<NetAddr> addresses;
for (const auto& addr : addrUnspecRec->addr_info->Addresses()) {
if ((af == addr.inet.family) &&
!addrUnspecRec->Blacklisted(&addr)) {
if (!addrRec->addr_info) {
addrRec->addr_info =
new AddrInfo(addrUnspecRec->addr_info->mHostName,
addrUnspecRec->addr_info->mCanonicalName,
addrUnspecRec->addr_info->IsTRR());
addresses.AppendElement(addr);
}
}
if (!addresses.IsEmpty()) {
addrRec->addr_info = new AddrInfo(
addrUnspecRec->addr_info->Hostname(),
addrUnspecRec->addr_info->CanonicalHostname(),
addrUnspecRec->addr_info->IsTRR(), std::move(addresses));
addrRec->addr_info_gencnt++;
rec->CopyExpirationTimesAndFlagsFrom(unspecRec);
}
addrRec->addr_info->mAddresses.AppendElement(addr);
}
}
}
// Now check if we have a new record.
if (rec->HasUsableResult(now, flags)) {
@ -1761,8 +1761,8 @@ void nsHostResolver::PrepareRecordExpirationAddrRecord(
unsigned int ttl = mDefaultCacheLifetime;
if (sGetTtlEnabled || rec->addr_info->IsTRR()) {
if (rec->addr_info && rec->addr_info->ttl != AddrInfo::NO_TTL_DATA) {
ttl = rec->addr_info->ttl;
if (rec->addr_info && rec->addr_info->TTL() != AddrInfo::NO_TTL_DATA) {
ttl = rec->addr_info->TTL();
}
lifetime = ttl;
grace = 0;
@ -1773,25 +1773,26 @@ void nsHostResolver::PrepareRecordExpirationAddrRecord(
lifetime, grace));
}
static nsresult merge_rrset(AddrInfo* rrto, AddrInfo* rrfrom) {
if (!rrto || !rrfrom) {
return NS_ERROR_NULL_POINTER;
}
static already_AddRefed<AddrInfo> merge_rrset(AddrInfo* rrto,
AddrInfo* rrfrom) {
MOZ_ASSERT(rrto && rrfrom);
// Each of the arguments are all-IPv4 or all-IPv6 hence judging
// by the first element. This is true only for TRR resolutions.
bool isIPv6 = rrfrom->mAddresses.Length() > 0 &&
rrfrom->mAddresses[0].raw.family == PR_AF_INET6;
for (auto& addr : rrfrom->mAddresses) {
bool isIPv6 = rrfrom->Addresses().Length() > 0 &&
rrfrom->Addresses()[0].raw.family == PR_AF_INET6;
nsTArray<NetAddr> addresses = rrto->Addresses().Clone();
for (const auto& addr : rrfrom->Addresses()) {
if (isIPv6) {
// rrfrom has IPv6 so it should be first
rrto->mAddresses.InsertElementAt(0, addr);
addresses.InsertElementAt(0, addr);
} else {
rrto->mAddresses.AppendElement(addr);
addresses.AppendElement(addr);
}
}
rrfrom->mAddresses.Clear(); // needed?
return NS_OK;
auto builder = rrto->Build();
builder.SetAddresses(std::move(addresses));
return builder.Finish();
}
static bool different_rrset(AddrInfo* rrset1, AddrInfo* rrset2) {
@ -1799,19 +1800,19 @@ static bool different_rrset(AddrInfo* rrset1, AddrInfo* rrset2) {
return true;
}
LOG(("different_rrset %s\n", rrset1->mHostName.get()));
LOG(("different_rrset %s\n", rrset1->Hostname().get()));
if (rrset1->IsTRR() != rrset2->IsTRR()) {
return true;
}
nsTArray<NetAddr> orderedSet1 = rrset1->mAddresses.Clone();
nsTArray<NetAddr> orderedSet2 = rrset2->mAddresses.Clone();
if (rrset1->mAddresses.Length() != rrset2->mAddresses.Length()) {
if (rrset1->Addresses().Length() != rrset2->Addresses().Length()) {
LOG(("different_rrset true due to length change\n"));
return true;
}
nsTArray<NetAddr> orderedSet1 = rrset1->Addresses().Clone();
nsTArray<NetAddr> orderedSet2 = rrset2->Addresses().Clone();
orderedSet1.Sort();
orderedSet2.Sort();
@ -1892,7 +1893,7 @@ nsHostResolver::LookupStatus nsHostResolver::CompleteLookup(
if (trrResult) {
MutexAutoLock trrlock(addrRec->mTrrLock);
LOG(("TRR lookup Complete (%d) %s %s\n", newRRSet->IsTRR(),
newRRSet->mHostName.get(), NS_SUCCEEDED(status) ? "OK" : "FAILED"));
newRRSet->Hostname().get(), NS_SUCCEEDED(status) ? "OK" : "FAILED"));
MOZ_ASSERT(TRROutstanding());
if (newRRSet->IsTRR() == TRRTYPE_A) {
MOZ_ASSERT(addrRec->mTrrA);
@ -1958,7 +1959,7 @@ nsHostResolver::LookupStatus nsHostResolver::CompleteLookup(
if (addrRec->mFirstTRR) {
if (NS_SUCCEEDED(status)) {
LOG(("Merging responses"));
merge_rrset(newRRSet, addrRec->mFirstTRR);
newRRSet = merge_rrset(newRRSet, addrRec->mFirstTRR);
} else {
LOG(("Will use previous response"));
newRRSet.swap(addrRec->mFirstTRR); // transfers
@ -2034,12 +2035,14 @@ nsHostResolver::LookupStatus nsHostResolver::CompleteLookup(
addrRec->addr_info_gencnt++;
} else {
if (addrRec->addr_info && newRRSet) {
addrRec->addr_info->ttl = newRRSet->ttl;
auto builder = addrRec->addr_info->Build();
builder.SetTTL(newRRSet->TTL());
// Update trr timings
addrRec->addr_info->SetTrrFetchDuration(
newRRSet->GetTrrFetchDuration());
addrRec->addr_info->SetTrrFetchDurationNetworkOnly(
builder.SetTrrFetchDuration(newRRSet->GetTrrFetchDuration());
builder.SetTrrFetchDurationNetworkOnly(
newRRSet->GetTrrFetchDurationNetworkOnly());
addrRec->addr_info = builder.Finish();
}
old_addr_info = std::move(newRRSet);
}
@ -2059,7 +2062,7 @@ nsHostResolver::LookupStatus nsHostResolver::CompleteLookup(
if (LOG_ENABLED()) {
MutexAutoLock lock(addrRec->addr_info_lock);
if (addrRec->addr_info) {
for (auto& elem : addrRec->addr_info->mAddresses) {
for (const auto& elem : addrRec->addr_info->Addresses()) {
char buf[128];
NetAddrToString(&elem, buf, sizeof(buf));
LOG(("CompleteLookup: %s has %s\n", addrRec->host.get(), buf));
@ -2374,7 +2377,7 @@ void nsHostResolver::GetDNSCacheEntries(nsTArray<DNSCacheEntries>* args) {
{
MutexAutoLock lock(addrRec->addr_info_lock);
for (auto& addr : addrRec->addr_info->mAddresses) {
for (const auto& addr : addrRec->addr_info->Addresses()) {
char buf[kIPv6CStrBufSize];
if (NetAddrToString(&addr, buf, sizeof(buf))) {
info.hostaddr.AppendElement(buf);

View File

@ -244,9 +244,9 @@ class AddrHostRecord final : public nsHostRecord {
mozilla::UniquePtr<mozilla::net::NetAddr> addr;
// hold addr_info_lock when calling the blacklist functions
bool Blacklisted(mozilla::net::NetAddr* query);
bool Blacklisted(const mozilla::net::NetAddr* query);
void ResetBlacklist();
void ReportUnusable(mozilla::net::NetAddr* addr);
void ReportUnusable(const mozilla::net::NetAddr* aAddress);
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const override;

View File

@ -547,8 +547,7 @@ bool nsHttpConnectionInfo::HostIsLocalIPLiteral() const {
} else if (PR_StringToNetAddr(Origin(), &prAddr) != PR_SUCCESS) {
return false;
}
NetAddr netAddr;
PRNetAddrToNetAddr(&prAddr, &netAddr);
NetAddr netAddr(&prAddr);
return IsIPAddrLocal(&netAddr);
}