blob: 4b39902eefb910bd2fb59d4ca27f99407b851488 [file] [log] [blame]
Luke Huang36796f32019-03-13 02:54:45 +08001/**
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 <resolv.h>
23#include "DnsProxyListener.h"
24#include "DnsResolverService.h"
25#include "resolv_private.h"
26
27bool resolv_init(const ResolverNetdCallbacks& callbacks) {
28 android::base::InitLogging(/*argv=*/nullptr);
29 android::base::SetDefaultTag("libnetd_resolv");
Ken Chenffc224a2019-03-19 17:41:28 +080030 LOG(INFO) << __func__ << "Initializing resolver";
Luke Huang36796f32019-03-13 02:54:45 +080031 const std::string logSeverityStr =
32 android::base::GetProperty("persist.sys.nw_dns_resolver_log", "WARNING");
33 android::base::SetMinimumLogSeverity(logSeverityStrToEnum(logSeverityStr));
34
35 android::net::gResNetdCallbacks = callbacks;
36 android::net::gDnsResolv = android::net::DnsResolver::getInstance();
37 return android::net::gDnsResolv->start();
38}
39
40namespace android {
41namespace net {
42
43namespace {
44
45bool verifyCallbacks() {
46 return gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context &&
Luke Huang9f8d8b72019-03-26 15:15:44 +080047 gResNetdCallbacks.log;
Luke Huang36796f32019-03-13 02:54:45 +080048}
49
50} // namespace
51
52DnsResolver* gDnsResolv = nullptr;
53ResolverNetdCallbacks gResNetdCallbacks;
54
55DnsResolver* DnsResolver::getInstance() {
56 // Instantiated on first use.
57 static DnsResolver instance;
58 return &instance;
59}
60
61bool DnsResolver::start() {
62 if (!verifyCallbacks()) {
Ken Chenffc224a2019-03-19 17:41:28 +080063 LOG(ERROR) << __func__ << "Callback verification failed";
Luke Huang36796f32019-03-13 02:54:45 +080064 return false;
65 }
66 if (mDnsProxyListener.startListener()) {
Ken Chenffc224a2019-03-19 17:41:28 +080067 PLOG(ERROR) << __func__ << "Unable to start DnsProxyListener";
Luke Huang36796f32019-03-13 02:54:45 +080068 return false;
69 }
70 binder_status_t ret;
71 if ((ret = DnsResolverService::start()) != STATUS_OK) {
Ken Chenffc224a2019-03-19 17:41:28 +080072 LOG(ERROR) << __func__ << "Unable to start DnsResolverService: " << ret;
Luke Huang36796f32019-03-13 02:54:45 +080073 return false;
74 }
75 return true;
76}
77
78} // namespace net
79} // namespace android