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