Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / dns / PlatformDNSUnix.cpp
blob08775ee0cbfe821b6eb7643cdfa96de2bd786e45
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"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <netinet/in.h>
19 #include <resolv.h>
21 namespace mozilla::net {
23 #if defined(HAVE_RES_NINIT)
24 MOZ_THREAD_LOCAL(struct __res_state*) sThreadRes;
25 #endif
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) {
33 DNSPacket packet;
34 nsAutoCString host(aHost);
35 nsAutoCString cname;
36 nsresult rv;
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());
53 #endif
55 LOG("resolving %s\n", host.get());
56 // Perform the query
57 rv = packet.FillBuffer(
58 [&](unsigned char response[DNSPacket::MAX_SIZE]) -> int {
59 int len = 0;
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,
64 DNSPacket::MAX_SIZE);
65 #else
66 len =
67 res_query(host.get(), ns_c_in, nsIDNSService::RESOLVE_TYPE_HTTPSSVC,
68 response, DNSPacket::MAX_SIZE);
69 #endif
71 mozilla::glean::networking::dns_native_https_call_time
72 .AccumulateRawDuration(TimeStamp::Now() - startTime);
73 if (len < 0) {
74 LOG("DNS query failed");
76 return len;
77 });
78 if (NS_FAILED(rv)) {
79 return rv;
82 return ParseHTTPSRecord(host, packet, aResult, aTTL);
85 void DNSThreadShutdown() {
86 #if defined(HAVE_RES_NINIT)
87 auto* res = sThreadRes.get();
88 if (!res) {
89 return;
92 sThreadRes.set(nullptr);
93 res_nclose(res);
94 #endif
97 } // namespace mozilla::net