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