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