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 #ifndef INCLUDE_DNSRESOLV_CONFIG_H
17 #define INCLUDE_DNSRESOLV_CONFIG_H
18 
19 #include <atomic>
20 #include <list>
21 #include <memory>
22 #include <vector>
23 
24 #include "delayed_queue.h"
25 #include "dns_config_client.h"
26 #include "lru_cache.h"
27 
28 namespace OHOS::nmd {
29 static constexpr size_t DEFAULT_DELAYED_COUNT = 30;
30 
31 class DnsResolvConfig {
32 public:
33     DnsResolvConfig();
34 
35     void SetNetId(uint16_t netId);
36     void SetTimeoutMsec(int32_t baseTimeoutMsec);
37     void SetRetryCount(uint8_t retryCount);
38     void SetServers(const std::vector<std::string> &servers);
39     void SetDomains(const std::vector<std::string> &domains);
40 
41     uint16_t GetNetId() const;
42     uint16_t GetTimeoutMsec() const;
43     std::vector<std::string> GetServers() const;
44     std::vector<std::string> GetDomains() const;
45     uint8_t GetRetryCount() const;
46 
47     NetManagerStandard::LRUCache<AddrInfo> &GetCache();
48 
49     void SetCacheDelayed(const std::string &hostName);
50 
51     bool IsIpv6Enable();
52 
53     void EnableIpv6();
54 
55 private:
56     class DelayedTaskWrapper {
57     public:
58         DelayedTaskWrapper(std::string hostName, NetManagerStandard::LRUCache<AddrInfo> &cache);
59 
60         void Execute() const;
61 
62         bool operator<(const DelayedTaskWrapper &other) const;
63 
64     private:
65         std::string hostName_;
66 
67         NetManagerStandard::LRUCache<AddrInfo> &cache_;
68     };
69 
70     uint16_t netId_;
71     std::atomic_bool netIdIsSet_;
72     int32_t revisionId_;
73     int32_t timeoutMsec_;
74     uint8_t retryCount_;
75     std::list<std::shared_ptr<DelayedTaskWrapper>> delayedTaskWrapperList_;
76     std::vector<std::string> nameServers_;
77     std::vector<std::string> searchDomains_;
78     NetManagerStandard::LRUCache<AddrInfo> cache_;
79     NetManagerStandard::DelayedQueue<DelayedTaskWrapper, NetManagerStandard::DEFAULT_CAPABILITY, DEFAULT_DELAYED_COUNT>
80         delayedQueue_;
81     bool isIpv6Enable_;
82 };
83 } // namespace OHOS::nmd
84 #endif // INCLUDE_DNSRESOLV_CONFIG_H
85