• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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_NET_MANAGER_NATIVE_H
17 #define INCLUDE_NET_MANAGER_NATIVE_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "bandwidth_manager.h"
24 #include "conn_manager.h"
25 #include "dns_manager.h"
26 #include "firewall_manager.h"
27 #include "interface_manager.h"
28 #include "interface_type.h"
29 #include "route_manager.h"
30 #include "route_type.h"
31 #include "sharing_manager.h"
32 
33 namespace OHOS {
34 namespace nmd {
35 class NetManagerNative {
36 public:
37     NetManagerNative();
38     ~NetManagerNative() = default;
39 
40     static void GetOriginInterfaceIndex();
41     static std::vector<uint32_t> GetCurrentInterfaceIndex();
42     static void UpdateInterfaceIndex(uint32_t infIndex);
43 
44     void Init();
45 
46     int32_t NetworkReinitRoute();
47     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission);
48     int32_t NetworkDestroy(int32_t netId);
49     int32_t NetworkAddInterface(int32_t netId, std::string iface);
50     int32_t NetworkRemoveInterface(int32_t netId, std::string iface);
51 
52     MarkMaskParcel GetFwmarkForNetwork(int32_t netId);
53     int32_t NetworkAddRoute(int32_t netId, std::string ifName, std::string destination, std::string nextHop);
54     int32_t NetworkRemoveRoute(int32_t netId, std::string ifName, std::string destination, std::string nextHop);
55     int32_t NetworkGetDefault();
56     int32_t NetworkSetDefault(int32_t netId);
57     int32_t NetworkClearDefault();
58     int32_t NetworkSetPermissionForNetwork(int32_t netId, NetworkPermission permission);
59     std::vector<std::string> InterfaceGetList();
60 
61     int32_t SetProcSysNet(int32_t ipversion, int32_t which, const std::string ifname, const std::string parameter,
62                           const std::string value);
63     int32_t GetProcSysNet(int32_t ipversion, int32_t which, const std::string ifname, const std::string parameter,
64                           std::string *value);
65 
66     nmd::InterfaceConfigurationParcel InterfaceGetConfig(std::string ifName);
67     void InterfaceSetConfig(InterfaceConfigurationParcel cfg);
68     void InterfaceClearAddrs(const std::string ifName);
69     int32_t InterfaceGetMtu(std::string ifName);
70     int32_t InterfaceSetMtu(std::string ifName, int32_t mtuValue);
71     int32_t InterfaceAddAddress(std::string ifName, std::string addrString, int32_t prefixLength);
72     int32_t InterfaceDelAddress(std::string ifName, std::string addrString, int32_t prefixLength);
73     int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress);
74     int32_t InterfaceSetIffUp(std::string ifaceName);
75     int32_t NetworkAddRouteParcel(int32_t netId, RouteInfoParcel routeInfo);
76     int32_t NetworkRemoveRouteParcel(int32_t netId, RouteInfoParcel routeInfo);
77 
78     int64_t GetCellularRxBytes();
79     int64_t GetCellularTxBytes();
80     int64_t GetAllRxBytes();
81     int64_t GetAllTxBytes();
82     int64_t GetUidTxBytes(int32_t uid);
83     int64_t GetUidRxBytes(int32_t uid);
84     int64_t GetIfaceRxBytes(std::string interfaceName);
85     int64_t GetIfaceTxBytes(std::string interfaceName);
86     int32_t IpEnableForwarding(const std::string &requester);
87     int32_t IpDisableForwarding(const std::string &requester);
88     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface);
89     int32_t DisableNat(const std::string &downstreamIface, const std::string &upsteramIface);
90     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface);
91     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface);
92 
93     int32_t DnsSetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
94                                  const std::vector<std::string> &servers, const std::vector<std::string> &domains);
95     int32_t DnsGetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
96                                  uint16_t &baseTimeoutMsec, uint8_t &retryCount);
97     int32_t DnsCreateNetworkCache(uint16_t netid);
98     int32_t DnsDestroyNetworkCache(uint16_t netId);
99     int32_t BandwidthEnableDataSaver(bool enable);
100     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes);
101     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName);
102     int32_t BandwidthAddDeniedList(uint32_t uid);
103     int32_t BandwidthRemoveDeniedList(uint32_t uid);
104     int32_t BandwidthAddAllowedList(uint32_t uid);
105     int32_t BandwidthRemoveAllowedList(uint32_t uid);
106 
107     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
108     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
109     int32_t FirewallEnableChain(uint32_t chain, bool enable);
110     int32_t FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule);
111     void ShareDnsSet(uint16_t netId);
112     void StartDnsProxyListen();
113     void StopDnsProxyListen();
114     void GetDumpInfo(std::string &infos);
115     int32_t DnsGetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
116                            uint16_t netId, std::vector<AddrInfo> &res);
117 
118 private:
119     std::shared_ptr<BandwidthManager> bandwidthManager_ = nullptr;
120     std::shared_ptr<ConnManager> connManager_ = nullptr;
121     std::shared_ptr<FirewallManager> firewallManager_ = nullptr;
122     std::shared_ptr<RouteManager> routeManager_ = nullptr;
123     std::shared_ptr<InterfaceManager> interfaceManager_ = nullptr;
124     std::shared_ptr<SharingManager> sharingManager_ = nullptr;
125     std::shared_ptr<DnsManager> dnsManager_ = nullptr;
126     static std::vector<uint32_t> interfaceIdex_;
127 };
128 } // namespace nmd
129 } // namespace OHOS
130 #endif // !INCLUDE_NET_MANAGER_NATIVE_H
131