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], ipAddr[%{public}s], prefixLength[%{public}d]",
446         ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
447     auto proxy = GetProxy();
448     if (proxy == nullptr) {
449         NETMGR_LOG_E("proxy is nullptr");
450         return NETMANAGER_ERR_GET_PROXY_FAIL;
451     }
452     return proxy->AddInterfaceAddress(ifName, ipAddr, prefixLength);
453 }
454 
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)455 int32_t NetsysNativeClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
456                                                 int32_t prefixLength)
457 {
458     NETMGR_LOG_D("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
459         ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
460     auto proxy = GetProxy();
461     if (proxy == nullptr) {
462         NETMGR_LOG_E("proxy is nullptr");
463         return NETMANAGER_ERR_GET_PROXY_FAIL;
464     }
465     return proxy->DelInterfaceAddress(ifName, ipAddr, prefixLength);
466 }
467 
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength,const std::string & netCapabilities)468 int32_t NetsysNativeClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
469                                                 int32_t prefixLength, const std::string &netCapabilities)
470 {
471     NETMGR_LOG_D("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
472         ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
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 proxy->DelInterfaceAddress(ifName, ipAddr, prefixLength, netCapabilities);
479 }
480 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)481 int32_t NetsysNativeClient::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
482 {
483     NETMGR_LOG_D("Set Ip Address: ifaceName[%{public}s], ipAddr[%{public}s]",
484         ifaceName.c_str(), ToAnonymousIp(ipAddress).c_str());
485     auto proxy = GetProxy();
486     if (proxy == nullptr) {
487         NETMGR_LOG_E("proxy is nullptr");
488         return IPC_PROXY_ERR;
489     }
490     return proxy->InterfaceSetIpAddress(ifaceName, ipAddress);
491 }
492 
InterfaceSetIffUp(const std::string & ifaceName)493 int32_t NetsysNativeClient::InterfaceSetIffUp(const std::string &ifaceName)
494 {
495     NETMGR_LOG_D("Set Iff Up: ifaceName[%{public}s]", ifaceName.c_str());
496     auto proxy = GetProxy();
497     if (proxy == nullptr) {
498         NETMGR_LOG_E("proxy is nullptr");
499         return IPC_PROXY_ERR;
500     }
501     return proxy->InterfaceSetIffUp(ifaceName);
502 }
503 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)504 int32_t NetsysNativeClient::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
505                                               const std::vector<std::string> &servers,
506                                               const std::vector<std::string> &domains)
507 {
508     NETMGR_LOG_D("Set resolver config: netId[%{public}d]", netId);
509     auto proxy = GetProxy();
510     if (proxy == nullptr) {
511         NETMGR_LOG_E("proxy is nullptr");
512         return NETMANAGER_ERR_GET_PROXY_FAIL;
513     }
514     return proxy->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
515 }
516 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)517 int32_t NetsysNativeClient::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
518                                               std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
519                                               uint8_t &retryCount)
520 {
521     NETMGR_LOG_D("Get resolver config: netId[%{public}d]", netId);
522     auto proxy = GetProxy();
523     if (proxy == nullptr) {
524         NETMGR_LOG_E("proxy is nullptr");
525         return NETMANAGER_ERR_GET_PROXY_FAIL;
526     }
527     return proxy->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
528 }
529 
CreateNetworkCache(uint16_t netId)530 int32_t NetsysNativeClient::CreateNetworkCache(uint16_t netId)
531 {
532     NETMGR_LOG_D("create dns cache: netId[%{public}d]", netId);
533     auto proxy = GetProxy();
534     if (proxy == nullptr) {
535         NETMGR_LOG_E("proxy is nullptr");
536         return NETMANAGER_ERR_GET_PROXY_FAIL;
537     }
538     return proxy->CreateNetworkCache(netId);
539 }
540 
DestroyNetworkCache(uint16_t netId)541 int32_t NetsysNativeClient::DestroyNetworkCache(uint16_t netId)
542 {
543     NETMGR_LOG_D("Destroy dns cache: netId[%{public}d]", netId);
544     auto proxy = GetProxy();
545     if (proxy == nullptr) {
546         NETMGR_LOG_E("proxy is nullptr");
547         return NETMANAGER_ERR_GET_PROXY_FAIL;
548     }
549     return proxy->DestroyNetworkCache(netId);
550 }
551 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)552 int32_t NetsysNativeClient::GetAddrInfo(const std::string &hostName, const std::string &serverName,
553                                         const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
554 {
555     auto proxy = GetProxy();
556     if (proxy == nullptr) {
557         NETMGR_LOG_E("GetAddrInfo netsysNativeService_ is null");
558         return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
559     }
560     return proxy->GetAddrInfo(hostName, serverName, hints, netId, res);
561 }
562 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,nmd::NetworkSharingTraffic & traffic)563 int32_t NetsysNativeClient::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
564                                                      nmd::NetworkSharingTraffic &traffic)
565 {
566     NETMGR_LOG_D("NetsysNativeClient GetNetworkSharingTraffic");
567     auto proxy = GetProxy();
568     if (proxy == nullptr) {
569         NETMGR_LOG_E("proxy is nullptr");
570         return NETMANAGER_ERR_GET_PROXY_FAIL;
571     }
572     return proxy->GetNetworkSharingTraffic(downIface, upIface, traffic);
573 }
574 
GetCellularRxBytes()575 int64_t NetsysNativeClient::GetCellularRxBytes()
576 {
577     NETMGR_LOG_D("NetsysNativeClient GetCellularRxBytes");
578     auto proxy = GetProxy();
579     if (proxy == nullptr) {
580         NETMGR_LOG_E("proxy is nullptr");
581         return NETMANAGER_ERR_GET_PROXY_FAIL;
582     }
583     return NETMANAGER_SUCCESS;
584 }
585 
GetCellularTxBytes()586 int64_t NetsysNativeClient::GetCellularTxBytes()
587 {
588     NETMGR_LOG_D("NetsysNativeClient GetCellularTxBytes");
589     auto proxy = GetProxy();
590     if (proxy == nullptr) {
591         NETMGR_LOG_E("proxy is nullptr");
592         return NETMANAGER_ERR_GET_PROXY_FAIL;
593     }
594     return NETMANAGER_SUCCESS;
595 }
596 
GetAllRxBytes()597 int64_t NetsysNativeClient::GetAllRxBytes()
598 {
599     NETMGR_LOG_D("NetsysNativeClient GetAllRxBytes");
600     auto proxy = GetProxy();
601     if (proxy == nullptr) {
602         NETMGR_LOG_E("proxy is nullptr");
603         return NETMANAGER_ERR_GET_PROXY_FAIL;
604     }
605     return NETMANAGER_SUCCESS;
606 }
607 
GetAllTxBytes()608 int64_t NetsysNativeClient::GetAllTxBytes()
609 {
610     NETMGR_LOG_D("NetsysNativeClient GetAllTxBytes");
611     auto proxy = GetProxy();
612     if (proxy == nullptr) {
613         NETMGR_LOG_E("proxy is nullptr");
614         return NETMANAGER_ERR_GET_PROXY_FAIL;
615     }
616     return NETMANAGER_SUCCESS;
617 }
618 
GetUidRxBytes(uint32_t uid)619 int64_t NetsysNativeClient::GetUidRxBytes(uint32_t uid)
620 {
621     NETMGR_LOG_D("NetsysNativeClient GetUidRxBytes uid is [%{public}u]", uid);
622     auto proxy = GetProxy();
623     if (proxy == nullptr) {
624         NETMGR_LOG_E("proxy is nullptr");
625         return NETMANAGER_ERR_GET_PROXY_FAIL;
626     }
627     return NETMANAGER_SUCCESS;
628 }
629 
GetUidTxBytes(uint32_t uid)630 int64_t NetsysNativeClient::GetUidTxBytes(uint32_t uid)
631 {
632     NETMGR_LOG_D("NetsysNativeClient GetUidTxBytes uid is [%{public}u]", uid);
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 NETMANAGER_SUCCESS;
639 }
640 
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)641 int64_t NetsysNativeClient::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
642 {
643     NETMGR_LOG_D("NetsysNativeClient GetUidOnIfaceRxBytes uid is [%{public}u] iface name is [%{public}s]", uid,
644                  interfaceName.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 NETMANAGER_SUCCESS;
651 }
652 
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)653 int64_t NetsysNativeClient::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
654 {
655     NETMGR_LOG_D("NetsysNativeClient GetUidOnIfaceTxBytes uid is [%{public}u] iface name is [%{public}s]", uid,
656                  interfaceName.c_str());
657     auto proxy = GetProxy();
658     if (proxy == nullptr) {
659         NETMGR_LOG_E("proxy is nullptr");
660         return NETMANAGER_ERR_GET_PROXY_FAIL;
661     }
662     return NETMANAGER_SUCCESS;
663 }
664 
GetIfaceRxBytes(const std::string & interfaceName)665 int64_t NetsysNativeClient::GetIfaceRxBytes(const std::string &interfaceName)
666 {
667     NETMGR_LOG_D("NetsysNativeClient GetIfaceRxBytes iface name is [%{public}s]", interfaceName.c_str());
668     auto proxy = GetProxy();
669     if (proxy == nullptr) {
670         NETMGR_LOG_E("proxy is nullptr");
671         return NETMANAGER_ERR_GET_PROXY_FAIL;
672     }
673     return NETMANAGER_SUCCESS;
674 }
675 
GetIfaceTxBytes(const std::string & interfaceName)676 int64_t NetsysNativeClient::GetIfaceTxBytes(const std::string &interfaceName)
677 {
678     NETMGR_LOG_D("NetsysNativeClient GetIfaceTxBytes iface name is [%{public}s]", interfaceName.c_str());
679     auto proxy = GetProxy();
680     if (proxy == nullptr) {
681         NETMGR_LOG_E("proxy is nullptr");
682         return NETMANAGER_ERR_GET_PROXY_FAIL;
683     }
684     return NETMANAGER_SUCCESS;
685 }
686 
InterfaceGetList()687 std::vector<std::string> NetsysNativeClient::InterfaceGetList()
688 {
689     NETMGR_LOG_D("NetsysNativeClient InterfaceGetList");
690     std::vector<std::string> ret;
691     auto proxy = GetProxy();
692     if (proxy == nullptr) {
693         NETMGR_LOG_E("proxy is nullptr");
694         return ret;
695     }
696     proxy->InterfaceGetList(ret);
697     return ret;
698 }
699 
UidGetList()700 std::vector<std::string> NetsysNativeClient::UidGetList()
701 {
702     NETMGR_LOG_D("NetsysNativeClient UidGetList");
703     auto proxy = GetProxy();
704     if (proxy == nullptr) {
705         NETMGR_LOG_E("proxy is nullptr");
706         return {};
707     }
708     return {};
709 }
710 
GetIfaceRxPackets(const std::string & interfaceName)711 int64_t NetsysNativeClient::GetIfaceRxPackets(const std::string &interfaceName)
712 {
713     NETMGR_LOG_D("NetsysNativeClient GetIfaceRxPackets iface name is [%{public}s]", interfaceName.c_str());
714     return NETMANAGER_SUCCESS;
715 }
716 
GetIfaceTxPackets(const std::string & interfaceName)717 int64_t NetsysNativeClient::GetIfaceTxPackets(const std::string &interfaceName)
718 {
719     NETMGR_LOG_D("NetsysNativeClient GetIfaceTxPackets iface name is [%{public}s]", interfaceName.c_str());
720     return NETMANAGER_SUCCESS;
721 }
722 
SetDefaultNetWork(int32_t netId)723 int32_t NetsysNativeClient::SetDefaultNetWork(int32_t netId)
724 {
725     NETMGR_LOG_D("NetsysNativeClient SetDefaultNetWork");
726     auto proxy = GetProxy();
727     if (proxy == nullptr) {
728         NETMGR_LOG_E("proxy is nullptr");
729         return NETMANAGER_ERR_GET_PROXY_FAIL;
730     }
731     return proxy->NetworkSetDefault(netId);
732 }
733 
ClearDefaultNetWorkNetId()734 int32_t NetsysNativeClient::ClearDefaultNetWorkNetId()
735 {
736     NETMGR_LOG_D("NetsysNativeClient ClearDefaultNetWorkNetId");
737     auto proxy = GetProxy();
738     if (proxy == nullptr) {
739         NETMGR_LOG_E("proxy is nullptr");
740         return NETMANAGER_ERR_GET_PROXY_FAIL;
741     }
742     return NETMANAGER_SUCCESS;
743 }
744 
BindSocket(int32_t socketFd,uint32_t netId)745 int32_t NetsysNativeClient::BindSocket(int32_t socketFd, uint32_t netId)
746 {
747     NETMGR_LOG_D("NetsysNativeClient::BindSocket: netId = [%{public}u]", netId);
748     auto proxy = GetProxy();
749     if (proxy == nullptr) {
750         NETMGR_LOG_E("proxy is nullptr");
751         return NETMANAGER_ERR_GET_PROXY_FAIL;
752     }
753     return NETMANAGER_SUCCESS;
754 }
755 
IpEnableForwarding(const std::string & requestor)756 int32_t NetsysNativeClient::IpEnableForwarding(const std::string &requestor)
757 {
758     NETMGR_LOG_D("NetsysNativeClient IpEnableForwarding: requestor[%{public}s]", requestor.c_str());
759     auto proxy = GetProxy();
760     if (proxy == nullptr) {
761         NETMGR_LOG_E("proxy is nullptr");
762         return NETMANAGER_ERR_GET_PROXY_FAIL;
763     }
764     return proxy->IpEnableForwarding(requestor);
765 }
766 
IpDisableForwarding(const std::string & requestor)767 int32_t NetsysNativeClient::IpDisableForwarding(const std::string &requestor)
768 {
769     NETMGR_LOG_D("NetsysNativeClient IpDisableForwarding: requestor[%{public}s]", requestor.c_str());
770     auto proxy = GetProxy();
771     if (proxy == nullptr) {
772         NETMGR_LOG_E("proxy is nullptr");
773         return NETMANAGER_ERR_GET_PROXY_FAIL;
774     }
775     return proxy->IpDisableForwarding(requestor);
776 }
777 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)778 int32_t NetsysNativeClient::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
779 {
780     NETMGR_LOG_D("NetsysNativeClient EnableNat: intIface[%{public}s] intIface[%{public}s]", downstreamIface.c_str(),
781                  upstreamIface.c_str());
782     auto proxy = GetProxy();
783     if (proxy == nullptr) {
784         NETMGR_LOG_E("proxy is nullptr");
785         return NETMANAGER_ERR_GET_PROXY_FAIL;
786     }
787     return proxy->EnableNat(downstreamIface, upstreamIface);
788 }
789 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)790 int32_t NetsysNativeClient::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
791 {
792     NETMGR_LOG_D("NetsysNativeClient DisableNat: intIface[%{public}s] intIface[%{public}s]", downstreamIface.c_str(),
793                  upstreamIface.c_str());
794     auto proxy = GetProxy();
795     if (proxy == nullptr) {
796         NETMGR_LOG_E("proxy is nullptr");
797         return NETMANAGER_ERR_GET_PROXY_FAIL;
798     }
799     return proxy->DisableNat(downstreamIface, upstreamIface);
800 }
801 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)802 int32_t NetsysNativeClient::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
803 {
804     auto proxy = GetProxy();
805     if (proxy == nullptr) {
806         NETMGR_LOG_E("proxy is nullptr");
807         return NETMANAGER_ERR_GET_PROXY_FAIL;
808     }
809     return proxy->IpfwdAddInterfaceForward(fromIface, toIface);
810 }
811 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)812 int32_t NetsysNativeClient::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
813 {
814     NETMGR_LOG_D("NetsysNativeClient IpfwdRemoveInterfaceForward: fromIface[%{public}s], toIface[%{public}s]",
815                  fromIface.c_str(), toIface.c_str());
816     auto proxy = GetProxy();
817     if (proxy == nullptr) {
818         NETMGR_LOG_E("proxy is nullptr");
819         return NETMANAGER_ERR_GET_PROXY_FAIL;
820     }
821     return proxy->IpfwdRemoveInterfaceForward(fromIface, toIface);
822 }
823 
ShareDnsSet(uint16_t netId)824 int32_t NetsysNativeClient::ShareDnsSet(uint16_t netId)
825 {
826     auto proxy = GetProxy();
827     if (proxy == nullptr) {
828         NETMGR_LOG_E("proxy is nullptr");
829         return NETMANAGER_ERR_GET_PROXY_FAIL;
830     }
831     return proxy->ShareDnsSet(netId);
832 }
833 
StartDnsProxyListen()834 int32_t NetsysNativeClient::StartDnsProxyListen()
835 {
836     auto proxy = GetProxy();
837     if (proxy == nullptr) {
838         NETMGR_LOG_E("proxy is nullptr");
839         return NETMANAGER_ERR_GET_PROXY_FAIL;
840     }
841     return proxy->StartDnsProxyListen();
842 }
843 
StopDnsProxyListen()844 int32_t NetsysNativeClient::StopDnsProxyListen()
845 {
846     auto proxy = GetProxy();
847     if (proxy == nullptr) {
848         NETMGR_LOG_E("proxy is nullptr");
849         return NETMANAGER_ERR_GET_PROXY_FAIL;
850     }
851     return proxy->StopDnsProxyListen();
852 }
853 
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)854 int32_t NetsysNativeClient::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
855 {
856     (void)callback;
857     NETMGR_LOG_D("NetsysNativeClient RegisterNetsysNotifyCallback");
858     return NETMANAGER_SUCCESS;
859 }
860 
GetProxy()861 sptr<OHOS::NetsysNative::INetsysService> NetsysNativeClient::GetProxy()
862 {
863     std::lock_guard lock(mutex_);
864     if (netsysNativeService_) {
865         return netsysNativeService_;
866     }
867 
868     NETMGR_LOG_D("Execute GetSystemAbilityManager");
869     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
870     if (samgr == nullptr) {
871         NETMGR_LOG_E("NetsysNativeClient samgr null");
872         return nullptr;
873     }
874 
875     auto remote = samgr->GetSystemAbility(OHOS::COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
876     if (remote == nullptr) {
877         NETMGR_LOG_E("Get remote service failed");
878         return nullptr;
879     }
880 
881     deathRecipient_ = new (std::nothrow) NetNativeConnDeathRecipient(*this);
882     if (deathRecipient_ == nullptr) {
883         NETMGR_LOG_E("Recipient new failed!");
884         return nullptr;
885     }
886 
887     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
888         NETMGR_LOG_E("add death recipient failed");
889         return nullptr;
890     }
891 
892     netsysNativeService_ = iface_cast<NetsysNative::INetsysService>(remote);
893     if (netsysNativeService_ == nullptr) {
894         NETMGR_LOG_E("Get remote service proxy failed");
895         return nullptr;
896     }
897 
898     return netsysNativeService_;
899 }
900 
RegisterNotifyCallback()901 void NetsysNativeClient::RegisterNotifyCallback()
902 {
903     std::thread t([this]() {
904         uint32_t count = 0;
905         while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
906             std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_S));
907             count++;
908         }
909         auto proxy = GetProxy();
910         NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
911         if (proxy != nullptr) {
912             if (nativeNotifyCallback_ == nullptr) {
913                 nativeNotifyCallback_ = new (std::nothrow) NativeNotifyCallback(*this);
914             }
915 
916             NETMGR_LOG_D("call proxy->RegisterNotifyCallback");
917             proxy->RegisterNotifyCallback(nativeNotifyCallback_);
918 
919             if (nativeDnsReportCallback_ == nullptr) {
920                 nativeDnsReportCallback_ = new (std::nothrow) NativeNetDnsResultCallback(*this);
921             }
922 
923             NETMGR_LOG_D("call proxy->RegisterDnsResultCallback");
924             proxy->RegisterDnsResultCallback(nativeDnsReportCallback_, dnsReportTimeStep);
925         }
926     });
927     std::string threadName = "netsysGetProxy";
928     pthread_setname_np(t.native_handle(), threadName.c_str());
929     t.detach();
930 }
931 
OnRemoteDied(const wptr<IRemoteObject> & remote)932 void NetsysNativeClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
933 {
934     NETMGR_LOG_D("on remote died");
935     if (remote == nullptr) {
936         NETMGR_LOG_E("remote object is nullptr");
937         return;
938     }
939 
940     std::lock_guard lock(mutex_);
941     if (netsysNativeService_ == nullptr) {
942         NETMGR_LOG_E("netsysNativeService_ is nullptr");
943         return;
944     }
945 
946     sptr<IRemoteObject> local = netsysNativeService_->AsObject();
947     if (local != remote.promote()) {
948         NETMGR_LOG_E("proxy and stub is not same remote object");
949         return;
950     }
951     local->RemoveDeathRecipient(deathRecipient_);
952 
953     if (access(NETSYS_ROUTE_INIT_DIR_PATH, F_OK) == 0) {
954         NETMGR_LOG_D("NetConnService netsys restart, clear NETSYS_ROUTE_INIT_DIR_PATH");
955         rmdir(NETSYS_ROUTE_INIT_DIR_PATH);
956     }
957 
958     netsysNativeService_ = nullptr;
959 
960     RegisterNotifyCallback();
961 }
962 
BindNetworkServiceVpn(int32_t socketFd)963 int32_t NetsysNativeClient::BindNetworkServiceVpn(int32_t socketFd)
964 {
965     NETMGR_LOG_D("NetsysNativeClient::BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
966     /* netsys provide default interface name */
967     const char *defaultNetName = "wlan0";
968     socklen_t defaultNetNameLen = strlen(defaultNetName);
969     /* set socket by option. */
970     int32_t ret = setsockopt(socketFd, SOL_SOCKET, SO_MARK, defaultNetName, defaultNetNameLen);
971     if (ret < 0) {
972         NETMGR_LOG_E("The SO_BINDTODEVICE of setsockopt failed.");
973         return NETSYS_ERR_VPN;
974     }
975     return NETMANAGER_SUCCESS;
976 }
977 
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)978 int32_t NetsysNativeClient::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
979 {
980     NETMGR_LOG_D("NetsysNativeClient::EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
981     int32_t ifaceFdTemp = 0;
982     if ((ifaceFdTemp = open(DEV_NET_TUN_PATH, O_RDWR)) < 0) {
983         NETMGR_LOG_E("VPN tunnel device open was failed.");
984         return NETSYS_ERR_VPN;
985     }
986 
987     /*
988      * Flags:
989      * IFF_TUN   - TUN device (no Ethernet headers)
990      * IFF_TAP   - TAP device
991      * IFF_NO_PI - Do not provide packet information
992      **/
993     ifRequest.ifr_flags = IFF_TUN | IFF_NO_PI;
994     /**
995      * Try to create the device. if it cannot assign the device interface name, kernel can
996      * allocate the next device interface name. for example, there is tun0, kernel can
997      * allocate tun1.
998      **/
999     if (ioctl(ifaceFdTemp, TUNSETIFF, &ifRequest) < 0) {
1000         NETMGR_LOG_E("The TUNSETIFF of ioctl failed, ifRequest.ifr_name[%{public}s]", ifRequest.ifr_name);
1001         close(ifaceFdTemp);
1002         return NETSYS_ERR_VPN;
1003     }
1004 
1005     /* Activate the device */
1006     ifRequest.ifr_flags = IFF_UP;
1007     if (ioctl(socketFd, SIOCSIFFLAGS, &ifRequest) < 0) {
1008         NETMGR_LOG_E("The SIOCSIFFLAGS of ioctl failed.");
1009         close(ifaceFdTemp);
1010         return NETSYS_ERR_VPN;
1011     }
1012 
1013     ifaceFd = ifaceFdTemp;
1014     return NETMANAGER_SUCCESS;
1015 }
1016 
AsInAddr(sockaddr * sa)1017 static inline in_addr_t *AsInAddr(sockaddr *sa)
1018 {
1019     return &(reinterpret_cast<sockaddr_in *>(sa))->sin_addr.s_addr;
1020 }
1021 
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)1022 int32_t NetsysNativeClient::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
1023                                          struct ifreq &ifRequest)
1024 {
1025     NETMGR_LOG_D("NetsysNativeClient::SetIpAddress: socketFd[%{public}d]", socketFd);
1026 
1027     ifRequest.ifr_addr.sa_family = AF_INET;
1028     ifRequest.ifr_netmask.sa_family = AF_INET;
1029 
1030     /* inet_pton is IP ipAddress translation to binary network byte order. */
1031     if (inet_pton(AF_INET, ipAddress.c_str(), AsInAddr(&ifRequest.ifr_addr)) != 1) {
1032         NETMGR_LOG_E("inet_pton failed.");
1033         return NETSYS_ERR_VPN;
1034     }
1035     if (ioctl(socketFd, SIOCSIFADDR, &ifRequest) < 0) {
1036         NETMGR_LOG_E("The SIOCSIFADDR of ioctl failed.");
1037         return NETSYS_ERR_VPN;
1038     }
1039     in_addr_t addressPrefixLength = prefixLen ? (~0 << (IPV4_MAX_LENGTH - prefixLen)) : 0;
1040     *AsInAddr(&ifRequest.ifr_netmask) = htonl(addressPrefixLength);
1041     if (ioctl(socketFd, SIOCSIFNETMASK, &ifRequest)) {
1042         NETMGR_LOG_E("The SIOCSIFNETMASK of ioctl failed.");
1043         return NETSYS_ERR_VPN;
1044     }
1045 
1046     return NETMANAGER_SUCCESS;
1047 }
1048 
SetBlocking(int32_t ifaceFd,bool isBlock)1049 int32_t NetsysNativeClient::SetBlocking(int32_t ifaceFd, bool isBlock)
1050 {
1051     NETMGR_LOG_D("NetsysNativeClient::SetBlocking");
1052     int32_t blockingFlag = 0;
1053     blockingFlag = fcntl(ifaceFd, F_GETFL);
1054     if (blockingFlag < 0) {
1055         NETMGR_LOG_E("The blockingFlag of fcntl failed.");
1056         return NETSYS_ERR_VPN;
1057     }
1058 
1059     if (!isBlock) {
1060         blockingFlag = static_cast<int>(static_cast<uint32_t>(blockingFlag) | static_cast<uint32_t>(O_NONBLOCK));
1061     } else {
1062         blockingFlag = static_cast<int>(static_cast<uint32_t>(blockingFlag) | static_cast<uint32_t>(~O_NONBLOCK));
1063     }
1064 
1065     if (fcntl(ifaceFd, F_SETFL, blockingFlag) < 0) {
1066         NETMGR_LOG_E("The F_SETFL of fcntl failed.");
1067         return NETSYS_ERR_VPN;
1068     }
1069     return NETMANAGER_SUCCESS;
1070 }
1071 
StartDhcpClient(const std::string & iface,bool bIpv6)1072 int32_t NetsysNativeClient::StartDhcpClient(const std::string &iface, bool bIpv6)
1073 {
1074     NETMGR_LOG_D("NetsysNativeClient::StartDhcpClient");
1075     auto proxy = GetProxy();
1076     if (proxy == nullptr) {
1077         NETMGR_LOG_E("proxy is nullptr");
1078         return NETMANAGER_ERR_GET_PROXY_FAIL;
1079     }
1080     return proxy->StartDhcpClient(iface, bIpv6);
1081 }
1082 
StopDhcpClient(const std::string & iface,bool bIpv6)1083 int32_t NetsysNativeClient::StopDhcpClient(const std::string &iface, bool bIpv6)
1084 {
1085     NETMGR_LOG_D("NetsysNativeClient::StopDhcpClient");
1086     auto proxy = GetProxy();
1087     if (proxy == nullptr) {
1088         NETMGR_LOG_E("proxy is nullptr");
1089         return NETMANAGER_ERR_GET_PROXY_FAIL;
1090     }
1091     return proxy->StopDhcpClient(iface, bIpv6);
1092 }
1093 
RegisterCallback(const sptr<NetsysControllerCallback> & callback)1094 int32_t NetsysNativeClient::RegisterCallback(const sptr<NetsysControllerCallback> &callback)
1095 {
1096     NETMGR_LOG_D("NetsysNativeClient::RegisterCallback");
1097     if (callback == nullptr) {
1098         NETMGR_LOG_E("Callback is nullptr");
1099         return NETMANAGER_ERR_LOCAL_PTR_NULL;
1100     }
1101     std::lock_guard lock(cbObjMutex_);
1102     cbObjects_.push_back(callback);
1103     return NETMANAGER_SUCCESS;
1104 }
1105 
ProcessDhcpResult(sptr<OHOS::NetsysNative::DhcpResultParcel> & dhcpResult)1106 void NetsysNativeClient::ProcessDhcpResult(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult)
1107 {
1108     NETMGR_LOG_I("NetsysNativeClient::ProcessDhcpResult");
1109     std::lock_guard lock(cbObjMutex_);
1110     NetsysControllerCallback::DhcpResult result;
1111     for (auto cb = cbObjects_.begin(); cb != cbObjects_.end();) {
1112         if (*cb == nullptr) {
1113             cb = cbObjects_.erase(cb);
1114         } else {
1115             result.iface_ = dhcpResult->iface_;
1116             result.ipAddr_ = dhcpResult->ipAddr_;
1117             result.gateWay_ = dhcpResult->gateWay_;
1118             result.subNet_ = dhcpResult->subNet_;
1119             result.route1_ = dhcpResult->route1_;
1120             result.route2_ = dhcpResult->route2_;
1121             result.dns1_ = dhcpResult->dns1_;
1122             result.dns2_ = dhcpResult->dns2_;
1123             (*cb)->OnDhcpSuccess(result);
1124             ++cb;
1125         }
1126     }
1127 }
1128 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)1129 int32_t NetsysNativeClient::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
1130 {
1131     NETMGR_LOG_D("NetsysNativeClient StartDhcpService");
1132     auto proxy = GetProxy();
1133     if (proxy == nullptr) {
1134         NETMGR_LOG_E("proxy is nullptr");
1135         return NETMANAGER_ERR_GET_PROXY_FAIL;
1136     }
1137     return proxy->StartDhcpService(iface, ipv4addr);
1138 }
1139 
StopDhcpService(const std::string & iface)1140 int32_t NetsysNativeClient::StopDhcpService(const std::string &iface)
1141 {
1142     NETMGR_LOG_D("NetsysNativeClient StopDhcpService");
1143     auto proxy = GetProxy();
1144     if (proxy == nullptr) {
1145         NETMGR_LOG_E("proxy is nullptr");
1146         return NETMANAGER_ERR_GET_PROXY_FAIL;
1147     }
1148     return proxy->StopDhcpService(iface);
1149 }
1150 
ProcessBandwidthReachedLimit(const std::string & limitName,const std::string & iface)1151 void NetsysNativeClient::ProcessBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
1152 {
1153     NETMGR_LOG_D("NetsysNativeClient ProcessBandwidthReachedLimit, limitName=%{public}s, iface=%{public}s",
1154                  limitName.c_str(), iface.c_str());
1155     std::lock_guard lock(cbObjMutex_);
1156     for (auto cb = cbObjects_.begin(); cb != cbObjects_.end();) {
1157         if (*cb == nullptr) {
1158             cb = cbObjects_.erase(cb);
1159         } else {
1160             (*cb)->OnBandwidthReachedLimit(limitName, iface);
1161             ++cb;
1162         }
1163     }
1164 }
1165 
BandwidthEnableDataSaver(bool enable)1166 int32_t NetsysNativeClient::BandwidthEnableDataSaver(bool enable)
1167 {
1168     auto proxy = GetProxy();
1169     if (proxy == nullptr) {
1170         NETMGR_LOG_E("proxy is nullptr");
1171         return NETMANAGER_ERR_GET_PROXY_FAIL;
1172     }
1173     return proxy->BandwidthEnableDataSaver(enable);
1174 }
1175 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)1176 int32_t NetsysNativeClient::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
1177 {
1178     auto proxy = GetProxy();
1179     if (proxy == nullptr) {
1180         NETMGR_LOG_E("proxy is nullptr");
1181         return NETMANAGER_ERR_GET_PROXY_FAIL;
1182     }
1183     return proxy->BandwidthSetIfaceQuota(ifName, bytes);
1184 }
1185 
BandwidthRemoveIfaceQuota(const std::string & ifName)1186 int32_t NetsysNativeClient::BandwidthRemoveIfaceQuota(const std::string &ifName)
1187 {
1188     auto proxy = GetProxy();
1189     if (proxy == nullptr) {
1190         NETMGR_LOG_E("proxy is nullptr");
1191         return NETMANAGER_ERR_GET_PROXY_FAIL;
1192     }
1193     return proxy->BandwidthRemoveIfaceQuota(ifName);
1194 }
1195 
BandwidthAddDeniedList(uint32_t uid)1196 int32_t NetsysNativeClient::BandwidthAddDeniedList(uint32_t uid)
1197 {
1198     auto proxy = GetProxy();
1199     if (proxy == nullptr) {
1200         NETMGR_LOG_E("proxy is nullptr");
1201         return NETMANAGER_ERR_GET_PROXY_FAIL;
1202     }
1203     return proxy->BandwidthAddDeniedList(uid);
1204 }
1205 
BandwidthRemoveDeniedList(uint32_t uid)1206 int32_t NetsysNativeClient::BandwidthRemoveDeniedList(uint32_t uid)
1207 {
1208     auto proxy = GetProxy();
1209     if (proxy == nullptr) {
1210         NETMGR_LOG_E("proxy is nullptr");
1211         return NETMANAGER_ERR_GET_PROXY_FAIL;
1212     }
1213     return proxy->BandwidthRemoveDeniedList(uid);
1214 }
1215 
BandwidthAddAllowedList(uint32_t uid)1216 int32_t NetsysNativeClient::BandwidthAddAllowedList(uint32_t uid)
1217 {
1218     auto proxy = GetProxy();
1219     if (proxy == nullptr) {
1220         NETMGR_LOG_E("proxy is nullptr");
1221         return NETMANAGER_ERR_GET_PROXY_FAIL;
1222     }
1223     return proxy->BandwidthAddAllowedList(uid);
1224 }
1225 
BandwidthRemoveAllowedList(uint32_t uid)1226 int32_t NetsysNativeClient::BandwidthRemoveAllowedList(uint32_t uid)
1227 {
1228     auto proxy = GetProxy();
1229     if (proxy == nullptr) {
1230         NETMGR_LOG_E("proxy is nullptr");
1231         return NETMANAGER_ERR_GET_PROXY_FAIL;
1232     }
1233     return proxy->BandwidthRemoveAllowedList(uid);
1234 }
1235 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1236 int32_t NetsysNativeClient::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1237 {
1238     auto proxy = GetProxy();
1239     if (proxy == nullptr) {
1240         NETMGR_LOG_E("proxy is nullptr");
1241         return NETMANAGER_ERR_GET_PROXY_FAIL;
1242     }
1243     return proxy->FirewallSetUidsAllowedListChain(chain, uids);
1244 }
1245 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1246 int32_t NetsysNativeClient::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1247 {
1248     auto proxy = GetProxy();
1249     if (proxy == nullptr) {
1250         NETMGR_LOG_E("proxy is nullptr");
1251         return NETMANAGER_ERR_GET_PROXY_FAIL;
1252     }
1253     return proxy->FirewallSetUidsDeniedListChain(chain, uids);
1254 }
1255 
FirewallEnableChain(uint32_t chain,bool enable)1256 int32_t NetsysNativeClient::FirewallEnableChain(uint32_t chain, bool enable)
1257 {
1258     auto proxy = GetProxy();
1259     if (proxy == nullptr) {
1260         NETMGR_LOG_E("proxy is nullptr");
1261         return NETMANAGER_ERR_GET_PROXY_FAIL;
1262     }
1263     return proxy->FirewallEnableChain(chain, enable);
1264 }
1265 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)1266 int32_t NetsysNativeClient::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)
1267 {
1268     auto proxy = GetProxy();
1269     if (proxy == nullptr) {
1270         NETMGR_LOG_E("proxy is nullptr");
1271         return NETMANAGER_ERR_GET_PROXY_FAIL;
1272     }
1273     return proxy->FirewallSetUidRule(chain, uids, firewallRule);
1274 }
1275 
GetTotalStats(uint64_t & stats,uint32_t type)1276 int32_t NetsysNativeClient::GetTotalStats(uint64_t &stats, uint32_t type)
1277 {
1278     auto proxy = GetProxy();
1279     if (proxy == nullptr) {
1280         NETMGR_LOG_E("proxy is nullptr");
1281         return NETMANAGER_ERR_GET_PROXY_FAIL;
1282     }
1283     return proxy->GetTotalStats(stats, type);
1284 }
1285 
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)1286 int32_t NetsysNativeClient::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
1287 {
1288     auto proxy = GetProxy();
1289     if (proxy == nullptr) {
1290         NETMGR_LOG_E("proxy is nullptr");
1291         return NETMANAGER_ERR_GET_PROXY_FAIL;
1292     }
1293     return proxy->GetUidStats(stats, type, uid);
1294 }
1295 
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)1296 int32_t NetsysNativeClient::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
1297 {
1298     auto proxy = GetProxy();
1299     if (proxy == nullptr) {
1300         NETMGR_LOG_E("proxy is nullptr");
1301         return NETMANAGER_ERR_GET_PROXY_FAIL;
1302     }
1303     return proxy->GetIfaceStats(stats, type, interfaceName);
1304 }
1305 
GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)1306 int32_t NetsysNativeClient::GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1307 {
1308     auto proxy = GetProxy();
1309     if (proxy == nullptr) {
1310         NETMGR_LOG_E("proxy is nullptr");
1311         return NETMANAGER_ERR_GET_PROXY_FAIL;
1312     }
1313     return proxy->GetAllSimStatsInfo(stats);
1314 }
1315 
DeleteSimStatsInfo(uint32_t uid)1316 int32_t NetsysNativeClient::DeleteSimStatsInfo(uint32_t uid)
1317 {
1318     auto proxy = GetProxy();
1319     if (proxy == nullptr) {
1320         NETMGR_LOG_E("proxy is nullptr");
1321         return NETMANAGER_ERR_GET_PROXY_FAIL;
1322     }
1323     return proxy->DeleteSimStatsInfo(uid);
1324 }
1325 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)1326 int32_t NetsysNativeClient::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1327 {
1328     auto proxy = GetProxy();
1329     if (proxy == nullptr) {
1330         NETMGR_LOG_E("proxy is nullptr");
1331         return NETMANAGER_ERR_GET_PROXY_FAIL;
1332     }
1333     return proxy->GetAllStatsInfo(stats);
1334 }
1335 
DeleteStatsInfo(uint32_t uid)1336 int32_t NetsysNativeClient::DeleteStatsInfo(uint32_t uid)
1337 {
1338     auto proxy = GetProxy();
1339     if (proxy == nullptr) {
1340         NETMGR_LOG_E("proxy is nullptr");
1341         return NETMANAGER_ERR_GET_PROXY_FAIL;
1342     }
1343     return proxy->DeleteStatsInfo(uid);
1344 }
1345 
SetIptablesCommandForRes(const std::string & cmd,std::string & respond,NetsysNative::IptablesType ipType)1346 int32_t NetsysNativeClient::SetIptablesCommandForRes(const std::string &cmd, std::string &respond,
1347     NetsysNative::IptablesType ipType)
1348 {
1349     auto proxy = GetProxy();
1350     if (proxy == nullptr) {
1351         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1352         return NETMANAGER_ERR_GET_PROXY_FAIL;
1353     }
1354     return proxy->SetIptablesCommandForRes(cmd, respond, ipType);
1355 }
1356 
NetDiagPingHost(const OHOS::NetsysNative::NetDiagPingOption & pingOption,const sptr<OHOS::NetsysNative::INetDiagCallback> & callback)1357 int32_t NetsysNativeClient::NetDiagPingHost(const OHOS::NetsysNative::NetDiagPingOption &pingOption,
1358                                             const sptr<OHOS::NetsysNative::INetDiagCallback> &callback)
1359 {
1360     auto proxy = GetProxy();
1361     if (proxy == nullptr) {
1362         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1363         return NETMANAGER_ERR_GET_PROXY_FAIL;
1364     }
1365     return proxy->NetDiagPingHost(pingOption, callback);
1366 }
1367 
NetDiagGetRouteTable(std::list<OHOS::NetsysNative::NetDiagRouteTable> & routeTables)1368 int32_t NetsysNativeClient::NetDiagGetRouteTable(std::list<OHOS::NetsysNative::NetDiagRouteTable> &routeTables)
1369 {
1370     auto proxy = GetProxy();
1371     if (proxy == nullptr) {
1372         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1373         return NETMANAGER_ERR_GET_PROXY_FAIL;
1374     }
1375     return proxy->NetDiagGetRouteTable(routeTables);
1376 }
1377 
NetDiagGetSocketsInfo(OHOS::NetsysNative::NetDiagProtocolType socketType,OHOS::NetsysNative::NetDiagSocketsInfo & socketsInfo)1378 int32_t NetsysNativeClient::NetDiagGetSocketsInfo(OHOS::NetsysNative::NetDiagProtocolType socketType,
1379                                                   OHOS::NetsysNative::NetDiagSocketsInfo &socketsInfo)
1380 {
1381     auto proxy = GetProxy();
1382     if (proxy == nullptr) {
1383         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1384         return NETMANAGER_ERR_GET_PROXY_FAIL;
1385     }
1386     return proxy->NetDiagGetSocketsInfo(socketType, socketsInfo);
1387 }
1388 
NetDiagGetInterfaceConfig(std::list<OHOS::NetsysNative::NetDiagIfaceConfig> & configs,const std::string & ifaceName)1389 int32_t NetsysNativeClient::NetDiagGetInterfaceConfig(std::list<OHOS::NetsysNative::NetDiagIfaceConfig> &configs,
1390                                                       const std::string &ifaceName)
1391 {
1392     auto proxy = GetProxy();
1393     if (proxy == nullptr) {
1394         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1395         return NETMANAGER_ERR_GET_PROXY_FAIL;
1396     }
1397     return proxy->NetDiagGetInterfaceConfig(configs, ifaceName);
1398 }
1399 
NetDiagUpdateInterfaceConfig(const OHOS::NetsysNative::NetDiagIfaceConfig & config,const std::string & ifaceName,bool add)1400 int32_t NetsysNativeClient::NetDiagUpdateInterfaceConfig(const OHOS::NetsysNative::NetDiagIfaceConfig &config,
1401                                                          const std::string &ifaceName, bool add)
1402 {
1403     auto proxy = GetProxy();
1404     if (proxy == nullptr) {
1405         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1406         return NETMANAGER_ERR_GET_PROXY_FAIL;
1407     }
1408     return proxy->NetDiagUpdateInterfaceConfig(config, ifaceName, add);
1409 }
1410 
NetDiagSetInterfaceActiveState(const std::string & ifaceName,bool up)1411 int32_t NetsysNativeClient::NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)
1412 {
1413     auto proxy = GetProxy();
1414     if (proxy == nullptr) {
1415         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1416         return NETMANAGER_ERR_GET_PROXY_FAIL;
1417     }
1418     return proxy->NetDiagSetInterfaceActiveState(ifaceName, up);
1419 }
1420 
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)1421 int32_t NetsysNativeClient::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
1422                                          const std::string &ifName)
1423 {
1424     auto proxy = GetProxy();
1425     if (proxy == nullptr) {
1426         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1427         return NETMANAGER_ERR_GET_PROXY_FAIL;
1428     }
1429     return proxy->AddStaticArp(ipAddr, macAddr, ifName);
1430 }
1431 
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)1432 int32_t NetsysNativeClient::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
1433                                          const std::string &ifName)
1434 {
1435     auto proxy = GetProxy();
1436     if (proxy == nullptr) {
1437         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1438         return NETMANAGER_ERR_GET_PROXY_FAIL;
1439     }
1440     return proxy->DelStaticArp(ipAddr, macAddr, ifName);
1441 }
1442 
RegisterDnsResultCallback(const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> & callback,uint32_t timeStep)1443 int32_t NetsysNativeClient::RegisterDnsResultCallback(
1444     const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> &callback, uint32_t timeStep)
1445 {
1446     NETMGR_LOG_I("NetsysNativeClient::RegisterCallback");
1447     if (callback == nullptr) {
1448         NETMGR_LOG_E("Callback is nullptr");
1449         return NETMANAGER_ERR_LOCAL_PTR_NULL;
1450     }
1451     std::lock_guard lock(cbDnsReportObjMutex_);
1452     cbDnsReportObjects_.push_back(callback);
1453     dnsReportTimeStep = timeStep;
1454     return NETMANAGER_SUCCESS;
1455 }
1456 
UnregisterDnsResultCallback(const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> & callback)1457 int32_t NetsysNativeClient::UnregisterDnsResultCallback(
1458     const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> &callback)
1459 {
1460     if (callback == nullptr) {
1461         NETMGR_LOG_E("Callback is nullptr");
1462         return NETMANAGER_ERR_LOCAL_PTR_NULL;
1463     }
1464     std::lock_guard lock(cbDnsReportObjMutex_);
1465     cbDnsReportObjects_.remove(callback);
1466     return NETMANAGER_SUCCESS;
1467 }
1468 
RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)1469 int32_t NetsysNativeClient::RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
1470 {
1471     auto proxy = GetProxy();
1472     if (proxy == nullptr) {
1473         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1474         return NETMANAGER_ERR_GET_PROXY_FAIL;
1475     }
1476     return proxy->RegisterDnsHealthCallback(callback);
1477 }
1478 
UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)1479 int32_t NetsysNativeClient::UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
1480 {
1481     auto proxy = GetProxy();
1482     if (proxy == nullptr) {
1483         NETMGR_LOG_E("NetsysNativeClient proxy is nullptr");
1484         return NETMANAGER_ERR_GET_PROXY_FAIL;
1485     }
1486     return proxy->UnregisterDnsHealthCallback(callback);
1487 }
1488 
GetCookieStats(uint64_t & stats,uint32_t type,uint64_t cookie)1489 int32_t NetsysNativeClient::GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)
1490 {
1491     auto proxy = GetProxy();
1492     if (proxy == nullptr) {
1493         NETMGR_LOG_E("proxy is nullptr");
1494         return NETMANAGER_ERR_GET_PROXY_FAIL;
1495     }
1496     return proxy->GetCookieStats(stats, type, cookie);
1497 }
1498 
GetNetworkSharingType(std::set<uint32_t> & sharingTypeIsOn)1499 int32_t NetsysNativeClient::GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn)
1500 {
1501     auto proxy = GetProxy();
1502     if (proxy == nullptr) {
1503         NETMGR_LOG_E("proxy is nullptr");
1504         return NETMANAGER_ERR_GET_PROXY_FAIL;
1505     }
1506     return proxy->GetNetworkSharingType(sharingTypeIsOn);
1507 }
1508 
UpdateNetworkSharingType(uint32_t type,bool isOpen)1509 int32_t NetsysNativeClient::UpdateNetworkSharingType(uint32_t type, bool isOpen)
1510 {
1511     auto proxy = GetProxy();
1512     if (proxy == nullptr) {
1513         NETMGR_LOG_E("proxy is nullptr");
1514         return NETMANAGER_ERR_GET_PROXY_FAIL;
1515     }
1516     return proxy->UpdateNetworkSharingType(type, isOpen);
1517 }
1518 
1519 #ifdef FEATURE_NET_FIREWALL_ENABLE
SetFirewallRules(NetFirewallRuleType type,const std::vector<sptr<NetFirewallBaseRule>> & ruleList,bool isFinish)1520 int32_t NetsysNativeClient::SetFirewallRules(NetFirewallRuleType type,
1521                                              const std::vector<sptr<NetFirewallBaseRule>> &ruleList, bool isFinish)
1522 {
1523     NETMGR_LOG_D("NetsysNativeClient::SetFirewallRules");
1524     auto proxy = GetProxy();
1525     if (proxy == nullptr) {
1526         NETMGR_LOG_E("proxy is nullptr");
1527         return NETMANAGER_ERR_GET_PROXY_FAIL;
1528     }
1529     return proxy->SetFirewallRules(type, ruleList, isFinish);
1530 }
1531 
SetFirewallDefaultAction(FirewallRuleAction inDefault,FirewallRuleAction outDefault)1532 int32_t NetsysNativeClient::SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)
1533 {
1534     NETMGR_LOG_D("NetsysNativeClient::SetFirewallDefaultAction");
1535     auto proxy = GetProxy();
1536     if (proxy == nullptr) {
1537         NETMGR_LOG_E("proxy is nullptr");
1538         return NETMANAGER_ERR_GET_PROXY_FAIL;
1539     }
1540     return proxy->SetFirewallDefaultAction(inDefault, outDefault);
1541 }
1542 
SetFirewallCurrentUserId(int32_t userId)1543 int32_t NetsysNativeClient::SetFirewallCurrentUserId(int32_t userId)
1544 {
1545     NETMGR_LOG_D("NetsysNativeClient::SetFirewallCurrentUserId");
1546     auto proxy = GetProxy();
1547     if (proxy == nullptr) {
1548         NETMGR_LOG_E("proxy is nullptr");
1549         return NETMANAGER_ERR_GET_PROXY_FAIL;
1550     }
1551     return proxy->SetFirewallCurrentUserId(userId);
1552 }
1553 
ClearFirewallRules(NetFirewallRuleType type)1554 int32_t NetsysNativeClient::ClearFirewallRules(NetFirewallRuleType type)
1555 {
1556     NETMGR_LOG_D("NetsysNativeClient::ClearFirewallRules");
1557     auto proxy = GetProxy();
1558     if (proxy == nullptr) {
1559         NETMGR_LOG_E("proxy is nullptr");
1560         return NETMANAGER_ERR_GET_PROXY_FAIL;
1561     }
1562     return proxy->ClearFirewallRules(type);
1563 }
1564 
RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> & callback)1565 int32_t NetsysNativeClient::RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)
1566 {
1567     NETMGR_LOG_D("NetsysNativeClient::RegisterNetFirewallCallback");
1568     auto proxy = GetProxy();
1569     if (proxy == nullptr) {
1570         NETMGR_LOG_E("proxy is nullptr");
1571         return NETMANAGER_ERR_GET_PROXY_FAIL;
1572     }
1573     return proxy->RegisterNetFirewallCallback(callback);
1574 }
1575 
UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> & callback)1576 int32_t NetsysNativeClient::UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)
1577 {
1578     NETMGR_LOG_D("NetsysNativeClient::UnRegisterNetFirewallCallback");
1579     auto proxy = GetProxy();
1580     if (proxy == nullptr) {
1581         NETMGR_LOG_E("proxy is nullptr");
1582         return NETMANAGER_ERR_GET_PROXY_FAIL;
1583     }
1584     return proxy->UnRegisterNetFirewallCallback(callback);
1585 }
1586 #endif
1587 
SetIpv6PrivacyExtensions(const std::string & interfaceName,const uint32_t on)1588 int32_t NetsysNativeClient::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
1589 {
1590     auto proxy = GetProxy();
1591     if (proxy == nullptr) {
1592         NETMGR_LOG_E("proxy is nullptr");
1593         return NETMANAGER_ERR_GET_PROXY_FAIL;
1594     }
1595     return proxy->SetIpv6PrivacyExtensions(interfaceName, on);
1596 }
1597 
SetEnableIpv6(const std::string & interfaceName,const uint32_t on)1598 int32_t NetsysNativeClient::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
1599 {
1600     auto proxy = GetProxy();
1601     if (proxy == nullptr) {
1602         NETMGR_LOG_E("proxy is nullptr");
1603         return NETMANAGER_ERR_GET_PROXY_FAIL;
1604     }
1605     return proxy->SetEnableIpv6(interfaceName, on);
1606 }
1607 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag,bool isBroker)1608 int32_t NetsysNativeClient::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
1609                                                    bool isBroker)
1610 {
1611     auto proxy = GetProxy();
1612     if (proxy == nullptr) {
1613         NETMGR_LOG_E("proxy is nullptr");
1614         return NETMANAGER_ERR_GET_PROXY_FAIL;
1615     }
1616 
1617     return proxy->SetNetworkAccessPolicy(uid, policy, reconfirmFlag, isBroker);
1618 }
1619 
DeleteNetworkAccessPolicy(uint32_t uid)1620 int32_t NetsysNativeClient::DeleteNetworkAccessPolicy(uint32_t uid)
1621 {
1622     auto proxy = GetProxy();
1623     if (proxy == nullptr) {
1624         NETMGR_LOG_E("proxy is nullptr");
1625         return NETMANAGER_ERR_GET_PROXY_FAIL;
1626     }
1627 
1628     return proxy->DeleteNetworkAccessPolicy(uid);
1629 }
1630 
ClearFirewallAllRules()1631 int32_t NetsysNativeClient::ClearFirewallAllRules()
1632 {
1633     auto proxy = GetProxy();
1634     if (proxy == nullptr) {
1635         NETMGR_LOG_E("proxy is nullptr");
1636         return NETMANAGER_ERR_GET_PROXY_FAIL;
1637     }
1638 
1639     return proxy->ClearFirewallAllRules();
1640 }
1641 
NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)1642 int32_t NetsysNativeClient::NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)
1643 {
1644     auto proxy = GetProxy();
1645     if (proxy == nullptr) {
1646         NETMGR_LOG_E("proxy is nullptr");
1647         return NETMANAGER_ERR_GET_PROXY_FAIL;
1648     }
1649 
1650     return proxy->NotifyNetBearerTypeChange(bearerTypes);
1651 }
1652 
StartClat(const std::string & interfaceName,int32_t netId,const std::string & nat64PrefixStr)1653 int32_t NetsysNativeClient::StartClat(const std::string &interfaceName, int32_t netId,
1654                                       const std::string &nat64PrefixStr)
1655 {
1656     auto proxy = GetProxy();
1657     if (proxy == nullptr) {
1658         NETMGR_LOG_E("proxy is nullptr");
1659         return NETMANAGER_ERR_GET_PROXY_FAIL;
1660     }
1661     return proxy->StartClat(interfaceName, netId, nat64PrefixStr);
1662 }
1663 
StopClat(const std::string & interfaceName)1664 int32_t NetsysNativeClient::StopClat(const std::string &interfaceName)
1665 {
1666     auto proxy = GetProxy();
1667     if (proxy == nullptr) {
1668         NETMGR_LOG_E("proxy is nullptr");
1669         return NETMANAGER_ERR_GET_PROXY_FAIL;
1670     }
1671     return proxy->StopClat(interfaceName);
1672 }
1673 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)1674 int32_t NetsysNativeClient::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
1675 {
1676     auto proxy = GetProxy();
1677     if (proxy == nullptr) {
1678         NETMGR_LOG_E("proxy is nullptr");
1679         return NETMANAGER_ERR_GET_PROXY_FAIL;
1680     }
1681     return proxy->SetNicTrafficAllowed(ifaceNames, status);
1682 }
1683 } // namespace NetManagerStandard
1684 } // namespace OHOS
1685