• 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();
38     ~DnsParamCache() = default;
39 
40     // for net_conn_service
41     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
42                               const std::vector<std::string> &servers, const std::vector<std::string> &domains);
43 
44     int32_t CreateCacheForNet(uint16_t netId);
45 
46     void SetDefaultNetwork(uint16_t netId);
47 
48     // for client
49     void SetDnsCache(uint16_t netId, const std::string &hostName, const AddrInfo &addrInfo);
50 
51     void SetCacheDelayed(uint16_t netId, const std::string &hostName);
52 
53     std::vector<AddrInfo> GetDnsCache(uint16_t netId, const std::string &hostName);
54 
55     int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
56                               uint16_t &baseTimeoutMsec, uint8_t &retryCount);
57 
58     int32_t GetDefaultNetwork() const;
59 
60     void GetDumpInfo(std::string &info);
61 
62     int32_t DestroyNetworkCache(uint16_t netId);
63 
64 private:
65     std::mutex cacheMutex_;
66 
67     std::atomic_uint defaultNetId_;
68 
69     std::map<uint16_t, DnsResolvConfig> serverConfigMap_;
70 
71     static std::vector<std::string> SelectNameservers(const std::vector<std::string> &servers);
72 };
73 } // namespace OHOS::nmd
74 #endif // NETSYS_DNS_PARAM_CACHE_H
75