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 "netsys_native_service_proxy.h"
17 
18 #include <securec.h>
19 
20 #include "net_manager_constants.h"
21 #include "net_stats_constants.h"
22 #include "netnative_log_wrapper.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
26 bool StatsInfoUnmarshallingVector(Parcel &parcel, std::vector<NetStatsInfo> &stats);
27 } // namespace NetManagerStandard
28 
29 namespace NetsysNative {
30 static constexpr uint32_t UIDS_LIST_MAX_SIZE = 1024;
31 static constexpr int32_t MAX_DNS_CONFIG_SIZE = 4;
32 static constexpr int32_t MAX_INTERFACE_CONFIG_SIZE = 16;
33 
34 namespace {
WriteNatDataToMessage(MessageParcel & data,const std::string & downstreamIface,const std::string & upstreamIface)35 bool WriteNatDataToMessage(MessageParcel &data, const std::string &downstreamIface, const std::string &upstreamIface)
36 {
37     if (!data.WriteInterfaceToken(NetsysNativeServiceProxy::GetDescriptor())) {
38         NETNATIVE_LOGI("WriteInterfaceToken failed");
39         return false;
40     }
41     if (!data.WriteString(downstreamIface)) {
42         return false;
43     }
44     if (!data.WriteString(upstreamIface)) {
45         return false;
46     }
47     return true;
48 }
49 } // namespace
50 
WriteInterfaceToken(MessageParcel & data)51 bool NetsysNativeServiceProxy::WriteInterfaceToken(MessageParcel &data)
52 {
53     if (!data.WriteInterfaceToken(NetsysNativeServiceProxy::GetDescriptor())) {
54         NETNATIVE_LOGI("WriteInterfaceToken failed");
55         return false;
56     }
57     return true;
58 }
59 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)60 int32_t NetsysNativeServiceProxy::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
61                                                     const std::vector<std::string> &servers,
62                                                     const std::vector<std::string> &domains)
63 {
64     NETNATIVE_LOGI("Begin to SetResolverConfig %{public}d", retryCount);
65     MessageParcel data;
66     if (!WriteInterfaceToken(data)) {
67         return ERR_FLATTEN_OBJECT;
68     }
69     if (!data.WriteUint16(netId)) {
70         return ERR_FLATTEN_OBJECT;
71     }
72     if (!data.WriteUint16(baseTimeoutMsec)) {
73         return ERR_FLATTEN_OBJECT;
74     }
75     if (!data.WriteUint8(retryCount)) {
76         return ERR_FLATTEN_OBJECT;
77     }
78 
79     auto vServerSize1 = static_cast<int32_t>(servers.size());
80     if (!data.WriteInt32(vServerSize1)) {
81         return ERR_FLATTEN_OBJECT;
82     }
83     std::vector<std::string> vServers;
84     vServers.assign(servers.begin(), servers.end());
85     NETNATIVE_LOGI("PROXY: SetResolverConfig Write Servers  String_SIZE: %{public}d",
86                    static_cast<int32_t>(vServers.size()));
87     for (auto &vServer : vServers) {
88         data.WriteString(vServer);
89     }
90 
91     int vDomainSize1 = static_cast<int>(domains.size());
92     if (!data.WriteInt32(vDomainSize1)) {
93         return ERR_FLATTEN_OBJECT;
94     }
95 
96     std::vector<std::string> vDomains;
97     vDomains.assign(domains.begin(), domains.end());
98     NETNATIVE_LOGI("PROXY: SetResolverConfig Write Domains String_SIZE: %{public}d",
99                    static_cast<int32_t>(vDomains.size()));
100     for (auto &vDomain : vDomains) {
101         data.WriteString(vDomain);
102     }
103 
104     MessageParcel reply;
105     MessageOption option;
106     int ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_RESOLVER_CONFIG),
107         data, reply, option);
108     if (ret != ERR_NONE) {
109         NETNATIVE_LOGE("SendRequest failed, ret code: [%{public}d]", ret);
110         return IPC_INVOKER_ERR;
111     }
112     return reply.ReadInt32();
113 }
114 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)115 int32_t NetsysNativeServiceProxy::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
116                                                     std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
117                                                     uint8_t &retryCount)
118 {
119     MessageParcel data;
120     if (!WriteInterfaceToken(data)) {
121         return ERR_FLATTEN_OBJECT;
122     }
123     if (!data.WriteUint16(netId)) {
124         return ERR_FLATTEN_OBJECT;
125     }
126     MessageParcel reply;
127     MessageOption option;
128     int ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_RESOLVER_CONFIG), data,
129         reply, option);
130     if (ret != ERR_NONE) {
131         NETNATIVE_LOGE("SendRequest failed, ret code: [%{public}d]", ret);
132         return IPC_INVOKER_ERR;
133     }
134     int result = reply.ReadInt32();
135     if (result != ERR_NONE) {
136         NETNATIVE_LOGE("Fail to GetResolverConfig ret= %{public}d", result);
137         return result;
138     }
139 
140     reply.ReadUint16(baseTimeoutMsec);
141     reply.ReadUint8(retryCount);
142     int32_t vServerSize = reply.ReadInt32();
143     vServerSize = vServerSize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vServerSize;
144     std::vector<std::string> vecString;
145     for (int i = 0; i < vServerSize; i++) {
146         vecString.push_back(reply.ReadString());
147     }
148     if (vServerSize > 0) {
149         servers.assign(vecString.begin(), vecString.end());
150     }
151     int32_t vDomainSize = reply.ReadInt32();
152     vDomainSize = vDomainSize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vDomainSize;
153     std::vector<std::string> vecDomain;
154     for (int i = 0; i < vDomainSize; i++) {
155         vecDomain.push_back(reply.ReadString());
156     }
157     if (vDomainSize > 0) {
158         domains.assign(vecDomain.begin(), vecDomain.end());
159     }
160     NETNATIVE_LOGI("Begin to GetResolverConfig %{public}d", result);
161     return result;
162 }
163 
CreateNetworkCache(uint16_t netId)164 int32_t NetsysNativeServiceProxy::CreateNetworkCache(uint16_t netId)
165 {
166     NETNATIVE_LOGI("Begin to CreateNetworkCache");
167     MessageParcel data;
168     if (!WriteInterfaceToken(data)) {
169         return ERR_FLATTEN_OBJECT;
170     }
171     if (!data.WriteUint16(netId)) {
172         return ERR_FLATTEN_OBJECT;
173     }
174 
175     MessageParcel reply;
176     MessageOption option;
177     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CREATE_NETWORK_CACHE), data,
178         reply, option);
179     if (result != ERR_NONE) {
180         NETNATIVE_LOGE("SendRequest failed, result code: [%{public}d]", result);
181         return IPC_INVOKER_ERR;
182     }
183     return reply.ReadInt32();
184 }
185 
DestroyNetworkCache(uint16_t netId)186 int32_t NetsysNativeServiceProxy::DestroyNetworkCache(uint16_t netId)
187 {
188     NETNATIVE_LOGI("Begin to DestroyNetworkCache");
189     MessageParcel data;
190     if (!WriteInterfaceToken(data)) {
191         return ERR_FLATTEN_OBJECT;
192     }
193     if (!data.WriteUint16(netId)) {
194         return ERR_FLATTEN_OBJECT;
195     }
196 
197     MessageParcel reply;
198     MessageOption option;
199     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DESTROY_NETWORK_CACHE), data,
200         reply, option);
201     if (result != ERR_NONE) {
202         NETNATIVE_LOGE("SendRequest failed, result code: [%{public}d]", result);
203         return IPC_INVOKER_ERR;
204     }
205     return reply.ReadInt32();
206 }
207 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)208 int32_t NetsysNativeServiceProxy::GetAddrInfo(const std::string &hostName, const std::string &serverName,
209                                               const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
210 {
211     MessageParcel data;
212     if (!WriteInterfaceToken(data) || !data.WriteString(hostName) || !data.WriteString(serverName) ||
213         !data.WriteRawData(&hints, sizeof(AddrInfo)) || !data.WriteUint16(netId)) {
214         return ERR_FLATTEN_OBJECT;
215     }
216 
217     MessageParcel reply;
218     MessageOption option;
219     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ADDR_INFO), data,
220         reply, option);
221     if (result != ERR_NONE) {
222         NETNATIVE_LOGE("SendRequest failed, result code: [%{public}d]", result);
223         return IPC_INVOKER_ERR;
224     }
225     int32_t ret;
226     uint32_t addrSize;
227     if (!reply.ReadInt32(ret) || ret != ERR_NONE || !reply.ReadUint32(addrSize) || addrSize > MAX_RESULTS) {
228         return ERR_INVALID_DATA;
229     }
230 
231     std::vector<AddrInfo> infos;
232     for (uint32_t i = 0; i < addrSize; ++i) {
233         auto p = reply.ReadRawData(sizeof(AddrInfo));
234         if (p == nullptr) {
235             return ERR_INVALID_DATA;
236         }
237 
238         AddrInfo info = {};
239         if (memcpy_s(&info, sizeof(AddrInfo), p, sizeof(AddrInfo)) != EOK) {
240             return ERR_INVALID_DATA;
241         }
242 
243         infos.emplace_back(info);
244     }
245 
246     res = infos;
247     return ret;
248 }
249 
SetInterfaceMtu(const std::string & interfaceName,int32_t mtu)250 int32_t NetsysNativeServiceProxy::SetInterfaceMtu(const std::string &interfaceName, int32_t mtu)
251 {
252     NETNATIVE_LOGI("Begin to SetInterfaceMtu");
253     MessageParcel data;
254     if (!WriteInterfaceToken(data)) {
255         return ERR_FLATTEN_OBJECT;
256     }
257     if (!data.WriteString(interfaceName)) {
258         return ERR_FLATTEN_OBJECT;
259     }
260     if (!data.WriteInt32(mtu)) {
261         return ERR_FLATTEN_OBJECT;
262     }
263 
264     MessageParcel reply;
265     MessageOption option;
266     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_MTU), data,
267         reply, option);
268     if (result != ERR_NONE) {
269         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
270         return IPC_INVOKER_ERR;
271     }
272     return reply.ReadInt32();
273 }
274 
SetTcpBufferSizes(const std::string & tcpBufferSizes)275 int32_t NetsysNativeServiceProxy::SetTcpBufferSizes(const std::string &tcpBufferSizes)
276 {
277     NETNATIVE_LOGI("Begin to SetTcpBufferSizes");
278     MessageParcel data;
279     if (!WriteInterfaceToken(data)) {
280         return ERR_FLATTEN_OBJECT;
281     }
282     if (!data.WriteString(tcpBufferSizes)) {
283         return ERR_FLATTEN_OBJECT;
284     }
285 
286     MessageParcel reply;
287     MessageOption option;
288     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_TCP_BUFFER_SIZES), data,
289         reply, option);
290     if (result != ERR_NONE) {
291         NETNATIVE_LOGE("SendRequest failed, result code: [%{public}d]", result);
292         return IPC_INVOKER_ERR;
293     }
294     return reply.ReadInt32();
295 }
296 
GetInterfaceMtu(const std::string & interfaceName)297 int32_t NetsysNativeServiceProxy::GetInterfaceMtu(const std::string &interfaceName)
298 {
299     NETNATIVE_LOGI("Begin to GetInterfaceMtu");
300     MessageParcel data;
301     if (!WriteInterfaceToken(data)) {
302         return ERR_FLATTEN_OBJECT;
303     }
304     if (!data.WriteString(interfaceName)) {
305         return ERR_FLATTEN_OBJECT;
306     }
307 
308     MessageParcel reply;
309     MessageOption option;
310     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_MTU), data,
311         reply, option);
312     if (result != ERR_NONE) {
313         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
314         return IPC_INVOKER_ERR;
315     }
316     return reply.ReadInt32();
317 }
318 
RegisterNotifyCallback(sptr<INotifyCallback> & callback)319 int32_t NetsysNativeServiceProxy::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
320 {
321     NETNATIVE_LOGI("Begin to RegisterNotifyCallback");
322     MessageParcel data;
323     if (callback == nullptr) {
324         NETNATIVE_LOGE("The parameter of callback is nullptr");
325         return NETMANAGER_ERR_LOCAL_PTR_NULL;
326     }
327 
328     if (!WriteInterfaceToken(data)) {
329         return ERR_FLATTEN_OBJECT;
330     }
331     data.WriteRemoteObject(callback->AsObject().GetRefPtr());
332 
333     MessageParcel reply;
334     MessageOption option;
335     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_NOTIFY_CALLBACK),
336         data, reply, option);
337     if (result != ERR_NONE) {
338         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
339         return IPC_INVOKER_ERR;
340     }
341     return reply.ReadInt32();
342 }
343 
UnRegisterNotifyCallback(sptr<INotifyCallback> & callback)344 int32_t NetsysNativeServiceProxy::UnRegisterNotifyCallback(sptr<INotifyCallback> &callback)
345 {
346     NETNATIVE_LOGI("Begin to UnRegisterNotifyCallback");
347     MessageParcel data;
348     if (callback == nullptr) {
349         NETNATIVE_LOGE("The parameter of callback is nullptr");
350         return NETMANAGER_ERR_LOCAL_PTR_NULL;
351     }
352 
353     if (!WriteInterfaceToken(data)) {
354         NETNATIVE_LOGE("WriteInterfaceToken fail");
355         return ERR_FLATTEN_OBJECT;
356     }
357 
358     data.WriteRemoteObject(callback->AsObject().GetRefPtr());
359 
360     MessageParcel reply;
361     MessageOption option;
362     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_NOTIFY_CALLBACK),
363         data, reply, option);
364     if (result != ERR_NONE) {
365         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
366         return IPC_INVOKER_ERR;
367     }
368     return reply.ReadInt32();
369 }
370 
NetworkAddRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)371 int32_t NetsysNativeServiceProxy::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
372                                                   const std::string &destination, const std::string &nextHop)
373 {
374     NETNATIVE_LOGI("Begin to NetworkAddRoute");
375     MessageParcel data;
376     if (!WriteInterfaceToken(data)) {
377         return ERR_FLATTEN_OBJECT;
378     }
379     if (!data.WriteInt32(netId)) {
380         return ERR_FLATTEN_OBJECT;
381     }
382     if (!data.WriteString(interfaceName)) {
383         return ERR_FLATTEN_OBJECT;
384     }
385     if (!data.WriteString(destination)) {
386         return ERR_FLATTEN_OBJECT;
387     }
388     if (!data.WriteString(nextHop)) {
389         return ERR_FLATTEN_OBJECT;
390     }
391 
392     MessageParcel reply;
393     MessageOption option;
394     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE),
395         data, reply, option);
396     if (result != ERR_NONE) {
397         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
398         return IPC_INVOKER_ERR;
399     }
400     return reply.ReadInt32();
401 }
402 
NetworkRemoveRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)403 int32_t NetsysNativeServiceProxy::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
404                                                      const std::string &destination, const std::string &nextHop)
405 {
406     NETNATIVE_LOGI("Begin to NetworkRemoveRoute");
407     MessageParcel data;
408     if (!WriteInterfaceToken(data)) {
409         return ERR_FLATTEN_OBJECT;
410     }
411     if (!data.WriteInt32(netId)) {
412         return ERR_FLATTEN_OBJECT;
413     }
414     if (!data.WriteString(interfaceName)) {
415         return ERR_FLATTEN_OBJECT;
416     }
417     if (!data.WriteString(destination)) {
418         return ERR_FLATTEN_OBJECT;
419     }
420     if (!data.WriteString(nextHop)) {
421         return ERR_FLATTEN_OBJECT;
422     }
423 
424     MessageParcel reply;
425     MessageOption option;
426     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE),
427         data, reply, option);
428     if (result != ERR_NONE) {
429         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
430         return IPC_INVOKER_ERR;
431     }
432     return reply.ReadInt32();
433 }
434 
NetworkAddRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)435 int32_t NetsysNativeServiceProxy::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
436 {
437     NETNATIVE_LOGI("Begin to NetworkAddRouteParcel");
438     MessageParcel data;
439     if (!WriteInterfaceToken(data)) {
440         return ERR_FLATTEN_OBJECT;
441     }
442     if (!data.WriteInt32(netId)) {
443         return ERR_FLATTEN_OBJECT;
444     }
445     if (!data.WriteString(routeInfo.ifName)) {
446         return ERR_FLATTEN_OBJECT;
447     }
448     if (!data.WriteString(routeInfo.destination)) {
449         return ERR_FLATTEN_OBJECT;
450     }
451     if (!data.WriteString(routeInfo.nextHop)) {
452         return ERR_FLATTEN_OBJECT;
453     }
454 
455     MessageParcel reply;
456     MessageOption option;
457     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE_PARCEL),
458         data, reply, option);
459     if (result != ERR_NONE) {
460         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
461         return IPC_INVOKER_ERR;
462     }
463     return reply.ReadInt32();
464 }
465 
NetworkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)466 int32_t NetsysNativeServiceProxy::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
467 {
468     NETNATIVE_LOGI("Begin to NetworkRemoveRouteParcel");
469     MessageParcel data;
470     if (!WriteInterfaceToken(data)) {
471         return ERR_FLATTEN_OBJECT;
472     }
473     if (!data.WriteInt32(netId)) {
474         return ERR_FLATTEN_OBJECT;
475     }
476     if (!data.WriteString(routeInfo.ifName)) {
477         return ERR_FLATTEN_OBJECT;
478     }
479     if (!data.WriteString(routeInfo.destination)) {
480         return ERR_FLATTEN_OBJECT;
481     }
482     if (!data.WriteString(routeInfo.nextHop)) {
483         return ERR_FLATTEN_OBJECT;
484     }
485 
486     MessageParcel reply;
487     MessageOption option;
488     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE_PARCEL),
489         data, reply, option);
490     if (result != ERR_NONE) {
491         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
492         return IPC_INVOKER_ERR;
493     }
494     return reply.ReadInt32();
495 }
496 
NetworkSetDefault(int32_t netId)497 int32_t NetsysNativeServiceProxy::NetworkSetDefault(int32_t netId)
498 {
499     NETNATIVE_LOGI("Begin to NetworkSetDefault");
500     MessageParcel data;
501     if (!WriteInterfaceToken(data)) {
502         return ERR_FLATTEN_OBJECT;
503     }
504     if (!data.WriteInt32(netId)) {
505         return ERR_FLATTEN_OBJECT;
506     }
507 
508     MessageParcel reply;
509     MessageOption option;
510     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_DEFAULT),
511         data, reply, option);
512     if (result != ERR_NONE) {
513         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
514         return IPC_INVOKER_ERR;
515     }
516     return reply.ReadInt32();
517 }
518 
NetworkGetDefault()519 int32_t NetsysNativeServiceProxy::NetworkGetDefault()
520 {
521     NETNATIVE_LOGI("Begin to NetworkGetDefault");
522     MessageParcel data;
523     if (!WriteInterfaceToken(data)) {
524         return ERR_FLATTEN_OBJECT;
525     }
526 
527     MessageParcel reply;
528     MessageOption option;
529     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_GET_DEFAULT),
530         data, reply, option);
531     if (result != ERR_NONE) {
532         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
533         return IPC_INVOKER_ERR;
534     }
535     return reply.ReadInt32();
536 }
537 
NetworkClearDefault()538 int32_t NetsysNativeServiceProxy::NetworkClearDefault()
539 {
540     NETNATIVE_LOGI("Begin to NetworkClearDefault");
541     MessageParcel data;
542     if (!WriteInterfaceToken(data)) {
543         return ERR_FLATTEN_OBJECT;
544     }
545 
546     MessageParcel reply;
547     MessageOption option;
548     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CLEAR_DEFAULT),
549         data, reply, option);
550     if (result != ERR_NONE) {
551         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
552         return IPC_INVOKER_ERR;
553     }
554     return reply.ReadInt32();
555 }
556 
GetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)557 int32_t NetsysNativeServiceProxy::GetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
558                                                 const std::string &parameter, std::string &value)
559 {
560     NETNATIVE_LOGI("Begin to GetSysProcNet");
561     MessageParcel data;
562     if (!WriteInterfaceToken(data)) {
563         return ERR_FLATTEN_OBJECT;
564     }
565 
566     if (!data.WriteInt32(family)) {
567         return ERR_FLATTEN_OBJECT;
568     }
569     if (!data.WriteInt32(which)) {
570         return ERR_FLATTEN_OBJECT;
571     }
572     if (data.WriteString(ifname)) {
573         return ERR_FLATTEN_OBJECT;
574     }
575     if (!data.WriteString(parameter)) {
576         return ERR_FLATTEN_OBJECT;
577     }
578     MessageParcel reply;
579     MessageOption option;
580     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_PROC_SYS_NET),
581         data, reply, option);
582     if (result != ERR_NONE) {
583         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
584         return IPC_INVOKER_ERR;
585     }
586     int32_t ret = reply.ReadInt32();
587     if (ret != ERR_NONE) {
588         NETNATIVE_LOGE("Fail to GetProcSysNet ret= %{public}d", ret);
589         return ret;
590     }
591     std::string valueRsl = reply.ReadString();
592     NETNATIVE_LOGE("NETSYS_GET_PROC_SYS_NET value %{public}s", valueRsl.c_str());
593     value = valueRsl;
594     return ret;
595 }
596 
SetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)597 int32_t NetsysNativeServiceProxy::SetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
598                                                 const std::string &parameter, std::string &value)
599 {
600     NETNATIVE_LOGI("Begin to SetSysProcNet");
601     MessageParcel data;
602     if (!WriteInterfaceToken(data)) {
603         return ERR_FLATTEN_OBJECT;
604     }
605     if (!data.WriteInt32(family)) {
606         return ERR_FLATTEN_OBJECT;
607     }
608     if (!data.WriteInt32(which)) {
609         return ERR_FLATTEN_OBJECT;
610     }
611     if (data.WriteString(ifname)) {
612         return ERR_FLATTEN_OBJECT;
613     }
614     if (!data.WriteString(parameter)) {
615         return ERR_FLATTEN_OBJECT;
616     }
617     if (!data.WriteString(value)) {
618         return ERR_FLATTEN_OBJECT;
619     }
620     MessageParcel reply;
621     MessageOption option;
622     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_PROC_SYS_NET),
623         data, reply, option);
624     if (result != ERR_NONE) {
625         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
626         return IPC_INVOKER_ERR;
627     }
628     return reply.ReadInt32();
629 }
630 
SetInternetPermission(uint32_t uid,uint8_t allow,uint8_t isBroker)631 int32_t NetsysNativeServiceProxy::SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker)
632 {
633     MessageParcel data;
634     if (!WriteInterfaceToken(data)) {
635         return ERR_FLATTEN_OBJECT;
636     }
637 
638     if (!data.WriteUint32(uid)) {
639         return ERR_FLATTEN_OBJECT;
640     }
641 
642     if (!data.WriteUint8(allow)) {
643         return ERR_FLATTEN_OBJECT;
644     }
645 
646     if (!data.WriteUint8(isBroker)) {
647         return ERR_FLATTEN_OBJECT;
648     }
649 
650     MessageParcel reply;
651     MessageOption option;
652     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_INTERNET_PERMISSION),
653         data, reply, option);
654     if (result != ERR_NONE) {
655         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
656         return IPC_INVOKER_ERR;
657     }
658     return reply.ReadInt32();
659 }
660 
NetworkCreatePhysical(int32_t netId,int32_t permission)661 int32_t NetsysNativeServiceProxy::NetworkCreatePhysical(int32_t netId, int32_t permission)
662 {
663     NETNATIVE_LOGI("Begin to NetworkCreatePhysical");
664     MessageParcel data;
665     if (!WriteInterfaceToken(data)) {
666         return ERR_FLATTEN_OBJECT;
667     }
668     if (!data.WriteInt32(netId)) {
669         return ERR_FLATTEN_OBJECT;
670     }
671     if (!data.WriteInt32(permission)) {
672         return ERR_FLATTEN_OBJECT;
673     }
674 
675     MessageParcel reply;
676     MessageOption option;
677     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_PHYSICAL),
678         data, reply, option);
679     if (result != ERR_NONE) {
680         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
681         return IPC_INVOKER_ERR;
682     }
683     return reply.ReadInt32();
684 }
685 
NetworkCreateVirtual(int32_t netId,bool hasDns)686 int32_t NetsysNativeServiceProxy::NetworkCreateVirtual(int32_t netId, bool hasDns)
687 {
688     MessageParcel data;
689     if (!WriteInterfaceToken(data) || !data.WriteInt32(netId) || !data.WriteBool(hasDns)) {
690         return ERR_FLATTEN_OBJECT;
691     }
692 
693     MessageParcel reply;
694     MessageOption option;
695     sptr<IRemoteObject> remote = Remote();
696     if (remote == nullptr) {
697         return IPC_PROXY_NULL_INVOKER_ERR;
698     }
699     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_VIRTUAL), data,
700                                       reply, option);
701     if (ERR_NONE != ret) {
702         NETNATIVE_LOGE("NetworkCreateVirtual proxy SendRequest failed, error code: [%{public}d]", ret);
703         return IPC_INVOKER_ERR;
704     }
705     int32_t result = ERR_INVALID_DATA;
706     if (!reply.ReadInt32(result)) {
707         return IPC_PROXY_TRANSACTION_ERR;
708     }
709     return result;
710 }
711 
NetworkAddUids(int32_t netId,const std::vector<UidRange> & uidRanges)712 int32_t NetsysNativeServiceProxy::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
713 {
714     MessageParcel data;
715     if (!WriteInterfaceToken(data) || !data.WriteInt32(netId)) {
716         return ERR_FLATTEN_OBJECT;
717     }
718 
719     if (!data.WriteInt32(uidRanges.size())) {
720         return IPC_PROXY_TRANSACTION_ERR;
721     }
722     for (auto iter : uidRanges) {
723         if (!iter.Marshalling(data)) {
724             return IPC_PROXY_TRANSACTION_ERR;
725         }
726     }
727 
728     MessageParcel reply;
729     MessageOption option;
730     sptr<IRemoteObject> remote = Remote();
731     if (remote == nullptr) {
732         return IPC_PROXY_NULL_INVOKER_ERR;
733     }
734     int32_t ret =
735         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_UIDS), data, reply, option);
736     if (ret != ERR_NONE) {
737         NETNATIVE_LOGE("NetworkAddUids proxy SendRequest failed, error code: [%{public}d]", ret);
738         return IPC_INVOKER_ERR;
739     }
740 
741     int32_t result = ERR_INVALID_DATA;
742     if (!reply.ReadInt32(result)) {
743         return IPC_PROXY_TRANSACTION_ERR;
744     }
745     return result;
746 }
747 
NetworkDelUids(int32_t netId,const std::vector<UidRange> & uidRanges)748 int32_t NetsysNativeServiceProxy::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
749 {
750     MessageParcel data;
751     if (!WriteInterfaceToken(data) || !data.WriteInt32(netId)) {
752         return ERR_FLATTEN_OBJECT;
753     }
754 
755     if (!data.WriteInt32(uidRanges.size())) {
756         return IPC_PROXY_TRANSACTION_ERR;
757     }
758     for (auto iter : uidRanges) {
759         if (!iter.Marshalling(data)) {
760             return IPC_PROXY_TRANSACTION_ERR;
761         }
762     }
763 
764     MessageParcel reply;
765     MessageOption option;
766     sptr<IRemoteObject> remote = Remote();
767     if (remote == nullptr) {
768         return IPC_PROXY_NULL_INVOKER_ERR;
769     }
770     int32_t ret =
771         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DEL_UIDS), data, reply, option);
772     if (ret != ERR_NONE) {
773         NETNATIVE_LOGE("NetworkDelUids proxy SendRequest failed, error code: [%{public}d]", ret);
774         return ERR_FLATTEN_OBJECT;
775     }
776 
777     int32_t result = ERR_INVALID_DATA;
778     if (!reply.ReadInt32(result)) {
779         return IPC_PROXY_TRANSACTION_ERR;
780     }
781     return result;
782 }
783 
AddInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)784 int32_t NetsysNativeServiceProxy::AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
785                                                       int32_t prefixLength)
786 {
787     NETNATIVE_LOGI("Begin to AddInterfaceAddress");
788     MessageParcel data;
789     if (!WriteInterfaceToken(data) || !data.WriteString(interfaceName) || !data.WriteString(addrString) ||
790         !data.WriteInt32(prefixLength)) {
791         return ERR_FLATTEN_OBJECT;
792     }
793 
794     MessageParcel reply;
795     MessageOption option;
796     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_ADD_ADDRESS), data, reply,
797                           option);
798 
799     return reply.ReadInt32();
800 }
801 
DelInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)802 int32_t NetsysNativeServiceProxy::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
803                                                       int32_t prefixLength)
804 {
805     NETNATIVE_LOGI("Begin to DelInterfaceAddress");
806     MessageParcel data;
807     if (!WriteInterfaceToken(data) || !data.WriteString(interfaceName) || !data.WriteString(addrString) ||
808         !data.WriteInt32(prefixLength)) {
809         return ERR_FLATTEN_OBJECT;
810     }
811 
812     MessageParcel reply;
813     MessageOption option;
814     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_DEL_ADDRESS),
815         data, reply, option);
816     if (result != ERR_NONE) {
817         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
818         return IPC_INVOKER_ERR;
819     }
820     return reply.ReadInt32();
821 }
822 
DelInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength,const std::string & netCapabilities)823 int32_t NetsysNativeServiceProxy::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
824                                                       int32_t prefixLength, const std::string &netCapabilities)
825 {
826     NETNATIVE_LOGI("Begin to DelInterfaceAddress");
827     MessageParcel data;
828     if (!WriteInterfaceToken(data) || !data.WriteString(interfaceName) || !data.WriteString(addrString) ||
829         !data.WriteInt32(prefixLength) || !data.WriteString(netCapabilities)) {
830         return ERR_FLATTEN_OBJECT;
831     }
832 
833     MessageParcel reply;
834     MessageOption option;
835     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_DEL_ADDRESS),
836         data, reply, option);
837     if (result != ERR_NONE) {
838         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
839         return IPC_INVOKER_ERR;
840     }
841     return reply.ReadInt32();
842 }
843 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)844 int32_t NetsysNativeServiceProxy::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
845 {
846     NETNATIVE_LOGI("Begin to InterfaceSetIpAddress");
847     MessageParcel data;
848     if (!WriteInterfaceToken(data)) {
849         return ERR_FLATTEN_OBJECT;
850     }
851     if (!data.WriteString(ifaceName) || !data.WriteString(ipAddress)) {
852         return ERR_FLATTEN_OBJECT;
853     }
854     MessageParcel reply;
855     MessageOption option;
856     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IP_ADDRESS),
857         data, reply, option);
858     if (result != ERR_NONE) {
859         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
860         return IPC_INVOKER_ERR;
861     }
862     return reply.ReadInt32();
863 }
864 
InterfaceSetIffUp(const std::string & ifaceName)865 int32_t NetsysNativeServiceProxy::InterfaceSetIffUp(const std::string &ifaceName)
866 {
867     NETNATIVE_LOGI("Begin to InterfaceSetIffUp");
868     MessageParcel data;
869     if (!WriteInterfaceToken(data)) {
870         return ERR_FLATTEN_OBJECT;
871     }
872     if (!data.WriteString(ifaceName)) {
873         return ERR_FLATTEN_OBJECT;
874     }
875     MessageParcel reply;
876     MessageOption option;
877     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IFF_UP),
878         data, reply, option);
879     if (result != ERR_NONE) {
880         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
881         return IPC_INVOKER_ERR;
882     }
883     return reply.ReadInt32();
884 }
885 
NetworkAddInterface(int32_t netId,const std::string & iface,NetBearType netBearerType)886 int32_t NetsysNativeServiceProxy::NetworkAddInterface(int32_t netId, const std::string &iface,
887                                                       NetBearType netBearerType)
888 {
889     NETNATIVE_LOGI("Begin to NetworkAddInterface");
890     MessageParcel data;
891     if (!WriteInterfaceToken(data)) {
892         return ERR_FLATTEN_OBJECT;
893     }
894     if (!data.WriteInt32(netId)) {
895         return ERR_FLATTEN_OBJECT;
896     }
897     if (!data.WriteString(iface)) {
898         return ERR_FLATTEN_OBJECT;
899     }
900     if (!data.WriteUint8(netBearerType)) {
901         return ERR_FLATTEN_OBJECT;
902     }
903 
904     MessageParcel reply;
905     MessageOption option;
906     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_INTERFACE),
907         data, reply, option);
908     if (result != ERR_NONE) {
909         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
910         return IPC_INVOKER_ERR;
911     }
912     return reply.ReadInt32();
913 }
914 
NetworkRemoveInterface(int32_t netId,const std::string & iface)915 int32_t NetsysNativeServiceProxy::NetworkRemoveInterface(int32_t netId, const std::string &iface)
916 {
917     NETNATIVE_LOGI("Begin to NetworkRemoveInterface");
918     MessageParcel data;
919     if (!WriteInterfaceToken(data)) {
920         return ERR_FLATTEN_OBJECT;
921     }
922     if (!data.WriteInt32(netId)) {
923         return ERR_FLATTEN_OBJECT;
924     }
925     if (!data.WriteString(iface)) {
926         return ERR_FLATTEN_OBJECT;
927     }
928 
929     MessageParcel reply;
930     MessageOption option;
931     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_INTERFACE),
932         data, reply, option);
933     if (result != ERR_NONE) {
934         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
935         return IPC_INVOKER_ERR;
936     }
937     return reply.ReadInt32();
938 }
939 
NetworkDestroy(int32_t netId)940 int32_t NetsysNativeServiceProxy::NetworkDestroy(int32_t netId)
941 {
942     NETNATIVE_LOGI("Begin to NetworkDestroy");
943     MessageParcel data;
944     if (!WriteInterfaceToken(data)) {
945         return ERR_FLATTEN_OBJECT;
946     }
947     if (!data.WriteInt32(netId)) {
948         return ERR_FLATTEN_OBJECT;
949     }
950 
951     MessageParcel reply;
952     MessageOption option;
953     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DESTROY),
954         data, reply, option);
955     if (result != ERR_NONE) {
956         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
957         return IPC_INVOKER_ERR;
958     }
959     return reply.ReadInt32();
960 }
961 
CreateVnic(uint16_t mtu,const std::string & tunAddr,int32_t prefix,const std::set<int32_t> & uids)962 int32_t NetsysNativeServiceProxy::CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix,
963                                              const std::set<int32_t> &uids)
964 {
965     NETNATIVE_LOGI("Begin to CreateVnic");
966     MessageParcel data;
967     if (!WriteInterfaceToken(data)) {
968         return ERR_FLATTEN_OBJECT;
969     }
970 
971     if (!data.WriteUint16(mtu)) {
972         return ERR_FLATTEN_OBJECT;
973     }
974 
975     if (!data.WriteString(tunAddr)) {
976         return ERR_FLATTEN_OBJECT;
977     }
978 
979     if (!data.WriteInt32(prefix)) {
980         return ERR_FLATTEN_OBJECT;
981     }
982 
983     if (!data.WriteInt32(uids.size())) {
984         return NETMANAGER_ERR_READ_DATA_FAIL;
985     }
986 
987     for (const auto &uid: uids) {
988         if (!data.WriteInt32(uid)) {
989             return NETMANAGER_ERR_READ_DATA_FAIL;
990         }
991     }
992 
993     MessageParcel reply;
994     MessageOption option;
995     int32_t error =
996         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_VNIC_CREATE), data, reply, option);
997     if (error != ERR_NONE) {
998         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
999         return IPC_INVOKER_ERR;
1000     }
1001     return reply.ReadInt32();
1002 }
1003 
DestroyVnic()1004 int32_t NetsysNativeServiceProxy::DestroyVnic()
1005 {
1006     NETNATIVE_LOGI("Begin to DestroyVnic");
1007     MessageParcel data;
1008     if (!WriteInterfaceToken(data)) {
1009         return ERR_FLATTEN_OBJECT;
1010     }
1011 
1012     MessageParcel reply;
1013     MessageOption option;
1014     int32_t error =
1015         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_VNIC_DESTROY), data, reply, option);
1016     if (error != ERR_NONE) {
1017         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1018         return IPC_INVOKER_ERR;
1019     }
1020     return reply.ReadInt32();
1021 }
1022 
GetFwmarkForNetwork(int32_t netId,MarkMaskParcel & markMaskParcel)1023 int32_t NetsysNativeServiceProxy::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
1024 {
1025     NETNATIVE_LOGI("Begin to GetFwmarkForNetwork");
1026     MessageParcel data;
1027     if (!WriteInterfaceToken(data)) {
1028         return ERR_FLATTEN_OBJECT;
1029     }
1030     if (!data.WriteInt32(netId)) {
1031         return ERR_FLATTEN_OBJECT;
1032     }
1033     if (!data.WriteInt32(markMaskParcel.mark)) {
1034         return ERR_FLATTEN_OBJECT;
1035     }
1036     if (!data.WriteInt32(markMaskParcel.mask)) {
1037         return ERR_FLATTEN_OBJECT;
1038     }
1039 
1040     MessageParcel reply;
1041     MessageOption option;
1042     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_FWMARK_FOR_NETWORK),
1043         data, reply, option);
1044     if (result != ERR_NONE) {
1045         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1046         return IPC_INVOKER_ERR;
1047     }
1048     return reply.ReadInt32();
1049 }
1050 
SetInterfaceConfig(const InterfaceConfigurationParcel & cfg)1051 int32_t NetsysNativeServiceProxy::SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)
1052 {
1053     NETNATIVE_LOGI("Begin to SetInterfaceConfig");
1054     MessageParcel data;
1055     if (!WriteInterfaceToken(data)) {
1056         return ERR_FLATTEN_OBJECT;
1057     }
1058     if (!data.WriteString(cfg.ifName)) {
1059         return ERR_FLATTEN_OBJECT;
1060     }
1061     if (!data.WriteString(cfg.hwAddr)) {
1062         return ERR_FLATTEN_OBJECT;
1063     }
1064     if (!data.WriteString(cfg.ipv4Addr)) {
1065         return ERR_FLATTEN_OBJECT;
1066     }
1067     if (!data.WriteInt32(cfg.prefixLength)) {
1068         return ERR_FLATTEN_OBJECT;
1069     }
1070     int32_t vsize = static_cast<int32_t>(cfg.flags.size());
1071     if (!data.WriteInt32(vsize)) {
1072         return ERR_FLATTEN_OBJECT;
1073     }
1074     std::vector<std::string> vCflags;
1075     vCflags.assign(cfg.flags.begin(), cfg.flags.end());
1076     NETNATIVE_LOGI("PROXY: SetInterfaceConfig Write flags String_SIZE: %{public}d",
1077                    static_cast<int32_t>(vCflags.size()));
1078     for (std::vector<std::string>::iterator it = vCflags.begin(); it != vCflags.end(); ++it) {
1079         data.WriteString(*it);
1080     }
1081     MessageParcel reply;
1082     MessageOption option;
1083     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_CONFIG),
1084         data, reply, option);
1085     if (result != ERR_NONE) {
1086         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1087         return IPC_INVOKER_ERR;
1088     }
1089     return reply.ReadInt32();
1090 }
1091 
GetInterfaceConfig(InterfaceConfigurationParcel & cfg)1092 int32_t NetsysNativeServiceProxy::GetInterfaceConfig(InterfaceConfigurationParcel &cfg)
1093 {
1094     NETNATIVE_LOGI("Begin to GetInterfaceConfig");
1095     MessageParcel data;
1096     int32_t ret;
1097     int32_t vSize;
1098     if (!WriteInterfaceToken(data)) {
1099         return ERR_FLATTEN_OBJECT;
1100     }
1101     if (!data.WriteString(cfg.ifName)) {
1102         return ERR_FLATTEN_OBJECT;
1103     }
1104 
1105     MessageParcel reply;
1106     MessageOption option;
1107     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_CONFIG),
1108         data, reply, option);
1109     if (result != ERR_NONE) {
1110         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1111         return IPC_INVOKER_ERR;
1112     }
1113     ret = reply.ReadInt32();
1114     if (ret != ERR_NONE) {
1115         NETNATIVE_LOGE("Fail to GetInterfaceConfig ret= %{public}d", ret);
1116         return ret;
1117     }
1118     reply.ReadString(cfg.ifName);
1119     reply.ReadString(cfg.hwAddr);
1120     reply.ReadString(cfg.ipv4Addr);
1121     reply.ReadInt32(cfg.prefixLength);
1122     vSize = reply.ReadInt32();
1123     vSize = vSize > MAX_INTERFACE_CONFIG_SIZE ? MAX_INTERFACE_CONFIG_SIZE : vSize;
1124     std::vector<std::string> vecString;
1125     for (int i = 0; i < vSize; i++) {
1126         vecString.push_back(reply.ReadString());
1127     }
1128     if (vSize > 0) {
1129         cfg.flags.assign(vecString.begin(), vecString.end());
1130     }
1131     NETNATIVE_LOGI("End to GetInterfaceConfig, ret =%{public}d", ret);
1132     return ret;
1133 }
1134 
InterfaceGetList(std::vector<std::string> & ifaces)1135 int32_t NetsysNativeServiceProxy::InterfaceGetList(std::vector<std::string> &ifaces)
1136 {
1137     NETNATIVE_LOGI("NetsysNativeServiceProxy Begin to InterfaceGetList");
1138     MessageParcel data;
1139     int32_t ret;
1140     int32_t vSize;
1141     if (!WriteInterfaceToken(data)) {
1142         return ERR_FLATTEN_OBJECT;
1143     }
1144     MessageParcel reply;
1145     MessageOption option;
1146     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_LIST),
1147         data, reply, option);
1148     if (result != ERR_NONE) {
1149         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1150         return IPC_INVOKER_ERR;
1151     }
1152     ret = reply.ReadInt32();
1153     if (ret != ERR_NONE) {
1154         NETNATIVE_LOGE("Fail to InterfaceGetList ret= %{public}d", ret);
1155         return ret;
1156     }
1157     vSize = reply.ReadInt32();
1158     std::vector<std::string> vecString;
1159     for (int i = 0; i < vSize; i++) {
1160         vecString.push_back(reply.ReadString());
1161     }
1162     if (vSize > 0) {
1163         ifaces.assign(vecString.begin(), vecString.end());
1164     }
1165     NETNATIVE_LOGI("NetsysNativeServiceProxy End to InterfaceGetList, ret =%{public}d", ret);
1166     return ret;
1167 }
1168 
StartDhcpClient(const std::string & iface,bool bIpv6)1169 int32_t NetsysNativeServiceProxy::StartDhcpClient(const std::string &iface, bool bIpv6)
1170 {
1171     NETNATIVE_LOGI("Begin to StartDhcpClient");
1172     MessageParcel data;
1173     int32_t ret;
1174     if (!WriteInterfaceToken(data)) {
1175         return ERR_FLATTEN_OBJECT;
1176     }
1177     if (!data.WriteString(iface)) {
1178         return ERR_FLATTEN_OBJECT;
1179     }
1180     if (!data.WriteBool(bIpv6)) {
1181         return ERR_FLATTEN_OBJECT;
1182     }
1183 
1184     MessageParcel reply;
1185     MessageOption option;
1186     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_CLIENT),
1187         data, reply, option);
1188     if (result != ERR_NONE) {
1189         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1190         return IPC_INVOKER_ERR;
1191     }
1192     ret = reply.ReadInt32();
1193     NETNATIVE_LOGI("End to StartDhcpClient, ret =%{public}d", ret);
1194     return ret;
1195 }
1196 
StopDhcpClient(const std::string & iface,bool bIpv6)1197 int32_t NetsysNativeServiceProxy::StopDhcpClient(const std::string &iface, bool bIpv6)
1198 {
1199     NETNATIVE_LOGI("Begin to StopDhcpClient");
1200     MessageParcel data;
1201     int32_t ret;
1202     if (!WriteInterfaceToken(data)) {
1203         return ERR_FLATTEN_OBJECT;
1204     }
1205     if (!data.WriteString(iface)) {
1206         return ERR_FLATTEN_OBJECT;
1207     }
1208     if (!data.WriteBool(bIpv6)) {
1209         return ERR_FLATTEN_OBJECT;
1210     }
1211 
1212     MessageParcel reply;
1213     MessageOption option;
1214     ret =
1215         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_CLIENT), data, reply, option);
1216     if (ret != ERR_NONE) {
1217         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", ret);
1218         return IPC_INVOKER_ERR;
1219     }
1220     NETNATIVE_LOGI("SendRequest, ret =%{public}d", ret);
1221     ret = reply.ReadInt32();
1222     NETNATIVE_LOGI("End to StopDhcpClient, ret =%{public}d", ret);
1223     return ret;
1224 }
1225 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)1226 int32_t NetsysNativeServiceProxy::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
1227 {
1228     NETNATIVE_LOGI("Begin to StartDhcpService");
1229     MessageParcel data;
1230 
1231     if (!WriteInterfaceToken(data)) {
1232         return ERR_FLATTEN_OBJECT;
1233     }
1234     if (!data.WriteString(iface)) {
1235         return ERR_FLATTEN_OBJECT;
1236     }
1237     if (!data.WriteString(ipv4addr)) {
1238         return ERR_FLATTEN_OBJECT;
1239     }
1240 
1241     MessageParcel reply;
1242     MessageOption option;
1243     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_SERVICE),
1244         data, reply, option);
1245     if (result != ERR_NONE) {
1246         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1247         return IPC_INVOKER_ERR;
1248     }
1249     int32_t ret = reply.ReadInt32();
1250     NETNATIVE_LOGI("End to StartDhcpService, ret =%{public}d", ret);
1251     return ret;
1252 }
1253 
StopDhcpService(const std::string & iface)1254 int32_t NetsysNativeServiceProxy::StopDhcpService(const std::string &iface)
1255 {
1256     NETNATIVE_LOGI("Begin to StopDhcpService");
1257     MessageParcel data;
1258     if (!WriteInterfaceToken(data)) {
1259         return ERR_FLATTEN_OBJECT;
1260     }
1261     if (!data.WriteString(iface)) {
1262         return ERR_FLATTEN_OBJECT;
1263     }
1264 
1265     MessageParcel reply;
1266     MessageOption option;
1267     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_SERVICE),
1268         data, reply, option);
1269     if (result != ERR_NONE) {
1270         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1271         return IPC_INVOKER_ERR;
1272     }
1273     int32_t ret = reply.ReadInt32();
1274     NETNATIVE_LOGI("End to StopDhcpService, ret =%{public}d", ret);
1275     return ret;
1276 }
1277 
IpEnableForwarding(const std::string & requestor)1278 int32_t NetsysNativeServiceProxy::IpEnableForwarding(const std::string &requestor)
1279 {
1280     MessageParcel data;
1281     if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1282         return ERR_FLATTEN_OBJECT;
1283     }
1284 
1285     MessageParcel reply;
1286     MessageOption option;
1287     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPENABLE_FORWARDING),
1288         data, reply, option);
1289     if (result != ERR_NONE) {
1290         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1291         return IPC_INVOKER_ERR;
1292     }
1293     int32_t ret = reply.ReadInt32();
1294     NETNATIVE_LOGI("End to IpEnableForwarding, ret =%{public}d", ret);
1295     return ret;
1296 }
1297 
IpDisableForwarding(const std::string & requestor)1298 int32_t NetsysNativeServiceProxy::IpDisableForwarding(const std::string &requestor)
1299 {
1300     MessageParcel data;
1301     if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1302         return ERR_FLATTEN_OBJECT;
1303     }
1304 
1305     MessageParcel reply;
1306     MessageOption option;
1307     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPDISABLE_FORWARDING),
1308         data, reply, option);
1309     if (result != ERR_NONE) {
1310         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1311         return IPC_INVOKER_ERR;
1312     }
1313     int32_t ret = reply.ReadInt32();
1314     NETNATIVE_LOGI("End to IpDisableForwarding, ret =%{public}d", ret);
1315     return ret;
1316 }
1317 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)1318 int32_t NetsysNativeServiceProxy::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1319 {
1320     MessageParcel data;
1321     if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1322         return ERR_FLATTEN_OBJECT;
1323     }
1324 
1325     MessageParcel reply;
1326     MessageOption option;
1327     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_NAT),
1328         data, reply, option);
1329     if (result != ERR_NONE) {
1330         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1331         return IPC_INVOKER_ERR;
1332     }
1333     int32_t ret = reply.ReadInt32();
1334     NETNATIVE_LOGI("End to EnableNat, ret =%{public}d", ret);
1335     return ret;
1336 }
1337 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)1338 int32_t NetsysNativeServiceProxy::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1339 {
1340     MessageParcel data;
1341     if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1342         return ERR_FLATTEN_OBJECT;
1343     }
1344 
1345     MessageParcel reply;
1346     MessageOption option;
1347     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_NAT),
1348         data, reply, option);
1349     if (result != ERR_NONE) {
1350         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1351         return IPC_INVOKER_ERR;
1352     }
1353     int32_t ret = reply.ReadInt32();
1354     NETNATIVE_LOGI("End to DisableNat, ret =%{public}d", ret);
1355     return ret;
1356 }
1357 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)1358 int32_t NetsysNativeServiceProxy::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
1359 {
1360     MessageParcel data;
1361     if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1362         return ERR_FLATTEN_OBJECT;
1363     }
1364 
1365     MessageParcel reply;
1366     MessageOption option;
1367     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_ADD_INTERFACE_FORWARD),
1368         data, reply, option);
1369     if (result != ERR_NONE) {
1370         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1371         return IPC_INVOKER_ERR;
1372     }
1373     int32_t ret = reply.ReadInt32();
1374     NETNATIVE_LOGI("End to IpfwdAddInterfaceForward, ret =%{public}d", ret);
1375     return ret;
1376 }
1377 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)1378 int32_t NetsysNativeServiceProxy::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
1379 {
1380     MessageParcel data;
1381     if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1382         return ERR_FLATTEN_OBJECT;
1383     }
1384 
1385     MessageParcel reply;
1386     MessageOption option;
1387     int result =
1388         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD),
1389             data, reply, option);
1390     if (result != ERR_NONE) {
1391         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1392         return IPC_INVOKER_ERR;
1393     }
1394     int32_t ret = reply.ReadInt32();
1395     NETNATIVE_LOGI("End to IpfwdRemoveInterfaceForward, ret =%{public}d", ret);
1396     return ret;
1397 }
1398 
BandwidthEnableDataSaver(bool enable)1399 int32_t NetsysNativeServiceProxy::BandwidthEnableDataSaver(bool enable)
1400 {
1401     MessageParcel data;
1402     if (!WriteInterfaceToken(data)) {
1403         NETNATIVE_LOGE("WriteInterfaceToken failed");
1404         return ERR_FLATTEN_OBJECT;
1405     }
1406     if (!data.WriteBool(enable)) {
1407         NETNATIVE_LOGE("WriteBool failed");
1408         return ERR_FLATTEN_OBJECT;
1409     }
1410 
1411     MessageParcel reply;
1412     MessageOption option;
1413     auto remote = Remote();
1414     if (remote == nullptr) {
1415         return IPC_PROXY_NULL_INVOKER_ERR;
1416     }
1417     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER),
1418                                         data, reply, option);
1419     if (error != ERR_NONE) {
1420         NETNATIVE_LOGE("proxy SendRequest failed");
1421         return ERR_FLATTEN_OBJECT;
1422     }
1423     int32_t ret = reply.ReadInt32();
1424     return ret;
1425 }
1426 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)1427 int32_t NetsysNativeServiceProxy::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
1428 {
1429     MessageParcel data;
1430     if (!WriteInterfaceToken(data)) {
1431         NETNATIVE_LOGE("WriteInterfaceToken failed");
1432         return ERR_FLATTEN_OBJECT;
1433     }
1434     if (!data.WriteString(ifName)) {
1435         NETNATIVE_LOGE("WriteString failed");
1436         return ERR_FLATTEN_OBJECT;
1437     }
1438     if (!data.WriteInt64(bytes)) {
1439         NETNATIVE_LOGE("WriteInt64 failed");
1440         return ERR_FLATTEN_OBJECT;
1441     }
1442 
1443     MessageParcel reply;
1444     MessageOption option;
1445     auto remote = Remote();
1446     if (remote == nullptr) {
1447         return IPC_PROXY_NULL_INVOKER_ERR;
1448     }
1449     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_SET_IFACE_QUOTA),
1450                                         data, reply, option);
1451     if (error != ERR_NONE) {
1452         NETNATIVE_LOGE("proxy SendRequest failed");
1453         return ERR_FLATTEN_OBJECT;
1454     }
1455     int32_t ret = reply.ReadInt32();
1456     return ret;
1457 }
1458 
BandwidthRemoveIfaceQuota(const std::string & ifName)1459 int32_t NetsysNativeServiceProxy::BandwidthRemoveIfaceQuota(const std::string &ifName)
1460 {
1461     MessageParcel data;
1462     if (!WriteInterfaceToken(data)) {
1463         NETNATIVE_LOGE("WriteInterfaceToken failed");
1464         return ERR_FLATTEN_OBJECT;
1465     }
1466     if (!data.WriteString(ifName)) {
1467         NETNATIVE_LOGE("WriteString failed");
1468         return ERR_FLATTEN_OBJECT;
1469     }
1470 
1471     MessageParcel reply;
1472     MessageOption option;
1473     auto remote = Remote();
1474     if (remote == nullptr) {
1475         return IPC_PROXY_NULL_INVOKER_ERR;
1476     }
1477     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA),
1478                                         data, reply, option);
1479     if (error != ERR_NONE) {
1480         NETNATIVE_LOGE("proxy SendRequest failed");
1481         return ERR_FLATTEN_OBJECT;
1482     }
1483     int32_t ret = reply.ReadInt32();
1484     return ret;
1485 }
1486 
BandwidthAddDeniedList(uint32_t uid)1487 int32_t NetsysNativeServiceProxy::BandwidthAddDeniedList(uint32_t uid)
1488 {
1489     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_DENIED_LIST));
1490     return ret;
1491 }
1492 
BandwidthRemoveDeniedList(uint32_t uid)1493 int32_t NetsysNativeServiceProxy::BandwidthRemoveDeniedList(uint32_t uid)
1494 {
1495     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST));
1496     return ret;
1497 }
1498 
BandwidthAddAllowedList(uint32_t uid)1499 int32_t NetsysNativeServiceProxy::BandwidthAddAllowedList(uint32_t uid)
1500 {
1501     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST));
1502     return ret;
1503 }
1504 
BandwidthRemoveAllowedList(uint32_t uid)1505 int32_t NetsysNativeServiceProxy::BandwidthRemoveAllowedList(uint32_t uid)
1506 {
1507     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST));
1508     return ret;
1509 }
1510 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1511 int32_t NetsysNativeServiceProxy::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1512 {
1513     MessageParcel data;
1514     uint32_t uidSize = uids.size();
1515     if (uidSize > UIDS_LIST_MAX_SIZE) {
1516         NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1517         return ERR_INVALID_DATA;
1518     }
1519     if (!WriteInterfaceToken(data)) {
1520         NETNATIVE_LOGE("WriteInterfaceToken failed");
1521         return ERR_FLATTEN_OBJECT;
1522     }
1523     if (!data.WriteUint32(chain)) {
1524         NETNATIVE_LOGE("WriteUint32 failed");
1525         return ERR_FLATTEN_OBJECT;
1526     }
1527     if (!data.WriteUint32(uidSize)) {
1528         NETNATIVE_LOGE("WriteUint32 failed");
1529         return ERR_FLATTEN_OBJECT;
1530     }
1531     std::vector<uint32_t> vUids;
1532     vUids.assign(uids.begin(), uids.end());
1533     std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1534 
1535     MessageParcel reply;
1536     MessageOption option;
1537     auto remote = Remote();
1538     if (remote == nullptr) {
1539         return IPC_PROXY_NULL_INVOKER_ERR;
1540     }
1541     int32_t error = remote->SendRequest(
1542         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN), data, reply, option);
1543     if (error != ERR_NONE) {
1544         NETNATIVE_LOGE("proxy SendRequest failed");
1545         return ERR_FLATTEN_OBJECT;
1546     }
1547     int32_t ret = reply.ReadInt32();
1548     return ret;
1549 }
1550 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1551 int32_t NetsysNativeServiceProxy::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1552 {
1553     MessageParcel data;
1554     uint32_t uidSize = uids.size();
1555     if (uidSize > UIDS_LIST_MAX_SIZE) {
1556         NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1557         return ERR_INVALID_DATA;
1558     }
1559     if (!WriteInterfaceToken(data)) {
1560         NETNATIVE_LOGE("WriteInterfaceToken failed");
1561         return ERR_FLATTEN_OBJECT;
1562     }
1563     if (!data.WriteUint32(chain)) {
1564         NETNATIVE_LOGE("WriteUint32 Error");
1565         return ERR_FLATTEN_OBJECT;
1566     }
1567     if (!data.WriteUint32(uidSize)) {
1568         NETNATIVE_LOGE("WriteUint32 Error");
1569         return ERR_FLATTEN_OBJECT;
1570     }
1571     std::vector<uint32_t> vUids;
1572     vUids.assign(uids.begin(), uids.end());
1573     std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1574 
1575     MessageParcel reply;
1576     MessageOption option;
1577     auto remote = Remote();
1578     if (remote == nullptr) {
1579         return IPC_PROXY_NULL_INVOKER_ERR;
1580     }
1581     int32_t error = remote->SendRequest(
1582         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN), data, reply, option);
1583     if (error != ERR_NONE) {
1584         NETNATIVE_LOGE("proxy SendRequest failed");
1585         return ERR_FLATTEN_OBJECT;
1586     }
1587     int32_t ret = reply.ReadInt32();
1588     return ret;
1589 }
1590 
FirewallEnableChain(uint32_t chain,bool enable)1591 int32_t NetsysNativeServiceProxy::FirewallEnableChain(uint32_t chain, bool enable)
1592 {
1593     MessageParcel data;
1594     if (!WriteInterfaceToken(data)) {
1595         NETNATIVE_LOGE("WriteInterfaceToken failed");
1596         return ERR_FLATTEN_OBJECT;
1597     }
1598     if (!data.WriteUint32(chain)) {
1599         NETNATIVE_LOGE("WriteUint32 Error");
1600         return ERR_FLATTEN_OBJECT;
1601     }
1602     if (!data.WriteBool(enable)) {
1603         NETNATIVE_LOGE("WriteBool Error");
1604         return ERR_FLATTEN_OBJECT;
1605     }
1606 
1607     MessageParcel reply;
1608     MessageOption option;
1609     auto remote = Remote();
1610     if (remote == nullptr) {
1611         return IPC_PROXY_NULL_INVOKER_ERR;
1612     }
1613     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_ENABLE_CHAIN), data,
1614                                         reply, option);
1615     if (error != ERR_NONE) {
1616         NETNATIVE_LOGE("proxy SendRequest failed");
1617         return ERR_FLATTEN_OBJECT;
1618     }
1619     int32_t ret = reply.ReadInt32();
1620     return ret;
1621 }
1622 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)1623 int32_t NetsysNativeServiceProxy::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
1624                                                      uint32_t firewallRule)
1625 {
1626     MessageParcel data;
1627     if (!WriteInterfaceToken(data)) {
1628         NETNATIVE_LOGE("WriteInterfaceToken failed");
1629         return ERR_FLATTEN_OBJECT;
1630     }
1631     if (!data.WriteUint32(chain)) {
1632         NETNATIVE_LOGE("WriteUint32 failed");
1633         return ERR_FLATTEN_OBJECT;
1634     }
1635     if (!data.WriteUInt32Vector(uids)) {
1636         NETNATIVE_LOGE("WriteUint32 failed");
1637         return ERR_FLATTEN_OBJECT;
1638     }
1639     if (!data.WriteUint32(firewallRule)) {
1640         NETNATIVE_LOGE("WriteUint32 failed");
1641         return ERR_FLATTEN_OBJECT;
1642     }
1643 
1644     MessageParcel reply;
1645     MessageOption option;
1646     auto remote = Remote();
1647     if (remote == nullptr) {
1648         return IPC_PROXY_NULL_INVOKER_ERR;
1649     }
1650     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_RULE), data,
1651                                         reply, option);
1652     if (error != ERR_NONE) {
1653         NETNATIVE_LOGE("proxy SendRequest failed");
1654         return ERR_FLATTEN_OBJECT;
1655     }
1656     int32_t ret = reply.ReadInt32();
1657     return ret;
1658 }
1659 
ShareDnsSet(uint16_t netId)1660 int32_t NetsysNativeServiceProxy::ShareDnsSet(uint16_t netId)
1661 {
1662     MessageParcel data;
1663     if (!WriteInterfaceToken(data)) {
1664         return ERR_FLATTEN_OBJECT;
1665     }
1666     if (!data.WriteUint16(netId)) {
1667         return ERR_FLATTEN_OBJECT;
1668     }
1669 
1670     MessageParcel reply;
1671     MessageOption option;
1672     int32_t error =
1673         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_TETHER_DNS_SET), data, reply, option);
1674     if (error != ERR_NONE) {
1675         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1676         return error;
1677     }
1678     return reply.ReadInt32();
1679 }
1680 
StartDnsProxyListen()1681 int32_t NetsysNativeServiceProxy::StartDnsProxyListen()
1682 {
1683     MessageParcel data;
1684     if (!WriteInterfaceToken(data)) {
1685         return ERR_FLATTEN_OBJECT;
1686     }
1687 
1688     MessageParcel reply;
1689     MessageOption option;
1690     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DNS_PROXY_LISTEN),
1691                                           data, reply, option);
1692     if (error != ERR_NONE) {
1693         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1694         return error;
1695     }
1696     return reply.ReadInt32();
1697 }
1698 
StopDnsProxyListen()1699 int32_t NetsysNativeServiceProxy::StopDnsProxyListen()
1700 {
1701     MessageParcel data;
1702     if (!WriteInterfaceToken(data)) {
1703         return ERR_FLATTEN_OBJECT;
1704     }
1705 
1706     MessageParcel reply;
1707     MessageOption option;
1708     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DNS_PROXY_LISTEN),
1709                                           data, reply, option);
1710     if (error != ERR_NONE) {
1711         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1712         return error;
1713     }
1714     return reply.ReadInt32();
1715 }
1716 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,NetworkSharingTraffic & traffic)1717 int32_t NetsysNativeServiceProxy::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
1718                                                            NetworkSharingTraffic &traffic)
1719 {
1720     MessageParcel data;
1721     if (!WriteInterfaceToken(data)) {
1722         return ERR_FLATTEN_OBJECT;
1723     }
1724     if (!data.WriteString(downIface)) {
1725         return ERR_FLATTEN_OBJECT;
1726     }
1727     if (!data.WriteString(upIface)) {
1728         return ERR_FLATTEN_OBJECT;
1729     }
1730 
1731     MessageParcel reply;
1732     MessageOption option;
1733     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_SHARING_NETWORK_TRAFFIC),
1734         data, reply, option);
1735     if (result != ERR_NONE) {
1736         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1737         return IPC_INVOKER_ERR;
1738     }
1739     int32_t ret = reply.ReadInt32();
1740     if (ret != ERR_NONE) {
1741         NETNATIVE_LOGE("Fail to GetNetworkSharingTraffic ret= %{public}d", ret);
1742         return ret;
1743     }
1744 
1745     traffic.receive = reply.ReadInt64();
1746     traffic.send = reply.ReadInt64();
1747     traffic.all = reply.ReadInt64();
1748     NETNATIVE_LOGI("NetsysNativeServiceProxy GetNetworkSharingTraffic ret=%{public}d", ret);
1749     return ret;
1750 }
1751 
GetTotalStats(uint64_t & stats,uint32_t type)1752 int32_t NetsysNativeServiceProxy::GetTotalStats(uint64_t &stats, uint32_t type)
1753 {
1754     MessageParcel data;
1755     if (!WriteInterfaceToken(data)) {
1756         return ERR_FLATTEN_OBJECT;
1757     }
1758     if (!data.WriteUint8(type)) {
1759         return ERR_FLATTEN_OBJECT;
1760     }
1761     MessageParcel reply;
1762     MessageOption option;
1763     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_TOTAL_STATS), data,
1764                                           reply, option)) {
1765         NETNATIVE_LOGE("proxy SendRequest failed");
1766         return ERR_FLATTEN_OBJECT;
1767     }
1768 
1769     int32_t ret;
1770     if (!reply.ReadInt32(ret)) {
1771         NETNATIVE_LOGE("get ret falil");
1772         return ERR_FLATTEN_OBJECT;
1773     }
1774     if (ret != ERR_NONE) {
1775         NETNATIVE_LOGE("fail to GetTotalStats ret= %{public}d", ret);
1776         return ret;
1777     }
1778     if (!reply.ReadUint64(stats)) {
1779         NETNATIVE_LOGE("get stats falil");
1780         return ERR_FLATTEN_OBJECT;
1781     }
1782 
1783     return ERR_NONE;
1784 }
1785 
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)1786 int32_t NetsysNativeServiceProxy::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
1787 {
1788     MessageParcel data;
1789     if (!WriteInterfaceToken(data)) {
1790         return ERR_FLATTEN_OBJECT;
1791     }
1792     if (!data.WriteUint32(type)) {
1793         return ERR_FLATTEN_OBJECT;
1794     }
1795     if (!data.WriteUint32(uid)) {
1796         return ERR_FLATTEN_OBJECT;
1797     }
1798     MessageParcel reply;
1799     MessageOption option;
1800     if (ERR_NONE !=
1801         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_UID_STATS), data, reply, option)) {
1802         NETNATIVE_LOGE("proxy SendRequest failed");
1803         return ERR_FLATTEN_OBJECT;
1804     }
1805 
1806     int32_t ret;
1807     if (!reply.ReadInt32(ret)) {
1808         NETNATIVE_LOGE("get ret falil");
1809         return ERR_FLATTEN_OBJECT;
1810     }
1811     if (ret != ERR_NONE) {
1812         if (ret != STATS_ERR_READ_BPF_FAIL) {
1813             NETNATIVE_LOGE("fail to GetUidStats ret= %{public}d", ret);
1814         }
1815         return ret;
1816     }
1817     if (!reply.ReadUint64(stats)) {
1818         NETNATIVE_LOGE("get stats falil");
1819         return ERR_FLATTEN_OBJECT;
1820     }
1821     return ERR_NONE;
1822 }
1823 
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)1824 int32_t NetsysNativeServiceProxy::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
1825 {
1826     MessageParcel data;
1827     if (!WriteInterfaceToken(data)) {
1828         return ERR_FLATTEN_OBJECT;
1829     }
1830     if (!data.WriteUint32(type)) {
1831         return ERR_FLATTEN_OBJECT;
1832     }
1833     if (!data.WriteString(interfaceName)) {
1834         return ERR_FLATTEN_OBJECT;
1835     }
1836     MessageParcel reply;
1837     MessageOption option;
1838     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_IFACE_STATS), data,
1839                                           reply, option)) {
1840         NETNATIVE_LOGE("proxy SendRequest failed");
1841         return ERR_FLATTEN_OBJECT;
1842     }
1843 
1844     int32_t ret;
1845     if (!reply.ReadInt32(ret)) {
1846         NETNATIVE_LOGE("get ret falil");
1847         return ERR_FLATTEN_OBJECT;
1848     }
1849     if (ret != ERR_NONE) {
1850         if (ret != STATS_ERR_READ_BPF_FAIL) {
1851             NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1852         }
1853         return ret;
1854     }
1855     if (!reply.ReadUint64(stats)) {
1856         NETNATIVE_LOGE("get stats falil");
1857         return ERR_FLATTEN_OBJECT;
1858     }
1859     return ERR_NONE;
1860 }
1861 
GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)1862 int32_t NetsysNativeServiceProxy::GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1863 {
1864     MessageParcel data;
1865     if (!WriteInterfaceToken(data)) {
1866         return ERR_FLATTEN_OBJECT;
1867     }
1868     MessageParcel reply;
1869     MessageOption option;
1870     auto result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_SIM_STATS_INFO),
1871                                         data,
1872                                         reply,
1873                                         option);
1874     if (result != ERR_NONE) {
1875         NETNATIVE_LOGE("proxy SendRequest failed");
1876         return ERR_FLATTEN_OBJECT;
1877     }
1878 
1879     int32_t ret;
1880     if (!reply.ReadInt32(ret)) {
1881         NETNATIVE_LOGE("get ret falil");
1882         return ERR_FLATTEN_OBJECT;
1883     }
1884     if (ret != ERR_NONE) {
1885         NETNATIVE_LOGE("fail to GetAllSimStatsInfo ret= %{public}d", ret);
1886         return ret;
1887     }
1888     if (!OHOS::NetManagerStandard::StatsInfoUnmarshallingVector(reply, stats)) {
1889         NETNATIVE_LOGE("Read stats info failed");
1890         return ERR_FLATTEN_OBJECT;
1891     }
1892 
1893     return ERR_NONE;
1894 }
1895 
DeleteSimStatsInfo(uint32_t uid)1896 int32_t NetsysNativeServiceProxy::DeleteSimStatsInfo(uint32_t uid)
1897 {
1898     MessageParcel data;
1899     if (!WriteInterfaceToken(data)) {
1900         return ERR_FLATTEN_OBJECT;
1901     }
1902     if (!data.WriteUint32(uid)) {
1903         return ERR_FLATTEN_OBJECT;
1904     }
1905     MessageParcel reply;
1906     MessageOption option;
1907     auto result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DELETE_SIM_STATS_INFO),
1908                                         data, reply, option);
1909     if (result != ERR_NONE) {
1910         NETNATIVE_LOGE("proxy SendRequest failed");
1911         return ERR_FLATTEN_OBJECT;
1912     }
1913     int32_t ret;
1914     if (!reply.ReadInt32(ret)) {
1915         NETNATIVE_LOGE("get ret falil");
1916         return ERR_FLATTEN_OBJECT;
1917     }
1918     if (ret != ERR_NONE) {
1919         NETNATIVE_LOGE("fail to DeleteSimStatsInfo ret= %{public}d", ret);
1920         return ret;
1921     }
1922     return ERR_NONE;
1923 }
1924 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)1925 int32_t NetsysNativeServiceProxy::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1926 {
1927     MessageParcel data;
1928     if (!WriteInterfaceToken(data)) {
1929         return ERR_FLATTEN_OBJECT;
1930     }
1931     MessageParcel reply;
1932     MessageOption option;
1933     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_STATS_INFO), data,
1934                                           reply, option)) {
1935         NETNATIVE_LOGE("proxy SendRequest failed");
1936         return ERR_FLATTEN_OBJECT;
1937     }
1938 
1939     int32_t ret;
1940     if (!reply.ReadInt32(ret)) {
1941         NETNATIVE_LOGE("get ret falil");
1942         return ERR_FLATTEN_OBJECT;
1943     }
1944     if (ret != ERR_NONE) {
1945         NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1946         return ret;
1947     }
1948     if (!OHOS::NetManagerStandard::StatsInfoUnmarshallingVector(reply, stats)) {
1949         NETNATIVE_LOGE("Read stats info failed");
1950         return ERR_FLATTEN_OBJECT;
1951     }
1952 
1953     return ERR_NONE;
1954 }
1955 
DeleteStatsInfo(uint32_t uid)1956 int32_t NetsysNativeServiceProxy::DeleteStatsInfo(uint32_t uid)
1957 {
1958     MessageParcel data;
1959     if (!WriteInterfaceToken(data)) {
1960         return ERR_FLATTEN_OBJECT;
1961     }
1962     if (!data.WriteUint32(uid)) {
1963         return ERR_FLATTEN_OBJECT;
1964     }
1965     MessageParcel reply;
1966     MessageOption option;
1967     auto result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DELETE_STATS_INFO), data,
1968                                         reply, option);
1969     if (result != ERR_NONE) {
1970         NETNATIVE_LOGE("proxy SendRequest failed");
1971         return ERR_FLATTEN_OBJECT;
1972     }
1973     int32_t ret;
1974     if (!reply.ReadInt32(ret)) {
1975         NETNATIVE_LOGE("get ret falil");
1976         return ERR_FLATTEN_OBJECT;
1977     }
1978     if (ret != ERR_NONE) {
1979         NETNATIVE_LOGE("fail to DeleteStatsInfo ret= %{public}d", ret);
1980         return ret;
1981     }
1982     return ERR_NONE;
1983 }
1984 
SetIptablesCommandForRes(const std::string & cmd,std::string & respond,IptablesType ipType)1985 int32_t NetsysNativeServiceProxy::SetIptablesCommandForRes(const std::string &cmd, std::string &respond,
1986                                                            IptablesType ipType)
1987 {
1988     MessageParcel data;
1989     if (!WriteInterfaceToken(data)) {
1990         return ERR_FLATTEN_OBJECT;
1991     }
1992     if (!data.WriteString(cmd)) {
1993         return ERR_FLATTEN_OBJECT;
1994     }
1995     if (!data.WriteUint32(ipType)) {
1996         return ERR_FLATTEN_OBJECT;
1997     }
1998     MessageParcel reply;
1999     MessageOption option;
2000     if (Remote() == nullptr) {
2001         NETNATIVE_LOGE("SetIptablesCommandForRes Remote pointer is null");
2002         return ERR_FLATTEN_OBJECT;
2003     }
2004     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES),
2005                                           data, reply, option);
2006     if (error != ERR_NONE) {
2007         NETNATIVE_LOGE("SetIptablesCommandForRes proxy SendRequest failed");
2008         return ERR_FLATTEN_OBJECT;
2009     }
2010     int32_t ret;
2011     if (!reply.ReadInt32(ret)) {
2012         NETNATIVE_LOGE("SetIptablesCommandForRes proxy read ret failed");
2013         return ERR_FLATTEN_OBJECT;
2014     }
2015     if (ret == ERR_NONE) {
2016         if (!reply.ReadString(respond)) {
2017             NETNATIVE_LOGE("SetIptablesCommandForRes proxy read respond failed");
2018             return ERR_FLATTEN_OBJECT;
2019         }
2020     }
2021     return ret;
2022 }
2023 
DealBandwidth(uint32_t uid,uint32_t code)2024 int32_t NetsysNativeServiceProxy::DealBandwidth(uint32_t uid, uint32_t code)
2025 {
2026     MessageParcel data;
2027     if (!WriteInterfaceToken(data)) {
2028         NETNATIVE_LOGE("WriteInterfaceToken failed");
2029         return ERR_FLATTEN_OBJECT;
2030     }
2031     if (!data.WriteUint32(uid)) {
2032         NETNATIVE_LOGE("WriteUint32 failed");
2033         return ERR_FLATTEN_OBJECT;
2034     }
2035 
2036     MessageParcel reply;
2037     MessageOption option;
2038     auto remote = Remote();
2039     if (remote == nullptr) {
2040         return IPC_PROXY_NULL_INVOKER_ERR;
2041     }
2042     int32_t error = remote->SendRequest(code, data, reply, option);
2043     if (error != ERR_NONE) {
2044         NETNATIVE_LOGE("proxy SendRequest failed");
2045         return ERR_FLATTEN_OBJECT;
2046     }
2047 
2048     return reply.ReadInt32();
2049 }
2050 
NetDiagPingHost(const NetDiagPingOption & pingOption,const sptr<INetDiagCallback> & callback)2051 int32_t NetsysNativeServiceProxy::NetDiagPingHost(const NetDiagPingOption &pingOption,
2052                                                   const sptr<INetDiagCallback> &callback)
2053 {
2054     NETNATIVE_LOGI("Begin to NetDiagPingHost");
2055     if (callback == nullptr) {
2056         NETNATIVE_LOGE("The parameter of callback is nullptr");
2057         return ERR_NULL_OBJECT;
2058     }
2059 
2060     MessageParcel data;
2061     if (!WriteInterfaceToken(data)) {
2062         return ERR_FLATTEN_OBJECT;
2063     }
2064     if (!pingOption.Marshalling(data)) {
2065         return ERR_FLATTEN_OBJECT;
2066     }
2067     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2068         return ERR_FLATTEN_OBJECT;
2069     }
2070 
2071     sptr<IRemoteObject> remote = Remote();
2072     if (remote == nullptr) {
2073         NETNATIVE_LOGE("Remote is null");
2074         return ERR_FLATTEN_OBJECT;
2075     }
2076     MessageParcel reply;
2077     MessageOption option;
2078     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_PING_HOST),
2079         data, reply, option);
2080     if (result != ERR_NONE) {
2081         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2082         return IPC_INVOKER_ERR;
2083     }
2084     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2085     if (!reply.ReadInt32(ret)) {
2086         NETNATIVE_LOGE("Read result failed");
2087         return ERR_FLATTEN_OBJECT;
2088     }
2089     return ret;
2090 }
2091 
NetDiagGetRouteTable(std::list<NetDiagRouteTable> & routeTables)2092 int32_t NetsysNativeServiceProxy::NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables)
2093 {
2094     NETNATIVE_LOGI("Begin to NetDiagGetRouteTable");
2095     MessageParcel data;
2096     if (!WriteInterfaceToken(data)) {
2097         return ERR_FLATTEN_OBJECT;
2098     }
2099     sptr<IRemoteObject> remote = Remote();
2100     if (remote == nullptr) {
2101         NETNATIVE_LOGE("Remote is null");
2102         return ERR_FLATTEN_OBJECT;
2103     }
2104 
2105     MessageParcel reply;
2106     MessageOption option;
2107     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_ROUTE_TABLE),
2108         data, reply, option);
2109     if (result != ERR_NONE) {
2110         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2111         return IPC_INVOKER_ERR;
2112     }
2113     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2114     if (!reply.ReadInt32(ret)) {
2115         NETNATIVE_LOGE("Read result failed");
2116         return ERR_FLATTEN_OBJECT;
2117     }
2118 
2119     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
2120         uint32_t size = 0;
2121         if (!reply.ReadUint32(size)) {
2122             NETNATIVE_LOGE("Read uint32 failed");
2123             return ERR_FLATTEN_OBJECT;
2124         }
2125 
2126         for (uint32_t i = 0; i < size; ++i) {
2127             NetDiagRouteTable routeTable;
2128             if (!NetDiagRouteTable::Unmarshalling(reply, routeTable)) {
2129                 NETNATIVE_LOGE("NetDiagRouteTable unmarshalling failed.");
2130                 return ERR_FLATTEN_OBJECT;
2131             }
2132             routeTables.push_back(routeTable);
2133         }
2134     }
2135     return ret;
2136 }
2137 
NetDiagGetSocketsInfo(NetDiagProtocolType socketType,NetDiagSocketsInfo & socketsInfo)2138 int32_t NetsysNativeServiceProxy::NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo)
2139 {
2140     NETNATIVE_LOGI("Begin to NetDiagGetSocketsInfo");
2141     MessageParcel data;
2142     if (!WriteInterfaceToken(data)) {
2143         return ERR_FLATTEN_OBJECT;
2144     }
2145     if (!data.WriteUint8(static_cast<uint8_t>(socketType))) {
2146         return ERR_FLATTEN_OBJECT;
2147     }
2148 
2149     sptr<IRemoteObject> remote = Remote();
2150     if (remote == nullptr) {
2151         NETNATIVE_LOGE("Remote is null");
2152         return ERR_FLATTEN_OBJECT;
2153     }
2154     MessageParcel reply;
2155     MessageOption option;
2156     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_SOCKETS_INFO),
2157         data, reply, option);
2158     if (result != ERR_NONE) {
2159         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2160         return IPC_INVOKER_ERR;
2161     }
2162     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2163     if (!reply.ReadInt32(ret)) {
2164         NETNATIVE_LOGE("Read result failed");
2165         return ERR_FLATTEN_OBJECT;
2166     }
2167 
2168     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
2169         if (!NetDiagSocketsInfo::Unmarshalling(reply, socketsInfo)) {
2170             NETNATIVE_LOGE("NetDiagSocketsInfo Unmarshalling failed.");
2171             return ERR_FLATTEN_OBJECT;
2172         }
2173     }
2174     return ret;
2175 }
2176 
NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> & configs,const std::string & ifaceName)2177 int32_t NetsysNativeServiceProxy::NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs,
2178                                                             const std::string &ifaceName)
2179 {
2180     NETNATIVE_LOGI("Begin to NetDiagGetInterfaceConfig");
2181     MessageParcel data;
2182     if (!WriteInterfaceToken(data)) {
2183         return ERR_FLATTEN_OBJECT;
2184     }
2185     if (!data.WriteString(ifaceName)) {
2186         return ERR_FLATTEN_OBJECT;
2187     }
2188 
2189     sptr<IRemoteObject> remote = Remote();
2190     if (remote == nullptr) {
2191         NETNATIVE_LOGE("Remote is null");
2192         return ERR_FLATTEN_OBJECT;
2193     }
2194     MessageParcel reply;
2195     MessageOption option;
2196     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_IFACE_CONFIG),
2197         data, reply, option);
2198     if (result != ERR_NONE) {
2199         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2200         return IPC_INVOKER_ERR;
2201     }
2202     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2203     if (!reply.ReadInt32(ret)) {
2204         NETNATIVE_LOGE("Read result failed");
2205         return ERR_FLATTEN_OBJECT;
2206     }
2207 
2208     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
2209         uint32_t size = 0;
2210         if (!reply.ReadUint32(size)) {
2211             NETNATIVE_LOGE("Read uint32 failed");
2212             return ERR_FLATTEN_OBJECT;
2213         }
2214 
2215         for (uint32_t i = 0; i < size; ++i) {
2216             NetDiagIfaceConfig ifaceConfig;
2217             if (!NetDiagIfaceConfig::Unmarshalling(reply, ifaceConfig)) {
2218                 NETNATIVE_LOGE("NetDiagIfaceConfig Unmarshalling failed.");
2219                 return ERR_FLATTEN_OBJECT;
2220             }
2221             configs.push_back(ifaceConfig);
2222         }
2223     }
2224     return ret;
2225 }
2226 
NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig & config,const std::string & ifaceName,bool add)2227 int32_t NetsysNativeServiceProxy::NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config,
2228                                                                const std::string &ifaceName, bool add)
2229 {
2230     NETNATIVE_LOGI("Begin to NetDiagUpdateInterfaceConfig");
2231     MessageParcel data;
2232     if (!WriteInterfaceToken(data)) {
2233         return ERR_FLATTEN_OBJECT;
2234     }
2235     if (!config.Marshalling(data)) {
2236         return ERR_FLATTEN_OBJECT;
2237     }
2238     if (!data.WriteString(ifaceName)) {
2239         return ERR_FLATTEN_OBJECT;
2240     }
2241     if (!data.WriteBool(add)) {
2242         return ERR_FLATTEN_OBJECT;
2243     }
2244 
2245     sptr<IRemoteObject> remote = Remote();
2246     if (remote == nullptr) {
2247         NETNATIVE_LOGE("Remote is null");
2248         return ERR_FLATTEN_OBJECT;
2249     }
2250     MessageParcel reply;
2251     MessageOption option;
2252     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_UPDATE_IFACE_CONFIG),
2253         data, reply, option);
2254     if (result != ERR_NONE) {
2255         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2256         return IPC_INVOKER_ERR;
2257     }
2258     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2259     if (!reply.ReadInt32(ret)) {
2260         NETNATIVE_LOGE("Read result failed");
2261         return ERR_FLATTEN_OBJECT;
2262     }
2263     return ret;
2264 }
2265 
NetDiagSetInterfaceActiveState(const std::string & ifaceName,bool up)2266 int32_t NetsysNativeServiceProxy::NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)
2267 {
2268     NETNATIVE_LOGI("Begin to NetDiagSetInterfaceActiveState");
2269     MessageParcel data;
2270     if (!WriteInterfaceToken(data)) {
2271         return ERR_FLATTEN_OBJECT;
2272     }
2273     if (!data.WriteString(ifaceName)) {
2274         return ERR_FLATTEN_OBJECT;
2275     }
2276     if (!data.WriteBool(up)) {
2277         return ERR_FLATTEN_OBJECT;
2278     }
2279 
2280     sptr<IRemoteObject> remote = Remote();
2281     if (remote == nullptr) {
2282         NETNATIVE_LOGE("Remote is null");
2283         return ERR_FLATTEN_OBJECT;
2284     }
2285     MessageParcel reply;
2286     MessageOption option;
2287     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_SET_IFACE_ACTIVE_STATE),
2288         data, reply, option);
2289     if (result != ERR_NONE) {
2290         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2291         return IPC_INVOKER_ERR;
2292     }
2293     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2294     if (!reply.ReadInt32(ret)) {
2295         NETNATIVE_LOGE("Read result failed");
2296         return ERR_FLATTEN_OBJECT;
2297     }
2298     return ret;
2299 }
2300 
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)2301 int32_t NetsysNativeServiceProxy::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
2302                                                const std::string &ifName)
2303 {
2304     MessageParcel data;
2305     if (!WriteInterfaceToken(data) || !data.WriteString(ipAddr) || !data.WriteString(macAddr) ||
2306         !data.WriteString(ifName)) {
2307         return ERR_FLATTEN_OBJECT;
2308     }
2309 
2310     MessageParcel reply;
2311     MessageOption option;
2312     sptr<IRemoteObject> remote = Remote();
2313     if (remote == nullptr) {
2314         NETNATIVE_LOGE("AddStaticArp Remote is null");
2315         return ERR_FLATTEN_OBJECT;
2316     }
2317     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ADD_STATIC_ARP),
2318                                         data, reply, option);
2319     if (error != ERR_NONE) {
2320         NETNATIVE_LOGE("AddStaticArp proxy SendRequest failed");
2321         return ERR_FLATTEN_OBJECT;
2322     }
2323     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2324     if (!reply.ReadInt32(ret)) {
2325         NETNATIVE_LOGE("AddStaticArp proxy read ret failed");
2326         return ERR_FLATTEN_OBJECT;
2327     }
2328     return ret;
2329 }
2330 
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)2331 int32_t NetsysNativeServiceProxy::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
2332                                                const std::string &ifName)
2333 {
2334     MessageParcel data;
2335     if (!WriteInterfaceToken(data) || !data.WriteString(ipAddr) || !data.WriteString(macAddr) ||
2336         !data.WriteString(ifName)) {
2337         return ERR_FLATTEN_OBJECT;
2338     }
2339 
2340     MessageParcel reply;
2341     MessageOption option;
2342     sptr<IRemoteObject> remote = Remote();
2343     if (remote == nullptr) {
2344         NETNATIVE_LOGE("DelStaticArp Remote is null");
2345         return ERR_FLATTEN_OBJECT;
2346     }
2347     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_STATIC_ARP),
2348                                         data, reply, option);
2349     if (error != ERR_NONE) {
2350         NETNATIVE_LOGE("DelStaticArp proxy SendRequest failed");
2351         return ERR_FLATTEN_OBJECT;
2352     }
2353     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2354     if (!reply.ReadInt32(ret)) {
2355         NETNATIVE_LOGE("DelStaticArp proxy read ret failed");
2356         return ERR_FLATTEN_OBJECT;
2357     }
2358     return ret;
2359 }
2360 
RegisterDnsResultCallback(const sptr<OHOS::NetsysNative::INetDnsResultCallback> & callback,uint32_t timeStep)2361 int32_t NetsysNativeServiceProxy::RegisterDnsResultCallback(
2362     const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep)
2363 {
2364     NETNATIVE_LOGI("Begin to RegisterDnsResultCallback");
2365     if (callback == nullptr) {
2366         NETNATIVE_LOGE("INetDnsResultCallback is nullptr");
2367         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2368     }
2369     MessageParcel data;
2370     if (!WriteInterfaceToken(data)) {
2371         return ERR_FLATTEN_OBJECT;
2372     }
2373     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2374         return ERR_FLATTEN_OBJECT;
2375     }
2376     if (!data.WriteUint32(timeStep)) {
2377         return ERR_FLATTEN_OBJECT;
2378     }
2379 
2380     sptr<IRemoteObject> remote = Remote();
2381     if (remote == nullptr) {
2382         NETNATIVE_LOGE("Remote is null");
2383         return ERR_FLATTEN_OBJECT;
2384     }
2385     MessageParcel reply;
2386     MessageOption option;
2387     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_RESULT_LISTENER),
2388         data, reply, option);
2389     if (result != ERR_NONE) {
2390         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2391         return IPC_INVOKER_ERR;
2392     }
2393     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2394     if (!reply.ReadInt32(ret)) {
2395         NETNATIVE_LOGE("Read result failed");
2396         return ERR_FLATTEN_OBJECT;
2397     }
2398     return ret;
2399 }
2400 
UnregisterDnsResultCallback(const sptr<OHOS::NetsysNative::INetDnsResultCallback> & callback)2401 int32_t NetsysNativeServiceProxy::UnregisterDnsResultCallback(
2402     const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback)
2403 {
2404     NETNATIVE_LOGI("Begin to UnregisterDnsResultCallback");
2405     if (callback == nullptr) {
2406         NETNATIVE_LOGE("INetDnsResultCallback is nullptr");
2407         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2408     }
2409     MessageParcel data;
2410     if (!WriteInterfaceToken(data)) {
2411         return ERR_FLATTEN_OBJECT;
2412     }
2413     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2414         return ERR_FLATTEN_OBJECT;
2415     }
2416 
2417     sptr<IRemoteObject> remote = Remote();
2418     if (remote == nullptr) {
2419         NETNATIVE_LOGE("Remote is null");
2420         return ERR_FLATTEN_OBJECT;
2421     }
2422     MessageParcel reply;
2423     MessageOption option;
2424     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_RESULT_LISTENER),
2425         data, reply, option);
2426     if (result != ERR_NONE) {
2427         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2428         return IPC_INVOKER_ERR;
2429     }
2430     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2431     if (!reply.ReadInt32(ret)) {
2432         NETNATIVE_LOGE("Read result failed");
2433         return ERR_FLATTEN_OBJECT;
2434     }
2435     return ret;
2436 }
2437 
RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)2438 int32_t NetsysNativeServiceProxy::RegisterDnsHealthCallback(
2439     const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
2440 {
2441     NETNATIVE_LOGI("Begin to RegisterDnsHealthCallback");
2442     if (callback == nullptr) {
2443         NETNATIVE_LOGE("INetDnsHealthCallback is nullptr");
2444         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2445     }
2446     MessageParcel data;
2447     if (!WriteInterfaceToken(data)) {
2448         return ERR_FLATTEN_OBJECT;
2449     }
2450     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2451         return ERR_FLATTEN_OBJECT;
2452     }
2453 
2454     sptr<IRemoteObject> remote = Remote();
2455     if (remote == nullptr) {
2456         NETNATIVE_LOGE("Remote is null");
2457         return ERR_FLATTEN_OBJECT;
2458     }
2459     MessageParcel reply;
2460     MessageOption option;
2461     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_HEALTH_LISTENER),
2462         data, reply, option);
2463     if (result != ERR_NONE) {
2464         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2465         return IPC_INVOKER_ERR;
2466     }
2467     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2468     if (!reply.ReadInt32(ret)) {
2469         NETNATIVE_LOGE("Read result failed");
2470         return ERR_FLATTEN_OBJECT;
2471     }
2472     return ret;
2473 }
2474 
UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)2475 int32_t NetsysNativeServiceProxy::UnregisterDnsHealthCallback(
2476     const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
2477 {
2478     NETNATIVE_LOGI("Begin to UnregisterDnsHealthCallback");
2479     if (callback == nullptr) {
2480         NETNATIVE_LOGE("INetDnsHealthCallback is nullptr");
2481         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2482     }
2483     MessageParcel data;
2484     if (!WriteInterfaceToken(data)) {
2485         return ERR_FLATTEN_OBJECT;
2486     }
2487     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2488         return ERR_FLATTEN_OBJECT;
2489     }
2490 
2491     sptr<IRemoteObject> remote = Remote();
2492     if (remote == nullptr) {
2493         NETNATIVE_LOGE("Remote is null");
2494         return ERR_FLATTEN_OBJECT;
2495     }
2496     MessageParcel reply;
2497     MessageOption option;
2498     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_HEALTH_LISTENER),
2499         data, reply, option);
2500     if (result != ERR_NONE) {
2501         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2502         return IPC_INVOKER_ERR;
2503     }
2504     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2505     if (!reply.ReadInt32(ret)) {
2506         NETNATIVE_LOGE("Read result failed");
2507         return ERR_FLATTEN_OBJECT;
2508     }
2509     return ret;
2510 }
2511 
GetCookieStats(uint64_t & stats,uint32_t type,uint64_t cookie)2512 int32_t NetsysNativeServiceProxy::GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)
2513 {
2514     MessageParcel data;
2515     if (!WriteInterfaceToken(data)) {
2516         return ERR_FLATTEN_OBJECT;
2517     }
2518     if (!data.WriteUint32(type)) {
2519         return ERR_FLATTEN_OBJECT;
2520     }
2521     if (!data.WriteUint64(cookie)) {
2522         return ERR_FLATTEN_OBJECT;
2523     }
2524     MessageParcel reply;
2525     MessageOption option;
2526     int32_t res = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_COOKIE_STATS),
2527                                         data, reply, option);
2528     if (res != ERR_NONE) {
2529         NETNATIVE_LOGE("GetCookieStats SendRequest failed");
2530         return ERR_FLATTEN_OBJECT;
2531     }
2532 
2533     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2534     if (!reply.ReadInt32(ret)) {
2535         NETNATIVE_LOGE("get ret falil");
2536         return ERR_FLATTEN_OBJECT;
2537     }
2538 
2539     if (!reply.ReadUint64(stats)) {
2540         NETNATIVE_LOGE("get stats falil");
2541         return ERR_FLATTEN_OBJECT;
2542     }
2543     return ret;
2544 }
2545 
GetNetworkSharingType(std::set<uint32_t> & sharingTypeIsOn)2546 int32_t NetsysNativeServiceProxy::GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn)
2547 {
2548     NETNATIVE_LOGI("GetNetworkSharingType in");
2549     MessageParcel data;
2550     if (!WriteInterfaceToken(data)) {
2551         return ERR_FLATTEN_OBJECT;
2552     }
2553     MessageParcel reply;
2554     MessageOption option;
2555     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_NETWORK_SHARING_TYPE),
2556                                         data, reply, option);
2557     if (ret != ERR_NONE) {
2558         NETNATIVE_LOGE("GetNetworkSharingType SendRequest failed");
2559         return ERR_FLATTEN_OBJECT;
2560     }
2561 
2562     ret = NetManagerStandard::NETMANAGER_SUCCESS;
2563     if (!reply.ReadInt32(ret)) {
2564         NETNATIVE_LOGE("get ret falil");
2565         return ERR_FLATTEN_OBJECT;
2566     }
2567 
2568     uint32_t count = ERR_NONE;
2569     if (!reply.ReadUint32(count)) {
2570         NETNATIVE_LOGE("get ret falil");
2571         return ERR_FLATTEN_OBJECT;
2572     }
2573     uint32_t tmp = ERR_NONE;
2574     for (size_t index = 0; index < count; ++index) {
2575         if (!reply.ReadUint32(tmp)) {
2576             NETNATIVE_LOGE("GetNetworkSharingType falil");
2577             return ERR_FLATTEN_OBJECT;
2578         }
2579         sharingTypeIsOn.insert(tmp);
2580     }
2581 
2582     return ret;
2583 }
2584 
UpdateNetworkSharingType(uint32_t type,bool isOpen)2585 int32_t NetsysNativeServiceProxy::UpdateNetworkSharingType(uint32_t type, bool isOpen)
2586 {
2587     NETNATIVE_LOGI("UpdateNetworkSharingType");
2588     MessageParcel data;
2589     if (!WriteInterfaceToken(data)) {
2590         return ERR_FLATTEN_OBJECT;
2591     }
2592     if (!data.WriteUint32(type)) {
2593         return ERR_FLATTEN_OBJECT;
2594     }
2595     if (!data.WriteBool(isOpen)) {
2596         return ERR_FLATTEN_OBJECT;
2597     }
2598     MessageParcel reply;
2599     MessageOption option;
2600     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UPDATE_NETWORK_SHARING_TYPE),
2601                                         data, reply, option);
2602     if (ret != ERR_NONE) {
2603         NETNATIVE_LOGE("UpdateNetworkSharingType SendRequest failed");
2604         return ERR_FLATTEN_OBJECT;
2605     }
2606 
2607     ret = NetManagerStandard::NETMANAGER_SUCCESS;
2608     if (!reply.ReadInt32(ret)) {
2609         NETNATIVE_LOGE("UpdateNetworkSharingType get ret falil");
2610         return ERR_FLATTEN_OBJECT;
2611     }
2612 
2613     return ret;
2614 }
2615 
2616 #ifdef FEATURE_NET_FIREWALL_ENABLE
SetFirewallRules(NetFirewallRuleType type,const std::vector<sptr<NetFirewallBaseRule>> & ruleList,bool isFinish)2617 int32_t NetsysNativeServiceProxy::SetFirewallRules(NetFirewallRuleType type,
2618                                                    const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
2619                                                    bool isFinish)
2620 {
2621     NETNATIVE_LOGI("NetsysNativeServiceProxy::SetFirewallRules: ruleList size=%{public}zu type=%{public}d",
2622                    ruleList.size(), type);
2623     auto it = ruleList.begin();
2624     uint32_t pageSize = type == NetFirewallRuleType::RULE_IP ? FIREWALL_IPC_IP_RULE_PAGE_SIZE : FIREWALL_RULE_SIZE_MAX;
2625     uint32_t offset = 0;
2626     uint32_t remain;
2627     while (it != ruleList.end()) {
2628         remain = ruleList.end() - it;
2629         offset = std::min(remain, pageSize);
2630         MessageParcel data;
2631         if (!WriteInterfaceToken(data)) {
2632             return ERR_FLATTEN_OBJECT;
2633         }
2634         if (!data.WriteInt32(static_cast<int32_t>(type))) {
2635             return ERR_FLATTEN_OBJECT;
2636         }
2637         if (!data.WriteUint32(offset)) {
2638             return ERR_FLATTEN_OBJECT;
2639         }
2640         if (!data.WriteBool(offset == remain)) {
2641             return ERR_FLATTEN_OBJECT;
2642         }
2643         for (uint32_t i = 0; i < offset && it != ruleList.end(); i++, it++) {
2644             if (!(*it)->Marshalling(data)) {
2645                 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
2646             }
2647         }
2648         MessageParcel reply;
2649         MessageOption option;
2650         int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_RULES),
2651                                             data, reply, option);
2652         if (ret != ERR_NONE) {
2653             NETNATIVE_LOGE("SetFirewallRules SendRequest failed");
2654             return ret;
2655         }
2656     }
2657 
2658     return NetManagerStandard::NETMANAGER_SUCCESS;
2659 }
2660 
SetFirewallDefaultAction(FirewallRuleAction inDefault,FirewallRuleAction outDefault)2661 int32_t NetsysNativeServiceProxy::SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)
2662 {
2663     NETNATIVE_LOGI("NetsysNativeServiceProxy::SetFirewallDefaultAction in=%{public}d out=%{public}d", inDefault,
2664                    outDefault);
2665     MessageParcel data;
2666     if (!WriteInterfaceToken(data)) {
2667         return ERR_FLATTEN_OBJECT;
2668     }
2669     if (!data.WriteInt32(static_cast<int32_t>(inDefault))) {
2670         return ERR_FLATTEN_OBJECT;
2671     }
2672     if (!data.WriteInt32(static_cast<int32_t>(outDefault))) {
2673         return ERR_FLATTEN_OBJECT;
2674     }
2675     MessageParcel reply;
2676     MessageOption option;
2677     int32_t ret = Remote()->SendRequest(
2678         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_DEFAULT_ACTION), data, reply, option);
2679     if (ret != ERR_NONE) {
2680         NETNATIVE_LOGE("SetFirewallDefaultAction SendRequest failed");
2681         return ret;
2682     }
2683 
2684     return NetManagerStandard::NETMANAGER_SUCCESS;
2685 }
2686 
SetFirewallCurrentUserId(int32_t userId)2687 int32_t NetsysNativeServiceProxy::SetFirewallCurrentUserId(int32_t userId)
2688 {
2689     NETNATIVE_LOGI("NetsysNativeServiceProxy::SetFirewallCurrentUserId userId=%{public}d", userId);
2690     MessageParcel data;
2691     if (!WriteInterfaceToken(data)) {
2692         return ERR_FLATTEN_OBJECT;
2693     }
2694     if (!data.WriteInt32(userId)) {
2695         return ERR_FLATTEN_OBJECT;
2696     }
2697     MessageParcel reply;
2698     MessageOption option;
2699     int32_t ret = Remote()->SendRequest(
2700         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_USER_ID), data, reply, option);
2701     if (ret != ERR_NONE) {
2702         NETNATIVE_LOGE("SetFirewallCurrentUserId SendRequest failed");
2703         return ret;
2704     }
2705 
2706     return NetManagerStandard::NETMANAGER_SUCCESS;
2707 }
2708 
ClearFirewallRules(NetFirewallRuleType type)2709 int32_t NetsysNativeServiceProxy::ClearFirewallRules(NetFirewallRuleType type)
2710 {
2711     NETNATIVE_LOGI("NetsysNativeServiceProxy::ClearFirewallRules type=%{public}d", type);
2712     MessageParcel data;
2713     if (!WriteInterfaceToken(data)) {
2714         return ERR_FLATTEN_OBJECT;
2715     }
2716     if (!data.WriteInt32(static_cast<int32_t>(type))) {
2717         return ERR_FLATTEN_OBJECT;
2718     }
2719     MessageParcel reply;
2720     MessageOption option;
2721     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_CLEAR_RULES),
2722         data, reply, option);
2723     if (ret != ERR_NONE) {
2724         NETNATIVE_LOGE("ClearFirewallRules SendRequest failed");
2725         return ret;
2726     }
2727 
2728     return NetManagerStandard::NETMANAGER_SUCCESS;
2729 }
2730 
RegisterNetFirewallCallback(const sptr<INetFirewallCallback> & callback)2731 int32_t NetsysNativeServiceProxy::RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)
2732 {
2733     NETNATIVE_LOGI("Begin to RegisterFirewallCallback");
2734     if (callback == nullptr) {
2735         NETNATIVE_LOGE("FirewallCallback is nullptr");
2736         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2737     }
2738     MessageParcel data;
2739     if (!WriteInterfaceToken(data)) {
2740         return ERR_FLATTEN_OBJECT;
2741     }
2742     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2743         return ERR_FLATTEN_OBJECT;
2744     }
2745     MessageParcel reply;
2746     MessageOption option;
2747     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_REGISTER), data,
2748         reply, option);
2749     if (ret != ERR_NONE) {
2750         NETNATIVE_LOGE("RegisterFirewallCallback SendRequest failed");
2751         return ret;
2752     }
2753 
2754     return NetManagerStandard::NETMANAGER_SUCCESS;
2755 }
2756 
UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> & callback)2757 int32_t NetsysNativeServiceProxy::UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)
2758 {
2759     NETNATIVE_LOGI("Begin to UnRegisterNetFirewallCallback");
2760     if (callback == nullptr) {
2761         NETNATIVE_LOGE("FirewallCallback is nullptr");
2762         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2763     }
2764     MessageParcel data;
2765     if (!WriteInterfaceToken(data)) {
2766         return ERR_FLATTEN_OBJECT;
2767     }
2768     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2769         return ERR_FLATTEN_OBJECT;
2770     }
2771     MessageParcel reply;
2772     MessageOption option;
2773     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_UNREGISTER),
2774         data, reply, option);
2775     if (ret != ERR_NONE) {
2776         NETNATIVE_LOGE("UnRegisterNetFirewallCallback SendRequest failed");
2777         return ret;
2778     }
2779 
2780     return NetManagerStandard::NETMANAGER_SUCCESS;
2781 }
2782 #endif
2783 
SetIpv6PrivacyExtensions(const std::string & interfaceName,const uint32_t on)2784 int32_t NetsysNativeServiceProxy::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
2785 {
2786     NETNATIVE_LOGI("Begin to SetIpv6PrivacyExtensions");
2787     MessageParcel data;
2788     if (!WriteInterfaceToken(data)) {
2789         return ERR_FLATTEN_OBJECT;
2790     }
2791     if (!data.WriteString(interfaceName)) {
2792         return ERR_FLATTEN_OBJECT;
2793     }
2794     if (!data.WriteUint32(on)) {
2795         return ERR_FLATTEN_OBJECT;
2796     }
2797 
2798     MessageParcel reply;
2799     MessageOption option;
2800     int result =
2801         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_IPV6_PRIVCAY_EXTENSION),
2802             data, reply, option);
2803     if (result != ERR_NONE) {
2804         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2805         return IPC_INVOKER_ERR;
2806     }
2807     return reply.ReadInt32();
2808 }
2809 
SetEnableIpv6(const std::string & interfaceName,const uint32_t on)2810 int32_t NetsysNativeServiceProxy::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
2811 {
2812     NETNATIVE_LOGI("Begin to SetEnableIpv6");
2813     MessageParcel data;
2814     if (!WriteInterfaceToken(data)) {
2815         return ERR_FLATTEN_OBJECT;
2816     }
2817     if (!data.WriteString(interfaceName)) {
2818         return ERR_FLATTEN_OBJECT;
2819     }
2820     if (!data.WriteUint32(on)) {
2821         return ERR_FLATTEN_OBJECT;
2822     }
2823 
2824     MessageParcel reply;
2825     MessageOption option;
2826     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ENABLE_IPV6),
2827         data, reply, option);
2828     if (result != ERR_NONE) {
2829         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2830         return IPC_INVOKER_ERR;
2831     }
2832     return reply.ReadInt32();
2833 }
2834 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag,bool isBroker)2835 int32_t NetsysNativeServiceProxy::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
2836                                                          bool isBroker)
2837 {
2838     NETNATIVE_LOGI("SetNetworkAccessPolicy");
2839     MessageParcel data;
2840     if (!WriteInterfaceToken(data)) {
2841         return ERR_FLATTEN_OBJECT;
2842     }
2843 
2844     if (!data.WriteUint32(uid)) {
2845         return ERR_FLATTEN_OBJECT;
2846     }
2847 
2848     if (!data.WriteUint8(policy.wifiAllow)) {
2849         return ERR_FLATTEN_OBJECT;
2850     }
2851 
2852     if (!data.WriteUint8(policy.cellularAllow)) {
2853         return ERR_FLATTEN_OBJECT;
2854     }
2855 
2856     if (!data.WriteBool(reconfirmFlag)) {
2857         return ERR_FLATTEN_OBJECT;
2858     }
2859 
2860     if (!data.WriteBool(isBroker)) {
2861         return ERR_FLATTEN_OBJECT;
2862     }
2863     MessageParcel reply;
2864     MessageOption option;
2865     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_NETWORK_ACCESS_POLICY),
2866         data, reply, option);
2867     if (result != ERR_NONE) {
2868         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2869         return IPC_INVOKER_ERR;
2870     }
2871     return reply.ReadInt32();
2872 }
2873 
DeleteNetworkAccessPolicy(uint32_t uid)2874 int32_t NetsysNativeServiceProxy::DeleteNetworkAccessPolicy(uint32_t uid)
2875 {
2876     NETNATIVE_LOGI("DeleteNetworkAccessPolicy");
2877     MessageParcel data;
2878 
2879     if (!WriteInterfaceToken(data)) {
2880         return ERR_FLATTEN_OBJECT;
2881     }
2882 
2883     if (!data.WriteUint32(uid)) {
2884         return ERR_FLATTEN_OBJECT;
2885     }
2886     MessageParcel reply;
2887     MessageOption option;
2888     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_NETWORK_ACCESS_POLICY),
2889         data, reply, option);
2890     if (result != ERR_NONE) {
2891         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2892         return IPC_INVOKER_ERR;
2893     }
2894     return reply.ReadInt32();
2895 }
2896 
ClearFirewallAllRules()2897 int32_t NetsysNativeServiceProxy::ClearFirewallAllRules()
2898 {
2899     NETNATIVE_LOGI("ClearFirewallAllRules");
2900     MessageParcel data;
2901     if (!WriteInterfaceToken(data)) {
2902         return ERR_FLATTEN_OBJECT;
2903     }
2904 
2905     MessageParcel reply;
2906     MessageOption option;
2907     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CLEAR_FIREWALL_RULE), data, reply,
2908                           option);
2909 
2910     return reply.ReadInt32();
2911 }
2912 
NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)2913 int32_t NetsysNativeServiceProxy::NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)
2914 {
2915     NETNATIVE_LOGI("NotifyNetBearerTypeChange");
2916     MessageParcel data;
2917 
2918     if (!WriteInterfaceToken(data)) {
2919         return ERR_FLATTEN_OBJECT;
2920     }
2921 
2922     uint32_t size = static_cast<uint32_t>(bearerTypes.size());
2923     if (!data.WriteUint32(size)) {
2924         return ERR_FLATTEN_OBJECT;
2925     }
2926     for (auto bearerType : bearerTypes) {
2927         if (!data.WriteUint32(static_cast<uint32_t>(bearerType))) {
2928             return ERR_FLATTEN_OBJECT;
2929         }
2930     }
2931 
2932     MessageParcel reply;
2933     MessageOption option;
2934     int result =
2935         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NOTIFY_NETWORK_BEARER_TYPE_CHANGE),
2936             data, reply, option);
2937     if (result != ERR_NONE) {
2938         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2939         return IPC_INVOKER_ERR;
2940     }
2941     return reply.ReadInt32();
2942 }
2943 
StartClat(const std::string & interfaceName,int32_t netId,const std::string & nat64PrefixStr)2944 int32_t NetsysNativeServiceProxy::StartClat(const std::string &interfaceName, int32_t netId,
2945                                             const std::string &nat64PrefixStr)
2946 {
2947     MessageParcel data;
2948     if (!WriteInterfaceToken(data)) {
2949         return ERR_FLATTEN_OBJECT;
2950     }
2951     if (!data.WriteString(interfaceName)) {
2952         return ERR_FLATTEN_OBJECT;
2953     }
2954     if (!data.WriteInt32(netId)) {
2955         return ERR_FLATTEN_OBJECT;
2956     }
2957     if (!data.WriteString(nat64PrefixStr)) {
2958         return ERR_FLATTEN_OBJECT;
2959     }
2960 
2961     MessageParcel reply;
2962     MessageOption option;
2963     sptr<IRemoteObject> remote = Remote();
2964     if (remote == nullptr) {
2965         return IPC_PROXY_NULL_INVOKER_ERR;
2966     }
2967     int32_t ret =
2968         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_START_CLAT), data, reply, option);
2969     if (ret != ERR_NONE) {
2970         NETNATIVE_LOGE("StartClat proxy SendRequest failed, error code: [%{public}d]", ret);
2971         return IPC_INVOKER_ERR;
2972     }
2973 
2974     int32_t result = ERR_INVALID_DATA;
2975     if (!reply.ReadInt32(result)) {
2976         return IPC_PROXY_TRANSACTION_ERR;
2977     }
2978     return result;
2979 }
2980 
StopClat(const std::string & interfaceName)2981 int32_t NetsysNativeServiceProxy::StopClat(const std::string &interfaceName)
2982 {
2983     MessageParcel data;
2984     if (!WriteInterfaceToken(data)) {
2985         return ERR_FLATTEN_OBJECT;
2986     }
2987     if (!data.WriteString(interfaceName)) {
2988         return ERR_FLATTEN_OBJECT;
2989     }
2990 
2991     MessageParcel reply;
2992     MessageOption option;
2993     sptr<IRemoteObject> remote = Remote();
2994     if (remote == nullptr) {
2995         return IPC_PROXY_NULL_INVOKER_ERR;
2996     }
2997     int32_t ret =
2998         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_STOP_CLAT), data, reply, option);
2999     if (ret != ERR_NONE) {
3000         NETNATIVE_LOGE("StopClat proxy SendRequest failed, error code: [%{public}d]", ret);
3001         return IPC_INVOKER_ERR;
3002     }
3003 
3004     int32_t result = ERR_INVALID_DATA;
3005     if (!reply.ReadInt32(result)) {
3006         return IPC_PROXY_TRANSACTION_ERR;
3007     }
3008     return result;
3009 }
3010 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)3011 int32_t NetsysNativeServiceProxy::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
3012 {
3013     MessageParcel data;
3014     if (!WriteInterfaceToken(data)) {
3015         return ERR_FLATTEN_OBJECT;
3016     }
3017     NETNATIVE_LOG_D("SetNicTrafficAllowed WriteParam func in");
3018     if (!data.WriteBool(status)) {
3019         NETNATIVE_LOGE("SetNicTrafficAllowed WriteBool func return error");
3020         return ERR_FLATTEN_OBJECT;
3021     }
3022     if (!data.WriteInt32(ifaceNames.size())) {
3023         NETNATIVE_LOGE("SetNicTrafficAllowed ifaceNames size return error");
3024         return ERR_FLATTEN_OBJECT;
3025     }
3026     for (const std::string& iter : ifaceNames) {
3027         if (!data.WriteString(iter)) {
3028             NETNATIVE_LOGE("SetNicTrafficAllowed write name return error");
3029             return ERR_FLATTEN_OBJECT;
3030         }
3031     }
3032     MessageParcel reply;
3033     MessageOption option;
3034     if (Remote() == nullptr) {
3035         NETNATIVE_LOGE("SetNicTrafficAllowed remote pointer is null");
3036         return ERR_FLATTEN_OBJECT;
3037     }
3038     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_NIC_TRAFFIC_ALLOWED),
3039         data, reply, option);
3040     if (error != ERR_NONE) {
3041         NETNATIVE_LOGE("SetNicTrafficAllowed proxy sendRequest failed");
3042         return ERR_FLATTEN_OBJECT;
3043     }
3044     int32_t ret;
3045     if (!reply.ReadInt32(ret)) {
3046         NETNATIVE_LOGE("SetNicTrafficAllowed proxy read ret failed");
3047         return ERR_FLATTEN_OBJECT;
3048     }
3049     NETNATIVE_LOG_D("SetNicTrafficAllowed WriteParam func out");
3050     return ret;
3051 }
3052 } // namespace NetsysNative
3053 } // namespace OHOS
3054