Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / dns / PlatformDNSUnix.cpp
blob8a328f3da51e75a7fba1b881366a11555fe35e34
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"
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, uint16_t aFlags,
31 TypeRecordResultType& aResult, uint32_t& aTTL) {
32 DNSPacket packet;
33 nsAutoCString host(aHost);
34 nsAutoCString cname;
35 nsresult rv;
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());
52 #endif
54 LOG("resolving %s\n", host.get());
55 // Perform the query
56 rv = packet.FillBuffer(
57 [&](unsigned char response[DNSPacket::MAX_SIZE]) -> int {
58 int len = 0;
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,
63 DNSPacket::MAX_SIZE);
64 #else
65 len =
66 res_query(host.get(), ns_c_in, nsIDNSService::RESOLVE_TYPE_HTTPSSVC,
67 response, DNSPacket::MAX_SIZE);
68 #endif
70 mozilla::glean::networking::dns_native_https_call_time
71 .AccumulateRawDuration(TimeStamp::Now() - startTime);
72 if (len < 0) {
73 LOG("DNS query failed");
75 return len;
76 });
77 if (NS_FAILED(rv)) {
78 return rv;
81 return ParseHTTPSRecord(host, packet, aResult, aTTL);
84 void DNSThreadShutdown() {
85 #if defined(HAVE_RES_NINIT)
86 auto* res = sThreadRes.get();
87 if (!res) {
88 return;
91 sThreadRes.set(nullptr);
92 res_nclose(res);
93 #endif
96 } // namespace mozilla::net