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