• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NETSYS_DNS_PARAM_CACHE_H
17 #define NETSYS_DNS_PARAM_CACHE_H
18 
19 #include <iostream>
20 #include <map>
21 
22 #include "dns_resolv_config.h"
23 #include "netnative_log_wrapper.h"
24 
25 #if DNS_CONFIG_DEBUG
26 #ifdef DNS_CONFIG_PRINT
27 #undef DNS_CONFIG_PRINT
28 #endif
29 #define DNS_CONFIG_PRINT(fmt, ...) NETNATIVE_LOGI("DNS" fmt, ##__VA_ARGS__)
30 #else
31 #define DNS_CONFIG_PRINT(fmt, ...)
32 #endif
33 
34 namespace OHOS::nmd {
35 class DnsParamCache {
36 public:
37     ~DnsParamCache() = default;
38 
39     static DnsParamCache &GetInstance();
40 
41     // for net_conn_service
42     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
43                               const std::vector<std::string> &servers, const std::vector<std::string> &domains);
44 
45     int32_t CreateCacheForNet(uint16_t netId);
46 
47     void SetDefaultNetwork(uint16_t netId);
48 
49     // for client
50     void SetDnsCache(uint16_t netId, const std::string &hostName, const AddrInfo &addrInfo);
51 
52     void SetCacheDelayed(uint16_t netId, const std::string &hostName);
53 
54     std::vector<AddrInfo> GetDnsCache(uint16_t netId, const std::string &hostName);
55 
56     int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
57                               uint16_t &baseTimeoutMsec, uint8_t &retryCount);
58 
59     int32_t GetDefaultNetwork() const;
60 
61     void GetDumpInfo(std::string &info);
62 
63     int32_t DestroyNetworkCache(uint16_t netId);
64 
65     bool IsIpv6Enable(uint16_t netId);
66 
67     void EnableIpv6(uint16_t netId);
68 
69 private:
70     DnsParamCache();
71 
72     std::mutex cacheMutex_;
73 
74     std::atomic_uint defaultNetId_;
75 
76     std::map<uint16_t, DnsResolvConfig> serverConfigMap_;
77 
78     static std::vector<std::string> SelectNameservers(const std::vector<std::string> &servers);
79 };
80 } // namespace OHOS::nmd
81 #endif // NETSYS_DNS_PARAM_CACHE_H
82