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 void SetUserDefinedServerFlag(bool flag); 56 57 bool IsUserDefinedServer(); 58 59 private: 60 class DelayedTaskWrapper { 61 public: 62 DelayedTaskWrapper(std::string hostName, NetManagerStandard::LRUCache<AddrInfo> &cache); 63 64 void Execute() const; 65 66 bool operator<(const DelayedTaskWrapper &other) const; 67 68 private: 69 std::string hostName_; 70 71 NetManagerStandard::LRUCache<AddrInfo> &cache_; 72 }; 73 74 uint16_t netId_; 75 std::atomic_bool netIdIsSet_; 76 int32_t revisionId_; 77 int32_t timeoutMsec_; 78 uint8_t retryCount_; 79 std::list<std::shared_ptr<DelayedTaskWrapper>> delayedTaskWrapperList_; 80 std::vector<std::string> nameServers_; 81 std::vector<std::string> searchDomains_; 82 NetManagerStandard::LRUCache<AddrInfo> cache_; 83 NetManagerStandard::DelayedQueue<DelayedTaskWrapper, NetManagerStandard::DEFAULT_CAPABILITY, DEFAULT_DELAYED_COUNT> 84 delayedQueue_; 85 bool isIpv6Enable_; 86 bool isUserDefinedDnsServer_; 87 }; 88 } // namespace OHOS::nmd 89 #endif // INCLUDE_DNSRESOLV_CONFIG_H 90