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)27 bool 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     uint64_t buildVersionSdk = android::base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
34     uint64_t buildVersionPreviewSdk =
35             android::base::GetUintProperty<uint64_t>("ro.build.version.preview_sdk", 0);
36     uint64_t firstApiLevel =
37             android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
38     using android::net::gApiLevel;
39     gApiLevel = std::max(buildVersionSdk + !!buildVersionPreviewSdk, firstApiLevel);
40     using android::net::gResNetdCallbacks;
41     gResNetdCallbacks.check_calling_permission = callbacks->check_calling_permission;
42     gResNetdCallbacks.get_network_context = callbacks->get_network_context;
43     gResNetdCallbacks.log = callbacks->log;
44     if (gApiLevel >= 30) {
45         gResNetdCallbacks.tagSocket = callbacks->tagSocket;
46         gResNetdCallbacks.evaluate_domain_name = callbacks->evaluate_domain_name;
47     }
48     android::net::gDnsResolv = android::net::DnsResolver::getInstance();
49     return android::net::gDnsResolv->start();
50 }
51 
52 namespace android {
53 namespace net {
54 
55 namespace {
56 
verifyCallbacks()57 bool verifyCallbacks() {
58     if (!(gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context &&
59           gResNetdCallbacks.log)) {
60         return false;
61     }
62     if (gApiLevel >= 30) {
63         return gResNetdCallbacks.tagSocket != nullptr;
64     }
65     return true;
66 }
67 
68 }  // namespace
69 
70 DnsResolver* gDnsResolv = nullptr;
71 ResolverNetdCallbacks gResNetdCallbacks;
72 netdutils::Log gDnsResolverLog("dnsResolver");
73 uint64_t gApiLevel = 0;
74 
getInstance()75 DnsResolver* DnsResolver::getInstance() {
76     // Instantiated on first use.
77     static DnsResolver instance;
78     return &instance;
79 }
80 
start()81 bool DnsResolver::start() {
82     if (!verifyCallbacks()) {
83         LOG(ERROR) << __func__ << ": Callback verification failed";
84         return false;
85     }
86     if (mDnsProxyListener.startListener()) {
87         PLOG(ERROR) << __func__ << ": Unable to start DnsProxyListener";
88         return false;
89     }
90     binder_status_t ret;
91     if ((ret = DnsResolverService::start()) != STATUS_OK) {
92         LOG(ERROR) << __func__ << ": Unable to start DnsResolverService: " << ret;
93         return false;
94     }
95     return true;
96 }
97 
setLogSeverity(int32_t logSeverity)98 int DnsResolver::setLogSeverity(int32_t logSeverity) {
99     return resolv_set_log_severity(logSeverity);
100 }
101 
102 }  // namespace net
103 }  // namespace android
104