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/NetwerkMetrics.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
,
31 nsIDNSService::DNSFlags aFlags
,
32 TypeRecordResultType
& aResult
, uint32_t& aTTL
) {
34 nsAutoCString
host(aHost
);
38 if (xpc::IsInAutomation() &&
39 !StaticPrefs::network_dns_native_https_query_in_automation()) {
40 return NS_ERROR_UNKNOWN_HOST
;
43 #if defined(HAVE_RES_NINIT)
44 if (!sThreadRes
.get()) {
45 UniquePtr
<struct __res_state
> resState(new struct __res_state
);
46 memset(resState
.get(), 0, sizeof(struct __res_state
));
47 if (int ret
= res_ninit(resState
.get())) {
48 LOG("res_ninit failed: %d", ret
);
49 return NS_ERROR_UNKNOWN_HOST
;
51 sThreadRes
.set(resState
.release());
55 LOG("resolving %s\n", host
.get());
57 rv
= packet
.FillBuffer(
58 [&](unsigned char response
[DNSPacket::MAX_SIZE
]) -> int {
60 TimeStamp startTime
= TimeStamp::Now();
61 #if defined(HAVE_RES_NINIT)
62 len
= res_nquery(sThreadRes
.get(), host
.get(), ns_c_in
,
63 nsIDNSService::RESOLVE_TYPE_HTTPSSVC
, response
,
67 res_query(host
.get(), ns_c_in
, nsIDNSService::RESOLVE_TYPE_HTTPSSVC
,
68 response
, DNSPacket::MAX_SIZE
);
71 mozilla::glean::networking::dns_native_https_call_time
72 .AccumulateRawDuration(TimeStamp::Now() - startTime
);
74 LOG("DNS query failed");
82 return ParseHTTPSRecord(host
, packet
, aResult
, aTTL
);
85 void DNSThreadShutdown() {
86 #if defined(HAVE_RES_NINIT)
87 auto* res
= sThreadRes
.get();
92 sThreadRes
.set(nullptr);
97 } // namespace mozilla::net