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 #define LOG_TAG "resolv"
18 
19 #include "DnsResolver.h"
20 
21 #include <android-base/logging.h>
22 
23 #include "DnsProxyListener.h"
24 #include "DnsResolverService.h"
25 #include "DnsTlsDispatcher.h"
26 #include "PrivateDnsConfiguration.h"
27 #include "netd_resolv/resolv.h"
28 #include "res_debug.h"
29 #include "util.h"
30 
resolv_init(const ResolverNetdCallbacks * callbacks)31 bool resolv_init(const ResolverNetdCallbacks* callbacks) {
32     android::base::InitLogging(/*argv=*/nullptr);
33     LOG(INFO) << __func__ << ": Initializing resolver";
34     const bool isDebug = isDebuggable();
35     resolv_set_log_severity(isDebug ? android::base::INFO : android::base::WARNING);
36     doh_init_logger(isDebug ? DOH_LOG_LEVEL_INFO : DOH_LOG_LEVEL_WARN);
37     using android::net::gApiLevel;
38     gApiLevel = getApiLevel();
39     using android::net::gResNetdCallbacks;
40     gResNetdCallbacks.check_calling_permission = callbacks->check_calling_permission;
41     gResNetdCallbacks.get_network_context = callbacks->get_network_context;
42     gResNetdCallbacks.log = callbacks->log;
43     if (gApiLevel >= 30) {
44         gResNetdCallbacks.tagSocket = callbacks->tagSocket;
45         gResNetdCallbacks.evaluate_domain_name = callbacks->evaluate_domain_name;
46     }
47     android::net::gDnsResolv = android::net::DnsResolver::getInstance();
48     return android::net::gDnsResolv->start();
49 }
50 
51 namespace android {
52 namespace net {
53 
54 namespace {
55 
verifyCallbacks()56 bool verifyCallbacks() {
57     if (!(gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context &&
58           gResNetdCallbacks.log)) {
59         return false;
60     }
61     if (gApiLevel >= 30) {
62         return gResNetdCallbacks.tagSocket != nullptr;
63     }
64     return true;
65 }
66 
67 }  // namespace
68 
69 DnsResolver* gDnsResolv = nullptr;
70 ResolverNetdCallbacks gResNetdCallbacks;
71 uint64_t gApiLevel = 0;
72 
getInstance()73 DnsResolver* DnsResolver::getInstance() {
74     // Instantiated on first use.
75     static DnsResolver instance;
76     return &instance;
77 }
78 
DnsResolver()79 DnsResolver::DnsResolver() {
80     // TODO: make them member variables after fixing the circular dependency:
81     //   DnsTlsDispatcher.h -> resolv_private.h -> DnsResolver.h -> DnsTlsDispatcher.h
82     auto& dnsTlsDispatcher = DnsTlsDispatcher::getInstance();
83     auto& privateDnsConfiguration = PrivateDnsConfiguration::getInstance();
84     privateDnsConfiguration.setObserver(&dnsTlsDispatcher);
85     privateDnsConfiguration.initDoh();
86 }
87 
start()88 bool DnsResolver::start() {
89     if (!verifyCallbacks()) {
90         LOG(ERROR) << __func__ << ": Callback verification failed";
91         return false;
92     }
93     if (mDnsProxyListener.startListener()) {
94         PLOG(ERROR) << __func__ << ": Unable to start DnsProxyListener";
95         return false;
96     }
97     binder_status_t ret;
98     if ((ret = DnsResolverService::start()) != STATUS_OK) {
99         LOG(ERROR) << __func__ << ": Unable to start DnsResolverService: " << ret;
100         return false;
101     }
102     return true;
103 }
104 
setLogSeverity(int32_t logSeverity)105 int DnsResolver::setLogSeverity(int32_t logSeverity) {
106     return resolv_set_log_severity(logSeverity);
107 }
108 
109 }  // namespace net
110 }  // namespace android
111