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