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 #include <android-base/properties.h> 21 22 #include "DnsProxyListener.h" 23 #include "DnsResolverService.h" 24 #include "netd_resolv/resolv.h" 25 #include "res_debug.h" 26 resolv_init(const ResolverNetdCallbacks * callbacks)27bool resolv_init(const ResolverNetdCallbacks* callbacks) { 28 android::base::InitLogging(/*argv=*/nullptr); 29 android::base::SetDefaultTag("libnetd_resolv"); 30 LOG(INFO) << __func__ << ": Initializing resolver"; 31 resolv_set_log_severity(android::base::WARNING); 32 33 android::net::gResNetdCallbacks = *callbacks; 34 android::net::gDnsResolv = android::net::DnsResolver::getInstance(); 35 return android::net::gDnsResolv->start(); 36 } 37 38 namespace android { 39 namespace net { 40 41 namespace { 42 verifyCallbacks()43bool verifyCallbacks() { 44 return gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context && 45 gResNetdCallbacks.log; 46 } 47 48 } // namespace 49 50 DnsResolver* gDnsResolv = nullptr; 51 ResolverNetdCallbacks gResNetdCallbacks; 52 netdutils::Log gDnsResolverLog("dnsResolver"); 53 getInstance()54DnsResolver* DnsResolver::getInstance() { 55 // Instantiated on first use. 56 static DnsResolver instance; 57 return &instance; 58 } 59 start()60bool DnsResolver::start() { 61 if (!verifyCallbacks()) { 62 LOG(ERROR) << __func__ << ": Callback verification failed"; 63 return false; 64 } 65 if (mDnsProxyListener.startListener()) { 66 PLOG(ERROR) << __func__ << ": Unable to start DnsProxyListener"; 67 return false; 68 } 69 binder_status_t ret; 70 if ((ret = DnsResolverService::start()) != STATUS_OK) { 71 LOG(ERROR) << __func__ << ": Unable to start DnsResolverService: " << ret; 72 return false; 73 } 74 return true; 75 } 76 setLogSeverity(int32_t logSeverity)77int DnsResolver::setLogSeverity(int32_t logSeverity) { 78 return resolv_set_log_severity(logSeverity); 79 } 80 81 } // namespace net 82 } // namespace android 83