1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sw=2 sts=2 et cin: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "GetAddrInfo.h"
8 #include "mozilla/glean/GleanMetrics.h"
9 #include "mozilla/net/DNSPacket.h"
10 #include "nsIDNSService.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/StaticPrefs_network.h"
13 #include "mozilla/ThreadLocal.h"
18 #include <netinet/in.h>
21 namespace mozilla::net
{
23 #if defined(HAVE_RES_NINIT)
24 MOZ_THREAD_LOCAL(struct __res_state
*) sThreadRes
;
27 #define LOG(msg, ...) \
28 MOZ_LOG(gGetAddrInfoLog, LogLevel::Debug, ("[DNS]: " msg, ##__VA_ARGS__))
30 nsresult
ResolveHTTPSRecordImpl(const nsACString
& aHost
, uint16_t aFlags
,
31 TypeRecordResultType
& aResult
, uint32_t& aTTL
) {
33 nsAutoCString
host(aHost
);
37 if (xpc::IsInAutomation() &&
38 !StaticPrefs::network_dns_native_https_query_in_automation()) {
39 return NS_ERROR_UNKNOWN_HOST
;
42 #if defined(HAVE_RES_NINIT)
43 if (!sThreadRes
.get()) {
44 UniquePtr
<struct __res_state
> resState(new struct __res_state
);
45 memset(resState
.get(), 0, sizeof(struct __res_state
));
46 if (int ret
= res_ninit(resState
.get())) {
47 LOG("res_ninit failed: %d", ret
);
48 return NS_ERROR_UNKNOWN_HOST
;
50 sThreadRes
.set(resState
.release());
54 LOG("resolving %s\n", host
.get());
56 rv
= packet
.FillBuffer(
57 [&](unsigned char response
[DNSPacket::MAX_SIZE
]) -> int {
59 TimeStamp startTime
= TimeStamp::Now();
60 #if defined(HAVE_RES_NINIT)
61 len
= res_nquery(sThreadRes
.get(), host
.get(), ns_c_in
,
62 nsIDNSService::RESOLVE_TYPE_HTTPSSVC
, response
,
66 res_query(host
.get(), ns_c_in
, nsIDNSService::RESOLVE_TYPE_HTTPSSVC
,
67 response
, DNSPacket::MAX_SIZE
);
70 mozilla::glean::networking::dns_native_https_call_time
71 .AccumulateRawDuration(TimeStamp::Now() - startTime
);
73 LOG("DNS query failed");
81 return ParseHTTPSRecord(host
, packet
, aResult
, aTTL
);
84 void DNSThreadShutdown() {
85 #if defined(HAVE_RES_NINIT)
86 auto* res
= sThreadRes
.get();
91 sThreadRes
.set(nullptr);
96 } // namespace mozilla::net