blob: 5b2847de02c7090e92488c1ed269fda4b2f486d0 [file] [edit]
// Copyright 2025 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdint>
#include <memory>
#include "DnsProxy.h"
#include "DnsResolver.h"
#include "dns_proxy_cxx_bridge.rs.h"
#include "include/netd_resolv/resolv.h"
namespace android {
namespace net {
namespace dns_proxy_ffi {
// getDnsMark must be thread-safe since it is used in GetDnsMarkCallback.
uint32_t getDnsMark(uint32_t netId, uint32_t uid) {
android_net_context netContext;
// Safety: get_network_context is thread-safe since the implementation is
// behind a mutex lock.
gResNetdCallbacks.get_network_context(netId, uid, &netContext);
return netContext.dns_mark;
}
// getNameServers must be thread-safe since it is used in GetNameServersCallback.
std::unique_ptr<std::vector<std::string>> getNameServers(uint32_t netId) {
std::vector<std::string> res_servers;
std::vector<std::string> res_domains;
std::vector<std::string> res_tls_servers;
std::vector<std::string> res_interface_names;
std::vector<int32_t> params32;
std::vector<int32_t> stats32;
int32_t wait_for_pending_req_timeout_count32 = 0;
// Safety: getResolverInfo is thread-safe since in its implementation, mutex
// lock is applied when shared information is accessed.
gDnsResolv->resolverCtrl.getResolverInfo(netId, &res_servers, &res_domains, &res_tls_servers,
&res_interface_names, &params32, &stats32,
&wait_for_pending_req_timeout_count32);
return std::make_unique<std::vector<std::string>>(std::move(res_servers));
}
DnsProxy::DnsProxy(uint16_t port)
: mServer(ffi_proxy_server_new(std::make_unique<GetDnsMarkCallback>(getDnsMark),
std::make_unique<GetNameServersCallback>(getNameServers),
port)) {}
int DnsProxy::enable() {
return mServer->enable();
}
int DnsProxy::disable() {
return mServer->disable();
}
bool DnsProxy::isProxyEnabled() const {
return mServer->is_enabled();
}
int DnsProxy::configureDnsForwarding(uint32_t ifindex, uint32_t netid, uint32_t uid) {
return mServer->configure_forwarding(ifindex, netid, uid);
}
int DnsProxy::stopDnsForwarding(uint32_t ifindex) {
return mServer->stop_forwarding(ifindex);
}
#if defined(DNS_PROXY_FFI_TEST)
int DnsProxy::panicForTest() {
return mServer->panic_for_test();
}
#endif
} // namespace dns_proxy_ffi
} // namespace net
} // namespace android