1 /** 2 * Copyright (c) 2019, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "DnsResolver.h" 18 19 #include <android-base/logging.h> 20 21 #include "DnsProxyListener.h" 22 #include "DnsResolverService.h" 23 #include "DnsTlsDispatcher.h" 24 #include "PrivateDnsConfiguration.h" 25 #include "netd_resolv/resolv.h" 26 #include "res_debug.h" 27 #include "util.h" 28 resolv_init(const ResolverNetdCallbacks * callbacks)29bool resolv_init(const ResolverNetdCallbacks* callbacks) { 30 android::base::InitLogging(/*argv=*/nullptr); 31 android::base::SetDefaultTag("libnetd_resolv"); 32 LOG(INFO) << __func__ << ": Initializing resolver"; 33 // TODO(b/170539625): restore log level to WARNING after clarifying flaky tests. 34 resolv_set_log_severity(isUserDebugBuild() ? android::base::DEBUG : android::base::WARNING); 35 using android::net::gApiLevel; 36 gApiLevel = getApiLevel(); 37 using android::net::gResNetdCallbacks; 38 gResNetdCallbacks.check_calling_permission = callbacks->check_calling_permission; 39 gResNetdCallbacks.get_network_context = callbacks->get_network_context; 40 gResNetdCallbacks.log = callbacks->log; 41 if (gApiLevel >= 30) { 42 gResNetdCallbacks.tagSocket = callbacks->tagSocket; 43 gResNetdCallbacks.evaluate_domain_name = callbacks->evaluate_domain_name; 44 } 45 android::net::gDnsResolv = android::net::DnsResolver::getInstance(); 46 return android::net::gDnsResolv->start(); 47 } 48 49 namespace android { 50 namespace net { 51 52 namespace { 53 verifyCallbacks()54bool verifyCallbacks() { 55 if (!(gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context && 56 gResNetdCallbacks.log)) { 57 return false; 58 } 59 if (gApiLevel >= 30) { 60 return gResNetdCallbacks.tagSocket != nullptr; 61 } 62 return true; 63 } 64 65 } // namespace 66 67 DnsResolver* gDnsResolv = nullptr; 68 ResolverNetdCallbacks gResNetdCallbacks; 69 netdutils::Log gDnsResolverLog("dnsResolver"); 70 uint64_t gApiLevel = 0; 71 getInstance()72DnsResolver* DnsResolver::getInstance() { 73 // Instantiated on first use. 74 static DnsResolver instance; 75 return &instance; 76 } 77 DnsResolver()78DnsResolver::DnsResolver() { 79 // TODO: make them member variables after fixing the circular dependency: 80 // DnsTlsDispatcher.h -> resolv_private.h -> DnsResolver.h -> DnsTlsDispatcher.h 81 auto& dnsTlsDispatcher = DnsTlsDispatcher::getInstance(); 82 auto& privateDnsConfiguration = PrivateDnsConfiguration::getInstance(); 83 privateDnsConfiguration.setObserver(&dnsTlsDispatcher); 84 } 85 start()86bool DnsResolver::start() { 87 if (!verifyCallbacks()) { 88 LOG(ERROR) << __func__ << ": Callback verification failed"; 89 return false; 90 } 91 if (mDnsProxyListener.startListener()) { 92 PLOG(ERROR) << __func__ << ": Unable to start DnsProxyListener"; 93 return false; 94 } 95 binder_status_t ret; 96 if ((ret = DnsResolverService::start()) != STATUS_OK) { 97 LOG(ERROR) << __func__ << ": Unable to start DnsResolverService: " << ret; 98 return false; 99 } 100 return true; 101 } 102 setLogSeverity(int32_t logSeverity)103int DnsResolver::setLogSeverity(int32_t logSeverity) { 104 return resolv_set_log_severity(logSeverity); 105 } 106 107 } // namespace net 108 } // namespace android 109