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