/* * Copyright (C) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef INCLUDE_NET_MANAGER_NATIVE_H #define INCLUDE_NET_MANAGER_NATIVE_H #include #include #include #include "bandwidth_manager.h" #include "conn_manager.h" #include "dns_manager.h" #include "firewall_manager.h" #include "interface_manager.h" #include "interface_type.h" #include "route_manager.h" #include "vnic_manager.h" #include "route_type.h" #include "sharing_manager.h" #include "uid_range.h" #include "net_all_capabilities.h" namespace OHOS { namespace nmd { using namespace OHOS::NetManagerStandard; using namespace OHOS::NetsysNative; class NetManagerNative { public: NetManagerNative(); ~NetManagerNative() = default; static void GetOriginInterfaceIndex(); static std::vector GetCurrentInterfaceIndex(); static void UpdateInterfaceIndex(uint32_t infIndex); void Init(); int32_t NetworkReinitRoute(); int32_t SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker); int32_t NetworkCreatePhysical(int32_t netId, int32_t permission); int32_t NetworkCreateVirtual(int32_t netId, bool hasDns); int32_t NetworkDestroy(int32_t netId); int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set &uids); int32_t DestroyVnic(); int32_t NetworkAddUids(int32_t netId, const std::vector &uidRanges); int32_t NetworkDelUids(int32_t netId, const std::vector &uidRanges); int32_t NetworkAddInterface(int32_t netId, std::string iface, NetBearType netBearerType); int32_t NetworkRemoveInterface(int32_t netId, std::string iface); MarkMaskParcel GetFwmarkForNetwork(int32_t netId); int32_t NetworkAddRoute(int32_t netId, std::string ifName, std::string destination, std::string nextHop); int32_t NetworkRemoveRoute(int32_t netId, std::string ifName, std::string destination, std::string nextHop); int32_t NetworkGetDefault(); int32_t NetworkSetDefault(int32_t netId); int32_t NetworkClearDefault(); int32_t NetworkSetPermissionForNetwork(int32_t netId, NetworkPermission permission); std::vector InterfaceGetList(); int32_t SetProcSysNet(int32_t family, int32_t which, const std::string ifname, const std::string parameter, const std::string value); int32_t GetProcSysNet(int32_t family, int32_t which, const std::string ifname, const std::string parameter, std::string *value); nmd::InterfaceConfigurationParcel GetInterfaceConfig(std::string ifName); void SetInterfaceConfig(InterfaceConfigurationParcel cfg); void ClearInterfaceAddrs(const std::string ifName); int32_t GetInterfaceMtu(std::string ifName); int32_t SetInterfaceMtu(std::string ifName, int32_t mtuValue); int32_t SetTcpBufferSizes(const std::string &tcpBufferSizes); int32_t AddInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength); int32_t DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength); int32_t DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength, const std::string &netCapabilities); int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress); int32_t InterfaceSetIffUp(std::string ifaceName); int32_t NetworkAddRouteParcel(int32_t netId, RouteInfoParcel routeInfo); int32_t NetworkRemoveRouteParcel(int32_t netId, RouteInfoParcel routeInfo); int64_t GetCellularRxBytes(); int64_t GetCellularTxBytes(); int64_t GetAllRxBytes(); int64_t GetAllTxBytes(); int64_t GetUidTxBytes(int32_t uid); int64_t GetUidRxBytes(int32_t uid); int64_t GetIfaceRxBytes(std::string interfaceName); int64_t GetIfaceTxBytes(std::string interfaceName); int32_t IpEnableForwarding(const std::string &requester); int32_t IpDisableForwarding(const std::string &requester); int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface); int32_t DisableNat(const std::string &downstreamIface, const std::string &upsteramIface); int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface); int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface); int32_t DnsSetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, const std::vector &servers, const std::vector &domains); int32_t DnsGetResolverConfig(uint16_t netId, std::vector &servers, std::vector &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount); int32_t DnsCreateNetworkCache(uint16_t netid); int32_t DnsDestroyNetworkCache(uint16_t netId); int32_t BandwidthEnableDataSaver(bool enable); int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes); int32_t BandwidthRemoveIfaceQuota(const std::string &ifName); int32_t BandwidthAddDeniedList(uint32_t uid); int32_t BandwidthRemoveDeniedList(uint32_t uid); int32_t BandwidthAddAllowedList(uint32_t uid); int32_t BandwidthRemoveAllowedList(uint32_t uid); int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector &uids); int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector &uids); int32_t FirewallEnableChain(uint32_t chain, bool enable); int32_t FirewallSetUidRule(uint32_t chain, const std::vector &uids, uint32_t firewallRule); void ShareDnsSet(uint16_t netId); void StartDnsProxyListen(); void StopDnsProxyListen(); void GetDumpInfo(std::string &infos); int32_t DnsGetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints, uint16_t netId, std::vector &res); int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName); int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName); int32_t RegisterDnsResultCallback(const sptr &callback, uint32_t timeStep); int32_t UnregisterDnsResultCallback(const sptr &callback); int32_t RegisterDnsHealthCallback(const sptr &callback); int32_t UnregisterDnsHealthCallback(const sptr &callback); int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on); int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on); #ifdef FEATURE_NET_FIREWALL_ENABLE int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault); int32_t SetFirewallCurrentUserId(int32_t userId); int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector> &ruleList, bool isFinish); int32_t ClearFirewallRules(NetFirewallRuleType type); int32_t RegisterNetFirewallCallback(const sptr &callback); int32_t UnRegisterNetFirewallCallback(const sptr &callback); #endif int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag, bool isBroker); int32_t DeleteNetworkAccessPolicy(uint32_t uid); int32_t NotifyNetBearerTypeChange(std::set bearerTypes); int32_t ClearFirewallAllRules(); private: std::shared_ptr bandwidthManager_ = nullptr; std::shared_ptr connManager_ = nullptr; std::shared_ptr firewallManager_ = nullptr; std::shared_ptr routeManager_ = nullptr; std::shared_ptr interfaceManager_ = nullptr; std::shared_ptr sharingManager_ = nullptr; std::shared_ptr dnsManager_ = nullptr; static inline std::vector interfaceIdex_; }; } // namespace nmd } // namespace OHOS #endif // !INCLUDE_NET_MANAGER_NATIVE_H