1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/* -*- 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"
#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
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/net/DNS.h"
#include "NativeDNSResolverOverrideParent.h"
#include "prnetdb.h"
#include "nsIOService.h"
#include "nsHostResolver.h"
#include "nsError.h"
#include "mozilla/net/DNS.h"
#include <algorithm>
#include "prerror.h"
#include "mozilla/Logging.h"
#include "mozilla/StaticPrefs_network.h"
namespace mozilla::net {
static StaticRefPtr<NativeDNSResolverOverride> gOverrideService;
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__))
#ifdef DNSQUERY_AVAILABLE
# define COMPUTER_NAME_BUFFER_SIZE 100
static char sDNSComputerName[COMPUTER_NAME_BUFFER_SIZE];
static char sNETBIOSComputerName[MAX_COMPUTERNAME_LENGTH + 1];
////////////////////////////
// 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_*");
// If successful, returns in aResult a TTL value that is smaller or
// equal with the one already there. Gets the TTL value by calling
// to DnsQuery_A and iterating through the returned
// records to find the one with the smallest TTL value.
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) {
LOG("No DNS records found for %s. status=%lX. reqFamily = %X\n",
aHost.BeginReading(), status, reqFamily);
return NS_ERROR_FAILURE;
} else if (status != NOERROR) {
LOG_WARNING("DnsQuery_A failed with status %lX.\n", status);
return NS_ERROR_UNEXPECTED;
}
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);
}
DnsFree(dnsData, DNS_FREE_TYPE::DnsFreeRecordList);
return NS_OK;
};
if (aAddressFamily == PR_AF_UNSPEC || aAddressFamily == PR_AF_INET) {
callDnsQuery_A(DNS_TYPE_A);
}
if (aAddressFamily == PR_AF_UNSPEC || aAddressFamily == PR_AF_INET6) {
callDnsQuery_A(DNS_TYPE_AAAA);
}
return NS_OK;
}
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;
}
static MOZ_ALWAYS_INLINE nsresult _GetTTLData_Windows(const nsACString& aHost,
uint32_t* aResult,
uint16_t aAddressFamily) {
MOZ_ASSERT(!aHost.IsEmpty());
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.
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);
unsigned int ttl = (unsigned int)-1;
_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());
}
});
if (ttl == (unsigned int)-1) {
LOG("No useable TTL found.");
return NS_ERROR_FAILURE;
}
*aResult = ttl;
return NS_OK;
}
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);
});
LOG("Query for: %s has %zu results", aCanonHost.BeginReading(),
addresses.Length());
if (addresses.IsEmpty()) {
return NS_ERROR_UNKNOWN_HOST;
}
RefPtr<AddrInfo> ai(new AddrInfo(
aCanonHost, canonName, DNSResolverType::Native, 0, std::move(addresses)));
ai.forget(aAddrInfo);
return NS_OK;
}
#endif
////////////////////////////////////
// PORTABLE RUNTIME IMPLEMENTATION//
////////////////////////////////////
static MOZ_ALWAYS_INLINE nsresult
_GetAddrInfo_Portable(const nsACString& aCanonHost, uint16_t aAddressFamily,
uint16_t aFlags, AddrInfo** aAddrInfo) {
MOZ_ASSERT(!aCanonHost.IsEmpty());
MOZ_ASSERT(aAddrInfo);
// 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;
}
#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
LOG("Resolving %s using PR_GetAddrInfoByName", aCanonHost.BeginReading());
PRAddrInfo* prai =
PR_GetAddrInfoByName(aCanonHost.BeginReading(), aAddressFamily, prFlags);
if (!prai) {
LOG("PR_GetAddrInfoByName returned null PR_GetError:%d PR_GetOSErrpr:%d",
PR_GetError(), PR_GetOSError());
return NS_ERROR_UNKNOWN_HOST;
}
nsAutoCString canonName;
if (aFlags & nsHostResolver::RES_CANON_NAME) {
canonName.Assign(PR_GetCanonNameFromAddrInfo(prai));
}
bool filterNameCollision =
!(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION);
RefPtr<AddrInfo> ai(new AddrInfo(aCanonHost, prai, disableIPv4,
filterNameCollision, canonName));
PR_FreeAddrInfo(prai);
if (ai->Addresses().IsEmpty()) {
LOG("PR_GetAddrInfoByName returned empty address list");
return NS_ERROR_UNKNOWN_HOST;
}
ai.forget(aAddrInfo);
LOG("PR_GetAddrInfoByName resolved successfully");
return NS_OK;
}
//////////////////////////////////////
// COMMON/PLATFORM INDEPENDENT CODE //
//////////////////////////////////////
nsresult GetAddrInfoInit() {
LOG("Initializing GetAddrInfo.\n");
#ifdef DNSQUERY_AVAILABLE
DWORD namesize = COMPUTER_NAME_BUFFER_SIZE;
if (!GetComputerNameExA(ComputerNameDnsHostname, sDNSComputerName,
&namesize)) {
sDNSComputerName[0] = 0;
}
namesize = MAX_COMPUTERNAME_LENGTH + 1;
if (!GetComputerNameExA(ComputerNameNetBIOS, sNETBIOSComputerName,
&namesize)) {
sNETBIOSComputerName[0] = 0;
}
#endif
return NS_OK;
}
nsresult GetAddrInfoShutdown() {
LOG("Shutting down GetAddrInfo.\n");
return NS_OK;
}
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);
auto overrides = overrideService->mOverrides.Lookup(aHost);
if (!overrides) {
return false;
}
nsCString* cname = nullptr;
if (aFlags & nsHostResolver::RES_CANON_NAME) {
cname = overrideService->mCnames.Lookup(aHost).DataPtrOrNull();
}
RefPtr<AddrInfo> ai;
nsTArray<NetAddr> addresses;
for (const auto& ip : *overrides) {
if (aAddressFamily != AF_UNSPEC && ip.raw.family != aAddressFamily) {
continue;
}
addresses.AppendElement(ip);
}
if (!cname) {
ai = new AddrInfo(aHost, DNSResolverType::Native, 0, std::move(addresses));
} else {
ai = new AddrInfo(aHost, *cname, DNSResolverType::Native, 0,
std::move(addresses));
}
ai.forget(aAddrInfo);
return true;
}
nsresult GetAddrInfo(const nsACString& aHost, uint16_t aAddressFamily,
uint16_t aFlags, AddrInfo** aAddrInfo, bool aGetTtl) {
if (NS_WARN_IF(aHost.IsEmpty()) || NS_WARN_IF(!aAddrInfo)) {
return NS_ERROR_NULL_POINTER;
}
*aAddrInfo = nullptr;
if (StaticPrefs::network_dns_disabled()) {
return NS_ERROR_UNKNOWN_HOST;
}
#ifdef DNSQUERY_AVAILABLE
// The GetTTLData needs the canonical name to function properly
if (aGetTtl) {
aFlags |= nsHostResolver::RES_CANON_NAME;
}
#endif
// If there is an override for this host, then we synthetize a result.
if (gOverrideService &&
FindAddrOverride(aHost, aAddressFamily, aFlags, aAddrInfo)) {
LOG("Returning IP address from NativeDNSResolverOverride");
return (*aAddrInfo)->Addresses().Length() ? NS_OK : NS_ERROR_UNKNOWN_HOST;
}
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;
}
if (gNativeIsLocalhost) {
// pretend we use the given host but use IPv4 localhost instead!
host = "localhost"_ns;
aAddressFamily = PR_AF_INET;
}
RefPtr<AddrInfo> info;
nsresult rv =
_GetAddrInfo_Portable(host, aAddressFamily, aFlags, getter_AddRefs(info));
#ifdef DNSQUERY_AVAILABLE
if (aGetTtl && NS_SUCCEEDED(rv)) {
// Figure out the canonical name, or if that fails, just use the host name
// we have.
nsAutoCString name;
if (info && !info->CanonicalHostname().IsEmpty()) {
name = info->CanonicalHostname();
} else {
name = host;
}
LOG("Getting TTL for %s (cname = %s).", host.get(), name.get());
uint32_t ttl = 0;
nsresult ttlRv = _GetTTLData_Windows(name, &ttl, aAddressFamily);
if (NS_SUCCEEDED(ttlRv)) {
auto builder = info->Build();
builder.SetTTL(ttl);
info = builder.Finish();
LOG("Got TTL %u for %s (name = %s).", ttl, host.get(), name.get());
} else {
LOG_WARNING("Could not get TTL for %s (cname = %s).", host.get(),
name.get());
}
}
#endif
info.forget(aAddrInfo);
return rv;
}
// static
already_AddRefed<nsINativeDNSResolverOverride>
NativeDNSResolverOverride::GetSingleton() {
if (nsIOService::UseSocketProcess() && XRE_IsParentProcess()) {
return NativeDNSResolverOverrideParent::GetSingleton();
}
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) {
NetAddr tempAddr;
if (aIPLiteral.Equals("N/A"_ns)) {
AutoWriteLock lock(mLock);
auto& overrides = mOverrides.LookupOrInsert(aHost);
overrides.Clear();
return NS_OK;
}
if (NS_FAILED(tempAddr.InitFromString(aIPLiteral))) {
return NS_ERROR_UNEXPECTED;
}
AutoWriteLock lock(mLock);
auto& overrides = mOverrides.LookupOrInsert(aHost);
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);
mCnames.InsertOrUpdate(aHost, nsCString(aCNAME));
return NS_OK;
}
NS_IMETHODIMP NativeDNSResolverOverride::ClearHostOverride(
const nsACString& aHost) {
AutoWriteLock lock(mLock);
mCnames.Remove(aHost);
auto overrides = mOverrides.Extract(aHost);
if (!overrides) {
return NS_OK;
}
overrides->Clear();
return NS_OK;
}
NS_IMETHODIMP NativeDNSResolverOverride::ClearOverrides() {
AutoWriteLock lock(mLock);
mOverrides.Clear();
mCnames.Clear();
return NS_OK;
}
} // namespace mozilla::net
|