2018-02-01 12:20:49 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim:set ts=4 sw=2 sts=2 et cin: */
|
2012-12-24 01:08:43 +04: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/net/DNS.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
2020-07-10 18:42:17 +03:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2017-11-30 10:30:27 +03:00
|
|
|
#include "nsString.h"
|
2012-12-24 01:08:43 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2013-01-02 21:25:32 +04:00
|
|
|
# include "ws2tcpip.h"
|
2012-12-24 01:08:43 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
const char* inet_ntop_internal(int af, const void* src, char* dst,
|
|
|
|
socklen_t size) {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (af == AF_INET) {
|
|
|
|
struct sockaddr_in s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
s.sin_family = AF_INET;
|
|
|
|
memcpy(&s.sin_addr, src, sizeof(struct in_addr));
|
|
|
|
int result = getnameinfo((struct sockaddr*)&s, sizeof(struct sockaddr_in),
|
|
|
|
dst, size, nullptr, 0, NI_NUMERICHOST);
|
|
|
|
if (result == 0) {
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
} else if (af == AF_INET6) {
|
|
|
|
struct sockaddr_in6 s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
s.sin6_family = AF_INET6;
|
|
|
|
memcpy(&s.sin6_addr, src, sizeof(struct in_addr6));
|
|
|
|
int result = getnameinfo((struct sockaddr*)&s, sizeof(struct sockaddr_in6),
|
|
|
|
dst, size, nullptr, 0, NI_NUMERICHOST);
|
|
|
|
if (result == 0) {
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
#else
|
|
|
|
return inet_ntop(af, src, dst, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies the contents of a PRNetAddr to a NetAddr.
|
|
|
|
// Does not do a ptr safety check!
|
|
|
|
void PRNetAddrToNetAddr(const PRNetAddr* prAddr, NetAddr* addr) {
|
|
|
|
if (prAddr->raw.family == PR_AF_INET) {
|
|
|
|
addr->inet.family = AF_INET;
|
|
|
|
addr->inet.port = prAddr->inet.port;
|
|
|
|
addr->inet.ip = prAddr->inet.ip;
|
|
|
|
} else if (prAddr->raw.family == PR_AF_INET6) {
|
|
|
|
addr->inet6.family = AF_INET6;
|
|
|
|
addr->inet6.port = prAddr->ipv6.port;
|
|
|
|
addr->inet6.flowinfo = prAddr->ipv6.flowinfo;
|
|
|
|
memcpy(&addr->inet6.ip, &prAddr->ipv6.ip, sizeof(addr->inet6.ip.u8));
|
|
|
|
addr->inet6.scope_id = prAddr->ipv6.scope_id;
|
|
|
|
}
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
else if (prAddr->raw.family == PR_AF_LOCAL) {
|
|
|
|
addr->local.family = AF_LOCAL;
|
|
|
|
memcpy(addr->local.path, prAddr->local.path, sizeof(addr->local.path));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies the contents of a NetAddr to a PRNetAddr.
|
|
|
|
// Does not do a ptr safety check!
|
|
|
|
void NetAddrToPRNetAddr(const NetAddr* addr, PRNetAddr* prAddr) {
|
|
|
|
if (addr->raw.family == AF_INET) {
|
|
|
|
prAddr->inet.family = PR_AF_INET;
|
|
|
|
prAddr->inet.port = addr->inet.port;
|
|
|
|
prAddr->inet.ip = addr->inet.ip;
|
|
|
|
} else if (addr->raw.family == AF_INET6) {
|
|
|
|
prAddr->ipv6.family = PR_AF_INET6;
|
|
|
|
prAddr->ipv6.port = addr->inet6.port;
|
|
|
|
prAddr->ipv6.flowinfo = addr->inet6.flowinfo;
|
|
|
|
memcpy(&prAddr->ipv6.ip, &addr->inet6.ip, sizeof(addr->inet6.ip.u8));
|
|
|
|
prAddr->ipv6.scope_id = addr->inet6.scope_id;
|
|
|
|
}
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2012-12-24 01:08:43 +04:00
|
|
|
else if (addr->raw.family == AF_LOCAL) {
|
|
|
|
prAddr->local.family = PR_AF_LOCAL;
|
|
|
|
memcpy(prAddr->local.path, addr->local.path, sizeof(addr->local.path));
|
|
|
|
}
|
2016-09-01 10:48:39 +03:00
|
|
|
#elif defined(XP_WIN)
|
|
|
|
else if (addr->raw.family == AF_LOCAL) {
|
|
|
|
prAddr->local.family = PR_AF_LOCAL;
|
|
|
|
memcpy(prAddr->local.path, addr->local.path, sizeof(addr->local.path));
|
|
|
|
}
|
2012-12-24 01:08:43 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::ToStringBuffer(char* buf, uint32_t bufSize) const {
|
|
|
|
const NetAddr* addr = this;
|
2012-12-24 01:08:43 +04:00
|
|
|
if (addr->raw.family == AF_INET) {
|
|
|
|
if (bufSize < INET_ADDRSTRLEN) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct in_addr nativeAddr = {};
|
|
|
|
nativeAddr.s_addr = addr->inet.ip;
|
|
|
|
return !!inet_ntop_internal(AF_INET, &nativeAddr, buf, bufSize);
|
|
|
|
}
|
2017-02-09 13:21:38 +03:00
|
|
|
if (addr->raw.family == AF_INET6) {
|
2012-12-24 01:08:43 +04:00
|
|
|
if (bufSize < INET6_ADDRSTRLEN) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct in6_addr nativeAddr = {};
|
|
|
|
memcpy(&nativeAddr.s6_addr, &addr->inet6.ip, sizeof(addr->inet6.ip.u8));
|
|
|
|
return !!inet_ntop_internal(AF_INET6, &nativeAddr, buf, bufSize);
|
|
|
|
}
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2020-09-01 10:22:14 +03:00
|
|
|
if (addr->raw.family == AF_LOCAL) {
|
2012-12-24 01:08:43 +04:00
|
|
|
if (bufSize < sizeof(addr->local.path)) {
|
2013-09-06 19:06:23 +04:00
|
|
|
// Many callers don't bother checking our return value, so
|
|
|
|
// null-terminate just in case.
|
|
|
|
if (bufSize > 0) {
|
|
|
|
buf[0] = '\0';
|
|
|
|
}
|
2012-12-24 01:08:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-06 19:06:23 +04:00
|
|
|
|
|
|
|
// Usually, the size passed to memcpy should be the size of the
|
|
|
|
// destination. Here, we know that the source is no larger than the
|
|
|
|
// destination, so using the source's size is always safe, whereas
|
|
|
|
// using the destination's size may cause us to read off the end of the
|
|
|
|
// source.
|
|
|
|
memcpy(buf, addr->local.path, sizeof(addr->local.path));
|
2012-12-24 01:08:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::IsLoopbackAddr() const {
|
|
|
|
const NetAddr* addr = this;
|
2012-12-24 01:08:43 +04:00
|
|
|
if (addr->raw.family == AF_INET) {
|
2019-08-30 10:57:34 +03:00
|
|
|
// Consider 127.0.0.1/8 as loopback
|
|
|
|
uint32_t ipv4Addr = ntohl(addr->inet.ip);
|
|
|
|
return (ipv4Addr >> 24) == 127;
|
2012-12-24 01:08:43 +04:00
|
|
|
}
|
2020-07-10 18:42:17 +03:00
|
|
|
if (addr->raw.family == AF_INET6) {
|
|
|
|
if (IPv6ADDR_IS_LOOPBACK(&addr->inet6.ip)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip) &&
|
|
|
|
IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) ==
|
|
|
|
htonl(INADDR_LOOPBACK)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-10 17:30:58 +03:00
|
|
|
}
|
2012-12-24 01:08:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::IsIPAddrAny() const {
|
|
|
|
if (this->raw.family == AF_INET) {
|
|
|
|
if (this->inet.ip == htonl(INADDR_ANY)) {
|
2012-12-24 01:08:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-15 13:39:56 +03:00
|
|
|
} else if (this->raw.family == AF_INET6) {
|
|
|
|
if (IPv6ADDR_IS_UNSPECIFIED(&this->inet6.ip)) {
|
2012-12-24 01:08:43 +04:00
|
|
|
return true;
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
2020-09-15 13:39:56 +03:00
|
|
|
if (IPv6ADDR_IS_V4MAPPED(&this->inet6.ip) &&
|
|
|
|
IPv6ADDR_V4MAPPED_TO_IPADDR(&this->inet6.ip) == htonl(INADDR_ANY)) {
|
2012-12-24 01:08:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:22:14 +03:00
|
|
|
NetAddr::NetAddr(const PRNetAddr* prAddr) { PRNetAddrToNetAddr(prAddr, this); }
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::IsIPAddrV4() const { return this->raw.family == AF_INET; }
|
2019-08-30 10:57:34 +03:00
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::IsIPAddrV4Mapped() const {
|
|
|
|
if (this->raw.family == AF_INET6) {
|
|
|
|
return IPv6ADDR_IS_V4MAPPED(&this->inet6.ip);
|
2012-12-24 01:08:43 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::IsIPAddrLocal() const {
|
|
|
|
const NetAddr* addr = this;
|
2013-11-11 10:59:40 +04:00
|
|
|
|
|
|
|
// IPv4 RFC1918 and Link Local Addresses.
|
|
|
|
if (addr->raw.family == AF_INET) {
|
|
|
|
uint32_t addr32 = ntohl(addr->inet.ip);
|
|
|
|
if (addr32 >> 24 == 0x0A || // 10/8 prefix (RFC 1918).
|
|
|
|
addr32 >> 20 == 0xAC1 || // 172.16/12 prefix (RFC 1918).
|
|
|
|
addr32 >> 16 == 0xC0A8 || // 192.168/16 prefix (RFC 1918).
|
|
|
|
addr32 >> 16 == 0xA9FE) { // 169.254/16 prefix (Link Local).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// IPv6 Unique and Link Local Addresses.
|
|
|
|
if (addr->raw.family == AF_INET6) {
|
|
|
|
uint16_t addr16 = ntohs(addr->inet6.ip.u16[0]);
|
|
|
|
if (addr16 >> 9 == 0xfc >> 1 || // fc00::/7 Unique Local Address.
|
|
|
|
addr16 >> 6 == 0xfe80 >> 6) { // fe80::/10 Link Local Address.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not an IPv4/6 local address.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
bool NetAddr::IsIPAddrShared() const {
|
|
|
|
const NetAddr* addr = this;
|
2019-08-28 13:58:48 +03:00
|
|
|
|
|
|
|
// IPv4 RFC6598.
|
|
|
|
if (addr->raw.family == AF_INET) {
|
|
|
|
uint32_t addr32 = ntohl(addr->inet.ip);
|
|
|
|
if (addr32 >> 22 == 0x644 >> 2) { // 100.64/10 prefix (RFC 6598).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not an IPv4 shared address.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:39:56 +03:00
|
|
|
nsresult NetAddr::GetPort(uint16_t* aResult) const {
|
2014-07-31 02:02:58 +04:00
|
|
|
uint16_t port;
|
2020-09-15 13:39:56 +03:00
|
|
|
if (this->raw.family == PR_AF_INET) {
|
|
|
|
port = this->inet.port;
|
|
|
|
} else if (this->raw.family == PR_AF_INET6) {
|
|
|
|
port = this->inet6.port;
|
2014-07-31 02:02:58 +04:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = ntohs(port);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
bool NetAddr::operator==(const NetAddr& other) const {
|
|
|
|
if (this->raw.family != other.raw.family) {
|
|
|
|
return false;
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
|
|
|
if (this->raw.family == AF_INET) {
|
2013-12-11 23:03:51 +04:00
|
|
|
return (this->inet.port == other.inet.port) &&
|
|
|
|
(this->inet.ip == other.inet.ip);
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
|
|
|
if (this->raw.family == AF_INET6) {
|
2013-12-11 23:03:51 +04:00
|
|
|
return (this->inet6.port == other.inet6.port) &&
|
|
|
|
(this->inet6.flowinfo == other.inet6.flowinfo) &&
|
|
|
|
(memcmp(&this->inet6.ip, &other.inet6.ip, sizeof(this->inet6.ip)) ==
|
|
|
|
0) &&
|
|
|
|
(this->inet6.scope_id == other.inet6.scope_id);
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
|
|
|
if (this->raw.family == AF_LOCAL) {
|
2013-12-11 23:03:51 +04:00
|
|
|
return PL_strncmp(this->local.path, other.local.path,
|
|
|
|
ArrayLength(this->local.path));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-08 04:04:36 +03:00
|
|
|
bool NetAddr::operator<(const NetAddr& other) const {
|
|
|
|
if (this->raw.family != other.raw.family) {
|
|
|
|
return this->raw.family < other.raw.family;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-02-09 13:21:38 +03:00
|
|
|
if (this->raw.family == AF_INET) {
|
2016-07-08 04:04:36 +03:00
|
|
|
if (this->inet.ip == other.inet.ip) {
|
|
|
|
return this->inet.port < other.inet.port;
|
2017-02-09 13:21:38 +03:00
|
|
|
}
|
2016-07-08 04:04:36 +03:00
|
|
|
return this->inet.ip < other.inet.ip;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-02-09 13:21:38 +03:00
|
|
|
if (this->raw.family == AF_INET6) {
|
2016-07-08 04:04:36 +03:00
|
|
|
int cmpResult =
|
2017-02-09 13:21:38 +03:00
|
|
|
memcmp(&this->inet6.ip, &other.inet6.ip, sizeof(this->inet6.ip));
|
2016-07-08 04:04:36 +03:00
|
|
|
if (cmpResult) {
|
2017-02-09 13:21:38 +03:00
|
|
|
return cmpResult < 0;
|
|
|
|
}
|
|
|
|
if (this->inet6.port != other.inet6.port) {
|
2016-07-08 04:04:36 +03:00
|
|
|
return this->inet6.port < other.inet6.port;
|
|
|
|
}
|
2017-02-09 13:21:38 +03:00
|
|
|
return this->inet6.flowinfo < other.inet6.flowinfo;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-08 04:04:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-11 23:03:51 +04:00
|
|
|
|
2017-11-30 10:30:27 +03:00
|
|
|
AddrInfo::AddrInfo(const nsACString& host, const PRAddrInfo* prAddrInfo,
|
|
|
|
bool disableIPv4, bool filterNameCollision,
|
|
|
|
const nsACString& cname)
|
2020-09-01 10:38:03 +03:00
|
|
|
: mHostName(host), mCanonicalName(cname) {
|
2013-08-16 22:49:57 +04:00
|
|
|
MOZ_ASSERT(prAddrInfo,
|
|
|
|
"Cannot construct AddrInfo with a null prAddrInfo pointer!");
|
2015-05-08 21:18:20 +03:00
|
|
|
const uint32_t nameCollisionAddr = htonl(0x7f003535); // 127.0.53.53
|
2012-12-24 01:08:43 +04:00
|
|
|
|
|
|
|
PRNetAddr tmpAddr;
|
|
|
|
void* iter = nullptr;
|
|
|
|
do {
|
|
|
|
iter = PR_EnumerateAddrInfo(iter, prAddrInfo, 0, &tmpAddr);
|
2015-05-08 21:18:20 +03:00
|
|
|
bool addIt = iter && (!disableIPv4 || tmpAddr.raw.family != PR_AF_INET) &&
|
|
|
|
(!filterNameCollision || tmpAddr.raw.family != PR_AF_INET ||
|
|
|
|
(tmpAddr.inet.ip != nameCollisionAddr));
|
|
|
|
if (addIt) {
|
2020-09-01 10:22:14 +03:00
|
|
|
NetAddr elem(&tmpAddr);
|
2020-09-01 10:38:03 +03:00
|
|
|
mAddresses.AppendElement(elem);
|
2012-12-24 01:08:43 +04:00
|
|
|
}
|
|
|
|
} while (iter);
|
|
|
|
}
|
|
|
|
|
2017-11-30 10:30:27 +03:00
|
|
|
AddrInfo::AddrInfo(const nsACString& host, const nsACString& cname,
|
2020-09-01 10:22:14 +03:00
|
|
|
unsigned int aTRR, nsTArray<NetAddr>&& addresses)
|
|
|
|
: mHostName(host),
|
|
|
|
mCanonicalName(cname),
|
|
|
|
mFromTRR(aTRR),
|
|
|
|
mAddresses(std::move(addresses)) {}
|
|
|
|
|
|
|
|
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)) {}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
// deep copy constructor
|
|
|
|
AddrInfo::AddrInfo(const AddrInfo* src) {
|
2017-11-30 10:30:27 +03:00
|
|
|
mHostName = src->mHostName;
|
|
|
|
mCanonicalName = src->mCanonicalName;
|
2018-02-01 12:20:49 +03:00
|
|
|
ttl = src->ttl;
|
|
|
|
mFromTRR = src->mFromTRR;
|
2020-02-04 14:09:11 +03:00
|
|
|
mTrrFetchDuration = src->mTrrFetchDuration;
|
|
|
|
mTrrFetchDurationNetworkOnly = src->mTrrFetchDurationNetworkOnly;
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2020-09-01 10:38:03 +03:00
|
|
|
mAddresses = src->mAddresses.Clone();
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-06 12:22:49 +03:00
|
|
|
AddrInfo::~AddrInfo() = default;
|
2013-08-16 22:49:57 +04:00
|
|
|
|
2013-11-25 03:24:12 +04:00
|
|
|
size_t AddrInfo::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const {
|
|
|
|
size_t n = mallocSizeOf(this);
|
2017-11-30 10:30:27 +03:00
|
|
|
n += mHostName.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
n += mCanonicalName.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
2020-09-01 10:38:03 +03:00
|
|
|
n += mAddresses.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2013-11-25 03:24:12 +04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
2012-12-24 01:08:43 +04:00
|
|
|
} // namespace mozilla
|