• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 
23 using namespace OHOS::NetManagerStandard::CommonUtils;
24 namespace OHOS {
25 namespace NetManagerStandard {
Init()26 void NetsysController::Init()
27 {
28     NETMGR_LOG_I("netsys Init");
29     if (initFlag_) {
30         NETMGR_LOG_I("netsys initialization is complete");
31         return;
32     }
33     netsysService_ = std::make_unique<NetsysControllerServiceImpl>().release();
34     netsysService_->Init();
35     initFlag_ = true;
36 }
37 
GetInstance()38 NetsysController &NetsysController::GetInstance()
39 {
40     static NetsysController singleInstance_;
41     static std::mutex mutex_;
42     if (!singleInstance_.initFlag_) {
43         std::unique_lock<std::mutex> lock(mutex_);
44         if (!singleInstance_.initFlag_) {
45             singleInstance_.Init();
46         }
47     }
48     return singleInstance_;
49 }
50 
SetInternetPermission(uint32_t uid,uint8_t allow)51 int32_t NetsysController::SetInternetPermission(uint32_t uid, uint8_t allow)
52 {
53     if (netsysService_ == nullptr) {
54         NETMGR_LOG_E("netsysService_ is null");
55         return NETSYS_NETSYSSERVICE_NULL;
56     }
57     return netsysService_->SetInternetPermission(uid, allow);
58 }
59 
NetworkCreatePhysical(int32_t netId,int32_t permission)60 int32_t NetsysController::NetworkCreatePhysical(int32_t netId, int32_t permission)
61 {
62     NETMGR_LOG_D("Create Physical network: netId[%{public}d], permission[%{public}d]", netId, permission);
63     if (netsysService_ == nullptr) {
64         NETMGR_LOG_E("netsysService_ is null");
65         return NETSYS_NETSYSSERVICE_NULL;
66     }
67     return netsysService_->NetworkCreatePhysical(netId, permission);
68 }
69 
NetworkCreateVirtual(int32_t netId,bool hasDns)70 int32_t NetsysController::NetworkCreateVirtual(int32_t netId, bool hasDns)
71 {
72     NETMGR_LOG_D("Create Virtual network: netId[%{public}d], hasDns[%{public}d]", netId, hasDns);
73     if (netsysService_ == nullptr) {
74         NETMGR_LOG_E("netsysService_ is null");
75         return NETSYS_NETSYSSERVICE_NULL;
76     }
77     return netsysService_->NetworkCreateVirtual(netId, hasDns);
78 }
79 
NetworkDestroy(int32_t netId)80 int32_t NetsysController::NetworkDestroy(int32_t netId)
81 {
82     NETMGR_LOG_D("Destroy network: netId[%{public}d]", netId);
83     if (netsysService_ == nullptr) {
84         NETMGR_LOG_E("netsysService_ is null");
85         return NETSYS_NETSYSSERVICE_NULL;
86     }
87     return netsysService_->NetworkDestroy(netId);
88 }
89 
NetworkAddUids(int32_t netId,const std::vector<int32_t> & beginUids,const std::vector<int32_t> & endUids)90 int32_t NetsysController::NetworkAddUids(int32_t netId, const std::vector<int32_t> &beginUids,
91                                          const std::vector<int32_t> &endUids)
92 {
93     NETMGR_LOG_D("Destroy network: netId[%{public}d]", netId);
94     if (netsysService_ == nullptr) {
95         NETMGR_LOG_E("netsysService_ is null");
96         return NETSYS_NETSYSSERVICE_NULL;
97     }
98     if (beginUids.size() != endUids.size()) {
99         NETMGR_LOG_E("beginUids and endUids size is mismatch");
100         return NETMANAGER_ERR_INTERNAL;
101     }
102     std::vector<UidRange> uidRanges;
103     for (size_t i = 0; i < beginUids.size(); i++) {
104         uidRanges.emplace_back(UidRange(beginUids[i], endUids[i]));
105     }
106     return netsysService_->NetworkAddUids(netId, uidRanges);
107 }
108 
NetworkDelUids(int32_t netId,const std::vector<int32_t> & beginUids,const std::vector<int32_t> & endUids)109 int32_t NetsysController::NetworkDelUids(int32_t netId, const std::vector<int32_t> &beginUids,
110                                          const std::vector<int32_t> &endUids)
111 {
112     NETMGR_LOG_D("Destroy network: netId[%{public}d]", netId);
113     if (netsysService_ == nullptr) {
114         NETMGR_LOG_E("netsysService_ is null");
115         return NETSYS_NETSYSSERVICE_NULL;
116     }
117     if (beginUids.size() != endUids.size()) {
118         NETMGR_LOG_E("beginUids and endUids size is mismatch");
119         return NETMANAGER_ERR_INTERNAL;
120     }
121     std::vector<UidRange> uidRanges;
122     for (size_t i = 0; i < beginUids.size(); i++) {
123         uidRanges.emplace_back(UidRange(beginUids[i], endUids[i]));
124     }
125     return netsysService_->NetworkDelUids(netId, uidRanges);
126 }
127 
NetworkAddInterface(int32_t netId,const std::string & iface)128 int32_t NetsysController::NetworkAddInterface(int32_t netId, const std::string &iface)
129 {
130     NETMGR_LOG_D("Add network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
131     if (netsysService_ == nullptr) {
132         NETMGR_LOG_E("netsysService_ is null");
133         return NETSYS_NETSYSSERVICE_NULL;
134     }
135     return netsysService_->NetworkAddInterface(netId, iface);
136 }
137 
NetworkRemoveInterface(int32_t netId,const std::string & iface)138 int32_t NetsysController::NetworkRemoveInterface(int32_t netId, const std::string &iface)
139 {
140     NETMGR_LOG_D("Remove network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
141     if (netsysService_ == nullptr) {
142         NETMGR_LOG_E("netsysService_ is null");
143         return NETSYS_NETSYSSERVICE_NULL;
144     }
145     return netsysService_->NetworkRemoveInterface(netId, iface);
146 }
147 
NetworkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)148 int32_t NetsysController::NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
149                                           const std::string &nextHop)
150 {
151     NETMGR_LOG_D("Add Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
152                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
153     if (netsysService_ == nullptr) {
154         NETMGR_LOG_E("netsysService_ is null");
155         return NETSYS_NETSYSSERVICE_NULL;
156     }
157     return netsysService_->NetworkAddRoute(netId, ifName, destination, nextHop);
158 }
159 
NetworkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)160 int32_t NetsysController::NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
161                                              const std::string &nextHop)
162 {
163     NETMGR_LOG_D("Remove Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
164                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
165     if (netsysService_ == nullptr) {
166         NETMGR_LOG_E("netsysService_ is null");
167         return NETSYS_NETSYSSERVICE_NULL;
168     }
169     return netsysService_->NetworkRemoveRoute(netId, ifName, destination, nextHop);
170 }
171 
GetInterfaceConfig(OHOS::nmd::InterfaceConfigurationParcel & cfg)172 int32_t NetsysController::GetInterfaceConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg)
173 {
174     NETMGR_LOG_D("get interface config");
175     if (netsysService_ == nullptr) {
176         NETMGR_LOG_E("netsysService_ is null");
177         return NETSYS_NETSYSSERVICE_NULL;
178     }
179     return netsysService_->GetInterfaceConfig(cfg);
180 }
181 
SetInterfaceConfig(const OHOS::nmd::InterfaceConfigurationParcel & cfg)182 int32_t NetsysController::SetInterfaceConfig(const OHOS::nmd::InterfaceConfigurationParcel &cfg)
183 {
184     NETMGR_LOG_D("set interface config");
185     if (netsysService_ == nullptr) {
186         NETMGR_LOG_E("netsysService_ is null");
187         return NETSYS_NETSYSSERVICE_NULL;
188     }
189     return netsysService_->SetInterfaceConfig(cfg);
190 }
191 
SetInterfaceDown(const std::string & iface)192 int32_t NetsysController::SetInterfaceDown(const std::string &iface)
193 {
194     NETMGR_LOG_D("Set interface down: iface[%{public}s]", iface.c_str());
195     if (netsysService_ == nullptr) {
196         NETMGR_LOG_E("netsysService_ is null");
197         return NETSYS_NETSYSSERVICE_NULL;
198     }
199     return netsysService_->SetInterfaceDown(iface);
200 }
201 
SetInterfaceUp(const std::string & iface)202 int32_t NetsysController::SetInterfaceUp(const std::string &iface)
203 {
204     NETMGR_LOG_D("Set interface up: iface[%{public}s]", iface.c_str());
205     if (netsysService_ == nullptr) {
206         NETMGR_LOG_E("netsysService_ is null");
207         return NETSYS_NETSYSSERVICE_NULL;
208     }
209     return netsysService_->SetInterfaceUp(iface);
210 }
211 
ClearInterfaceAddrs(const std::string & ifName)212 void NetsysController::ClearInterfaceAddrs(const std::string &ifName)
213 {
214     NETMGR_LOG_D("Clear addrs: ifName[%{public}s]", ifName.c_str());
215     if (netsysService_ == nullptr) {
216         NETMGR_LOG_E("netsysService_ is null");
217         return;
218     }
219     return netsysService_->ClearInterfaceAddrs(ifName);
220 }
221 
GetInterfaceMtu(const std::string & ifName)222 int32_t NetsysController::GetInterfaceMtu(const std::string &ifName)
223 {
224     NETMGR_LOG_D("Get mtu: ifName[%{public}s]", ifName.c_str());
225     if (netsysService_ == nullptr) {
226         NETMGR_LOG_E("netsysService_ is null");
227         return NETSYS_NETSYSSERVICE_NULL;
228     }
229     return netsysService_->GetInterfaceMtu(ifName);
230 }
231 
SetInterfaceMtu(const std::string & ifName,int32_t mtu)232 int32_t NetsysController::SetInterfaceMtu(const std::string &ifName, int32_t mtu)
233 {
234     NETMGR_LOG_D("Set mtu: ifName[%{public}s], mtu[%{public}d]", ifName.c_str(), mtu);
235     if (netsysService_ == nullptr) {
236         NETMGR_LOG_E("netsysService_ is null");
237         return NETSYS_NETSYSSERVICE_NULL;
238     }
239     return netsysService_->SetInterfaceMtu(ifName, mtu);
240 }
241 
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)242 int32_t NetsysController::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
243                                               int32_t prefixLength)
244 {
245     NETMGR_LOG_D("Add address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]", ifName.c_str(),
246                  ToAnonymousIp(ipAddr).c_str(), prefixLength);
247     if (netsysService_ == nullptr) {
248         NETMGR_LOG_E("netsysService_ is null");
249         return NETSYS_NETSYSSERVICE_NULL;
250     }
251     return netsysService_->AddInterfaceAddress(ifName, ipAddr, prefixLength);
252 }
253 
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)254 int32_t NetsysController::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
255                                               int32_t prefixLength)
256 {
257     NETMGR_LOG_D("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]", ifName.c_str(),
258                  ToAnonymousIp(ipAddr).c_str(), prefixLength);
259     if (netsysService_ == nullptr) {
260         NETMGR_LOG_E("netsysService_ is null");
261         return NETSYS_NETSYSSERVICE_NULL;
262     }
263     return netsysService_->DelInterfaceAddress(ifName, ipAddr, prefixLength);
264 }
265 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)266 int32_t NetsysController::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
267 {
268     NETMGR_LOG_D("Set Ip Address: ifName[%{public}s]", ifaceName.c_str());
269     if (netsysService_ == nullptr) {
270         NETMGR_LOG_E("netsysService_ is null");
271         return NETSYS_NETSYSSERVICE_NULL;
272     }
273     return netsysService_->InterfaceSetIpAddress(ifaceName, ipAddress);
274 }
275 
InterfaceSetIffUp(const std::string & ifaceName)276 int32_t NetsysController::InterfaceSetIffUp(const std::string &ifaceName)
277 {
278     NETMGR_LOG_D("Set Iff Up: ifName[%{public}s]", ifaceName.c_str());
279     if (netsysService_ == nullptr) {
280         NETMGR_LOG_E("netsysService_ is null");
281         return NETSYS_NETSYSSERVICE_NULL;
282     }
283     return netsysService_->InterfaceSetIffUp(ifaceName);
284 }
285 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)286 int32_t NetsysController::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
287                                             const std::vector<std::string> &servers,
288                                             const std::vector<std::string> &domains)
289 {
290     NETMGR_LOG_D("Set resolver config: netId[%{public}d]", netId);
291     if (netsysService_ == nullptr) {
292         NETMGR_LOG_E("netsysService_ is null");
293         return NETSYS_NETSYSSERVICE_NULL;
294     }
295     return netsysService_->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
296 }
297 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)298 int32_t NetsysController::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
299                                             std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
300                                             uint8_t &retryCount)
301 {
302     NETMGR_LOG_D("Get resolver config: netId[%{public}d]", netId);
303     if (netsysService_ == nullptr) {
304         NETMGR_LOG_E("netsysService_ is null");
305         return NETSYS_NETSYSSERVICE_NULL;
306     }
307     return netsysService_->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
308 }
309 
CreateNetworkCache(uint16_t netId)310 int32_t NetsysController::CreateNetworkCache(uint16_t netId)
311 {
312     NETMGR_LOG_D("create dns cache: netId[%{public}d]", netId);
313     if (netsysService_ == nullptr) {
314         NETMGR_LOG_E("netsysService_ is null");
315         return NETSYS_NETSYSSERVICE_NULL;
316     }
317     return netsysService_->CreateNetworkCache(netId);
318 }
319 
DestroyNetworkCache(uint16_t netId)320 int32_t NetsysController::DestroyNetworkCache(uint16_t netId)
321 {
322     NETMGR_LOG_D("Destroy dns cache: netId[%{public}d]", netId);
323     if (netsysService_ == nullptr) {
324         NETMGR_LOG_E("netsysService_ is null");
325         return NETSYS_NETSYSSERVICE_NULL;
326     }
327     return netsysService_->DestroyNetworkCache(netId);
328 }
329 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)330 int32_t NetsysController::GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
331                                       uint16_t netId, std::vector<AddrInfo> &res)
332 {
333     if (netsysService_ == nullptr) {
334         NETMGR_LOG_E("netsysService_ is null");
335         return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
336     }
337     return netsysService_->GetAddrInfo(hostName, serverName, hints, netId, res);
338 }
339 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,nmd::NetworkSharingTraffic & traffic)340 int32_t NetsysController::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
341                                                    nmd::NetworkSharingTraffic &traffic)
342 {
343     NETMGR_LOG_D("NetsysController GetNetworkSharingTraffic");
344     if (netsysService_ == nullptr) {
345         NETMGR_LOG_E("netsysService_ is null");
346         return NETSYS_NETSYSSERVICE_NULL;
347     }
348     return netsysService_->GetNetworkSharingTraffic(downIface, upIface, traffic);
349 }
350 
GetCellularRxBytes()351 int64_t NetsysController::GetCellularRxBytes()
352 {
353     NETMGR_LOG_D("NetsysController GetCellularRxBytes");
354     if (netsysService_ == nullptr) {
355         NETMGR_LOG_E("netsysService_ is null");
356         return NETSYS_NETSYSSERVICE_NULL;
357     }
358     return netsysService_->GetCellularRxBytes();
359 }
360 
GetCellularTxBytes()361 int64_t NetsysController::GetCellularTxBytes()
362 {
363     NETMGR_LOG_D("NetsysController GetCellularTxBytes");
364     if (netsysService_ == nullptr) {
365         NETMGR_LOG_E("netsysService_ is null");
366         return NETSYS_NETSYSSERVICE_NULL;
367     }
368     return netsysService_->GetCellularTxBytes();
369 }
370 
GetAllRxBytes()371 int64_t NetsysController::GetAllRxBytes()
372 {
373     NETMGR_LOG_D("NetsysController GetAllRxBytes");
374     if (netsysService_ == nullptr) {
375         NETMGR_LOG_E("netsysService_ is null");
376         return NETSYS_NETSYSSERVICE_NULL;
377     }
378     return netsysService_->GetAllRxBytes();
379 }
380 
GetAllTxBytes()381 int64_t NetsysController::GetAllTxBytes()
382 {
383     NETMGR_LOG_D("NetsysController GetAllTxBytes");
384     if (netsysService_ == nullptr) {
385         NETMGR_LOG_E("netsysService_ is null");
386         return NETSYS_NETSYSSERVICE_NULL;
387     }
388     return netsysService_->GetAllTxBytes();
389 }
390 
GetUidRxBytes(uint32_t uid)391 int64_t NetsysController::GetUidRxBytes(uint32_t uid)
392 {
393     NETMGR_LOG_D("NetsysController GetUidRxBytes");
394     if (netsysService_ == nullptr) {
395         NETMGR_LOG_E("netsysService_ is null");
396         return NETSYS_NETSYSSERVICE_NULL;
397     }
398     return netsysService_->GetUidRxBytes(uid);
399 }
400 
GetUidTxBytes(uint32_t uid)401 int64_t NetsysController::GetUidTxBytes(uint32_t uid)
402 {
403     NETMGR_LOG_D("NetsysController GetUidTxBytes");
404     if (netsysService_ == nullptr) {
405         NETMGR_LOG_E("netsysService_ is null");
406         return NETSYS_NETSYSSERVICE_NULL;
407     }
408     return netsysService_->GetUidTxBytes(uid);
409 }
410 
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)411 int64_t NetsysController::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
412 {
413     NETMGR_LOG_D("NetsysController GetUidOnIfaceRxBytes");
414     if (netsysService_ == nullptr) {
415         NETMGR_LOG_E("netsysService_ is null");
416         return NETSYS_NETSYSSERVICE_NULL;
417     }
418     return netsysService_->GetUidOnIfaceRxBytes(uid, interfaceName);
419 }
420 
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)421 int64_t NetsysController::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
422 {
423     NETMGR_LOG_D("NetsysController GetUidOnIfaceTxBytes");
424     if (netsysService_ == nullptr) {
425         NETMGR_LOG_E("netsysService_ is null");
426         return NETSYS_NETSYSSERVICE_NULL;
427     }
428     return netsysService_->GetUidOnIfaceTxBytes(uid, interfaceName);
429 }
430 
GetIfaceRxBytes(const std::string & interfaceName)431 int64_t NetsysController::GetIfaceRxBytes(const std::string &interfaceName)
432 {
433     NETMGR_LOG_D("NetsysController GetIfaceRxBytes");
434     if (netsysService_ == nullptr) {
435         NETMGR_LOG_E("netsysService_ is null");
436         return NETSYS_NETSYSSERVICE_NULL;
437     }
438     return netsysService_->GetIfaceRxBytes(interfaceName);
439 }
440 
GetIfaceTxBytes(const std::string & interfaceName)441 int64_t NetsysController::GetIfaceTxBytes(const std::string &interfaceName)
442 {
443     NETMGR_LOG_D("NetsysController GetIfaceTxBytes");
444     if (netsysService_ == nullptr) {
445         NETMGR_LOG_E("netsysService_ is null");
446         return NETSYS_NETSYSSERVICE_NULL;
447     }
448     return netsysService_->GetIfaceTxBytes(interfaceName);
449 }
450 
InterfaceGetList()451 std::vector<std::string> NetsysController::InterfaceGetList()
452 {
453     NETMGR_LOG_D("NetsysController InterfaceGetList");
454     if (netsysService_ == nullptr) {
455         NETMGR_LOG_E("netsysService_ is null");
456         return {};
457     }
458     return netsysService_->InterfaceGetList();
459 }
460 
UidGetList()461 std::vector<std::string> NetsysController::UidGetList()
462 {
463     NETMGR_LOG_D("NetsysController UidGetList");
464     if (netsysService_ == nullptr) {
465         NETMGR_LOG_E("netsysService_ is null");
466         return {};
467     }
468     return netsysService_->UidGetList();
469 }
470 
GetIfaceRxPackets(const std::string & interfaceName)471 int64_t NetsysController::GetIfaceRxPackets(const std::string &interfaceName)
472 {
473     NETMGR_LOG_D("NetsysController GetIfaceRxPackets");
474     if (netsysService_ == nullptr) {
475         NETMGR_LOG_E("netsysService_ is null");
476         return NETSYS_NETSYSSERVICE_NULL;
477     }
478     return netsysService_->GetIfaceRxPackets(interfaceName);
479 }
480 
GetIfaceTxPackets(const std::string & interfaceName)481 int64_t NetsysController::GetIfaceTxPackets(const std::string &interfaceName)
482 {
483     NETMGR_LOG_D("NetsysController GetIfaceTxPackets");
484     if (netsysService_ == nullptr) {
485         NETMGR_LOG_E("netsysService_ is null");
486         return NETSYS_NETSYSSERVICE_NULL;
487     }
488     return netsysService_->GetIfaceTxPackets(interfaceName);
489 }
490 
SetDefaultNetWork(int32_t netId)491 int32_t NetsysController::SetDefaultNetWork(int32_t netId)
492 {
493     NETMGR_LOG_D("Set DefaultNetWork: netId[%{public}d]", netId);
494     if (netsysService_ == nullptr) {
495         NETMGR_LOG_E("netsysService_ is null");
496         return NETSYS_NETSYSSERVICE_NULL;
497     }
498     return netsysService_->SetDefaultNetWork(netId);
499 }
500 
ClearDefaultNetWorkNetId()501 int32_t NetsysController::ClearDefaultNetWorkNetId()
502 {
503     NETMGR_LOG_D("ClearDefaultNetWorkNetId");
504     if (netsysService_ == nullptr) {
505         NETMGR_LOG_E("netsysService_ is null");
506         return NETSYS_NETSYSSERVICE_NULL;
507     }
508     return netsysService_->ClearDefaultNetWorkNetId();
509 }
510 
BindSocket(int32_t socketFd,uint32_t netId)511 int32_t NetsysController::BindSocket(int32_t socketFd, uint32_t netId)
512 {
513     NETMGR_LOG_D("NetsysController::BindSocket: netId = [%{public}u]", netId);
514     if (netsysService_ == nullptr) {
515         NETMGR_LOG_E("netsysService_ is null");
516         return NETSYS_NETSYSSERVICE_NULL;
517     }
518     return netsysService_->BindSocket(socketFd, netId);
519 }
520 
IpEnableForwarding(const std::string & requestor)521 int32_t NetsysController::IpEnableForwarding(const std::string &requestor)
522 {
523     NETMGR_LOG_D("IpEnableForwarding: requestor[%{public}s]", requestor.c_str());
524     if (netsysService_ == nullptr) {
525         NETMGR_LOG_E("netsysService_ is null");
526         return NETSYS_NETSYSSERVICE_NULL;
527     }
528     return netsysService_->IpEnableForwarding(requestor);
529 }
530 
IpDisableForwarding(const std::string & requestor)531 int32_t NetsysController::IpDisableForwarding(const std::string &requestor)
532 {
533     NETMGR_LOG_D("IpDisableForwarding: requestor[%{public}s]", requestor.c_str());
534     if (netsysService_ == nullptr) {
535         NETMGR_LOG_E("netsysService_ is null");
536         return NETSYS_NETSYSSERVICE_NULL;
537     }
538     return netsysService_->IpDisableForwarding(requestor);
539 }
540 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)541 int32_t NetsysController::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
542 {
543     NETMGR_LOG_D("MockNetsysNativeClient EnableNat: intIface[%{public}s] intIface[%{public}s]", downstreamIface.c_str(),
544                  upstreamIface.c_str());
545     if (netsysService_ == nullptr) {
546         NETMGR_LOG_E("netsysService_ is null");
547         return NETSYS_NETSYSSERVICE_NULL;
548     }
549     return netsysService_->EnableNat(downstreamIface, upstreamIface);
550 }
551 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)552 int32_t NetsysController::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
553 {
554     NETMGR_LOG_D("MockNetsysNativeClient DisableNat: intIface[%{public}s] intIface[%{public}s]",
555                  downstreamIface.c_str(), upstreamIface.c_str());
556     if (netsysService_ == nullptr) {
557         NETMGR_LOG_E("netsysService_ is null");
558         return NETSYS_NETSYSSERVICE_NULL;
559     }
560     return netsysService_->DisableNat(downstreamIface, upstreamIface);
561 }
562 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)563 int32_t NetsysController::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
564 {
565     NETMGR_LOG_D("IpfwdAddInterfaceForward: fromIface[%{public}s], toIface[%{public}s]", fromIface.c_str(),
566                  toIface.c_str());
567     if (netsysService_ == nullptr) {
568         NETMGR_LOG_E("netsysService_ is null");
569         return NETSYS_NETSYSSERVICE_NULL;
570     }
571     return netsysService_->IpfwdAddInterfaceForward(fromIface, toIface);
572 }
573 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)574 int32_t NetsysController::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
575 {
576     NETMGR_LOG_D("IpfwdRemoveInterfaceForward: fromIface[%{public}s], toIface[%{public}s]", fromIface.c_str(),
577                  toIface.c_str());
578     if (netsysService_ == nullptr) {
579         NETMGR_LOG_E("netsysService_ is null");
580         return NETSYS_NETSYSSERVICE_NULL;
581     }
582     return netsysService_->IpfwdRemoveInterfaceForward(fromIface, toIface);
583 }
584 
ShareDnsSet(uint16_t netId)585 int32_t NetsysController::ShareDnsSet(uint16_t netId)
586 {
587     NETMGR_LOG_D("ShareDnsSet: netId[%{public}d]", netId);
588     if (netsysService_ == nullptr) {
589         NETMGR_LOG_E("netsysService_ is null");
590         return NETSYS_NETSYSSERVICE_NULL;
591     }
592     return netsysService_->ShareDnsSet(netId);
593 }
594 
StartDnsProxyListen()595 int32_t NetsysController::StartDnsProxyListen()
596 {
597     NETMGR_LOG_D("NetsysController::StartDnsProxyListen");
598     if (netsysService_ == nullptr) {
599         NETMGR_LOG_E("netsysService_ is null");
600         return NETSYS_NETSYSSERVICE_NULL;
601     }
602     return netsysService_->StartDnsProxyListen();
603 }
604 
StopDnsProxyListen()605 int32_t NetsysController::StopDnsProxyListen()
606 {
607     NETMGR_LOG_D("NetsysController::StopDnsProxyListen");
608     if (netsysService_ == nullptr) {
609         NETMGR_LOG_E("netsysService_ is null");
610         return NETSYS_NETSYSSERVICE_NULL;
611     }
612     return netsysService_->StopDnsProxyListen();
613 }
614 
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)615 int32_t NetsysController::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
616 {
617     if (netsysService_ == nullptr) {
618         NETMGR_LOG_E("netsysService_ is null");
619         return NETSYS_NETSYSSERVICE_NULL;
620     }
621     return netsysService_->RegisterNetsysNotifyCallback(callback);
622 }
623 
BindNetworkServiceVpn(int32_t socketFd)624 int32_t NetsysController::BindNetworkServiceVpn(int32_t socketFd)
625 {
626     NETMGR_LOG_D("NetsysController::BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
627     if (socketFd <= 0) {
628         NETMGR_LOG_E("socketFd is null");
629         return NETSYS_ERR_VPN;
630     }
631     if (netsysService_ == nullptr) {
632         NETMGR_LOG_E("netsysService_ is null");
633         return NETSYS_NETSYSSERVICE_NULL;
634     }
635     return netsysService_->BindNetworkServiceVpn(socketFd);
636 }
637 
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)638 int32_t NetsysController::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
639 {
640     NETMGR_LOG_D("NetsysController::EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
641     if (socketFd <= 0) {
642         NETMGR_LOG_E("socketFd is null");
643         return NETSYS_ERR_VPN;
644     }
645     if (netsysService_ == nullptr) {
646         NETMGR_LOG_E("netsysService_ is null");
647         return NETSYS_NETSYSSERVICE_NULL;
648     }
649     return netsysService_->EnableVirtualNetIfaceCard(socketFd, ifRequest, ifaceFd);
650 }
651 
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)652 int32_t NetsysController::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
653                                        struct ifreq &ifRequest)
654 {
655     NETMGR_LOG_D("NetsysController::set addr");
656     if ((socketFd <= 0) || (ipAddress.length() == 0) || (ipAddress.length() > MAX_IPV4_ADDRESS_LEN) ||
657         (prefixLen <= 0) || (prefixLen > MAX_IPV4_ADDRESS_LEN)) {
658         NETMGR_LOG_E(
659             "The paramemters of SetIpAddress is failed, socketFd[%{public}d], "
660             "ipAddress[%{public}s], prefixLen[%{public}d].",
661             socketFd, ToAnonymousIp(ipAddress).c_str(), prefixLen);
662         return NETSYS_ERR_VPN;
663     }
664     if (netsysService_ == nullptr) {
665         NETMGR_LOG_E("netsysService_ is null");
666         return NETSYS_NETSYSSERVICE_NULL;
667     }
668     return netsysService_->SetIpAddress(socketFd, ipAddress, prefixLen, ifRequest);
669 }
670 
SetBlocking(int32_t ifaceFd,bool isBlock)671 int32_t NetsysController::SetBlocking(int32_t ifaceFd, bool isBlock)
672 {
673     NETMGR_LOG_D("NetsysController::SetBlocking: ifaceFd[%{public}d], isBlock[%{public}d]", ifaceFd, isBlock);
674     if (netsysService_ == nullptr) {
675         NETMGR_LOG_E("netsysService_ is null");
676         return NETSYS_NETSYSSERVICE_NULL;
677     }
678     return netsysService_->SetBlocking(ifaceFd, isBlock);
679 }
680 
StartDhcpClient(const std::string & iface,bool bIpv6)681 int32_t NetsysController::StartDhcpClient(const std::string &iface, bool bIpv6)
682 {
683     NETMGR_LOG_D("NetsysController::StartDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
684     if (netsysService_ == nullptr) {
685         NETMGR_LOG_E("netsysService_ is null");
686         return NETSYS_NETSYSSERVICE_NULL;
687     }
688     return netsysService_->StartDhcpClient(iface, bIpv6);
689 }
690 
StopDhcpClient(const std::string & iface,bool bIpv6)691 int32_t NetsysController::StopDhcpClient(const std::string &iface, bool bIpv6)
692 {
693     NETMGR_LOG_D("NetsysController::SetBlocking: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
694     if (netsysService_ == nullptr) {
695         NETMGR_LOG_E("netsysService_ is null");
696         return NETSYS_NETSYSSERVICE_NULL;
697     }
698     return netsysService_->StopDhcpClient(iface, bIpv6);
699 }
700 
RegisterCallback(sptr<NetsysControllerCallback> callback)701 int32_t NetsysController::RegisterCallback(sptr<NetsysControllerCallback> callback)
702 {
703     NETMGR_LOG_D("NetsysController::RegisterCallback");
704     return netsysService_->RegisterCallback(callback);
705 }
706 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)707 int32_t NetsysController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
708 {
709     NETMGR_LOG_D("NetsysController::StartDhcpService: iface[%{public}s], ipv4addr[%{public}s]", iface.c_str(),
710                  ToAnonymousIp(ipv4addr).c_str());
711     if (netsysService_ == nullptr) {
712         NETMGR_LOG_E("netsysService_ is null");
713         return NETSYS_NETSYSSERVICE_NULL;
714     }
715     return netsysService_->StartDhcpService(iface, ipv4addr);
716 }
717 
StopDhcpService(const std::string & iface)718 int32_t NetsysController::StopDhcpService(const std::string &iface)
719 {
720     NETMGR_LOG_D("NetsysController::StopDhcpService: ifaceFd[%{public}s]", iface.c_str());
721     if (netsysService_ == nullptr) {
722         NETMGR_LOG_E("netsysService_ is null");
723         return NETSYS_NETSYSSERVICE_NULL;
724     }
725     return netsysService_->StopDhcpService(iface);
726 }
727 
BandwidthEnableDataSaver(bool enable)728 int32_t NetsysController::BandwidthEnableDataSaver(bool enable)
729 {
730     NETMGR_LOG_D("NetsysController::BandwidthEnableDataSaver: enable=%{public}d", enable);
731     if (netsysService_ == nullptr) {
732         NETMGR_LOG_E("netsysService_ is null");
733         return NETSYS_NETSYSSERVICE_NULL;
734     }
735     return netsysService_->BandwidthEnableDataSaver(enable);
736 }
737 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)738 int32_t NetsysController::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
739 {
740     NETMGR_LOG_D("NetsysController::BandwidthSetIfaceQuota: ifName=%{public}s", ifName.c_str());
741     if (netsysService_ == nullptr) {
742         NETMGR_LOG_E("netsysService_ is null");
743         return NETSYS_NETSYSSERVICE_NULL;
744     }
745     return netsysService_->BandwidthSetIfaceQuota(ifName, bytes);
746 }
747 
BandwidthRemoveIfaceQuota(const std::string & ifName)748 int32_t NetsysController::BandwidthRemoveIfaceQuota(const std::string &ifName)
749 {
750     NETMGR_LOG_D("NetsysController::BandwidthRemoveIfaceQuota: ifName=%{public}s", ifName.c_str());
751     if (netsysService_ == nullptr) {
752         NETMGR_LOG_E("netsysService_ is null");
753         return NETSYS_NETSYSSERVICE_NULL;
754     }
755     return netsysService_->BandwidthRemoveIfaceQuota(ifName);
756 }
757 
BandwidthAddDeniedList(uint32_t uid)758 int32_t NetsysController::BandwidthAddDeniedList(uint32_t uid)
759 {
760     NETMGR_LOG_D("NetsysController::BandwidthAddDeniedList: uid=%{public}d", uid);
761     if (netsysService_ == nullptr) {
762         NETMGR_LOG_E("netsysService_ is null");
763         return NETSYS_NETSYSSERVICE_NULL;
764     }
765     return netsysService_->BandwidthAddDeniedList(uid);
766 }
767 
BandwidthRemoveDeniedList(uint32_t uid)768 int32_t NetsysController::BandwidthRemoveDeniedList(uint32_t uid)
769 {
770     NETMGR_LOG_D("NetsysController::BandwidthRemoveDeniedList: uid=%{public}d", uid);
771     if (netsysService_ == nullptr) {
772         NETMGR_LOG_E("netsysService_ is null");
773         return NETSYS_NETSYSSERVICE_NULL;
774     }
775     return netsysService_->BandwidthRemoveDeniedList(uid);
776 }
777 
BandwidthAddAllowedList(uint32_t uid)778 int32_t NetsysController::BandwidthAddAllowedList(uint32_t uid)
779 {
780     NETMGR_LOG_D("NetsysController::BandwidthAddAllowedList: uid=%{public}d", uid);
781     if (netsysService_ == nullptr) {
782         NETMGR_LOG_E("netsysService_ is null");
783         return NETSYS_NETSYSSERVICE_NULL;
784     }
785     return netsysService_->BandwidthAddAllowedList(uid);
786 }
787 
BandwidthRemoveAllowedList(uint32_t uid)788 int32_t NetsysController::BandwidthRemoveAllowedList(uint32_t uid)
789 {
790     NETMGR_LOG_D("NetsysController::BandwidthRemoveAllowedList: uid=%{public}d", uid);
791     if (netsysService_ == nullptr) {
792         NETMGR_LOG_E("netsysService_ is null");
793         return NETSYS_NETSYSSERVICE_NULL;
794     }
795     return netsysService_->BandwidthRemoveAllowedList(uid);
796 }
797 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)798 int32_t NetsysController::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
799 {
800     NETMGR_LOG_D("NetsysController::FirewallSetUidsAllowedListChain: chain=%{public}d", chain);
801     if (netsysService_ == nullptr) {
802         NETMGR_LOG_E("netsysService_ is null");
803         return NETSYS_NETSYSSERVICE_NULL;
804     }
805     return netsysService_->FirewallSetUidsAllowedListChain(chain, uids);
806 }
807 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)808 int32_t NetsysController::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
809 {
810     NETMGR_LOG_D("NetsysController::FirewallSetUidsDeniedListChain: chain=%{public}d", chain);
811     if (netsysService_ == nullptr) {
812         NETMGR_LOG_E("netsysService_ is null");
813         return NETSYS_NETSYSSERVICE_NULL;
814     }
815     return netsysService_->FirewallSetUidsDeniedListChain(chain, uids);
816 }
817 
FirewallEnableChain(uint32_t chain,bool enable)818 int32_t NetsysController::FirewallEnableChain(uint32_t chain, bool enable)
819 {
820     NETMGR_LOG_D("NetsysController::FirewallEnableChain: chain=%{public}d, enable=%{public}d", chain, enable);
821     if (netsysService_ == nullptr) {
822         NETMGR_LOG_E("netsysService_ is null");
823         return NETSYS_NETSYSSERVICE_NULL;
824     }
825     return netsysService_->FirewallEnableChain(chain, enable);
826 }
827 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)828 int32_t NetsysController::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)
829 {
830     NETMGR_LOG_D("NetsysController::FirewallSetUidRule Start");
831     if (netsysService_ == nullptr) {
832         NETMGR_LOG_E("netsysService_ is null");
833         return NETSYS_NETSYSSERVICE_NULL;
834     }
835     return netsysService_->FirewallSetUidRule(chain, uids, firewallRule);
836 }
837 
FreeAddrInfo(addrinfo * aihead)838 void NetsysController::FreeAddrInfo(addrinfo *aihead)
839 {
840     addrinfo *tmpNext = nullptr;
841     for (addrinfo *tmp = aihead; tmp != nullptr;) {
842         if (tmp->ai_addr != nullptr) {
843             free(tmp->ai_addr);
844         }
845         if (tmp->ai_canonname != nullptr) {
846             free(tmp->ai_canonname);
847         }
848         tmpNext = tmp->ai_next;
849         free(tmp);
850         tmp = tmpNext;
851     }
852 }
853 
GetTotalStats(uint64_t & stats,uint32_t type)854 int32_t NetsysController::GetTotalStats(uint64_t &stats, uint32_t type)
855 {
856     if (netsysService_ == nullptr) {
857         NETMGR_LOG_E("netsysService is null");
858         return NETSYS_NETSYSSERVICE_NULL;
859     }
860     return netsysService_->GetTotalStats(stats, static_cast<uint32_t>(type));
861 }
862 
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)863 int32_t NetsysController::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
864 {
865     if (netsysService_ == nullptr) {
866         NETMGR_LOG_E("netsysService is null");
867         return NETSYS_NETSYSSERVICE_NULL;
868     }
869     return netsysService_->GetUidStats(stats, static_cast<uint32_t>(type), uid);
870 }
871 
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)872 int32_t NetsysController::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
873 {
874     if (netsysService_ == nullptr) {
875         NETMGR_LOG_E("netsysService is null");
876         return NETSYS_NETSYSSERVICE_NULL;
877     }
878     return netsysService_->GetIfaceStats(stats, static_cast<uint32_t>(type), interfaceName);
879 }
880 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)881 int32_t NetsysController::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
882 {
883     if (netsysService_ == nullptr) {
884         NETMGR_LOG_E("netsysService is null");
885         return NETSYS_NETSYSSERVICE_NULL;
886     }
887     return netsysService_->GetAllStatsInfo(stats);
888 }
889 
SetIptablesCommandForRes(const std::string & cmd,std::string & respond)890 int32_t NetsysController::SetIptablesCommandForRes(const std::string &cmd, std::string &respond)
891 {
892     if (cmd.empty()) {
893         NETMGR_LOG_E("SetIptablesCommandForRes cmd is empty");
894         return ERR_INVALID_DATA;
895     }
896     if (netsysService_ == nullptr) {
897         NETMGR_LOG_E("SetIptablesCommandForRes netsysService is null");
898         return NETSYS_NETSYSSERVICE_NULL;
899     }
900     return netsysService_->SetIptablesCommandForRes(cmd, respond);
901 }
902 
903 } // namespace NetManagerStandard
904 } // namespace OHOS
905