/* * Copyright (c) 2021-2022 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 NETSYS_CONTROLLER_H #define NETSYS_CONTROLLER_H #include #include "refbase.h" #include "i_netsys_controller_service.h" namespace OHOS { namespace NetManagerStandard { class NetsysController { public: ~NetsysController() = default; void Init(); static NetsysController &GetInstance(); /** * Create a physical network * * @param netId * @param permission Permission to create a physical network * @return Return the return value of the netsys interface call */ int32_t NetworkCreatePhysical(int32_t netId, int32_t permission); /** * Destroy the network * * @param netId * @return Return the return value of the netsys interface call */ int32_t NetworkDestroy(int32_t netId); /** * Add network port device * * @param netId * @param iface Network port device name * @return Return the return value of the netsys interface call */ int32_t NetworkAddInterface(int32_t netId, const std::string &iface); /** * Delete network port device * * @param netId * @param iface Network port device name * @return Return the return value of the netsys interface call */ int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface); /** * Add route * * @param netId * @param ifName Network port device name * @param destination Target host ip * @param nextHop Next hop address * @return Return the return value of the netsys interface call */ int32_t NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination, const std::string &nextHop); /** * Remove route * * @param netId * @param ifName Network port device name * @param destination Target host ip * @param nextHop Next hop address * @return Return the return value of the netsys interface call */ int32_t NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination, const std::string &nextHop); /** * @brief Get interface config * * @param iface Network port device name * @return Return the result of this action */ int32_t InterfaceGetConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg); /** * Turn off the device * * @param iface Network port device name * @return Return the result of this action */ int32_t SetInterfaceDown(const std::string &iface); /** * Turn on the device * * @param iface Network port device name * @return Return the result of this action */ int32_t SetInterfaceUp(const std::string &iface); /** * Clear the network interface ip address * * @param ifName Network port device name */ void InterfaceClearAddrs(const std::string &ifName); /** * Obtain mtu from the network interface device * * @param ifName Network port device name * @return Return the return value of the netsys interface call */ int32_t InterfaceGetMtu(const std::string &ifName); /** * Set mtu to network interface device * * @param ifName Network port device name * @param mtu * @return Return the return value of the netsys interface call */ int32_t InterfaceSetMtu(const std::string &ifName, int32_t mtu); /** * Add ip address * * @param ifName Network port device name * @param ipAddr ip address * @param prefixLength subnet mask * @return Return the return value of the netsys interface call */ int32_t InterfaceAddAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength); /** * Delete ip address * * @param ifName Network port device name * @param ipAddr ip address * @param prefixLength subnet mask * @return Return the return value of the netsys interface call */ int32_t InterfaceDelAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength); /** * Set iface ip address * * @param ifaceName Network port device name * @param ipAddress Ip address * @return Return the return value of the netsys interface call */ int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress); /** * Set iface up * * @param ifaceName Network port device name * @return Return the return value of the netsys interface call */ int32_t InterfaceSetIffUp(const std::string &ifaceName); /** * Set dns * * @param netId * @param baseTimeoutMsec * @param retryCount * @param servers * @param domains * @return Return the return value of the netsys interface call */ int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, const std::vector &servers, const std::vector &domains); /** * Get dns server param info * * @param netId * @param servers * @param domains * @param baseTimeoutMsec * @param retryCount * @return Return the return value of the netsys interface call */ int32_t GetResolverConfig(uint16_t netId, std::vector &servers, std::vector &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount); /** * Create dns cache before set dns * * @param netId * @return Return the return value for status of call */ int32_t CreateNetworkCache(uint16_t netId); /** * Destroy dns cache * * @param netId * @return Return the return value of the netsys interface call */ int32_t DestroyNetworkCache(uint16_t netId); /** * Domain name resolution Obtains the domain name address * * @param hostName Domain name to be resolved * @param serverName Server name used for query * @param hints Limit parameters when querying * @param netId Network id * @param res return addrinfo * @return Return the return value of the netsys interface call */ int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints, uint16_t netId, std::vector &res); /** * free addrinfo * * @param aihead struct is addrinfo's variable */ void FreeAddrInfo(addrinfo *aihead); /** * Obtains the bytes of the sharing network. * * @return Success return 0. */ int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface, nmd::NetworkSharingTraffic &traffic); /** * Obtains the bytes received over the cellular network. * * @return The number of received bytes. */ int64_t GetCellularRxBytes(); /** * Obtains the bytes sent over the cellular network. * * @return The number of sent bytes. */ int64_t GetCellularTxBytes(); /** * Obtains the bytes received through all NICs. * * @return The number of received bytes. */ int64_t GetAllRxBytes(); /** * Obtains the bytes sent through all NICs. * * @return The number of sent bytes. */ int64_t GetAllTxBytes(); /** * Obtains the bytes received through a specified UID. * * @param uid app id. * @return The number of received bytes. */ int64_t GetUidRxBytes(uint32_t uid); /** * Obtains the bytes sent through a specified UID. * * @param uid app id. * @return The number of sent bytes. */ int64_t GetUidTxBytes(uint32_t uid); /** * Obtains the bytes received through a specified UID on Iface. * * @param uid app id. * @param iface The name of the interface. * @return The number of received bytes. */ int64_t GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName); /** * Obtains the bytes sent through a specified UID on Iface. * * @param uid app id. * @param iface The name of the interface. * @return The number of sent bytes. */ int64_t GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName); /** * Obtains the bytes received through a specified NIC. * * @param iface The name of the interface. * @return The number of received bytes. */ int64_t GetIfaceRxBytes(const std::string &interfaceName); /** * Obtains the bytes sent through a specified NIC. * * @param iface The name of the interface. * @return The number of sent bytes. */ int64_t GetIfaceTxBytes(const std::string &interfaceName); /** * Obtains the NIC list. * * @return The list of interface. */ std::vector InterfaceGetList(); /** * Obtains the uid list. * * @return The list of uid. */ std::vector UidGetList(); /** * Obtains the packets received through a specified NIC. * * @param iface The name of the interface. * @return The number of received packets. */ int64_t GetIfaceRxPackets(const std::string &interfaceName); /** * Obtains the packets sent through a specified NIC. * * @param iface The name of the interface. * @return The number of sent packets. */ int64_t GetIfaceTxPackets(const std::string &interfaceName); /** * set default network. * * @return Return the return value of the netsys interface call */ int32_t SetDefaultNetWork(int32_t netId); /** * clear default network netId. * * @return Return the return value of the netsys interface call */ int32_t ClearDefaultNetWorkNetId(); /** * Obtains the NIC list. * * @param socket_fd * @param netId * @return Return the return value of the netsys interface call */ int32_t BindSocket(int32_t socket_fd, uint32_t netId); /** * Enable ip forwarding. * * @param requestor the requestor of forwarding * @return Return the return value of the netsys interface call. */ int32_t IpEnableForwarding(const std::string &requestor); /** * Disable ip forwarding. * * @param requestor the requestor of forwarding * @return Return the return value of the netsys interface call. */ int32_t IpDisableForwarding(const std::string &requestor); /** * Enable Nat. * * @param downstreamIface the name of downstream interface * @param upstreamIface the name of upstream interface * @return Return the return value of the netsys interface call. */ int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface); /** * Disable Nat. * * @param downstreamIface the name of downstream interface * @param upstreamIface the name of upstream interface * @return Return the return value of the netsys interface call. */ int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface); /** * Add interface forward. * * @param fromIface the name of incoming interface * @param toIface the name of outcoming interface * @return Return the return value of the netsys interface call. */ int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface); /** * Remove interface forward. * * @param fromIface the name of incoming interface * @param toIface the name of outcoming interface * @return Return the return value of the netsys interface call. */ int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface); /** * Set tether dns. * * @param netId network id * @param dnsAddr the list of dns address * @return Return the return value of the netsys interface call. */ int32_t ShareDnsSet(uint16_t netId); /** * start dns proxy listen * * @return success or failed */ int32_t StartDnsProxyListen(); /** * stop dns proxy listen * * @return success or failed */ int32_t StopDnsProxyListen(); /** * Set net callbackfuction. * * @param callback callbackfuction class * @return Return the return value of the netsys interface call. */ int32_t RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback); /** * Protect tradition network to connect VPN. * * @param socketFd socket file description * @return Return the return value of the netsys interface call. */ int32_t BindNetworkServiceVpn(int32_t socketFd); /** * Enable virtual network iterface card. * * @param socketFd socket file description * @param ifRequest interface request * @param ifaceFd interface file description at output paramenter * @return Return the return value of the netsys interface call. */ int32_t EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd); /** * Set ip address. * * @param socketFd socket file description * @param ipAddress ip address * @param prefixLen the mask of ip address * @param ifRequest interface request * @return Return the return value of the netsys interface call. */ int32_t SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen, struct ifreq &ifRequest); /** * Set network blocking. * * @param ifaceFd interface file description * @param isBlock network blocking * @return Return the return value of the netsys interface call. */ int32_t SetBlocking(int32_t ifaceFd, bool isBlock); /** * Start Dhcp Client. * * @param iface interface file description * @param bIpv6 network blocking * @return success or failed */ int32_t StartDhcpClient(const std::string &iface, bool bIpv6); /** * Stop Dhcp Client. * * @param iface interface file description * @param bIpv6 network blocking * @return success or failed */ int32_t StopDhcpClient(const std::string &iface, bool bIpv6); /** * Register Notify Callback * * @param callback * @return success or failed */ int32_t RegisterCallback(sptr callback); /** * start dhcpservice. * * @param iface interface name * @param ipv4addr ipv4 addr * @return Return the return value of the netsys interface call. */ int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr); /** * stop dhcpservice. * * @param iface interface name * @return Return the return value of the netsys interface call. */ int32_t StopDhcpService(const std::string &iface); /** * Turn on data saving mode. * * @param enable enable or disable * @return value the return value of the netsys interface call. */ int32_t BandwidthEnableDataSaver(bool enable); /** * Set quota. * * @param iface interface name * @param bytes * @return success or failed */ int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes); /** * Delete quota. * * @param iface interface name * @return success or failed */ int32_t BandwidthRemoveIfaceQuota(const std::string &ifName); /** * Add DeniedList. * * @param uid * @return success or failed */ int32_t BandwidthAddDeniedList(uint32_t uid); /** * Remove DeniedList. * * @param uid * @return success or failed */ int32_t BandwidthRemoveDeniedList(uint32_t uid); /** * Add DeniedList. * * @param uid * @return success or failed */ int32_t BandwidthAddAllowedList(uint32_t uid); /** * remove DeniedList. * * @param uid * @return success or failed */ int32_t BandwidthRemoveAllowedList(uint32_t uid); /** * Set firewall rules. * * @param chain chain type * @param isAllowedList is or not AllowedList * @param uids * @return Return the return value of the netsys interface call. */ int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector &uids); /** * Set firewall rules. * * @param chain chain type * @param isAllowedList is or not AllowedList * @param uids * @return Return the return value of the netsys interface call. */ int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector &uids); /** * Enable or disable the specified firewall chain. * * @param chain chain type * @param enable enable or disable * @return success or failed */ int32_t FirewallEnableChain(uint32_t chain, bool enable); /** * Firewall set uid rule. * * @param chain chain type * @param uid uid * @param firewallRule firewall rule * @return success or failed */ int32_t FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule); private: NetsysController() = default; private: bool initFlag_ = false; sptr netsysService_; }; } // namespace NetManagerStandard } // namespace OHOS #endif // NETSYS_CONTROLLER_H