Bug 1960474 - Add LNA utility functions. r=necko-reviewers,valentin

Differential Revision: https://phabricator.services.mozilla.com/D238479
This commit is contained in:
smayya
2025-05-22 11:11:27 +00:00
committed by smayya@mozilla.com
parent 78cc49157a
commit 7fc56ed888
3 changed files with 49 additions and 10 deletions

View File

@@ -225,8 +225,8 @@ LoadInfo::LoadInfo(
aLoadingContext->OwnerDoc()->CookieJarSettings())
->Clone();
}
// TODO browsing context id is not set. Check how we need to handle setting
// of parent IP address space if not availble.
// XXX(sunil) browsing context id is not set. Check how we need to handle
// setting of parent IP address space if not availble.
mInnerWindowID = aLoadingContext->OwnerDoc()->InnerWindowID();
RefPtr<WindowContext> ctx = WindowContext::GetById(mInnerWindowID);
@@ -2635,8 +2635,6 @@ void LoadInfo::UpdateParentAddressSpaceInfo() {
RefPtr<mozilla::dom::BrowsingContext> bc;
GetBrowsingContext(getter_AddRefs(bc));
if (!bc) {
// TODO: confirm this assumption holds for all cases
// See Bug 1967165
mParentIPAddressSpace = nsILoadInfo::Local;
return;
}
@@ -2649,8 +2647,8 @@ void LoadInfo::UpdateParentAddressSpaceInfo() {
} else if (RefPtr<dom::BrowsingContext> opener = bc->GetOpener()) {
mParentIPAddressSpace = opener->GetCurrentIPAddressSpace();
} else {
// TODO: add if this was loaded from about:blank. In that case we need to
// give assign local IPAddress
// XXX (sunil): add if this was loaded from about:blank. In that case we
// need to give assign local IPAddress
}
} else {
// For non-document loads, we need to set the parent IPAddressSpace to

View File

@@ -159,8 +159,30 @@ bool NetAddr::IsLoopbackAddr() const {
return false;
}
return IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip) &&
IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) == htonl(INADDR_LOOPBACK);
if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip)) {
return IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) ==
htonl(INADDR_LOOPBACK);
}
// IPv6 loopback address ::1
uint64_t ipv6Addr1 = ntohl(addr->inet6.ip.u64[0]);
uint64_t ipv6Addr2 = ntohl(addr->inet6.ip.u64[1]);
return (ipv6Addr1 == 0 && ipv6Addr2 == 1);
}
bool NetAddr::IsBenchMarkingAddress() const {
// check for 198.18.0.0/15
if (this->raw.family == AF_INET) {
uint32_t addr = ntohl(this->inet.ip) >> 17;
return addr == (0xC612 >> 1);
}
if (IPv6ADDR_IS_V4MAPPED(&this->inet6.ip)) {
uint32_t addr = ntohl(IPv6ADDR_V4MAPPED_TO_IPADDR(&this->inet6.ip)) >> 17;
return addr == (0xC612 >> 1);
}
return false;
}
bool NetAddr::IsLoopBackAddressWithoutIPv6Mapping() const {
@@ -214,6 +236,21 @@ bool NetAddr::IsIPAddrAny() const {
NetAddr::NetAddr(const PRNetAddr* prAddr) { PRNetAddrToNetAddr(prAddr, this); }
nsILoadInfo::IPAddressSpace NetAddr::GetIpAddressSpace() const {
const NetAddr* addr = this;
if (addr->IsBenchMarkingAddress() || addr->IsLoopbackAddr() ||
addr->IsIPAddrAny()) {
return nsILoadInfo::IPAddressSpace::Local;
}
if (addr->IsIPAddrLocal() || addr->IsIPAddrShared()) {
return nsILoadInfo::IPAddressSpace::Private;
}
return nsILoadInfo::IPAddressSpace::Public;
}
nsresult NetAddr::InitFromString(const nsACString& aString, uint16_t aPort) {
PRNetAddr prAddr{};
memset(&prAddr, 0, sizeof(PRNetAddr));
@@ -245,7 +282,7 @@ static bool isLocalIPv4(uint32_t networkEndianIP) {
uint32_t addr32 = ntohl(networkEndianIP);
return addr32 >> 24 == 0x00 || // 0/8 prefix (RFC 1122).
addr32 >> 24 == 0x0A || // 10/8 prefix (RFC 1918).
addr32 >> 20 == 0xAC1 || // 172.16/12 prefix (RFC 1918).
addr32 >> 20 == 0x0AC1 || // 172.16/12 prefix (RFC 1918).
addr32 >> 16 == 0xC0A8 || // 192.168/16 prefix (RFC 1918).
addr32 >> 16 == 0xA9FE; // 169.254/16 prefix (Link Local).
}

View File

@@ -7,6 +7,7 @@
#ifndef DNS_h_
#define DNS_h_
#include "nsILoadInfo.h"
#include "nscore.h"
#include "nsString.h"
#include "prio.h"
@@ -148,6 +149,7 @@ union NetAddr {
bool IsLoopbackAddr() const;
bool IsLoopBackAddressWithoutIPv6Mapping() const;
bool IsIPAddrV4() const;
bool IsBenchMarkingAddress() const;
bool IsIPAddrV4Mapped() const;
bool IsIPAddrLocal() const;
bool IsIPAddrShared() const;
@@ -155,6 +157,7 @@ union NetAddr {
bool ToStringBuffer(char* buf, uint32_t bufSize) const;
nsCString ToString() const;
void ToAddrPortString(nsACString& aOutput) const;
nsILoadInfo::IPAddressSpace GetIpAddressSpace() const;
};
enum class DNSResolverType : uint32_t { Native = 0, TRR };
@@ -176,7 +179,8 @@ class AddrInfo {
DNSResolverType aResolverType, unsigned int aTRRType,
nsTArray<NetAddr>&& addresses);
// Creates a basic AddrInfo object (initialize only the host and TRR status).
// Creates a basic AddrInfo object (initialize only the host and TRR
// status).
explicit AddrInfo(const nsACString& host, DNSResolverType aResolverType,
unsigned int aTRRType, nsTArray<NetAddr>&& addresses,
uint32_t aTTL = NO_TTL_DATA);