1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef INCLUDE_MANAGER_DNS_MANAGER_H
17b1b8bc3fSopenharmony_ci#define INCLUDE_MANAGER_DNS_MANAGER_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include <vector>
20b1b8bc3fSopenharmony_ci
21b1b8bc3fSopenharmony_ci#include "dns_param_cache.h"
22b1b8bc3fSopenharmony_ci#include "dns_proxy_listen.h"
23b1b8bc3fSopenharmony_ci#include "i_net_dns_result_callback.h"
24b1b8bc3fSopenharmony_ci#include "i_net_dns_health_callback.h"
25b1b8bc3fSopenharmony_ci#include "uid_range.h"
26b1b8bc3fSopenharmony_ci
27b1b8bc3fSopenharmony_cinamespace OHOS {
28b1b8bc3fSopenharmony_cinamespace nmd {
29b1b8bc3fSopenharmony_ciclass DnsManager {
30b1b8bc3fSopenharmony_cipublic:
31b1b8bc3fSopenharmony_ci    DnsManager();
32b1b8bc3fSopenharmony_ci    ~DnsManager() = default;
33b1b8bc3fSopenharmony_ci
34b1b8bc3fSopenharmony_ci    /**
35b1b8bc3fSopenharmony_ci     * Set the Resolver Config object
36b1b8bc3fSopenharmony_ci     *
37b1b8bc3fSopenharmony_ci     * @param netId network ID
38b1b8bc3fSopenharmony_ci     * @param baseTimeoutMillis base Timeout Ms, default 5000
39b1b8bc3fSopenharmony_ci     * @param retryCount retry Count, default 2
40b1b8bc3fSopenharmony_ci     * @param servers server name set in config
41b1b8bc3fSopenharmony_ci     * @param domains domain set in config
42b1b8bc3fSopenharmony_ci     * @return int32_t 0:success -1:failed
43b1b8bc3fSopenharmony_ci     */
44b1b8bc3fSopenharmony_ci    int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMillis, uint8_t retryCount,
45b1b8bc3fSopenharmony_ci                              const std::vector<std::string> &servers, const std::vector<std::string> &domains);
46b1b8bc3fSopenharmony_ci
47b1b8bc3fSopenharmony_ci    /**
48b1b8bc3fSopenharmony_ci     * Get the Resolver Config object
49b1b8bc3fSopenharmony_ci     *
50b1b8bc3fSopenharmony_ci     * @param netId network ID
51b1b8bc3fSopenharmony_ci     * @param servers return value server name
52b1b8bc3fSopenharmony_ci     * @param domains return value doamin
53b1b8bc3fSopenharmony_ci     * @param baseTimeoutMillis return value Timeout Ms
54b1b8bc3fSopenharmony_ci     * @param retryCount return value retry Count
55b1b8bc3fSopenharmony_ci     * @return int32_t 0:success -1:failed
56b1b8bc3fSopenharmony_ci     */
57b1b8bc3fSopenharmony_ci    int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
58b1b8bc3fSopenharmony_ci                              uint16_t &baseTimeoutMillis, uint8_t &retryCount);
59b1b8bc3fSopenharmony_ci
60b1b8bc3fSopenharmony_ci    /**
61b1b8bc3fSopenharmony_ci     * Create a Network Cache object
62b1b8bc3fSopenharmony_ci     *
63b1b8bc3fSopenharmony_ci     * @param netId network ID
64b1b8bc3fSopenharmony_ci     * @return int32_t 0:success -1:failed
65b1b8bc3fSopenharmony_ci     */
66b1b8bc3fSopenharmony_ci    int32_t CreateNetworkCache(uint16_t netId);
67b1b8bc3fSopenharmony_ci
68b1b8bc3fSopenharmony_ci    /**
69b1b8bc3fSopenharmony_ci     * Set the Default Network object
70b1b8bc3fSopenharmony_ci     *
71b1b8bc3fSopenharmony_ci     * @param netId network ID
72b1b8bc3fSopenharmony_ci     */
73b1b8bc3fSopenharmony_ci    void SetDefaultNetwork(uint16_t netId);
74b1b8bc3fSopenharmony_ci
75b1b8bc3fSopenharmony_ci    /**
76b1b8bc3fSopenharmony_ci     * Network share set netId
77b1b8bc3fSopenharmony_ci     *
78b1b8bc3fSopenharmony_ci     * @param netId network ID
79b1b8bc3fSopenharmony_ci     */
80b1b8bc3fSopenharmony_ci    void ShareDnsSet(uint16_t netId);
81b1b8bc3fSopenharmony_ci
82b1b8bc3fSopenharmony_ci    /**
83b1b8bc3fSopenharmony_ci     * Start Dns proxy for network share
84b1b8bc3fSopenharmony_ci     *
85b1b8bc3fSopenharmony_ci     */
86b1b8bc3fSopenharmony_ci    void StartDnsProxyListen();
87b1b8bc3fSopenharmony_ci
88b1b8bc3fSopenharmony_ci    /**
89b1b8bc3fSopenharmony_ci     * Stop Dns proxy for network share
90b1b8bc3fSopenharmony_ci     *
91b1b8bc3fSopenharmony_ci     */
92b1b8bc3fSopenharmony_ci    void StopDnsProxyListen();
93b1b8bc3fSopenharmony_ci
94b1b8bc3fSopenharmony_ci    /**
95b1b8bc3fSopenharmony_ci     * Get the Dump Info object, this is for dump.
96b1b8bc3fSopenharmony_ci     *
97b1b8bc3fSopenharmony_ci     * @param info Infos for dump
98b1b8bc3fSopenharmony_ci     */
99b1b8bc3fSopenharmony_ci    void GetDumpInfo(std::string &info);
100b1b8bc3fSopenharmony_ci
101b1b8bc3fSopenharmony_ci    /**
102b1b8bc3fSopenharmony_ci     * dns resolution object
103b1b8bc3fSopenharmony_ci     *
104b1b8bc3fSopenharmony_ci     * @param node hostname
105b1b8bc3fSopenharmony_ci     * @param service service name
106b1b8bc3fSopenharmony_ci     * @param hints limit
107b1b8bc3fSopenharmony_ci     * @param result return value
108b1b8bc3fSopenharmony_ci     * @param netId network id
109b1b8bc3fSopenharmony_ci     * @return int32_t  0 is success -1 is failed
110b1b8bc3fSopenharmony_ci     */
111b1b8bc3fSopenharmony_ci    int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
112b1b8bc3fSopenharmony_ci                        uint16_t netId, std::vector<AddrInfo> &res);
113b1b8bc3fSopenharmony_ci
114b1b8bc3fSopenharmony_ci    /**
115b1b8bc3fSopenharmony_ci     * destroy this netid's cache
116b1b8bc3fSopenharmony_ci     * @param netId network's id
117b1b8bc3fSopenharmony_ci     * @return destroy is success? 0 : -1
118b1b8bc3fSopenharmony_ci     */
119b1b8bc3fSopenharmony_ci    int32_t DestroyNetworkCache(uint16_t netId);
120b1b8bc3fSopenharmony_ci
121b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE
122b1b8bc3fSopenharmony_ci    /**
123b1b8bc3fSopenharmony_ci     * Set firewall default action
124b1b8bc3fSopenharmony_ci     *
125b1b8bc3fSopenharmony_ci     * @param inDefault Default action of NetFirewallRuleDirection:RULE_IN
126b1b8bc3fSopenharmony_ci     * @param outDefault Default action of NetFirewallRuleDirection:RULE_OUT
127b1b8bc3fSopenharmony_ci     * @return 0 if success or-1 if an error occurred
128b1b8bc3fSopenharmony_ci     */
129b1b8bc3fSopenharmony_ci    int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault);
130b1b8bc3fSopenharmony_ci
131b1b8bc3fSopenharmony_ci    /**
132b1b8bc3fSopenharmony_ci     * Set firewall current user id
133b1b8bc3fSopenharmony_ci     *
134b1b8bc3fSopenharmony_ci     * @param userId current user id
135b1b8bc3fSopenharmony_ci     * @return 0 if success or -1 if an error occurred
136b1b8bc3fSopenharmony_ci     */
137b1b8bc3fSopenharmony_ci    int32_t SetFirewallCurrentUserId(int32_t userId);
138b1b8bc3fSopenharmony_ci
139b1b8bc3fSopenharmony_ci    /**
140b1b8bc3fSopenharmony_ci     * Set firewall rules to native
141b1b8bc3fSopenharmony_ci     *
142b1b8bc3fSopenharmony_ci     * @param type ip, dns, domain
143b1b8bc3fSopenharmony_ci     * @param ruleList list of NetFirewallIpRule
144b1b8bc3fSopenharmony_ci     * @param isFinish transmit finish or not
145b1b8bc3fSopenharmony_ci     * @return 0 if success or -1 if an error occurred
146b1b8bc3fSopenharmony_ci     */
147b1b8bc3fSopenharmony_ci    int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
148b1b8bc3fSopenharmony_ci                             bool isFinish);
149b1b8bc3fSopenharmony_ci
150b1b8bc3fSopenharmony_ci    /**
151b1b8bc3fSopenharmony_ci     * Clear the Firewall rules
152b1b8bc3fSopenharmony_ci     *
153b1b8bc3fSopenharmony_ci     * @return 0 if success or-1 if an error occurred
154b1b8bc3fSopenharmony_ci     */
155b1b8bc3fSopenharmony_ci    int32_t ClearFirewallRules(NetFirewallRuleType type);
156b1b8bc3fSopenharmony_ci
157b1b8bc3fSopenharmony_ci    /**
158b1b8bc3fSopenharmony_ci     * Register callback for recevie intercept event
159b1b8bc3fSopenharmony_ci     *
160b1b8bc3fSopenharmony_ci     * @param callback implement of INetFirewallCallback
161b1b8bc3fSopenharmony_ci     * @return 0 if success or -1 if an error occurred
162b1b8bc3fSopenharmony_ci     */
163b1b8bc3fSopenharmony_ci    int32_t RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback);
164b1b8bc3fSopenharmony_ci
165b1b8bc3fSopenharmony_ci    /**
166b1b8bc3fSopenharmony_ci     * Unregister callback for recevie intercept event
167b1b8bc3fSopenharmony_ci     *
168b1b8bc3fSopenharmony_ci     * @param callback register callback for recevie intercept event
169b1b8bc3fSopenharmony_ci     * @return 0 if success or -1 if an error occurred
170b1b8bc3fSopenharmony_ci     */
171b1b8bc3fSopenharmony_ci    int32_t UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback);
172b1b8bc3fSopenharmony_ci#endif
173b1b8bc3fSopenharmony_ci
174b1b8bc3fSopenharmony_ci    void EnableIpv6(uint16_t netId, std::string &destination, const std::string &nextHop);
175b1b8bc3fSopenharmony_ci
176b1b8bc3fSopenharmony_ci    int32_t RegisterDnsResultCallback(const sptr<NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep);
177b1b8bc3fSopenharmony_ci    int32_t UnregisterDnsResultCallback(const sptr<NetsysNative::INetDnsResultCallback> &callback);
178b1b8bc3fSopenharmony_ci    int32_t RegisterDnsHealthCallback(const sptr<NetsysNative::INetDnsHealthCallback> &callback);
179b1b8bc3fSopenharmony_ci    int32_t UnregisterDnsHealthCallback(const sptr<NetsysNative::INetDnsHealthCallback> &callback);
180b1b8bc3fSopenharmony_ci
181b1b8bc3fSopenharmony_ci    int32_t AddUidRange(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges);
182b1b8bc3fSopenharmony_ci    int32_t DelUidRange(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges);
183b1b8bc3fSopenharmony_ci
184b1b8bc3fSopenharmony_ciprivate:
185b1b8bc3fSopenharmony_ci    std::shared_ptr<DnsProxyListen> dnsProxyListen_;
186b1b8bc3fSopenharmony_ci    int32_t FillAddrInfo(std::vector<AddrInfo> &addrInfo, addrinfo *res);
187b1b8bc3fSopenharmony_ci};
188b1b8bc3fSopenharmony_ci} // namespace nmd
189b1b8bc3fSopenharmony_ci} // namespace OHOS
190b1b8bc3fSopenharmony_ci#endif // INCLUDE_MANAGER_DNS_MANAGER_H
191