• 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 "netnative_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace NetsysNative {
25 static constexpr uint32_t UIDS_LIST_MAX_SIZE = 1024;
26 static constexpr int32_t MAX_DNS_CONFIG_SIZE = 4;
27 static constexpr int32_t MAX_INTERFACE_CONFIG_SIZE = 16;
28 
29 namespace {
WriteNatDataToMessage(MessageParcel & data,const std::string & downstreamIface,const std::string & upstreamIface)30 bool WriteNatDataToMessage(MessageParcel &data, const std::string &downstreamIface, const std::string &upstreamIface)
31 {
32     if (!data.WriteInterfaceToken(NetsysNativeServiceProxy::GetDescriptor())) {
33         NETNATIVE_LOGI("WriteInterfaceToken failed");
34         return false;
35     }
36     if (!data.WriteString(downstreamIface)) {
37         return false;
38     }
39     if (!data.WriteString(upstreamIface)) {
40         return false;
41     }
42     return true;
43 }
44 } // namespace
45 
WriteInterfaceToken(MessageParcel & data)46 bool NetsysNativeServiceProxy::WriteInterfaceToken(MessageParcel &data)
47 {
48     if (!data.WriteInterfaceToken(NetsysNativeServiceProxy::GetDescriptor())) {
49         NETNATIVE_LOGI("WriteInterfaceToken failed");
50         return false;
51     }
52     return true;
53 }
54 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)55 int32_t NetsysNativeServiceProxy::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
56                                                     const std::vector<std::string> &servers,
57                                                     const std::vector<std::string> &domains)
58 {
59     NETNATIVE_LOGI("Begin to SetResolverConfig %{public}d", retryCount);
60     MessageParcel data;
61     if (!WriteInterfaceToken(data)) {
62         return ERR_FLATTEN_OBJECT;
63     }
64     if (!data.WriteUint16(netId)) {
65         return ERR_FLATTEN_OBJECT;
66     }
67     if (!data.WriteUint16(baseTimeoutMsec)) {
68         return ERR_FLATTEN_OBJECT;
69     }
70     if (!data.WriteUint8(retryCount)) {
71         return ERR_FLATTEN_OBJECT;
72     }
73 
74     auto vServerSize1 = static_cast<int32_t>(servers.size());
75     if (!data.WriteInt32(vServerSize1)) {
76         return ERR_FLATTEN_OBJECT;
77     }
78     std::vector<std::string> vServers;
79     vServers.assign(servers.begin(), servers.end());
80     NETNATIVE_LOGI("PROXY: SetResolverConfig Write Servers  String_SIZE: %{public}d",
81                    static_cast<int32_t>(vServers.size()));
82     for (auto &vServer : vServers) {
83         data.WriteString(vServer);
84     }
85 
86     int vDomainSize1 = static_cast<int>(domains.size());
87     if (!data.WriteInt32(vDomainSize1)) {
88         return ERR_FLATTEN_OBJECT;
89     }
90 
91     std::vector<std::string> vDomains;
92     vDomains.assign(domains.begin(), domains.end());
93     NETNATIVE_LOGI("PROXY: SetResolverConfig Write Domains String_SIZE: %{public}d",
94                    static_cast<int32_t>(vDomains.size()));
95     for (auto &vDomain : vDomains) {
96         data.WriteString(vDomain);
97     }
98 
99     MessageParcel reply;
100     MessageOption option;
101     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_RESOLVER_CONFIG), data, reply, option);
102 
103     return reply.ReadInt32();
104 }
105 
GetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)106 int32_t NetsysNativeServiceProxy::GetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
107                                                     std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
108                                                     uint8_t &retryCount)
109 {
110     MessageParcel data;
111     if (!WriteInterfaceToken(data)) {
112         return ERR_FLATTEN_OBJECT;
113     }
114     if (!data.WriteUint16(netId)) {
115         return ERR_FLATTEN_OBJECT;
116     }
117     MessageParcel reply;
118     MessageOption option;
119     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_RESOLVER_CONFIG), data, reply, option);
120     int result = reply.ReadInt32();
121     if (result != ERR_NONE) {
122         NETNATIVE_LOGE("Fail to GetResolverConfig ret= %{public}d", result);
123         return result;
124     }
125 
126     reply.ReadUint16(baseTimeoutMsec);
127     reply.ReadUint8(retryCount);
128     int32_t vServerSize = reply.ReadInt32();
129     vServerSize = vServerSize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vServerSize;
130     std::vector<std::string> vecString;
131     for (int i = 0; i < vServerSize; i++) {
132         vecString.push_back(reply.ReadString());
133     }
134     if (vServerSize > 0) {
135         servers.assign(vecString.begin(), vecString.end());
136     }
137     int32_t vDomainSize = reply.ReadInt32();
138     vDomainSize = vDomainSize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vDomainSize;
139     std::vector<std::string> vecDomain;
140     for (int i = 0; i < vDomainSize; i++) {
141         vecDomain.push_back(reply.ReadString());
142     }
143     if (vDomainSize > 0) {
144         domains.assign(vecDomain.begin(), vecDomain.end());
145     }
146     NETNATIVE_LOGI("Begin to GetResolverConfig %{public}d", result);
147     return result;
148 }
149 
CreateNetworkCache(uint16_t netId)150 int32_t NetsysNativeServiceProxy::CreateNetworkCache(uint16_t netId)
151 {
152     NETNATIVE_LOGI("Begin to CreateNetworkCache");
153     MessageParcel data;
154     if (!WriteInterfaceToken(data)) {
155         return ERR_FLATTEN_OBJECT;
156     }
157     if (!data.WriteUint16(netId)) {
158         return ERR_FLATTEN_OBJECT;
159     }
160 
161     MessageParcel reply;
162     MessageOption option;
163     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CREATE_NETWORK_CACHE), data, reply, option);
164 
165     return reply.ReadInt32();
166 }
167 
DestroyNetworkCache(uint16_t netId)168 int32_t NetsysNativeServiceProxy::DestroyNetworkCache(uint16_t netId)
169 {
170     NETNATIVE_LOGI("Begin to DestroyNetworkCache");
171     MessageParcel data;
172     if (!WriteInterfaceToken(data)) {
173         return ERR_FLATTEN_OBJECT;
174     }
175     if (!data.WriteUint16(netId)) {
176         return ERR_FLATTEN_OBJECT;
177     }
178 
179     MessageParcel reply;
180     MessageOption option;
181     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DESTROY_NETWORK_CACHE), data, reply,
182                           option);
183 
184     return reply.ReadInt32();
185 }
186 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)187 int32_t NetsysNativeServiceProxy::GetAddrInfo(const std::string &hostName, const std::string &serverName,
188                                               const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
189 {
190     MessageParcel data;
191     if (!WriteInterfaceToken(data) || !data.WriteString(hostName) || !data.WriteString(serverName) ||
192         !data.WriteRawData(&hints, sizeof(AddrInfo)) || !data.WriteUint16(netId)) {
193         return ERR_FLATTEN_OBJECT;
194     }
195 
196     MessageParcel reply;
197     MessageOption option;
198     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ADDR_INFO), data, reply, option);
199 
200     int32_t ret;
201     uint32_t addrSize;
202     if (!reply.ReadInt32(ret) || ret != ERR_NONE || !reply.ReadUint32(addrSize) || addrSize > MAX_RESULTS) {
203         return ERR_INVALID_DATA;
204     }
205 
206     std::vector<AddrInfo> infos;
207     for (uint32_t i = 0; i < addrSize; ++i) {
208         auto p = reply.ReadRawData(sizeof(AddrInfo));
209         if (p == nullptr) {
210             return ERR_INVALID_DATA;
211         }
212 
213         AddrInfo info = {};
214         if (memcpy_s(&info, sizeof(AddrInfo), p, sizeof(AddrInfo)) != EOK) {
215             return ERR_INVALID_DATA;
216         }
217 
218         infos.emplace_back(info);
219     }
220 
221     res = infos;
222     return ret;
223 }
224 
SetInterfaceMtu(const std::string & interfaceName,int32_t mtu)225 int32_t NetsysNativeServiceProxy::SetInterfaceMtu(const std::string &interfaceName, int32_t mtu)
226 {
227     NETNATIVE_LOGI("Begin to SetInterfaceMtu");
228     MessageParcel data;
229     if (!WriteInterfaceToken(data)) {
230         return ERR_FLATTEN_OBJECT;
231     }
232     if (!data.WriteString(interfaceName)) {
233         return ERR_FLATTEN_OBJECT;
234     }
235     if (!data.WriteInt32(mtu)) {
236         return ERR_FLATTEN_OBJECT;
237     }
238 
239     MessageParcel reply;
240     MessageOption option;
241     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_MTU), data, reply, option);
242 
243     return reply.ReadInt32();
244 }
245 
SetTcpBufferSizes(const std::string & tcpBufferSizes)246 int32_t NetsysNativeServiceProxy::SetTcpBufferSizes(const std::string &tcpBufferSizes)
247 {
248     NETNATIVE_LOGI("Begin to SetTcpBufferSizes");
249     MessageParcel data;
250     if (!WriteInterfaceToken(data)) {
251         return ERR_FLATTEN_OBJECT;
252     }
253     if (!data.WriteString(tcpBufferSizes)) {
254         return ERR_FLATTEN_OBJECT;
255     }
256 
257     MessageParcel reply;
258     MessageOption option;
259     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_TCP_BUFFER_SIZES), data, reply, option);
260 
261     return reply.ReadInt32();
262 }
263 
GetInterfaceMtu(const std::string & interfaceName)264 int32_t NetsysNativeServiceProxy::GetInterfaceMtu(const std::string &interfaceName)
265 {
266     NETNATIVE_LOGI("Begin to GetInterfaceMtu");
267     MessageParcel data;
268     if (!WriteInterfaceToken(data)) {
269         return ERR_FLATTEN_OBJECT;
270     }
271     if (!data.WriteString(interfaceName)) {
272         return ERR_FLATTEN_OBJECT;
273     }
274 
275     MessageParcel reply;
276     MessageOption option;
277     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_MTU), data, reply, option);
278 
279     return reply.ReadInt32();
280 }
281 
RegisterNotifyCallback(sptr<INotifyCallback> & callback)282 int32_t NetsysNativeServiceProxy::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
283 {
284     NETNATIVE_LOGI("Begin to RegisterNotifyCallback");
285     MessageParcel data;
286     if (callback == nullptr) {
287         NETNATIVE_LOGE("The parameter of callback is nullptr");
288         return NETMANAGER_ERR_LOCAL_PTR_NULL;
289     }
290 
291     if (!WriteInterfaceToken(data)) {
292         return ERR_FLATTEN_OBJECT;
293     }
294     data.WriteRemoteObject(callback->AsObject().GetRefPtr());
295 
296     MessageParcel reply;
297     MessageOption option;
298     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_NOTIFY_CALLBACK), data, reply,
299                           option);
300 
301     return reply.ReadInt32();
302 }
303 
UnRegisterNotifyCallback(sptr<INotifyCallback> & callback)304 int32_t NetsysNativeServiceProxy::UnRegisterNotifyCallback(sptr<INotifyCallback> &callback)
305 {
306     NETNATIVE_LOGI("Begin to UnRegisterNotifyCallback");
307     MessageParcel data;
308     if (callback == nullptr) {
309         NETNATIVE_LOGE("The parameter of callback is nullptr");
310         return NETMANAGER_ERR_LOCAL_PTR_NULL;
311     }
312 
313     if (!WriteInterfaceToken(data)) {
314         NETNATIVE_LOGE("WriteInterfaceToken fail");
315         return ERR_FLATTEN_OBJECT;
316     }
317 
318     data.WriteRemoteObject(callback->AsObject().GetRefPtr());
319 
320     MessageParcel reply;
321     MessageOption option;
322     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_NOTIFY_CALLBACK), data, reply,
323                           option);
324 
325     return reply.ReadInt32();
326 }
327 
NetworkAddRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)328 int32_t NetsysNativeServiceProxy::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
329                                                   const std::string &destination, const std::string &nextHop)
330 {
331     NETNATIVE_LOGI("Begin to NetworkAddRoute");
332     MessageParcel data;
333     if (!WriteInterfaceToken(data)) {
334         return ERR_FLATTEN_OBJECT;
335     }
336     if (!data.WriteInt32(netId)) {
337         return ERR_FLATTEN_OBJECT;
338     }
339     if (!data.WriteString(interfaceName)) {
340         return ERR_FLATTEN_OBJECT;
341     }
342     if (!data.WriteString(destination)) {
343         return ERR_FLATTEN_OBJECT;
344     }
345     if (!data.WriteString(nextHop)) {
346         return ERR_FLATTEN_OBJECT;
347     }
348 
349     MessageParcel reply;
350     MessageOption option;
351     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE), data, reply, option);
352 
353     return reply.ReadInt32();
354 }
355 
NetworkRemoveRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)356 int32_t NetsysNativeServiceProxy::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
357                                                      const std::string &destination, const std::string &nextHop)
358 {
359     NETNATIVE_LOGI("Begin to NetworkRemoveRoute");
360     MessageParcel data;
361     if (!WriteInterfaceToken(data)) {
362         return ERR_FLATTEN_OBJECT;
363     }
364     if (!data.WriteInt32(netId)) {
365         return ERR_FLATTEN_OBJECT;
366     }
367     if (!data.WriteString(interfaceName)) {
368         return ERR_FLATTEN_OBJECT;
369     }
370     if (!data.WriteString(destination)) {
371         return ERR_FLATTEN_OBJECT;
372     }
373     if (!data.WriteString(nextHop)) {
374         return ERR_FLATTEN_OBJECT;
375     }
376 
377     MessageParcel reply;
378     MessageOption option;
379     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE), data, reply, option);
380 
381     return reply.ReadInt32();
382 }
383 
NetworkAddRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)384 int32_t NetsysNativeServiceProxy::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
385 {
386     NETNATIVE_LOGI("Begin to NetworkAddRouteParcel");
387     MessageParcel data;
388     if (!WriteInterfaceToken(data)) {
389         return ERR_FLATTEN_OBJECT;
390     }
391     if (!data.WriteInt32(netId)) {
392         return ERR_FLATTEN_OBJECT;
393     }
394     if (!data.WriteString(routeInfo.ifName)) {
395         return ERR_FLATTEN_OBJECT;
396     }
397     if (!data.WriteString(routeInfo.destination)) {
398         return ERR_FLATTEN_OBJECT;
399     }
400     if (!data.WriteString(routeInfo.nextHop)) {
401         return ERR_FLATTEN_OBJECT;
402     }
403 
404     MessageParcel reply;
405     MessageOption option;
406     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE_PARCEL), data, reply,
407                           option);
408 
409     return reply.ReadInt32();
410 }
411 
NetworkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)412 int32_t NetsysNativeServiceProxy::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
413 {
414     NETNATIVE_LOGI("Begin to NetworkRemoveRouteParcel");
415     MessageParcel data;
416     if (!WriteInterfaceToken(data)) {
417         return ERR_FLATTEN_OBJECT;
418     }
419     if (!data.WriteInt32(netId)) {
420         return ERR_FLATTEN_OBJECT;
421     }
422     if (!data.WriteString(routeInfo.ifName)) {
423         return ERR_FLATTEN_OBJECT;
424     }
425     if (!data.WriteString(routeInfo.destination)) {
426         return ERR_FLATTEN_OBJECT;
427     }
428     if (!data.WriteString(routeInfo.nextHop)) {
429         return ERR_FLATTEN_OBJECT;
430     }
431 
432     MessageParcel reply;
433     MessageOption option;
434     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE_PARCEL), data, reply,
435                           option);
436 
437     return reply.ReadInt32();
438 }
439 
NetworkSetDefault(int32_t netId)440 int32_t NetsysNativeServiceProxy::NetworkSetDefault(int32_t netId)
441 {
442     NETNATIVE_LOGI("Begin to NetworkSetDefault");
443     MessageParcel data;
444     if (!WriteInterfaceToken(data)) {
445         return ERR_FLATTEN_OBJECT;
446     }
447     if (!data.WriteInt32(netId)) {
448         return ERR_FLATTEN_OBJECT;
449     }
450 
451     MessageParcel reply;
452     MessageOption option;
453     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_DEFAULT), data, reply, option);
454 
455     return reply.ReadInt32();
456 }
457 
NetworkGetDefault()458 int32_t NetsysNativeServiceProxy::NetworkGetDefault()
459 {
460     NETNATIVE_LOGI("Begin to NetworkGetDefault");
461     MessageParcel data;
462     if (!WriteInterfaceToken(data)) {
463         return ERR_FLATTEN_OBJECT;
464     }
465 
466     MessageParcel reply;
467     MessageOption option;
468     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_GET_DEFAULT), data, reply, option);
469 
470     return reply.ReadInt32();
471 }
472 
NetworkClearDefault()473 int32_t NetsysNativeServiceProxy::NetworkClearDefault()
474 {
475     NETNATIVE_LOGI("Begin to NetworkClearDefault");
476     MessageParcel data;
477     if (!WriteInterfaceToken(data)) {
478         return ERR_FLATTEN_OBJECT;
479     }
480 
481     MessageParcel reply;
482     MessageOption option;
483     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CLEAR_DEFAULT), data, reply,
484                           option);
485 
486     return reply.ReadInt32();
487 }
488 
GetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)489 int32_t NetsysNativeServiceProxy::GetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
490                                                 const std::string &parameter, std::string &value)
491 {
492     NETNATIVE_LOGI("Begin to GetSysProcNet");
493     MessageParcel data;
494     if (!WriteInterfaceToken(data)) {
495         return ERR_FLATTEN_OBJECT;
496     }
497 
498     if (!data.WriteInt32(family)) {
499         return ERR_FLATTEN_OBJECT;
500     }
501     if (!data.WriteInt32(which)) {
502         return ERR_FLATTEN_OBJECT;
503     }
504     if (data.WriteString(ifname)) {
505         return ERR_FLATTEN_OBJECT;
506     }
507     if (!data.WriteString(parameter)) {
508         return ERR_FLATTEN_OBJECT;
509     }
510     MessageParcel reply;
511     MessageOption option;
512     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_PROC_SYS_NET), data, reply, option);
513     int32_t ret = reply.ReadInt32();
514     if (ret != ERR_NONE) {
515         NETNATIVE_LOGE("Fail to GetProcSysNet ret= %{public}d", ret);
516         return ret;
517     }
518     std::string valueRsl = reply.ReadString();
519     NETNATIVE_LOGE("NETSYS_GET_PROC_SYS_NET value %{public}s", valueRsl.c_str());
520     value = valueRsl;
521     return ret;
522 }
523 
SetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)524 int32_t NetsysNativeServiceProxy::SetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
525                                                 const std::string &parameter, std::string &value)
526 {
527     NETNATIVE_LOGI("Begin to SetSysProcNet");
528     MessageParcel data;
529     if (!WriteInterfaceToken(data)) {
530         return ERR_FLATTEN_OBJECT;
531     }
532     if (!data.WriteInt32(family)) {
533         return ERR_FLATTEN_OBJECT;
534     }
535     if (!data.WriteInt32(which)) {
536         return ERR_FLATTEN_OBJECT;
537     }
538     if (data.WriteString(ifname)) {
539         return ERR_FLATTEN_OBJECT;
540     }
541     if (!data.WriteString(parameter)) {
542         return ERR_FLATTEN_OBJECT;
543     }
544     if (!data.WriteString(value)) {
545         return ERR_FLATTEN_OBJECT;
546     }
547     MessageParcel reply;
548     MessageOption option;
549     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_PROC_SYS_NET), data, reply, option);
550 
551     return reply.ReadInt32();
552 }
553 
SetInternetPermission(uint32_t uid,uint8_t allow,uint8_t isBroker)554 int32_t NetsysNativeServiceProxy::SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker)
555 {
556     MessageParcel data;
557     if (!WriteInterfaceToken(data)) {
558         return ERR_FLATTEN_OBJECT;
559     }
560 
561     if (!data.WriteUint32(uid)) {
562         return ERR_FLATTEN_OBJECT;
563     }
564 
565     if (!data.WriteUint8(allow)) {
566         return ERR_FLATTEN_OBJECT;
567     }
568 
569     if (!data.WriteUint8(isBroker)) {
570         return ERR_FLATTEN_OBJECT;
571     }
572 
573     MessageParcel reply;
574     MessageOption option;
575     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_INTERNET_PERMISSION), data, reply,
576                           option);
577 
578     return reply.ReadInt32();
579 }
580 
NetworkCreatePhysical(int32_t netId,int32_t permission)581 int32_t NetsysNativeServiceProxy::NetworkCreatePhysical(int32_t netId, int32_t permission)
582 {
583     NETNATIVE_LOGI("Begin to NetworkCreatePhysical");
584     MessageParcel data;
585     if (!WriteInterfaceToken(data)) {
586         return ERR_FLATTEN_OBJECT;
587     }
588     if (!data.WriteInt32(netId)) {
589         return ERR_FLATTEN_OBJECT;
590     }
591     if (!data.WriteInt32(permission)) {
592         return ERR_FLATTEN_OBJECT;
593     }
594 
595     MessageParcel reply;
596     MessageOption option;
597     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_PHYSICAL), data, reply,
598                           option);
599 
600     return reply.ReadInt32();
601 }
602 
NetworkCreateVirtual(int32_t netId,bool hasDns)603 int32_t NetsysNativeServiceProxy::NetworkCreateVirtual(int32_t netId, bool hasDns)
604 {
605     MessageParcel data;
606     if (!WriteInterfaceToken(data) || !data.WriteInt32(netId) || !data.WriteBool(hasDns)) {
607         return ERR_FLATTEN_OBJECT;
608     }
609 
610     MessageParcel reply;
611     MessageOption option;
612     sptr<IRemoteObject> remote = Remote();
613     if (remote == nullptr) {
614         return IPC_PROXY_NULL_INVOKER_ERR;
615     }
616     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_VIRTUAL), data,
617                                       reply, option);
618     if (ERR_NONE != ret) {
619         NETNATIVE_LOGE("NetworkCreateVirtual proxy SendRequest failed, error code: [%{public}d]", ret);
620         return IPC_INVOKER_ERR;
621     }
622 
623     int32_t result = ERR_INVALID_DATA;
624     if (!reply.ReadInt32(result)) {
625         return IPC_PROXY_TRANSACTION_ERR;
626     }
627     return result;
628 }
629 
NetworkAddUids(int32_t netId,const std::vector<UidRange> & uidRanges)630 int32_t NetsysNativeServiceProxy::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
631 {
632     MessageParcel data;
633     if (!WriteInterfaceToken(data) || !data.WriteInt32(netId)) {
634         return ERR_FLATTEN_OBJECT;
635     }
636 
637     if (!data.WriteInt32(uidRanges.size())) {
638         return IPC_PROXY_TRANSACTION_ERR;
639     }
640     for (auto iter : uidRanges) {
641         if (!iter.Marshalling(data)) {
642             return IPC_PROXY_TRANSACTION_ERR;
643         }
644     }
645 
646     MessageParcel reply;
647     MessageOption option;
648     sptr<IRemoteObject> remote = Remote();
649     if (remote == nullptr) {
650         return IPC_PROXY_NULL_INVOKER_ERR;
651     }
652     int32_t ret =
653         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_UIDS), data, reply, option);
654     if (ret != ERR_NONE) {
655         NETNATIVE_LOGE("NetworkAddUids proxy SendRequest failed, error code: [%{public}d]", ret);
656         return IPC_INVOKER_ERR;
657     }
658 
659     int32_t result = ERR_INVALID_DATA;
660     if (!reply.ReadInt32(result)) {
661         return IPC_PROXY_TRANSACTION_ERR;
662     }
663     return result;
664 }
665 
NetworkDelUids(int32_t netId,const std::vector<UidRange> & uidRanges)666 int32_t NetsysNativeServiceProxy::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
667 {
668     MessageParcel data;
669     if (!WriteInterfaceToken(data) || !data.WriteInt32(netId)) {
670         return ERR_FLATTEN_OBJECT;
671     }
672 
673     if (!data.WriteInt32(uidRanges.size())) {
674         return IPC_PROXY_TRANSACTION_ERR;
675     }
676     for (auto iter : uidRanges) {
677         if (!iter.Marshalling(data)) {
678             return IPC_PROXY_TRANSACTION_ERR;
679         }
680     }
681 
682     MessageParcel reply;
683     MessageOption option;
684     sptr<IRemoteObject> remote = Remote();
685     if (remote == nullptr) {
686         return IPC_PROXY_NULL_INVOKER_ERR;
687     }
688     int32_t ret =
689         remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DEL_UIDS), data, reply, option);
690     if (ret != ERR_NONE) {
691         NETNATIVE_LOGE("NetworkDelUids proxy SendRequest failed, error code: [%{public}d]", ret);
692         return ERR_FLATTEN_OBJECT;
693     }
694 
695     int32_t result = ERR_INVALID_DATA;
696     if (!reply.ReadInt32(result)) {
697         return IPC_PROXY_TRANSACTION_ERR;
698     }
699     return result;
700 }
701 
AddInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)702 int32_t NetsysNativeServiceProxy::AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
703                                                       int32_t prefixLength)
704 {
705     NETNATIVE_LOGI("Begin to AddInterfaceAddress");
706     MessageParcel data;
707     if (!WriteInterfaceToken(data)) {
708         return ERR_FLATTEN_OBJECT;
709     }
710     if (!data.WriteString(interfaceName)) {
711         return ERR_FLATTEN_OBJECT;
712     }
713     if (!data.WriteString(addrString)) {
714         return ERR_FLATTEN_OBJECT;
715     }
716     if (!data.WriteInt32(prefixLength)) {
717         return ERR_FLATTEN_OBJECT;
718     }
719 
720     MessageParcel reply;
721     MessageOption option;
722     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_ADD_ADDRESS), data, reply,
723                           option);
724 
725     return reply.ReadInt32();
726 }
727 
DelInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)728 int32_t NetsysNativeServiceProxy::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
729                                                       int32_t prefixLength)
730 {
731     NETNATIVE_LOGI("Begin to DelInterfaceAddress");
732     MessageParcel data;
733     if (!WriteInterfaceToken(data)) {
734         return ERR_FLATTEN_OBJECT;
735     }
736     if (!data.WriteString(interfaceName)) {
737         return ERR_FLATTEN_OBJECT;
738     }
739     if (!data.WriteString(addrString)) {
740         return ERR_FLATTEN_OBJECT;
741     }
742     if (!data.WriteInt32(prefixLength)) {
743         return ERR_FLATTEN_OBJECT;
744     }
745 
746     MessageParcel reply;
747     MessageOption option;
748     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_DEL_ADDRESS), data, reply,
749                           option);
750 
751     return reply.ReadInt32();
752 }
753 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)754 int32_t NetsysNativeServiceProxy::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
755 {
756     NETNATIVE_LOGI("Begin to InterfaceSetIpAddress");
757     MessageParcel data;
758     if (!WriteInterfaceToken(data)) {
759         return ERR_FLATTEN_OBJECT;
760     }
761     if (!data.WriteString(ifaceName) || !data.WriteString(ipAddress)) {
762         return ERR_FLATTEN_OBJECT;
763     }
764     MessageParcel reply;
765     MessageOption option;
766     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IP_ADDRESS), data, reply,
767                           option);
768 
769     return reply.ReadInt32();
770 }
771 
InterfaceSetIffUp(const std::string & ifaceName)772 int32_t NetsysNativeServiceProxy::InterfaceSetIffUp(const std::string &ifaceName)
773 {
774     NETNATIVE_LOGI("Begin to InterfaceSetIffUp");
775     MessageParcel data;
776     if (!WriteInterfaceToken(data)) {
777         return ERR_FLATTEN_OBJECT;
778     }
779     if (!data.WriteString(ifaceName)) {
780         return ERR_FLATTEN_OBJECT;
781     }
782     MessageParcel reply;
783     MessageOption option;
784     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IFF_UP), data, reply, option);
785 
786     return reply.ReadInt32();
787 }
788 
NetworkAddInterface(int32_t netId,const std::string & iface)789 int32_t NetsysNativeServiceProxy::NetworkAddInterface(int32_t netId, const std::string &iface)
790 {
791     NETNATIVE_LOGI("Begin to NetworkAddInterface");
792     MessageParcel data;
793     if (!WriteInterfaceToken(data)) {
794         return ERR_FLATTEN_OBJECT;
795     }
796     if (!data.WriteInt32(netId)) {
797         return ERR_FLATTEN_OBJECT;
798     }
799     if (!data.WriteString(iface)) {
800         return ERR_FLATTEN_OBJECT;
801     }
802 
803     MessageParcel reply;
804     MessageOption option;
805     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_INTERFACE), data, reply,
806                           option);
807 
808     return reply.ReadInt32();
809 }
810 
NetworkRemoveInterface(int32_t netId,const std::string & iface)811 int32_t NetsysNativeServiceProxy::NetworkRemoveInterface(int32_t netId, const std::string &iface)
812 {
813     NETNATIVE_LOGI("Begin to NetworkRemoveInterface");
814     MessageParcel data;
815     if (!WriteInterfaceToken(data)) {
816         return ERR_FLATTEN_OBJECT;
817     }
818     if (!data.WriteInt32(netId)) {
819         return ERR_FLATTEN_OBJECT;
820     }
821     if (!data.WriteString(iface)) {
822         return ERR_FLATTEN_OBJECT;
823     }
824 
825     MessageParcel reply;
826     MessageOption option;
827     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_INTERFACE), data, reply,
828                           option);
829 
830     return reply.ReadInt32();
831 }
832 
NetworkDestroy(int32_t netId)833 int32_t NetsysNativeServiceProxy::NetworkDestroy(int32_t netId)
834 {
835     NETNATIVE_LOGI("Begin to NetworkDestroy");
836     MessageParcel data;
837     if (!WriteInterfaceToken(data)) {
838         return ERR_FLATTEN_OBJECT;
839     }
840     if (!data.WriteInt32(netId)) {
841         return ERR_FLATTEN_OBJECT;
842     }
843 
844     MessageParcel reply;
845     MessageOption option;
846     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DESTROY), data, reply, option);
847 
848     return reply.ReadInt32();
849 }
850 
GetFwmarkForNetwork(int32_t netId,MarkMaskParcel & markMaskParcel)851 int32_t NetsysNativeServiceProxy::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
852 {
853     NETNATIVE_LOGI("Begin to GetFwmarkForNetwork");
854     MessageParcel data;
855     if (!WriteInterfaceToken(data)) {
856         return ERR_FLATTEN_OBJECT;
857     }
858     if (!data.WriteInt32(netId)) {
859         return ERR_FLATTEN_OBJECT;
860     }
861     if (!data.WriteInt32(markMaskParcel.mark)) {
862         return ERR_FLATTEN_OBJECT;
863     }
864     if (!data.WriteInt32(markMaskParcel.mask)) {
865         return ERR_FLATTEN_OBJECT;
866     }
867 
868     MessageParcel reply;
869     MessageOption option;
870     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_FWMARK_FOR_NETWORK), data, reply,
871                           option);
872 
873     return reply.ReadInt32();
874 }
875 
SetInterfaceConfig(const InterfaceConfigurationParcel & cfg)876 int32_t NetsysNativeServiceProxy::SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)
877 {
878     NETNATIVE_LOGI("Begin to SetInterfaceConfig");
879     MessageParcel data;
880     if (!WriteInterfaceToken(data)) {
881         return ERR_FLATTEN_OBJECT;
882     }
883     if (!data.WriteString(cfg.ifName)) {
884         return ERR_FLATTEN_OBJECT;
885     }
886     if (!data.WriteString(cfg.hwAddr)) {
887         return ERR_FLATTEN_OBJECT;
888     }
889     if (!data.WriteString(cfg.ipv4Addr)) {
890         return ERR_FLATTEN_OBJECT;
891     }
892     if (!data.WriteInt32(cfg.prefixLength)) {
893         return ERR_FLATTEN_OBJECT;
894     }
895     int32_t vsize = static_cast<int32_t>(cfg.flags.size());
896     if (!data.WriteInt32(vsize)) {
897         return ERR_FLATTEN_OBJECT;
898     }
899     std::vector<std::string> vCflags;
900     vCflags.assign(cfg.flags.begin(), cfg.flags.end());
901     NETNATIVE_LOGI("PROXY: SetInterfaceConfig Write flags String_SIZE: %{public}d",
902                    static_cast<int32_t>(vCflags.size()));
903     for (std::vector<std::string>::iterator it = vCflags.begin(); it != vCflags.end(); ++it) {
904         data.WriteString(*it);
905     }
906     MessageParcel reply;
907     MessageOption option;
908     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_CONFIG), data, reply, option);
909 
910     return reply.ReadInt32();
911 }
912 
GetInterfaceConfig(InterfaceConfigurationParcel & cfg)913 int32_t NetsysNativeServiceProxy::GetInterfaceConfig(InterfaceConfigurationParcel &cfg)
914 {
915     NETNATIVE_LOGI("Begin to GetInterfaceConfig");
916     MessageParcel data;
917     int32_t ret;
918     int32_t vSize;
919     if (!WriteInterfaceToken(data)) {
920         return ERR_FLATTEN_OBJECT;
921     }
922     if (!data.WriteString(cfg.ifName)) {
923         return ERR_FLATTEN_OBJECT;
924     }
925 
926     MessageParcel reply;
927     MessageOption option;
928     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_CONFIG), data, reply, option);
929     ret = reply.ReadInt32();
930     if (ret != ERR_NONE) {
931         NETNATIVE_LOGE("Fail to GetInterfaceConfig ret= %{public}d", ret);
932         return ret;
933     }
934     reply.ReadString(cfg.ifName);
935     reply.ReadString(cfg.hwAddr);
936     reply.ReadString(cfg.ipv4Addr);
937     reply.ReadInt32(cfg.prefixLength);
938     vSize = reply.ReadInt32();
939     vSize = vSize > MAX_INTERFACE_CONFIG_SIZE ? MAX_INTERFACE_CONFIG_SIZE : vSize;
940     std::vector<std::string> vecString;
941     for (int i = 0; i < vSize; i++) {
942         vecString.push_back(reply.ReadString());
943     }
944     if (vSize > 0) {
945         cfg.flags.assign(vecString.begin(), vecString.end());
946     }
947     NETNATIVE_LOGI("End to GetInterfaceConfig, ret =%{public}d", ret);
948     return ret;
949 }
950 
InterfaceGetList(std::vector<std::string> & ifaces)951 int32_t NetsysNativeServiceProxy::InterfaceGetList(std::vector<std::string> &ifaces)
952 {
953     NETNATIVE_LOGI("NetsysNativeServiceProxy Begin to InterfaceGetList");
954     MessageParcel data;
955     int32_t ret;
956     int32_t vSize;
957     if (!WriteInterfaceToken(data)) {
958         return ERR_FLATTEN_OBJECT;
959     }
960     MessageParcel reply;
961     MessageOption option;
962     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_LIST), data, reply, option);
963     ret = reply.ReadInt32();
964     if (ret != ERR_NONE) {
965         NETNATIVE_LOGE("Fail to InterfaceGetList ret= %{public}d", ret);
966         return ret;
967     }
968     vSize = reply.ReadInt32();
969     std::vector<std::string> vecString;
970     for (int i = 0; i < vSize; i++) {
971         vecString.push_back(reply.ReadString());
972     }
973     if (vSize > 0) {
974         ifaces.assign(vecString.begin(), vecString.end());
975     }
976     NETNATIVE_LOGI("NetsysNativeServiceProxy End to InterfaceGetList, ret =%{public}d", ret);
977     return ret;
978 }
979 
StartDhcpClient(const std::string & iface,bool bIpv6)980 int32_t NetsysNativeServiceProxy::StartDhcpClient(const std::string &iface, bool bIpv6)
981 {
982     NETNATIVE_LOGI("Begin to StartDhcpClient");
983     MessageParcel data;
984     int32_t ret;
985     if (!WriteInterfaceToken(data)) {
986         return ERR_FLATTEN_OBJECT;
987     }
988     if (!data.WriteString(iface)) {
989         return ERR_FLATTEN_OBJECT;
990     }
991     if (!data.WriteBool(bIpv6)) {
992         return ERR_FLATTEN_OBJECT;
993     }
994 
995     MessageParcel reply;
996     MessageOption option;
997     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_CLIENT), data, reply, option);
998 
999     ret = reply.ReadInt32();
1000     NETNATIVE_LOGI("End to StartDhcpClient, ret =%{public}d", ret);
1001     return ret;
1002 }
1003 
StopDhcpClient(const std::string & iface,bool bIpv6)1004 int32_t NetsysNativeServiceProxy::StopDhcpClient(const std::string &iface, bool bIpv6)
1005 {
1006     NETNATIVE_LOGI("Begin to StopDhcpClient");
1007     MessageParcel data;
1008     int32_t ret;
1009     if (!WriteInterfaceToken(data)) {
1010         return ERR_FLATTEN_OBJECT;
1011     }
1012     if (!data.WriteString(iface)) {
1013         return ERR_FLATTEN_OBJECT;
1014     }
1015     if (!data.WriteBool(bIpv6)) {
1016         return ERR_FLATTEN_OBJECT;
1017     }
1018 
1019     MessageParcel reply;
1020     MessageOption option;
1021     ret =
1022         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_CLIENT), data, reply, option);
1023     NETNATIVE_LOGI("SendRequest, ret =%{public}d", ret);
1024     ret = reply.ReadInt32();
1025     NETNATIVE_LOGI("End to StopDhcpClient, ret =%{public}d", ret);
1026     return ret;
1027 }
1028 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)1029 int32_t NetsysNativeServiceProxy::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
1030 {
1031     NETNATIVE_LOGI("Begin to StartDhcpService");
1032     MessageParcel data;
1033 
1034     if (!WriteInterfaceToken(data)) {
1035         return ERR_FLATTEN_OBJECT;
1036     }
1037     if (!data.WriteString(iface)) {
1038         return ERR_FLATTEN_OBJECT;
1039     }
1040     if (!data.WriteString(ipv4addr)) {
1041         return ERR_FLATTEN_OBJECT;
1042     }
1043 
1044     MessageParcel reply;
1045     MessageOption option;
1046     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_SERVICE), data, reply, option);
1047     int32_t ret = reply.ReadInt32();
1048     NETNATIVE_LOGI("End to StartDhcpService, ret =%{public}d", ret);
1049     return ret;
1050 }
1051 
StopDhcpService(const std::string & iface)1052 int32_t NetsysNativeServiceProxy::StopDhcpService(const std::string &iface)
1053 {
1054     NETNATIVE_LOGI("Begin to StopDhcpService");
1055     MessageParcel data;
1056     if (!WriteInterfaceToken(data)) {
1057         return ERR_FLATTEN_OBJECT;
1058     }
1059     if (!data.WriteString(iface)) {
1060         return ERR_FLATTEN_OBJECT;
1061     }
1062 
1063     MessageParcel reply;
1064     MessageOption option;
1065     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_SERVICE), data, reply, option);
1066     int32_t ret = reply.ReadInt32();
1067     NETNATIVE_LOGI("End to StopDhcpService, ret =%{public}d", ret);
1068     return ret;
1069 }
1070 
IpEnableForwarding(const std::string & requestor)1071 int32_t NetsysNativeServiceProxy::IpEnableForwarding(const std::string &requestor)
1072 {
1073     MessageParcel data;
1074     if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1075         return ERR_FLATTEN_OBJECT;
1076     }
1077 
1078     MessageParcel reply;
1079     MessageOption option;
1080     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPENABLE_FORWARDING), data, reply, option);
1081 
1082     int32_t ret = reply.ReadInt32();
1083     NETNATIVE_LOGI("End to IpEnableForwarding, ret =%{public}d", ret);
1084     return ret;
1085 }
1086 
IpDisableForwarding(const std::string & requestor)1087 int32_t NetsysNativeServiceProxy::IpDisableForwarding(const std::string &requestor)
1088 {
1089     MessageParcel data;
1090     if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1091         return ERR_FLATTEN_OBJECT;
1092     }
1093 
1094     MessageParcel reply;
1095     MessageOption option;
1096     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPDISABLE_FORWARDING), data, reply, option);
1097 
1098     int32_t ret = reply.ReadInt32();
1099     NETNATIVE_LOGI("End to IpDisableForwarding, ret =%{public}d", ret);
1100     return ret;
1101 }
1102 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)1103 int32_t NetsysNativeServiceProxy::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1104 {
1105     MessageParcel data;
1106     if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1107         return ERR_FLATTEN_OBJECT;
1108     }
1109 
1110     MessageParcel reply;
1111     MessageOption option;
1112     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_NAT), data, reply, option);
1113 
1114     int32_t ret = reply.ReadInt32();
1115     NETNATIVE_LOGI("End to EnableNat, ret =%{public}d", ret);
1116     return ret;
1117 }
1118 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)1119 int32_t NetsysNativeServiceProxy::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1120 {
1121     MessageParcel data;
1122     if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1123         return ERR_FLATTEN_OBJECT;
1124     }
1125 
1126     MessageParcel reply;
1127     MessageOption option;
1128     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_NAT), data, reply, option);
1129 
1130     int32_t ret = reply.ReadInt32();
1131     NETNATIVE_LOGI("End to DisableNat, ret =%{public}d", ret);
1132     return ret;
1133 }
1134 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)1135 int32_t NetsysNativeServiceProxy::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
1136 {
1137     MessageParcel data;
1138     if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1139         return ERR_FLATTEN_OBJECT;
1140     }
1141 
1142     MessageParcel reply;
1143     MessageOption option;
1144     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_ADD_INTERFACE_FORWARD), data, reply,
1145                           option);
1146 
1147     int32_t ret = reply.ReadInt32();
1148     NETNATIVE_LOGI("End to IpfwdAddInterfaceForward, ret =%{public}d", ret);
1149     return ret;
1150 }
1151 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)1152 int32_t NetsysNativeServiceProxy::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
1153 {
1154     MessageParcel data;
1155     if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1156         return ERR_FLATTEN_OBJECT;
1157     }
1158 
1159     MessageParcel reply;
1160     MessageOption option;
1161     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD), data,
1162                           reply, option);
1163 
1164     int32_t ret = reply.ReadInt32();
1165     NETNATIVE_LOGI("End to IpfwdRemoveInterfaceForward, ret =%{public}d", ret);
1166     return ret;
1167 }
1168 
BandwidthEnableDataSaver(bool enable)1169 int32_t NetsysNativeServiceProxy::BandwidthEnableDataSaver(bool enable)
1170 {
1171     MessageParcel data;
1172     if (!WriteInterfaceToken(data)) {
1173         NETNATIVE_LOGE("WriteInterfaceToken failed");
1174         return ERR_FLATTEN_OBJECT;
1175     }
1176     if (!data.WriteBool(enable)) {
1177         NETNATIVE_LOGE("WriteBool failed");
1178         return ERR_FLATTEN_OBJECT;
1179     }
1180 
1181     MessageParcel reply;
1182     MessageOption option;
1183     auto remote = Remote();
1184     if (remote == nullptr) {
1185         return IPC_PROXY_NULL_INVOKER_ERR;
1186     }
1187     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER),
1188                                         data, reply, option);
1189     if (error != ERR_NONE) {
1190         NETNATIVE_LOGE("proxy SendRequest failed");
1191         return ERR_FLATTEN_OBJECT;
1192     }
1193     int32_t ret = reply.ReadInt32();
1194     return ret;
1195 }
1196 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)1197 int32_t NetsysNativeServiceProxy::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
1198 {
1199     MessageParcel data;
1200     if (!WriteInterfaceToken(data)) {
1201         NETNATIVE_LOGE("WriteInterfaceToken failed");
1202         return ERR_FLATTEN_OBJECT;
1203     }
1204     if (!data.WriteString(ifName)) {
1205         NETNATIVE_LOGE("WriteString failed");
1206         return ERR_FLATTEN_OBJECT;
1207     }
1208     if (!data.WriteInt64(bytes)) {
1209         NETNATIVE_LOGE("WriteInt64 failed");
1210         return ERR_FLATTEN_OBJECT;
1211     }
1212 
1213     MessageParcel reply;
1214     MessageOption option;
1215     auto remote = Remote();
1216     if (remote == nullptr) {
1217         return IPC_PROXY_NULL_INVOKER_ERR;
1218     }
1219     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_SET_IFACE_QUOTA),
1220                                         data, reply, option);
1221     if (error != ERR_NONE) {
1222         NETNATIVE_LOGE("proxy SendRequest failed");
1223         return ERR_FLATTEN_OBJECT;
1224     }
1225     int32_t ret = reply.ReadInt32();
1226     return ret;
1227 }
1228 
BandwidthRemoveIfaceQuota(const std::string & ifName)1229 int32_t NetsysNativeServiceProxy::BandwidthRemoveIfaceQuota(const std::string &ifName)
1230 {
1231     MessageParcel data;
1232     if (!WriteInterfaceToken(data)) {
1233         NETNATIVE_LOGE("WriteInterfaceToken failed");
1234         return ERR_FLATTEN_OBJECT;
1235     }
1236     if (!data.WriteString(ifName)) {
1237         NETNATIVE_LOGE("WriteString failed");
1238         return ERR_FLATTEN_OBJECT;
1239     }
1240 
1241     MessageParcel reply;
1242     MessageOption option;
1243     auto remote = Remote();
1244     if (remote == nullptr) {
1245         return IPC_PROXY_NULL_INVOKER_ERR;
1246     }
1247     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA),
1248                                         data, reply, option);
1249     if (error != ERR_NONE) {
1250         NETNATIVE_LOGE("proxy SendRequest failed");
1251         return ERR_FLATTEN_OBJECT;
1252     }
1253     int32_t ret = reply.ReadInt32();
1254     return ret;
1255 }
1256 
BandwidthAddDeniedList(uint32_t uid)1257 int32_t NetsysNativeServiceProxy::BandwidthAddDeniedList(uint32_t uid)
1258 {
1259     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_DENIED_LIST));
1260     return ret;
1261 }
1262 
BandwidthRemoveDeniedList(uint32_t uid)1263 int32_t NetsysNativeServiceProxy::BandwidthRemoveDeniedList(uint32_t uid)
1264 {
1265     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST));
1266     return ret;
1267 }
1268 
BandwidthAddAllowedList(uint32_t uid)1269 int32_t NetsysNativeServiceProxy::BandwidthAddAllowedList(uint32_t uid)
1270 {
1271     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST));
1272     return ret;
1273 }
1274 
BandwidthRemoveAllowedList(uint32_t uid)1275 int32_t NetsysNativeServiceProxy::BandwidthRemoveAllowedList(uint32_t uid)
1276 {
1277     int32_t ret = DealBandwidth(uid, static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST));
1278     return ret;
1279 }
1280 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1281 int32_t NetsysNativeServiceProxy::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1282 {
1283     MessageParcel data;
1284     uint32_t uidSize = uids.size();
1285     if (uidSize > UIDS_LIST_MAX_SIZE) {
1286         NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1287         return ERR_INVALID_DATA;
1288     }
1289     if (!WriteInterfaceToken(data)) {
1290         NETNATIVE_LOGE("WriteInterfaceToken failed");
1291         return ERR_FLATTEN_OBJECT;
1292     }
1293     if (!data.WriteUint32(chain)) {
1294         NETNATIVE_LOGE("WriteUint32 failed");
1295         return ERR_FLATTEN_OBJECT;
1296     }
1297     if (!data.WriteUint32(uidSize)) {
1298         NETNATIVE_LOGE("WriteUint32 failed");
1299         return ERR_FLATTEN_OBJECT;
1300     }
1301     std::vector<uint32_t> vUids;
1302     vUids.assign(uids.begin(), uids.end());
1303     std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1304 
1305     MessageParcel reply;
1306     MessageOption option;
1307     auto remote = Remote();
1308     if (remote == nullptr) {
1309         return IPC_PROXY_NULL_INVOKER_ERR;
1310     }
1311     int32_t error = remote->SendRequest(
1312         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN), data, reply, option);
1313     if (error != ERR_NONE) {
1314         NETNATIVE_LOGE("proxy SendRequest failed");
1315         return ERR_FLATTEN_OBJECT;
1316     }
1317     int32_t ret = reply.ReadInt32();
1318     return ret;
1319 }
1320 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1321 int32_t NetsysNativeServiceProxy::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1322 {
1323     MessageParcel data;
1324     uint32_t uidSize = uids.size();
1325     if (uidSize > UIDS_LIST_MAX_SIZE) {
1326         NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1327         return ERR_INVALID_DATA;
1328     }
1329     if (!WriteInterfaceToken(data)) {
1330         NETNATIVE_LOGE("WriteInterfaceToken failed");
1331         return ERR_FLATTEN_OBJECT;
1332     }
1333     if (!data.WriteUint32(chain)) {
1334         NETNATIVE_LOGE("WriteUint32 Error");
1335         return ERR_FLATTEN_OBJECT;
1336     }
1337     if (!data.WriteUint32(uidSize)) {
1338         NETNATIVE_LOGE("WriteUint32 Error");
1339         return ERR_FLATTEN_OBJECT;
1340     }
1341     std::vector<uint32_t> vUids;
1342     vUids.assign(uids.begin(), uids.end());
1343     std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1344 
1345     MessageParcel reply;
1346     MessageOption option;
1347     auto remote = Remote();
1348     if (remote == nullptr) {
1349         return IPC_PROXY_NULL_INVOKER_ERR;
1350     }
1351     int32_t error = remote->SendRequest(
1352         static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN), data, reply, option);
1353     if (error != ERR_NONE) {
1354         NETNATIVE_LOGE("proxy SendRequest failed");
1355         return ERR_FLATTEN_OBJECT;
1356     }
1357     int32_t ret = reply.ReadInt32();
1358     return ret;
1359 }
1360 
FirewallEnableChain(uint32_t chain,bool enable)1361 int32_t NetsysNativeServiceProxy::FirewallEnableChain(uint32_t chain, bool enable)
1362 {
1363     MessageParcel data;
1364     if (!WriteInterfaceToken(data)) {
1365         NETNATIVE_LOGE("WriteInterfaceToken failed");
1366         return ERR_FLATTEN_OBJECT;
1367     }
1368     if (!data.WriteUint32(chain)) {
1369         NETNATIVE_LOGE("WriteUint32 Error");
1370         return ERR_FLATTEN_OBJECT;
1371     }
1372     if (!data.WriteBool(enable)) {
1373         NETNATIVE_LOGE("WriteBool Error");
1374         return ERR_FLATTEN_OBJECT;
1375     }
1376 
1377     MessageParcel reply;
1378     MessageOption option;
1379     auto remote = Remote();
1380     if (remote == nullptr) {
1381         return IPC_PROXY_NULL_INVOKER_ERR;
1382     }
1383     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_ENABLE_CHAIN), data,
1384                                         reply, option);
1385     if (error != ERR_NONE) {
1386         NETNATIVE_LOGE("proxy SendRequest failed");
1387         return ERR_FLATTEN_OBJECT;
1388     }
1389     int32_t ret = reply.ReadInt32();
1390     return ret;
1391 }
1392 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)1393 int32_t NetsysNativeServiceProxy::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
1394                                                      uint32_t firewallRule)
1395 {
1396     MessageParcel data;
1397     if (!WriteInterfaceToken(data)) {
1398         NETNATIVE_LOGE("WriteInterfaceToken failed");
1399         return ERR_FLATTEN_OBJECT;
1400     }
1401     if (!data.WriteUint32(chain)) {
1402         NETNATIVE_LOGE("WriteUint32 failed");
1403         return ERR_FLATTEN_OBJECT;
1404     }
1405     if (!data.WriteUInt32Vector(uids)) {
1406         NETNATIVE_LOGE("WriteUint32 failed");
1407         return ERR_FLATTEN_OBJECT;
1408     }
1409     if (!data.WriteUint32(firewallRule)) {
1410         NETNATIVE_LOGE("WriteUint32 failed");
1411         return ERR_FLATTEN_OBJECT;
1412     }
1413 
1414     MessageParcel reply;
1415     MessageOption option;
1416     auto remote = Remote();
1417     if (remote == nullptr) {
1418         return IPC_PROXY_NULL_INVOKER_ERR;
1419     }
1420     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_RULE), data,
1421                                         reply, option);
1422     if (error != ERR_NONE) {
1423         NETNATIVE_LOGE("proxy SendRequest failed");
1424         return ERR_FLATTEN_OBJECT;
1425     }
1426     int32_t ret = reply.ReadInt32();
1427     return ret;
1428 }
1429 
ShareDnsSet(uint16_t netId)1430 int32_t NetsysNativeServiceProxy::ShareDnsSet(uint16_t netId)
1431 {
1432     MessageParcel data;
1433     if (!WriteInterfaceToken(data)) {
1434         return ERR_FLATTEN_OBJECT;
1435     }
1436     if (!data.WriteUint16(netId)) {
1437         return ERR_FLATTEN_OBJECT;
1438     }
1439 
1440     MessageParcel reply;
1441     MessageOption option;
1442     int32_t error =
1443         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_TETHER_DNS_SET), data, reply, option);
1444     if (error != ERR_NONE) {
1445         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1446         return error;
1447     }
1448     return reply.ReadInt32();
1449 }
1450 
StartDnsProxyListen()1451 int32_t NetsysNativeServiceProxy::StartDnsProxyListen()
1452 {
1453     MessageParcel data;
1454     if (!WriteInterfaceToken(data)) {
1455         return ERR_FLATTEN_OBJECT;
1456     }
1457 
1458     MessageParcel reply;
1459     MessageOption option;
1460     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DNS_PROXY_LISTEN),
1461                                           data, reply, option);
1462     if (error != ERR_NONE) {
1463         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1464         return error;
1465     }
1466     return reply.ReadInt32();
1467 }
1468 
StopDnsProxyListen()1469 int32_t NetsysNativeServiceProxy::StopDnsProxyListen()
1470 {
1471     MessageParcel data;
1472     if (!WriteInterfaceToken(data)) {
1473         return ERR_FLATTEN_OBJECT;
1474     }
1475 
1476     MessageParcel reply;
1477     MessageOption option;
1478     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DNS_PROXY_LISTEN),
1479                                           data, reply, option);
1480     if (error != ERR_NONE) {
1481         NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1482         return error;
1483     }
1484     return reply.ReadInt32();
1485 }
1486 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,NetworkSharingTraffic & traffic)1487 int32_t NetsysNativeServiceProxy::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
1488                                                            NetworkSharingTraffic &traffic)
1489 {
1490     MessageParcel data;
1491     if (!WriteInterfaceToken(data)) {
1492         return ERR_FLATTEN_OBJECT;
1493     }
1494     if (!data.WriteString(downIface)) {
1495         return ERR_FLATTEN_OBJECT;
1496     }
1497     if (!data.WriteString(upIface)) {
1498         return ERR_FLATTEN_OBJECT;
1499     }
1500 
1501     MessageParcel reply;
1502     MessageOption option;
1503     Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_SHARING_NETWORK_TRAFFIC), data, reply,
1504                           option);
1505 
1506     int32_t ret = reply.ReadInt32();
1507     if (ret != ERR_NONE) {
1508         NETNATIVE_LOGE("Fail to GetNetworkSharingTraffic ret= %{public}d", ret);
1509         return ret;
1510     }
1511 
1512     traffic.receive = reply.ReadInt64();
1513     traffic.send = reply.ReadInt64();
1514     traffic.all = reply.ReadInt64();
1515     NETNATIVE_LOGI("NetsysNativeServiceProxy GetNetworkSharingTraffic ret=%{public}d", ret);
1516     return ret;
1517 }
1518 
GetTotalStats(uint64_t & stats,uint32_t type)1519 int32_t NetsysNativeServiceProxy::GetTotalStats(uint64_t &stats, uint32_t type)
1520 {
1521     MessageParcel data;
1522     if (!WriteInterfaceToken(data)) {
1523         return ERR_FLATTEN_OBJECT;
1524     }
1525     if (!data.WriteUint8(type)) {
1526         return ERR_FLATTEN_OBJECT;
1527     }
1528     MessageParcel reply;
1529     MessageOption option;
1530     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_TOTAL_STATS), data,
1531                                           reply, option)) {
1532         NETNATIVE_LOGE("proxy SendRequest failed");
1533         return ERR_FLATTEN_OBJECT;
1534     }
1535 
1536     int32_t ret;
1537     if (!reply.ReadInt32(ret)) {
1538         NETNATIVE_LOGE("get ret falil");
1539         return ERR_FLATTEN_OBJECT;
1540     }
1541     if (ret != ERR_NONE) {
1542         NETNATIVE_LOGE("fail to GetTotalStats ret= %{public}d", ret);
1543         return ret;
1544     }
1545     if (!reply.ReadUint64(stats)) {
1546         NETNATIVE_LOGE("get stats falil");
1547         return ERR_FLATTEN_OBJECT;
1548     }
1549 
1550     return ERR_NONE;
1551 }
1552 
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)1553 int32_t NetsysNativeServiceProxy::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
1554 {
1555     MessageParcel data;
1556     if (!WriteInterfaceToken(data)) {
1557         return ERR_FLATTEN_OBJECT;
1558     }
1559     if (!data.WriteUint32(type)) {
1560         return ERR_FLATTEN_OBJECT;
1561     }
1562     if (!data.WriteUint32(uid)) {
1563         return ERR_FLATTEN_OBJECT;
1564     }
1565     MessageParcel reply;
1566     MessageOption option;
1567     if (ERR_NONE !=
1568         Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_UID_STATS), data, reply, option)) {
1569         NETNATIVE_LOGE("proxy SendRequest failed");
1570         return ERR_FLATTEN_OBJECT;
1571     }
1572 
1573     int32_t ret;
1574     if (!reply.ReadInt32(ret)) {
1575         NETNATIVE_LOGE("get ret falil");
1576         return ERR_FLATTEN_OBJECT;
1577     }
1578     if (ret != ERR_NONE) {
1579         NETNATIVE_LOGE("fail to GetUidStats ret= %{public}d", ret);
1580         return ret;
1581     }
1582     if (!reply.ReadUint64(stats)) {
1583         NETNATIVE_LOGE("get stats falil");
1584         return ERR_FLATTEN_OBJECT;
1585     }
1586     return ERR_NONE;
1587 }
1588 
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)1589 int32_t NetsysNativeServiceProxy::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
1590 {
1591     MessageParcel data;
1592     if (!WriteInterfaceToken(data)) {
1593         return ERR_FLATTEN_OBJECT;
1594     }
1595     if (!data.WriteUint32(type)) {
1596         return ERR_FLATTEN_OBJECT;
1597     }
1598     if (!data.WriteString(interfaceName)) {
1599         return ERR_FLATTEN_OBJECT;
1600     }
1601     MessageParcel reply;
1602     MessageOption option;
1603     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_IFACE_STATS), data,
1604                                           reply, option)) {
1605         NETNATIVE_LOGE("proxy SendRequest failed");
1606         return ERR_FLATTEN_OBJECT;
1607     }
1608 
1609     int32_t ret;
1610     if (!reply.ReadInt32(ret)) {
1611         NETNATIVE_LOGE("get ret falil");
1612         return ERR_FLATTEN_OBJECT;
1613     }
1614     if (ret != ERR_NONE) {
1615         NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1616         return ret;
1617     }
1618     if (!reply.ReadUint64(stats)) {
1619         NETNATIVE_LOGE("get stats falil");
1620         return ERR_FLATTEN_OBJECT;
1621     }
1622     return ERR_NONE;
1623 }
1624 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)1625 int32_t NetsysNativeServiceProxy::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1626 {
1627     MessageParcel data;
1628     if (!WriteInterfaceToken(data)) {
1629         return ERR_FLATTEN_OBJECT;
1630     }
1631     MessageParcel reply;
1632     MessageOption option;
1633     if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_STATS_INFO), data,
1634                                           reply, option)) {
1635         NETNATIVE_LOGE("proxy SendRequest failed");
1636         return ERR_FLATTEN_OBJECT;
1637     }
1638 
1639     int32_t ret;
1640     if (!reply.ReadInt32(ret)) {
1641         NETNATIVE_LOGE("get ret falil");
1642         return ERR_FLATTEN_OBJECT;
1643     }
1644     if (ret != ERR_NONE) {
1645         NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1646         return ret;
1647     }
1648     if (!OHOS::NetManagerStandard::NetStatsInfo::Unmarshalling(reply, stats)) {
1649         NETNATIVE_LOGE("Read stats info failed");
1650         return ERR_FLATTEN_OBJECT;
1651     }
1652 
1653     return ERR_NONE;
1654 }
1655 
SetIptablesCommandForRes(const std::string & cmd,std::string & respond)1656 int32_t NetsysNativeServiceProxy::SetIptablesCommandForRes(const std::string &cmd, std::string &respond)
1657 {
1658     MessageParcel data;
1659     if (!WriteInterfaceToken(data)) {
1660         return ERR_FLATTEN_OBJECT;
1661     }
1662     if (!data.WriteString(cmd)) {
1663         return ERR_FLATTEN_OBJECT;
1664     }
1665     MessageParcel reply;
1666     MessageOption option;
1667     if (Remote() == nullptr) {
1668         NETNATIVE_LOGE("SetIptablesCommandForRes Remote pointer is null");
1669         return ERR_FLATTEN_OBJECT;
1670     }
1671     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES),
1672                                           data, reply, option);
1673     if (error != ERR_NONE) {
1674         NETNATIVE_LOGE("SetIptablesCommandForRes proxy SendRequest failed");
1675         return ERR_FLATTEN_OBJECT;
1676     }
1677     int32_t ret;
1678     if (!reply.ReadInt32(ret)) {
1679         NETNATIVE_LOGE("SetIptablesCommandForRes proxy read ret failed");
1680         return ERR_FLATTEN_OBJECT;
1681     }
1682     if (ret == ERR_NONE) {
1683         if (!reply.ReadString(respond)) {
1684             NETNATIVE_LOGE("SetIptablesCommandForRes proxy read respond failed");
1685             return ERR_FLATTEN_OBJECT;
1686         }
1687     }
1688     return ret;
1689 }
1690 
DealBandwidth(uint32_t uid,uint32_t code)1691 int32_t NetsysNativeServiceProxy::DealBandwidth(uint32_t uid, uint32_t code)
1692 {
1693     MessageParcel data;
1694     if (!WriteInterfaceToken(data)) {
1695         NETNATIVE_LOGE("WriteInterfaceToken failed");
1696         return ERR_FLATTEN_OBJECT;
1697     }
1698     if (!data.WriteUint32(uid)) {
1699         NETNATIVE_LOGE("WriteUint32 failed");
1700         return ERR_FLATTEN_OBJECT;
1701     }
1702 
1703     MessageParcel reply;
1704     MessageOption option;
1705     auto remote = Remote();
1706     if (remote == nullptr) {
1707         return IPC_PROXY_NULL_INVOKER_ERR;
1708     }
1709     int32_t error = remote->SendRequest(code, data, reply, option);
1710     if (error != ERR_NONE) {
1711         NETNATIVE_LOGE("proxy SendRequest failed");
1712         return ERR_FLATTEN_OBJECT;
1713     }
1714 
1715     return reply.ReadInt32();
1716 }
1717 
NetDiagPingHost(const NetDiagPingOption & pingOption,const sptr<INetDiagCallback> & callback)1718 int32_t NetsysNativeServiceProxy::NetDiagPingHost(const NetDiagPingOption &pingOption,
1719                                                   const sptr<INetDiagCallback> &callback)
1720 {
1721     NETNATIVE_LOGI("Begin to NetDiagPingHost");
1722     if (callback == nullptr) {
1723         NETNATIVE_LOGE("The parameter of callback is nullptr");
1724         return ERR_NULL_OBJECT;
1725     }
1726 
1727     MessageParcel data;
1728     if (!WriteInterfaceToken(data)) {
1729         return ERR_FLATTEN_OBJECT;
1730     }
1731     if (!pingOption.Marshalling(data)) {
1732         return ERR_FLATTEN_OBJECT;
1733     }
1734     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
1735         return ERR_FLATTEN_OBJECT;
1736     }
1737 
1738     sptr<IRemoteObject> remote = Remote();
1739     if (remote == nullptr) {
1740         NETNATIVE_LOGE("Remote is null");
1741         return ERR_FLATTEN_OBJECT;
1742     }
1743     MessageParcel reply;
1744     MessageOption option;
1745     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_PING_HOST), data, reply, option);
1746     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1747     if (!reply.ReadInt32(ret)) {
1748         NETNATIVE_LOGE("Read result failed");
1749         return ERR_FLATTEN_OBJECT;
1750     }
1751     return ret;
1752 }
1753 
NetDiagGetRouteTable(std::list<NetDiagRouteTable> & routeTables)1754 int32_t NetsysNativeServiceProxy::NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables)
1755 {
1756     NETNATIVE_LOGI("Begin to NetDiagGetRouteTable");
1757     MessageParcel data;
1758     if (!WriteInterfaceToken(data)) {
1759         return ERR_FLATTEN_OBJECT;
1760     }
1761     sptr<IRemoteObject> remote = Remote();
1762     if (remote == nullptr) {
1763         NETNATIVE_LOGE("Remote is null");
1764         return ERR_FLATTEN_OBJECT;
1765     }
1766 
1767     MessageParcel reply;
1768     MessageOption option;
1769     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_ROUTE_TABLE), data, reply,
1770                         option);
1771     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1772     if (!reply.ReadInt32(ret)) {
1773         NETNATIVE_LOGE("Read result failed");
1774         return ERR_FLATTEN_OBJECT;
1775     }
1776 
1777     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
1778         uint32_t size = 0;
1779         if (!reply.ReadUint32(size)) {
1780             NETNATIVE_LOGE("Read uint32 failed");
1781             return ERR_FLATTEN_OBJECT;
1782         }
1783 
1784         for (uint32_t i = 0; i < size; ++i) {
1785             NetDiagRouteTable routeTable;
1786             if (!NetDiagRouteTable::Unmarshalling(reply, routeTable)) {
1787                 NETNATIVE_LOGE("NetDiagRouteTable unmarshalling failed.");
1788                 return ERR_FLATTEN_OBJECT;
1789             }
1790             routeTables.push_back(routeTable);
1791         }
1792     }
1793     return ret;
1794 }
1795 
NetDiagGetSocketsInfo(NetDiagProtocolType socketType,NetDiagSocketsInfo & socketsInfo)1796 int32_t NetsysNativeServiceProxy::NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo)
1797 {
1798     NETNATIVE_LOGI("Begin to NetDiagGetSocketsInfo");
1799     MessageParcel data;
1800     if (!WriteInterfaceToken(data)) {
1801         return ERR_FLATTEN_OBJECT;
1802     }
1803     if (!data.WriteUint8(static_cast<uint8_t>(socketType))) {
1804         return ERR_FLATTEN_OBJECT;
1805     }
1806 
1807     sptr<IRemoteObject> remote = Remote();
1808     if (remote == nullptr) {
1809         NETNATIVE_LOGE("Remote is null");
1810         return ERR_FLATTEN_OBJECT;
1811     }
1812     MessageParcel reply;
1813     MessageOption option;
1814     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_SOCKETS_INFO), data, reply,
1815                         option);
1816     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1817     if (!reply.ReadInt32(ret)) {
1818         NETNATIVE_LOGE("Read result failed");
1819         return ERR_FLATTEN_OBJECT;
1820     }
1821 
1822     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
1823         if (!NetDiagSocketsInfo::Unmarshalling(reply, socketsInfo)) {
1824             NETNATIVE_LOGE("NetDiagSocketsInfo Unmarshalling failed.");
1825             return ERR_FLATTEN_OBJECT;
1826         }
1827     }
1828     return ret;
1829 }
1830 
NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> & configs,const std::string & ifaceName)1831 int32_t NetsysNativeServiceProxy::NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs,
1832                                                             const std::string &ifaceName)
1833 {
1834     NETNATIVE_LOGI("Begin to NetDiagGetInterfaceConfig");
1835     MessageParcel data;
1836     if (!WriteInterfaceToken(data)) {
1837         return ERR_FLATTEN_OBJECT;
1838     }
1839     if (!data.WriteString(ifaceName)) {
1840         return ERR_FLATTEN_OBJECT;
1841     }
1842 
1843     sptr<IRemoteObject> remote = Remote();
1844     if (remote == nullptr) {
1845         NETNATIVE_LOGE("Remote is null");
1846         return ERR_FLATTEN_OBJECT;
1847     }
1848     MessageParcel reply;
1849     MessageOption option;
1850     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_IFACE_CONFIG), data, reply,
1851                         option);
1852     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1853     if (!reply.ReadInt32(ret)) {
1854         NETNATIVE_LOGE("Read result failed");
1855         return ERR_FLATTEN_OBJECT;
1856     }
1857 
1858     if (ret == NetManagerStandard::NETMANAGER_SUCCESS) {
1859         uint32_t size = 0;
1860         if (!reply.ReadUint32(size)) {
1861             NETNATIVE_LOGE("Read uint32 failed");
1862             return ERR_FLATTEN_OBJECT;
1863         }
1864 
1865         for (uint32_t i = 0; i < size; ++i) {
1866             NetDiagIfaceConfig ifaceConfig;
1867             if (!NetDiagIfaceConfig::Unmarshalling(reply, ifaceConfig)) {
1868                 NETNATIVE_LOGE("NetDiagIfaceConfig Unmarshalling failed.");
1869                 return ERR_FLATTEN_OBJECT;
1870             }
1871             configs.push_back(ifaceConfig);
1872         }
1873     }
1874     return ret;
1875 }
1876 
NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig & config,const std::string & ifaceName,bool add)1877 int32_t NetsysNativeServiceProxy::NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config,
1878                                                                const std::string &ifaceName, bool add)
1879 {
1880     NETNATIVE_LOGI("Begin to NetDiagUpdateInterfaceConfig");
1881     MessageParcel data;
1882     if (!WriteInterfaceToken(data)) {
1883         return ERR_FLATTEN_OBJECT;
1884     }
1885     if (!config.Marshalling(data)) {
1886         return ERR_FLATTEN_OBJECT;
1887     }
1888     if (!data.WriteString(ifaceName)) {
1889         return ERR_FLATTEN_OBJECT;
1890     }
1891     if (!data.WriteBool(add)) {
1892         return ERR_FLATTEN_OBJECT;
1893     }
1894 
1895     sptr<IRemoteObject> remote = Remote();
1896     if (remote == nullptr) {
1897         NETNATIVE_LOGE("Remote is null");
1898         return ERR_FLATTEN_OBJECT;
1899     }
1900     MessageParcel reply;
1901     MessageOption option;
1902     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_UPDATE_IFACE_CONFIG), data, reply,
1903                         option);
1904     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1905     if (!reply.ReadInt32(ret)) {
1906         NETNATIVE_LOGE("Read result failed");
1907         return ERR_FLATTEN_OBJECT;
1908     }
1909     return ret;
1910 }
1911 
NetDiagSetInterfaceActiveState(const std::string & ifaceName,bool up)1912 int32_t NetsysNativeServiceProxy::NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)
1913 {
1914     NETNATIVE_LOGI("Begin to NetDiagSetInterfaceActiveState");
1915     MessageParcel data;
1916     if (!WriteInterfaceToken(data)) {
1917         return ERR_FLATTEN_OBJECT;
1918     }
1919     if (!data.WriteString(ifaceName)) {
1920         return ERR_FLATTEN_OBJECT;
1921     }
1922     if (!data.WriteBool(up)) {
1923         return ERR_FLATTEN_OBJECT;
1924     }
1925 
1926     sptr<IRemoteObject> remote = Remote();
1927     if (remote == nullptr) {
1928         NETNATIVE_LOGE("Remote is null");
1929         return ERR_FLATTEN_OBJECT;
1930     }
1931     MessageParcel reply;
1932     MessageOption option;
1933     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_SET_IFACE_ACTIVE_STATE), data, reply,
1934                         option);
1935     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1936     if (!reply.ReadInt32(ret)) {
1937         NETNATIVE_LOGE("Read result failed");
1938         return ERR_FLATTEN_OBJECT;
1939     }
1940     return ret;
1941 }
1942 
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)1943 int32_t NetsysNativeServiceProxy::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
1944                                                const std::string &ifName)
1945 {
1946     MessageParcel data;
1947     if (!WriteInterfaceToken(data)) {
1948         return ERR_FLATTEN_OBJECT;
1949     }
1950     if (!data.WriteString(ipAddr)) {
1951         return ERR_FLATTEN_OBJECT;
1952     }
1953     if (!data.WriteString(macAddr)) {
1954         return ERR_FLATTEN_OBJECT;
1955     }
1956     if (!data.WriteString(ifName)) {
1957         return ERR_FLATTEN_OBJECT;
1958     }
1959 
1960     MessageParcel reply;
1961     MessageOption option;
1962     sptr<IRemoteObject> remote = Remote();
1963     if (remote == nullptr) {
1964         NETNATIVE_LOGE("AddStaticArp Remote is null");
1965         return ERR_FLATTEN_OBJECT;
1966     }
1967     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ADD_STATIC_ARP),
1968                                         data, reply, option);
1969     if (error != ERR_NONE) {
1970         NETNATIVE_LOGE("AddStaticArp proxy SendRequest failed");
1971         return ERR_FLATTEN_OBJECT;
1972     }
1973     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
1974     if (!reply.ReadInt32(ret)) {
1975         NETNATIVE_LOGE("AddStaticArp proxy read ret failed");
1976         return ERR_FLATTEN_OBJECT;
1977     }
1978     return ret;
1979 }
1980 
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)1981 int32_t NetsysNativeServiceProxy::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
1982                                                const std::string &ifName)
1983 {
1984     MessageParcel data;
1985     if (!WriteInterfaceToken(data)) {
1986         return ERR_FLATTEN_OBJECT;
1987     }
1988     if (!data.WriteString(ipAddr)) {
1989         return ERR_FLATTEN_OBJECT;
1990     }
1991     if (!data.WriteString(macAddr)) {
1992         return ERR_FLATTEN_OBJECT;
1993     }
1994     if (!data.WriteString(ifName)) {
1995         return ERR_FLATTEN_OBJECT;
1996     }
1997 
1998     MessageParcel reply;
1999     MessageOption option;
2000     sptr<IRemoteObject> remote = Remote();
2001     if (remote == nullptr) {
2002         NETNATIVE_LOGE("DelStaticArp Remote is null");
2003         return ERR_FLATTEN_OBJECT;
2004     }
2005     int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_STATIC_ARP),
2006                                         data, reply, option);
2007     if (error != ERR_NONE) {
2008         NETNATIVE_LOGE("DelStaticArp proxy SendRequest failed");
2009         return ERR_FLATTEN_OBJECT;
2010     }
2011     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2012     if (!reply.ReadInt32(ret)) {
2013         NETNATIVE_LOGE("DelStaticArp proxy read ret failed");
2014         return ERR_FLATTEN_OBJECT;
2015     }
2016     return ret;
2017 }
2018 
RegisterDnsResultCallback(const sptr<OHOS::NetsysNative::INetDnsResultCallback> & callback,uint32_t timeStep)2019 int32_t NetsysNativeServiceProxy::RegisterDnsResultCallback(
2020     const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep)
2021 {
2022     NETNATIVE_LOGI("Begin to RegisterDnsResultCallback");
2023     if (callback == nullptr) {
2024         NETNATIVE_LOGE("INetDnsResultCallback is nullptr");
2025         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2026     }
2027     MessageParcel data;
2028     if (!WriteInterfaceToken(data)) {
2029         return ERR_FLATTEN_OBJECT;
2030     }
2031     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2032         return ERR_FLATTEN_OBJECT;
2033     }
2034     if (!data.WriteUint32(timeStep)) {
2035         return ERR_FLATTEN_OBJECT;
2036     }
2037 
2038     sptr<IRemoteObject> remote = Remote();
2039     if (remote == nullptr) {
2040         NETNATIVE_LOGE("Remote is null");
2041         return ERR_FLATTEN_OBJECT;
2042     }
2043     MessageParcel reply;
2044     MessageOption option;
2045     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_RESULT_LISTENER), data, reply,
2046                         option);
2047     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2048     if (!reply.ReadInt32(ret)) {
2049         NETNATIVE_LOGE("Read result failed");
2050         return ERR_FLATTEN_OBJECT;
2051     }
2052     return ret;
2053 }
2054 
UnregisterDnsResultCallback(const sptr<OHOS::NetsysNative::INetDnsResultCallback> & callback)2055 int32_t NetsysNativeServiceProxy::UnregisterDnsResultCallback(
2056     const sptr<OHOS::NetsysNative::INetDnsResultCallback> &callback)
2057 {
2058     NETNATIVE_LOGI("Begin to UnregisterDnsResultCallback");
2059     if (callback == nullptr) {
2060         NETNATIVE_LOGE("INetDnsResultCallback is nullptr");
2061         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2062     }
2063     MessageParcel data;
2064     if (!WriteInterfaceToken(data)) {
2065         return ERR_FLATTEN_OBJECT;
2066     }
2067     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2068         return ERR_FLATTEN_OBJECT;
2069     }
2070 
2071     sptr<IRemoteObject> remote = Remote();
2072     if (remote == nullptr) {
2073         NETNATIVE_LOGE("Remote is null");
2074         return ERR_FLATTEN_OBJECT;
2075     }
2076     MessageParcel reply;
2077     MessageOption option;
2078     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_RESULT_LISTENER), data, reply,
2079                         option);
2080     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2081     if (!reply.ReadInt32(ret)) {
2082         NETNATIVE_LOGE("Read result failed");
2083         return ERR_FLATTEN_OBJECT;
2084     }
2085     return ret;
2086 }
2087 
RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)2088 int32_t NetsysNativeServiceProxy::RegisterDnsHealthCallback(
2089     const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
2090 {
2091     NETNATIVE_LOGI("Begin to RegisterDnsHealthCallback");
2092     if (callback == nullptr) {
2093         NETNATIVE_LOGE("INetDnsHealthCallback is nullptr");
2094         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2095     }
2096     MessageParcel data;
2097     if (!WriteInterfaceToken(data)) {
2098         return ERR_FLATTEN_OBJECT;
2099     }
2100     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2101         return ERR_FLATTEN_OBJECT;
2102     }
2103 
2104     sptr<IRemoteObject> remote = Remote();
2105     if (remote == nullptr) {
2106         NETNATIVE_LOGE("Remote is null");
2107         return ERR_FLATTEN_OBJECT;
2108     }
2109     MessageParcel reply;
2110     MessageOption option;
2111     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_HEALTH_LISTENER), data, reply,
2112                         option);
2113     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2114     if (!reply.ReadInt32(ret)) {
2115         NETNATIVE_LOGE("Read result failed");
2116         return ERR_FLATTEN_OBJECT;
2117     }
2118     return ret;
2119 }
2120 
UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> & callback)2121 int32_t NetsysNativeServiceProxy::UnregisterDnsHealthCallback(
2122     const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback)
2123 {
2124     NETNATIVE_LOGI("Begin to UnregisterDnsHealthCallback");
2125     if (callback == nullptr) {
2126         NETNATIVE_LOGE("INetDnsHealthCallback is nullptr");
2127         return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
2128     }
2129     MessageParcel data;
2130     if (!WriteInterfaceToken(data)) {
2131         return ERR_FLATTEN_OBJECT;
2132     }
2133     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
2134         return ERR_FLATTEN_OBJECT;
2135     }
2136 
2137     sptr<IRemoteObject> remote = Remote();
2138     if (remote == nullptr) {
2139         NETNATIVE_LOGE("Remote is null");
2140         return ERR_FLATTEN_OBJECT;
2141     }
2142     MessageParcel reply;
2143     MessageOption option;
2144     remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_HEALTH_LISTENER), data, reply,
2145                         option);
2146     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2147     if (!reply.ReadInt32(ret)) {
2148         NETNATIVE_LOGE("Read result failed");
2149         return ERR_FLATTEN_OBJECT;
2150     }
2151     return ret;
2152 }
2153 
GetCookieStats(uint64_t & stats,uint32_t type,uint64_t cookie)2154 int32_t NetsysNativeServiceProxy::GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)
2155 {
2156     MessageParcel data;
2157     if (!WriteInterfaceToken(data)) {
2158         return ERR_FLATTEN_OBJECT;
2159     }
2160     if (!data.WriteUint32(type)) {
2161         return ERR_FLATTEN_OBJECT;
2162     }
2163     if (!data.WriteUint64(cookie)) {
2164         return ERR_FLATTEN_OBJECT;
2165     }
2166     MessageParcel reply;
2167     MessageOption option;
2168     int32_t res = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_COOKIE_STATS),
2169                                         data, reply, option);
2170     if (res != ERR_NONE) {
2171         NETNATIVE_LOGE("GetCookieStats SendRequest failed");
2172         return ERR_FLATTEN_OBJECT;
2173     }
2174 
2175     int32_t ret = NetManagerStandard::NETMANAGER_SUCCESS;
2176     if (!reply.ReadInt32(ret)) {
2177         NETNATIVE_LOGE("get ret falil");
2178         return ERR_FLATTEN_OBJECT;
2179     }
2180 
2181     if (!reply.ReadUint64(stats)) {
2182         NETNATIVE_LOGE("get stats falil");
2183         return ERR_FLATTEN_OBJECT;
2184     }
2185     return ret;
2186 }
2187 } // namespace NetsysNative
2188 } // namespace OHOS
2189