• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #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 
NetworkCreatePhysical(int32_t netId,int32_t permission)51 int32_t NetsysController::NetworkCreatePhysical(int32_t netId, int32_t permission)
52 {
53     NETMGR_LOG_D("Create Physical network: netId[%{public}d], permission[%{public}d]", netId, permission);
54     if (netsysService_ == nullptr) {
55         NETMGR_LOG_E("netsysService_ is null");
56         return NETSYS_NETSYSSERVICE_NULL;
57     }
58     return netsysService_->NetworkCreatePhysical(netId, permission);
59 }
60 
NetworkDestroy(int32_t netId)61 int32_t NetsysController::NetworkDestroy(int32_t netId)
62 {
63     NETMGR_LOG_D("Destroy network: netId[%{public}d]", netId);
64     if (netsysService_ == nullptr) {
65         NETMGR_LOG_E("netsysService_ is null");
66         return NETSYS_NETSYSSERVICE_NULL;
67     }
68     return netsysService_->NetworkDestroy(netId);
69 }
70 
NetworkAddInterface(int32_t netId,const std::string & iface)71 int32_t NetsysController::NetworkAddInterface(int32_t netId, const std::string &iface)
72 {
73     NETMGR_LOG_D("Add network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
74     if (netsysService_ == nullptr) {
75         NETMGR_LOG_E("netsysService_ is null");
76         return NETSYS_NETSYSSERVICE_NULL;
77     }
78     return netsysService_->NetworkAddInterface(netId, iface);
79 }
80 
NetworkRemoveInterface(int32_t netId,const std::string & iface)81 int32_t NetsysController::NetworkRemoveInterface(int32_t netId, const std::string &iface)
82 {
83     NETMGR_LOG_D("Remove network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
84     if (netsysService_ == nullptr) {
85         NETMGR_LOG_E("netsysService_ is null");
86         return NETSYS_NETSYSSERVICE_NULL;
87     }
88     return netsysService_->NetworkRemoveInterface(netId, iface);
89 }
90 
NetworkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)91 int32_t NetsysController::NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
92                                           const std::string &nextHop)
93 {
94     NETMGR_LOG_D("Add Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
95                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
96     if (netsysService_ == nullptr) {
97         NETMGR_LOG_E("netsysService_ is null");
98         return NETSYS_NETSYSSERVICE_NULL;
99     }
100     return netsysService_->NetworkAddRoute(netId, ifName, destination, nextHop);
101 }
102 
NetworkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)103 int32_t NetsysController::NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
104                                              const std::string &nextHop)
105 {
106     if (netsysService_ == nullptr) {
107         NETMGR_LOG_E("netsysService_ is null");
108         return NETSYS_NETSYSSERVICE_NULL;
109     }
110     return netsysService_->NetworkRemoveRoute(netId, ifName, destination, nextHop);
111 }
112 
InterfaceGetConfig(OHOS::nmd::InterfaceConfigurationParcel & cfg)113 int32_t NetsysController::InterfaceGetConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg)
114 {
115     NETMGR_LOG_D("get interface config");
116     if (netsysService_ == nullptr) {
117         NETMGR_LOG_E("netsysService_ is null");
118         return NETSYS_NETSYSSERVICE_NULL;
119     }
120     return netsysService_->InterfaceGetConfig(cfg);
121 }
122 
SetInterfaceDown(const std::string & iface)123 int32_t NetsysController::SetInterfaceDown(const std::string &iface)
124 {
125     NETMGR_LOG_D("Set interface down: iface[%{public}s]", iface.c_str());
126     if (netsysService_ == nullptr) {
127         NETMGR_LOG_E("netsysService_ is null");
128         return NETSYS_NETSYSSERVICE_NULL;
129     }
130     return netsysService_->SetInterfaceDown(iface);
131 }
132 
SetInterfaceUp(const std::string & iface)133 int32_t NetsysController::SetInterfaceUp(const std::string &iface)
134 {
135     NETMGR_LOG_D("Set interface up: iface[%{public}s]", iface.c_str());
136     if (netsysService_ == nullptr) {
137         NETMGR_LOG_E("netsysService_ is null");
138         return NETSYS_NETSYSSERVICE_NULL;
139     }
140     return netsysService_->SetInterfaceUp(iface);
141 }
142 
InterfaceClearAddrs(const std::string & ifName)143 void NetsysController::InterfaceClearAddrs(const std::string &ifName)
144 {
145     NETMGR_LOG_D("Clear addrs: ifName[%{public}s]", ifName.c_str());
146     if (netsysService_ == nullptr) {
147         NETMGR_LOG_E("netsysService_ is null");
148         return;
149     }
150     return netsysService_->InterfaceClearAddrs(ifName);
151 }
152 
InterfaceGetMtu(const std::string & ifName)153 int32_t NetsysController::InterfaceGetMtu(const std::string &ifName)
154 {
155     NETMGR_LOG_D("Get mtu: ifName[%{public}s]", ifName.c_str());
156     if (netsysService_ == nullptr) {
157         NETMGR_LOG_E("netsysService_ is null");
158         return NETSYS_NETSYSSERVICE_NULL;
159     }
160     return netsysService_->InterfaceGetMtu(ifName);
161 }
162 
InterfaceSetMtu(const std::string & ifName,int32_t mtu)163 int32_t NetsysController::InterfaceSetMtu(const std::string &ifName, int32_t mtu)
164 {
165     NETMGR_LOG_D("Set mtu: ifName[%{public}s], mtu[%{public}d]", ifName.c_str(), mtu);
166     if (netsysService_ == nullptr) {
167         NETMGR_LOG_E("netsysService_ is null");
168         return NETSYS_NETSYSSERVICE_NULL;
169     }
170     return netsysService_->InterfaceSetMtu(ifName, mtu);
171 }
172 
InterfaceAddAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)173 int32_t NetsysController::InterfaceAddAddress(const std::string &ifName, const std::string &ipAddr,
174                                               int32_t prefixLength)
175 {
176     NETMGR_LOG_D("Add address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]", ifName.c_str(),
177                  ToAnonymousIp(ipAddr).c_str(), prefixLength);
178     if (netsysService_ == nullptr) {
179         NETMGR_LOG_E("netsysService_ is null");
180         return NETSYS_NETSYSSERVICE_NULL;
181     }
182     return netsysService_->InterfaceAddAddress(ifName, ipAddr, prefixLength);
183 }
184 
InterfaceDelAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)185 int32_t NetsysController::InterfaceDelAddress(const std::string &ifName, const std::string &ipAddr,
186                                               int32_t prefixLength)
187 {
188     NETMGR_LOG_D("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]", ifName.c_str(),
189                  ToAnonymousIp(ipAddr).c_str(), prefixLength);
190     if (netsysService_ == nullptr) {
191         NETMGR_LOG_E("netsysService_ is null");
192         return NETSYS_NETSYSSERVICE_NULL;
193     }
194     return netsysService_->InterfaceDelAddress(ifName, ipAddr, prefixLength);
195 }
196 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)197 int32_t NetsysController::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
198 {
199     NETMGR_LOG_D("Set Ip Address: ifName[%{public}s]", ifaceName.c_str());
200     if (netsysService_ == nullptr) {
201         NETMGR_LOG_E("netsysService_ is null");
202         return NETSYS_NETSYSSERVICE_NULL;
203     }
204     return netsysService_->InterfaceSetIpAddress(ifaceName, ipAddress);
205 }
206 
InterfaceSetIffUp(const std::string & ifaceName)207 int32_t NetsysController::InterfaceSetIffUp(const std::string &ifaceName)
208 {
209     NETMGR_LOG_D("Set Iff Up: ifName[%{public}s]", ifaceName.c_str());
210     if (netsysService_ == nullptr) {
211         NETMGR_LOG_E("netsysService_ is null");
212         return NETSYS_NETSYSSERVICE_NULL;
213     }
214     return netsysService_->InterfaceSetIffUp(ifaceName);
215 }
216 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)217 int32_t NetsysController::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
218                                             const std::vector<std::string> &servers,
219                                             const std::vector<std::string> &domains)
220 {
221     NETMGR_LOG_D("Set resolver config: netId[%{public}d]", netId);
222     if (netsysService_ == nullptr) {
223         NETMGR_LOG_E("netsysService_ is null");
224         return NETSYS_NETSYSSERVICE_NULL;
225     }
226     return netsysService_->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
227 }
228 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)229 int32_t NetsysController::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
230                                             std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
231                                             uint8_t &retryCount)
232 {
233     NETMGR_LOG_D("Get resolver config: netId[%{public}d]", netId);
234     if (netsysService_ == nullptr) {
235         NETMGR_LOG_E("netsysService_ is null");
236         return NETSYS_NETSYSSERVICE_NULL;
237     }
238     return netsysService_->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
239 }
240 
CreateNetworkCache(uint16_t netId)241 int32_t NetsysController::CreateNetworkCache(uint16_t netId)
242 {
243     NETMGR_LOG_D("create dns cache: netId[%{public}d]", netId);
244     if (netsysService_ == nullptr) {
245         NETMGR_LOG_E("netsysService_ is null");
246         return NETSYS_NETSYSSERVICE_NULL;
247     }
248     return netsysService_->CreateNetworkCache(netId);
249 }
250 
DestroyNetworkCache(uint16_t netId)251 int32_t NetsysController::DestroyNetworkCache(uint16_t netId)
252 {
253     NETMGR_LOG_D("Destroy dns cache: netId[%{public}d]", netId);
254     if (netsysService_ == nullptr) {
255         NETMGR_LOG_E("netsysService_ is null");
256         return NETSYS_NETSYSSERVICE_NULL;
257     }
258     return netsysService_->DestroyNetworkCache(netId);
259 }
260 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)261 int32_t NetsysController::GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
262                                       uint16_t netId, std::vector<AddrInfo> &res)
263 {
264     if (netsysService_ == nullptr) {
265         NETMGR_LOG_E("netsysService_ is null");
266         return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
267     }
268     return netsysService_->GetAddrInfo(hostName, serverName, hints, netId, res);
269 }
270 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,nmd::NetworkSharingTraffic & traffic)271 int32_t NetsysController::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
272                                                    nmd::NetworkSharingTraffic &traffic)
273 {
274     NETMGR_LOG_D("NetsysController GetNetworkSharingTraffic");
275     if (netsysService_ == nullptr) {
276         NETMGR_LOG_E("netsysService_ is null");
277         return NETSYS_NETSYSSERVICE_NULL;
278     }
279     return netsysService_->GetNetworkSharingTraffic(downIface, upIface, traffic);
280 }
281 
GetCellularRxBytes()282 int64_t NetsysController::GetCellularRxBytes()
283 {
284     NETMGR_LOG_D("NetsysController GetCellularRxBytes");
285     if (netsysService_ == nullptr) {
286         NETMGR_LOG_E("netsysService_ is null");
287         return NETSYS_NETSYSSERVICE_NULL;
288     }
289     return netsysService_->GetCellularRxBytes();
290 }
291 
GetCellularTxBytes()292 int64_t NetsysController::GetCellularTxBytes()
293 {
294     NETMGR_LOG_D("NetsysController GetCellularTxBytes");
295     if (netsysService_ == nullptr) {
296         NETMGR_LOG_E("netsysService_ is null");
297         return NETSYS_NETSYSSERVICE_NULL;
298     }
299     return netsysService_->GetCellularTxBytes();
300 }
301 
GetAllRxBytes()302 int64_t NetsysController::GetAllRxBytes()
303 {
304     NETMGR_LOG_D("NetsysController GetAllRxBytes");
305     if (netsysService_ == nullptr) {
306         NETMGR_LOG_E("netsysService_ is null");
307         return NETSYS_NETSYSSERVICE_NULL;
308     }
309     return netsysService_->GetAllRxBytes();
310 }
311 
GetAllTxBytes()312 int64_t NetsysController::GetAllTxBytes()
313 {
314     NETMGR_LOG_D("NetsysController GetAllTxBytes");
315     if (netsysService_ == nullptr) {
316         NETMGR_LOG_E("netsysService_ is null");
317         return NETSYS_NETSYSSERVICE_NULL;
318     }
319     return netsysService_->GetAllTxBytes();
320 }
321 
GetUidRxBytes(uint32_t uid)322 int64_t NetsysController::GetUidRxBytes(uint32_t uid)
323 {
324     NETMGR_LOG_D("NetsysController GetUidRxBytes");
325     if (netsysService_ == nullptr) {
326         NETMGR_LOG_E("netsysService_ is null");
327         return NETSYS_NETSYSSERVICE_NULL;
328     }
329     return netsysService_->GetUidRxBytes(uid);
330 }
331 
GetUidTxBytes(uint32_t uid)332 int64_t NetsysController::GetUidTxBytes(uint32_t uid)
333 {
334     NETMGR_LOG_D("NetsysController GetUidTxBytes");
335     if (netsysService_ == nullptr) {
336         NETMGR_LOG_E("netsysService_ is null");
337         return NETSYS_NETSYSSERVICE_NULL;
338     }
339     return netsysService_->GetUidTxBytes(uid);
340 }
341 
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)342 int64_t NetsysController::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
343 {
344     NETMGR_LOG_D("NetsysController GetUidOnIfaceRxBytes");
345     if (netsysService_ == nullptr) {
346         NETMGR_LOG_E("netsysService_ is null");
347         return NETSYS_NETSYSSERVICE_NULL;
348     }
349     return netsysService_->GetUidOnIfaceRxBytes(uid, interfaceName);
350 }
351 
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)352 int64_t NetsysController::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
353 {
354     NETMGR_LOG_D("NetsysController GetUidOnIfaceTxBytes");
355     if (netsysService_ == nullptr) {
356         NETMGR_LOG_E("netsysService_ is null");
357         return NETSYS_NETSYSSERVICE_NULL;
358     }
359     return netsysService_->GetUidOnIfaceTxBytes(uid, interfaceName);
360 }
361 
GetIfaceRxBytes(const std::string & interfaceName)362 int64_t NetsysController::GetIfaceRxBytes(const std::string &interfaceName)
363 {
364     NETMGR_LOG_D("NetsysController GetIfaceRxBytes");
365     if (netsysService_ == nullptr) {
366         NETMGR_LOG_E("netsysService_ is null");
367         return NETSYS_NETSYSSERVICE_NULL;
368     }
369     return netsysService_->GetIfaceRxBytes(interfaceName);
370 }
371 
GetIfaceTxBytes(const std::string & interfaceName)372 int64_t NetsysController::GetIfaceTxBytes(const std::string &interfaceName)
373 {
374     NETMGR_LOG_D("NetsysController GetIfaceTxBytes");
375     if (netsysService_ == nullptr) {
376         NETMGR_LOG_E("netsysService_ is null");
377         return NETSYS_NETSYSSERVICE_NULL;
378     }
379     return netsysService_->GetIfaceTxBytes(interfaceName);
380 }
381 
InterfaceGetList()382 std::vector<std::string> NetsysController::InterfaceGetList()
383 {
384     NETMGR_LOG_D("NetsysController InterfaceGetList");
385     if (netsysService_ == nullptr) {
386         NETMGR_LOG_E("netsysService_ is null");
387         return {};
388     }
389     return netsysService_->InterfaceGetList();
390 }
391 
UidGetList()392 std::vector<std::string> NetsysController::UidGetList()
393 {
394     NETMGR_LOG_D("NetsysController UidGetList");
395     if (netsysService_ == nullptr) {
396         NETMGR_LOG_E("netsysService_ is null");
397         return {};
398     }
399     return netsysService_->UidGetList();
400 }
401 
GetIfaceRxPackets(const std::string & interfaceName)402 int64_t NetsysController::GetIfaceRxPackets(const std::string &interfaceName)
403 {
404     NETMGR_LOG_D("NetsysController GetIfaceRxPackets");
405     if (netsysService_ == nullptr) {
406         NETMGR_LOG_E("netsysService_ is null");
407         return NETSYS_NETSYSSERVICE_NULL;
408     }
409     return netsysService_->GetIfaceRxPackets(interfaceName);
410 }
411 
GetIfaceTxPackets(const std::string & interfaceName)412 int64_t NetsysController::GetIfaceTxPackets(const std::string &interfaceName)
413 {
414     NETMGR_LOG_D("NetsysController GetIfaceTxPackets");
415     if (netsysService_ == nullptr) {
416         NETMGR_LOG_E("netsysService_ is null");
417         return NETSYS_NETSYSSERVICE_NULL;
418     }
419     return netsysService_->GetIfaceTxPackets(interfaceName);
420 }
421 
SetDefaultNetWork(int32_t netId)422 int32_t NetsysController::SetDefaultNetWork(int32_t netId)
423 {
424     NETMGR_LOG_D("Set DefaultNetWork: netId[%{public}d]", netId);
425     if (netsysService_ == nullptr) {
426         NETMGR_LOG_E("netsysService_ is null");
427         return NETSYS_NETSYSSERVICE_NULL;
428     }
429     return netsysService_->SetDefaultNetWork(netId);
430 }
431 
ClearDefaultNetWorkNetId()432 int32_t NetsysController::ClearDefaultNetWorkNetId()
433 {
434     NETMGR_LOG_D("ClearDefaultNetWorkNetId");
435     if (netsysService_ == nullptr) {
436         NETMGR_LOG_E("netsysService_ is null");
437         return NETSYS_NETSYSSERVICE_NULL;
438     }
439     return netsysService_->ClearDefaultNetWorkNetId();
440 }
441 
BindSocket(int32_t socketFd,uint32_t netId)442 int32_t NetsysController::BindSocket(int32_t socketFd, uint32_t netId)
443 {
444     NETMGR_LOG_D("NetsysController::BindSocket: netId = [%{public}u]", netId);
445     if (netsysService_ == nullptr) {
446         NETMGR_LOG_E("netsysService_ is null");
447         return NETSYS_NETSYSSERVICE_NULL;
448     }
449     return netsysService_->BindSocket(socketFd, netId);
450 }
451 
IpEnableForwarding(const std::string & requestor)452 int32_t NetsysController::IpEnableForwarding(const std::string &requestor)
453 {
454     NETMGR_LOG_D("IpEnableForwarding: requestor[%{public}s]", requestor.c_str());
455     if (netsysService_ == nullptr) {
456         NETMGR_LOG_E("netsysService_ is null");
457         return NETSYS_NETSYSSERVICE_NULL;
458     }
459     return netsysService_->IpEnableForwarding(requestor);
460 }
461 
IpDisableForwarding(const std::string & requestor)462 int32_t NetsysController::IpDisableForwarding(const std::string &requestor)
463 {
464     NETMGR_LOG_D("IpDisableForwarding: requestor[%{public}s]", requestor.c_str());
465     if (netsysService_ == nullptr) {
466         NETMGR_LOG_E("netsysService_ is null");
467         return NETSYS_NETSYSSERVICE_NULL;
468     }
469     return netsysService_->IpDisableForwarding(requestor);
470 }
471 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)472 int32_t NetsysController::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
473 {
474     NETMGR_LOG_D("MockNetsysNativeClient EnableNat: intIface[%{public}s] intIface[%{public}s]",
475                  downstreamIface.c_str(), upstreamIface.c_str());
476     if (netsysService_ == nullptr) {
477         NETMGR_LOG_E("netsysService_ is null");
478         return NETSYS_NETSYSSERVICE_NULL;
479     }
480     return netsysService_->EnableNat(downstreamIface, upstreamIface);
481 }
482 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)483 int32_t NetsysController::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
484 {
485     NETMGR_LOG_D("MockNetsysNativeClient DisableNat: intIface[%{public}s] intIface[%{public}s]",
486                  downstreamIface.c_str(), upstreamIface.c_str());
487     if (netsysService_ == nullptr) {
488         NETMGR_LOG_E("netsysService_ is null");
489         return NETSYS_NETSYSSERVICE_NULL;
490     }
491     return netsysService_->DisableNat(downstreamIface, upstreamIface);
492 }
493 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)494 int32_t NetsysController::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
495 {
496     NETMGR_LOG_D("IpfwdAddInterfaceForward: fromIface[%{public}s], toIface[%{public}s]", fromIface.c_str(),
497                  toIface.c_str());
498     if (netsysService_ == nullptr) {
499         NETMGR_LOG_E("netsysService_ is null");
500         return NETSYS_NETSYSSERVICE_NULL;
501     }
502     return netsysService_->IpfwdAddInterfaceForward(fromIface, toIface);
503 }
504 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)505 int32_t NetsysController::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
506 {
507     NETMGR_LOG_D("IpfwdRemoveInterfaceForward: fromIface[%{public}s], toIface[%{public}s]", fromIface.c_str(),
508                  toIface.c_str());
509     if (netsysService_ == nullptr) {
510         NETMGR_LOG_E("netsysService_ is null");
511         return NETSYS_NETSYSSERVICE_NULL;
512     }
513     return netsysService_->IpfwdRemoveInterfaceForward(fromIface, toIface);
514 }
515 
ShareDnsSet(uint16_t netId)516 int32_t NetsysController::ShareDnsSet(uint16_t netId)
517 {
518     NETMGR_LOG_D("ShareDnsSet: netId[%{public}d]", netId);
519     if (netsysService_ == nullptr) {
520         NETMGR_LOG_E("netsysService_ is null");
521         return NETSYS_NETSYSSERVICE_NULL;
522     }
523     return netsysService_->ShareDnsSet(netId);
524 }
525 
StartDnsProxyListen()526 int32_t NetsysController::StartDnsProxyListen()
527 {
528     NETMGR_LOG_D("NetsysController::StartDnsProxyListen");
529     if (netsysService_ == nullptr) {
530         NETMGR_LOG_E("netsysService_ is null");
531         return NETSYS_NETSYSSERVICE_NULL;
532     }
533     return netsysService_->StartDnsProxyListen();
534 }
535 
StopDnsProxyListen()536 int32_t NetsysController::StopDnsProxyListen()
537 {
538     NETMGR_LOG_D("NetsysController::StopDnsProxyListen");
539     if (netsysService_ == nullptr) {
540         NETMGR_LOG_E("netsysService_ is null");
541         return NETSYS_NETSYSSERVICE_NULL;
542     }
543     return netsysService_->StopDnsProxyListen();
544 }
545 
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)546 int32_t NetsysController::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
547 {
548     if (netsysService_ == nullptr) {
549         NETMGR_LOG_E("netsysService_ is null");
550         return NETSYS_NETSYSSERVICE_NULL;
551     }
552     return netsysService_->RegisterNetsysNotifyCallback(callback);
553 }
554 
BindNetworkServiceVpn(int32_t socketFd)555 int32_t NetsysController::BindNetworkServiceVpn(int32_t socketFd)
556 {
557     NETMGR_LOG_D("NetsysController::BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
558     if (socketFd <= 0) {
559         NETMGR_LOG_E("socketFd is null");
560         return NETSYS_ERR_VPN;
561     }
562     if (netsysService_ == nullptr) {
563         NETMGR_LOG_E("netsysService_ is null");
564         return NETSYS_NETSYSSERVICE_NULL;
565     }
566     return netsysService_->BindNetworkServiceVpn(socketFd);
567 }
568 
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)569 int32_t NetsysController::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
570 {
571     NETMGR_LOG_D("NetsysController::EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
572     if (socketFd <= 0) {
573         NETMGR_LOG_E("socketFd is null");
574         return NETSYS_ERR_VPN;
575     }
576     if (netsysService_ == nullptr) {
577         NETMGR_LOG_E("netsysService_ is null");
578         return NETSYS_NETSYSSERVICE_NULL;
579     }
580     return netsysService_->EnableVirtualNetIfaceCard(socketFd, ifRequest, ifaceFd);
581 }
582 
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)583 int32_t NetsysController::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
584                                        struct ifreq &ifRequest)
585 {
586     NETMGR_LOG_D("NetsysController::set addr");
587     if ((socketFd <= 0) || (ipAddress.length() == 0) || (ipAddress.length() > MAX_IPV4_ADDRESS_LEN) ||
588         (prefixLen <= 0) || (prefixLen > MAX_IPV4_ADDRESS_LEN)) {
589         NETMGR_LOG_E(
590             "The paramemters of SetIpAddress is failed, socketFd[%{public}d], "
591             "ipAddress[%{public}s], prefixLen[%{public}d].",
592             socketFd, ToAnonymousIp(ipAddress).c_str(), prefixLen);
593         return NETSYS_ERR_VPN;
594     }
595     if (netsysService_ == nullptr) {
596         NETMGR_LOG_E("netsysService_ is null");
597         return NETSYS_NETSYSSERVICE_NULL;
598     }
599     return netsysService_->SetIpAddress(socketFd, ipAddress, prefixLen, ifRequest);
600 }
601 
SetBlocking(int32_t ifaceFd,bool isBlock)602 int32_t NetsysController::SetBlocking(int32_t ifaceFd, bool isBlock)
603 {
604     NETMGR_LOG_D("NetsysController::SetBlocking: ifaceFd[%{public}d], isBlock[%{public}d]", ifaceFd, isBlock);
605     if (netsysService_ == nullptr) {
606         NETMGR_LOG_E("netsysService_ is null");
607         return NETSYS_NETSYSSERVICE_NULL;
608     }
609     return netsysService_->SetBlocking(ifaceFd, isBlock);
610 }
611 
StartDhcpClient(const std::string & iface,bool bIpv6)612 int32_t NetsysController::StartDhcpClient(const std::string &iface, bool bIpv6)
613 {
614     NETMGR_LOG_D("NetsysController::StartDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
615     if (netsysService_ == nullptr) {
616         NETMGR_LOG_E("netsysService_ is null");
617         return NETSYS_NETSYSSERVICE_NULL;
618     }
619     return netsysService_->StartDhcpClient(iface, bIpv6);
620 }
621 
StopDhcpClient(const std::string & iface,bool bIpv6)622 int32_t NetsysController::StopDhcpClient(const std::string &iface, bool bIpv6)
623 {
624     NETMGR_LOG_D("NetsysController::SetBlocking: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
625     if (netsysService_ == nullptr) {
626         NETMGR_LOG_E("netsysService_ is null");
627         return NETSYS_NETSYSSERVICE_NULL;
628     }
629     return netsysService_->StopDhcpClient(iface, bIpv6);
630 }
631 
RegisterCallback(sptr<NetsysControllerCallback> callback)632 int32_t NetsysController::RegisterCallback(sptr<NetsysControllerCallback> callback)
633 {
634     NETMGR_LOG_D("NetsysController::RegisterCallback");
635     return netsysService_->RegisterCallback(callback);
636 }
637 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)638 int32_t NetsysController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
639 {
640     NETMGR_LOG_D("NetsysController::StartDhcpService: iface[%{public}s], ipv4addr[%{public}s]", iface.c_str(),
641                  ToAnonymousIp(ipv4addr).c_str());
642     if (netsysService_ == nullptr) {
643         NETMGR_LOG_E("netsysService_ is null");
644         return NETSYS_NETSYSSERVICE_NULL;
645     }
646     return netsysService_->StartDhcpService(iface, ipv4addr);
647 }
648 
StopDhcpService(const std::string & iface)649 int32_t NetsysController::StopDhcpService(const std::string &iface)
650 {
651     NETMGR_LOG_D("NetsysController::StopDhcpService: ifaceFd[%{public}s]", iface.c_str());
652     if (netsysService_ == nullptr) {
653         NETMGR_LOG_E("netsysService_ is null");
654         return NETSYS_NETSYSSERVICE_NULL;
655     }
656     return netsysService_->StopDhcpService(iface);
657 }
658 
BandwidthEnableDataSaver(bool enable)659 int32_t NetsysController::BandwidthEnableDataSaver(bool enable)
660 {
661     NETMGR_LOG_D("NetsysController::BandwidthEnableDataSaver: enable=%{public}d", enable);
662     if (netsysService_ == nullptr) {
663         NETMGR_LOG_E("netsysService_ is null");
664         return NETSYS_NETSYSSERVICE_NULL;
665     }
666     return netsysService_->BandwidthEnableDataSaver(enable);
667 }
668 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)669 int32_t NetsysController::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
670 {
671     NETMGR_LOG_D("NetsysController::BandwidthSetIfaceQuota: ifName=%{public}s", ifName.c_str());
672     if (netsysService_ == nullptr) {
673         NETMGR_LOG_E("netsysService_ is null");
674         return NETSYS_NETSYSSERVICE_NULL;
675     }
676     return netsysService_->BandwidthSetIfaceQuota(ifName, bytes);
677 }
678 
BandwidthRemoveIfaceQuota(const std::string & ifName)679 int32_t NetsysController::BandwidthRemoveIfaceQuota(const std::string &ifName)
680 {
681     NETMGR_LOG_D("NetsysController::BandwidthRemoveIfaceQuota: ifName=%{public}s", ifName.c_str());
682     if (netsysService_ == nullptr) {
683         NETMGR_LOG_E("netsysService_ is null");
684         return NETSYS_NETSYSSERVICE_NULL;
685     }
686     return netsysService_->BandwidthRemoveIfaceQuota(ifName);
687 }
688 
BandwidthAddDeniedList(uint32_t uid)689 int32_t NetsysController::BandwidthAddDeniedList(uint32_t uid)
690 {
691     NETMGR_LOG_D("NetsysController::BandwidthAddDeniedList: uid=%{public}d", uid);
692     if (netsysService_ == nullptr) {
693         NETMGR_LOG_E("netsysService_ is null");
694         return NETSYS_NETSYSSERVICE_NULL;
695     }
696     return netsysService_->BandwidthAddDeniedList(uid);
697 }
698 
BandwidthRemoveDeniedList(uint32_t uid)699 int32_t NetsysController::BandwidthRemoveDeniedList(uint32_t uid)
700 {
701     NETMGR_LOG_D("NetsysController::BandwidthRemoveDeniedList: uid=%{public}d", uid);
702     if (netsysService_ == nullptr) {
703         NETMGR_LOG_E("netsysService_ is null");
704         return NETSYS_NETSYSSERVICE_NULL;
705     }
706     return netsysService_->BandwidthRemoveDeniedList(uid);
707 }
708 
BandwidthAddAllowedList(uint32_t uid)709 int32_t NetsysController::BandwidthAddAllowedList(uint32_t uid)
710 {
711     NETMGR_LOG_D("NetsysController::BandwidthAddAllowedList: uid=%{public}d", uid);
712     if (netsysService_ == nullptr) {
713         NETMGR_LOG_E("netsysService_ is null");
714         return NETSYS_NETSYSSERVICE_NULL;
715     }
716     return netsysService_->BandwidthAddAllowedList(uid);
717 }
718 
BandwidthRemoveAllowedList(uint32_t uid)719 int32_t NetsysController::BandwidthRemoveAllowedList(uint32_t uid)
720 {
721     NETMGR_LOG_D("NetsysController::BandwidthRemoveAllowedList: uid=%{public}d", uid);
722     if (netsysService_ == nullptr) {
723         NETMGR_LOG_E("netsysService_ is null");
724         return NETSYS_NETSYSSERVICE_NULL;
725     }
726     return netsysService_->BandwidthRemoveAllowedList(uid);
727 }
728 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)729 int32_t NetsysController::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
730 {
731     NETMGR_LOG_D("NetsysController::FirewallSetUidsAllowedListChain: chain=%{public}d", chain);
732     if (netsysService_ == nullptr) {
733         NETMGR_LOG_E("netsysService_ is null");
734         return NETSYS_NETSYSSERVICE_NULL;
735     }
736     return netsysService_->FirewallSetUidsAllowedListChain(chain, uids);
737 }
738 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)739 int32_t NetsysController::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
740 {
741     NETMGR_LOG_D("NetsysController::FirewallSetUidsDeniedListChain: chain=%{public}d", chain);
742     if (netsysService_ == nullptr) {
743         NETMGR_LOG_E("netsysService_ is null");
744         return NETSYS_NETSYSSERVICE_NULL;
745     }
746     return netsysService_->FirewallSetUidsDeniedListChain(chain, uids);
747 }
748 
FirewallEnableChain(uint32_t chain,bool enable)749 int32_t NetsysController::FirewallEnableChain(uint32_t chain, bool enable)
750 {
751     NETMGR_LOG_D("NetsysController::FirewallEnableChain: chain=%{public}d, enable=%{public}d", chain, enable);
752     if (netsysService_ == nullptr) {
753         NETMGR_LOG_E("netsysService_ is null");
754         return NETSYS_NETSYSSERVICE_NULL;
755     }
756     return netsysService_->FirewallEnableChain(chain, enable);
757 }
758 
FirewallSetUidRule(uint32_t chain,uint32_t uid,uint32_t firewallRule)759 int32_t NetsysController::FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule)
760 {
761     NETMGR_LOG_D("NetsysController::FirewallSetUidRule: chain=%{public}d,uid=%{public}d,firewallRule=%{public}d",
762                  chain, uid, firewallRule);
763     if (netsysService_ == nullptr) {
764         NETMGR_LOG_E("netsysService_ is null");
765         return NETSYS_NETSYSSERVICE_NULL;
766     }
767     return netsysService_->FirewallSetUidRule(chain, uid, firewallRule);
768 }
769 
FreeAddrInfo(addrinfo * aihead)770 void NetsysController::FreeAddrInfo(addrinfo *aihead)
771 {
772     addrinfo *tmpNext = nullptr;
773     for (addrinfo *tmp = aihead; tmp != nullptr;) {
774         if (tmp->ai_addr != nullptr) {
775             free(tmp->ai_addr);
776         }
777         if (tmp->ai_canonname != nullptr) {
778             free(tmp->ai_canonname);
779         }
780         tmpNext = tmp->ai_next;
781         free(tmp);
782         tmp = tmpNext;
783     }
784 }
785 } // namespace NetManagerStandard
786 } // namespace OHOS
787