Bug 1866777 - Disable test_race_cache_with_network.js on windows opt for frequent...
[gecko.git] / netwerk / dns / PlatformDNSAndroid.cpp
blob01ff9ea49d820bfb678623695a9bb7dd79f1bb71
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 (!sLibLoading.exchange(true)) {
47 // We're the first call here, load the library and symbols.
48 void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
49 if (!handle) {
50 LOG("Error loading libandroid_net %s", dlerror());
51 return NS_ERROR_UNKNOWN_HOST;
54 auto x = dlsym(handle, "android_res_nquery");
55 if (!x) {
56 LOG("No android_res_nquery symbol");
59 sAndroidResNQuery = (android_res_nquery_ptr)x;
62 if (!sAndroidResNQuery) {
63 LOG("nquery not loaded");
64 // The library hasn't been loaded yet.
65 return NS_ERROR_UNKNOWN_HOST;
68 LOG("resolving %s\n", host.get());
69 // Perform the query
70 rv = packet.FillBuffer(
71 [&](unsigned char response[DNSPacket::MAX_SIZE]) -> int {
72 int fd = 0;
73 auto closeSocket = MakeScopeExit([&] {
74 if (fd > 0) {
75 close(fd);
77 });
78 uint32_t flags = 0;
79 if (aFlags & nsIDNSService::RESOLVE_BYPASS_CACHE) {
80 flags = ANDROID_RESOLV_NO_CACHE_LOOKUP;
82 fd = sAndroidResNQuery(0, host.get(), ns_c_in,
83 nsIDNSService::RESOLVE_TYPE_HTTPSSVC, flags);
85 if (fd < 0) {
86 LOG("DNS query failed");
87 return fd;
90 struct pollfd fds;
91 fds.fd = fd;
92 fds.events = POLLIN; // Wait for read events
94 // Wait for an event on the file descriptor
95 int ret = poll(&fds, 1,
96 StaticPrefs::network_dns_native_https_timeout_android());
97 if (ret <= 0) {
98 LOG("poll failed %d", ret);
99 return -1;
102 ssize_t len = recv(fd, response, DNSPacket::MAX_SIZE - 1, 0);
103 if (len <= 8) {
104 LOG("size too small %zd", len);
105 return len < 0 ? len : -1;
108 // The first 8 bytes are UDP header.
109 // XXX: we should consider avoiding this move somehow.
110 for (int i = 0; i < len - 8; i++) {
111 response[i] = response[i + 8];
114 return len - 8;
116 if (NS_FAILED(rv)) {
117 LOG("failed rv");
118 return rv;
120 packet.SetNativePacket(true);
122 int32_t loopCount = 64;
123 while (loopCount > 0 && aResult.is<Nothing>()) {
124 loopCount--;
125 DOHresp resp;
126 nsClassHashtable<nsCStringHashKey, DOHresp> additionalRecords;
127 rv = packet.Decode(host, TRRTYPE_HTTPSSVC, cname, true, resp, aResult,
128 additionalRecords, aTTL);
129 if (NS_FAILED(rv)) {
130 LOG("Decode failed %x", static_cast<uint32_t>(rv));
131 return rv;
133 if (!cname.IsEmpty() && aResult.is<Nothing>()) {
134 host = cname;
135 cname.Truncate();
136 continue;
140 if (aResult.is<Nothing>()) {
141 LOG("Result is nothing");
142 // The call succeeded, but no HTTPS records were found.
143 return NS_ERROR_UNKNOWN_HOST;
146 return NS_OK;
149 } // namespace mozilla::net