1 /*
2  * Copyright (c) 2021-2024 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 #include "netsys_controller.h"
16 
17 #include "net_conn_constants.h"
18 #include "net_conn_types.h"
19 #include "net_mgr_log_wrapper.h"
20 #include "netmanager_base_common_utils.h"
21 #include "netsys_controller_service_impl.h"
22 #include "i_net_dns_result_callback.h"
23 #include "i_net_dns_health_callback.h"
24 
25 using namespace OHOS::NetManagerStandard::CommonUtils;
26 namespace OHOS {
27 namespace NetManagerStandard {
28 static constexpr uint32_t IPV4_MAX_LENGTH = 32;
29 
Init()30 void NetsysController::Init()
31 {
32     NETMGR_LOG_I("netsys Init");
33     if (initFlag_) {
34         NETMGR_LOG_I("netsys initialization is complete");
35         return;
36     }
37     netsysService_ = std::make_unique<NetsysControllerServiceImpl>().release();
38     netsysService_->Init();
39     initFlag_ = true;
40 }
41 
GetInstance()42 NetsysController &NetsysController::GetInstance()
43 {
44     static NetsysController singleInstance_;
45     static std::mutex mutex_;
46     if (!singleInstance_.initFlag_) {
47         std::unique_lock<std::mutex> lock(mutex_);
48         if (!singleInstance_.initFlag_) {
49             singleInstance_.Init();
50         }
51     }
52     return singleInstance_;
53 }
54 
SetInternetPermission(uint32_t uid,uint8_t allow)55 int32_t NetsysController::SetInternetPermission(uint32_t uid, uint8_t allow)
56 {
57     if (netsysService_ == nullptr) {
58         NETMGR_LOG_E("netsysService_ is null");
59         return NETSYS_NETSYSSERVICE_NULL;
60     }
61     return netsysService_->SetInternetPermission(uid, allow);
62 }
63 
NetworkCreatePhysical(int32_t netId,int32_t permission)64 int32_t NetsysController::NetworkCreatePhysical(int32_t netId, int32_t permission)
65 {
66     NETMGR_LOG_I("Create Physical network: netId[%{public}d], permission[%{public}d]", netId, permission);
67     if (netsysService_ == nullptr) {
68         NETMGR_LOG_E("netsysService_ is null");
69         return NETSYS_NETSYSSERVICE_NULL;
70     }
71     return netsysService_->NetworkCreatePhysical(netId, permission);
72 }
73 
NetworkCreateVirtual(int32_t netId,bool hasDns)74 int32_t NetsysController::NetworkCreateVirtual(int32_t netId, bool hasDns)
75 {
76     NETMGR_LOG_I("Create Virtual network: netId[%{public}d], hasDns[%{public}d]", netId, hasDns);
77     if (netsysService_ == nullptr) {
78         NETMGR_LOG_E("netsysService_ is null");
79         return NETSYS_NETSYSSERVICE_NULL;
80     }
81     return netsysService_->NetworkCreateVirtual(netId, hasDns);
82 }
83 
NetworkDestroy(int32_t netId)84 int32_t NetsysController::NetworkDestroy(int32_t netId)
85 {
86     NETMGR_LOG_I("Destroy network: netId[%{public}d]", netId);
87     if (netsysService_ == nullptr) {
88         NETMGR_LOG_E("netsysService_ is null");
89         return NETSYS_NETSYSSERVICE_NULL;
90     }
91     return netsysService_->NetworkDestroy(netId);
92 }
93 
CreateVnic(uint16_t mtu,const std::string & tunAddr,int32_t prefix,const std::set<int32_t> & uids)94 int32_t NetsysController::CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix,
95                                      const std::set<int32_t> &uids)
96 {
97     NETMGR_LOG_I("Create Vnic network");
98     if (netsysService_ == nullptr) {
99         NETMGR_LOG_E("netsysService_ is null");
100         return NETSYS_NETSYSSERVICE_NULL;
101     }
102     return netsysService_->CreateVnic(mtu, tunAddr, prefix, uids);
103 }
104 
DestroyVnic()105 int32_t NetsysController::DestroyVnic()
106 {
107     NETMGR_LOG_I("Destroy Vnic network");
108     if (netsysService_ == nullptr) {
109         NETMGR_LOG_E("netsysService_ is null");
110         return NETSYS_NETSYSSERVICE_NULL;
111     }
112     return netsysService_->DestroyVnic();
113 }
114 
NetworkAddUids(int32_t netId,const std::vector<int32_t> & beginUids,const std::vector<int32_t> & endUids)115 int32_t NetsysController::NetworkAddUids(int32_t netId, const std::vector<int32_t> &beginUids,
116                                          const std::vector<int32_t> &endUids)
117 {
118     NETMGR_LOG_I("Destroy network: netId[%{public}d]", netId);
119     if (netsysService_ == nullptr) {
120         NETMGR_LOG_E("netsysService_ is null");
121         return NETSYS_NETSYSSERVICE_NULL;
122     }
123     if (beginUids.size() != endUids.size()) {
124         NETMGR_LOG_E("beginUids and endUids size is mismatch");
125         return NETMANAGER_ERR_INTERNAL;
126     }
127     std::vector<UidRange> uidRanges;
128     for (size_t i = 0; i < beginUids.size(); i++) {
129         uidRanges.emplace_back(UidRange(beginUids[i], endUids[i]));
130     }
131     return netsysService_->NetworkAddUids(netId, uidRanges);
132 }
133 
NetworkDelUids(int32_t netId,const std::vector<int32_t> & beginUids,const std::vector<int32_t> & endUids)134 int32_t NetsysController::NetworkDelUids(int32_t netId, const std::vector<int32_t> &beginUids,
135                                          const std::vector<int32_t> &endUids)
136 {
137     NETMGR_LOG_I("Destroy network: netId[%{public}d]", netId);
138     if (netsysService_ == nullptr) {
139         NETMGR_LOG_E("netsysService_ is null");
140         return NETSYS_NETSYSSERVICE_NULL;
141     }
142     if (beginUids.size() != endUids.size()) {
143         NETMGR_LOG_E("beginUids and endUids size is mismatch");
144         return NETMANAGER_ERR_INTERNAL;
145     }
146     std::vector<UidRange> uidRanges;
147     for (size_t i = 0; i < beginUids.size(); i++) {
148         uidRanges.emplace_back(UidRange(beginUids[i], endUids[i]));
149     }
150     return netsysService_->NetworkDelUids(netId, uidRanges);
151 }
152 
NetworkAddInterface(int32_t netId,const std::string & iface,NetBearType netBearerType)153 int32_t NetsysController::NetworkAddInterface(int32_t netId, const std::string &iface, NetBearType netBearerType)
154 {
155     NETMGR_LOG_I("Add network interface: netId[%{public}d], iface[%{public}s, bearerType[%{public}u]]", netId,
156                  iface.c_str(), netBearerType);
157     if (netsysService_ == nullptr) {
158         NETMGR_LOG_E("netsysService_ is null");
159         return NETSYS_NETSYSSERVICE_NULL;
160     }
161     return netsysService_->NetworkAddInterface(netId, iface, netBearerType);
162 }
163 
NetworkRemoveInterface(int32_t netId,const std::string & iface)164 int32_t NetsysController::NetworkRemoveInterface(int32_t netId, const std::string &iface)
165 {
166     NETMGR_LOG_I("Remove network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
167     if (netsysService_ == nullptr) {
168         NETMGR_LOG_E("netsysService_ is null");
169         return NETSYS_NETSYSSERVICE_NULL;
170     }
171     return netsysService_->NetworkRemoveInterface(netId, iface);
172 }
173 
NetworkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)174 int32_t NetsysController::NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
175                                           const std::string &nextHop)
176 {
177     NETMGR_LOG_D("Add Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
178                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
179     if (netsysService_ == nullptr) {
180         NETMGR_LOG_E("netsysService_ is null");
181         return NETSYS_NETSYSSERVICE_NULL;
182     }
183     return netsysService_->NetworkAddRoute(netId, ifName, destination, nextHop);
184 }
185 
NetworkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)186 int32_t NetsysController::NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
187                                              const std::string &nextHop)
188 {
189     NETMGR_LOG_D("Remove Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
190                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
191     if (netsysService_ == nullptr) {
192         NETMGR_LOG_E("netsysService_ is null");
193         return NETSYS_NETSYSSERVICE_NULL;
194     }
195     return netsysService_->NetworkRemoveRoute(netId, ifName, destination, nextHop);
196 }
197 
GetInterfaceConfig(OHOS::nmd::InterfaceConfigurationParcel & cfg)198 int32_t NetsysController::GetInterfaceConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg)
199 {
200     NETMGR_LOG_I("get interface config");
201     if (netsysService_ == nullptr) {
202         NETMGR_LOG_E("netsysService_ is null");
203         return NETSYS_NETSYSSERVICE_NULL;
204     }
205     return netsysService_->GetInterfaceConfig(cfg);
206 }
207 
SetInterfaceConfig(const OHOS::nmd::InterfaceConfigurationParcel & cfg)208 int32_t NetsysController::SetInterfaceConfig(const OHOS::nmd::InterfaceConfigurationParcel &cfg)
209 {
210     NETMGR_LOG_I("set interface config");
211     if (netsysService_ == nullptr) {
212         NETMGR_LOG_E("netsysService_ is null");
213         return NETSYS_NETSYSSERVICE_NULL;
214     }
215     return netsysService_->SetInterfaceConfig(cfg);
216 }
217 
SetInterfaceDown(const std::string & iface)218 int32_t NetsysController::SetInterfaceDown(const std::string &iface)
219 {
220     NETMGR_LOG_I("Set interface down: iface[%{public}s]", iface.c_str());
221     if (netsysService_ == nullptr) {
222         NETMGR_LOG_E("netsysService_ is null");
223         return NETSYS_NETSYSSERVICE_NULL;
224     }
225     return netsysService_->SetInterfaceDown(iface);
226 }
227 
SetInterfaceUp(const std::string & iface)228 int32_t NetsysController::SetInterfaceUp(const std::string &iface)
229 {
230     NETMGR_LOG_I("Set interface up: iface[%{public}s]", iface.c_str());
231     if (netsysService_ == nullptr) {
232         NETMGR_LOG_E("netsysService_ is null");
233         return NETSYS_NETSYSSERVICE_NULL;
234     }
235     return netsysService_->SetInterfaceUp(iface);
236 }
237 
ClearInterfaceAddrs(const std::string & ifName)238 void NetsysController::ClearInterfaceAddrs(const std::string &ifName)
239 {
240     NETMGR_LOG_I("Clear addrs: ifName[%{public}s]", ifName.c_str());
241     if (netsysService_ == nullptr) {
242         NETMGR_LOG_E("netsysService_ is null");
243         return;
244     }
245     return netsysService_->ClearInterfaceAddrs(ifName);
246 }
247 
GetInterfaceMtu(const std::string & ifName)248 int32_t NetsysController::GetInterfaceMtu(const std::string &ifName)
249 {
250     NETMGR_LOG_I("Get mtu: ifName[%{public}s]", ifName.c_str());
251     if (netsysService_ == nullptr) {
252         NETMGR_LOG_E("netsysService_ is null");
253         return NETSYS_NETSYSSERVICE_NULL;
254     }
255     return netsysService_->GetInterfaceMtu(ifName);
256 }
257 
SetInterfaceMtu(const std::string & ifName,int32_t mtu)258 int32_t NetsysController::SetInterfaceMtu(const std::string &ifName, int32_t mtu)
259 {
260     NETMGR_LOG_I("Set mtu: ifName[%{public}s], mtu[%{public}d]", ifName.c_str(), mtu);
261     if (netsysService_ == nullptr) {
262         NETMGR_LOG_E("netsysService_ is null");
263         return NETSYS_NETSYSSERVICE_NULL;
264     }
265     return netsysService_->SetInterfaceMtu(ifName, mtu);
266 }
267 
SetTcpBufferSizes(const std::string & tcpBufferSizes)268 int32_t NetsysController::SetTcpBufferSizes(const std::string &tcpBufferSizes)
269 {
270     NETMGR_LOG_I("Set tcp buffer sizes: tcpBufferSizes[%{public}s]", tcpBufferSizes.c_str());
271     if (netsysService_ == nullptr) {
272         NETMGR_LOG_E("netsysService_ is null");
273         return NETSYS_NETSYSSERVICE_NULL;
274     }
275     return netsysService_->SetTcpBufferSizes(tcpBufferSizes);
276 }
277 
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)278 int32_t NetsysController::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
279                                               int32_t prefixLength)
280 {
281     NETMGR_LOG_I("Add address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
282         ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
283     if (netsysService_ == nullptr) {
284         NETMGR_LOG_E("netsysService_ is null");
285         return NETSYS_NETSYSSERVICE_NULL;
286     }
287     return netsysService_->AddInterfaceAddress(ifName, ipAddr, prefixLength);
288 }
289 
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)290 int32_t NetsysController::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
291                                               int32_t prefixLength)
292 {
293     NETMGR_LOG_I("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
294         ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
295     if (netsysService_ == nullptr) {
296         NETMGR_LOG_E("netsysService_ is null");
297         return NETSYS_NETSYSSERVICE_NULL;
298     }
299     return netsysService_->DelInterfaceAddress(ifName, ipAddr, prefixLength);
300 }
301 
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength,const std::string & netCapabilities)302 int32_t NetsysController::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
303                                               int32_t prefixLength, const std::string &netCapabilities)
304 {
305     NETMGR_LOG_I("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
306         ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
307     if (netsysService_ == nullptr) {
308         NETMGR_LOG_E("netsysService_ is null");
309         return NETSYS_NETSYSSERVICE_NULL;
310     }
311     return netsysService_->DelInterfaceAddress(ifName, ipAddr, prefixLength, netCapabilities);
312 }
313 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)314 int32_t NetsysController::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
315 {
316     NETMGR_LOG_D("Set Ip Address: ifName[%{public}s]", ifaceName.c_str());
317     if (netsysService_ == nullptr) {
318         NETMGR_LOG_E("netsysService_ is null");
319         return NETSYS_NETSYSSERVICE_NULL;
320     }
321     return netsysService_->InterfaceSetIpAddress(ifaceName, ipAddress);
322 }
323 
InterfaceSetIffUp(const std::string & ifaceName)324 int32_t NetsysController::InterfaceSetIffUp(const std::string &ifaceName)
325 {
326     NETMGR_LOG_D("Set Iff Up: ifName[%{public}s]", ifaceName.c_str());
327     if (netsysService_ == nullptr) {
328         NETMGR_LOG_E("netsysService_ is null");
329         return NETSYS_NETSYSSERVICE_NULL;
330     }
331     return netsysService_->InterfaceSetIffUp(ifaceName);
332 }
333 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)334 int32_t NetsysController::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
335                                             const std::vector<std::string> &servers,
336                                             const std::vector<std::string> &domains)
337 {
338     NETMGR_LOG_I("Set resolver config: netId[%{public}d]", netId);
339     if (netsysService_ == nullptr) {
340         NETMGR_LOG_E("netsysService_ is null");
341         return NETSYS_NETSYSSERVICE_NULL;
342     }
343     return netsysService_->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
344 }
345 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)346 int32_t NetsysController::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
347                                             std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
348                                             uint8_t &retryCount)
349 {
350     NETMGR_LOG_I("Get resolver config: netId[%{public}d]", netId);
351     if (netsysService_ == nullptr) {
352         NETMGR_LOG_E("netsysService_ is null");
353         return NETSYS_NETSYSSERVICE_NULL;
354     }
355     return netsysService_->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
356 }
357 
CreateNetworkCache(uint16_t netId)358 int32_t NetsysController::CreateNetworkCache(uint16_t netId)
359 {
360     NETMGR_LOG_I("create dns cache: netId[%{public}d]", netId);
361     if (netsysService_ == nullptr) {
362         NETMGR_LOG_E("netsysService_ is null");
363         return NETSYS_NETSYSSERVICE_NULL;
364     }
365     return netsysService_->CreateNetworkCache(netId);
366 }
367 
DestroyNetworkCache(uint16_t netId)368 int32_t NetsysController::DestroyNetworkCache(uint16_t netId)
369 {
370     NETMGR_LOG_I("Destroy dns cache: netId[%{public}d]", netId);
371     if (netsysService_ == nullptr) {
372         NETMGR_LOG_E("netsysService_ is null");
373         return NETSYS_NETSYSSERVICE_NULL;
374     }
375     return netsysService_->DestroyNetworkCache(netId);
376 }
377 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)378 int32_t NetsysController::GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
379                                       uint16_t netId, std::vector<AddrInfo> &res)
380 {
381     if (netsysService_ == nullptr) {
382         NETMGR_LOG_E("netsysService_ is null");
383         return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
384     }
385     return netsysService_->GetAddrInfo(hostName, serverName, hints, netId, res);
386 }
387 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,nmd::NetworkSharingTraffic & traffic)388 int32_t NetsysController::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
389                                                    nmd::NetworkSharingTraffic &traffic)
390 {
391     NETMGR_LOG_I("NetsysController GetNetworkSharingTraffic");
392     if (netsysService_ == nullptr) {
393         NETMGR_LOG_E("netsysService_ is null");
394         return NETSYS_NETSYSSERVICE_NULL;
395     }
396     return netsysService_->GetNetworkSharingTraffic(downIface, upIface, traffic);
397 }
398 
GetCellularRxBytes()399 int64_t NetsysController::GetCellularRxBytes()
400 {
401     NETMGR_LOG_D("NetsysController GetCellularRxBytes");
402     if (netsysService_ == nullptr) {
403         NETMGR_LOG_E("netsysService_ is null");
404         return NETSYS_NETSYSSERVICE_NULL;
405     }
406     return netsysService_->GetCellularRxBytes();
407 }
408 
GetCellularTxBytes()409 int64_t NetsysController::GetCellularTxBytes()
410 {
411     NETMGR_LOG_D("NetsysController GetCellularTxBytes");
412     if (netsysService_ == nullptr) {
413         NETMGR_LOG_E("netsysService_ is null");
414         return NETSYS_NETSYSSERVICE_NULL;
415     }
416     return netsysService_->GetCellularTxBytes();
417 }
418 
GetAllRxBytes()419 int64_t NetsysController::GetAllRxBytes()
420 {
421     NETMGR_LOG_D("NetsysController GetAllRxBytes");
422     if (netsysService_ == nullptr) {
423         NETMGR_LOG_E("netsysService_ is null");
424         return NETSYS_NETSYSSERVICE_NULL;
425     }
426     return netsysService_->GetAllRxBytes();
427 }
428 
GetAllTxBytes()429 int64_t NetsysController::GetAllTxBytes()
430 {
431     NETMGR_LOG_D("NetsysController GetAllTxBytes");
432     if (netsysService_ == nullptr) {
433         NETMGR_LOG_E("netsysService_ is null");
434         return NETSYS_NETSYSSERVICE_NULL;
435     }
436     return netsysService_->GetAllTxBytes();
437 }
438 
GetUidRxBytes(uint32_t uid)439 int64_t NetsysController::GetUidRxBytes(uint32_t uid)
440 {
441     NETMGR_LOG_D("NetsysController GetUidRxBytes");
442     if (netsysService_ == nullptr) {
443         NETMGR_LOG_E("netsysService_ is null");
444         return NETSYS_NETSYSSERVICE_NULL;
445     }
446     return netsysService_->GetUidRxBytes(uid);
447 }
448 
GetUidTxBytes(uint32_t uid)449 int64_t NetsysController::GetUidTxBytes(uint32_t uid)
450 {
451     NETMGR_LOG_D("NetsysController GetUidTxBytes");
452     if (netsysService_ == nullptr) {
453         NETMGR_LOG_E("netsysService_ is null");
454         return NETSYS_NETSYSSERVICE_NULL;
455     }
456     return netsysService_->GetUidTxBytes(uid);
457 }
458 
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)459 int64_t NetsysController::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
460 {
461     NETMGR_LOG_D("NetsysController GetUidOnIfaceRxBytes");
462     if (netsysService_ == nullptr) {
463         NETMGR_LOG_E("netsysService_ is null");
464         return NETSYS_NETSYSSERVICE_NULL;
465     }
466     return netsysService_->GetUidOnIfaceRxBytes(uid, interfaceName);
467 }
468 
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)469 int64_t NetsysController::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
470 {
471     NETMGR_LOG_D("NetsysController GetUidOnIfaceTxBytes");
472     if (netsysService_ == nullptr) {
473         NETMGR_LOG_E("netsysService_ is null");
474         return NETSYS_NETSYSSERVICE_NULL;
475     }
476     return netsysService_->GetUidOnIfaceTxBytes(uid, interfaceName);
477 }
478 
GetIfaceRxBytes(const std::string & interfaceName)479 int64_t NetsysController::GetIfaceRxBytes(const std::string &interfaceName)
480 {
481     NETMGR_LOG_D("NetsysController GetIfaceRxBytes");
482     if (netsysService_ == nullptr) {
483         NETMGR_LOG_E("netsysService_ is null");
484         return NETSYS_NETSYSSERVICE_NULL;
485     }
486     return netsysService_->GetIfaceRxBytes(interfaceName);
487 }
488 
GetIfaceTxBytes(const std::string & interfaceName)489 int64_t NetsysController::GetIfaceTxBytes(const std::string &interfaceName)
490 {
491     NETMGR_LOG_D("NetsysController GetIfaceTxBytes");
492     if (netsysService_ == nullptr) {
493         NETMGR_LOG_E("netsysService_ is null");
494         return NETSYS_NETSYSSERVICE_NULL;
495     }
496     return netsysService_->GetIfaceTxBytes(interfaceName);
497 }
498 
InterfaceGetList()499 std::vector<std::string> NetsysController::InterfaceGetList()
500 {
501     NETMGR_LOG_I("InterfaceGetList");
502     if (netsysService_ == nullptr) {
503         NETMGR_LOG_E("netsysService_ is null");
504         return {};
505     }
506     return netsysService_->InterfaceGetList();
507 }
508 
UidGetList()509 std::vector<std::string> NetsysController::UidGetList()
510 {
511     NETMGR_LOG_I("UidGetList");
512     if (netsysService_ == nullptr) {
513         NETMGR_LOG_E("netsysService_ is null");
514         return {};
515     }
516     return netsysService_->UidGetList();
517 }
518 
GetIfaceRxPackets(const std::string & interfaceName)519 int64_t NetsysController::GetIfaceRxPackets(const std::string &interfaceName)
520 {
521     NETMGR_LOG_D("NetsysController GetIfaceRxPackets");
522     if (netsysService_ == nullptr) {
523         NETMGR_LOG_E("netsysService_ is null");
524         return NETSYS_NETSYSSERVICE_NULL;
525     }
526     return netsysService_->GetIfaceRxPackets(interfaceName);
527 }
528 
GetIfaceTxPackets(const std::string & interfaceName)529 int64_t NetsysController::GetIfaceTxPackets(const std::string &interfaceName)
530 {
531     NETMGR_LOG_D("NetsysController GetIfaceTxPackets");
532     if (netsysService_ == nullptr) {
533         NETMGR_LOG_E("netsysService_ is null");
534         return NETSYS_NETSYSSERVICE_NULL;
535     }
536     return netsysService_->GetIfaceTxPackets(interfaceName);
537 }
538 
SetDefaultNetWork(int32_t netId)539 int32_t NetsysController::SetDefaultNetWork(int32_t netId)
540 {
541     NETMGR_LOG_D("Set DefaultNetWork: netId[%{public}d]", netId);
542     if (netsysService_ == nullptr) {
543         NETMGR_LOG_E("netsysService_ is null");
544         return NETSYS_NETSYSSERVICE_NULL;
545     }
546     return netsysService_->SetDefaultNetWork(netId);
547 }
548 
ClearDefaultNetWorkNetId()549 int32_t NetsysController::ClearDefaultNetWorkNetId()
550 {
551     NETMGR_LOG_D("ClearDefaultNetWorkNetId");
552     if (netsysService_ == nullptr) {
553         NETMGR_LOG_E("netsysService_ is null");
554         return NETSYS_NETSYSSERVICE_NULL;
555     }
556     return netsysService_->ClearDefaultNetWorkNetId();
557 }
558 
BindSocket(int32_t socketFd,uint32_t netId)559 int32_t NetsysController::BindSocket(int32_t socketFd, uint32_t netId)
560 {
561     NETMGR_LOG_D("NetsysController::BindSocket: netId = [%{public}u]", netId);
562     if (netsysService_ == nullptr) {
563         NETMGR_LOG_E("netsysService_ is null");
564         return NETSYS_NETSYSSERVICE_NULL;
565     }
566     return netsysService_->BindSocket(socketFd, netId);
567 }
568 
IpEnableForwarding(const std::string & requestor)569 int32_t NetsysController::IpEnableForwarding(const std::string &requestor)
570 {
571     NETMGR_LOG_I("IpEnableForwarding: requestor[%{public}s]", requestor.c_str());
572     if (netsysService_ == nullptr) {
573         NETMGR_LOG_E("netsysService_ is null");
574         return NETSYS_NETSYSSERVICE_NULL;
575     }
576     return netsysService_->IpEnableForwarding(requestor);
577 }
578 
IpDisableForwarding(const std::string & requestor)579 int32_t NetsysController::IpDisableForwarding(const std::string &requestor)
580 {
581     NETMGR_LOG_I("IpDisableForwarding: requestor[%{public}s]", requestor.c_str());
582     if (netsysService_ == nullptr) {
583         NETMGR_LOG_E("netsysService_ is null");
584         return NETSYS_NETSYSSERVICE_NULL;
585     }
586     return netsysService_->IpDisableForwarding(requestor);
587 }
588 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)589 int32_t NetsysController::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
590 {
591     NETMGR_LOG_I("EnableNat: intIface[%{public}s] intIface[%{public}s]", downstreamIface.c_str(),
592                  upstreamIface.c_str());
593     if (netsysService_ == nullptr) {
594         NETMGR_LOG_E("netsysService_ is null");
595         return NETSYS_NETSYSSERVICE_NULL;
596     }
597     return netsysService_->EnableNat(downstreamIface, upstreamIface);
598 }
599 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)600 int32_t NetsysController::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
601 {
602     NETMGR_LOG_I("DisableNat: intIface[%{public}s] intIface[%{public}s]",
603                  downstreamIface.c_str(), upstreamIface.c_str());
604     if (netsysService_ == nullptr) {
605         NETMGR_LOG_E("netsysService_ is null");
606         return NETSYS_NETSYSSERVICE_NULL;
607     }
608     return netsysService_->DisableNat(downstreamIface, upstreamIface);
609 }
610 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)611 int32_t NetsysController::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
612 {
613     NETMGR_LOG_I("IpfwdAddInterfaceForward: fromIface[%{public}s], toIface[%{public}s]", fromIface.c_str(),
614                  toIface.c_str());
615     if (netsysService_ == nullptr) {
616         NETMGR_LOG_E("netsysService_ is null");
617         return NETSYS_NETSYSSERVICE_NULL;
618     }
619     return netsysService_->IpfwdAddInterfaceForward(fromIface, toIface);
620 }
621 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)622 int32_t NetsysController::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
623 {
624     NETMGR_LOG_I("IpfwdRemoveInterfaceForward: fromIface[%{public}s], toIface[%{public}s]", fromIface.c_str(),
625                  toIface.c_str());
626     if (netsysService_ == nullptr) {
627         NETMGR_LOG_E("netsysService_ is null");
628         return NETSYS_NETSYSSERVICE_NULL;
629     }
630     return netsysService_->IpfwdRemoveInterfaceForward(fromIface, toIface);
631 }
632 
ShareDnsSet(uint16_t netId)633 int32_t NetsysController::ShareDnsSet(uint16_t netId)
634 {
635     NETMGR_LOG_I("ShareDnsSet: netId[%{public}d]", netId);
636     if (netsysService_ == nullptr) {
637         NETMGR_LOG_E("netsysService_ is null");
638         return NETSYS_NETSYSSERVICE_NULL;
639     }
640     return netsysService_->ShareDnsSet(netId);
641 }
642 
StartDnsProxyListen()643 int32_t NetsysController::StartDnsProxyListen()
644 {
645     NETMGR_LOG_I("StartDnsProxyListen");
646     if (netsysService_ == nullptr) {
647         NETMGR_LOG_E("netsysService_ is null");
648         return NETSYS_NETSYSSERVICE_NULL;
649     }
650     return netsysService_->StartDnsProxyListen();
651 }
652 
StopDnsProxyListen()653 int32_t NetsysController::StopDnsProxyListen()
654 {
655     NETMGR_LOG_I("StopDnsProxyListen");
656     if (netsysService_ == nullptr) {
657         NETMGR_LOG_E("netsysService_ is null");
658         return NETSYS_NETSYSSERVICE_NULL;
659     }
660     return netsysService_->StopDnsProxyListen();
661 }
662 
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)663 int32_t NetsysController::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
664 {
665     if (netsysService_ == nullptr) {
666         NETMGR_LOG_E("netsysService_ is null");
667         return NETSYS_NETSYSSERVICE_NULL;
668     }
669     return netsysService_->RegisterNetsysNotifyCallback(callback);
670 }
671 
BindNetworkServiceVpn(int32_t socketFd)672 int32_t NetsysController::BindNetworkServiceVpn(int32_t socketFd)
673 {
674     NETMGR_LOG_I("BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
675     if (socketFd <= 0) {
676         NETMGR_LOG_E("socketFd is null");
677         return NETSYS_ERR_VPN;
678     }
679     if (netsysService_ == nullptr) {
680         NETMGR_LOG_E("netsysService_ is null");
681         return NETSYS_NETSYSSERVICE_NULL;
682     }
683     return netsysService_->BindNetworkServiceVpn(socketFd);
684 }
685 
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)686 int32_t NetsysController::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
687 {
688     NETMGR_LOG_I("EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
689     if (socketFd <= 0) {
690         NETMGR_LOG_E("socketFd is null");
691         return NETSYS_ERR_VPN;
692     }
693     if (netsysService_ == nullptr) {
694         NETMGR_LOG_E("netsysService_ is null");
695         return NETSYS_NETSYSSERVICE_NULL;
696     }
697     return netsysService_->EnableVirtualNetIfaceCard(socketFd, ifRequest, ifaceFd);
698 }
699 
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)700 int32_t NetsysController::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
701                                        struct ifreq &ifRequest)
702 {
703     NETMGR_LOG_D("NetsysController::set addr");
704     if ((socketFd <= 0) || (ipAddress.length() == 0) || (static_cast<uint32_t>(ipAddress.length()) > IPV4_MAX_LENGTH) ||
705 	    (prefixLen <= 0) || (static_cast<uint32_t>(prefixLen) > IPV4_MAX_LENGTH)) {
706         NETMGR_LOG_E(
707             "The paramemters of SetIpAddress is failed, socketFd[%{public}d], "
708             "ipAddress[%{public}s], prefixLen[%{public}d].",
709             socketFd, ToAnonymousIp(ipAddress).c_str(), prefixLen);
710         return NETSYS_ERR_VPN;
711     }
712     if (netsysService_ == nullptr) {
713         NETMGR_LOG_E("netsysService_ is null");
714         return NETSYS_NETSYSSERVICE_NULL;
715     }
716     return netsysService_->SetIpAddress(socketFd, ipAddress, prefixLen, ifRequest);
717 }
718 
SetBlocking(int32_t ifaceFd,bool isBlock)719 int32_t NetsysController::SetBlocking(int32_t ifaceFd, bool isBlock)
720 {
721     NETMGR_LOG_D("NetsysController::SetBlocking: ifaceFd[%{public}d], isBlock[%{public}d]", ifaceFd, isBlock);
722     if (netsysService_ == nullptr) {
723         NETMGR_LOG_E("netsysService_ is null");
724         return NETSYS_NETSYSSERVICE_NULL;
725     }
726     return netsysService_->SetBlocking(ifaceFd, isBlock);
727 }
728 
StartDhcpClient(const std::string & iface,bool bIpv6)729 int32_t NetsysController::StartDhcpClient(const std::string &iface, bool bIpv6)
730 {
731     NETMGR_LOG_I("StartDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
732     if (netsysService_ == nullptr) {
733         NETMGR_LOG_E("netsysService_ is null");
734         return NETSYS_NETSYSSERVICE_NULL;
735     }
736     return netsysService_->StartDhcpClient(iface, bIpv6);
737 }
738 
StopDhcpClient(const std::string & iface,bool bIpv6)739 int32_t NetsysController::StopDhcpClient(const std::string &iface, bool bIpv6)
740 {
741     NETMGR_LOG_I("StopDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
742     if (netsysService_ == nullptr) {
743         NETMGR_LOG_E("netsysService_ is null");
744         return NETSYS_NETSYSSERVICE_NULL;
745     }
746     return netsysService_->StopDhcpClient(iface, bIpv6);
747 }
748 
RegisterCallback(sptr<NetsysControllerCallback> callback)749 int32_t NetsysController::RegisterCallback(sptr<NetsysControllerCallback> callback)
750 {
751     NETMGR_LOG_D("NetsysController::RegisterCallback");
752     return netsysService_->RegisterCallback(callback);
753 }
754 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)755 int32_t NetsysController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
756 {
757     NETMGR_LOG_I("StartDhcpService: iface[%{public}s], ipv4addr[%{public}s]",
758         iface.c_str(), ToAnonymousIp(ipv4addr).c_str());
759     if (netsysService_ == nullptr) {
760         NETMGR_LOG_E("netsysService_ is null");
761         return NETSYS_NETSYSSERVICE_NULL;
762     }
763     return netsysService_->StartDhcpService(iface, ipv4addr);
764 }
765 
StopDhcpService(const std::string & iface)766 int32_t NetsysController::StopDhcpService(const std::string &iface)
767 {
768     NETMGR_LOG_I("StopDhcpService: ifaceFd[%{public}s]", iface.c_str());
769     if (netsysService_ == nullptr) {
770         NETMGR_LOG_E("netsysService_ is null");
771         return NETSYS_NETSYSSERVICE_NULL;
772     }
773     return netsysService_->StopDhcpService(iface);
774 }
775 
BandwidthEnableDataSaver(bool enable)776 int32_t NetsysController::BandwidthEnableDataSaver(bool enable)
777 {
778     NETMGR_LOG_D("NetsysController::BandwidthEnableDataSaver: enable=%{public}d", enable);
779     if (netsysService_ == nullptr) {
780         NETMGR_LOG_E("netsysService_ is null");
781         return NETSYS_NETSYSSERVICE_NULL;
782     }
783     return netsysService_->BandwidthEnableDataSaver(enable);
784 }
785 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)786 int32_t NetsysController::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
787 {
788     NETMGR_LOG_D("NetsysController::BandwidthSetIfaceQuota: ifName=%{public}s", ifName.c_str());
789     if (netsysService_ == nullptr) {
790         NETMGR_LOG_E("netsysService_ is null");
791         return NETSYS_NETSYSSERVICE_NULL;
792     }
793     return netsysService_->BandwidthSetIfaceQuota(ifName, bytes);
794 }
795 
BandwidthRemoveIfaceQuota(const std::string & ifName)796 int32_t NetsysController::BandwidthRemoveIfaceQuota(const std::string &ifName)
797 {
798     NETMGR_LOG_D("NetsysController::BandwidthRemoveIfaceQuota: ifName=%{public}s", ifName.c_str());
799     if (netsysService_ == nullptr) {
800         NETMGR_LOG_E("netsysService_ is null");
801         return NETSYS_NETSYSSERVICE_NULL;
802     }
803     return netsysService_->BandwidthRemoveIfaceQuota(ifName);
804 }
805 
BandwidthAddDeniedList(uint32_t uid)806 int32_t NetsysController::BandwidthAddDeniedList(uint32_t uid)
807 {
808     NETMGR_LOG_D("NetsysController::BandwidthAddDeniedList: uid=%{public}d", uid);
809     if (netsysService_ == nullptr) {
810         NETMGR_LOG_E("netsysService_ is null");
811         return NETSYS_NETSYSSERVICE_NULL;
812     }
813     return netsysService_->BandwidthAddDeniedList(uid);
814 }
815 
BandwidthRemoveDeniedList(uint32_t uid)816 int32_t NetsysController::BandwidthRemoveDeniedList(uint32_t uid)
817 {
818     NETMGR_LOG_D("NetsysController::BandwidthRemoveDeniedList: uid=%{public}d", uid);
819     if (netsysService_ == nullptr) {
820         NETMGR_LOG_E("netsysService_ is null");
821         return NETSYS_NETSYSSERVICE_NULL;
822     }
823     return netsysService_->BandwidthRemoveDeniedList(uid);
824 }
825 
BandwidthAddAllowedList(uint32_t uid)826 int32_t NetsysController::BandwidthAddAllowedList(uint32_t uid)
827 {
828     NETMGR_LOG_D("NetsysController::BandwidthAddAllowedList: uid=%{public}d", uid);
829     if (netsysService_ == nullptr) {
830         NETMGR_LOG_E("netsysService_ is null");
831         return NETSYS_NETSYSSERVICE_NULL;
832     }
833     return netsysService_->BandwidthAddAllowedList(uid);
834 }
835 
BandwidthRemoveAllowedList(uint32_t uid)836 int32_t NetsysController::BandwidthRemoveAllowedList(uint32_t uid)
837 {
838     NETMGR_LOG_D("NetsysController::BandwidthRemoveAllowedList: uid=%{public}d", uid);
839     if (netsysService_ == nullptr) {
840         NETMGR_LOG_E("netsysService_ is null");
841         return NETSYS_NETSYSSERVICE_NULL;
842     }
843     return netsysService_->BandwidthRemoveAllowedList(uid);
844 }
845 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)846 int32_t NetsysController::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
847 {
848     NETMGR_LOG_I("NetsysController::FirewallSetUidsAllowedListChain: chain=%{public}d", chain);
849     if (netsysService_ == nullptr) {
850         NETMGR_LOG_E("netsysService_ is null");
851         return NETSYS_NETSYSSERVICE_NULL;
852     }
853     return netsysService_->FirewallSetUidsAllowedListChain(chain, uids);
854 }
855 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)856 int32_t NetsysController::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
857 {
858     NETMGR_LOG_I("NetsysController::FirewallSetUidsDeniedListChain: chain=%{public}d", chain);
859     if (netsysService_ == nullptr) {
860         NETMGR_LOG_E("netsysService_ is null");
861         return NETSYS_NETSYSSERVICE_NULL;
862     }
863     return netsysService_->FirewallSetUidsDeniedListChain(chain, uids);
864 }
865 
FirewallEnableChain(uint32_t chain,bool enable)866 int32_t NetsysController::FirewallEnableChain(uint32_t chain, bool enable)
867 {
868     NETMGR_LOG_I("NetsysController::FirewallEnableChain: chain=%{public}d, enable=%{public}d", chain, enable);
869     if (netsysService_ == nullptr) {
870         NETMGR_LOG_E("netsysService_ is null");
871         return NETSYS_NETSYSSERVICE_NULL;
872     }
873     return netsysService_->FirewallEnableChain(chain, enable);
874 }
875 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)876 int32_t NetsysController::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)
877 {
878     NETMGR_LOG_I("NetsysController::FirewallSetUidRule Start");
879     if (netsysService_ == nullptr) {
880         NETMGR_LOG_E("netsysService_ is null");
881         return NETSYS_NETSYSSERVICE_NULL;
882     }
883     return netsysService_->FirewallSetUidRule(chain, uids, firewallRule);
884 }
885 
FreeAddrInfo(addrinfo * aihead)886 void NetsysController::FreeAddrInfo(addrinfo *aihead)
887 {
888     addrinfo *tmpNext = nullptr;
889     for (addrinfo *tmp = aihead; tmp != nullptr;) {
890         if (tmp->ai_addr != nullptr) {
891             free(tmp->ai_addr);
892         }
893         if (tmp->ai_canonname != nullptr) {
894             free(tmp->ai_canonname);
895         }
896         tmpNext = tmp->ai_next;
897         free(tmp);
898         tmp = tmpNext;
899     }
900 }
901 
GetTotalStats(uint64_t & stats,uint32_t type)902 int32_t NetsysController::GetTotalStats(uint64_t &stats, uint32_t type)
903 {
904     if (netsysService_ == nullptr) {
905         NETMGR_LOG_E("netsysService is null");
906         return NETSYS_NETSYSSERVICE_NULL;
907     }
908     return netsysService_->GetTotalStats(stats, static_cast<uint32_t>(type));
909 }
910 
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)911 int32_t NetsysController::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
912 {
913     if (netsysService_ == nullptr) {
914         NETMGR_LOG_E("netsysService is null");
915         return NETSYS_NETSYSSERVICE_NULL;
916     }
917     return netsysService_->GetUidStats(stats, static_cast<uint32_t>(type), uid);
918 }
919 
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)920 int32_t NetsysController::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
921 {
922     if (netsysService_ == nullptr) {
923         NETMGR_LOG_E("netsysService is null");
924         return NETSYS_NETSYSSERVICE_NULL;
925     }
926     return netsysService_->GetIfaceStats(stats, static_cast<uint32_t>(type), interfaceName);
927 }
928 
GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)929 int32_t NetsysController::GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
930 {
931     if (netsysService_ == nullptr) {
932         NETMGR_LOG_E("netsysService is null");
933         return NETSYS_NETSYSSERVICE_NULL;
934     }
935     return netsysService_->GetAllSimStatsInfo(stats);
936 }
937 
DeleteSimStatsInfo(uint32_t uid)938 int32_t NetsysController::DeleteSimStatsInfo(uint32_t uid)
939 {
940     if (netsysService_ == nullptr) {
941         NETMGR_LOG_E("netsysService is null");
942         return NETSYS_NETSYSSERVICE_NULL;
943     }
944     return netsysService_->DeleteSimStatsInfo(uid);
945 }
946 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)947 int32_t NetsysController::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
948 {
949     if (netsysService_ == nullptr) {
950         NETMGR_LOG_E("netsysService is null");
951         return NETSYS_NETSYSSERVICE_NULL;
952     }
953     return netsysService_->GetAllStatsInfo(stats);
954 }
955 
DeleteStatsInfo(uint32_t uid)956 int32_t NetsysController::DeleteStatsInfo(uint32_t uid)
957 {
958     if (netsysService_ == nullptr) {
959         NETMGR_LOG_E("netsysService is null");
960         return NETSYS_NETSYSSERVICE_NULL;
961     }
962     return netsysService_->DeleteStatsInfo(uid);
963 }
964 
SetIptablesCommandForRes(const std::string & cmd,std::string & respond,NetsysNative::IptablesType ipType)965 int32_t NetsysController::SetIptablesCommandForRes(const std::string &cmd, std::string &respond,
966     NetsysNative::IptablesType ipType)
967 {
968     if (cmd.empty()) {
969         NETMGR_LOG_E("SetIptablesCommandForRes cmd is empty");
970         return ERR_INVALID_DATA;
971     }
972     if (netsysService_ == nullptr) {
973         NETMGR_LOG_E("SetIptablesCommandForRes netsysService is null");
974         return NETSYS_NETSYSSERVICE_NULL;
975     }
976     NETMGR_LOG_I("SetIptablesCommandForRes, iptables is %{public}d.", ipType);
977     return netsysService_->SetIptablesCommandForRes(cmd, respond, ipType);
978 }
979 
NetDiagPingHost(const OHOS::NetsysNative::NetDiagPingOption & pingOption,const sptr<OHOS::NetsysNative::INetDiagCallback> & callback)980 int32_t NetsysController::NetDiagPingHost(const OHOS::NetsysNative::NetDiagPingOption &pingOption,
981                                           const sptr<OHOS::NetsysNative::INetDiagCallback> &callback)
982 {
983     if (netsysService_ == nullptr) {
984         NETMGR_LOG_E("netsysService is null");
985         return NETSYS_NETSYSSERVICE_NULL;
986     }
987     return netsysService_->NetDiagPingHost(pingOption, callback);
988 }
989 
NetDiagGetRouteTable(std::list<OHOS::NetsysNative::NetDiagRouteTable> & routeTables)990 int32_t NetsysController::NetDiagGetRouteTable(std::list<OHOS::NetsysNative::NetDiagRouteTable> &routeTables)
991 {
992     if (netsysService_ == nullptr) {
993         NETMGR_LOG_E("netsysService is null");
994         return NETSYS_NETSYSSERVICE_NULL;
995     }
996     return netsysService_->NetDiagGetRouteTable(routeTables);
997 }
998 
NetDiagGetSocketsInfo(OHOS::NetsysNative::NetDiagProtocolType socketType,OHOS::NetsysNative::NetDiagSocketsInfo & socketsInfo)999 int32_t NetsysController::NetDiagGetSocketsInfo(OHOS::NetsysNative::NetDiagProtocolType socketType,
1000                                                 OHOS::NetsysNative::NetDiagSocketsInfo &socketsInfo)
1001 {
1002     if (netsysService_ == nullptr) {
1003         NETMGR_LOG_E("netsysService is null");
1004         return NETSYS_NETSYSSERVICE_NULL;
1005     }
1006     return netsysService_->NetDiagGetSocketsInfo(socketType, socketsInfo);
1007 }
1008 
NetDiagGetInterfaceConfig(std::list<OHOS::NetsysNative::NetDiagIfaceConfig> & configs,const std::string & ifaceName)1009 int32_t NetsysController::NetDiagGetInterfaceConfig(std::list<OHOS::NetsysNative::NetDiagIfaceConfig> &configs,
1010                                                     const std::string &ifaceName)
1011 {
1012     if (netsysService_ == nullptr) {
1013         NETMGR_LOG_E("netsysService is null");
1014         return NETSYS_NETSYSSERVICE_NULL;
1015     }
1016     return netsysService_->NetDiagGetInterfaceConfig(configs, ifaceName);
1017 }
1018 
NetDiagUpdateInterfaceConfig(const OHOS::NetsysNative::NetDiagIfaceConfig & config,const std::string & ifaceName,bool add)1019 int32_t NetsysController::NetDiagUpdateInterfaceConfig(const OHOS::NetsysNative::NetDiagIfaceConfig &config,
1020                                                        const std::string &ifaceName, bool add)
1021 {
1022     if (netsysService_ == nullptr) {
1023         NETMGR_LOG_E("netsysService is null");
1024         return NETSYS_NETSYSSERVICE_NULL;
1025     }
1026     return netsysService_->NetDiagUpdateInterfaceConfig(config, ifaceName, add);
1027 }
1028 
NetDiagSetInterfaceActiveState(const std::string & ifaceName,bool up)1029 int32_t NetsysController::NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)
1030 {
1031     if (netsysService_ == nullptr) {
1032         NETMGR_LOG_E("netsysService is null");
1033         return NETSYS_NETSYSSERVICE_NULL;
1034     }
1035     return netsysService_->NetDiagSetInterfaceActiveState(ifaceName, up);
1036 }
1037 
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)1038 int32_t NetsysController::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
1039                                        const std::string &ifName)
1040 {
1041     if (netsysService_ == nullptr) {
1042         NETMGR_LOG_E("AddStaticArp netsysService is null");
1043         return NETSYS_NETSYSSERVICE_NULL;
1044     }
1045     return netsysService_->AddStaticArp(ipAddr, macAddr, ifName);
1046 }
1047 
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)1048 int32_t NetsysController::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
1049                                        const std::string &ifName)
1050 {
1051     if (netsysService_ == nullptr) {
1052         NETMGR_LOG_E("DelStaticArp netsysService is null");
1053         return NETSYS_NETSYSSERVICE_NULL;
1054     }
1055     return netsysService_->DelStaticArp(ipAddr, macAddr, ifName);
1056 }
1057 
RegisterDnsResultCallback(const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> & callback,uint32_t timeStep)1058 int32_t NetsysController::RegisterDnsResultCallback(
1059     const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> &callback, uint32_t timeStep)
1060 {
1061     if (netsysService_ == nullptr) {
1062         NETMGR_LOG_E("netsysService is null");
1063         return NETSYS_NETSYSSERVICE_NULL;
1064     }
1065     return netsysService_->RegisterDnsResultCallback(callback, timeStep);
1066 }
1067 
UnregisterDnsResultCallback(const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> & callback)1068 int32_t NetsysController::UnregisterDnsResultCallback(
1069     const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> &callback)
1070 {
1071     if (netsysService_ == nullptr) {
1072         NETMGR_LOG_E("netsysService is null");
1073         return NETSYS_NETSYSSERVICE_NULL;
1074     }
1075     return netsysService_->UnregisterDnsResultCallback(callback);
1076 }
1077 
RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)1078 int32_t NetsysController::RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
1079 {
1080     if (netsysService_ == nullptr) {
1081         NETMGR_LOG_E("netsysService is null");
1082         return NETSYS_NETSYSSERVICE_NULL;
1083     }
1084     return netsysService_->RegisterDnsHealthCallback(callback);
1085 }
1086 
UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)1087 int32_t NetsysController::UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
1088 {
1089     if (netsysService_ == nullptr) {
1090         NETMGR_LOG_E("netsysService is null");
1091         return NETSYS_NETSYSSERVICE_NULL;
1092     }
1093     return netsysService_->UnregisterDnsHealthCallback(callback);
1094 }
1095 
GetCookieStats(uint64_t & stats,uint32_t type,uint64_t cookie)1096 int32_t NetsysController::GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)
1097 {
1098     if (netsysService_ == nullptr) {
1099         NETMGR_LOG_E("GetCookieStats netsysService is null");
1100         return NETSYS_NETSYSSERVICE_NULL;
1101     }
1102     return netsysService_->GetCookieStats(stats, type, cookie);
1103 }
1104 
GetNetworkSharingType(std::set<uint32_t> & sharingTypeIsOn)1105 int32_t NetsysController::GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn)
1106 {
1107     if (netsysService_ == nullptr) {
1108         NETMGR_LOG_E("GetNetworkSharingType netsysService is null");
1109         return NETSYS_NETSYSSERVICE_NULL;
1110     }
1111     return netsysService_->GetNetworkSharingType(sharingTypeIsOn);
1112 }
1113 
UpdateNetworkSharingType(uint32_t type,bool isOpen)1114 int32_t NetsysController::UpdateNetworkSharingType(uint32_t type, bool isOpen)
1115 {
1116     if (netsysService_ == nullptr) {
1117         NETMGR_LOG_E("UpdateNetworkSharingType netsysService is null");
1118         return NETSYS_NETSYSSERVICE_NULL;
1119     }
1120     return netsysService_->UpdateNetworkSharingType(type, isOpen);
1121 }
1122 
1123 #ifdef FEATURE_NET_FIREWALL_ENABLE
SetFirewallRules(NetFirewallRuleType type,const std::vector<sptr<NetFirewallBaseRule>> & ruleList,bool isFinish)1124 int32_t NetsysController::SetFirewallRules(NetFirewallRuleType type,
1125                                            const std::vector<sptr<NetFirewallBaseRule>> &ruleList, bool isFinish)
1126 {
1127     NETMGR_LOG_I("NetsysController::SetFirewallRules");
1128     if (netsysService_ == nullptr) {
1129         NETMGR_LOG_E("SetFirewallRules netsysService is null");
1130         return NETSYS_NETSYSSERVICE_NULL;
1131     }
1132     return netsysService_->SetFirewallRules(type, ruleList, isFinish);
1133 }
1134 
SetFirewallDefaultAction(FirewallRuleAction inDefault,FirewallRuleAction outDefault)1135 int32_t NetsysController::SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)
1136 {
1137     NETMGR_LOG_I("NetsysController::SetFirewallDefaultAction");
1138     if (netsysService_ == nullptr) {
1139         NETMGR_LOG_E("SetFirewallDefaultAction netsysService is null");
1140         return NETSYS_NETSYSSERVICE_NULL;
1141     }
1142     return netsysService_->SetFirewallDefaultAction(inDefault, outDefault);
1143 }
1144 
SetFirewallCurrentUserId(int32_t userId)1145 int32_t NetsysController::SetFirewallCurrentUserId(int32_t userId)
1146 {
1147     NETMGR_LOG_I("NetsysController::SetFirewallCurrentUserId");
1148     if (netsysService_ == nullptr) {
1149         NETMGR_LOG_E("SetFirewallCurrentUserId netsysService is null");
1150         return NETSYS_NETSYSSERVICE_NULL;
1151     }
1152     return netsysService_->SetFirewallCurrentUserId(userId);
1153 }
1154 
ClearFirewallRules(NetFirewallRuleType type)1155 int32_t NetsysController::ClearFirewallRules(NetFirewallRuleType type)
1156 {
1157     NETMGR_LOG_I("NetsysController::ClearFirewallRules");
1158     if (netsysService_ == nullptr) {
1159         NETMGR_LOG_E("ClearFirewallRules netsysService is null");
1160         return NETSYS_NETSYSSERVICE_NULL;
1161     }
1162     return netsysService_->ClearFirewallRules(type);
1163 }
1164 
RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> & callback)1165 int32_t NetsysController::RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)
1166 {
1167     if (netsysService_ == nullptr) {
1168         NETMGR_LOG_E("netsysService is null");
1169         return NETSYS_NETSYSSERVICE_NULL;
1170     }
1171     return netsysService_->RegisterNetFirewallCallback(callback);
1172 }
1173 
UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> & callback)1174 int32_t NetsysController::UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)
1175 {
1176     if (netsysService_ == nullptr) {
1177         NETMGR_LOG_E("netsysService is null");
1178         return NETSYS_NETSYSSERVICE_NULL;
1179     }
1180     return netsysService_->UnRegisterNetFirewallCallback(callback);
1181 }
1182 #endif
1183 
SetIpv6PrivacyExtensions(const std::string & interfaceName,const uint32_t on)1184 int32_t NetsysController::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
1185 {
1186     if (netsysService_ == nullptr) {
1187         NETMGR_LOG_E("SetIpv6PrivacyExtensions netsysService is null");
1188         return NETSYS_NETSYSSERVICE_NULL;
1189     }
1190     return netsysService_->SetIpv6PrivacyExtensions(interfaceName, on);
1191 }
1192 
SetEnableIpv6(const std::string & interfaceName,const uint32_t on)1193 int32_t NetsysController::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
1194 {
1195     if (netsysService_ == nullptr) {
1196         NETMGR_LOG_E("SetEnableIpv6 netsysService is null");
1197         return NETSYS_NETSYSSERVICE_NULL;
1198     }
1199     return netsysService_->SetEnableIpv6(interfaceName, on);
1200 }
1201 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag,bool isBroker)1202 int32_t NetsysController::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
1203                                                  bool isBroker)
1204 {
1205     if (netsysService_ == nullptr) {
1206         NETMGR_LOG_E("netsysService_ is null");
1207         return NETSYS_NETSYSSERVICE_NULL;
1208     }
1209     return netsysService_->SetNetworkAccessPolicy(uid, policy, reconfirmFlag, isBroker);
1210 }
1211 
NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)1212 int32_t NetsysController::NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)
1213 {
1214     if (netsysService_ == nullptr) {
1215         NETMGR_LOG_E("netsysService_ is null");
1216         return NETSYS_NETSYSSERVICE_NULL;
1217     }
1218     return netsysService_->NotifyNetBearerTypeChange(bearerTypes);
1219 }
1220 
DeleteNetworkAccessPolicy(uint32_t uid)1221 int32_t NetsysController::DeleteNetworkAccessPolicy(uint32_t uid)
1222 {
1223     if (netsysService_ == nullptr) {
1224         NETMGR_LOG_E("netsysService_ is null");
1225         return NETSYS_NETSYSSERVICE_NULL;
1226     }
1227     return netsysService_->DeleteNetworkAccessPolicy(uid);
1228 }
1229 
ClearFirewallAllRules()1230 int32_t NetsysController::ClearFirewallAllRules()
1231 {
1232     if (netsysService_ == nullptr) {
1233         NETMGR_LOG_E("netsysService_ is null");
1234         return NETSYS_NETSYSSERVICE_NULL;
1235     }
1236     return netsysService_->ClearFirewallAllRules();
1237 }
1238 
StartClat(const std::string & interfaceName,int32_t netId,const std::string & nat64PrefixStr)1239 int32_t NetsysController::StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr)
1240 {
1241     if (netsysService_ == nullptr) {
1242         NETMGR_LOG_E("StartClat netsysService is null");
1243         return NETSYS_NETSYSSERVICE_NULL;
1244     }
1245     return netsysService_->StartClat(interfaceName, netId, nat64PrefixStr);
1246 }
1247 
StopClat(const std::string & interfaceName)1248 int32_t NetsysController::StopClat(const std::string &interfaceName)
1249 {
1250     if (netsysService_ == nullptr) {
1251         NETMGR_LOG_E("StopClat netsysService is null");
1252         return NETSYS_NETSYSSERVICE_NULL;
1253     }
1254     return netsysService_->StopClat(interfaceName);
1255 }
1256 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)1257 int32_t NetsysController::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
1258 {
1259     if (netsysService_ == nullptr) {
1260         NETMGR_LOG_E("SetNicTrafficAllowed netsysService is null");
1261         return NETSYS_NETSYSSERVICE_NULL;
1262     }
1263     return netsysService_->SetNicTrafficAllowed(ifaceNames, status);
1264 }
1265 } // namespace NetManagerStandard
1266 } // namespace OHOS
1267