1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <thread>
17
18 #include "dns_resolv_listen.h"
19 #include "netnative_log_wrapper.h"
20 #include "singleton.h"
21
22 #include "dns_manager.h"
23
24 namespace OHOS {
25 namespace nmd {
StartListen()26 void StartListen()
27 {
28 NETNATIVE_LOG_D("Enter threadStart");
29 DnsResolvListen().StartListen();
30 }
31
DnsManager()32 DnsManager::DnsManager() : dnsProxyListen_(std::make_shared<DnsProxyListen>())
33 {
34 std::thread(StartListen).detach();
35 }
36
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMillis,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)37 int32_t DnsManager::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMillis, uint8_t retryCount,
38 const std::vector<std::string> &servers, const std::vector<std::string> &domains)
39 {
40 NETNATIVE_LOG_D("manager_SetResolverConfig netId[%{public}d]", netId);
41 return DelayedSingleton<DnsParamCache>::GetInstance()->SetResolverConfig(netId, baseTimeoutMillis, retryCount,
42 servers, domains);
43 }
44
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMillis,uint8_t & retryCount)45 int32_t DnsManager::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
46 std::vector<std::string> &domains, uint16_t &baseTimeoutMillis,
47 uint8_t &retryCount)
48 {
49 NETNATIVE_LOG_D("manager_GetResolverConfig netId[%{public}d]", netId);
50 return DelayedSingleton<DnsParamCache>::GetInstance()->GetResolverConfig(netId, servers, domains, baseTimeoutMillis,
51 retryCount);
52 }
53
CreateNetworkCache(uint16_t netId)54 int32_t DnsManager::CreateNetworkCache(uint16_t netId)
55 {
56 NETNATIVE_LOG_D("manager_CreateNetworkCache netId[%{public}d]", netId);
57 return DelayedSingleton<DnsParamCache>::GetInstance()->CreateCacheForNet(netId);
58 }
59
DestroyNetworkCache(uint16_t netId)60 int32_t DnsManager::DestroyNetworkCache(uint16_t netId)
61 {
62 return DelayedSingleton<DnsParamCache>::GetInstance()->DestroyNetworkCache(netId);
63 }
64
SetDefaultNetwork(uint16_t netId)65 void DnsManager::SetDefaultNetwork(uint16_t netId)
66 {
67 DelayedSingleton<DnsParamCache>::GetInstance()->SetDefaultNetwork(netId);
68 }
69
StartProxyListen()70 void StartProxyListen()
71 {
72 NETNATIVE_LOG_D("begin StartProxyListen");
73 DnsProxyListen().StartListen();
74 }
75
ShareDnsSet(uint16_t netId)76 void DnsManager::ShareDnsSet(uint16_t netId)
77 {
78 dnsProxyListen_->SetParseNetId(netId);
79 }
80
StartDnsProxyListen()81 void DnsManager::StartDnsProxyListen()
82 {
83 dnsProxyListen_->OnListen();
84 std::thread(StartProxyListen).detach();
85 }
86
StopDnsProxyListen()87 void DnsManager::StopDnsProxyListen()
88 {
89 dnsProxyListen_->OffListen();
90 }
91
GetDumpInfo(std::string & info)92 void DnsManager::GetDumpInfo(std::string &info)
93 {
94 NETNATIVE_LOG_D("Get dump info");
95 DelayedSingleton<DnsParamCache>::GetInstance()->GetDumpInfo(info);
96 }
97
98
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)99 int32_t DnsManager::GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
100 uint16_t netId, std::vector<AddrInfo> &res)
101 {
102 if (netId == 0) {
103 netId = DelayedSingleton<DnsParamCache>::GetInstance()->GetDefaultNetwork();
104 NETNATIVE_LOG_D("DnsManager DnsGetaddrinfo netId == 0 defaultNetId_ : %{public}d", netId);
105 }
106 return dnsGetAddrInfo_->GetAddrInfo(hostName, serverName, hints, netId, res);
107 }
108 } // namespace nmd
109 } // namespace OHOS
110