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