18e745fdaSopenharmony_ci/* 28e745fdaSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License. 58e745fdaSopenharmony_ci * You may obtain a copy of the License at 68e745fdaSopenharmony_ci * 78e745fdaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88e745fdaSopenharmony_ci * 98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and 138e745fdaSopenharmony_ci * limitations under the License. 148e745fdaSopenharmony_ci */ 158e745fdaSopenharmony_ci 168e745fdaSopenharmony_ci#ifndef NET_FIREWALL_RULES_MANAGER_H 178e745fdaSopenharmony_ci#define NET_FIREWALL_RULES_MANAGER_H 188e745fdaSopenharmony_ci 198e745fdaSopenharmony_ci#include <string> 208e745fdaSopenharmony_ci#include <shared_mutex> 218e745fdaSopenharmony_ci 228e745fdaSopenharmony_ci#include "netfirewall_common.h" 238e745fdaSopenharmony_ci 248e745fdaSopenharmony_cinamespace OHOS { 258e745fdaSopenharmony_cinamespace NetManagerStandard { 268e745fdaSopenharmony_ciclass NetFirewallRuleManager { 278e745fdaSopenharmony_cipublic: 288e745fdaSopenharmony_ci static NetFirewallRuleManager &GetInstance(); 298e745fdaSopenharmony_ci NetFirewallRuleManager(); 308e745fdaSopenharmony_ci ~NetFirewallRuleManager(); 318e745fdaSopenharmony_ci 328e745fdaSopenharmony_ci /** 338e745fdaSopenharmony_ci * Add firewall rules 348e745fdaSopenharmony_ci * 358e745fdaSopenharmony_ci * @param rule Firewall rules 368e745fdaSopenharmony_ci * @param ruleId Rule id genarated by database 378e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 388e745fdaSopenharmony_ci */ 398e745fdaSopenharmony_ci int32_t AddNetFirewallRule(const sptr<NetFirewallRule> &rule, int32_t &ruleId); 408e745fdaSopenharmony_ci 418e745fdaSopenharmony_ci /** 428e745fdaSopenharmony_ci * Modify firewall rules 438e745fdaSopenharmony_ci * 448e745fdaSopenharmony_ci * @param rule Firewall rules 458e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 468e745fdaSopenharmony_ci */ 478e745fdaSopenharmony_ci int32_t UpdateNetFirewallRule(const sptr<NetFirewallRule> &rule); 488e745fdaSopenharmony_ci 498e745fdaSopenharmony_ci /** 508e745fdaSopenharmony_ci * Delete firewall rules 518e745fdaSopenharmony_ci * 528e745fdaSopenharmony_ci * @param userId User ID 538e745fdaSopenharmony_ci * @param ruleId Rule ID 548e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 558e745fdaSopenharmony_ci */ 568e745fdaSopenharmony_ci int32_t DeleteNetFirewallRule(const int32_t userId, const int32_t ruleId); 578e745fdaSopenharmony_ci 588e745fdaSopenharmony_ci /** 598e745fdaSopenharmony_ci * Get all firewall rules 608e745fdaSopenharmony_ci * 618e745fdaSopenharmony_ci * @param userId User ID 628e745fdaSopenharmony_ci * @param requestParam Paging in parameter information 638e745fdaSopenharmony_ci * @param info Paging data information 648e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 658e745fdaSopenharmony_ci */ 668e745fdaSopenharmony_ci int32_t GetNetFirewallRules(const int32_t userId, const sptr<RequestParam> &requestParam, 678e745fdaSopenharmony_ci sptr<FirewallRulePage> &info); 688e745fdaSopenharmony_ci 698e745fdaSopenharmony_ci /** 708e745fdaSopenharmony_ci * Get information about the specified rule ID 718e745fdaSopenharmony_ci * 728e745fdaSopenharmony_ci * @param ruleId Rule ID 738e745fdaSopenharmony_ci * @param rule Return to firewall rules 748e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 758e745fdaSopenharmony_ci */ 768e745fdaSopenharmony_ci int32_t GetNetFirewallRule(const int32_t userId, const int32_t ruleId, sptr<NetFirewallRule> &rule); 778e745fdaSopenharmony_ci 788e745fdaSopenharmony_ci int32_t DeleteNetFirewallRuleByUserId(const int32_t userId); 798e745fdaSopenharmony_ci 808e745fdaSopenharmony_ci int32_t DeleteNetFirewallRuleByAppId(const int32_t appUid); 818e745fdaSopenharmony_ci 828e745fdaSopenharmony_ci int32_t GetEnabledNetFirewallRules(const int32_t userId, std::vector<NetFirewallRule> &ruleList, 838e745fdaSopenharmony_ci NetFirewallRuleType type = NetFirewallRuleType::RULE_ALL); 848e745fdaSopenharmony_ci 858e745fdaSopenharmony_ci int32_t AddDefaultNetFirewallRule(int32_t userId); 868e745fdaSopenharmony_ci 878e745fdaSopenharmony_ci void DeleteUserRuleSize(const int32_t userId); 888e745fdaSopenharmony_ci 898e745fdaSopenharmony_ci int32_t OpenOrCloseNativeFirewall(bool isOpen); 908e745fdaSopenharmony_ci 918e745fdaSopenharmony_ci uint64_t GetCurrentSetRuleSecond(); 928e745fdaSopenharmony_ci 938e745fdaSopenharmony_ci int64_t GetLastRulePushResult(); 948e745fdaSopenharmony_ci 958e745fdaSopenharmony_ciprivate: 968e745fdaSopenharmony_ci int32_t AddNetFirewallRule(const sptr<NetFirewallRule> &rule, bool isNotify, int32_t &ruleId); 978e745fdaSopenharmony_ci 988e745fdaSopenharmony_ci int32_t CheckUserExist(const int32_t userId); 998e745fdaSopenharmony_ci 1008e745fdaSopenharmony_ci int32_t CheckRuleExist(const int32_t ruleId, NetFirewallRule &oldRule); 1018e745fdaSopenharmony_ci 1028e745fdaSopenharmony_ci int32_t GetAllRuleConstraint(const int32_t userId); 1038e745fdaSopenharmony_ci 1048e745fdaSopenharmony_ci int32_t CheckRuleConstraint(const sptr<NetFirewallRule> &rule); 1058e745fdaSopenharmony_ci 1068e745fdaSopenharmony_ci bool CheckAccountExist(int32_t userId); 1078e745fdaSopenharmony_ci 1088e745fdaSopenharmony_ci bool ExtractIpRules(const std::vector<NetFirewallRule> &rules, std::vector<sptr<NetFirewallIpRule>> &ipRules); 1098e745fdaSopenharmony_ci 1108e745fdaSopenharmony_ci bool ExtractDomainRules(const std::vector<NetFirewallRule> &rules, 1118e745fdaSopenharmony_ci std::vector<sptr<NetFirewallDomainRule>> &domainRules); 1128e745fdaSopenharmony_ci 1138e745fdaSopenharmony_ci bool ExtractDnsRules(const std::vector<NetFirewallRule> &rules, std::vector<sptr<NetFirewallDnsRule>> &dnsRules); 1148e745fdaSopenharmony_ci 1158e745fdaSopenharmony_ci int32_t HandleIpTypeForDistributeRules(std::vector<NetFirewallRule> &rules); 1168e745fdaSopenharmony_ci 1178e745fdaSopenharmony_ci int32_t HandleDnsTypeForDistributeRules(std::vector<NetFirewallRule> &rules); 1188e745fdaSopenharmony_ci 1198e745fdaSopenharmony_ci int32_t HandleDomainTypeForDistributeRules(std::vector<NetFirewallRule> &rules); 1208e745fdaSopenharmony_ci 1218e745fdaSopenharmony_ci int32_t GetCurrentAccountId(); 1228e745fdaSopenharmony_ci 1238e745fdaSopenharmony_ci int32_t SetRulesToNativeByType(const int32_t userId, const NetFirewallRuleType type); 1248e745fdaSopenharmony_ci 1258e745fdaSopenharmony_ci int32_t DistributeRulesToNative(NetFirewallRuleType type = NetFirewallRuleType::RULE_ALL); 1268e745fdaSopenharmony_ci 1278e745fdaSopenharmony_ci void SetNetFirewallDumpMessage(const int32_t result); 1288e745fdaSopenharmony_ci 1298e745fdaSopenharmony_ci void UpdateUserRuleSize(const int32_t userId, bool isInc); 1308e745fdaSopenharmony_ci 1318e745fdaSopenharmony_ciprivate: 1328e745fdaSopenharmony_ci // Cache the current state 1338e745fdaSopenharmony_ci std::atomic<int64_t> allUserRule_ = 0; 1348e745fdaSopenharmony_ci int32_t allUserDomain_ = 0; 1358e745fdaSopenharmony_ci int64_t maxDefaultRuleSize_ = 0; 1368e745fdaSopenharmony_ci std::shared_mutex setFirewallRuleMutex_; 1378e745fdaSopenharmony_ci std::map<int32_t, int64_t> userRuleSize_; 1388e745fdaSopenharmony_ci std::atomic<uint64_t> currentSetRuleSecond_ = 0; 1398e745fdaSopenharmony_ci std::atomic<int64_t> lastRulePushResult_ = -1; 1408e745fdaSopenharmony_ci}; 1418e745fdaSopenharmony_ci} // namespace NetManagerStandard 1428e745fdaSopenharmony_ci} // namespace OHOS 1438e745fdaSopenharmony_ci#endif /* NET_FIREWALL_RULES_MANAGER_H */ 144