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 "dns_resolv_config.h"
17 
18 namespace OHOS::nmd {
DelayedTaskWrapper(std::string hostName,NetManagerStandard::LRUCache<AddrInfo> & cache)19 DnsResolvConfig::DelayedTaskWrapper::DelayedTaskWrapper(std::string hostName,
20                                                         NetManagerStandard::LRUCache<AddrInfo> &cache)
21     : hostName_(std::move(hostName)), cache_(cache)
22 {
23 }
24 
Execute() const25 void DnsResolvConfig::DelayedTaskWrapper::Execute() const
26 {
27     cache_.Delete(hostName_);
28 }
29 
operator <(const DelayedTaskWrapper & other) const30 bool DnsResolvConfig::DelayedTaskWrapper::operator<(const DelayedTaskWrapper &other) const
31 {
32     return hostName_ < other.hostName_;
33 }
34 
DnsResolvConfig()35 DnsResolvConfig::DnsResolvConfig()
36     : netId_(0), netIdIsSet_(false), revisionId_(0), timeoutMsec_(0), retryCount_(0), isIpv6Enable_(false)
37 {
38 }
39 
EnableIpv6()40 void DnsResolvConfig::EnableIpv6()
41 {
42     isIpv6Enable_ = true;
43 }
44 
IsIpv6Enable()45 bool DnsResolvConfig::IsIpv6Enable()
46 {
47     return isIpv6Enable_;
48 }
49 
SetNetId(uint16_t netId)50 void DnsResolvConfig::SetNetId(uint16_t netId)
51 {
52     if (netIdIsSet_) {
53         return;
54     }
55     netIdIsSet_ = true;
56     netId_ = netId;
57 }
58 
GetNetId() const59 uint16_t DnsResolvConfig::GetNetId() const
60 {
61     return netId_;
62 }
63 
SetTimeoutMsec(int32_t baseTimeoutMsec)64 void DnsResolvConfig::SetTimeoutMsec(int32_t baseTimeoutMsec)
65 {
66     timeoutMsec_ = baseTimeoutMsec;
67 }
68 
GetTimeoutMsec() const69 uint16_t DnsResolvConfig::GetTimeoutMsec() const
70 {
71     return timeoutMsec_;
72 }
73 
SetRetryCount(uint8_t retryCount)74 void DnsResolvConfig::SetRetryCount(uint8_t retryCount)
75 {
76     retryCount_ = retryCount;
77 }
78 
GetRetryCount() const79 uint8_t DnsResolvConfig::GetRetryCount() const
80 {
81     return retryCount_;
82 }
83 
SetServers(const std::vector<std::string> & servers)84 void DnsResolvConfig::SetServers(const std::vector<std::string> &servers)
85 {
86     nameServers_ = servers;
87     revisionId_++;
88 }
89 
GetServers() const90 std::vector<std::string> DnsResolvConfig::GetServers() const
91 {
92     return nameServers_;
93 }
94 
SetDomains(const std::vector<std::string> & domains)95 void DnsResolvConfig::SetDomains(const std::vector<std::string> &domains)
96 {
97     searchDomains_ = domains;
98 }
99 
GetDomains() const100 std::vector<std::string> DnsResolvConfig::GetDomains() const
101 {
102     return searchDomains_;
103 }
104 
GetCache()105 NetManagerStandard::LRUCache<AddrInfo> &DnsResolvConfig::GetCache()
106 {
107     return cache_;
108 }
109 
SetCacheDelayed(const std::string & hostName)110 void DnsResolvConfig::SetCacheDelayed(const std::string &hostName)
111 {
112     auto wrapper = std::make_shared<DelayedTaskWrapper>(hostName, cache_);
113     delayedQueue_.Put(wrapper);
114 }
115 } // namespace OHOS::nmd
116