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 
EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif)1023 int32_t NetsysNativeServiceProxy::EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif)
1024 {
1025     NETNATIVE_LOGI("Begin to EnableDistributedClientNet");
1026     MessageParcel data;
1027     if (!WriteInterfaceToken(data)) {
1028         return ERR_FLATTEN_OBJECT;
1029     }
1030 
1031     if (!data.WriteString(virnicAddr)) {
1032         return ERR_FLATTEN_OBJECT;
1033     }
1034 
1035     if (!data.WriteString(iif)) {
1036         return ERR_FLATTEN_OBJECT;
1037     }
1038 
1039     MessageParcel reply;
1040     MessageOption option;
1041     int32_t ret =
1042         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_DISTRIBUTE_CLIENT_NET), data,
1043                               reply, option);
1044     if (ret != ERR_NONE) {
1045         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", ret);
1046         return IPC_INVOKER_ERR;
1047     }
1048     if (!reply.ReadInt32(ret)) {
1049         return NETMANAGER_ERR_READ_REPLY_FAIL;
1050     }
1051     return ret;
1052 }
1053 
EnableDistributedServerNet(const std::string &iif, const std::string &devIface, const std::string &dstAddr)1054 int32_t NetsysNativeServiceProxy::EnableDistributedServerNet(const std::string &iif, const std::string &devIface,
1055                                                              const std::string &dstAddr)
1056 {
1057     NETNATIVE_LOGI("Begin to EnableDistributedServerNet");
1058     MessageParcel data;
1059     if (!WriteInterfaceToken(data)) {
1060         return ERR_FLATTEN_OBJECT;
1061     }
1062 
1063     if (!data.WriteString(iif)) {
1064         return ERR_FLATTEN_OBJECT;
1065     }
1066 
1067     if (!data.WriteString(devIface)) {
1068         return ERR_FLATTEN_OBJECT;
1069     }
1070 
1071     if (!data.WriteString(dstAddr)) {
1072         return ERR_FLATTEN_OBJECT;
1073     }
1074 
1075     MessageParcel reply;
1076     MessageOption option;
1077     int32_t ret =
1078         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_DISTRIBUTE_SERVER_NET), data,
1079                               reply, option);
1080     if (ret != ERR_NONE) {
1081         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", ret);
1082         return IPC_INVOKER_ERR;
1083     }
1084     if (!reply.ReadInt32(ret)) {
1085         return NETMANAGER_ERR_READ_REPLY_FAIL;
1086     }
1087     return ret;
1088 }
1089 
DisableDistributedNet(bool isServer)1090 int32_t NetsysNativeServiceProxy::DisableDistributedNet(bool isServer)
1091 {
1092     NETNATIVE_LOGI("Begin to DisableDistributedNet");
1093     MessageParcel data;
1094     if (!WriteInterfaceToken(data)) {
1095         return ERR_FLATTEN_OBJECT;
1096     }
1097 
1098     if (!data.WriteBool(isServer)) {
1099         return ERR_FLATTEN_OBJECT;
1100     }
1101 
1102     MessageParcel reply;
1103     MessageOption option;
1104     int32_t ret =
1105         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_DISTRIBUTE_NET), data,
1106                               reply, option);
1107     if (ret != ERR_NONE) {
1108         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", ret);
1109         return IPC_INVOKER_ERR;
1110     }
1111     if (!reply.ReadInt32(ret)) {
1112         return NETMANAGER_ERR_READ_REPLY_FAIL;
1113     }
1114     return ret;
1115 }
1116 
GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)1117 int32_t NetsysNativeServiceProxy::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
1118 {
1119     NETNATIVE_LOGI("Begin to GetFwmarkForNetwork");
1120     MessageParcel data;
1121     if (!WriteInterfaceToken(data)) {
1122         return ERR_FLATTEN_OBJECT;
1123     }
1124     if (!data.WriteInt32(netId)) {
1125         return ERR_FLATTEN_OBJECT;
1126     }
1127     if (!data.WriteInt32(markMaskParcel.mark)) {
1128         return ERR_FLATTEN_OBJECT;
1129     }
1130     if (!data.WriteInt32(markMaskParcel.mask)) {
1131         return ERR_FLATTEN_OBJECT;
1132     }
1133 
1134     MessageParcel reply;
1135     MessageOption option;
1136     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_FWMARK_FOR_NETWORK),
1137         data, reply, option);
1138     if (result != ERR_NONE) {
1139         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1140         return IPC_INVOKER_ERR;
1141     }
1142     return reply.ReadInt32();
1143 }
1144 
SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)1145 int32_t NetsysNativeServiceProxy::SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)
1146 {
1147     NETNATIVE_LOGI("Begin to SetInterfaceConfig");
1148     MessageParcel data;
1149     if (!WriteInterfaceToken(data)) {
1150         return ERR_FLATTEN_OBJECT;
1151     }
1152     if (!data.WriteString(cfg.ifName)) {
1153         return ERR_FLATTEN_OBJECT;
1154     }
1155     if (!data.WriteString(cfg.hwAddr)) {
1156         return ERR_FLATTEN_OBJECT;
1157     }
1158     if (!data.WriteString(cfg.ipv4Addr)) {
1159         return ERR_FLATTEN_OBJECT;
1160     }
1161     if (!data.WriteInt32(cfg.prefixLength)) {
1162         return ERR_FLATTEN_OBJECT;
1163     }
1164     int32_t vsize = static_cast<int32_t>(cfg.flags.size());
1165     if (!data.WriteInt32(vsize)) {
1166         return ERR_FLATTEN_OBJECT;
1167     }
1168     std::vector<std::string> vCflags;
1169     vCflags.assign(cfg.flags.begin(), cfg.flags.end());
1170     NETNATIVE_LOGI("PROXY: SetInterfaceConfig Write flags String_SIZE: %{public}d",
1171                    static_cast<int32_t>(vCflags.size()));
1172     for (std::vector<std::string>::iterator it = vCflags.begin(); it != vCflags.end(); ++it) {
1173         data.WriteString(*it);
1174     }
1175     MessageParcel reply;
1176     MessageOption option;
1177     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_CONFIG),
1178         data, reply, option);
1179     if (result != ERR_NONE) {
1180         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1181         return IPC_INVOKER_ERR;
1182     }
1183     return reply.ReadInt32();
1184 }
1185 
GetInterfaceConfig(InterfaceConfigurationParcel &cfg)1186 int32_t NetsysNativeServiceProxy::GetInterfaceConfig(InterfaceConfigurationParcel &cfg)
1187 {
1188     NETNATIVE_LOGI("Begin to GetInterfaceConfig");
1189     MessageParcel data;
1190     int32_t ret;
1191     int32_t vSize;
1192     if (!WriteInterfaceToken(data)) {
1193         return ERR_FLATTEN_OBJECT;
1194     }
1195     if (!data.WriteString(cfg.ifName)) {
1196         return ERR_FLATTEN_OBJECT;
1197     }
1198 
1199     MessageParcel reply;
1200     MessageOption option;
1201     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_CONFIG),
1202         data, reply, option);
1203     if (result != ERR_NONE) {
1204         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1205         return IPC_INVOKER_ERR;
1206     }
1207     ret = reply.ReadInt32();
1208     if (ret != ERR_NONE) {
1209         NETNATIVE_LOGE("Fail to GetInterfaceConfig ret= %{public}d", ret);
1210         return ret;
1211     }
1212     reply.ReadString(cfg.ifName);
1213     reply.ReadString(cfg.hwAddr);
1214     reply.ReadString(cfg.ipv4Addr);
1215     reply.ReadInt32(cfg.prefixLength);
1216     vSize = reply.ReadInt32();
1217     vSize = vSize > MAX_INTERFACE_CONFIG_SIZE ? MAX_INTERFACE_CONFIG_SIZE : vSize;
1218     std::vector<std::string> vecString;
1219     for (int i = 0; i < vSize; i++) {
1220         vecString.push_back(reply.ReadString());
1221     }
1222     if (vSize > 0) {
1223         cfg.flags.assign(vecString.begin(), vecString.end());
1224     }
1225     NETNATIVE_LOGI("End to GetInterfaceConfig, ret =%{public}d", ret);
1226     return ret;
1227 }
1228 
InterfaceGetList(std::vector<std::string> &ifaces)1229 int32_t NetsysNativeServiceProxy::InterfaceGetList(std::vector<std::string> &ifaces)
1230 {
1231     NETNATIVE_LOGI("NetsysNativeServiceProxy Begin to InterfaceGetList");
1232     MessageParcel data;
1233     int32_t ret;
1234     int32_t vSize;
1235     if (!WriteInterfaceToken(data)) {
1236         return ERR_FLATTEN_OBJECT;
1237     }
1238     MessageParcel reply;
1239     MessageOption option;
1240     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_LIST),
1241         data, reply, option);
1242     if (result != ERR_NONE) {
1243         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1244         return IPC_INVOKER_ERR;
1245     }
1246     ret = reply.ReadInt32();
1247     if (ret != ERR_NONE) {
1248         NETNATIVE_LOGE("Fail to InterfaceGetList ret= %{public}d", ret);
1249         return ret;
1250     }
1251     vSize = reply.ReadInt32();
1252     std::vector<std::string> vecString;
1253     for (int i = 0; i < vSize; i++) {
1254         vecString.push_back(reply.ReadString());
1255     }
1256     if (vSize > 0) {
1257         ifaces.assign(vecString.begin(), vecString.end());
1258     }
1259     NETNATIVE_LOGI("NetsysNativeServiceProxy End to InterfaceGetList, ret =%{public}d", ret);
1260     return ret;
1261 }
1262 
StartDhcpClient(const std::string &iface, bool bIpv6)1263 int32_t NetsysNativeServiceProxy::StartDhcpClient(const std::string &iface, bool bIpv6)
1264 {
1265     NETNATIVE_LOGI("Begin to StartDhcpClient");
1266     MessageParcel data;
1267     int32_t ret;
1268     if (!WriteInterfaceToken(data)) {
1269         return ERR_FLATTEN_OBJECT;
1270     }
1271     if (!data.WriteString(iface)) {
1272         return ERR_FLATTEN_OBJECT;
1273     }
1274     if (!data.WriteBool(bIpv6)) {
1275         return ERR_FLATTEN_OBJECT;
1276     }
1277 
1278     MessageParcel reply;
1279     MessageOption option;
1280     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_CLIENT),
1281         data, reply, option);
1282     if (result != ERR_NONE) {
1283         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1284         return IPC_INVOKER_ERR;
1285     }
1286     ret = reply.ReadInt32();
1287     NETNATIVE_LOGI("End to StartDhcpClient, ret =%{public}d", ret);
1288     return ret;
1289 }
1290 
StopDhcpClient(const std::string &iface, bool bIpv6)1291 int32_t NetsysNativeServiceProxy::StopDhcpClient(const std::string &iface, bool bIpv6)
1292 {
1293     NETNATIVE_LOGI("Begin to StopDhcpClient");
1294     MessageParcel data;
1295     int32_t ret;
1296     if (!WriteInterfaceToken(data)) {
1297         return ERR_FLATTEN_OBJECT;
1298     }
1299     if (!data.WriteString(iface)) {
1300         return ERR_FLATTEN_OBJECT;
1301     }
1302     if (!data.WriteBool(bIpv6)) {
1303         return ERR_FLATTEN_OBJECT;
1304     }
1305 
1306     MessageParcel reply;
1307     MessageOption option;
1308     ret =
1309         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_CLIENT), data, reply, option);
1310     if (ret != ERR_NONE) {
1311         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", ret);
1312         return IPC_INVOKER_ERR;
1313     }
1314     NETNATIVE_LOGI("SendRequest, ret =%{public}d", ret);
1315     ret = reply.ReadInt32();
1316     NETNATIVE_LOGI("End to StopDhcpClient, ret =%{public}d", ret);
1317     return ret;
1318 }
1319 
StartDhcpService(const std::string &iface, const std::string &ipv4addr)1320 int32_t NetsysNativeServiceProxy::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
1321 {
1322     NETNATIVE_LOGI("Begin to StartDhcpService");
1323     MessageParcel data;
1324 
1325     if (!WriteInterfaceToken(data)) {
1326         return ERR_FLATTEN_OBJECT;
1327     }
1328     if (!data.WriteString(iface)) {
1329         return ERR_FLATTEN_OBJECT;
1330     }
1331     if (!data.WriteString(ipv4addr)) {
1332         return ERR_FLATTEN_OBJECT;
1333     }
1334 
1335     MessageParcel reply;
1336     MessageOption option;
1337     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_SERVICE),
1338         data, reply, option);
1339     if (result != ERR_NONE) {
1340         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1341         return IPC_INVOKER_ERR;
1342     }
1343     int32_t ret = reply.ReadInt32();
1344     NETNATIVE_LOGI("End to StartDhcpService, ret =%{public}d", ret);
1345     return ret;
1346 }
1347 
StopDhcpService(const std::string &iface)1348 int32_t NetsysNativeServiceProxy::StopDhcpService(const std::string &iface)
1349 {
1350     NETNATIVE_LOGI("Begin to StopDhcpService");
1351     MessageParcel data;
1352     if (!WriteInterfaceToken(data)) {
1353         return ERR_FLATTEN_OBJECT;
1354     }
1355     if (!data.WriteString(iface)) {
1356         return ERR_FLATTEN_OBJECT;
1357     }
1358 
1359     MessageParcel reply;
1360     MessageOption option;
1361     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_SERVICE),
1362         data, reply, option);
1363     if (result != ERR_NONE) {
1364         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1365         return IPC_INVOKER_ERR;
1366     }
1367     int32_t ret = reply.ReadInt32();
1368     NETNATIVE_LOGI("End to StopDhcpService, ret =%{public}d", ret);
1369     return ret;
1370 }
1371 
IpEnableForwarding(const std::string &requestor)1372 int32_t NetsysNativeServiceProxy::IpEnableForwarding(const std::string &requestor)
1373 {
1374     MessageParcel data;
1375     if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1376         return ERR_FLATTEN_OBJECT;
1377     }
1378 
1379     MessageParcel reply;
1380     MessageOption option;
1381     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPENABLE_FORWARDING),
1382         data, reply, option);
1383     if (result != ERR_NONE) {
1384         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1385         return IPC_INVOKER_ERR;
1386     }
1387     int32_t ret = reply.ReadInt32();
1388     NETNATIVE_LOGI("End to IpEnableForwarding, ret =%{public}d", ret);
1389     return ret;
1390 }
1391 
IpDisableForwarding(const std::string &requestor)1392 int32_t NetsysNativeServiceProxy::IpDisableForwarding(const std::string &requestor)
1393 {
1394     MessageParcel data;
1395     if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1396         return ERR_FLATTEN_OBJECT;
1397     }
1398 
1399     MessageParcel reply;
1400     MessageOption option;
1401     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPDISABLE_FORWARDING),
1402         data, reply, option);
1403     if (result != ERR_NONE) {
1404         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1405         return IPC_INVOKER_ERR;
1406     }
1407     int32_t ret = reply.ReadInt32();
1408     NETNATIVE_LOGI("End to IpDisableForwarding, ret =%{public}d", ret);
1409     return ret;
1410 }
1411 
EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)1412 int32_t NetsysNativeServiceProxy::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1413 {
1414     MessageParcel data;
1415     if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1416         return ERR_FLATTEN_OBJECT;
1417     }
1418 
1419     MessageParcel reply;
1420     MessageOption option;
1421     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_NAT),
1422         data, reply, option);
1423     if (result != ERR_NONE) {
1424         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1425         return IPC_INVOKER_ERR;
1426     }
1427     int32_t ret = reply.ReadInt32();
1428     NETNATIVE_LOGI("End to EnableNat, ret =%{public}d", ret);
1429     return ret;
1430 }
1431 
DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)1432 int32_t NetsysNativeServiceProxy::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1433 {
1434     MessageParcel data;
1435     if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1436         return ERR_FLATTEN_OBJECT;
1437     }
1438 
1439     MessageParcel reply;
1440     MessageOption option;
1441     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_NAT),
1442         data, reply, option);
1443     if (result != ERR_NONE) {
1444         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1445         return IPC_INVOKER_ERR;
1446     }
1447     int32_t ret = reply.ReadInt32();
1448     NETNATIVE_LOGI("End to DisableNat, ret =%{public}d", ret);
1449     return ret;
1450 }
1451 
IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)1452 int32_t NetsysNativeServiceProxy::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
1453 {
1454     MessageParcel data;
1455     if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1456         return ERR_FLATTEN_OBJECT;
1457     }
1458 
1459     MessageParcel reply;
1460     MessageOption option;
1461     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_ADD_INTERFACE_FORWARD),
1462         data, reply, option);
1463     if (result != ERR_NONE) {
1464         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1465         return IPC_INVOKER_ERR;
1466     }
1467     int32_t ret = reply.ReadInt32();
1468     NETNATIVE_LOGI("End to IpfwdAddInterfaceForward, ret =%{public}d", ret);
1469     return ret;
1470 }
1471 
IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)1472 int32_t NetsysNativeServiceProxy::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
1473 {
1474     MessageParcel data;
1475     if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1476         return ERR_FLATTEN_OBJECT;
1477     }
1478 
1479     MessageParcel reply;
1480     MessageOption option;
1481     int result =
1482         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD),
1483             data, reply, option);
1484     if (result != ERR_NONE) {
1485         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1486         return IPC_INVOKER_ERR;
1487     }
1488     int32_t ret = reply.ReadInt32();
1489     NETNATIVE_LOGI("End to IpfwdRemoveInterfaceForward, ret =%{public}d", ret);
1490     return ret;
1491 }
1492 
BandwidthEnableDataSaver(bool enable)1493 int32_t NetsysNativeServiceProxy::BandwidthEnableDataSaver(bool enable)
1494 {
1495     MessageParcel data;
1496     if (!WriteInterfaceToken(data)) {
1497         NETNATIVE_LOGE("WriteInterfaceToken failed");
1498         return ERR_FLATTEN_OBJECT;
1499     }
1500     if (!data.WriteBool(enable)) {
1501         NETNATIVE_LOGE("WriteBool failed");
1502         return ERR_FLATTEN_OBJECT;
1503     }
1504 
1505     MessageParcel reply;
1506     MessageOption option;
1507     auto remote = Remote();
1508     if (remote == nullptr) {
1509         return IPC_PROXY_NULL_INVOKER_ERR;
1510     }
1511     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER),
1512                                         data, reply, option);
1513     if (error != ERR_NONE) {
1514         NETNATIVE_LOGE("proxy SendRequest failed");
1515         return ERR_FLATTEN_OBJECT;
1516     }
1517     int32_t ret = reply.ReadInt32();
1518     return ret;
1519 }
1520 
BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)1521 int32_t NetsysNativeServiceProxy::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
1522 {
1523     MessageParcel data;
1524     if (!WriteInterfaceToken(data)) {
1525         NETNATIVE_LOGE("WriteInterfaceToken failed");
1526         return ERR_FLATTEN_OBJECT;
1527     }
1528     if (!data.WriteString(ifName)) {
1529         NETNATIVE_LOGE("WriteString failed");
1530         return ERR_FLATTEN_OBJECT;
1531     }
1532     if (!data.WriteInt64(bytes)) {
1533         NETNATIVE_LOGE("WriteInt64 failed");
1534         return ERR_FLATTEN_OBJECT;
1535     }
1536 
1537     MessageParcel reply;
1538     MessageOption option;
1539     auto remote = Remote();
1540     if (remote == nullptr) {
1541         return IPC_PROXY_NULL_INVOKER_ERR;
1542     }
1543     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_SET_IFACE_QUOTA),
1544                                         data, reply, option);
1545     if (error != ERR_NONE) {
1546         NETNATIVE_LOGE("proxy SendRequest failed");
1547         return ERR_FLATTEN_OBJECT;
1548     }
1549     int32_t ret = reply.ReadInt32();
1550     return ret;
1551 }
1552 
BandwidthRemoveIfaceQuota(const std::string &ifName)1553 int32_t NetsysNativeServiceProxy::BandwidthRemoveIfaceQuota(const std::string &ifName)
1554 {
1555     MessageParcel data;
1556     if (!WriteInterfaceToken(data)) {
1557         NETNATIVE_LOGE("WriteInterfaceToken failed");
1558         return ERR_FLATTEN_OBJECT;
1559     }
1560     if (!data.WriteString(ifName)) {
1561         NETNATIVE_LOGE("WriteString failed");
1562         return ERR_FLATTEN_OBJECT;
1563     }
1564 
1565     MessageParcel reply;
1566     MessageOption option;
1567     auto remote = Remote();
1568     if (remote == nullptr) {
1569         return IPC_PROXY_NULL_INVOKER_ERR;
1570     }
1571     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA),
1572                                         data, reply, option);
1573     if (error != ERR_NONE) {
1574         NETNATIVE_LOGE("proxy SendRequest failed");
1575         return ERR_FLATTEN_OBJECT;
1576     }
1577     int32_t ret = reply.ReadInt32();
1578     return ret;
1579 }
1580 
BandwidthAddDeniedList(uint32_t uid)1581 int32_t NetsysNativeServiceProxy::BandwidthAddDeniedList(uint32_t uid)
1582 {
1583     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_DENIED_LIST));
1584     return ret;
1585 }
1586 
BandwidthRemoveDeniedList(uint32_t uid)1587 int32_t NetsysNativeServiceProxy::BandwidthRemoveDeniedList(uint32_t uid)
1588 {
1589     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST));
1590     return ret;
1591 }
1592 
BandwidthAddAllowedList(uint32_t uid)1593 int32_t NetsysNativeServiceProxy::BandwidthAddAllowedList(uint32_t uid)
1594 {
1595     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST));
1596     return ret;
1597 }
1598 
BandwidthRemoveAllowedList(uint32_t uid)1599 int32_t NetsysNativeServiceProxy::BandwidthRemoveAllowedList(uint32_t uid)
1600 {
1601     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST));
1602     return ret;
1603 }
1604 
FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)1605 int32_t NetsysNativeServiceProxy::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1606 {
1607     MessageParcel data;
1608     uint32_t uidSize = uids.size();
1609     if (uidSize > UIDS_LIST_MAX_SIZE) {
1610         NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1611         return ERR_INVALID_DATA;
1612     }
1613     if (!WriteInterfaceToken(data)) {
1614         NETNATIVE_LOGE("WriteInterfaceToken failed");
1615         return ERR_FLATTEN_OBJECT;
1616     }
1617     if (!data.WriteUint32(chain)) {
1618         NETNATIVE_LOGE("WriteUint32 failed");
1619         return ERR_FLATTEN_OBJECT;
1620     }
1621     if (!data.WriteUint32(uidSize)) {
1622         NETNATIVE_LOGE("WriteUint32 failed");
1623         return ERR_FLATTEN_OBJECT;
1624     }
1625     std::vector<uint32_t> vUids;
1626     vUids.assign(uids.begin(), uids.end());
1627     std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1628 
1629     MessageParcel reply;
1630     MessageOption option;
1631     auto remote = Remote();
1632     if (remote == nullptr) {
1633         return IPC_PROXY_NULL_INVOKER_ERR;
1634     }
1635     int32_t error = remote->SendRequest(
1636         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN), data, reply, option);
1637     if (error != ERR_NONE) {
1638         NETNATIVE_LOGE("proxy SendRequest failed");
1639         return ERR_FLATTEN_OBJECT;
1640     }
1641     int32_t ret = reply.ReadInt32();
1642     return ret;
1643 }
1644 
FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)1645 int32_t NetsysNativeServiceProxy::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1646 {
1647     MessageParcel data;
1648     uint32_t uidSize = uids.size();
1649     if (uidSize > UIDS_LIST_MAX_SIZE) {
1650         NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1651         return ERR_INVALID_DATA;
1652     }
1653     if (!WriteInterfaceToken(data)) {
1654         NETNATIVE_LOGE("WriteInterfaceToken failed");
1655         return ERR_FLATTEN_OBJECT;
1656     }
1657     if (!data.WriteUint32(chain)) {
1658         NETNATIVE_LOGE("WriteUint32 Error");
1659         return ERR_FLATTEN_OBJECT;
1660     }
1661     if (!data.WriteUint32(uidSize)) {
1662         NETNATIVE_LOGE("WriteUint32 Error");
1663         return ERR_FLATTEN_OBJECT;
1664     }
1665     std::vector<uint32_t> vUids;
1666     vUids.assign(uids.begin(), uids.end());
1667     std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1668 
1669     MessageParcel reply;
1670     MessageOption option;
1671     auto remote = Remote();
1672     if (remote == nullptr) {
1673         return IPC_PROXY_NULL_INVOKER_ERR;
1674     }
1675     int32_t error = remote->SendRequest(
1676         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN), data, reply, option);
1677     if (error != ERR_NONE) {
1678         NETNATIVE_LOGE("proxy SendRequest failed");
1679         return ERR_FLATTEN_OBJECT;
1680     }
1681     int32_t ret = reply.ReadInt32();
1682     return ret;
1683 }
1684 
FirewallEnableChain(uint32_t chain, bool enable)1685 int32_t NetsysNativeServiceProxy::FirewallEnableChain(uint32_t chain, bool enable)
1686 {
1687     MessageParcel data;
1688     if (!WriteInterfaceToken(data)) {
1689         NETNATIVE_LOGE("WriteInterfaceToken failed");
1690         return ERR_FLATTEN_OBJECT;
1691     }
1692     if (!data.WriteUint32(chain)) {
1693         NETNATIVE_LOGE("WriteUint32 Error");
1694         return ERR_FLATTEN_OBJECT;
1695     }
1696     if (!data.WriteBool(enable)) {
1697         NETNATIVE_LOGE("WriteBool Error");
1698         return ERR_FLATTEN_OBJECT;
1699     }
1700 
1701     MessageParcel reply;
1702     MessageOption option;
1703     auto remote = Remote();
1704     if (remote == nullptr) {
1705         return IPC_PROXY_NULL_INVOKER_ERR;
1706     }
1707     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_ENABLE_CHAIN), data,
1708                                         reply, option);
1709     if (error != ERR_NONE) {
1710         NETNATIVE_LOGE("proxy SendRequest failed");
1711         return ERR_FLATTEN_OBJECT;
1712     }
1713     int32_t ret = reply.ReadInt32();
1714     return ret;
1715 }
1716 
FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)1717 int32_t NetsysNativeServiceProxy::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
1718                                                      uint32_t firewallRule)
1719 {
1720     MessageParcel data;
1721     if (!WriteInterfaceToken(data)) {
1722         NETNATIVE_LOGE("WriteInterfaceToken failed");
1723         return ERR_FLATTEN_OBJECT;
1724     }
1725     if (!data.WriteUint32(chain)) {
1726         NETNATIVE_LOGE("WriteUint32 failed");
1727         return ERR_FLATTEN_OBJECT;
1728     }
1729     if (!data.WriteUInt32Vector(uids)) {
1730         NETNATIVE_LOGE("WriteUint32 failed");
1731         return ERR_FLATTEN_OBJECT;
1732     }
1733     if (!data.WriteUint32(firewallRule)) {
1734         NETNATIVE_LOGE("WriteUint32 failed");
1735         return ERR_FLATTEN_OBJECT;
1736     }
1737 
1738     MessageParcel reply;
1739     MessageOption option;
1740     auto remote = Remote();
1741     if (remote == nullptr) {
1742         return IPC_PROXY_NULL_INVOKER_ERR;
1743     }
1744     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_RULE), data,
1745                                         reply, option);
1746     if (error != ERR_NONE) {
1747         NETNATIVE_LOGE("proxy SendRequest failed");
1748         return ERR_FLATTEN_OBJECT;
1749     }
1750     int32_t ret = reply.ReadInt32();
1751     return ret;
1752 }
1753 
ShareDnsSet(uint16_t netId)1754 int32_t NetsysNativeServiceProxy::ShareDnsSet(uint16_t netId)
1755 {
1756     MessageParcel data;
1757     if (!WriteInterfaceToken(data)) {
1758         return ERR_FLATTEN_OBJECT;
1759     }
1760     if (!data.WriteUint16(netId)) {
1761         return ERR_FLATTEN_OBJECT;
1762     }
1763 
1764     MessageParcel reply;
1765     MessageOption option;
1766     int32_t error =
1767         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_TETHER_DNS_SET), data, reply, option);
1768     if (error != ERR_NONE) {
1769         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1770         return error;
1771     }
1772     return reply.ReadInt32();
1773 }
1774 
StartDnsProxyListen()1775 int32_t NetsysNativeServiceProxy::StartDnsProxyListen()
1776 {
1777     MessageParcel data;
1778     if (!WriteInterfaceToken(data)) {
1779         return ERR_FLATTEN_OBJECT;
1780     }
1781 
1782     MessageParcel reply;
1783     MessageOption option;
1784     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DNS_PROXY_LISTEN),
1785                                           data, reply, option);
1786     if (error != ERR_NONE) {
1787         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1788         return error;
1789     }
1790     return reply.ReadInt32();
1791 }
1792 
StopDnsProxyListen()1793 int32_t NetsysNativeServiceProxy::StopDnsProxyListen()
1794 {
1795     MessageParcel data;
1796     if (!WriteInterfaceToken(data)) {
1797         return ERR_FLATTEN_OBJECT;
1798     }
1799 
1800     MessageParcel reply;
1801     MessageOption option;
1802     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DNS_PROXY_LISTEN),
1803                                           data, reply, option);
1804     if (error != ERR_NONE) {
1805         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1806         return error;
1807     }
1808     return reply.ReadInt32();
1809 }
1810 
GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface, NetworkSharingTraffic &traffic)1811 int32_t NetsysNativeServiceProxy::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
1812                                                            NetworkSharingTraffic &traffic)
1813 {
1814     MessageParcel data;
1815     if (!WriteInterfaceToken(data)) {
1816         return ERR_FLATTEN_OBJECT;
1817     }
1818     if (!data.WriteString(downIface)) {
1819         return ERR_FLATTEN_OBJECT;
1820     }
1821     if (!data.WriteString(upIface)) {
1822         return ERR_FLATTEN_OBJECT;
1823     }
1824 
1825     MessageParcel reply;
1826     MessageOption option;
1827     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_SHARING_NETWORK_TRAFFIC),
1828         data, reply, option);
1829     if (result != ERR_NONE) {
1830         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
1831         return IPC_INVOKER_ERR;
1832     }
1833     int32_t ret = reply.ReadInt32();
1834     if (ret != ERR_NONE) {
1835         NETNATIVE_LOGE("Fail to GetNetworkSharingTraffic ret= %{public}d", ret);
1836         return ret;
1837     }
1838 
1839     traffic.receive = reply.ReadInt64();
1840     traffic.send = reply.ReadInt64();
1841     traffic.all = reply.ReadInt64();
1842     NETNATIVE_LOGI("NetsysNativeServiceProxy GetNetworkSharingTraffic ret=%{public}d", ret);
1843     return ret;
1844 }
1845 
GetTotalStats(uint64_t &stats, uint32_t type)1846 int32_t NetsysNativeServiceProxy::GetTotalStats(uint64_t &stats, uint32_t type)
1847 {
1848     MessageParcel data;
1849     if (!WriteInterfaceToken(data)) {
1850         return ERR_FLATTEN_OBJECT;
1851     }
1852     if (!data.WriteUint8(type)) {
1853         return ERR_FLATTEN_OBJECT;
1854     }
1855     MessageParcel reply;
1856     MessageOption option;
1857     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_TOTAL_STATS), data,
1858                                           reply, option)) {
1859         NETNATIVE_LOGE("proxy SendRequest failed");
1860         return ERR_FLATTEN_OBJECT;
1861     }
1862 
1863     int32_t ret;
1864     if (!reply.ReadInt32(ret)) {
1865         NETNATIVE_LOGE("get ret falil");
1866         return ERR_FLATTEN_OBJECT;
1867     }
1868     if (ret != ERR_NONE) {
1869         NETNATIVE_LOGE("fail to GetTotalStats ret= %{public}d", ret);
1870         return ret;
1871     }
1872     if (!reply.ReadUint64(stats)) {
1873         NETNATIVE_LOGE("get stats falil");
1874         return ERR_FLATTEN_OBJECT;
1875     }
1876 
1877     return ERR_NONE;
1878 }
1879 
GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)1880 int32_t NetsysNativeServiceProxy::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
1881 {
1882     MessageParcel data;
1883     if (!WriteInterfaceToken(data)) {
1884         return ERR_FLATTEN_OBJECT;
1885     }
1886     if (!data.WriteUint32(type)) {
1887         return ERR_FLATTEN_OBJECT;
1888     }
1889     if (!data.WriteUint32(uid)) {
1890         return ERR_FLATTEN_OBJECT;
1891     }
1892     MessageParcel reply;
1893     MessageOption option;
1894     if (ERR_NONE !=
1895         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_UID_STATS), data, reply, option)) {
1896         NETNATIVE_LOGE("proxy SendRequest failed");
1897         return ERR_FLATTEN_OBJECT;
1898     }
1899 
1900     int32_t ret;
1901     if (!reply.ReadInt32(ret)) {
1902         NETNATIVE_LOGE("get ret falil");
1903         return ERR_FLATTEN_OBJECT;
1904     }
1905     if (ret != ERR_NONE) {
1906         if (ret != STATS_ERR_READ_BPF_FAIL) {
1907             NETNATIVE_LOGE("fail to GetUidStats ret= %{public}d", ret);
1908         }
1909         return ret;
1910     }
1911     if (!reply.ReadUint64(stats)) {
1912         NETNATIVE_LOGE("get stats falil");
1913         return ERR_FLATTEN_OBJECT;
1914     }
1915     return ERR_NONE;
1916 }
1917 
GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)1918 int32_t NetsysNativeServiceProxy::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
1919 {
1920     MessageParcel data;
1921     if (!WriteInterfaceToken(data)) {
1922         return ERR_FLATTEN_OBJECT;
1923     }
1924     if (!data.WriteUint32(type)) {
1925         return ERR_FLATTEN_OBJECT;
1926     }
1927     if (!data.WriteString(interfaceName)) {
1928         return ERR_FLATTEN_OBJECT;
1929     }
1930     MessageParcel reply;
1931     MessageOption option;
1932     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_IFACE_STATS), data,
1933                                           reply, option)) {
1934         NETNATIVE_LOGE("proxy SendRequest failed");
1935         return ERR_FLATTEN_OBJECT;
1936     }
1937 
1938     int32_t ret;
1939     if (!reply.ReadInt32(ret)) {
1940         NETNATIVE_LOGE("get ret falil");
1941         return ERR_FLATTEN_OBJECT;
1942     }
1943     if (ret != ERR_NONE) {
1944         if (ret != STATS_ERR_READ_BPF_FAIL) {
1945             NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1946         }
1947         return ret;
1948     }
1949     if (!reply.ReadUint64(stats)) {
1950         NETNATIVE_LOGE("get stats falil");
1951         return ERR_FLATTEN_OBJECT;
1952     }
1953     return ERR_NONE;
1954 }
1955 
GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)1956 int32_t NetsysNativeServiceProxy::GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1957 {
1958     MessageParcel data;
1959     if (!WriteInterfaceToken(data)) {
1960         return ERR_FLATTEN_OBJECT;
1961     }
1962     MessageParcel reply;
1963     MessageOption option;
1964     auto result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_SIM_STATS_INFO),
1965                                         data,
1966                                         reply,
1967                                         option);
1968     if (result != ERR_NONE) {
1969         NETNATIVE_LOGE("proxy SendRequest failed");
1970         return ERR_FLATTEN_OBJECT;
1971     }
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 GetAllSimStatsInfo ret= %{public}d", ret);
1980         return ret;
1981     }
1982     if (!OHOS::NetManagerStandard::StatsInfoUnmarshallingVector(reply, stats)) {
1983         NETNATIVE_LOGE("Read stats info failed");
1984         return ERR_FLATTEN_OBJECT;
1985     }
1986 
1987     return ERR_NONE;
1988 }
1989 
DeleteSimStatsInfo(uint32_t uid)1990 int32_t NetsysNativeServiceProxy::DeleteSimStatsInfo(uint32_t uid)
1991 {
1992     MessageParcel data;
1993     if (!WriteInterfaceToken(data)) {
1994         return ERR_FLATTEN_OBJECT;
1995     }
1996     if (!data.WriteUint32(uid)) {
1997         return ERR_FLATTEN_OBJECT;
1998     }
1999     MessageParcel reply;
2000     MessageOption option;
2001     auto result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DELETE_SIM_STATS_INFO),
2002                                         data, reply, option);
2003     if (result != ERR_NONE) {
2004         NETNATIVE_LOGE("proxy SendRequest failed");
2005         return ERR_FLATTEN_OBJECT;
2006     }
2007     int32_t ret;
2008     if (!reply.ReadInt32(ret)) {
2009         NETNATIVE_LOGE("get ret falil");
2010         return ERR_FLATTEN_OBJECT;
2011     }
2012     if (ret != ERR_NONE) {
2013         NETNATIVE_LOGE("fail to DeleteSimStatsInfo ret= %{public}d", ret);
2014         return ret;
2015     }
2016     return ERR_NONE;
2017 }
2018 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)2019 int32_t NetsysNativeServiceProxy::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
2020 {
2021     MessageParcel data;
2022     if (!WriteInterfaceToken(data)) {
2023         return ERR_FLATTEN_OBJECT;
2024     }
2025     MessageParcel reply;
2026     MessageOption option;
2027     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_STATS_INFO), data,
2028                                           reply, option)) {
2029         NETNATIVE_LOGE("proxy SendRequest failed");
2030         return ERR_FLATTEN_OBJECT;
2031     }
2032 
2033     int32_t ret;
2034     if (!reply.ReadInt32(ret)) {
2035         NETNATIVE_LOGE("get ret falil");
2036         return ERR_FLATTEN_OBJECT;
2037     }
2038     if (ret != ERR_NONE) {
2039         NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
2040         return ret;
2041     }
2042     if (!OHOS::NetManagerStandard::StatsInfoUnmarshallingVector(reply, stats)) {
2043         NETNATIVE_LOGE("Read stats info failed");
2044         return ERR_FLATTEN_OBJECT;
2045     }
2046 
2047     return ERR_NONE;
2048 }
2049 
DeleteStatsInfo(uint32_t uid)2050 int32_t NetsysNativeServiceProxy::DeleteStatsInfo(uint32_t uid)
2051 {
2052     MessageParcel data;
2053     if (!WriteInterfaceToken(data)) {
2054         return ERR_FLATTEN_OBJECT;
2055     }
2056     if (!data.WriteUint32(uid)) {
2057         return ERR_FLATTEN_OBJECT;
2058     }
2059     MessageParcel reply;
2060     MessageOption option;
2061     auto result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DELETE_STATS_INFO), data,
2062                                         reply, option);
2063     if (result != ERR_NONE) {
2064         NETNATIVE_LOGE("proxy SendRequest failed");
2065         return ERR_FLATTEN_OBJECT;
2066     }
2067     int32_t ret;
2068     if (!reply.ReadInt32(ret)) {
2069         NETNATIVE_LOGE("get ret falil");
2070         return ERR_FLATTEN_OBJECT;
2071     }
2072     if (ret != ERR_NONE) {
2073         NETNATIVE_LOGE("fail to DeleteStatsInfo ret= %{public}d", ret);
2074         return ret;
2075     }
2076     return ERR_NONE;
2077 }
2078 
SetIptablesCommandForRes(const std::string &cmd, std::string &respond, IptablesType ipType)2079 int32_t NetsysNativeServiceProxy::SetIptablesCommandForRes(const std::string &cmd, std::string &respond,
2080                                                            IptablesType ipType)
2081 {
2082     MessageParcel data;
2083     if (!WriteInterfaceToken(data)) {
2084         return ERR_FLATTEN_OBJECT;
2085     }
2086     if (!data.WriteString(cmd)) {
2087         return ERR_FLATTEN_OBJECT;
2088     }
2089     if (!data.WriteUint32(ipType)) {
2090         return ERR_FLATTEN_OBJECT;
2091     }
2092     MessageParcel reply;
2093     MessageOption option;
2094     if (Remote() == nullptr) {
2095         NETNATIVE_LOGE("SetIptablesCommandForRes Remote pointer is null");
2096         return ERR_FLATTEN_OBJECT;
2097     }
2098     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES),
2099                                           data, reply, option);
2100     if (error != ERR_NONE) {
2101         NETNATIVE_LOGE("SetIptablesCommandForRes proxy SendRequest failed");
2102         return ERR_FLATTEN_OBJECT;
2103     }
2104     int32_t ret;
2105     if (!reply.ReadInt32(ret)) {
2106         NETNATIVE_LOGE("SetIptablesCommandForRes proxy read ret failed");
2107         return ERR_FLATTEN_OBJECT;
2108     }
2109     if (ret == ERR_NONE) {
2110         if (!reply.ReadString(respond)) {
2111             NETNATIVE_LOGE("SetIptablesCommandForRes proxy read respond failed");
2112             return ERR_FLATTEN_OBJECT;
2113         }
2114     }
2115     return ret;
2116 }
2117 
DealBandwidth(uint32_t uid, uint32_t code)2118 int32_t NetsysNativeServiceProxy::DealBandwidth(uint32_t uid, uint32_t code)
2119 {
2120     MessageParcel data;
2121     if (!WriteInterfaceToken(data)) {
2122         NETNATIVE_LOGE("WriteInterfaceToken failed");
2123         return ERR_FLATTEN_OBJECT;
2124     }
2125     if (!data.WriteUint32(uid)) {
2126         NETNATIVE_LOGE("WriteUint32 failed");
2127         return ERR_FLATTEN_OBJECT;
2128     }
2129 
2130     MessageParcel reply;
2131     MessageOption option;
2132     auto remote = Remote();
2133     if (remote == nullptr) {
2134         return IPC_PROXY_NULL_INVOKER_ERR;
2135     }
2136     int32_t error = remote->SendRequest(code, data, reply, option);
2137     if (error != ERR_NONE) {
2138         NETNATIVE_LOGE("proxy SendRequest failed");
2139         return ERR_FLATTEN_OBJECT;
2140     }
2141 
2142     return reply.ReadInt32();
2143 }
2144 
NetDiagPingHost(const NetDiagPingOption &pingOption, const sptr<INetDiagCallback> &callback)2145 int32_t NetsysNativeServiceProxy::NetDiagPingHost(const NetDiagPingOption &pingOption,
2146                                                   const sptr<INetDiagCallback> &callback)
2147 {
2148     NETNATIVE_LOGI("Begin to NetDiagPingHost");
2149     if (callback == nullptr) {
2150         NETNATIVE_LOGE("The parameter of callback is nullptr");
2151         return ERR_NULL_OBJECT;
2152     }
2153 
2154     MessageParcel data;
2155     if (!WriteInterfaceToken(data)) {
2156         return ERR_FLATTEN_OBJECT;
2157     }
2158     if (!pingOption.Marshalling(data)) {
2159         return ERR_FLATTEN_OBJECT;
2160     }
2161     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2162         return ERR_FLATTEN_OBJECT;
2163     }
2164 
2165     sptr<IRemoteObject> remote = Remote();
2166     if (remote == nullptr) {
2167         NETNATIVE_LOGE("Remote is null");
2168         return ERR_FLATTEN_OBJECT;
2169     }
2170     MessageParcel reply;
2171     MessageOption option;
2172     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_PING_HOST),
2173         data, reply, option);
2174     if (result != ERR_NONE) {
2175         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2176         return IPC_INVOKER_ERR;
2177     }
2178     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2179     if (!reply.ReadInt32(ret)) {
2180         NETNATIVE_LOGE("Read result failed");
2181         return ERR_FLATTEN_OBJECT;
2182     }
2183     return ret;
2184 }
2185 
NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables)2186 int32_t NetsysNativeServiceProxy::NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables)
2187 {
2188     NETNATIVE_LOGI("Begin to NetDiagGetRouteTable");
2189     MessageParcel data;
2190     if (!WriteInterfaceToken(data)) {
2191         return ERR_FLATTEN_OBJECT;
2192     }
2193     sptr<IRemoteObject> remote = Remote();
2194     if (remote == nullptr) {
2195         NETNATIVE_LOGE("Remote is null");
2196         return ERR_FLATTEN_OBJECT;
2197     }
2198 
2199     MessageParcel reply;
2200     MessageOption option;
2201     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_ROUTE_TABLE),
2202         data, reply, option);
2203     if (result != ERR_NONE) {
2204         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2205         return IPC_INVOKER_ERR;
2206     }
2207     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2208     if (!reply.ReadInt32(ret)) {
2209         NETNATIVE_LOGE("Read result failed");
2210         return ERR_FLATTEN_OBJECT;
2211     }
2212 
2213     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
2214         uint32_t size = 0;
2215         if (!reply.ReadUint32(size)) {
2216             NETNATIVE_LOGE("Read uint32 failed");
2217             return ERR_FLATTEN_OBJECT;
2218         }
2219 
2220         for (uint32_t i = 0; i < size; ++i) {
2221             NetDiagRouteTable routeTable;
2222             if (!NetDiagRouteTable::Unmarshalling(reply, routeTable)) {
2223                 NETNATIVE_LOGE("NetDiagRouteTable unmarshalling failed.");
2224                 return ERR_FLATTEN_OBJECT;
2225             }
2226             routeTables.push_back(routeTable);
2227         }
2228     }
2229     return ret;
2230 }
2231 
NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo)2232 int32_t NetsysNativeServiceProxy::NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo)
2233 {
2234     NETNATIVE_LOGI("Begin to NetDiagGetSocketsInfo");
2235     MessageParcel data;
2236     if (!WriteInterfaceToken(data)) {
2237         return ERR_FLATTEN_OBJECT;
2238     }
2239     if (!data.WriteUint8(static_cast<uint8_t>(socketType))) {
2240         return ERR_FLATTEN_OBJECT;
2241     }
2242 
2243     sptr<IRemoteObject> remote = Remote();
2244     if (remote == nullptr) {
2245         NETNATIVE_LOGE("Remote is null");
2246         return ERR_FLATTEN_OBJECT;
2247     }
2248     MessageParcel reply;
2249     MessageOption option;
2250     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_SOCKETS_INFO),
2251         data, reply, option);
2252     if (result != ERR_NONE) {
2253         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2254         return IPC_INVOKER_ERR;
2255     }
2256     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2257     if (!reply.ReadInt32(ret)) {
2258         NETNATIVE_LOGE("Read result failed");
2259         return ERR_FLATTEN_OBJECT;
2260     }
2261 
2262     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
2263         if (!NetDiagSocketsInfo::Unmarshalling(reply, socketsInfo)) {
2264             NETNATIVE_LOGE("NetDiagSocketsInfo Unmarshalling failed.");
2265             return ERR_FLATTEN_OBJECT;
2266         }
2267     }
2268     return ret;
2269 }
2270 
NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs, const std::string &ifaceName)2271 int32_t NetsysNativeServiceProxy::NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs,
2272                                                             const std::string &ifaceName)
2273 {
2274     NETNATIVE_LOGI("Begin to NetDiagGetInterfaceConfig");
2275     MessageParcel data;
2276     if (!WriteInterfaceToken(data)) {
2277         return ERR_FLATTEN_OBJECT;
2278     }
2279     if (!data.WriteString(ifaceName)) {
2280         return ERR_FLATTEN_OBJECT;
2281     }
2282 
2283     sptr<IRemoteObject> remote = Remote();
2284     if (remote == nullptr) {
2285         NETNATIVE_LOGE("Remote is null");
2286         return ERR_FLATTEN_OBJECT;
2287     }
2288     MessageParcel reply;
2289     MessageOption option;
2290     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_IFACE_CONFIG),
2291         data, reply, option);
2292     if (result != ERR_NONE) {
2293         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2294         return IPC_INVOKER_ERR;
2295     }
2296     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2297     if (!reply.ReadInt32(ret)) {
2298         NETNATIVE_LOGE("Read result failed");
2299         return ERR_FLATTEN_OBJECT;
2300     }
2301 
2302     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
2303         uint32_t size = 0;
2304         if (!reply.ReadUint32(size)) {
2305             NETNATIVE_LOGE("Read uint32 failed");
2306             return ERR_FLATTEN_OBJECT;
2307         }
2308 
2309         for (uint32_t i = 0; i < size; ++i) {
2310             NetDiagIfaceConfig ifaceConfig;
2311             if (!NetDiagIfaceConfig::Unmarshalling(reply, ifaceConfig)) {
2312                 NETNATIVE_LOGE("NetDiagIfaceConfig Unmarshalling failed.");
2313                 return ERR_FLATTEN_OBJECT;
2314             }
2315             configs.push_back(ifaceConfig);
2316         }
2317     }
2318     return ret;
2319 }
2320 
NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config, const std::string &ifaceName, bool add)2321 int32_t NetsysNativeServiceProxy::NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config,
2322                                                                const std::string &ifaceName, bool add)
2323 {
2324     NETNATIVE_LOGI("Begin to NetDiagUpdateInterfaceConfig");
2325     MessageParcel data;
2326     if (!WriteInterfaceToken(data)) {
2327         return ERR_FLATTEN_OBJECT;
2328     }
2329     if (!config.Marshalling(data)) {
2330         return ERR_FLATTEN_OBJECT;
2331     }
2332     if (!data.WriteString(ifaceName)) {
2333         return ERR_FLATTEN_OBJECT;
2334     }
2335     if (!data.WriteBool(add)) {
2336         return ERR_FLATTEN_OBJECT;
2337     }
2338 
2339     sptr<IRemoteObject> remote = Remote();
2340     if (remote == nullptr) {
2341         NETNATIVE_LOGE("Remote is null");
2342         return ERR_FLATTEN_OBJECT;
2343     }
2344     MessageParcel reply;
2345     MessageOption option;
2346     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_UPDATE_IFACE_CONFIG),
2347         data, reply, option);
2348     if (result != ERR_NONE) {
2349         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2350         return IPC_INVOKER_ERR;
2351     }
2352     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2353     if (!reply.ReadInt32(ret)) {
2354         NETNATIVE_LOGE("Read result failed");
2355         return ERR_FLATTEN_OBJECT;
2356     }
2357     return ret;
2358 }
2359 
NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)2360 int32_t NetsysNativeServiceProxy::NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)
2361 {
2362     NETNATIVE_LOGI("Begin to NetDiagSetInterfaceActiveState");
2363     MessageParcel data;
2364     if (!WriteInterfaceToken(data)) {
2365         return ERR_FLATTEN_OBJECT;
2366     }
2367     if (!data.WriteString(ifaceName)) {
2368         return ERR_FLATTEN_OBJECT;
2369     }
2370     if (!data.WriteBool(up)) {
2371         return ERR_FLATTEN_OBJECT;
2372     }
2373 
2374     sptr<IRemoteObject> remote = Remote();
2375     if (remote == nullptr) {
2376         NETNATIVE_LOGE("Remote is null");
2377         return ERR_FLATTEN_OBJECT;
2378     }
2379     MessageParcel reply;
2380     MessageOption option;
2381     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_SET_IFACE_ACTIVE_STATE),
2382         data, reply, option);
2383     if (result != ERR_NONE) {
2384         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2385         return IPC_INVOKER_ERR;
2386     }
2387     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2388     if (!reply.ReadInt32(ret)) {
2389         NETNATIVE_LOGE("Read result failed");
2390         return ERR_FLATTEN_OBJECT;
2391     }
2392     return ret;
2393 }
2394 
AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)2395 int32_t NetsysNativeServiceProxy::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
2396                                                const std::string &ifName)
2397 {
2398     MessageParcel data;
2399     if (!WriteInterfaceToken(data) || !data.WriteString(ipAddr) || !data.WriteString(macAddr) ||
2400         !data.WriteString(ifName)) {
2401         return ERR_FLATTEN_OBJECT;
2402     }
2403 
2404     MessageParcel reply;
2405     MessageOption option;
2406     sptr<IRemoteObject> remote = Remote();
2407     if (remote == nullptr) {
2408         NETNATIVE_LOGE("AddStaticArp Remote is null");
2409         return ERR_FLATTEN_OBJECT;
2410     }
2411     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ADD_STATIC_ARP),
2412                                         data, reply, option);
2413     if (error != ERR_NONE) {
2414         NETNATIVE_LOGE("AddStaticArp proxy SendRequest failed");
2415         return ERR_FLATTEN_OBJECT;
2416     }
2417     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2418     if (!reply.ReadInt32(ret)) {
2419         NETNATIVE_LOGE("AddStaticArp proxy read ret failed");
2420         return ERR_FLATTEN_OBJECT;
2421     }
2422     return ret;
2423 }
2424 
DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)2425 int32_t NetsysNativeServiceProxy::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
2426                                                const std::string &ifName)
2427 {
2428     MessageParcel data;
2429     if (!WriteInterfaceToken(data) || !data.WriteString(ipAddr) || !data.WriteString(macAddr) ||
2430         !data.WriteString(ifName)) {
2431         return ERR_FLATTEN_OBJECT;
2432     }
2433 
2434     MessageParcel reply;
2435     MessageOption option;
2436     sptr<IRemoteObject> remote = Remote();
2437     if (remote == nullptr) {
2438         NETNATIVE_LOGE("DelStaticArp Remote is null");
2439         return ERR_FLATTEN_OBJECT;
2440     }
2441     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_STATIC_ARP),
2442                                         data, reply, option);
2443     if (error != ERR_NONE) {
2444         NETNATIVE_LOGE("DelStaticArp proxy SendRequest failed");
2445         return ERR_FLATTEN_OBJECT;
2446     }
2447     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2448     if (!reply.ReadInt32(ret)) {
2449         NETNATIVE_LOGE("DelStaticArp proxy read ret failed");
2450         return ERR_FLATTEN_OBJECT;
2451     }
2452     return ret;
2453 }
2454 
RegisterDnsResultCallback( const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep)2455 int32_t NetsysNativeServiceProxy::RegisterDnsResultCallback(
2456     const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep)
2457 {
2458     NETNATIVE_LOGI("Begin to RegisterDnsResultCallback");
2459     if (callback == nullptr) {
2460         NETNATIVE_LOGE("INetDnsResultCallback is nullptr");
2461         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2462     }
2463     MessageParcel data;
2464     if (!WriteInterfaceToken(data)) {
2465         return ERR_FLATTEN_OBJECT;
2466     }
2467     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2468         return ERR_FLATTEN_OBJECT;
2469     }
2470     if (!data.WriteUint32(timeStep)) {
2471         return ERR_FLATTEN_OBJECT;
2472     }
2473 
2474     sptr<IRemoteObject> remote = Remote();
2475     if (remote == nullptr) {
2476         NETNATIVE_LOGE("Remote is null");
2477         return ERR_FLATTEN_OBJECT;
2478     }
2479     MessageParcel reply;
2480     MessageOption option;
2481     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_RESULT_LISTENER),
2482         data, reply, option);
2483     if (result != ERR_NONE) {
2484         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2485         return IPC_INVOKER_ERR;
2486     }
2487     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2488     if (!reply.ReadInt32(ret)) {
2489         NETNATIVE_LOGE("Read result failed");
2490         return ERR_FLATTEN_OBJECT;
2491     }
2492     return ret;
2493 }
2494 
UnregisterDnsResultCallback( const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback)2495 int32_t NetsysNativeServiceProxy::UnregisterDnsResultCallback(
2496     const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback)
2497 {
2498     NETNATIVE_LOGI("Begin to UnregisterDnsResultCallback");
2499     if (callback == nullptr) {
2500         NETNATIVE_LOGE("INetDnsResultCallback is nullptr");
2501         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2502     }
2503     MessageParcel data;
2504     if (!WriteInterfaceToken(data)) {
2505         return ERR_FLATTEN_OBJECT;
2506     }
2507     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2508         return ERR_FLATTEN_OBJECT;
2509     }
2510 
2511     sptr<IRemoteObject> remote = Remote();
2512     if (remote == nullptr) {
2513         NETNATIVE_LOGE("Remote is null");
2514         return ERR_FLATTEN_OBJECT;
2515     }
2516     MessageParcel reply;
2517     MessageOption option;
2518     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_RESULT_LISTENER),
2519         data, reply, option);
2520     if (result != ERR_NONE) {
2521         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2522         return IPC_INVOKER_ERR;
2523     }
2524     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2525     if (!reply.ReadInt32(ret)) {
2526         NETNATIVE_LOGE("Read result failed");
2527         return ERR_FLATTEN_OBJECT;
2528     }
2529     return ret;
2530 }
2531 
RegisterDnsHealthCallback( const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)2532 int32_t NetsysNativeServiceProxy::RegisterDnsHealthCallback(
2533     const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
2534 {
2535     NETNATIVE_LOGI("Begin to RegisterDnsHealthCallback");
2536     if (callback == nullptr) {
2537         NETNATIVE_LOGE("INetDnsHealthCallback is nullptr");
2538         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2539     }
2540     MessageParcel data;
2541     if (!WriteInterfaceToken(data)) {
2542         return ERR_FLATTEN_OBJECT;
2543     }
2544     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2545         return ERR_FLATTEN_OBJECT;
2546     }
2547 
2548     sptr<IRemoteObject> remote = Remote();
2549     if (remote == nullptr) {
2550         NETNATIVE_LOGE("Remote is null");
2551         return ERR_FLATTEN_OBJECT;
2552     }
2553     MessageParcel reply;
2554     MessageOption option;
2555     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_HEALTH_LISTENER),
2556         data, reply, option);
2557     if (result != ERR_NONE) {
2558         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2559         return IPC_INVOKER_ERR;
2560     }
2561     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2562     if (!reply.ReadInt32(ret)) {
2563         NETNATIVE_LOGE("Read result failed");
2564         return ERR_FLATTEN_OBJECT;
2565     }
2566     return ret;
2567 }
2568 
UnregisterDnsHealthCallback( const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)2569 int32_t NetsysNativeServiceProxy::UnregisterDnsHealthCallback(
2570     const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
2571 {
2572     NETNATIVE_LOGI("Begin to UnregisterDnsHealthCallback");
2573     if (callback == nullptr) {
2574         NETNATIVE_LOGE("INetDnsHealthCallback is nullptr");
2575         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2576     }
2577     MessageParcel data;
2578     if (!WriteInterfaceToken(data)) {
2579         return ERR_FLATTEN_OBJECT;
2580     }
2581     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2582         return ERR_FLATTEN_OBJECT;
2583     }
2584 
2585     sptr<IRemoteObject> remote = Remote();
2586     if (remote == nullptr) {
2587         NETNATIVE_LOGE("Remote is null");
2588         return ERR_FLATTEN_OBJECT;
2589     }
2590     MessageParcel reply;
2591     MessageOption option;
2592     int result = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_HEALTH_LISTENER),
2593         data, reply, option);
2594     if (result != ERR_NONE) {
2595         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2596         return IPC_INVOKER_ERR;
2597     }
2598     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2599     if (!reply.ReadInt32(ret)) {
2600         NETNATIVE_LOGE("Read result failed");
2601         return ERR_FLATTEN_OBJECT;
2602     }
2603     return ret;
2604 }
2605 
GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)2606 int32_t NetsysNativeServiceProxy::GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)
2607 {
2608     MessageParcel data;
2609     if (!WriteInterfaceToken(data)) {
2610         return ERR_FLATTEN_OBJECT;
2611     }
2612     if (!data.WriteUint32(type)) {
2613         return ERR_FLATTEN_OBJECT;
2614     }
2615     if (!data.WriteUint64(cookie)) {
2616         return ERR_FLATTEN_OBJECT;
2617     }
2618     MessageParcel reply;
2619     MessageOption option;
2620     int32_t res = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_COOKIE_STATS),
2621                                         data, reply, option);
2622     if (res != ERR_NONE) {
2623         NETNATIVE_LOGE("GetCookieStats SendRequest failed");
2624         return ERR_FLATTEN_OBJECT;
2625     }
2626 
2627     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2628     if (!reply.ReadInt32(ret)) {
2629         NETNATIVE_LOGE("get ret falil");
2630         return ERR_FLATTEN_OBJECT;
2631     }
2632 
2633     if (!reply.ReadUint64(stats)) {
2634         NETNATIVE_LOGE("get stats falil");
2635         return ERR_FLATTEN_OBJECT;
2636     }
2637     return ret;
2638 }
2639 
GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn)2640 int32_t NetsysNativeServiceProxy::GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn)
2641 {
2642     NETNATIVE_LOGI("GetNetworkSharingType in");
2643     MessageParcel data;
2644     if (!WriteInterfaceToken(data)) {
2645         return ERR_FLATTEN_OBJECT;
2646     }
2647     MessageParcel reply;
2648     MessageOption option;
2649     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_NETWORK_SHARING_TYPE),
2650                                         data, reply, option);
2651     if (ret != ERR_NONE) {
2652         NETNATIVE_LOGE("GetNetworkSharingType SendRequest failed");
2653         return ERR_FLATTEN_OBJECT;
2654     }
2655 
2656     ret = NetManagerStandard::NETMANAGER_SUCCESS;
2657     if (!reply.ReadInt32(ret)) {
2658         NETNATIVE_LOGE("get ret falil");
2659         return ERR_FLATTEN_OBJECT;
2660     }
2661 
2662     uint32_t count = ERR_NONE;
2663     if (!reply.ReadUint32(count)) {
2664         NETNATIVE_LOGE("get ret falil");
2665         return ERR_FLATTEN_OBJECT;
2666     }
2667     uint32_t tmp = ERR_NONE;
2668     for (size_t index = 0; index < count; ++index) {
2669         if (!reply.ReadUint32(tmp)) {
2670             NETNATIVE_LOGE("GetNetworkSharingType falil");
2671             return ERR_FLATTEN_OBJECT;
2672         }
2673         sharingTypeIsOn.insert(tmp);
2674     }
2675 
2676     return ret;
2677 }
2678 
UpdateNetworkSharingType(uint32_t type, bool isOpen)2679 int32_t NetsysNativeServiceProxy::UpdateNetworkSharingType(uint32_t type, bool isOpen)
2680 {
2681     NETNATIVE_LOGI("UpdateNetworkSharingType");
2682     MessageParcel data;
2683     if (!WriteInterfaceToken(data)) {
2684         return ERR_FLATTEN_OBJECT;
2685     }
2686     if (!data.WriteUint32(type)) {
2687         return ERR_FLATTEN_OBJECT;
2688     }
2689     if (!data.WriteBool(isOpen)) {
2690         return ERR_FLATTEN_OBJECT;
2691     }
2692     MessageParcel reply;
2693     MessageOption option;
2694     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UPDATE_NETWORK_SHARING_TYPE),
2695                                         data, reply, option);
2696     if (ret != ERR_NONE) {
2697         NETNATIVE_LOGE("UpdateNetworkSharingType SendRequest failed");
2698         return ERR_FLATTEN_OBJECT;
2699     }
2700 
2701     ret = NetManagerStandard::NETMANAGER_SUCCESS;
2702     if (!reply.ReadInt32(ret)) {
2703         NETNATIVE_LOGE("UpdateNetworkSharingType get ret falil");
2704         return ERR_FLATTEN_OBJECT;
2705     }
2706 
2707     return ret;
2708 }
2709 
2710 #ifdef FEATURE_NET_FIREWALL_ENABLE
SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList, bool isFinish)2711 int32_t NetsysNativeServiceProxy::SetFirewallRules(NetFirewallRuleType type,
2712                                                    const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
2713                                                    bool isFinish)
2714 {
2715     NETNATIVE_LOGI("NetsysNativeServiceProxy::SetFirewallRules: ruleList size=%{public}zu type=%{public}d",
2716                    ruleList.size(), type);
2717     auto it = ruleList.begin();
2718     uint32_t pageSize = type == NetFirewallRuleType::RULE_IP ? FIREWALL_IPC_IP_RULE_PAGE_SIZE : FIREWALL_RULE_SIZE_MAX;
2719     uint32_t offset = 0;
2720     uint32_t remain;
2721     while (it != ruleList.end()) {
2722         remain = static_cast<uint32_t>(ruleList.end() - it);
2723         offset = std::min(remain, pageSize);
2724         MessageParcel data;
2725         if (!WriteInterfaceToken(data)) {
2726             return ERR_FLATTEN_OBJECT;
2727         }
2728         if (!data.WriteInt32(static_cast<int32_t>(type))) {
2729             return ERR_FLATTEN_OBJECT;
2730         }
2731         if (!data.WriteUint32(offset)) {
2732             return ERR_FLATTEN_OBJECT;
2733         }
2734         if (!data.WriteBool(offset == remain)) {
2735             return ERR_FLATTEN_OBJECT;
2736         }
2737         for (uint32_t i = 0; i < offset && it != ruleList.end(); i++, it++) {
2738             if (!(*it)->Marshalling(data)) {
2739                 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
2740             }
2741         }
2742         MessageParcel reply;
2743         MessageOption option;
2744         int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_RULES),
2745                                             data, reply, option);
2746         if (ret != ERR_NONE) {
2747             NETNATIVE_LOGE("SetFirewallRules SendRequest failed");
2748             return ret;
2749         }
2750     }
2751 
2752     return NetManagerStandard::NETMANAGER_SUCCESS;
2753 }
2754 
SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)2755 int32_t NetsysNativeServiceProxy::SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)
2756 {
2757     NETNATIVE_LOGI("NetsysNativeServiceProxy::SetFirewallDefaultAction in=%{public}d out=%{public}d", inDefault,
2758                    outDefault);
2759     MessageParcel data;
2760     if (!WriteInterfaceToken(data)) {
2761         return ERR_FLATTEN_OBJECT;
2762     }
2763     if (!data.WriteInt32(static_cast<int32_t>(inDefault))) {
2764         return ERR_FLATTEN_OBJECT;
2765     }
2766     if (!data.WriteInt32(static_cast<int32_t>(outDefault))) {
2767         return ERR_FLATTEN_OBJECT;
2768     }
2769     MessageParcel reply;
2770     MessageOption option;
2771     int32_t ret = Remote()->SendRequest(
2772         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_DEFAULT_ACTION), data, reply, option);
2773     if (ret != ERR_NONE) {
2774         NETNATIVE_LOGE("SetFirewallDefaultAction SendRequest failed");
2775         return ret;
2776     }
2777 
2778     return NetManagerStandard::NETMANAGER_SUCCESS;
2779 }
2780 
SetFirewallCurrentUserId(int32_t userId)2781 int32_t NetsysNativeServiceProxy::SetFirewallCurrentUserId(int32_t userId)
2782 {
2783     NETNATIVE_LOGI("NetsysNativeServiceProxy::SetFirewallCurrentUserId userId=%{public}d", userId);
2784     MessageParcel data;
2785     if (!WriteInterfaceToken(data)) {
2786         return ERR_FLATTEN_OBJECT;
2787     }
2788     if (!data.WriteInt32(userId)) {
2789         return ERR_FLATTEN_OBJECT;
2790     }
2791     MessageParcel reply;
2792     MessageOption option;
2793     int32_t ret = Remote()->SendRequest(
2794         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_USER_ID), data, reply, option);
2795     if (ret != ERR_NONE) {
2796         NETNATIVE_LOGE("SetFirewallCurrentUserId SendRequest failed");
2797         return ret;
2798     }
2799 
2800     return NetManagerStandard::NETMANAGER_SUCCESS;
2801 }
2802 
ClearFirewallRules(NetFirewallRuleType type)2803 int32_t NetsysNativeServiceProxy::ClearFirewallRules(NetFirewallRuleType type)
2804 {
2805     NETNATIVE_LOGI("NetsysNativeServiceProxy::ClearFirewallRules type=%{public}d", type);
2806     MessageParcel data;
2807     if (!WriteInterfaceToken(data)) {
2808         return ERR_FLATTEN_OBJECT;
2809     }
2810     if (!data.WriteInt32(static_cast<int32_t>(type))) {
2811         return ERR_FLATTEN_OBJECT;
2812     }
2813     MessageParcel reply;
2814     MessageOption option;
2815     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_CLEAR_RULES),
2816         data, reply, option);
2817     if (ret != ERR_NONE) {
2818         NETNATIVE_LOGE("ClearFirewallRules SendRequest failed");
2819         return ret;
2820     }
2821 
2822     return NetManagerStandard::NETMANAGER_SUCCESS;
2823 }
2824 
RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)2825 int32_t NetsysNativeServiceProxy::RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)
2826 {
2827     NETNATIVE_LOGI("Begin to RegisterFirewallCallback");
2828     if (callback == nullptr) {
2829         NETNATIVE_LOGE("FirewallCallback is nullptr");
2830         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2831     }
2832     MessageParcel data;
2833     if (!WriteInterfaceToken(data)) {
2834         return ERR_FLATTEN_OBJECT;
2835     }
2836     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2837         return ERR_FLATTEN_OBJECT;
2838     }
2839     MessageParcel reply;
2840     MessageOption option;
2841     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_REGISTER), data,
2842         reply, option);
2843     if (ret != ERR_NONE) {
2844         NETNATIVE_LOGE("RegisterFirewallCallback SendRequest failed");
2845         return ret;
2846     }
2847 
2848     return NetManagerStandard::NETMANAGER_SUCCESS;
2849 }
2850 
UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)2851 int32_t NetsysNativeServiceProxy::UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)
2852 {
2853     NETNATIVE_LOGI("Begin to UnRegisterNetFirewallCallback");
2854     if (callback == nullptr) {
2855         NETNATIVE_LOGE("FirewallCallback is nullptr");
2856         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2857     }
2858     MessageParcel data;
2859     if (!WriteInterfaceToken(data)) {
2860         return ERR_FLATTEN_OBJECT;
2861     }
2862     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2863         return ERR_FLATTEN_OBJECT;
2864     }
2865     MessageParcel reply;
2866     MessageOption option;
2867     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_UNREGISTER),
2868         data, reply, option);
2869     if (ret != ERR_NONE) {
2870         NETNATIVE_LOGE("UnRegisterNetFirewallCallback SendRequest failed");
2871         return ret;
2872     }
2873 
2874     return NetManagerStandard::NETMANAGER_SUCCESS;
2875 }
2876 #endif
2877 
2878 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId)2879 int32_t NetsysNativeServiceProxy::EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId)
2880 {
2881     MessageParcel data;
2882     if (!WriteInterfaceToken(data)) {
2883         return ERR_FLATTEN_OBJECT;
2884     }
2885     if (!data.WriteInt32(tcpPortId)) {
2886         return ERR_FLATTEN_OBJECT;
2887     }
2888     if (!data.WriteInt32(udpPortId)) {
2889         return ERR_FLATTEN_OBJECT;
2890     }
2891     MessageParcel reply;
2892     MessageOption option;
2893     sptr<IRemoteObject> remote = Remote();
2894     if (remote == nullptr) {
2895         NETNATIVE_LOGE("Remote is null in EnableWearableDistributedNetForward");
2896         return NETMANAGER_ERR_LOCAL_PTR_NULL;
2897     }
2898     int32_t ret = remote->SendRequest(
2899         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_WEARABLE_DISTRIBUTED_NET_FORWARD),
2900         data, reply, option);
2901     if (ret != ERR_NONE) {
2902         NETNATIVE_LOGE("EnableWearableDistributedNetForward SendRequest failed");
2903         return ret;
2904     }
2905 
2906     return reply.ReadInt32();
2907 }
2908 
DisableWearableDistributedNetForward()2909 int32_t NetsysNativeServiceProxy::DisableWearableDistributedNetForward()
2910 {
2911     NETNATIVE_LOGI("NetsysNativeServiceProxy DisableWearableDistributedNetForward");
2912     MessageParcel data;
2913     if (!WriteInterfaceToken(data)) {
2914         return ERR_FLATTEN_OBJECT;
2915     }
2916     MessageParcel reply;
2917     MessageOption option;
2918     sptr<IRemoteObject> remote = Remote();
2919     if (remote == nullptr) {
2920         NETNATIVE_LOGE("Remote is null in DisableWearableDistributedNetForward");
2921         return NETMANAGER_ERR_LOCAL_PTR_NULL;
2922     }
2923     int32_t ret = remote->SendRequest(
2924         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_WEARABLE_DISTRIBUTED_NET_FORWARD),
2925         data, reply, option);
2926     if (ret != ERR_NONE) {
2927         NETNATIVE_LOGE("DisableWearableDistributedNetForward SendRequest failed");
2928         return ret;
2929     }
2930 
2931     return reply.ReadInt32();
2932 }
2933 #endif
2934 
SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)2935 int32_t NetsysNativeServiceProxy::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
2936 {
2937     NETNATIVE_LOGI("Begin to SetIpv6PrivacyExtensions");
2938     MessageParcel data;
2939     if (!WriteInterfaceToken(data)) {
2940         return ERR_FLATTEN_OBJECT;
2941     }
2942     if (!data.WriteString(interfaceName)) {
2943         return ERR_FLATTEN_OBJECT;
2944     }
2945     if (!data.WriteUint32(on)) {
2946         return ERR_FLATTEN_OBJECT;
2947     }
2948 
2949     MessageParcel reply;
2950     MessageOption option;
2951     int result =
2952         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_IPV6_PRIVCAY_EXTENSION),
2953             data, reply, option);
2954     if (result != ERR_NONE) {
2955         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2956         return IPC_INVOKER_ERR;
2957     }
2958     return reply.ReadInt32();
2959 }
2960 
SetEnableIpv6(const std::string &interfaceName, const uint32_t on)2961 int32_t NetsysNativeServiceProxy::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
2962 {
2963     NETNATIVE_LOGI("Begin to SetEnableIpv6");
2964     MessageParcel data;
2965     if (!WriteInterfaceToken(data)) {
2966         return ERR_FLATTEN_OBJECT;
2967     }
2968     if (!data.WriteString(interfaceName)) {
2969         return ERR_FLATTEN_OBJECT;
2970     }
2971     if (!data.WriteUint32(on)) {
2972         return ERR_FLATTEN_OBJECT;
2973     }
2974 
2975     MessageParcel reply;
2976     MessageOption option;
2977     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ENABLE_IPV6),
2978         data, reply, option);
2979     if (result != ERR_NONE) {
2980         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
2981         return IPC_INVOKER_ERR;
2982     }
2983     return reply.ReadInt32();
2984 }
2985 
SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag, bool isBroker)2986 int32_t NetsysNativeServiceProxy::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
2987                                                          bool isBroker)
2988 {
2989     NETNATIVE_LOGI("SetNetworkAccessPolicy");
2990     MessageParcel data;
2991     if (!WriteInterfaceToken(data)) {
2992         return ERR_FLATTEN_OBJECT;
2993     }
2994 
2995     if (!data.WriteUint32(uid)) {
2996         return ERR_FLATTEN_OBJECT;
2997     }
2998 
2999     if (!data.WriteUint8(policy.wifiAllow)) {
3000         return ERR_FLATTEN_OBJECT;
3001     }
3002 
3003     if (!data.WriteUint8(policy.cellularAllow)) {
3004         return ERR_FLATTEN_OBJECT;
3005     }
3006 
3007     if (!data.WriteBool(reconfirmFlag)) {
3008         return ERR_FLATTEN_OBJECT;
3009     }
3010 
3011     if (!data.WriteBool(isBroker)) {
3012         return ERR_FLATTEN_OBJECT;
3013     }
3014     MessageParcel reply;
3015     MessageOption option;
3016     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_NETWORK_ACCESS_POLICY),
3017         data, reply, option);
3018     if (result != ERR_NONE) {
3019         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
3020         return IPC_INVOKER_ERR;
3021     }
3022     return reply.ReadInt32();
3023 }
3024 
DeleteNetworkAccessPolicy(uint32_t uid)3025 int32_t NetsysNativeServiceProxy::DeleteNetworkAccessPolicy(uint32_t uid)
3026 {
3027     NETNATIVE_LOGI("DeleteNetworkAccessPolicy");
3028     MessageParcel data;
3029 
3030     if (!WriteInterfaceToken(data)) {
3031         return ERR_FLATTEN_OBJECT;
3032     }
3033 
3034     if (!data.WriteUint32(uid)) {
3035         return ERR_FLATTEN_OBJECT;
3036     }
3037     MessageParcel reply;
3038     MessageOption option;
3039     int result = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_NETWORK_ACCESS_POLICY),
3040         data, reply, option);
3041     if (result != ERR_NONE) {
3042         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
3043         return IPC_INVOKER_ERR;
3044     }
3045     return reply.ReadInt32();
3046 }
3047 
ClearFirewallAllRules()3048 int32_t NetsysNativeServiceProxy::ClearFirewallAllRules()
3049 {
3050     NETNATIVE_LOGI("ClearFirewallAllRules");
3051     MessageParcel data;
3052     if (!WriteInterfaceToken(data)) {
3053         return ERR_FLATTEN_OBJECT;
3054     }
3055 
3056     MessageParcel reply;
3057     MessageOption option;
3058     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CLEAR_FIREWALL_RULE), data, reply,
3059                           option);
3060 
3061     return reply.ReadInt32();
3062 }
3063 
NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)3064 int32_t NetsysNativeServiceProxy::NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)
3065 {
3066     NETNATIVE_LOGI("NotifyNetBearerTypeChange");
3067     MessageParcel data;
3068 
3069     if (!WriteInterfaceToken(data)) {
3070         return ERR_FLATTEN_OBJECT;
3071     }
3072 
3073     uint32_t size = static_cast<uint32_t>(bearerTypes.size());
3074     if (!data.WriteUint32(size)) {
3075         return ERR_FLATTEN_OBJECT;
3076     }
3077     for (auto bearerType : bearerTypes) {
3078         if (!data.WriteUint32(static_cast<uint32_t>(bearerType))) {
3079             return ERR_FLATTEN_OBJECT;
3080         }
3081     }
3082 
3083     MessageParcel reply;
3084     MessageOption option;
3085     int result =
3086         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NOTIFY_NETWORK_BEARER_TYPE_CHANGE),
3087             data, reply, option);
3088     if (result != ERR_NONE) {
3089         NETNATIVE_LOGE("SendRequest failed, error code: [%{public}d]", result);
3090         return IPC_INVOKER_ERR;
3091     }
3092     return reply.ReadInt32();
3093 }
3094 
StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr)3095 int32_t NetsysNativeServiceProxy::StartClat(const std::string &interfaceName, int32_t netId,
3096                                             const std::string &nat64PrefixStr)
3097 {
3098     MessageParcel data;
3099     if (!WriteInterfaceToken(data)) {
3100         return ERR_FLATTEN_OBJECT;
3101     }
3102     if (!data.WriteString(interfaceName)) {
3103         return ERR_FLATTEN_OBJECT;
3104     }
3105     if (!data.WriteInt32(netId)) {
3106         return ERR_FLATTEN_OBJECT;
3107     }
3108     if (!data.WriteString(nat64PrefixStr)) {
3109         return ERR_FLATTEN_OBJECT;
3110     }
3111 
3112     MessageParcel reply;
3113     MessageOption option;
3114     sptr<IRemoteObject> remote = Remote();
3115     if (remote == nullptr) {
3116         return IPC_PROXY_NULL_INVOKER_ERR;
3117     }
3118     int32_t ret =
3119         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_START_CLAT), data, reply, option);
3120     if (ret != ERR_NONE) {
3121         NETNATIVE_LOGE("StartClat proxy SendRequest failed, error code: [%{public}d]", ret);
3122         return IPC_INVOKER_ERR;
3123     }
3124 
3125     int32_t result = ERR_INVALID_DATA;
3126     if (!reply.ReadInt32(result)) {
3127         return IPC_PROXY_TRANSACTION_ERR;
3128     }
3129     return result;
3130 }
3131 
StopClat(const std::string &interfaceName)3132 int32_t NetsysNativeServiceProxy::StopClat(const std::string &interfaceName)
3133 {
3134     MessageParcel data;
3135     if (!WriteInterfaceToken(data)) {
3136         return ERR_FLATTEN_OBJECT;
3137     }
3138     if (!data.WriteString(interfaceName)) {
3139         return ERR_FLATTEN_OBJECT;
3140     }
3141 
3142     MessageParcel reply;
3143     MessageOption option;
3144     sptr<IRemoteObject> remote = Remote();
3145     if (remote == nullptr) {
3146         return IPC_PROXY_NULL_INVOKER_ERR;
3147     }
3148     int32_t ret =
3149         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_STOP_CLAT), data, reply, option);
3150     if (ret != ERR_NONE) {
3151         NETNATIVE_LOGE("StopClat proxy SendRequest failed, error code: [%{public}d]", ret);
3152         return IPC_INVOKER_ERR;
3153     }
3154 
3155     int32_t result = ERR_INVALID_DATA;
3156     if (!reply.ReadInt32(result)) {
3157         return IPC_PROXY_TRANSACTION_ERR;
3158     }
3159     return result;
3160 }
3161 
SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)3162 int32_t NetsysNativeServiceProxy::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
3163 {
3164     MessageParcel data;
3165     if (!WriteInterfaceToken(data)) {
3166         return ERR_FLATTEN_OBJECT;
3167     }
3168     NETNATIVE_LOG_D("SetNicTrafficAllowed WriteParam func in");
3169     if (!data.WriteBool(status)) {
3170         NETNATIVE_LOGE("SetNicTrafficAllowed WriteBool func return error");
3171         return ERR_FLATTEN_OBJECT;
3172     }
3173     if (!data.WriteInt32(ifaceNames.size())) {
3174         NETNATIVE_LOGE("SetNicTrafficAllowed ifaceNames size return error");
3175         return ERR_FLATTEN_OBJECT;
3176     }
3177     for (const std::string& iter : ifaceNames) {
3178         if (!data.WriteString(iter)) {
3179             NETNATIVE_LOGE("SetNicTrafficAllowed write name return error");
3180             return ERR_FLATTEN_OBJECT;
3181         }
3182     }
3183     MessageParcel reply;
3184     MessageOption option;
3185     if (Remote() == nullptr) {
3186         NETNATIVE_LOGE("SetNicTrafficAllowed remote pointer is null");
3187         return ERR_FLATTEN_OBJECT;
3188     }
3189     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_NIC_TRAFFIC_ALLOWED),
3190         data, reply, option);
3191     if (error != ERR_NONE) {
3192         NETNATIVE_LOGE("SetNicTrafficAllowed proxy sendRequest failed");
3193         return ERR_FLATTEN_OBJECT;
3194     }
3195     int32_t ret;
3196     if (!reply.ReadInt32(ret)) {
3197         NETNATIVE_LOGE("SetNicTrafficAllowed proxy read ret failed");
3198         return ERR_FLATTEN_OBJECT;
3199     }
3200     NETNATIVE_LOG_D("SetNicTrafficAllowed WriteParam func out");
3201     return ret;
3202 }
3203 
CloseSocketsUid(const std::string &ipAddr, uint32_t uid)3204 int32_t NetsysNativeServiceProxy::CloseSocketsUid(const std::string &ipAddr, uint32_t uid)
3205 {
3206     MessageParcel data;
3207     if (!WriteInterfaceToken(data)) {
3208         return ERR_FLATTEN_OBJECT;
3209     }
3210     if (!data.WriteString(ipAddr)) {
3211         return ERR_FLATTEN_OBJECT;
3212     }
3213     if (!data.WriteUint32(uid)) {
3214         return ERR_FLATTEN_OBJECT;
3215     }
3216 
3217     MessageParcel reply;
3218     MessageOption option;
3219     sptr<IRemoteObject> remote = Remote();
3220     if (remote == nullptr) {
3221         return IPC_PROXY_NULL_INVOKER_ERR;
3222     }
3223     int32_t ret =
3224         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CLOSE_SOCKETS_UID), data, reply, option);
3225     if (ret != ERR_NONE) {
3226         NETNATIVE_LOGE("CloseSocketsUid proxy SendRequest failed, error code: [%{public}d]", ret);
3227         return IPC_INVOKER_ERR;
3228     }
3229 
3230     int32_t result = ERR_INVALID_DATA;
3231     if (!reply.ReadInt32(result)) {
3232         return IPC_PROXY_TRANSACTION_ERR;
3233     }
3234     return result;
3235 }
3236 
3237 #ifdef SUPPORT_SYSVPN
ProcessVpnStage(NetsysNative::SysVpnStageCode stage)3238 int32_t NetsysNativeServiceProxy::ProcessVpnStage(NetsysNative::SysVpnStageCode stage)
3239 {
3240     MessageParcel data;
3241     if (!WriteInterfaceToken(data)) {
3242         return ERR_FLATTEN_OBJECT;
3243     }
3244     if (!data.WriteInt32(stage)) {
3245         NETNATIVE_LOGE("ProcessVpnStage write stage error");
3246         return ERR_FLATTEN_OBJECT;
3247     }
3248 
3249     MessageParcel reply;
3250     MessageOption option;
3251     sptr<IRemoteObject> remote = Remote();
3252     if (remote == nullptr) {
3253         return IPC_PROXY_NULL_INVOKER_ERR;
3254     }
3255     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_PROCESS_VPN_STAGE),
3256         data, reply, option);
3257     if (ret != ERR_NONE) {
3258         NETNATIVE_LOGE("ProcessVpnStage proxy SendRequest failed, ret: [%{public}d]", ret);
3259         return IPC_INVOKER_ERR;
3260     }
3261 
3262     int32_t result = ERR_INVALID_DATA;
3263     if (!reply.ReadInt32(result)) {
3264         NETNATIVE_LOGE("ProcessVpnStage proxy read result failed");
3265         return IPC_PROXY_TRANSACTION_ERR;
3266     }
3267     return result;
3268 }
3269 #endif // SUPPORT_SYSVPN
3270 } // namespace NetsysNative
3271 } // namespace OHOS
3272