Bug 1844673 [wpt PR 41130] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / dns / PlatformDNSAndroid.cpp
blob171797b93820261f88f033a973938108cbf7cbec
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/net/DNSPacket.h"
9 #include "nsIDNSService.h"
10 #include "mozilla/Maybe.h"
11 #include "mozilla/Atomics.h"
12 #include "mozilla/StaticPrefs_network.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <netinet/in.h>
18 #include <resolv.h>
19 #include <poll.h>
20 #include <dlfcn.h>
21 #include <android/multinetwork.h>
23 namespace mozilla::net {
25 // The first call to ResolveHTTPSRecordImpl will load the library
26 // and function pointers.
27 static Atomic<bool> sLibLoading{false};
29 // https://developer.android.com/ndk/reference/group/networking#android_res_nquery
30 // The function android_res_nquery is defined in <android/multinetwork.h>
31 typedef int (*android_res_nquery_ptr)(net_handle_t network, const char* dname,
32 int ns_class, int ns_type,
33 uint32_t flags);
34 static Atomic<android_res_nquery_ptr> sAndroidResNQuery;
36 #define LOG(msg, ...) \
37 MOZ_LOG(gGetAddrInfoLog, LogLevel::Debug, ("[DNS]: " msg, ##__VA_ARGS__))
39 nsresult ResolveHTTPSRecordImpl(const nsACString& aHost, uint16_t aFlags,
40 TypeRecordResultType& aResult, uint32_t& aTTL) {
41 DNSPacket packet;
42 nsAutoCString host(aHost);
43 nsAutoCString cname;
44 nsresult rv;
46 if (xpc::IsInAutomation() &&
47 !StaticPrefs::network_dns_native_https_query_in_automation()) {
48 return NS_ERROR_UNKNOWN_HOST;
51 if (!sLibLoading.exchange(true)) {
52 // We're the first call here, load the library and symbols.
53 void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
54 if (!handle) {
55 LOG("Error loading libandroid_net %s", dlerror());
56 return NS_ERROR_UNKNOWN_HOST;
59 auto x = dlsym(handle, "android_res_nquery");
60 if (!x) {
61 LOG("No android_res_nquery symbol");
64 sAndroidResNQuery = (android_res_nquery_ptr)x;
67 if (!sAndroidResNQuery) {
68 LOG("nquery not loaded");
69 // The library hasn't been loaded yet.
70 return NS_ERROR_UNKNOWN_HOST;
73 LOG("resolving %s\n", host.get());
74 // Perform the query
75 rv = packet.FillBuffer(
76 [&](unsigned char response[DNSPacket::MAX_SIZE]) -> int {
77 int fd = 0;
78 auto closeSocket = MakeScopeExit([&] {
79 if (fd > 0) {
80 close(fd);
82 });
83 uint32_t flags = 0;
84 if (aFlags & nsIDNSService::RESOLVE_BYPASS_CACHE) {
85 flags = ANDROID_RESOLV_NO_CACHE_LOOKUP;
87 fd = sAndroidResNQuery(0, host.get(), ns_c_in,
88 nsIDNSService::RESOLVE_TYPE_HTTPSSVC, flags);
90 if (fd < 0) {
91 LOG("DNS query failed");
92 return fd;
95 struct pollfd fds;
96 fds.fd = fd;
97 fds.events = POLLIN; // Wait for read events
99 // Wait for an event on the file descriptor
100 int ret = poll(&fds, 1,
101 StaticPrefs::network_dns_native_https_timeout_android());
102 if (ret <= 0) {
103 LOG("poll failed %d", ret);
104 return -1;
107 ssize_t len = recv(fd, response, DNSPacket::MAX_SIZE - 1, 0);
108 if (len <= 8) {
109 LOG("size too small %zd", len);
110 return len < 0 ? len : -1;
113 // The first 8 bytes are UDP header.
114 // XXX: we should consider avoiding this move somehow.
115 for (int i = 0; i < len - 8; i++) {
116 response[i] = response[i + 8];
119 return len - 8;
121 if (NS_FAILED(rv)) {
122 LOG("failed rv");
123 return rv;
125 packet.SetNativePacket(true);
127 int32_t loopCount = 64;
128 while (loopCount > 0 && aResult.is<Nothing>()) {
129 loopCount--;
130 DOHresp resp;
131 nsClassHashtable<nsCStringHashKey, DOHresp> additionalRecords;
132 rv = packet.Decode(host, TRRTYPE_HTTPSSVC, cname, true, resp, aResult,
133 additionalRecords, aTTL);
134 if (NS_FAILED(rv)) {
135 LOG("Decode failed %x", static_cast<uint32_t>(rv));
136 return rv;
138 if (!cname.IsEmpty() && aResult.is<Nothing>()) {
139 host = cname;
140 cname.Truncate();
141 continue;
145 if (aResult.is<Nothing>()) {
146 LOG("Result is nothing");
147 // The call succeeded, but no HTTPS records were found.
148 return NS_ERROR_UNKNOWN_HOST;
151 return NS_OK;
154 void DNSThreadShutdown() {}
156 } // namespace mozilla::net