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