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 "mock_netsys_native_client.h"
17  
18  #include <ctime>
19  #include <dirent.h>
20  #include <sys/ioctl.h>
21  #include <sys/types.h>
22  #include <sys/stat.h>
23  #include <fcntl.h>
24  #include <unistd.h>
25  #include <cstring>
26  #include <sys/socket.h>
27  #include <sys/un.h>
28  #include <netinet/in.h>
29  #include <arpa/inet.h>
30  #include <net/route.h>
31  #include <netdb.h>
32  
33  #include "securec.h"
34  
35  #include "net_conn_constants.h"
36  #include "net_mgr_log_wrapper.h"
37  #include "netmanager_base_common_utils.h"
38  
39  using namespace OHOS::NetManagerStandard::CommonUtils;
40  namespace OHOS {
41  namespace NetManagerStandard {
42  namespace {
43  const std::string INTERFACE_LIST_DIR = "/sys/class/net/";
44  const std::string UID_LIST_DIR = "/data/data/uid/";
45  const std::string UID_TRAFFIC_BPF_PATH = "/dev/socket/traffic";
46  const std::string TEST_CELL_RX = "/data/cell_rx";
47  const std::string TEST_CELL_TX = "/data/cell_tx";
48  const std::string TEST_IFACE_RX = "/data/iface_rx";
49  const std::string TEST_IFACE_TX = "/data/iface_tx";
50  const std::string TEST_IFACE_RX_P = "/data/iface_rx_p";
51  const std::string TEST_IFACE_TX_P = "/data/iface_tx_p";
52  const std::string TEST_ALL_RX = "/data/all_tx";
53  const std::string TEST_ALL_TX = "/data/all_tx";
54  const std::string NET_STATS_FILE_RX_BYTES = "rx_bytes";
55  const std::string NET_STATS_FILE_TX_BYTES = "tx_bytes";
56  const std::string NET_STATS_FILE_RX_PACKETS = "rx_packets";
57  const std::string NET_STATS_FILE_TX_PACKETS = "tx_packets";
58  constexpr int32_t MOCK_MODULO_LAST_SIX_DIGITS = 100000;
59  
CheckFilePath(const std::string & fileName,std::string & realPath)60  bool CheckFilePath(const std::string &fileName, std::string &realPath)
61  {
62      char tmpPath[PATH_MAX] = {0};
63      if (!realpath(fileName.c_str(), tmpPath)) {
64          NETMGR_LOG_E("file name is illegal");
65          return false;
66      }
67      realPath = tmpPath;
68      return true;
69  }
70  } // namespace
71  
MockNetsysNativeClient()72  MockNetsysNativeClient::MockNetsysNativeClient()
73  {
74      Init();
75  }
76  
~MockNetsysNativeClient()77  MockNetsysNativeClient::~MockNetsysNativeClient() {}
78  
Init()79  void MockNetsysNativeClient::Init()
80  {
81      return;
82  }
83  
RegisterMockApi()84  void MockNetsysNativeClient::RegisterMockApi()
85  {
86      mockApi_.insert(MOCK_INTERFACECLEARADDRS_API);
87      mockApi_.insert(MOCK_GETCELLULARRXBYTES_API);
88      mockApi_.insert(MOCK_GETCELLULARTXBYTES_API);
89      mockApi_.insert(MOCK_GETALLRXBYTES_API);
90      mockApi_.insert(MOCK_GETALLTXBYTES_API);
91      mockApi_.insert(MOCK_GETUIDRXBYTES_API);
92      mockApi_.insert(MOCK_GETUIDTXBYTES_API);
93      mockApi_.insert(MOCK_GETUIDONIFACETXBYTES_API);
94      mockApi_.insert(MOCK_GETUIDONIFACETXBYTES_API);
95      mockApi_.insert(MOCK_GETIFACERXBYTES_API);
96      mockApi_.insert(MOCK_GETIFACETXBYTES_API);
97      mockApi_.insert(MOCK_INTERFACEGETLIST_API);
98      mockApi_.insert(MOCK_UIDGETLIST_API);
99      mockApi_.insert(MOCK_GETIFACERXPACKETS_API);
100      mockApi_.insert(MOCK_GETIFACETXPACKETS_API);
101      mockApi_.insert(MOCK_BINDSOCKET_API);
102      mockApi_.insert(MOCK_SHAREDNSSET_API);
103      mockApi_.insert(MOCK_REGISTERNETSYSNOTIFYCALLBACK_API);
104      mockApi_.insert(MOCK_BINDNETWORKSERVICEVPN_API);
105      mockApi_.insert(MOCK_ENABLEVIRTUALNETIFACECARD_API);
106      mockApi_.insert(MOCK_SETIPADDRESS_API);
107      mockApi_.insert(MOCK_SETBLOCKING_API);
108  }
109  
CheckMockApi(const std::string & api)110  bool MockNetsysNativeClient::CheckMockApi(const std::string &api)
111  {
112      return mockApi_.find(api) != mockApi_.end();
113  }
114  
NetworkCreatePhysical(int32_t netId,int32_t permission)115  int32_t MockNetsysNativeClient::NetworkCreatePhysical(int32_t netId, int32_t permission)
116  {
117      NETMGR_LOG_I("Create Physical network: netId[%{public}d], permission[%{public}d]", netId, permission);
118      return 0;
119  }
120  
NetworkDestroy(int32_t netId)121  int32_t MockNetsysNativeClient::NetworkDestroy(int32_t netId)
122  {
123      NETMGR_LOG_I("Destroy network: netId[%{public}d]", netId);
124      return 0;
125  }
126  
NetworkAddInterface(int32_t netId,const std::string & iface,NetBearType netBearerType)127  int32_t MockNetsysNativeClient::NetworkAddInterface(int32_t netId, const std::string &iface, NetBearType netBearerType)
128  {
129      NETMGR_LOG_I("Add network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
130      return 0;
131  }
132  
NetworkRemoveInterface(int32_t netId,const std::string & iface)133  int32_t MockNetsysNativeClient::NetworkRemoveInterface(int32_t netId, const std::string &iface)
134  {
135      NETMGR_LOG_I("Remove network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
136      return 0;
137  }
138  
NetworkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)139  int32_t MockNetsysNativeClient::NetworkAddRoute(int32_t netId, const std::string &ifName,
140      const std::string &destination, const std::string &nextHop)
141  {
142      NETMGR_LOG_I("Add Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
143          netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
144      std::string mask = "0.0.0.0";
145      return AddRoute(destination, mask, nextHop, ifName);
146  }
147  
NetworkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)148  int32_t MockNetsysNativeClient::NetworkRemoveRoute(int32_t netId, const std::string &ifName,
149      const std::string &destination, const std::string &nextHop)
150  {
151      NETMGR_LOG_I("Remove Route: netId[%{public}d], ifName[%{public}s], destination[%{public}s], nextHop[%{public}s]",
152          netId, ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
153      return 0;
154  }
155  
SetInterfaceDown(const std::string & iface)156  int32_t MockNetsysNativeClient::SetInterfaceDown(const std::string &iface)
157  {
158      NETMGR_LOG_I("Set interface down: iface[%{public}s]", iface.c_str());
159      return 0;
160  }
161  
SetInterfaceUp(const std::string & iface)162  int32_t MockNetsysNativeClient::SetInterfaceUp(const std::string &iface)
163  {
164      NETMGR_LOG_I("Set interface up: iface[%{public}s]", iface.c_str());
165      return 0;
166  }
167  
ClearInterfaceAddrs(const std::string & ifName)168  void MockNetsysNativeClient::ClearInterfaceAddrs(const std::string &ifName)
169  {
170      NETMGR_LOG_I("Clear addrs: ifName[%{public}s]", ifName.c_str());
171      return;
172  }
173  
GetInterfaceMtu(const std::string & ifName)174  int32_t MockNetsysNativeClient::GetInterfaceMtu(const std::string &ifName)
175  {
176      NETMGR_LOG_I("Get mtu: ifName[%{public}s]", ifName.c_str());
177      return 0;
178  }
179  
SetInterfaceMtu(const std::string & ifName,int32_t mtu)180  int32_t MockNetsysNativeClient::SetInterfaceMtu(const std::string &ifName, int32_t mtu)
181  {
182      NETMGR_LOG_I("Set mtu: ifName[%{public}s], mtu[%{public}d]", ifName.c_str(), mtu);
183      return 0;
184  }
185  
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)186  int32_t MockNetsysNativeClient::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
187      int32_t prefixLength)
188  {
189      NETMGR_LOG_I("Add address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
190          ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
191      return 0;
192  }
193  
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)194  int32_t MockNetsysNativeClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
195      int32_t prefixLength)
196  {
197      NETMGR_LOG_I("Delete address: ifName[%{public}s], ipAddr[%{public}s], prefixLength[%{public}d]",
198          ifName.c_str(), ToAnonymousIp(ipAddr).c_str(), prefixLength);
199      return 0;
200  }
201  
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)202  int32_t MockNetsysNativeClient::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
203      const std::vector<std::string> &servers, const std::vector<std::string> &domains)
204  {
205      NETMGR_LOG_I("Set resolver config: netId[%{public}d]", netId);
206      return 0;
207  }
208  
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)209  int32_t MockNetsysNativeClient::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
210      std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount)
211  {
212      NETMGR_LOG_I("Get resolver config: netId[%{public}d]", netId);
213      return 0;
214  }
215  
CreateNetworkCache(uint16_t netId)216  int32_t MockNetsysNativeClient::CreateNetworkCache(uint16_t netId)
217  {
218      NETMGR_LOG_I("create dns cache: netId[%{public}d]", netId);
219      return 0;
220  }
221  
DestroyNetworkCache(uint16_t netId)222  int32_t MockNetsysNativeClient::DestroyNetworkCache(uint16_t netId)
223  {
224      NETMGR_LOG_I("Destroy dns cache: netId[%{public}d]", netId);
225      return 0;
226  }
227  
GetInterfaceTrafficByType(const std::string & path,const std::string & type)228  static long GetInterfaceTrafficByType(const std::string &path, const std::string &type)
229  {
230      if (path.empty()) {
231          return -1;
232      }
233      std::string trafficPath = path + type;
234      std::string realPath;
235      if (!CheckFilePath(trafficPath, realPath)) {
236          NETMGR_LOG_E("file does not exist! ");
237          return -1;
238      }
239      int fd = open(realPath.c_str(), 0, 0666);
240      if (fd == -1) {
241          return -1;
242      }
243      char buf[100] = {0};
244      if (read(fd, buf, sizeof(buf)) == -1) {
245          close(fd);
246          return -1;
247      }
248      close(fd);
249      long infBytes = atol(buf);
250      return infBytes;
251  }
252  
GetCellularRxBytes()253  int64_t MockNetsysNativeClient::GetCellularRxBytes()
254  {
255      NETMGR_LOG_I("GetCellularRxBytes");
256      if (access(TEST_CELL_RX.c_str(), F_OK) == 0) {
257          return 0;
258      }
259      return GetIfaceBytes("usb0", NET_STATS_FILE_RX_BYTES.c_str());
260  }
261  
GetCellularTxBytes()262  int64_t MockNetsysNativeClient::GetCellularTxBytes()
263  {
264      NETMGR_LOG_I("GetCellularTxBytes");
265      if (access(TEST_CELL_TX.c_str(), F_OK) == 0) {
266          return 0;
267      }
268      return GetIfaceBytes("usb0", NET_STATS_FILE_TX_BYTES.c_str());
269  }
270  
GetAllBytes(const std::string & filename)271  int64_t MockNetsysNativeClient::GetAllBytes(const std::string &filename)
272  {
273      NETMGR_LOG_I("GetAllBytes");
274      long allBytes = 0;
275      std::vector<std::string> ifNameList = InterfaceGetList();
276      if (ifNameList.empty()) {
277          return allBytes;
278      }
279      for (auto iter = ifNameList.begin(); iter != ifNameList.end(); iter++) {
280          if (*iter != "lo") {
281              std::string base_traffic_path = INTERFACE_LIST_DIR + (*iter) + "/" + "statistics" + "/";
282              long bytes = GetInterfaceTrafficByType(base_traffic_path, filename.c_str());
283              allBytes = allBytes + bytes;
284          }
285      }
286      return allBytes;
287  }
288  
GetAllRxBytes()289  int64_t MockNetsysNativeClient::GetAllRxBytes()
290  {
291      NETMGR_LOG_I("GetAllRxBytes");
292      if (access(TEST_ALL_RX.c_str(), F_OK) == 0) {
293          return 0;
294      }
295      return GetAllBytes(NET_STATS_FILE_RX_BYTES.c_str());
296  }
297  
GetAllTxBytes()298  int64_t MockNetsysNativeClient::GetAllTxBytes()
299  {
300      NETMGR_LOG_I("GetAllTxBytes");
301      if (access(TEST_ALL_TX.c_str(), F_OK) == 0) {
302          return 0;
303      }
304      return GetAllBytes(NET_STATS_FILE_TX_BYTES.c_str());
305  }
306  
GetUidTrafficFromBpf(int uid,int cgroupType)307  static long GetUidTrafficFromBpf(int uid, int cgroupType)
308  {
309      int sock = socket(AF_UNIX, SOCK_STREAM, 0);
310      if (sock < 0) {
311          close(sock);
312          return -1;
313      }
314      sockaddr_un sockAddrInfo;
315      sockAddrInfo.sun_family = AF_UNIX;
316      if (strcpy_s(sockAddrInfo.sun_path, sizeof(sockAddrInfo.sun_path), UID_TRAFFIC_BPF_PATH.c_str()) != 0) {
317          close(sock);
318          return -1;
319      }
320      if (connect(sock, reinterpret_cast<sockaddr *>(&sockAddrInfo), sizeof(sockAddrInfo)) != 0) {
321          close(sock);
322          return -1;
323      }
324      char buf[128];
325      bzero(buf, sizeof(buf));
326      std::string query = std::to_string(uid) + "," + std::to_string(cgroupType);
327      if (strcpy_s(buf, sizeof(buf), query.c_str()) != 0) {
328          close(sock);
329          return -1;
330      }
331      ssize_t writeRet = write(sock, buf, strlen(buf));
332      if (writeRet < 0) {
333          close(sock);
334          return -1;
335      }
336      bzero(buf, sizeof(buf));
337      ssize_t readRet = read(sock, buf, sizeof(buf));
338      if (readRet < 0) {
339          close(sock);
340          return -1;
341      }
342      close(sock);
343      return atol(buf);
344  }
345  
GetUidRxBytes(uint32_t uid)346  int64_t MockNetsysNativeClient::GetUidRxBytes(uint32_t uid)
347  {
348      NETMGR_LOG_D("GetUidRxBytes uid is [%{public}u]", uid);
349      long result = GetUidTrafficFromBpf(uid, 0);
350      return static_cast<int64_t>(result);
351  }
352  
GetUidTxBytes(uint32_t uid)353  int64_t MockNetsysNativeClient::GetUidTxBytes(uint32_t uid)
354  {
355      NETMGR_LOG_D("GetUidTxBytes uid is [%{public}u]", uid);
356      long result = GetUidTrafficFromBpf(uid, 1);
357      return static_cast<int64_t>(result);
358  }
359  
GetUidOnIfaceBytes(uint32_t uid,const std::string & interfaceName)360  static int64_t GetUidOnIfaceBytes(uint32_t uid, const std::string &interfaceName)
361  {
362      time_t now = time(nullptr);
363      now %= MOCK_MODULO_LAST_SIX_DIGITS;
364      return static_cast<int64_t>(now);
365  }
366  
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)367  int64_t MockNetsysNativeClient::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
368  {
369      NETMGR_LOG_D("GetUidOnIfaceRxBytes uid is [%{public}u] "
370          "iface name is [%{public}s]", uid, interfaceName.c_str());
371      return GetUidOnIfaceBytes(uid, interfaceName);
372  }
373  
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)374  int64_t MockNetsysNativeClient::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
375  {
376      NETMGR_LOG_D("GetUidOnIfaceRxBytes uid is [%{public}u] "
377          "iface name is [%{public}s]", uid, interfaceName.c_str());
378      return GetUidOnIfaceBytes(uid, interfaceName);
379  }
380  
GetIfaceBytes(const std::string & interfaceName,const std::string & filename)381  int64_t MockNetsysNativeClient::GetIfaceBytes(const std::string &interfaceName, const std::string &filename)
382  {
383      int64_t bytes = 0;
384      std::vector<std::string> ifNameList = InterfaceGetList();
385      if (ifNameList.empty()) {
386          return bytes;
387      }
388      for (auto iter = ifNameList.begin(); iter != ifNameList.end(); iter++) {
389          if (interfaceName == *iter) {
390              std::string baseTrafficPath = INTERFACE_LIST_DIR + (*iter) + "/" + "statistics" + "/";
391              long infRxBytes = GetInterfaceTrafficByType(baseTrafficPath, filename.c_str());
392              bytes = infRxBytes == -1 ? 0 : infRxBytes;
393          }
394      }
395      return bytes;
396  }
397  
GetIfaceRxBytes(const std::string & interfaceName)398  int64_t MockNetsysNativeClient::GetIfaceRxBytes(const std::string &interfaceName)
399  {
400      NETMGR_LOG_D("GetIfaceRxBytes iface name is [%{public}s]", interfaceName.c_str());
401      if (access(TEST_IFACE_RX.c_str(), F_OK) == 0) {
402          return 0;
403      }
404      return GetIfaceBytes(interfaceName, NET_STATS_FILE_RX_BYTES.c_str());
405  }
406  
GetIfaceTxBytes(const std::string & interfaceName)407  int64_t MockNetsysNativeClient::GetIfaceTxBytes(const std::string &interfaceName)
408  {
409      NETMGR_LOG_D("GetIfaceTxBytes iface name is [%{public}s]", interfaceName.c_str());
410      if (access(TEST_IFACE_TX.c_str(), F_OK) == 0) {
411          return 0;
412      }
413      return GetIfaceBytes(interfaceName, NET_STATS_FILE_TX_BYTES.c_str());
414  }
415  
GetIfaceRxPackets(const std::string & interfaceName)416  int64_t MockNetsysNativeClient::GetIfaceRxPackets(const std::string &interfaceName)
417  {
418      NETMGR_LOG_D("GetIfaceRxPackets iface name is [%{public}s]", interfaceName.c_str());
419      if (access(TEST_IFACE_RX_P.c_str(), F_OK) == 0) {
420          return 0;
421      }
422      return GetIfaceBytes(interfaceName, NET_STATS_FILE_RX_PACKETS.c_str());
423  }
424  
GetIfaceTxPackets(const std::string & interfaceName)425  int64_t MockNetsysNativeClient::GetIfaceTxPackets(const std::string &interfaceName)
426  {
427      NETMGR_LOG_D("GetIfaceTxPackets iface name is [%{public}s]", interfaceName.c_str());
428      if (access(TEST_IFACE_TX_P.c_str(), F_OK) == 0) {
429          return 0;
430      }
431      return GetIfaceBytes(interfaceName, NET_STATS_FILE_TX_PACKETS.c_str());
432  }
433  
InterfaceGetList()434  std::vector<std::string> MockNetsysNativeClient::InterfaceGetList()
435  {
436      NETMGR_LOG_D("InterfaceGetList");
437      DIR *dir(nullptr);
438      std::vector<std::string> ifList;
439      if ((dir = opendir(INTERFACE_LIST_DIR.c_str())) == nullptr) {
440          return ifList;
441      }
442      struct dirent *ptr(nullptr);
443      while ((ptr = readdir(dir)) != nullptr) {
444          if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
445              continue;
446          }
447          ifList.push_back(ptr->d_name);
448      }
449      closedir(dir);
450      return ifList;
451  }
452  
UidGetList()453  std::vector<std::string> MockNetsysNativeClient::UidGetList()
454  {
455      NETMGR_LOG_I("UidGetList");
456      DIR *dir(nullptr);
457      std::vector<std::string> uidList;
458      if ((dir = opendir(UID_LIST_DIR.c_str())) == nullptr) {
459          return uidList;
460      }
461      struct dirent *ptr(nullptr);
462      while ((ptr = readdir(dir)) != nullptr) {
463          if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
464              continue;
465          }
466          uidList.push_back(ptr->d_name);
467      }
468      closedir(dir);
469      return uidList;
470  }
471  
AddRoute(const std::string & ip,const std::string & mask,const std::string & gateWay,const std::string & devName)472  int32_t MockNetsysNativeClient::AddRoute(const std::string &ip, const std::string &mask,
473      const std::string &gateWay, const std::string &devName)
474  {
475      struct sockaddr_in _sin;
476      struct rtentry  rt;
477      bzero(&rt, sizeof(struct rtentry));
478      bzero(&_sin, sizeof(struct sockaddr_in));
479      _sin.sin_family = AF_INET;
480      _sin.sin_port = 0;
481      if (inet_aton(gateWay.c_str(), &(_sin.sin_addr)) < 0) {
482          NETMGR_LOG_E("inet_aton gateWay[%{private}s]", gateWay.c_str());
483          return -1;
484      }
485      int copyRet = memcpy_s(&rt.rt_gateway, sizeof(rt.rt_gateway), &_sin, sizeof(struct sockaddr_in));
486      NETMGR_LOG_I("copyRet = %{public}d", copyRet);
487      (reinterpret_cast<struct sockaddr_in *>(&rt.rt_dst))->sin_family = AF_INET;
488      if (inet_aton(ip.c_str(), &((struct sockaddr_in *)&rt.rt_dst)->sin_addr) < 0) {
489          NETMGR_LOG_E("inet_aton ip[%{public}s]", ToAnonymousIp(ip).c_str());
490          return -1;
491      }
492      (reinterpret_cast<struct sockaddr_in *>(&rt.rt_genmask))->sin_family = AF_INET;
493      if (inet_aton(mask.c_str(), &(reinterpret_cast<struct sockaddr_in *>(&rt.rt_genmask))->sin_addr) < 0) {
494          NETMGR_LOG_E("inet_aton mask[%{public}s]", mask.c_str());
495          return -1;
496      }
497      auto name = std::make_unique<char[]>(devName.size());
498      if (!devName.empty()) {
499          if (strcpy_s(name.get(), devName.size(), devName.c_str()) != 0) {
500              return -1;
501          }
502          rt.rt_dev = name.get();
503      }
504      rt.rt_flags = RTF_GATEWAY;
505      int fd = socket(AF_INET, SOCK_DGRAM, 0);
506      if (fd < 0) {
507          NETMGR_LOG_E("create socket fd[%{public}d]", fd);
508          return -1;
509      }
510      if (ioctl(fd, SIOCADDRT, &rt) < 0) {
511          NETMGR_LOG_E("ioctl error");
512          close(fd);
513          return -1;
514      }
515      close(fd);
516      return 0;
517  }
518  
SetDefaultNetWork(int32_t netId)519  int32_t MockNetsysNativeClient::SetDefaultNetWork(int32_t netId)
520  {
521      NETMGR_LOG_D("SetDefaultNetWork netId is [%{public}d]", netId);
522      return 0;
523  }
524  
ClearDefaultNetWorkNetId()525  int32_t MockNetsysNativeClient::ClearDefaultNetWorkNetId()
526  {
527      NETMGR_LOG_D("ClearDefaultNetWorkNetId");
528      return 0;
529  }
530  
BindSocket(int32_t socketFd,uint32_t netId)531  int32_t MockNetsysNativeClient::BindSocket(int32_t socketFd, uint32_t netId)
532  {
533      NETMGR_LOG_D("BindSocket: netId = [%{public}u]", netId);
534      return NETMANAGER_SUCCESS;
535  }
536  
ShareDnsSet(uint16_t netId)537  int32_t MockNetsysNativeClient::ShareDnsSet(uint16_t netId)
538  {
539      NETMGR_LOG_D("ShareDnsSet: netId[%{public}d]", netId);
540      return 0;
541  }
542  
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)543  int32_t MockNetsysNativeClient::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
544  {
545      NETMGR_LOG_D("RegisterNetsysNotifyCallback");
546      return 0;
547  }
548  
BindNetworkServiceVpn(int32_t socketFd)549  int32_t MockNetsysNativeClient::BindNetworkServiceVpn(int32_t socketFd)
550  {
551      NETMGR_LOG_D("BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
552      return 0;
553  }
554  
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)555  int32_t MockNetsysNativeClient::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
556  {
557      NETMGR_LOG_D("EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
558      const char *ifaceName = "wlan0";
559      strncpy_s(ifRequest.ifr_name, sizeof(ifRequest.ifr_name), ifaceName, strlen(ifaceName));
560      NETMGR_LOG_D("ifRequest.ifr_name[%{public}s]", ifRequest.ifr_name);
561      ifaceFd = 1;
562      return 0;
563  }
564  
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)565  int32_t MockNetsysNativeClient::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
566      struct ifreq &ifRequest)
567  {
568      return 0;
569  }
570  
SetBlocking(int32_t ifaceFd,bool isBlock)571  int32_t MockNetsysNativeClient::SetBlocking(int32_t ifaceFd, bool isBlock)
572  {
573      NETMGR_LOG_D("SetBlocking: ifaceFd[%{public}d], isBlock[%{public}d]", ifaceFd, isBlock);
574      return 0;
575  }
576  
StartDhcpClient(const std::string & iface,bool bIpv6)577  int32_t MockNetsysNativeClient::StartDhcpClient(const std::string &iface, bool bIpv6)
578  {
579      NETMGR_LOG_D("StartDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(),
580          bIpv6);
581      return 0;
582  }
583  
StopDhcpClient(const std::string & iface,bool bIpv6)584  int32_t MockNetsysNativeClient::StopDhcpClient(const std::string &iface, bool bIpv6)
585  {
586      NETMGR_LOG_D("StopDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(),
587          bIpv6);
588      return 0;
589  }
590  
RegisterCallback(sptr<NetsysControllerCallback> callback)591  int32_t MockNetsysNativeClient::RegisterCallback(sptr<NetsysControllerCallback> callback)
592  {
593      NETMGR_LOG_D("RegisterCallback");
594      return 0;
595  }
596  
StartDhcpService(const std::string & iface,const std::string & ipv4addr)597  int32_t MockNetsysNativeClient::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
598  {
599      return 0;
600  }
601  
StopDhcpService(const std::string & iface)602  int32_t MockNetsysNativeClient::StopDhcpService(const std::string &iface)
603  {
604      return 0;
605  }
606  
SetIpv6PrivacyExtensions(const std::string & interfaceName,const uint32_t on)607  int32_t MockNetsysNativeClient::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
608  {
609      return 0;
610  }
611  
SetEnableIpv6(const std::string & interfaceName,const uint32_t on)612  int32_t MockNetsysNativeClient::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
613  {
614      return 0;
615  }
616  } // namespace NetManagerStandard
617  } // namespace OHOS
618