Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 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 <resolv.h> |
| 23 | #include "DnsProxyListener.h" |
| 24 | #include "DnsResolverService.h" |
| 25 | #include "resolv_private.h" |
| 26 | |
| 27 | bool resolv_init(const ResolverNetdCallbacks& callbacks) { |
| 28 | android::base::InitLogging(/*argv=*/nullptr); |
| 29 | android::base::SetDefaultTag("libnetd_resolv"); |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 30 | LOG(INFO) << __func__ << "Initializing resolver"; |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 31 | 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 | |
| 40 | namespace android { |
| 41 | namespace net { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | bool verifyCallbacks() { |
| 46 | return gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context && |
Luke Huang | 9f8d8b7 | 2019-03-26 15:15:44 +0800 | [diff] [blame^] | 47 | gResNetdCallbacks.log; |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | DnsResolver* gDnsResolv = nullptr; |
| 53 | ResolverNetdCallbacks gResNetdCallbacks; |
| 54 | |
| 55 | DnsResolver* DnsResolver::getInstance() { |
| 56 | // Instantiated on first use. |
| 57 | static DnsResolver instance; |
| 58 | return &instance; |
| 59 | } |
| 60 | |
| 61 | bool DnsResolver::start() { |
| 62 | if (!verifyCallbacks()) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 63 | LOG(ERROR) << __func__ << "Callback verification failed"; |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | if (mDnsProxyListener.startListener()) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 67 | PLOG(ERROR) << __func__ << "Unable to start DnsProxyListener"; |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | binder_status_t ret; |
| 71 | if ((ret = DnsResolverService::start()) != STATUS_OK) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 72 | LOG(ERROR) << __func__ << "Unable to start DnsResolverService: " << ret; |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | } // namespace net |
| 79 | } // namespace android |