• 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 
16 #include <arpa/inet.h>
17 #include <cstdlib>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <linux/if_tun.h>
21 #include <net/route.h>
22 #include <netinet/in.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <thread>
28 #include <unistd.h>
29 
30 #include "iservice_registry.h"
31 #include "securec.h"
32 #include "system_ability_definition.h"
33 
34 #include "net_conn_constants.h"
35 #include "net_conn_types.h"
36 #include "net_manager_constants.h"
37 #include "net_mgr_log_wrapper.h"
38 #include "netmanager_base_common_utils.h"
39 #include "netsys_native_client.h"
40 #include "netsys_native_service_proxy.h"
41 
42 using namespace OHOS::NetManagerStandard::CommonUtils;
43 namespace OHOS {
44 namespace NetManagerStandard {
45 static constexpr const char *DEV_NET_TUN_PATH = "/dev/net/tun";
46 static constexpr const char *IF_CFG_UP = "up";
47 static constexpr const char *IF_CFG_DOWN = "down";
48 static constexpr const char *NETSYS_ROUTE_INIT_DIR_PATH = "/data/service/el1/public/netmanager/route";
49 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_S = 1;
50 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 30;
51 
NativeNotifyCallback(NetsysNativeClient & netsysNativeClient)52 NetsysNativeClient::NativeNotifyCallback::NativeNotifyCallback(NetsysNativeClient &netsysNativeClient)
53     : netsysNativeClient_(netsysNativeClient)
54 {
55 }
56 
~NativeNotifyCallback()57 NetsysNativeClient::NativeNotifyCallback::~NativeNotifyCallback() {}
58 
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int flags,int scope)59 int32_t NetsysNativeClient::NativeNotifyCallback::OnInterfaceAddressUpdated(const std::string &addr,
60                                                                             const std::string &ifName, int flags,
61                                                                             int scope)
62 {
63     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
64     for (auto &cb : netsysNativeClient_.cbObjects_) {
65         cb->OnInterfaceAddressUpdated(addr, ifName, flags, scope);
66     }
67     return NETMANAGER_SUCCESS;
68 }
69 
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)70 int32_t NetsysNativeClient::NativeNotifyCallback::OnInterfaceAddressRemoved(const std::string &addr,
71                                                                             const std::string &ifName, int flags,
72                                                                             int scope)
73 {
74     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
75     for (auto &cb : netsysNativeClient_.cbObjects_) {
76         cb->OnInterfaceAddressRemoved(addr, ifName, flags, scope);
77     }
78     return NETMANAGER_SUCCESS;
79 }
80 
OnInterfaceAdded(const std::string & ifName)81 int32_t NetsysNativeClient::NativeNotifyCallback::OnInterfaceAdded(const std::string &ifName)
82 {
83     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
84     for (auto &cb : netsysNativeClient_.cbObjects_) {
85         cb->OnInterfaceAdded(ifName);
86     }
87     return NETMANAGER_SUCCESS;
88 }
89 
OnInterfaceRemoved(const std::string & ifName)90 int32_t NetsysNativeClient::NativeNotifyCallback::OnInterfaceRemoved(const std::string &ifName)
91 {
92     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
93     for (auto &cb : netsysNativeClient_.cbObjects_) {
94         cb->OnInterfaceRemoved(ifName);
95     }
96     return NETMANAGER_SUCCESS;
97 }
98 
OnInterfaceChanged(const std::string & ifName,bool up)99 int32_t NetsysNativeClient::NativeNotifyCallback::OnInterfaceChanged(const std::string &ifName, bool up)
100 {
101     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
102     for (auto &cb : netsysNativeClient_.cbObjects_) {
103         cb->OnInterfaceChanged(ifName, up);
104     }
105     return NETMANAGER_SUCCESS;
106 }
107 
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)108 int32_t NetsysNativeClient::NativeNotifyCallback::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
109 {
110     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
111     for (auto &cb : netsysNativeClient_.cbObjects_) {
112         cb->OnInterfaceLinkStateChanged(ifName, up);
113     }
114     return NETMANAGER_SUCCESS;
115 }
116 
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)117 int32_t NetsysNativeClient::NativeNotifyCallback::OnRouteChanged(bool updated, const std::string &route,
118                                                                  const std::string &gateway, const std::string &ifName)
119 {
120     std::lock_guard lock(netsysNativeClient_.cbObjMutex_);
121     for (auto &cb : netsysNativeClient_.cbObjects_) {
122         cb->OnRouteChanged(updated, route, gateway, ifName);
123     }
124     return NETMANAGER_SUCCESS;
125 }
126 
OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> & dhcpResult)127 int32_t NetsysNativeClient::NativeNotifyCallback::OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult)
128 {
129     NETMGR_LOG_I("NetsysNativeClient::NativeNotifyCallback::OnDhcpSuccess");
130     netsysNativeClient_.ProcessDhcpResult(dhcpResult);
131     return NETMANAGER_SUCCESS;
132 }
133 
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)134 int32_t NetsysNativeClient::NativeNotifyCallback::OnBandwidthReachedLimit(const std::string &limitName,
135                                                                           const std::string &iface)
136 {
137     NETMGR_LOG_I("NetsysNativeClient::NativeNotifyCallback::OnBandwidthReachedLimit");
138     netsysNativeClient_.ProcessBandwidthReachedLimit(limitName, iface);
139     return NETMANAGER_SUCCESS;
140 }
141 
NetsysNativeClient()142 NetsysNativeClient::NetsysNativeClient()
143 {
144     std::thread([this]() {
145         uint32_t count = 0;
146         while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
147             std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_S));
148             count++;
149         }
150         NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", GetProxy() == nullptr ? "failed" : "success", count);
151     }).detach();
152 }
153 
NetworkCreatePhysical(int32_t netId,int32_t permission)154 int32_t NetsysNativeClient::NetworkCreatePhysical(int32_t netId, int32_t permission)
155 {
156     NETMGR_LOG_I("Create Physical network: netId[%{public}d], permission[%{public}d]", netId, permission);
157     auto proxy = GetProxy();
158     if (proxy == nullptr) {
159         NETMGR_LOG_E("proxy is nullptr");
160         return NETMANAGER_ERR_GET_PROXY_FAIL;
161     }
162     return proxy->NetworkCreatePhysical(netId, permission);
163 }
164 
NetworkDestroy(int32_t netId)165 int32_t NetsysNativeClient::NetworkDestroy(int32_t netId)
166 {
167     NETMGR_LOG_I("Destroy network: netId[%{public}d]", netId);
168     auto proxy = GetProxy();
169     if (proxy == nullptr) {
170         NETMGR_LOG_E("proxy is nullptr");
171         return NETMANAGER_ERR_GET_PROXY_FAIL;
172     }
173     return proxy->NetworkDestroy(netId);
174 }
175 
NetworkAddInterface(int32_t netId,const std::string & iface)176 int32_t NetsysNativeClient::NetworkAddInterface(int32_t netId, const std::string &iface)
177 {
178     NETMGR_LOG_I("Add network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
179     auto proxy = GetProxy();
180     if (proxy == nullptr) {
181         NETMGR_LOG_E("proxy is nullptr");
182         return NETMANAGER_ERR_GET_PROXY_FAIL;
183     }
184     return proxy->NetworkAddInterface(netId, iface);
185 }
186 
NetworkRemoveInterface(int32_t netId,const std::string & iface)187 int32_t NetsysNativeClient::NetworkRemoveInterface(int32_t netId, const std::string &iface)
188 {
189     NETMGR_LOG_I("Remove network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
190     auto proxy = GetProxy();
191     if (proxy == nullptr) {
192         NETMGR_LOG_E("proxy is nullptr");
193         return NETMANAGER_ERR_GET_PROXY_FAIL;
194     }
195     return proxy->NetworkRemoveInterface(netId, iface);
196 }
197 
NetworkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)198 int32_t NetsysNativeClient::NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
199                                             const std::string &nextHop)
200 {
201     NETMGR_LOG_I("Add Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
202                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
203     auto proxy = GetProxy();
204     if (proxy == nullptr) {
205         NETMGR_LOG_E("proxy is nullptr");
206         return NETMANAGER_ERR_GET_PROXY_FAIL;
207     }
208     return proxy->NetworkAddRoute(netId, ifName, destination, nextHop);
209 }
210 
NetworkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)211 int32_t NetsysNativeClient::NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
212                                                const std::string &nextHop)
213 {
214     NETMGR_LOG_D("Remove Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
215                  netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
216     auto proxy = GetProxy();
217     if (proxy == nullptr) {
218         NETMGR_LOG_E("proxy is nullptr");
219         return NETMANAGER_ERR_GET_PROXY_FAIL;
220     }
221     return proxy->NetworkRemoveRoute(netId, ifName, destination, nextHop);
222 }
223 
InterfaceGetConfig(OHOS::nmd::InterfaceConfigurationParcel & cfg)224 int32_t NetsysNativeClient::InterfaceGetConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg)
225 {
226     NETMGR_LOG_D("Get interface config: ifName[%{public}s]", cfg.ifName.c_str());
227     auto proxy = GetProxy();
228     if (proxy == nullptr) {
229         NETMGR_LOG_E("proxy is nullptr");
230         return NETMANAGER_ERR_GET_PROXY_FAIL;
231     }
232     return proxy->InterfaceGetConfig(cfg);
233 }
234 
SetInterfaceDown(const std::string & iface)235 int32_t NetsysNativeClient::SetInterfaceDown(const std::string &iface)
236 {
237     NETMGR_LOG_D("Set interface down: iface[%{public}s]", iface.c_str());
238     auto proxy = GetProxy();
239     if (proxy == nullptr) {
240         NETMGR_LOG_E("proxy is nullptr");
241         return NETMANAGER_ERR_GET_PROXY_FAIL;
242     }
243     OHOS::nmd::InterfaceConfigurationParcel ifCfg;
244     ifCfg.ifName = iface;
245     proxy->InterfaceGetConfig(ifCfg);
246     auto fit = std::find(ifCfg.flags.begin(), ifCfg.flags.end(), IF_CFG_UP);
247     if (fit != ifCfg.flags.end()) {
248         ifCfg.flags.erase(fit);
249     }
250     ifCfg.flags.push_back(IF_CFG_DOWN);
251     return proxy->InterfaceSetConfig(ifCfg);
252 }
253 
SetInterfaceUp(const std::string & iface)254 int32_t NetsysNativeClient::SetInterfaceUp(const std::string &iface)
255 {
256     NETMGR_LOG_D("Set interface up: iface[%{public}s]", iface.c_str());
257     auto proxy = GetProxy();
258     if (proxy == nullptr) {
259         NETMGR_LOG_E("proxy is nullptr");
260         return NETMANAGER_ERR_GET_PROXY_FAIL;
261     }
262     OHOS::nmd::InterfaceConfigurationParcel ifCfg;
263     ifCfg.ifName = iface;
264     proxy->InterfaceGetConfig(ifCfg);
265     auto fit = std::find(ifCfg.flags.begin(), ifCfg.flags.end(), IF_CFG_DOWN);
266     if (fit != ifCfg.flags.end()) {
267         ifCfg.flags.erase(fit);
268     }
269     ifCfg.flags.push_back(IF_CFG_UP);
270     return proxy->InterfaceSetConfig(ifCfg);
271 }
272 
InterfaceClearAddrs(const std::string & ifName)273 void NetsysNativeClient::InterfaceClearAddrs(const std::string &ifName)
274 {
275     NETMGR_LOG_D("Clear addrs: ifName[%{public}s]", ifName.c_str());
276     auto proxy = GetProxy();
277     if (proxy == nullptr) {
278         NETMGR_LOG_E("proxy is nullptr");
279         return;
280     }
281     return;
282 }
283 
InterfaceGetMtu(const std::string & ifName)284 int32_t NetsysNativeClient::InterfaceGetMtu(const std::string &ifName)
285 {
286     NETMGR_LOG_D("Get mtu: ifName[%{public}s]", ifName.c_str());
287     auto proxy = GetProxy();
288     if (proxy == nullptr) {
289         NETMGR_LOG_E("proxy is nullptr");
290         return NETMANAGER_ERR_GET_PROXY_FAIL;
291     }
292     return proxy->InterfaceGetMtu(ifName);
293 }
294 
InterfaceSetMtu(const std::string & ifName,int32_t mtu)295 int32_t NetsysNativeClient::InterfaceSetMtu(const std::string &ifName, int32_t mtu)
296 {
297     NETMGR_LOG_D("Set mtu: ifName[%{public}s], mtu[%{public}d]", ifName.c_str(), mtu);
298     auto proxy = GetProxy();
299     if (proxy == nullptr) {
300         NETMGR_LOG_E("proxy is nullptr");
301         return NETMANAGER_ERR_GET_PROXY_FAIL;
302     }
303     return proxy->InterfaceSetMtu(ifName, mtu);
304 }
305 
InterfaceAddAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)306 int32_t NetsysNativeClient::InterfaceAddAddress(const std::string &ifName, const std::string &ipAddr,
307                                                 int32_t prefixLength)
308 {
309     NETMGR_LOG_D("Add address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]", ifName.c_str(),
310                  ToAnonymousIp(ipAddr).c_str(), prefixLength);
311     auto proxy = GetProxy();
312     if (proxy == nullptr) {
313         NETMGR_LOG_E("proxy is nullptr");
314         return NETMANAGER_ERR_GET_PROXY_FAIL;
315     }
316     return proxy->InterfaceAddAddress(ifName, ipAddr, prefixLength);
317 }
318 
InterfaceDelAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)319 int32_t NetsysNativeClient::InterfaceDelAddress(const std::string &ifName, const std::string &ipAddr,
320                                                 int32_t prefixLength)
321 {
322     NETMGR_LOG_D("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]", ifName.c_str(),
323                  ToAnonymousIp(ipAddr).c_str(), prefixLength);
324     auto proxy = GetProxy();
325     if (proxy == nullptr) {
326         NETMGR_LOG_E("proxy is nullptr");
327         return NETMANAGER_ERR_GET_PROXY_FAIL;
328     }
329     return proxy->InterfaceDelAddress(ifName, ipAddr, prefixLength);
330 }
331 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)332 int32_t NetsysNativeClient::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
333 {
334     NETMGR_LOG_D("Set Ip Address: ifaceName[%{public}s], ipAddr[%{public}s]", ifaceName.c_str(),
335                  ToAnonymousIp(ipAddress).c_str());
336     auto proxy = GetProxy();
337     if (proxy == nullptr) {
338         NETMGR_LOG_E("proxy is nullptr");
339         return IPC_PROXY_ERR;
340     }
341     return proxy->InterfaceSetIpAddress(ifaceName, ipAddress);
342 }
343 
InterfaceSetIffUp(const std::string & ifaceName)344 int32_t NetsysNativeClient::InterfaceSetIffUp(const std::string &ifaceName)
345 {
346     NETMGR_LOG_D("Set Iff Up: ifaceName[%{public}s]", ifaceName.c_str());
347     auto proxy = GetProxy();
348     if (proxy == nullptr) {
349         NETMGR_LOG_E("proxy is nullptr");
350         return IPC_PROXY_ERR;
351     }
352     return proxy->InterfaceSetIffUp(ifaceName);
353 }
354 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)355 int32_t NetsysNativeClient::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
356                                               const std::vector<std::string> &servers,
357                                               const std::vector<std::string> &domains)
358 {
359     NETMGR_LOG_D("Set resolver config: netId[%{public}d]", netId);
360     auto proxy = GetProxy();
361     if (proxy == nullptr) {
362         NETMGR_LOG_E("proxy is nullptr");
363         return NETMANAGER_ERR_GET_PROXY_FAIL;
364     }
365     return proxy->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
366 }
367 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)368 int32_t NetsysNativeClient::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
369                                               std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
370                                               uint8_t &retryCount)
371 {
372     NETMGR_LOG_D("Get resolver config: netId[%{public}d]", netId);
373     auto proxy = GetProxy();
374     if (proxy == nullptr) {
375         NETMGR_LOG_E("proxy is nullptr");
376         return NETMANAGER_ERR_GET_PROXY_FAIL;
377     }
378     return proxy->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
379 }
380 
CreateNetworkCache(uint16_t netId)381 int32_t NetsysNativeClient::CreateNetworkCache(uint16_t netId)
382 {
383     NETMGR_LOG_D("create dns cache: netId[%{public}d]", netId);
384     auto proxy = GetProxy();
385     if (proxy == nullptr) {
386         NETMGR_LOG_E("proxy is nullptr");
387         return NETMANAGER_ERR_GET_PROXY_FAIL;
388     }
389     return proxy->CreateNetworkCache(netId);
390 }
391 
DestroyNetworkCache(uint16_t netId)392 int32_t NetsysNativeClient::DestroyNetworkCache(uint16_t netId)
393 {
394     NETMGR_LOG_D("Destroy dns cache: netId[%{public}d]", netId);
395     auto proxy = GetProxy();
396     if (proxy == nullptr) {
397         NETMGR_LOG_E("proxy is nullptr");
398         return NETMANAGER_ERR_GET_PROXY_FAIL;
399     }
400     return proxy->DestroyNetworkCache(netId);
401 }
402 
403 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)404 int32_t NetsysNativeClient::GetAddrInfo(const std::string &hostName, const std::string &serverName,
405                                         const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
406 {
407     if (netsysNativeService_ == nullptr) {
408         NETMGR_LOG_E("GetAddrInfo netsysNativeService_ is null");
409         return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
410     }
411     return netsysNativeService_->GetAddrInfo(hostName, serverName, hints, netId, res);
412 }
413 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,nmd::NetworkSharingTraffic & traffic)414 int32_t NetsysNativeClient::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
415                                                      nmd::NetworkSharingTraffic &traffic)
416 {
417     NETMGR_LOG_D("NetsysNativeClient GetNetworkSharingTraffic");
418     auto proxy = GetProxy();
419     if (proxy == nullptr) {
420         NETMGR_LOG_E("proxy is nullptr");
421         return NETMANAGER_ERR_GET_PROXY_FAIL;
422     }
423     return proxy->GetNetworkSharingTraffic(downIface, upIface, traffic);
424 }
425 
GetCellularRxBytes()426 int64_t NetsysNativeClient::GetCellularRxBytes()
427 {
428     NETMGR_LOG_D("NetsysNativeClient GetCellularRxBytes");
429     auto proxy = GetProxy();
430     if (proxy == nullptr) {
431         NETMGR_LOG_E("proxy is nullptr");
432         return NETMANAGER_ERR_GET_PROXY_FAIL;
433     }
434     return NETMANAGER_SUCCESS;
435 }
436 
GetCellularTxBytes()437 int64_t NetsysNativeClient::GetCellularTxBytes()
438 {
439     NETMGR_LOG_D("NetsysNativeClient GetCellularTxBytes");
440     auto proxy = GetProxy();
441     if (proxy == nullptr) {
442         NETMGR_LOG_E("proxy is nullptr");
443         return NETMANAGER_ERR_GET_PROXY_FAIL;
444     }
445     return NETMANAGER_SUCCESS;
446 }
447 
GetAllRxBytes()448 int64_t NetsysNativeClient::GetAllRxBytes()
449 {
450     NETMGR_LOG_D("NetsysNativeClient GetAllRxBytes");
451     auto proxy = GetProxy();
452     if (proxy == nullptr) {
453         NETMGR_LOG_E("proxy is nullptr");
454         return NETMANAGER_ERR_GET_PROXY_FAIL;
455     }
456     return NETMANAGER_SUCCESS;
457 }
458 
GetAllTxBytes()459 int64_t NetsysNativeClient::GetAllTxBytes()
460 {
461     NETMGR_LOG_D("NetsysNativeClient GetAllTxBytes");
462     auto proxy = GetProxy();
463     if (proxy == nullptr) {
464         NETMGR_LOG_E("proxy is nullptr");
465         return NETMANAGER_ERR_GET_PROXY_FAIL;
466     }
467     return NETMANAGER_SUCCESS;
468 }
469 
GetUidRxBytes(uint32_t uid)470 int64_t NetsysNativeClient::GetUidRxBytes(uint32_t uid)
471 {
472     NETMGR_LOG_D("NetsysNativeClient GetUidRxBytes uid is [%{public}u]", uid);
473     auto proxy = GetProxy();
474     if (proxy == nullptr) {
475         NETMGR_LOG_E("proxy is nullptr");
476         return NETMANAGER_ERR_GET_PROXY_FAIL;
477     }
478     return NETMANAGER_SUCCESS;
479 }
480 
GetUidTxBytes(uint32_t uid)481 int64_t NetsysNativeClient::GetUidTxBytes(uint32_t uid)
482 {
483     NETMGR_LOG_D("NetsysNativeClient GetUidTxBytes uid is [%{public}u]", uid);
484     auto proxy = GetProxy();
485     if (proxy == nullptr) {
486         NETMGR_LOG_E("proxy is nullptr");
487         return NETMANAGER_ERR_GET_PROXY_FAIL;
488     }
489     return NETMANAGER_SUCCESS;
490 }
491 
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)492 int64_t NetsysNativeClient::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
493 {
494     NETMGR_LOG_D("NetsysNativeClient GetUidOnIfaceRxBytes uid is [%{public}u] iface name is [%{public}s]", uid,
495                  interfaceName.c_str());
496     auto proxy = GetProxy();
497     if (proxy == nullptr) {
498         NETMGR_LOG_E("proxy is nullptr");
499         return NETMANAGER_ERR_GET_PROXY_FAIL;
500     }
501     return NETMANAGER_SUCCESS;
502 }
503 
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)504 int64_t NetsysNativeClient::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
505 {
506     NETMGR_LOG_D("NetsysNativeClient GetUidOnIfaceTxBytes uid is [%{public}u] iface name is [%{public}s]", uid,
507                  interfaceName.c_str());
508     auto proxy = GetProxy();
509     if (proxy == nullptr) {
510         NETMGR_LOG_E("proxy is nullptr");
511         return NETMANAGER_ERR_GET_PROXY_FAIL;
512     }
513     return NETMANAGER_SUCCESS;
514 }
515 
GetIfaceRxBytes(const std::string & interfaceName)516 int64_t NetsysNativeClient::GetIfaceRxBytes(const std::string &interfaceName)
517 {
518     NETMGR_LOG_D("NetsysNativeClient GetIfaceRxBytes iface name is [%{public}s]", interfaceName.c_str());
519     auto proxy = GetProxy();
520     if (proxy == nullptr) {
521         NETMGR_LOG_E("proxy is nullptr");
522         return NETMANAGER_ERR_GET_PROXY_FAIL;
523     }
524     return NETMANAGER_SUCCESS;
525 }
526 
GetIfaceTxBytes(const std::string & interfaceName)527 int64_t NetsysNativeClient::GetIfaceTxBytes(const std::string &interfaceName)
528 {
529     NETMGR_LOG_D("NetsysNativeClient GetIfaceTxBytes iface name is [%{public}s]", interfaceName.c_str());
530     auto proxy = GetProxy();
531     if (proxy == nullptr) {
532         NETMGR_LOG_E("proxy is nullptr");
533         return NETMANAGER_ERR_GET_PROXY_FAIL;
534     }
535     return NETMANAGER_SUCCESS;
536 }
537 
InterfaceGetList()538 std::vector<std::string> NetsysNativeClient::InterfaceGetList()
539 {
540     NETMGR_LOG_D("NetsysNativeClient InterfaceGetList");
541     std::vector<std::string> ret;
542     auto proxy = GetProxy();
543     if (proxy == nullptr) {
544         NETMGR_LOG_E("proxy is nullptr");
545         return ret;
546     }
547     proxy->InterfaceGetList(ret);
548     return ret;
549 }
550 
UidGetList()551 std::vector<std::string> NetsysNativeClient::UidGetList()
552 {
553     NETMGR_LOG_D("NetsysNativeClient UidGetList");
554     auto proxy = GetProxy();
555     if (proxy == nullptr) {
556         NETMGR_LOG_E("proxy is nullptr");
557         return {};
558     }
559     return {};
560 }
561 
GetIfaceRxPackets(const std::string & interfaceName)562 int64_t NetsysNativeClient::GetIfaceRxPackets(const std::string &interfaceName)
563 {
564     NETMGR_LOG_D("NetsysNativeClient GetIfaceRxPackets iface name is [%{public}s]", interfaceName.c_str());
565     return NETMANAGER_SUCCESS;
566 }
567 
GetIfaceTxPackets(const std::string & interfaceName)568 int64_t NetsysNativeClient::GetIfaceTxPackets(const std::string &interfaceName)
569 {
570     NETMGR_LOG_D("NetsysNativeClient GetIfaceTxPackets iface name is [%{public}s]", interfaceName.c_str());
571     return NETMANAGER_SUCCESS;
572 }
573 
SetDefaultNetWork(int32_t netId)574 int32_t NetsysNativeClient::SetDefaultNetWork(int32_t netId)
575 {
576     NETMGR_LOG_D("NetsysNativeClient SetDefaultNetWork");
577     auto proxy = GetProxy();
578     if (proxy == nullptr) {
579         NETMGR_LOG_E("proxy is nullptr");
580         return NETMANAGER_ERR_GET_PROXY_FAIL;
581     }
582     return proxy->NetworkSetDefault(netId);
583 }
584 
ClearDefaultNetWorkNetId()585 int32_t NetsysNativeClient::ClearDefaultNetWorkNetId()
586 {
587     NETMGR_LOG_D("NetsysNativeClient ClearDefaultNetWorkNetId");
588     auto proxy = GetProxy();
589     if (proxy == nullptr) {
590         NETMGR_LOG_E("proxy is nullptr");
591         return NETMANAGER_ERR_GET_PROXY_FAIL;
592     }
593     return NETMANAGER_SUCCESS;
594 }
595 
BindSocket(int32_t socket_fd,uint32_t netId)596 int32_t NetsysNativeClient::BindSocket(int32_t socket_fd, uint32_t netId)
597 {
598     NETMGR_LOG_D("NetsysNativeClient::BindSocket: netId = [%{public}u]", netId);
599     auto proxy = GetProxy();
600     if (proxy == nullptr) {
601         NETMGR_LOG_E("proxy is nullptr");
602         return NETMANAGER_ERR_GET_PROXY_FAIL;
603     }
604     return NETMANAGER_SUCCESS;
605 }
606 
IpEnableForwarding(const std::string & requestor)607 int32_t NetsysNativeClient::IpEnableForwarding(const std::string &requestor)
608 {
609     NETMGR_LOG_D("NetsysNativeClient IpEnableForwarding: requestor[%{public}s]", requestor.c_str());
610     auto proxy = GetProxy();
611     if (proxy == nullptr) {
612         NETMGR_LOG_E("proxy is nullptr");
613         return NETMANAGER_ERR_GET_PROXY_FAIL;
614     }
615     return proxy->IpEnableForwarding(requestor);
616 }
617 
IpDisableForwarding(const std::string & requestor)618 int32_t NetsysNativeClient::IpDisableForwarding(const std::string &requestor)
619 {
620     NETMGR_LOG_D("NetsysNativeClient IpDisableForwarding: requestor[%{public}s]", requestor.c_str());
621     auto proxy = GetProxy();
622     if (proxy == nullptr) {
623         NETMGR_LOG_E("proxy is nullptr");
624         return NETMANAGER_ERR_GET_PROXY_FAIL;
625     }
626     return proxy->IpDisableForwarding(requestor);
627 }
628 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)629 int32_t NetsysNativeClient::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
630 {
631     NETMGR_LOG_D("NetsysNativeClient EnableNat: intIface[%{public}s] intIface[%{public}s]", downstreamIface.c_str(),
632                  upstreamIface.c_str());
633     auto proxy = GetProxy();
634     if (proxy == nullptr) {
635         NETMGR_LOG_E("proxy is nullptr");
636         return NETMANAGER_ERR_GET_PROXY_FAIL;
637     }
638     return proxy->EnableNat(downstreamIface, upstreamIface);
639 }
640 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)641 int32_t NetsysNativeClient::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
642 {
643     NETMGR_LOG_D("NetsysNativeClient DisableNat: intIface[%{public}s] intIface[%{public}s]", downstreamIface.c_str(),
644                  upstreamIface.c_str());
645     auto proxy = GetProxy();
646     if (proxy == nullptr) {
647         NETMGR_LOG_E("proxy is nullptr");
648         return NETMANAGER_ERR_GET_PROXY_FAIL;
649     }
650     return proxy->DisableNat(downstreamIface, upstreamIface);
651 }
652 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)653 int32_t NetsysNativeClient::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
654 {
655     auto proxy = GetProxy();
656     if (proxy == nullptr) {
657         NETMGR_LOG_E("proxy is nullptr");
658         return NETMANAGER_ERR_GET_PROXY_FAIL;
659     }
660     return proxy->IpfwdAddInterfaceForward(fromIface, toIface);
661 }
662 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)663 int32_t NetsysNativeClient::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
664 {
665     NETMGR_LOG_D("NetsysNativeClient IpfwdRemoveInterfaceForward: fromIface[%{public}s], toIface[%{public}s]",
666                  fromIface.c_str(), toIface.c_str());
667     auto proxy = GetProxy();
668     if (proxy == nullptr) {
669         NETMGR_LOG_E("proxy is nullptr");
670         return NETMANAGER_ERR_GET_PROXY_FAIL;
671     }
672     return proxy->IpfwdRemoveInterfaceForward(fromIface, toIface);
673 }
674 
ShareDnsSet(uint16_t netId)675 int32_t NetsysNativeClient::ShareDnsSet(uint16_t netId)
676 {
677     auto proxy = GetProxy();
678     if (proxy == nullptr) {
679         NETMGR_LOG_E("proxy is nullptr");
680         return NETMANAGER_ERR_GET_PROXY_FAIL;
681     }
682     return proxy->ShareDnsSet(netId);
683 }
684 
StartDnsProxyListen()685 int32_t NetsysNativeClient::StartDnsProxyListen()
686 {
687     auto proxy = GetProxy();
688     if (proxy == nullptr) {
689         NETMGR_LOG_E("proxy is nullptr");
690         return NETMANAGER_ERR_GET_PROXY_FAIL;
691     }
692     return proxy->StartDnsProxyListen();
693 }
694 
StopDnsProxyListen()695 int32_t NetsysNativeClient::StopDnsProxyListen()
696 {
697     auto proxy = GetProxy();
698     if (proxy == nullptr) {
699         NETMGR_LOG_E("proxy is nullptr");
700         return NETMANAGER_ERR_GET_PROXY_FAIL;
701     }
702     return proxy->StopDnsProxyListen();
703 }
704 
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)705 int32_t NetsysNativeClient::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
706 {
707     NETMGR_LOG_D("NetsysNativeClient RegisterNetsysNotifyCallback");
708     return NETMANAGER_SUCCESS;
709 }
710 
GetProxy()711 sptr<OHOS::NetsysNative::INetsysService> NetsysNativeClient::GetProxy()
712 {
713     std::lock_guard lock(mutex_);
714     if (netsysNativeService_) {
715         return netsysNativeService_;
716     }
717 
718     NETMGR_LOG_D("Execute GetSystemAbilityManager");
719     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
720     if (samgr == nullptr) {
721         NETMGR_LOG_E("NetsysNativeClient samgr null");
722         return nullptr;
723     }
724 
725     auto remote = samgr->GetSystemAbility(OHOS::COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
726     if (remote == nullptr) {
727         NETMGR_LOG_E("Get remote service failed");
728         return nullptr;
729     }
730 
731     deathRecipient_ = new (std::nothrow) NetNativeConnDeathRecipient(*this);
732     if (deathRecipient_ == nullptr) {
733         NETMGR_LOG_E("Recipient new failed!");
734         return nullptr;
735     }
736 
737     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
738         NETMGR_LOG_E("add death recipient failed");
739         return nullptr;
740     }
741 
742     netsysNativeService_ = iface_cast<NetsysNative::INetsysService>(remote);
743     if (netsysNativeService_ == nullptr) {
744         NETMGR_LOG_E("Get remote service proxy failed");
745         return nullptr;
746     }
747 
748     nativeNotifyCallback_ = new (std::nothrow) NativeNotifyCallback(*this);
749     netsysNativeService_->RegisterNotifyCallback(nativeNotifyCallback_);
750 
751     return netsysNativeService_;
752 }
753 
OnRemoteDied(const wptr<IRemoteObject> & remote)754 void NetsysNativeClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
755 {
756     NETMGR_LOG_D("on remote died");
757     if (remote == nullptr) {
758         NETMGR_LOG_E("remote object is nullptr");
759         return;
760     }
761 
762     std::lock_guard lock(mutex_);
763     if (netsysNativeService_ == nullptr) {
764         NETMGR_LOG_E("netsysNativeService_ is nullptr");
765         return;
766     }
767 
768     sptr<IRemoteObject> local = netsysNativeService_->AsObject();
769     if (local != remote.promote()) {
770         NETMGR_LOG_E("proxy and stub is not same remote object");
771         return;
772     }
773     local->RemoveDeathRecipient(deathRecipient_);
774 
775     if (access(NETSYS_ROUTE_INIT_DIR_PATH, F_OK) == 0) {
776         NETMGR_LOG_D("NetConnService netsys restart, clear NETSYS_ROUTE_INIT_DIR_PATH");
777         rmdir(NETSYS_ROUTE_INIT_DIR_PATH);
778     }
779 
780     netsysNativeService_ = nullptr;
781 }
782 
BindNetworkServiceVpn(int32_t socketFd)783 int32_t NetsysNativeClient::BindNetworkServiceVpn(int32_t socketFd)
784 {
785     NETMGR_LOG_D("NetsysNativeClient::BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
786     /* netsys provide default interface name */
787     const char *defaultNetName = "wlan0";
788     socklen_t defaultNetNameLen = strlen(defaultNetName);
789     /* set socket by option. */
790     int32_t ret = setsockopt(socketFd, SOL_SOCKET, SO_MARK, defaultNetName, defaultNetNameLen);
791     if (ret < 0) {
792         NETMGR_LOG_E("The SO_BINDTODEVICE of setsockopt failed.");
793         return NETSYS_ERR_VPN;
794     }
795     return NETMANAGER_SUCCESS;
796 }
797 
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)798 int32_t NetsysNativeClient::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
799 {
800     NETMGR_LOG_D("NetsysNativeClient::EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
801     int32_t ifaceFdTemp = 0;
802     if ((ifaceFdTemp = open(DEV_NET_TUN_PATH, O_RDWR)) < 0) {
803         NETMGR_LOG_E("VPN tunnel device open was failed.");
804         return NETSYS_ERR_VPN;
805     }
806 
807     /*
808      * Flags:
809      * IFF_TUN   - TUN device (no Ethernet headers)
810      * IFF_TAP   - TAP device
811      * IFF_NO_PI - Do not provide packet information
812      **/
813     ifRequest.ifr_flags = IFF_TUN | IFF_NO_PI;
814     /**
815      * Try to create the device. if it cannot assign the device interface name, kernel can
816      * allocate the next device interface name. for example, there is tun0, kernel can
817      * allocate tun1.
818      **/
819     if (ioctl(ifaceFdTemp, TUNSETIFF, &ifRequest) < 0) {
820         NETMGR_LOG_E("The TUNSETIFF of ioctl failed, ifRequest.ifr_name[%{public}s]", ifRequest.ifr_name);
821         return NETSYS_ERR_VPN;
822     }
823 
824     /* Activate the device */
825     ifRequest.ifr_flags = IFF_UP;
826     if (ioctl(socketFd, SIOCSIFFLAGS, &ifRequest) < 0) {
827         NETMGR_LOG_E("The SIOCSIFFLAGS of ioctl failed.");
828         return NETSYS_ERR_VPN;
829     }
830 
831     ifaceFd = ifaceFdTemp;
832     return NETMANAGER_SUCCESS;
833 }
834 
AsInAddr(sockaddr * sa)835 static inline in_addr_t *AsInAddr(sockaddr *sa)
836 {
837     return &(reinterpret_cast<sockaddr_in *>(sa))->sin_addr.s_addr;
838 }
839 
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)840 int32_t NetsysNativeClient::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
841                                          struct ifreq &ifRequest)
842 {
843     NETMGR_LOG_D("NetsysNativeClient::SetIpAddress: socketFd[%{public}d]", socketFd);
844 
845     ifRequest.ifr_addr.sa_family = AF_INET;
846     ifRequest.ifr_netmask.sa_family = AF_INET;
847 
848     /* inet_pton is IP ipAddress translation to binary network byte order. */
849     if (inet_pton(AF_INET, ipAddress.c_str(), AsInAddr(&ifRequest.ifr_addr)) != 1) {
850         NETMGR_LOG_E("inet_pton failed.");
851         return NETSYS_ERR_VPN;
852     }
853     if (ioctl(socketFd, SIOCSIFADDR, &ifRequest) < 0) {
854         NETMGR_LOG_E("The SIOCSIFADDR of ioctl failed.");
855         return NETSYS_ERR_VPN;
856     }
857     in_addr_t addressPrefixLength = prefixLen ? (~0 << (MAX_IPV4_ADDRESS_LEN - prefixLen)) : 0;
858     *AsInAddr(&ifRequest.ifr_netmask) = htonl(addressPrefixLength);
859     if (ioctl(socketFd, SIOCSIFNETMASK, &ifRequest)) {
860         NETMGR_LOG_E("The SIOCSIFNETMASK of ioctl failed.");
861         return NETSYS_ERR_VPN;
862     }
863 
864     return NETMANAGER_SUCCESS;
865 }
866 
SetBlocking(int32_t ifaceFd,bool isBlock)867 int32_t NetsysNativeClient::SetBlocking(int32_t ifaceFd, bool isBlock)
868 {
869     NETMGR_LOG_D("NetsysNativeClient::SetBlocking");
870     int32_t blockingFlag = 0;
871     blockingFlag = fcntl(ifaceFd, F_GETFL);
872     if (blockingFlag < 0) {
873         NETMGR_LOG_E("The blockingFlag of fcntl failed.");
874         return NETSYS_ERR_VPN;
875     }
876 
877     if (!isBlock) {
878         blockingFlag = static_cast<int>(static_cast<uint32_t>(blockingFlag) | static_cast<uint32_t>(O_NONBLOCK));
879     } else {
880         blockingFlag = static_cast<int>(static_cast<uint32_t>(blockingFlag) | static_cast<uint32_t>(~O_NONBLOCK));
881     }
882 
883     if (fcntl(ifaceFd, F_SETFL, blockingFlag) < 0) {
884         NETMGR_LOG_E("The F_SETFL of fcntl failed.");
885         return NETSYS_ERR_VPN;
886     }
887     return NETMANAGER_SUCCESS;
888 }
889 
StartDhcpClient(const std::string & iface,bool bIpv6)890 int32_t NetsysNativeClient::StartDhcpClient(const std::string &iface, bool bIpv6)
891 {
892     NETMGR_LOG_D("NetsysNativeClient::StartDhcpClient");
893     auto proxy = GetProxy();
894     if (proxy == nullptr) {
895         NETMGR_LOG_E("proxy is nullptr");
896         return NETMANAGER_ERR_GET_PROXY_FAIL;
897     }
898     return proxy->StartDhcpClient(iface, bIpv6);
899 }
900 
StopDhcpClient(const std::string & iface,bool bIpv6)901 int32_t NetsysNativeClient::StopDhcpClient(const std::string &iface, bool bIpv6)
902 {
903     NETMGR_LOG_D("NetsysNativeClient::StopDhcpClient");
904     auto proxy = GetProxy();
905     if (proxy == nullptr) {
906         NETMGR_LOG_E("proxy is nullptr");
907         return NETMANAGER_ERR_GET_PROXY_FAIL;
908     }
909     return proxy->StopDhcpClient(iface, bIpv6);
910 }
911 
RegisterCallback(sptr<NetsysControllerCallback> callback)912 int32_t NetsysNativeClient::RegisterCallback(sptr<NetsysControllerCallback> callback)
913 {
914     NETMGR_LOG_D("NetsysNativeClient::RegisterCallback");
915     if (callback == nullptr) {
916         NETMGR_LOG_E("Callback is nullptr");
917         return NETMANAGER_ERR_LOCAL_PTR_NULL;
918     }
919     auto proxy = GetProxy();
920     if (proxy == nullptr) {
921         NETMGR_LOG_E("proxy is nullptr");
922         return IPC_PROXY_ERR;
923     }
924     std::lock_guard lock(cbObjMutex_);
925     cbObjects_.push_back(callback);
926     return NETMANAGER_SUCCESS;
927 }
928 
ProcessDhcpResult(sptr<OHOS::NetsysNative::DhcpResultParcel> & dhcpResult)929 void NetsysNativeClient::ProcessDhcpResult(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult)
930 {
931     NETMGR_LOG_I("NetsysNativeClient::ProcessDhcpResult");
932     std::lock_guard lock(cbObjMutex_);
933     NetsysControllerCallback::DhcpResult result;
934     for (std::vector<sptr<NetsysControllerCallback>>::iterator it = cbObjects_.begin(); it != cbObjects_.end(); ++it) {
935         result.iface_ = dhcpResult->iface_;
936         result.ipAddr_ = dhcpResult->ipAddr_;
937         result.gateWay_ = dhcpResult->gateWay_;
938         result.subNet_ = dhcpResult->subNet_;
939         result.route1_ = dhcpResult->route1_;
940         result.route2_ = dhcpResult->route2_;
941         result.dns1_ = dhcpResult->dns1_;
942         result.dns2_ = dhcpResult->dns2_;
943         (*it)->OnDhcpSuccess(result);
944     }
945 }
946 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)947 int32_t NetsysNativeClient::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
948 {
949     NETMGR_LOG_D("NetsysNativeClient StartDhcpService");
950     auto proxy = GetProxy();
951     if (proxy == nullptr) {
952         NETMGR_LOG_E("proxy is nullptr");
953         return NETMANAGER_ERR_GET_PROXY_FAIL;
954     }
955     return proxy->StartDhcpService(iface, ipv4addr);
956 }
957 
StopDhcpService(const std::string & iface)958 int32_t NetsysNativeClient::StopDhcpService(const std::string &iface)
959 {
960     NETMGR_LOG_D("NetsysNativeClient StopDhcpService");
961     auto proxy = GetProxy();
962     if (proxy == nullptr) {
963         NETMGR_LOG_E("proxy is nullptr");
964         return NETMANAGER_ERR_GET_PROXY_FAIL;
965     }
966     return proxy->StopDhcpService(iface);
967 }
968 
ProcessBandwidthReachedLimit(const std::string & limitName,const std::string & iface)969 void NetsysNativeClient::ProcessBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
970 {
971     NETMGR_LOG_D("NetsysNativeClient ProcessBandwidthReachedLimit, limitName=%{public}s, iface=%{public}s",
972                  limitName.c_str(), iface.c_str());
973     std::lock_guard lock(cbObjMutex_);
974     std::for_each(cbObjects_.begin(), cbObjects_.end(),
975                   [limitName, iface](const sptr<NetsysControllerCallback> &callback) {
976                       callback->OnBandwidthReachedLimit(limitName, iface);
977                   });
978 }
979 
BandwidthEnableDataSaver(bool enable)980 int32_t NetsysNativeClient::BandwidthEnableDataSaver(bool enable)
981 {
982     auto proxy = GetProxy();
983     if (proxy == nullptr) {
984         NETMGR_LOG_E("proxy is nullptr");
985         return NETMANAGER_ERR_GET_PROXY_FAIL;
986     }
987     return proxy->BandwidthEnableDataSaver(enable);
988 }
989 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)990 int32_t NetsysNativeClient::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
991 {
992     auto proxy = GetProxy();
993     if (proxy == nullptr) {
994         NETMGR_LOG_E("proxy is nullptr");
995         return NETMANAGER_ERR_GET_PROXY_FAIL;
996     }
997     return proxy->BandwidthSetIfaceQuota(ifName, bytes);
998 }
999 
BandwidthRemoveIfaceQuota(const std::string & ifName)1000 int32_t NetsysNativeClient::BandwidthRemoveIfaceQuota(const std::string &ifName)
1001 {
1002     auto proxy = GetProxy();
1003     if (proxy == nullptr) {
1004         NETMGR_LOG_E("proxy is nullptr");
1005         return NETMANAGER_ERR_GET_PROXY_FAIL;
1006     }
1007     return proxy->BandwidthRemoveIfaceQuota(ifName);
1008 }
1009 
BandwidthAddDeniedList(uint32_t uid)1010 int32_t NetsysNativeClient::BandwidthAddDeniedList(uint32_t uid)
1011 {
1012     auto proxy = GetProxy();
1013     if (proxy == nullptr) {
1014         NETMGR_LOG_E("proxy is nullptr");
1015         return NETMANAGER_ERR_GET_PROXY_FAIL;
1016     }
1017     return proxy->BandwidthAddDeniedList(uid);
1018 }
1019 
BandwidthRemoveDeniedList(uint32_t uid)1020 int32_t NetsysNativeClient::BandwidthRemoveDeniedList(uint32_t uid)
1021 {
1022     auto proxy = GetProxy();
1023     if (proxy == nullptr) {
1024         NETMGR_LOG_E("proxy is nullptr");
1025         return NETMANAGER_ERR_GET_PROXY_FAIL;
1026     }
1027     return proxy->BandwidthRemoveDeniedList(uid);
1028 }
1029 
BandwidthAddAllowedList(uint32_t uid)1030 int32_t NetsysNativeClient::BandwidthAddAllowedList(uint32_t uid)
1031 {
1032     auto proxy = GetProxy();
1033     if (proxy == nullptr) {
1034         NETMGR_LOG_E("proxy is nullptr");
1035         return NETMANAGER_ERR_GET_PROXY_FAIL;
1036     }
1037     return proxy->BandwidthAddAllowedList(uid);
1038 }
1039 
BandwidthRemoveAllowedList(uint32_t uid)1040 int32_t NetsysNativeClient::BandwidthRemoveAllowedList(uint32_t uid)
1041 {
1042     auto proxy = GetProxy();
1043     if (proxy == nullptr) {
1044         NETMGR_LOG_E("proxy is nullptr");
1045         return NETMANAGER_ERR_GET_PROXY_FAIL;
1046     }
1047     return proxy->BandwidthRemoveAllowedList(uid);
1048 }
1049 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1050 int32_t NetsysNativeClient::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1051 {
1052     auto proxy = GetProxy();
1053     if (proxy == nullptr) {
1054         NETMGR_LOG_E("proxy is nullptr");
1055         return NETMANAGER_ERR_GET_PROXY_FAIL;
1056     }
1057     return proxy->FirewallSetUidsAllowedListChain(chain, uids);
1058 }
1059 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1060 int32_t NetsysNativeClient::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1061 {
1062     auto proxy = GetProxy();
1063     if (proxy == nullptr) {
1064         NETMGR_LOG_E("proxy is nullptr");
1065         return NETMANAGER_ERR_GET_PROXY_FAIL;
1066     }
1067     return proxy->FirewallSetUidsDeniedListChain(chain, uids);
1068 }
1069 
FirewallEnableChain(uint32_t chain,bool enable)1070 int32_t NetsysNativeClient::FirewallEnableChain(uint32_t chain, bool enable)
1071 {
1072     auto proxy = GetProxy();
1073     if (proxy == nullptr) {
1074         NETMGR_LOG_E("proxy is nullptr");
1075         return NETMANAGER_ERR_GET_PROXY_FAIL;
1076     }
1077     return proxy->FirewallEnableChain(chain, enable);
1078 }
1079 
FirewallSetUidRule(uint32_t chain,uint32_t uid,uint32_t firewallRule)1080 int32_t NetsysNativeClient::FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule)
1081 {
1082     auto proxy = GetProxy();
1083     if (proxy == nullptr) {
1084         NETMGR_LOG_E("proxy is nullptr");
1085         return NETMANAGER_ERR_GET_PROXY_FAIL;
1086     }
1087     return proxy->FirewallSetUidRule(chain, uid, firewallRule);
1088 }
1089 } // namespace NetManagerStandard
1090 } // namespace OHOS
1091