2014-09-21 02:10:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "GetAddrInfo.h"
|
2020-10-30 11:16:25 +03:00
|
|
|
|
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
|
|
|
// There is a bug in windns.h where the type of parameter ppQueryResultsSet for
|
|
|
|
// DnsQuery_A is dependent on UNICODE being set. It should *always* be
|
|
|
|
// PDNS_RECORDA, but if UNICODE is set it is PDNS_RECORDW. To get around this
|
|
|
|
// we make sure that UNICODE is unset.
|
|
|
|
# undef UNICODE
|
|
|
|
# include <ws2tcpip.h>
|
|
|
|
# undef GetAddrInfo
|
|
|
|
# include <windns.h>
|
|
|
|
#endif // DNSQUERY_AVAILABLE
|
|
|
|
|
2020-05-11 11:20:34 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
#include "mozilla/net/DNS.h"
|
2020-06-05 16:27:35 +03:00
|
|
|
#include "NativeDNSResolverOverrideParent.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
#include "prnetdb.h"
|
2020-06-05 16:27:35 +03:00
|
|
|
#include "nsIOService.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
#include "nsHostResolver.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "mozilla/net/DNS.h"
|
2014-09-22 05:52:28 +04:00
|
|
|
#include <algorithm>
|
2015-02-19 08:14:00 +03:00
|
|
|
#include "prerror.h"
|
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2020-05-14 21:26:17 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2021-02-24 19:37:04 +03:00
|
|
|
namespace mozilla::net {
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2020-03-02 22:44:37 +03:00
|
|
|
static StaticRefPtr<NativeDNSResolverOverride> gOverrideService;
|
|
|
|
|
2015-11-03 07:35:29 +03:00
|
|
|
static LazyLogModule gGetAddrInfoLog("GetAddrInfo");
|
|
|
|
#define LOG(msg, ...) \
|
|
|
|
MOZ_LOG(gGetAddrInfoLog, LogLevel::Debug, ("[DNS]: " msg, ##__VA_ARGS__))
|
|
|
|
#define LOG_WARNING(msg, ...) \
|
|
|
|
MOZ_LOG(gGetAddrInfoLog, LogLevel::Warning, ("[DNS]: " msg, ##__VA_ARGS__))
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
2020-10-30 11:16:25 +03:00
|
|
|
|
|
|
|
# define COMPUTER_NAME_BUFFER_SIZE 100
|
|
|
|
static char sDNSComputerName[COMPUTER_NAME_BUFFER_SIZE];
|
|
|
|
static char sNETBIOSComputerName[MAX_COMPUTERNAME_LENGTH + 1];
|
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
////////////////////////////
|
|
|
|
// WINDOWS IMPLEMENTATION //
|
|
|
|
////////////////////////////
|
|
|
|
|
|
|
|
// Ensure consistency of PR_* and AF_* constants to allow for legacy usage of
|
|
|
|
// PR_* constants with this API.
|
|
|
|
static_assert(PR_AF_INET == AF_INET && PR_AF_INET6 == AF_INET6 &&
|
|
|
|
PR_AF_UNSPEC == AF_UNSPEC,
|
|
|
|
"PR_AF_* must match AF_*");
|
|
|
|
|
2015-05-01 16:49:00 +03:00
|
|
|
// If successful, returns in aResult a TTL value that is smaller or
|
|
|
|
// equal with the one already there. Gets the TTL value by calling
|
2020-02-14 14:40:57 +03:00
|
|
|
// to DnsQuery_A and iterating through the returned
|
2015-05-01 16:49:00 +03:00
|
|
|
// records to find the one with the smallest TTL value.
|
2020-10-30 11:16:25 +03:00
|
|
|
static MOZ_ALWAYS_INLINE nsresult _CallDnsQuery_A_Windows(
|
|
|
|
const nsACString& aHost, uint16_t aAddressFamily, DWORD aFlags,
|
|
|
|
std::function<void(PDNS_RECORDA)> aCallback) {
|
|
|
|
NS_ConvertASCIItoUTF16 name(aHost);
|
|
|
|
|
|
|
|
auto callDnsQuery_A = [&](uint16_t reqFamily) {
|
|
|
|
PDNS_RECORDA dnsData = nullptr;
|
|
|
|
DNS_STATUS status = DnsQuery_A(aHost.BeginReading(), reqFamily, aFlags,
|
|
|
|
nullptr, &dnsData, nullptr);
|
|
|
|
if (status == DNS_INFO_NO_RECORDS || status == DNS_ERROR_RCODE_NAME_ERROR ||
|
|
|
|
!dnsData) {
|
Bug 1766561 - Add missing l modifier for long formatting. r=Jamie,media-playback-reviewers,gfx-reviewers,nika,necko-reviewers,mhowell,rkraesig,gerald,application-update-reviewers,bytesized,alwu,kershaw
Differential Revision: https://phabricator.services.mozilla.com/D144917
2022-05-03 23:49:08 +03:00
|
|
|
LOG("No DNS records found for %s. status=%lX. reqFamily = %X\n",
|
2020-10-30 11:16:25 +03:00
|
|
|
aHost.BeginReading(), status, reqFamily);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
} else if (status != NOERROR) {
|
Bug 1766561 - Add missing l modifier for long formatting. r=Jamie,media-playback-reviewers,gfx-reviewers,nika,necko-reviewers,mhowell,rkraesig,gerald,application-update-reviewers,bytesized,alwu,kershaw
Differential Revision: https://phabricator.services.mozilla.com/D144917
2022-05-03 23:49:08 +03:00
|
|
|
LOG_WARNING("DnsQuery_A failed with status %lX.\n", status);
|
2020-10-30 11:16:25 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2020-10-26 17:50:37 +03:00
|
|
|
}
|
2020-10-26 14:40:22 +03:00
|
|
|
|
2020-10-30 11:16:25 +03:00
|
|
|
for (PDNS_RECORDA curRecord = dnsData; curRecord;
|
|
|
|
curRecord = curRecord->pNext) {
|
|
|
|
// Only records in the answer section are important
|
|
|
|
if (curRecord->Flags.S.Section != DnsSectionAnswer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (curRecord->wType != reqFamily) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback(curRecord);
|
2020-10-26 17:50:37 +03:00
|
|
|
}
|
2020-10-30 11:16:25 +03:00
|
|
|
|
|
|
|
DnsFree(dnsData, DNS_FREE_TYPE::DnsFreeRecordList);
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (aAddressFamily == PR_AF_UNSPEC || aAddressFamily == PR_AF_INET) {
|
|
|
|
callDnsQuery_A(DNS_TYPE_A);
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:16:25 +03:00
|
|
|
if (aAddressFamily == PR_AF_UNSPEC || aAddressFamily == PR_AF_INET6) {
|
|
|
|
callDnsQuery_A(DNS_TYPE_AAAA);
|
|
|
|
}
|
2015-05-01 16:49:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:16:25 +03:00
|
|
|
bool recordTypeMatchesRequest(uint16_t wType, uint16_t aAddressFamily) {
|
|
|
|
if (aAddressFamily == PR_AF_UNSPEC) {
|
|
|
|
return wType == DNS_TYPE_A || wType == DNS_TYPE_AAAA;
|
|
|
|
}
|
|
|
|
if (aAddressFamily == PR_AF_INET) {
|
|
|
|
return wType == DNS_TYPE_A;
|
|
|
|
}
|
|
|
|
if (aAddressFamily == PR_AF_INET6) {
|
|
|
|
return wType == DNS_TYPE_AAAA;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:15:13 +03:00
|
|
|
static MOZ_ALWAYS_INLINE nsresult _GetTTLData_Windows(const nsACString& aHost,
|
|
|
|
uint32_t* aResult,
|
|
|
|
uint16_t aAddressFamily) {
|
|
|
|
MOZ_ASSERT(!aHost.IsEmpty());
|
2015-05-01 16:49:00 +03:00
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
if (aAddressFamily != PR_AF_UNSPEC && aAddressFamily != PR_AF_INET &&
|
|
|
|
aAddressFamily != PR_AF_INET6) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to avoid using ANY records which are not always implemented as a
|
|
|
|
// "Gimme what you have" request in hostname resolvers, we should send A
|
|
|
|
// and/or AAAA requests, based on the address family requested.
|
2020-10-30 11:16:25 +03:00
|
|
|
const DWORD ttlFlags =
|
|
|
|
(DNS_QUERY_STANDARD | DNS_QUERY_NO_NETBT | DNS_QUERY_NO_HOSTS_FILE |
|
|
|
|
DNS_QUERY_NO_MULTICAST | DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE |
|
|
|
|
DNS_QUERY_DONT_RESET_TTL_VALUES);
|
2017-09-29 01:47:43 +03:00
|
|
|
unsigned int ttl = (unsigned int)-1;
|
2020-10-30 11:16:25 +03:00
|
|
|
_CallDnsQuery_A_Windows(
|
|
|
|
aHost, aAddressFamily, ttlFlags,
|
|
|
|
[&ttl, &aHost, aAddressFamily](PDNS_RECORDA curRecord) {
|
|
|
|
if (recordTypeMatchesRequest(curRecord->wType, aAddressFamily)) {
|
|
|
|
ttl = std::min<unsigned int>(ttl, curRecord->dwTtl);
|
|
|
|
} else {
|
|
|
|
LOG("Received unexpected record type %u in response for %s.\n",
|
|
|
|
curRecord->wType, aHost.BeginReading());
|
|
|
|
}
|
|
|
|
});
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2017-09-29 01:47:43 +03:00
|
|
|
if (ttl == (unsigned int)-1) {
|
2014-09-21 02:10:18 +04:00
|
|
|
LOG("No useable TTL found.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = ttl;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-10-30 11:16:25 +03:00
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE nsresult
|
|
|
|
_DNSQuery_A_SingleLabel(const nsACString& aCanonHost, uint16_t aAddressFamily,
|
|
|
|
uint16_t aFlags, AddrInfo** aAddrInfo) {
|
|
|
|
bool setCanonName = aFlags & nsHostResolver::RES_CANON_NAME;
|
|
|
|
nsAutoCString canonName;
|
|
|
|
const DWORD flags = (DNS_QUERY_STANDARD | DNS_QUERY_NO_MULTICAST |
|
|
|
|
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE);
|
|
|
|
nsTArray<NetAddr> addresses;
|
|
|
|
|
|
|
|
_CallDnsQuery_A_Windows(
|
|
|
|
aCanonHost, aAddressFamily, flags, [&](PDNS_RECORDA curRecord) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(curRecord->wType == DNS_TYPE_A ||
|
|
|
|
curRecord->wType == DNS_TYPE_AAAA);
|
|
|
|
if (setCanonName) {
|
|
|
|
canonName.Assign(curRecord->pName);
|
|
|
|
}
|
|
|
|
NetAddr addr{};
|
|
|
|
addr.inet.family = AF_INET;
|
|
|
|
addr.inet.ip = curRecord->Data.A.IpAddress;
|
|
|
|
addresses.AppendElement(addr);
|
|
|
|
});
|
|
|
|
|
2022-05-03 23:49:09 +03:00
|
|
|
LOG("Query for: %s has %zu results", aCanonHost.BeginReading(),
|
2020-10-30 11:16:25 +03:00
|
|
|
addresses.Length());
|
|
|
|
if (addresses.IsEmpty()) {
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
2021-02-18 15:50:02 +03:00
|
|
|
RefPtr<AddrInfo> ai(new AddrInfo(
|
|
|
|
aCanonHost, canonName, DNSResolverType::Native, 0, std::move(addresses)));
|
2020-10-30 11:16:25 +03:00
|
|
|
ai.forget(aAddrInfo);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-21 02:10:18 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// PORTABLE RUNTIME IMPLEMENTATION//
|
|
|
|
////////////////////////////////////
|
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE nsresult
|
2018-06-15 02:15:13 +03:00
|
|
|
_GetAddrInfo_Portable(const nsACString& aCanonHost, uint16_t aAddressFamily,
|
2018-05-16 23:05:03 +03:00
|
|
|
uint16_t aFlags, AddrInfo** aAddrInfo) {
|
2018-06-15 02:15:13 +03:00
|
|
|
MOZ_ASSERT(!aCanonHost.IsEmpty());
|
2017-12-04 18:02:59 +03:00
|
|
|
MOZ_ASSERT(aAddrInfo);
|
2014-09-21 02:10:18 +04:00
|
|
|
|
|
|
|
// We accept the same aFlags that nsHostResolver::ResolveHost accepts, but we
|
|
|
|
// need to translate the aFlags into a form that PR_GetAddrInfoByName
|
|
|
|
// accepts.
|
|
|
|
int prFlags = PR_AI_ADDRCONFIG;
|
|
|
|
if (!(aFlags & nsHostResolver::RES_CANON_NAME)) {
|
|
|
|
prFlags |= PR_AI_NOCANONNAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to remove IPv4 records manually because PR_GetAddrInfoByName
|
|
|
|
// doesn't support PR_AF_INET6.
|
|
|
|
bool disableIPv4 = aAddressFamily == PR_AF_INET6;
|
|
|
|
if (disableIPv4) {
|
|
|
|
aAddressFamily = PR_AF_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:16:25 +03:00
|
|
|
#if defined(DNSQUERY_AVAILABLE)
|
|
|
|
if (StaticPrefs::network_dns_dns_query_single_label() &&
|
|
|
|
!aCanonHost.Contains('.') && aCanonHost != "localhost"_ns) {
|
|
|
|
// For some reason we can't use DnsQuery_A to get the computer's IP.
|
|
|
|
if (!aCanonHost.Equals(nsDependentCString(sDNSComputerName),
|
|
|
|
nsCaseInsensitiveCStringComparator) &&
|
|
|
|
!aCanonHost.Equals(nsDependentCString(sNETBIOSComputerName),
|
|
|
|
nsCaseInsensitiveCStringComparator)) {
|
|
|
|
// This is a single label name resolve without a dot.
|
|
|
|
// We use DNSQuery_A for these.
|
|
|
|
LOG("Resolving %s using DnsQuery_A (computername: %s)\n",
|
|
|
|
aCanonHost.BeginReading(), sDNSComputerName);
|
|
|
|
return _DNSQuery_A_SingleLabel(aCanonHost, aAddressFamily, aFlags,
|
|
|
|
aAddrInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-30 15:14:19 +03:00
|
|
|
LOG("Resolving %s using PR_GetAddrInfoByName", aCanonHost.BeginReading());
|
2018-06-15 02:15:13 +03:00
|
|
|
PRAddrInfo* prai =
|
|
|
|
PR_GetAddrInfoByName(aCanonHost.BeginReading(), aAddressFamily, prFlags);
|
2014-09-21 02:10:18 +04:00
|
|
|
|
|
|
|
if (!prai) {
|
2021-09-30 15:14:19 +03:00
|
|
|
LOG("PR_GetAddrInfoByName returned null PR_GetError:%d PR_GetOSErrpr:%d",
|
|
|
|
PR_GetError(), PR_GetOSError());
|
2014-09-21 02:10:18 +04:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:15:13 +03:00
|
|
|
nsAutoCString canonName;
|
2014-09-21 02:10:18 +04:00
|
|
|
if (aFlags & nsHostResolver::RES_CANON_NAME) {
|
2018-06-15 02:15:13 +03:00
|
|
|
canonName.Assign(PR_GetCanonNameFromAddrInfo(prai));
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2015-05-08 21:18:20 +03:00
|
|
|
bool filterNameCollision =
|
|
|
|
!(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION);
|
2019-03-19 15:22:12 +03:00
|
|
|
RefPtr<AddrInfo> ai(new AddrInfo(aCanonHost, prai, disableIPv4,
|
|
|
|
filterNameCollision, canonName));
|
2014-09-21 02:10:18 +04:00
|
|
|
PR_FreeAddrInfo(prai);
|
2020-09-01 10:22:14 +03:00
|
|
|
if (ai->Addresses().IsEmpty()) {
|
2021-09-30 15:14:19 +03:00
|
|
|
LOG("PR_GetAddrInfoByName returned empty address list");
|
2014-09-21 02:10:18 +04:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
2019-03-19 15:22:12 +03:00
|
|
|
ai.forget(aAddrInfo);
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2021-09-30 15:14:19 +03:00
|
|
|
LOG("PR_GetAddrInfoByName resolved successfully");
|
2014-09-21 02:10:18 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
// COMMON/PLATFORM INDEPENDENT CODE //
|
|
|
|
//////////////////////////////////////
|
|
|
|
nsresult GetAddrInfoInit() {
|
|
|
|
LOG("Initializing GetAddrInfo.\n");
|
2020-10-30 11:16:25 +03:00
|
|
|
|
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
|
|
|
DWORD namesize = COMPUTER_NAME_BUFFER_SIZE;
|
2021-01-04 23:34:50 +03:00
|
|
|
if (!GetComputerNameExA(ComputerNameDnsHostname, sDNSComputerName,
|
|
|
|
&namesize)) {
|
2020-10-30 11:16:25 +03:00
|
|
|
sDNSComputerName[0] = 0;
|
|
|
|
}
|
|
|
|
namesize = MAX_COMPUTERNAME_LENGTH + 1;
|
2021-01-04 23:34:50 +03:00
|
|
|
if (!GetComputerNameExA(ComputerNameNetBIOS, sNETBIOSComputerName,
|
|
|
|
&namesize)) {
|
2020-10-30 11:16:25 +03:00
|
|
|
sNETBIOSComputerName[0] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-21 02:10:18 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult GetAddrInfoShutdown() {
|
|
|
|
LOG("Shutting down GetAddrInfo.\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:44:37 +03:00
|
|
|
bool FindAddrOverride(const nsACString& aHost, uint16_t aAddressFamily,
|
|
|
|
uint16_t aFlags, AddrInfo** aAddrInfo) {
|
|
|
|
RefPtr<NativeDNSResolverOverride> overrideService = gOverrideService;
|
|
|
|
if (!overrideService) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
AutoReadLock lock(overrideService->mLock);
|
2021-03-01 12:59:30 +03:00
|
|
|
auto overrides = overrideService->mOverrides.Lookup(aHost);
|
2020-03-02 22:44:37 +03:00
|
|
|
if (!overrides) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCString* cname = nullptr;
|
|
|
|
if (aFlags & nsHostResolver::RES_CANON_NAME) {
|
2021-03-01 12:59:30 +03:00
|
|
|
cname = overrideService->mCnames.Lookup(aHost).DataPtrOrNull();
|
2020-03-02 22:44:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<AddrInfo> ai;
|
|
|
|
|
2020-09-01 10:22:14 +03:00
|
|
|
nsTArray<NetAddr> addresses;
|
2020-03-02 22:44:37 +03:00
|
|
|
for (const auto& ip : *overrides) {
|
|
|
|
if (aAddressFamily != AF_UNSPEC && ip.raw.family != aAddressFamily) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 12:26:01 +03:00
|
|
|
addresses.AppendElement(ip);
|
2020-09-01 10:22:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cname) {
|
2021-02-18 15:50:02 +03:00
|
|
|
ai = new AddrInfo(aHost, DNSResolverType::Native, 0, std::move(addresses));
|
2020-09-01 10:22:14 +03:00
|
|
|
} else {
|
2021-02-18 15:50:02 +03:00
|
|
|
ai = new AddrInfo(aHost, *cname, DNSResolverType::Native, 0,
|
|
|
|
std::move(addresses));
|
2020-03-02 22:44:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ai.forget(aAddrInfo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:15:13 +03:00
|
|
|
nsresult GetAddrInfo(const nsACString& aHost, uint16_t aAddressFamily,
|
2018-05-16 23:05:03 +03:00
|
|
|
uint16_t aFlags, AddrInfo** aAddrInfo, bool aGetTtl) {
|
2018-06-15 02:15:13 +03:00
|
|
|
if (NS_WARN_IF(aHost.IsEmpty()) || NS_WARN_IF(!aAddrInfo)) {
|
2014-09-21 02:10:18 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2020-09-01 10:22:14 +03:00
|
|
|
*aAddrInfo = nullptr;
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2020-05-14 21:26:17 +03:00
|
|
|
if (StaticPrefs::network_dns_disabled()) {
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
2014-09-21 02:10:18 +04:00
|
|
|
// The GetTTLData needs the canonical name to function properly
|
|
|
|
if (aGetTtl) {
|
|
|
|
aFlags |= nsHostResolver::RES_CANON_NAME;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 22:44:37 +03:00
|
|
|
// If there is an override for this host, then we synthetize a result.
|
|
|
|
if (gOverrideService &&
|
|
|
|
FindAddrOverride(aHost, aAddressFamily, aFlags, aAddrInfo)) {
|
2021-09-30 15:14:19 +03:00
|
|
|
LOG("Returning IP address from NativeDNSResolverOverride");
|
2021-04-28 10:43:30 +03:00
|
|
|
return (*aAddrInfo)->Addresses().Length() ? NS_OK : NS_ERROR_UNKNOWN_HOST;
|
2020-03-02 22:44:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-22 12:19:55 +03:00
|
|
|
nsAutoCString host;
|
|
|
|
if (StaticPrefs::network_dns_copy_string_before_call()) {
|
|
|
|
host = Substring(aHost.BeginReading(), aHost.Length());
|
|
|
|
MOZ_ASSERT(aHost.BeginReading() != host.BeginReading());
|
|
|
|
} else {
|
|
|
|
host = aHost;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (gNativeIsLocalhost) {
|
|
|
|
// pretend we use the given host but use IPv4 localhost instead!
|
2018-06-15 02:15:13 +03:00
|
|
|
host = "localhost"_ns;
|
2018-02-01 12:20:49 +03:00
|
|
|
aAddressFamily = PR_AF_INET;
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:22:14 +03:00
|
|
|
RefPtr<AddrInfo> info;
|
|
|
|
nsresult rv =
|
|
|
|
_GetAddrInfo_Portable(host, aAddressFamily, aFlags, getter_AddRefs(info));
|
2014-09-21 02:10:18 +04:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
#ifdef DNSQUERY_AVAILABLE
|
2014-09-21 02:10:18 +04:00
|
|
|
if (aGetTtl && NS_SUCCEEDED(rv)) {
|
|
|
|
// Figure out the canonical name, or if that fails, just use the host name
|
|
|
|
// we have.
|
2018-06-15 02:15:13 +03:00
|
|
|
nsAutoCString name;
|
2020-09-01 10:22:14 +03:00
|
|
|
if (info && !info->CanonicalHostname().IsEmpty()) {
|
|
|
|
name = info->CanonicalHostname();
|
2014-09-21 02:10:18 +04:00
|
|
|
} else {
|
2018-06-15 02:15:13 +03:00
|
|
|
name = host;
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
|
2018-06-15 02:15:13 +03:00
|
|
|
LOG("Getting TTL for %s (cname = %s).", host.get(), name.get());
|
2017-11-09 23:43:00 +03:00
|
|
|
uint32_t ttl = 0;
|
2015-05-01 16:49:00 +03:00
|
|
|
nsresult ttlRv = _GetTTLData_Windows(name, &ttl, aAddressFamily);
|
2014-09-21 02:10:18 +04:00
|
|
|
if (NS_SUCCEEDED(ttlRv)) {
|
2020-09-01 10:22:14 +03:00
|
|
|
auto builder = info->Build();
|
|
|
|
builder.SetTTL(ttl);
|
|
|
|
info = builder.Finish();
|
2018-06-15 02:15:13 +03:00
|
|
|
LOG("Got TTL %u for %s (name = %s).", ttl, host.get(), name.get());
|
2014-09-21 02:10:18 +04:00
|
|
|
} else {
|
2018-06-15 02:15:13 +03:00
|
|
|
LOG_WARNING("Could not get TTL for %s (cname = %s).", host.get(),
|
|
|
|
name.get());
|
2014-09-21 02:10:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-01 10:22:14 +03:00
|
|
|
info.forget(aAddrInfo);
|
2014-09-21 02:10:18 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:44:37 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<nsINativeDNSResolverOverride>
|
|
|
|
NativeDNSResolverOverride::GetSingleton() {
|
2020-06-05 16:27:35 +03:00
|
|
|
if (nsIOService::UseSocketProcess() && XRE_IsParentProcess()) {
|
|
|
|
return NativeDNSResolverOverrideParent::GetSingleton();
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:44:37 +03:00
|
|
|
if (gOverrideService) {
|
|
|
|
return do_AddRef(gOverrideService);
|
|
|
|
}
|
|
|
|
|
|
|
|
gOverrideService = new NativeDNSResolverOverride();
|
|
|
|
ClearOnShutdown(&gOverrideService);
|
|
|
|
return do_AddRef(gOverrideService);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(NativeDNSResolverOverride, nsINativeDNSResolverOverride)
|
|
|
|
|
|
|
|
NS_IMETHODIMP NativeDNSResolverOverride::AddIPOverride(
|
|
|
|
const nsACString& aHost, const nsACString& aIPLiteral) {
|
2021-05-10 12:26:01 +03:00
|
|
|
NetAddr tempAddr;
|
2020-03-02 22:44:37 +03:00
|
|
|
|
2021-04-28 10:43:30 +03:00
|
|
|
if (aIPLiteral.Equals("N/A"_ns)) {
|
|
|
|
AutoWriteLock lock(mLock);
|
|
|
|
auto& overrides = mOverrides.LookupOrInsert(aHost);
|
|
|
|
overrides.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:26:01 +03:00
|
|
|
if (NS_FAILED(tempAddr.InitFromString(aIPLiteral))) {
|
2020-03-02 22:44:37 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoWriteLock lock(mLock);
|
2021-02-26 12:11:45 +03:00
|
|
|
auto& overrides = mOverrides.LookupOrInsert(aHost);
|
2020-03-02 22:44:37 +03:00
|
|
|
overrides.AppendElement(tempAddr);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NativeDNSResolverOverride::SetCnameOverride(
|
|
|
|
const nsACString& aHost, const nsACString& aCNAME) {
|
|
|
|
if (aCNAME.IsEmpty()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoWriteLock lock(mLock);
|
2021-02-26 12:11:46 +03:00
|
|
|
mCnames.InsertOrUpdate(aHost, nsCString(aCNAME));
|
2020-03-02 22:44:37 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NativeDNSResolverOverride::ClearHostOverride(
|
|
|
|
const nsACString& aHost) {
|
|
|
|
AutoWriteLock lock(mLock);
|
|
|
|
mCnames.Remove(aHost);
|
2021-02-22 15:07:48 +03:00
|
|
|
auto overrides = mOverrides.Extract(aHost);
|
2020-03-02 22:44:37 +03:00
|
|
|
if (!overrides) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
overrides->Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NativeDNSResolverOverride::ClearOverrides() {
|
|
|
|
AutoWriteLock lock(mLock);
|
|
|
|
mOverrides.Clear();
|
|
|
|
mCnames.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-02-24 19:37:04 +03:00
|
|
|
} // namespace mozilla::net
|