• 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 <cstdlib>
17 #include <net/route.h>
18 #include <netdb.h>
19 #include <unistd.h>
20 
21 #include "ipc_skeleton.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_base_common_utils.h"
24 #include "netmanager_base_permission.h"
25 #include "netnative_log_wrapper.h"
26 #include "securec.h"
27 
28 #include "netsys_native_service_stub.h"
29 
30 using namespace OHOS::NetManagerStandard::CommonUtils;
31 namespace OHOS {
32 namespace NetsysNative {
33 static constexpr int32_t MAX_FLAG_NUM = 64;
34 static constexpr int32_t MAX_DNS_CONFIG_SIZE = 4;
35 static constexpr int32_t NETMANAGER_ERR_PERMISSION_DENIED = 201;
36 static constexpr uint32_t UIDS_LIST_MAX_SIZE = 1024;
37 static constexpr uint32_t MAX_UID_ARRAY_SIZE = 1024;
38 
NetsysNativeServiceStub()39 NetsysNativeServiceStub::NetsysNativeServiceStub()
40 {
41     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_RESOLVER_CONFIG)] =
42         &NetsysNativeServiceStub::CmdSetResolverConfig;
43     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_RESOLVER_CONFIG)] =
44         &NetsysNativeServiceStub::CmdGetResolverConfig;
45     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CREATE_NETWORK_CACHE)] =
46         &NetsysNativeServiceStub::CmdCreateNetworkCache;
47     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DESTROY_NETWORK_CACHE)] =
48         &NetsysNativeServiceStub::CmdDestroyNetworkCache;
49     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ADDR_INFO)] =
50         &NetsysNativeServiceStub::CmdGetAddrInfo;
51     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_MTU)] =
52         &NetsysNativeServiceStub::CmdSetInterfaceMtu;
53     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_MTU)] =
54         &NetsysNativeServiceStub::CmdGetInterfaceMtu;
55     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_NOTIFY_CALLBACK)] =
56         &NetsysNativeServiceStub::CmdRegisterNotifyCallback;
57     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_NOTIFY_CALLBACK)] =
58         &NetsysNativeServiceStub::CmdUnRegisterNotifyCallback;
59     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE)] =
60         &NetsysNativeServiceStub::CmdNetworkAddRoute;
61     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE)] =
62         &NetsysNativeServiceStub::CmdNetworkRemoveRoute;
63     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE_PARCEL)] =
64         &NetsysNativeServiceStub::CmdNetworkAddRouteParcel;
65     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE_PARCEL)] =
66         &NetsysNativeServiceStub::CmdNetworkRemoveRouteParcel;
67     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_DEFAULT)] =
68         &NetsysNativeServiceStub::CmdNetworkSetDefault;
69     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_GET_DEFAULT)] =
70         &NetsysNativeServiceStub::CmdNetworkGetDefault;
71     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CLEAR_DEFAULT)] =
72         &NetsysNativeServiceStub::CmdNetworkClearDefault;
73     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_PROC_SYS_NET)] =
74         &NetsysNativeServiceStub::CmdGetProcSysNet;
75     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_PROC_SYS_NET)] =
76         &NetsysNativeServiceStub::CmdSetProcSysNet;
77     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_PHYSICAL)] =
78         &NetsysNativeServiceStub::CmdNetworkCreatePhysical;
79     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_ADD_ADDRESS)] =
80         &NetsysNativeServiceStub::CmdAddInterfaceAddress;
81     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_DEL_ADDRESS)] =
82         &NetsysNativeServiceStub::CmdDelInterfaceAddress;
83 
84     InitBandwidthOpToInterfaceMap();
85     InitFirewallOpToInterfaceMap();
86     InitOpToInterfaceMapExt();
87     uids_ = {UID_ROOT, UID_SHELL, UID_NET_MANAGER, UID_WIFI, UID_RADIO, UID_HIDUMPER_SERVICE,
88              UID_SAMGR, UID_PARAM_WATCHER, UID_EDM};
89 }
90 
InitBandwidthOpToInterfaceMap()91 void NetsysNativeServiceStub::InitBandwidthOpToInterfaceMap()
92 {
93     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_SHARING_NETWORK_TRAFFIC)] =
94         &NetsysNativeServiceStub::CmdGetNetworkSharingTraffic;
95     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_TOTAL_STATS)] =
96         &NetsysNativeServiceStub::CmdGetTotalStats;
97     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_UID_STATS)] =
98         &NetsysNativeServiceStub::CmdGetUidStats;
99     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_IFACE_STATS)] =
100         &NetsysNativeServiceStub::CmdGetIfaceStats;
101     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_STATS_INFO)] =
102         &NetsysNativeServiceStub::CmdGetAllStatsInfo;
103     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_VIRTUAL)] =
104         &NetsysNativeServiceStub::CmdNetworkCreateVirtual;
105     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_UIDS)] =
106         &NetsysNativeServiceStub::CmdNetworkAddUids;
107     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DEL_UIDS)] =
108         &NetsysNativeServiceStub::CmdNetworkDelUids;
109     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER)] =
110         &NetsysNativeServiceStub::CmdBandwidthEnableDataSaver;
111     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_SET_IFACE_QUOTA)] =
112         &NetsysNativeServiceStub::CmdBandwidthSetIfaceQuota;
113     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA)] =
114         &NetsysNativeServiceStub::CmdBandwidthRemoveIfaceQuota;
115     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_DENIED_LIST)] =
116         &NetsysNativeServiceStub::CmdBandwidthAddDeniedList;
117     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST)] =
118         &NetsysNativeServiceStub::CmdBandwidthRemoveDeniedList;
119     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST)] =
120         &NetsysNativeServiceStub::CmdBandwidthAddAllowedList;
121     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST)] =
122         &NetsysNativeServiceStub::CmdBandwidthRemoveAllowedList;
123     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_INTERNET_PERMISSION)] =
124         &NetsysNativeServiceStub::CmdSetInternetPermission;
125 }
126 
InitFirewallOpToInterfaceMap()127 void NetsysNativeServiceStub::InitFirewallOpToInterfaceMap()
128 {
129     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN)] =
130         &NetsysNativeServiceStub::CmdFirewallSetUidsAllowedListChain;
131     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN)] =
132         &NetsysNativeServiceStub::CmdFirewallSetUidsDeniedListChain;
133     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_ENABLE_CHAIN)] =
134         &NetsysNativeServiceStub::CmdFirewallEnableChain;
135     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_RULE)] =
136         &NetsysNativeServiceStub::CmdFirewallSetUidRule;
137 }
138 
InitOpToInterfaceMapExt()139 void NetsysNativeServiceStub::InitOpToInterfaceMapExt()
140 {
141     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IP_ADDRESS)] =
142         &NetsysNativeServiceStub::CmdInterfaceSetIpAddress;
143     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IFF_UP)] =
144         &NetsysNativeServiceStub::CmdInterfaceSetIffUp;
145     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_INTERFACE)] =
146         &NetsysNativeServiceStub::CmdNetworkAddInterface;
147     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_INTERFACE)] =
148         &NetsysNativeServiceStub::CmdNetworkRemoveInterface;
149     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DESTROY)] =
150         &NetsysNativeServiceStub::CmdNetworkDestroy;
151     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_FWMARK_FOR_NETWORK)] =
152         &NetsysNativeServiceStub::CmdGetFwmarkForNetwork;
153     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_CONFIG)] =
154         &NetsysNativeServiceStub::CmdSetInterfaceConfig;
155     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_CONFIG)] =
156         &NetsysNativeServiceStub::CmdGetInterfaceConfig;
157     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_LIST)] =
158         &NetsysNativeServiceStub::CmdInterfaceGetList;
159     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_CLIENT)] =
160         &NetsysNativeServiceStub::CmdStartDhcpClient;
161     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_CLIENT)] =
162         &NetsysNativeServiceStub::CmdStopDhcpClient;
163     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_SERVICE)] =
164         &NetsysNativeServiceStub::CmdStartDhcpService;
165     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_SERVICE)] =
166         &NetsysNativeServiceStub::CmdStopDhcpService;
167     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPENABLE_FORWARDING)] =
168         &NetsysNativeServiceStub::CmdIpEnableForwarding;
169     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPDISABLE_FORWARDING)] =
170         &NetsysNativeServiceStub::CmdIpDisableForwarding;
171     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_NAT)] =
172         &NetsysNativeServiceStub::CmdEnableNat;
173     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_NAT)] =
174         &NetsysNativeServiceStub::CmdDisableNat;
175     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_ADD_INTERFACE_FORWARD)] =
176         &NetsysNativeServiceStub::CmdIpfwdAddInterfaceForward;
177     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD)] =
178         &NetsysNativeServiceStub::CmdIpfwdRemoveInterfaceForward;
179     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES)] =
180         &NetsysNativeServiceStub::CmdSetIptablesCommandForRes;
181     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_TETHER_DNS_SET)] =
182         &NetsysNativeServiceStub::CmdShareDnsSet;
183     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DNS_PROXY_LISTEN)] =
184         &NetsysNativeServiceStub::CmdStartDnsProxyListen;
185     opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DNS_PROXY_LISTEN)] =
186         &NetsysNativeServiceStub::CmdStopDnsProxyListen;
187 }
188 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)189 int32_t NetsysNativeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
190                                                  MessageOption &option)
191 {
192     NETNATIVE_LOG_D("Begin to call procedure with code %{public}u", code);
193     auto interfaceIndex = opToInterfaceMap_.find(code);
194     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
195         NETNATIVE_LOGE("Cannot response request %d: unknown tranction", code);
196         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
197     }
198     auto uid = IPCSkeleton::GetCallingUid();
199     if (std::find(uids_.begin(), uids_.end(), uid) == uids_.end()) {
200         NETNATIVE_LOGE("This uid connot use netsys");
201         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
202             return IPC_STUB_WRITE_PARCEL_ERR;
203         }
204         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
205     }
206 
207     const std::u16string descriptor = NetsysNativeServiceStub::GetDescriptor();
208     const std::u16string remoteDescriptor = data.ReadInterfaceToken();
209     if (descriptor != remoteDescriptor) {
210         NETNATIVE_LOGE("Check remote descriptor failed");
211         return IPC_STUB_INVALID_DATA_ERR;
212     }
213     return (this->*(interfaceIndex->second))(data, reply);
214 }
215 
CmdSetResolverConfig(MessageParcel & data,MessageParcel & reply)216 int32_t NetsysNativeServiceStub::CmdSetResolverConfig(MessageParcel &data, MessageParcel &reply)
217 {
218     uint16_t netId = 0;
219     uint16_t baseTimeoutMsec = 0;
220     uint8_t retryCount = 0;
221     std::vector<std::string> servers;
222     std::vector<std::string> domains;
223     if (!data.ReadUint16(netId)) {
224         return ERR_FLATTEN_OBJECT;
225     }
226     if (!data.ReadUint16(baseTimeoutMsec)) {
227         return ERR_FLATTEN_OBJECT;
228     }
229     if (!data.ReadUint8(retryCount)) {
230         return ERR_FLATTEN_OBJECT;
231     }
232     int32_t vServerSize;
233     if (!data.ReadInt32(vServerSize)) {
234         return ERR_FLATTEN_OBJECT;
235     }
236     vServerSize = (vServerSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vServerSize;
237     std::string s;
238     for (int32_t i = 0; i < vServerSize; ++i) {
239         std::string().swap(s);
240         if (!data.ReadString(s)) {
241             return ERR_FLATTEN_OBJECT;
242         }
243         servers.push_back(s);
244     }
245     int32_t vDomainSize;
246     if (!data.ReadInt32(vDomainSize)) {
247         return ERR_FLATTEN_OBJECT;
248     }
249     vDomainSize = (vDomainSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vDomainSize;
250     for (int32_t i = 0; i < vDomainSize; ++i) {
251         std::string().swap(s);
252         if (!data.ReadString(s)) {
253             return ERR_FLATTEN_OBJECT;
254         }
255         domains.push_back(s);
256     }
257 
258     int32_t result = SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
259     reply.WriteInt32(result);
260     NETNATIVE_LOG_D("SetResolverConfig has received result %{public}d", result);
261 
262     return ERR_NONE;
263 }
264 
CmdGetResolverConfig(MessageParcel & data,MessageParcel & reply)265 int32_t NetsysNativeServiceStub::CmdGetResolverConfig(MessageParcel &data, MessageParcel &reply)
266 {
267     uint16_t baseTimeoutMsec;
268     uint8_t retryCount;
269     uint16_t netId = 0;
270     std::vector<std::string> servers;
271     std::vector<std::string> domains;
272 
273     data.ReadUint16(netId);
274     int32_t result = GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
275     reply.WriteInt32(result);
276     reply.WriteUint16(baseTimeoutMsec);
277     reply.WriteUint8(retryCount);
278     auto vServerSize = static_cast<int32_t>(servers.size());
279     vServerSize = (vServerSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vServerSize;
280     reply.WriteInt32(vServerSize);
281     int32_t index = 0;
282     for (auto &server : servers) {
283         if (++index > MAX_DNS_CONFIG_SIZE) {
284             break;
285         }
286         reply.WriteString(server);
287     }
288     auto vDomainsSize = static_cast<int32_t>(domains.size());
289     vDomainsSize = (vDomainsSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vDomainsSize;
290     reply.WriteInt32(vDomainsSize);
291     std::vector<std::string>::iterator iterDomains;
292     index = 0;
293     for (iterDomains = domains.begin(); iterDomains != domains.end(); ++iterDomains) {
294         if (++index > MAX_DNS_CONFIG_SIZE) {
295             break;
296         }
297         reply.WriteString(*iterDomains);
298     }
299     NETNATIVE_LOG_D("GetResolverConfig has recved result %{public}d", result);
300     return ERR_NONE;
301 }
302 
CmdCreateNetworkCache(MessageParcel & data,MessageParcel & reply)303 int32_t NetsysNativeServiceStub::CmdCreateNetworkCache(MessageParcel &data, MessageParcel &reply)
304 {
305     uint16_t netid = data.ReadUint16();
306     NETNATIVE_LOGI("CreateNetworkCache  netid %{public}d", netid);
307     int32_t result = CreateNetworkCache(netid);
308     reply.WriteInt32(result);
309     NETNATIVE_LOG_D("CreateNetworkCache has recved result %{public}d", result);
310 
311     return ERR_NONE;
312 }
313 
CmdDestroyNetworkCache(MessageParcel & data,MessageParcel & reply)314 int32_t NetsysNativeServiceStub::CmdDestroyNetworkCache(MessageParcel &data, MessageParcel &reply)
315 {
316     uint16_t netId = data.ReadUint16();
317     int32_t result = DestroyNetworkCache(netId);
318     reply.WriteInt32(result);
319     NETNATIVE_LOG_D("DestroyNetworkCache has recved result %{public}d", result);
320 
321     return ERR_NONE;
322 }
323 
NetsysFreeAddrinfo(struct addrinfo * aihead)324 int32_t NetsysNativeServiceStub::NetsysFreeAddrinfo(struct addrinfo *aihead)
325 {
326     struct addrinfo *ai, *ainext;
327     for (ai = aihead; ai != nullptr; ai = ainext) {
328         if (ai->ai_addr != nullptr)
329             free(ai->ai_addr);
330         if (ai->ai_canonname != nullptr)
331             free(ai->ai_canonname);
332         ainext = ai->ai_next;
333         free(ai);
334     }
335     return ERR_NONE;
336 }
337 
CmdGetAddrInfo(MessageParcel & data,MessageParcel & reply)338 int32_t NetsysNativeServiceStub::CmdGetAddrInfo(MessageParcel &data, MessageParcel &reply)
339 {
340     std::string hostName;
341     std::string serverName;
342     AddrInfo hints = {};
343     uint16_t netId;
344     if (!data.ReadString(hostName)) {
345         return IPC_STUB_INVALID_DATA_ERR;
346     }
347 
348     if (!data.ReadString(serverName)) {
349         return IPC_STUB_INVALID_DATA_ERR;
350     }
351 
352     auto p = data.ReadRawData(sizeof(AddrInfo));
353     if (p == nullptr) {
354         return IPC_STUB_INVALID_DATA_ERR;
355     }
356     if (memcpy_s(&hints, sizeof(AddrInfo), p, sizeof(AddrInfo)) != EOK) {
357         return IPC_STUB_INVALID_DATA_ERR;
358     }
359 
360     if (!data.ReadUint16(netId)) {
361         return IPC_STUB_INVALID_DATA_ERR;
362     }
363 
364     std::vector<AddrInfo> retInfo;
365     auto ret = GetAddrInfo(hostName, serverName, hints, netId, retInfo);
366     if (retInfo.size() > MAX_RESULTS) {
367         return IPC_STUB_INVALID_DATA_ERR;
368     }
369 
370     if (!reply.WriteInt32(ret)) {
371         return IPC_STUB_WRITE_PARCEL_ERR;
372     }
373 
374     if (ret != ERR_NONE) {
375         return ERR_NONE;
376     }
377 
378     if (!reply.WriteUint32(static_cast<uint32_t>(retInfo.size()))) {
379         return IPC_STUB_WRITE_PARCEL_ERR;
380     }
381 
382     for (const auto &info : retInfo) {
383         if (!reply.WriteRawData(&info, sizeof(AddrInfo))) {
384             return IPC_STUB_WRITE_PARCEL_ERR;
385         }
386     }
387     return ERR_NONE;
388 }
389 
CmdSetInterfaceMtu(MessageParcel & data,MessageParcel & reply)390 int32_t NetsysNativeServiceStub::CmdSetInterfaceMtu(MessageParcel &data, MessageParcel &reply)
391 {
392     std::string ifName = data.ReadString();
393     int32_t mtu = data.ReadInt32();
394     int32_t result = SetInterfaceMtu(ifName, mtu);
395     reply.WriteInt32(result);
396     NETNATIVE_LOG_D("SetInterfaceMtu has recved result %{public}d", result);
397 
398     return ERR_NONE;
399 }
400 
CmdGetInterfaceMtu(MessageParcel & data,MessageParcel & reply)401 int32_t NetsysNativeServiceStub::CmdGetInterfaceMtu(MessageParcel &data, MessageParcel &reply)
402 {
403     std::string ifName = data.ReadString();
404     int32_t result = GetInterfaceMtu(ifName);
405     reply.WriteInt32(result);
406     NETNATIVE_LOG_D("GetInterfaceMtu has recved result %{public}d", result);
407 
408     return ERR_NONE;
409 }
410 
CmdRegisterNotifyCallback(MessageParcel & data,MessageParcel & reply)411 int32_t NetsysNativeServiceStub::CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)
412 {
413     NETNATIVE_LOG_D("Begin to dispatch cmd RegisterNotifyCallback");
414     sptr<IRemoteObject> remote = data.ReadRemoteObject();
415     if (remote == nullptr) {
416         NETNATIVE_LOGE("Callback ptr is nullptr.");
417         return -1;
418     }
419 
420     sptr<INotifyCallback> callback = iface_cast<INotifyCallback>(remote);
421     int32_t result = RegisterNotifyCallback(callback);
422     reply.WriteInt32(result);
423     return ERR_NONE;
424 }
425 
CmdUnRegisterNotifyCallback(MessageParcel & data,MessageParcel & reply)426 int32_t NetsysNativeServiceStub::CmdUnRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)
427 {
428     NETNATIVE_LOG_D("Begin to dispatch cmd UnRegisterNotifyCallback");
429     sptr<IRemoteObject> remote = data.ReadRemoteObject();
430     if (remote == nullptr) {
431         NETNATIVE_LOGE("Callback ptr is nullptr.");
432         return -1;
433     }
434 
435     sptr<INotifyCallback> callback = iface_cast<INotifyCallback>(remote);
436     int32_t result = UnRegisterNotifyCallback(callback);
437     reply.WriteInt32(result);
438     return ERR_NONE;
439 }
440 
CmdNetworkAddRoute(MessageParcel & data,MessageParcel & reply)441 int32_t NetsysNativeServiceStub::CmdNetworkAddRoute(MessageParcel &data, MessageParcel &reply)
442 {
443     int32_t netId = data.ReadInt32();
444     std::string ifName = data.ReadString();
445     std::string destination = data.ReadString();
446     std::string nextHop = data.ReadString();
447 
448     NETNATIVE_LOGI("netId[%{public}d}, ifName[%{public}s], destination[%{public}s}, nextHop[%{public}s]", netId,
449                    ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
450     int32_t result = NetworkAddRoute(netId, ifName, destination, nextHop);
451     reply.WriteInt32(result);
452     NETNATIVE_LOG_D("NetworkAddRoute has recved result %{public}d", result);
453     return result;
454 }
455 
CmdNetworkRemoveRoute(MessageParcel & data,MessageParcel & reply)456 int32_t NetsysNativeServiceStub::CmdNetworkRemoveRoute(MessageParcel &data, MessageParcel &reply)
457 {
458     int32_t netId = data.ReadInt32();
459     std::string interfaceName = data.ReadString();
460     std::string destination = data.ReadString();
461     std::string nextHop = data.ReadString();
462 
463     NETNATIVE_LOGI("netId[%{public}d}, ifName[%{public}s], destination[%{public}s}, nextHop[%{public}s]", netId,
464                    interfaceName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
465     int32_t result = NetworkRemoveRoute(netId, interfaceName, destination, nextHop);
466     reply.WriteInt32(result);
467     NETNATIVE_LOG_D("NetworkRemoveRoute has recved result %{public}d", result);
468 
469     return result;
470 }
471 
CmdNetworkAddRouteParcel(MessageParcel & data,MessageParcel & reply)472 int32_t NetsysNativeServiceStub::CmdNetworkAddRouteParcel(MessageParcel &data, MessageParcel &reply)
473 {
474     RouteInfoParcel routeInfo = {};
475     int32_t netId = data.ReadInt32();
476     routeInfo.ifName = data.ReadString();
477     routeInfo.destination = data.ReadString();
478     routeInfo.nextHop = data.ReadString();
479     int32_t result = NetworkAddRouteParcel(netId, routeInfo);
480     reply.WriteInt32(result);
481     NETNATIVE_LOG_D("NetworkAddRouteParcel has recved result %{public}d", result);
482 
483     return result;
484 }
485 
CmdNetworkRemoveRouteParcel(MessageParcel & data,MessageParcel & reply)486 int32_t NetsysNativeServiceStub::CmdNetworkRemoveRouteParcel(MessageParcel &data, MessageParcel &reply)
487 {
488     RouteInfoParcel routeInfo = {};
489     int32_t netId = data.ReadInt32();
490     routeInfo.ifName = data.ReadString();
491     routeInfo.destination = data.ReadString();
492     routeInfo.nextHop = data.ReadString();
493 
494     int32_t result = NetworkRemoveRouteParcel(netId, routeInfo);
495     reply.WriteInt32(result);
496     NETNATIVE_LOG_D("NetworkRemoveRouteParcel has recved result %{public}d", result);
497 
498     return result;
499 }
500 
CmdNetworkSetDefault(MessageParcel & data,MessageParcel & reply)501 int32_t NetsysNativeServiceStub::CmdNetworkSetDefault(MessageParcel &data, MessageParcel &reply)
502 {
503     int32_t netId = data.ReadInt32();
504 
505     int32_t result = NetworkSetDefault(netId);
506     reply.WriteInt32(result);
507     NETNATIVE_LOG_D("NetworkSetDefault has recved result %{public}d", result);
508 
509     return result;
510 }
511 
CmdNetworkGetDefault(MessageParcel & data,MessageParcel & reply)512 int32_t NetsysNativeServiceStub::CmdNetworkGetDefault(MessageParcel &data, MessageParcel &reply)
513 {
514     int32_t result = NetworkGetDefault();
515     reply.WriteInt32(result);
516     NETNATIVE_LOG_D("NetworkGetDefault has recved result %{public}d", result);
517 
518     return result;
519 }
520 
CmdNetworkClearDefault(MessageParcel & data,MessageParcel & reply)521 int32_t NetsysNativeServiceStub::CmdNetworkClearDefault(MessageParcel &data, MessageParcel &reply)
522 {
523     int32_t result = NetworkClearDefault();
524     reply.WriteInt32(result);
525     NETNATIVE_LOG_D("NetworkClearDefault has recved result %{public}d", result);
526 
527     return result;
528 }
529 
CmdGetProcSysNet(MessageParcel & data,MessageParcel & reply)530 int32_t NetsysNativeServiceStub::CmdGetProcSysNet(MessageParcel &data, MessageParcel &reply)
531 {
532     NETNATIVE_LOG_D("Begin to dispatch cmd GetProcSysNet");
533     int32_t family = data.ReadInt32();
534     int32_t which = data.ReadInt32();
535     std::string ifname = data.ReadString();
536     std::string parameter = data.ReadString();
537     std::string value;
538     int32_t result = GetProcSysNet(family, which, ifname, parameter, value);
539     reply.WriteInt32(result);
540     std::string valueRsl = value;
541     reply.WriteString(valueRsl);
542     return result;
543 }
544 
CmdSetProcSysNet(MessageParcel & data,MessageParcel & reply)545 int32_t NetsysNativeServiceStub::CmdSetProcSysNet(MessageParcel &data, MessageParcel &reply)
546 {
547     int32_t family = data.ReadInt32();
548     int32_t which = data.ReadInt32();
549     std::string ifname = data.ReadString();
550     std::string parameter = data.ReadString();
551     std::string value = data.ReadString();
552     int32_t result = SetProcSysNet(family, which, ifname, parameter, value);
553     reply.WriteInt32(result);
554     NETNATIVE_LOG_D("SetProcSysNet has recved result %{public}d", result);
555 
556     return result;
557 }
558 
CmdSetInternetPermission(MessageParcel & data,MessageParcel & reply)559 int32_t NetsysNativeServiceStub::CmdSetInternetPermission(MessageParcel &data, MessageParcel &reply)
560 {
561     uint32_t uid = data.ReadUint32();
562     uint8_t allow = data.ReadUint8();
563     int32_t result = SetInternetPermission(uid, allow);
564     reply.WriteInt32(result);
565     NETNATIVE_LOG_D("SetInternetPermission has recved result %{public}d", result);
566     return result;
567 }
568 
CmdNetworkCreatePhysical(MessageParcel & data,MessageParcel & reply)569 int32_t NetsysNativeServiceStub::CmdNetworkCreatePhysical(MessageParcel &data, MessageParcel &reply)
570 {
571     int32_t netId = data.ReadInt32();
572     int32_t permission = data.ReadInt32();
573 
574     int32_t result = NetworkCreatePhysical(netId, permission);
575     reply.WriteInt32(result);
576     NETNATIVE_LOG_D("NetworkCreatePhysical has recved result %{public}d", result);
577 
578     return result;
579 }
580 
CmdNetworkCreateVirtual(MessageParcel & data,MessageParcel & reply)581 int32_t NetsysNativeServiceStub::CmdNetworkCreateVirtual(MessageParcel &data, MessageParcel &reply)
582 {
583     int32_t netId = 0;
584     bool hasDns = false;
585     if (!data.ReadInt32(netId) || !data.ReadBool(hasDns)) {
586         NETNATIVE_LOGE("read net id or hasDns failed");
587         return IPC_STUB_ERR;
588     }
589 
590     int32_t result = NetworkCreateVirtual(netId, hasDns);
591     if (!reply.WriteInt32(result)) {
592         return IPC_STUB_WRITE_PARCEL_ERR;
593     }
594     NETNATIVE_LOG_D("NetworkCreateVirtual has recved result %{public}d", result);
595     return ERR_NONE;
596 }
597 
CmdNetworkAddUids(MessageParcel & data,MessageParcel & reply)598 int32_t NetsysNativeServiceStub::CmdNetworkAddUids(MessageParcel &data, MessageParcel &reply)
599 {
600     int32_t netId = 0;
601     int32_t size = 0;
602     if (!data.ReadInt32(netId) || !data.ReadInt32(size)) {
603         NETNATIVE_LOGE("read net id or size failed");
604         return IPC_STUB_ERR;
605     }
606     size = (size > static_cast<int32_t>(MAX_UID_ARRAY_SIZE)) ? static_cast<int32_t>(MAX_UID_ARRAY_SIZE) : size;
607 
608     sptr<UidRange> uid;
609     std::vector<UidRange> uidRanges;
610     for (int32_t index = 0; index < size; index++) {
611         uid = UidRange::Unmarshalling(data);
612         if (uid == nullptr) {
613             NETNATIVE_LOGE("UidRange::Unmarshalling(parcel) is null");
614             return IPC_STUB_ERR;
615         }
616         uidRanges.push_back(*uid);
617     }
618     int32_t result = NetworkAddUids(netId, uidRanges);
619     if (!reply.WriteInt32(result)) {
620         return IPC_STUB_WRITE_PARCEL_ERR;
621     }
622     NETNATIVE_LOG_D("NetworkAddUids has recved result %{public}d", result);
623     return ERR_NONE;
624 }
625 
CmdNetworkDelUids(MessageParcel & data,MessageParcel & reply)626 int32_t NetsysNativeServiceStub::CmdNetworkDelUids(MessageParcel &data, MessageParcel &reply)
627 {
628     int32_t netId = 0;
629     int32_t size = 0;
630     if (!data.ReadInt32(netId) || !data.ReadInt32(size)) {
631         NETNATIVE_LOGE("read net id or size failed");
632         return IPC_STUB_ERR;
633     }
634 
635     size = (size > static_cast<int32_t>(MAX_UID_ARRAY_SIZE)) ? static_cast<int32_t>(MAX_UID_ARRAY_SIZE) : size;
636 
637     sptr<UidRange> uid;
638     std::vector<UidRange> uidRanges;
639     for (int32_t index = 0; index < size; index++) {
640         uid = UidRange::Unmarshalling(data);
641         if (uid == nullptr) {
642             NETNATIVE_LOGE("UidRange::Unmarshalling(parcel) is null");
643             return IPC_STUB_ERR;
644         }
645         uidRanges.push_back(*uid);
646     }
647     int32_t result = NetworkDelUids(netId, uidRanges);
648     if (!reply.WriteInt32(result)) {
649         return IPC_STUB_WRITE_PARCEL_ERR;
650     }
651     NETNATIVE_LOG_D("NetworkDelUids has recved result %{public}d", result);
652     return ERR_NONE;
653 }
654 
CmdAddInterfaceAddress(MessageParcel & data,MessageParcel & reply)655 int32_t NetsysNativeServiceStub::CmdAddInterfaceAddress(MessageParcel &data, MessageParcel &reply)
656 {
657     std::string interfaceName = data.ReadString();
658     std::string ipAddr = data.ReadString();
659     int32_t prefixLength = data.ReadInt32();
660 
661     int32_t result = AddInterfaceAddress(interfaceName, ipAddr, prefixLength);
662     reply.WriteInt32(result);
663     NETNATIVE_LOG_D("AddInterfaceAddress has recved result %{public}d", result);
664 
665     return result;
666 }
667 
CmdDelInterfaceAddress(MessageParcel & data,MessageParcel & reply)668 int32_t NetsysNativeServiceStub::CmdDelInterfaceAddress(MessageParcel &data, MessageParcel &reply)
669 {
670     std::string interfaceName = data.ReadString();
671     std::string ipAddr = data.ReadString();
672     int32_t prefixLength = data.ReadInt32();
673 
674     int32_t result = DelInterfaceAddress(interfaceName, ipAddr, prefixLength);
675     reply.WriteInt32(result);
676     NETNATIVE_LOG_D("DelInterfaceAddress has recved result %{public}d", result);
677 
678     return result;
679 }
680 
CmdInterfaceSetIpAddress(MessageParcel & data,MessageParcel & reply)681 int32_t NetsysNativeServiceStub::CmdInterfaceSetIpAddress(MessageParcel &data, MessageParcel &reply)
682 {
683     std::string ifaceName = data.ReadString();
684     std::string ipAddress = data.ReadString();
685 
686     int32_t result = InterfaceSetIpAddress(ifaceName, ipAddress);
687     reply.WriteInt32(result);
688     NETNATIVE_LOG_D("InterfaceSetIpAddress has recved result %{public}d", result);
689 
690     return result;
691 }
692 
CmdInterfaceSetIffUp(MessageParcel & data,MessageParcel & reply)693 int32_t NetsysNativeServiceStub::CmdInterfaceSetIffUp(MessageParcel &data, MessageParcel &reply)
694 {
695     std::string ifaceName = data.ReadString();
696 
697     int32_t result = InterfaceSetIffUp(ifaceName);
698     reply.WriteInt32(result);
699     NETNATIVE_LOG_D("InterfaceSetIffUp has recved result %{public}d", result);
700 
701     return result;
702 }
703 
CmdNetworkAddInterface(MessageParcel & data,MessageParcel & reply)704 int32_t NetsysNativeServiceStub::CmdNetworkAddInterface(MessageParcel &data, MessageParcel &reply)
705 {
706     int32_t netId = data.ReadInt32();
707     std::string iface = data.ReadString();
708 
709     int32_t result = NetworkAddInterface(netId, iface);
710     reply.WriteInt32(result);
711     NETNATIVE_LOG_D("NetworkAddInterface has recved result %{public}d", result);
712 
713     return result;
714 }
715 
CmdNetworkRemoveInterface(MessageParcel & data,MessageParcel & reply)716 int32_t NetsysNativeServiceStub::CmdNetworkRemoveInterface(MessageParcel &data, MessageParcel &reply)
717 {
718     int32_t netId = data.ReadInt32();
719     std::string iface = data.ReadString();
720     int32_t result = NetworkRemoveInterface(netId, iface);
721     reply.WriteInt32(result);
722     NETNATIVE_LOG_D("NetworkRemoveInterface has recved result %{public}d", result);
723 
724     return result;
725 }
726 
CmdNetworkDestroy(MessageParcel & data,MessageParcel & reply)727 int32_t NetsysNativeServiceStub::CmdNetworkDestroy(MessageParcel &data, MessageParcel &reply)
728 {
729     int32_t netId = data.ReadInt32();
730     int32_t result = NetworkDestroy(netId);
731     reply.WriteInt32(result);
732     NETNATIVE_LOG_D("NetworkDestroy has recved result %{public}d", result);
733 
734     return result;
735 }
736 
CmdGetFwmarkForNetwork(MessageParcel & data,MessageParcel & reply)737 int32_t NetsysNativeServiceStub::CmdGetFwmarkForNetwork(MessageParcel &data, MessageParcel &reply)
738 {
739     MarkMaskParcel markMaskParcel = {};
740     int32_t netId = data.ReadInt32();
741     markMaskParcel.mark = data.ReadInt32();
742     markMaskParcel.mask = data.ReadInt32();
743     int32_t result = GetFwmarkForNetwork(netId, markMaskParcel);
744     reply.WriteInt32(result);
745     NETNATIVE_LOG_D("GetFwmarkForNetwork has recved result %{public}d", result);
746 
747     return result;
748 }
749 
CmdSetInterfaceConfig(MessageParcel & data,MessageParcel & reply)750 int32_t NetsysNativeServiceStub::CmdSetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
751 {
752     InterfaceConfigurationParcel cfg = {};
753     cfg.ifName = data.ReadString();
754     cfg.hwAddr = data.ReadString();
755     cfg.ipv4Addr = data.ReadString();
756     cfg.prefixLength = data.ReadInt32();
757     int32_t vSize = data.ReadInt32();
758     vSize = (vSize > MAX_FLAG_NUM) ? MAX_FLAG_NUM : vSize;
759     std::vector<std::string> vFlags;
760     for (int i = 0; i < vSize; i++) {
761         vFlags.emplace_back(data.ReadString());
762     }
763     cfg.flags.assign(vFlags.begin(), vFlags.end());
764     int32_t result = SetInterfaceConfig(cfg);
765     reply.WriteInt32(result);
766     NETNATIVE_LOG_D("SetInterfaceConfig has recved result %{public}d", result);
767 
768     return result;
769 }
770 
CmdGetInterfaceConfig(MessageParcel & data,MessageParcel & reply)771 int32_t NetsysNativeServiceStub::CmdGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
772 {
773     NETNATIVE_LOG_D("Begin to dispatch cmd GetInterfaceConfig");
774     InterfaceConfigurationParcel cfg = {};
775     cfg.ifName = data.ReadString();
776     int32_t result = GetInterfaceConfig(cfg);
777     reply.WriteInt32(result);
778     reply.WriteString(cfg.ifName);
779     reply.WriteString(cfg.hwAddr);
780     reply.WriteString(cfg.ipv4Addr);
781     reply.WriteInt32(cfg.prefixLength);
782     int32_t vsize = static_cast<int32_t>(cfg.flags.size());
783     vsize = vsize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vsize;
784     reply.WriteInt32(vsize);
785     std::vector<std::string>::iterator iter;
786     int32_t index = 0;
787     for (iter = cfg.flags.begin(); iter != cfg.flags.end(); ++iter) {
788         if (++index > MAX_DNS_CONFIG_SIZE) {
789             break;
790         }
791         reply.WriteString(*iter);
792     }
793     return result;
794 }
795 
CmdInterfaceGetList(MessageParcel & data,MessageParcel & reply)796 int32_t NetsysNativeServiceStub::CmdInterfaceGetList(MessageParcel &data, MessageParcel &reply)
797 {
798     NETNATIVE_LOG_D("Begin to dispatch cmd InterfaceGetList");
799     std::vector<std::string> ifaces;
800     int32_t result = InterfaceGetList(ifaces);
801     reply.WriteInt32(result);
802     auto vsize = static_cast<int32_t>(ifaces.size());
803     reply.WriteInt32(vsize);
804     std::vector<std::string>::iterator iter;
805     for (iter = ifaces.begin(); iter != ifaces.end(); ++iter) {
806         reply.WriteString(*iter);
807     }
808     return result;
809 }
810 
CmdStartDhcpClient(MessageParcel & data,MessageParcel & reply)811 int32_t NetsysNativeServiceStub::CmdStartDhcpClient(MessageParcel &data, MessageParcel &reply)
812 {
813     NETNATIVE_LOG_D("Begin to dispatch cmd CmdStartDhcpClient");
814     std::string iface = data.ReadString();
815     bool bIpv6 = data.ReadBool();
816     int32_t result = StartDhcpClient(iface, bIpv6);
817     reply.WriteInt32(result);
818     return result;
819 }
820 
CmdStopDhcpClient(MessageParcel & data,MessageParcel & reply)821 int32_t NetsysNativeServiceStub::CmdStopDhcpClient(MessageParcel &data, MessageParcel &reply)
822 {
823     NETNATIVE_LOG_D("Begin to dispatch cmd CmdStopDhcpClient");
824     std::string iface = data.ReadString();
825     bool bIpv6 = data.ReadBool();
826     int32_t result = StopDhcpClient(iface, bIpv6);
827     reply.WriteInt32(result);
828     return result;
829 }
830 
CmdStartDhcpService(MessageParcel & data,MessageParcel & reply)831 int32_t NetsysNativeServiceStub::CmdStartDhcpService(MessageParcel &data, MessageParcel &reply)
832 {
833     NETNATIVE_LOG_D("Begin to dispatch cmd CmdStartDhcpService");
834     std::string iface = data.ReadString();
835     std::string ipv4addr = data.ReadString();
836     int32_t result = StartDhcpService(iface, ipv4addr);
837     reply.WriteInt32(result);
838     return result;
839 }
840 
CmdStopDhcpService(MessageParcel & data,MessageParcel & reply)841 int32_t NetsysNativeServiceStub::CmdStopDhcpService(MessageParcel &data, MessageParcel &reply)
842 {
843     NETNATIVE_LOG_D("Begin to dispatch cmd CmdStopDhcpService");
844     std::string iface = data.ReadString();
845     int32_t result = StopDhcpService(iface);
846     reply.WriteInt32(result);
847     return result;
848 }
849 
CmdIpEnableForwarding(MessageParcel & data,MessageParcel & reply)850 int32_t NetsysNativeServiceStub::CmdIpEnableForwarding(MessageParcel &data, MessageParcel &reply)
851 {
852     NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpEnableForwarding");
853     const auto &requester = data.ReadString();
854     int32_t result = IpEnableForwarding(requester);
855     reply.WriteInt32(result);
856     return result;
857 }
858 
CmdIpDisableForwarding(MessageParcel & data,MessageParcel & reply)859 int32_t NetsysNativeServiceStub::CmdIpDisableForwarding(MessageParcel &data, MessageParcel &reply)
860 {
861     NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpDisableForwarding");
862     const auto &requester = data.ReadString();
863     int32_t result = IpDisableForwarding(requester);
864     reply.WriteInt32(result);
865     return result;
866 }
867 
CmdEnableNat(MessageParcel & data,MessageParcel & reply)868 int32_t NetsysNativeServiceStub::CmdEnableNat(MessageParcel &data, MessageParcel &reply)
869 {
870     NETNATIVE_LOG_D("Begin to dispatch cmd CmdEnableNat");
871     const auto &downstreamIface = data.ReadString();
872     const auto &upstreamIface = data.ReadString();
873     int32_t result = EnableNat(downstreamIface, upstreamIface);
874     reply.WriteInt32(result);
875     return result;
876 }
877 
CmdDisableNat(MessageParcel & data,MessageParcel & reply)878 int32_t NetsysNativeServiceStub::CmdDisableNat(MessageParcel &data, MessageParcel &reply)
879 {
880     NETNATIVE_LOG_D("Begin to dispatch cmd CmdDisableNat");
881     const auto &downstreamIface = data.ReadString();
882     const auto &upstreamIface = data.ReadString();
883     int32_t result = DisableNat(downstreamIface, upstreamIface);
884     reply.WriteInt32(result);
885     return result;
886 }
887 
CmdIpfwdAddInterfaceForward(MessageParcel & data,MessageParcel & reply)888 int32_t NetsysNativeServiceStub::CmdIpfwdAddInterfaceForward(MessageParcel &data, MessageParcel &reply)
889 {
890     NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpfwdAddInterfaceForward");
891     const auto &fromIface = data.ReadString();
892     const auto &toIface = data.ReadString();
893     int32_t result = IpfwdAddInterfaceForward(fromIface, toIface);
894     reply.WriteInt32(result);
895     return result;
896 }
897 
CmdIpfwdRemoveInterfaceForward(MessageParcel & data,MessageParcel & reply)898 int32_t NetsysNativeServiceStub::CmdIpfwdRemoveInterfaceForward(MessageParcel &data, MessageParcel &reply)
899 {
900     NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpfwdRemoveInterfaceForward");
901     const auto &fromIface = data.ReadString();
902     const auto &toIface = data.ReadString();
903     int32_t result = IpfwdRemoveInterfaceForward(fromIface, toIface);
904     reply.WriteInt32(result);
905     return result;
906 }
907 
CmdBandwidthEnableDataSaver(MessageParcel & data,MessageParcel & reply)908 int32_t NetsysNativeServiceStub::CmdBandwidthEnableDataSaver(MessageParcel &data, MessageParcel &reply)
909 {
910     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthEnableDataSaver");
911     bool enable = data.ReadBool();
912     int32_t result = BandwidthEnableDataSaver(enable);
913     reply.WriteInt32(result);
914     return result;
915 }
916 
CmdBandwidthSetIfaceQuota(MessageParcel & data,MessageParcel & reply)917 int32_t NetsysNativeServiceStub::CmdBandwidthSetIfaceQuota(MessageParcel &data, MessageParcel &reply)
918 {
919     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthSetIfaceQuota");
920     std::string ifName = data.ReadString();
921     int64_t bytes = data.ReadInt64();
922     int32_t result = BandwidthSetIfaceQuota(ifName, bytes);
923     reply.WriteInt32(result);
924     return result;
925 }
926 
CmdBandwidthRemoveIfaceQuota(MessageParcel & data,MessageParcel & reply)927 int32_t NetsysNativeServiceStub::CmdBandwidthRemoveIfaceQuota(MessageParcel &data, MessageParcel &reply)
928 {
929     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthRemoveIfaceQuota");
930     std::string ifName = data.ReadString();
931     int32_t result = BandwidthRemoveIfaceQuota(ifName);
932     reply.WriteInt32(result);
933     return result;
934 }
935 
CmdBandwidthAddDeniedList(MessageParcel & data,MessageParcel & reply)936 int32_t NetsysNativeServiceStub::CmdBandwidthAddDeniedList(MessageParcel &data, MessageParcel &reply)
937 {
938     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthAddDeniedList");
939     uint32_t uid = data.ReadUint32();
940     int32_t result = BandwidthAddDeniedList(uid);
941     reply.WriteInt32(result);
942     return result;
943 }
CmdBandwidthRemoveDeniedList(MessageParcel & data,MessageParcel & reply)944 int32_t NetsysNativeServiceStub::CmdBandwidthRemoveDeniedList(MessageParcel &data, MessageParcel &reply)
945 {
946     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthRemoveDeniedList");
947     uint32_t uid = data.ReadUint32();
948     int32_t result = BandwidthRemoveDeniedList(uid);
949     reply.WriteInt32(result);
950     return result;
951 }
952 
CmdBandwidthAddAllowedList(MessageParcel & data,MessageParcel & reply)953 int32_t NetsysNativeServiceStub::CmdBandwidthAddAllowedList(MessageParcel &data, MessageParcel &reply)
954 {
955     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthAddAllowedList");
956     uint32_t uid = data.ReadUint32();
957     int32_t result = BandwidthAddAllowedList(uid);
958     reply.WriteInt32(result);
959     return result;
960 }
961 
CmdBandwidthRemoveAllowedList(MessageParcel & data,MessageParcel & reply)962 int32_t NetsysNativeServiceStub::CmdBandwidthRemoveAllowedList(MessageParcel &data, MessageParcel &reply)
963 {
964     NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthRemoveAllowedList");
965     uint32_t uid = data.ReadUint32();
966     int32_t result = BandwidthRemoveAllowedList(uid);
967     reply.WriteInt32(result);
968     return result;
969 }
970 
CmdFirewallSetUidsAllowedListChain(MessageParcel & data,MessageParcel & reply)971 int32_t NetsysNativeServiceStub::CmdFirewallSetUidsAllowedListChain(MessageParcel &data, MessageParcel &reply)
972 {
973     NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallSetUidsAllowedListChain");
974     uint32_t chain = data.ReadUint32();
975     std::vector<uint32_t> uids;
976     uint32_t uidSize = data.ReadUint32();
977     uidSize = (uidSize > UIDS_LIST_MAX_SIZE) ? UIDS_LIST_MAX_SIZE : uidSize;
978     for (uint32_t i = 0; i < uidSize; i++) {
979         uint32_t uid = data.ReadUint32();
980         uids.push_back(uid);
981     }
982     int32_t result = FirewallSetUidsAllowedListChain(chain, uids);
983     reply.WriteInt32(result);
984     return result;
985 }
986 
CmdFirewallSetUidsDeniedListChain(MessageParcel & data,MessageParcel & reply)987 int32_t NetsysNativeServiceStub::CmdFirewallSetUidsDeniedListChain(MessageParcel &data, MessageParcel &reply)
988 {
989     NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallSetUidsDeniedListChain");
990     uint32_t chain = data.ReadUint32();
991     std::vector<uint32_t> uids;
992     uint32_t uidSize = data.ReadUint32();
993     uidSize = (uidSize > UIDS_LIST_MAX_SIZE) ? UIDS_LIST_MAX_SIZE : uidSize;
994     for (uint32_t i = 0; i < uidSize; i++) {
995         uint32_t uid = data.ReadUint32();
996         uids.push_back(uid);
997     }
998     int32_t result = FirewallSetUidsDeniedListChain(chain, uids);
999     reply.WriteInt32(result);
1000     return result;
1001 }
1002 
CmdFirewallEnableChain(MessageParcel & data,MessageParcel & reply)1003 int32_t NetsysNativeServiceStub::CmdFirewallEnableChain(MessageParcel &data, MessageParcel &reply)
1004 {
1005     NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallEnableChain");
1006     uint32_t chain = data.ReadUint32();
1007     bool enable = data.ReadBool();
1008     int32_t result = FirewallEnableChain(chain, enable);
1009     reply.WriteInt32(result);
1010     return result;
1011 }
1012 
CmdFirewallSetUidRule(MessageParcel & data,MessageParcel & reply)1013 int32_t NetsysNativeServiceStub::CmdFirewallSetUidRule(MessageParcel &data, MessageParcel &reply)
1014 {
1015     NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallSetUidRule");
1016     uint32_t chain = (unsigned)data.ReadUint32();
1017     std::vector<uint32_t> uids;
1018     data.ReadUInt32Vector(&uids);
1019     uint32_t firewallRule = (unsigned)data.ReadInt32();
1020     int32_t result = FirewallSetUidRule(chain, uids, firewallRule);
1021     reply.WriteInt32(result);
1022     return result;
1023 }
1024 
CmdShareDnsSet(MessageParcel & data,MessageParcel & reply)1025 int32_t NetsysNativeServiceStub::CmdShareDnsSet(MessageParcel &data, MessageParcel &reply)
1026 {
1027     uint16_t netId = 0;
1028     data.ReadUint16(netId);
1029     int32_t result = ShareDnsSet(netId);
1030     reply.WriteInt32(result);
1031     NETNATIVE_LOG_D("ShareDnsSet has received result %{public}d", result);
1032 
1033     return result;
1034 }
1035 
CmdStartDnsProxyListen(MessageParcel & data,MessageParcel & reply)1036 int32_t NetsysNativeServiceStub::CmdStartDnsProxyListen(MessageParcel &data, MessageParcel &reply)
1037 {
1038     int32_t result = StartDnsProxyListen();
1039     reply.WriteInt32(result);
1040     NETNATIVE_LOG_D("StartDnsProxyListen has recved result %{public}d", result);
1041 
1042     return result;
1043 }
1044 
CmdStopDnsProxyListen(MessageParcel & data,MessageParcel & reply)1045 int32_t NetsysNativeServiceStub::CmdStopDnsProxyListen(MessageParcel &data, MessageParcel &reply)
1046 {
1047     int32_t result = StopDnsProxyListen();
1048     reply.WriteInt32(result);
1049     NETNATIVE_LOG_D("StopDnsProxyListen has recved result %{public}d", result);
1050 
1051     return result;
1052 }
1053 
CmdGetNetworkSharingTraffic(MessageParcel & data,MessageParcel & reply)1054 int32_t NetsysNativeServiceStub::CmdGetNetworkSharingTraffic(MessageParcel &data, MessageParcel &reply)
1055 {
1056     NETNATIVE_LOG_D("Begin to dispatch cmd GetNetworkSharingTraffic");
1057     std::string downIface = data.ReadString();
1058     std::string upIface = data.ReadString();
1059     NetworkSharingTraffic traffic;
1060     int32_t result = GetNetworkSharingTraffic(downIface, upIface, traffic);
1061     reply.WriteInt32(result);
1062     reply.WriteInt64(traffic.receive);
1063     reply.WriteInt64(traffic.send);
1064     reply.WriteInt64(traffic.all);
1065 
1066     return result;
1067 }
1068 
CmdGetTotalStats(MessageParcel & data,MessageParcel & reply)1069 int32_t NetsysNativeServiceStub::CmdGetTotalStats(MessageParcel &data, MessageParcel &reply)
1070 {
1071     uint32_t type = data.ReadUint32();
1072     uint64_t stats = 0;
1073     int32_t result = GetTotalStats(stats, type);
1074     if (!reply.WriteInt32(result)) {
1075         NETNATIVE_LOGE("Write parcel failed");
1076         return ERR_FLATTEN_OBJECT;
1077     }
1078     if (!reply.WriteUint64(stats)) {
1079         NETNATIVE_LOGE("Write parcel failed");
1080         return ERR_FLATTEN_OBJECT;
1081     }
1082     return result;
1083 }
1084 
CmdGetUidStats(MessageParcel & data,MessageParcel & reply)1085 int32_t NetsysNativeServiceStub::CmdGetUidStats(MessageParcel &data, MessageParcel &reply)
1086 {
1087     uint32_t type = data.ReadUint32();
1088     uint32_t uId = data.ReadUint32();
1089     uint64_t stats = 0;
1090     int32_t result = GetUidStats(stats, type, uId);
1091     if (!reply.WriteInt32(result)) {
1092         NETNATIVE_LOGE("Write parcel failed");
1093         return ERR_FLATTEN_OBJECT;
1094     }
1095     if (!reply.WriteUint64(stats)) {
1096         NETNATIVE_LOGE("Write parcel failed");
1097         return ERR_FLATTEN_OBJECT;
1098     }
1099     return result;
1100 }
1101 
CmdGetIfaceStats(MessageParcel & data,MessageParcel & reply)1102 int32_t NetsysNativeServiceStub::CmdGetIfaceStats(MessageParcel &data, MessageParcel &reply)
1103 {
1104     uint32_t type = data.ReadUint32();
1105     std::string interfaceName = data.ReadString();
1106     uint64_t stats = 0;
1107     int32_t result = GetIfaceStats(stats, type, interfaceName);
1108     if (!reply.WriteInt32(result)) {
1109         NETNATIVE_LOGE("Write parcel failed");
1110         return ERR_FLATTEN_OBJECT;
1111     }
1112     if (!reply.WriteUint64(stats)) {
1113         NETNATIVE_LOGE("Write parcel failed");
1114         return ERR_FLATTEN_OBJECT;
1115     }
1116     return result;
1117 }
1118 
CmdGetAllStatsInfo(MessageParcel & data,MessageParcel & reply)1119 int32_t NetsysNativeServiceStub::CmdGetAllStatsInfo(MessageParcel &data, MessageParcel &reply)
1120 {
1121     std::vector<OHOS::NetManagerStandard::NetStatsInfo> stats;
1122     int32_t result = GetAllStatsInfo(stats);
1123     if (!reply.WriteInt32(result)) {
1124         NETNATIVE_LOGE("Write parcel failed");
1125         return ERR_FLATTEN_OBJECT;
1126     }
1127     if (!OHOS::NetManagerStandard::NetStatsInfo::Marshalling(reply, stats)) {
1128         NETNATIVE_LOGE("Read stats info failed");
1129         return ERR_FLATTEN_OBJECT;
1130     }
1131     return result;
1132 }
1133 
CmdSetIptablesCommandForRes(MessageParcel & data,MessageParcel & reply)1134 int32_t NetsysNativeServiceStub::CmdSetIptablesCommandForRes(MessageParcel &data, MessageParcel &reply)
1135 {
1136     if (!NetManagerStandard::NetManagerPermission::CheckNetSysInternalPermission(
1137         NetManagerStandard::Permission::NETSYS_INTERNAL)) {
1138         NETNATIVE_LOGE("CmdSetIptablesCommandForRes CheckNetSysInternalPermission failed");
1139         return NETMANAGER_ERR_PERMISSION_DENIED;
1140     }
1141     std::string cmd = data.ReadString();
1142     std::string respond;
1143     int32_t result = SetIptablesCommandForRes(cmd, respond);
1144     if (!reply.WriteInt32(result)) {
1145         NETNATIVE_LOGE("Write CmdSetIptablesCommandForRes result failed");
1146         return ERR_FLATTEN_OBJECT;
1147     }
1148     if (!reply.WriteString(respond)) {
1149         NETNATIVE_LOGE("Write CmdSetIptablesCommandForRes respond failed");
1150         return ERR_FLATTEN_OBJECT;
1151     }
1152     return result;
1153 }
1154 
1155 } // namespace NetsysNative
1156 } // namespace OHOS
1157