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 "netnative_log_wrapper.h"
21 #include "netsys_addr_info_parcel.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),
182 data, reply, 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
GetInterfaceMtu(const std::string & interfaceName)246 int32_t NetsysNativeServiceProxy::GetInterfaceMtu(const std::string &interfaceName)
247 {
248 NETNATIVE_LOGI("Begin to GetInterfaceMtu");
249 MessageParcel data;
250 if (!WriteInterfaceToken(data)) {
251 return ERR_FLATTEN_OBJECT;
252 }
253 if (!data.WriteString(interfaceName)) {
254 return ERR_FLATTEN_OBJECT;
255 }
256
257 MessageParcel reply;
258 MessageOption option;
259 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_MTU), data, reply, option);
260
261 return reply.ReadInt32();
262 }
263
RegisterNotifyCallback(sptr<INotifyCallback> & callback)264 int32_t NetsysNativeServiceProxy::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
265 {
266 NETNATIVE_LOGI("Begin to RegisterNotifyCallback");
267 MessageParcel data;
268 if (callback == nullptr) {
269 NETNATIVE_LOGE("The parameter of callback is nullptr");
270 return ERR_NULL_OBJECT;
271 }
272
273 if (!WriteInterfaceToken(data)) {
274 return ERR_FLATTEN_OBJECT;
275 }
276 data.WriteRemoteObject(callback->AsObject().GetRefPtr());
277
278 MessageParcel reply;
279 MessageOption option;
280 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_NOTIFY_CALLBACK),
281 data, reply, option);
282
283 return reply.ReadInt32();
284 }
285
UnRegisterNotifyCallback(sptr<INotifyCallback> & callback)286 int32_t NetsysNativeServiceProxy::UnRegisterNotifyCallback(sptr<INotifyCallback> &callback)
287 {
288 NETNATIVE_LOGI("Begin to UnRegisterNotifyCallback");
289 MessageParcel data;
290 if (callback == nullptr) {
291 NETNATIVE_LOGE("The parameter of callback is nullptr");
292 return ERR_NULL_OBJECT;
293 }
294
295 if (!WriteInterfaceToken(data)) {
296 NETNATIVE_LOGE("WriteInterfaceToken fail");
297 return ERR_FLATTEN_OBJECT;
298 }
299
300 data.WriteRemoteObject(callback->AsObject().GetRefPtr());
301
302 MessageParcel reply;
303 MessageOption option;
304 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_NOTIFY_CALLBACK),
305 data, reply, option);
306
307 return reply.ReadInt32();
308 }
309
NetworkAddRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)310 int32_t NetsysNativeServiceProxy::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
311 const std::string &destination, const std::string &nextHop)
312 {
313 NETNATIVE_LOGI("Begin to NetworkAddRoute");
314 MessageParcel data;
315 if (!WriteInterfaceToken(data)) {
316 return ERR_FLATTEN_OBJECT;
317 }
318 if (!data.WriteInt32(netId)) {
319 return ERR_FLATTEN_OBJECT;
320 }
321 if (!data.WriteString(interfaceName)) {
322 return ERR_FLATTEN_OBJECT;
323 }
324 if (!data.WriteString(destination)) {
325 return ERR_FLATTEN_OBJECT;
326 }
327 if (!data.WriteString(nextHop)) {
328 return ERR_FLATTEN_OBJECT;
329 }
330
331 MessageParcel reply;
332 MessageOption option;
333 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE), data, reply, option);
334
335 return reply.ReadInt32();
336 }
337
NetworkRemoveRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)338 int32_t NetsysNativeServiceProxy::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
339 const std::string &destination, const std::string &nextHop)
340 {
341 NETNATIVE_LOGI("Begin to NetworkRemoveRoute");
342 MessageParcel data;
343 if (!WriteInterfaceToken(data)) {
344 return ERR_FLATTEN_OBJECT;
345 }
346 if (!data.WriteInt32(netId)) {
347 return ERR_FLATTEN_OBJECT;
348 }
349 if (!data.WriteString(interfaceName)) {
350 return ERR_FLATTEN_OBJECT;
351 }
352 if (!data.WriteString(destination)) {
353 return ERR_FLATTEN_OBJECT;
354 }
355 if (!data.WriteString(nextHop)) {
356 return ERR_FLATTEN_OBJECT;
357 }
358
359 MessageParcel reply;
360 MessageOption option;
361 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE), data, reply, option);
362
363 return reply.ReadInt32();
364 }
365
NetworkAddRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)366 int32_t NetsysNativeServiceProxy::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
367 {
368 NETNATIVE_LOGI("Begin to NetworkAddRouteParcel");
369 MessageParcel data;
370 if (!WriteInterfaceToken(data)) {
371 return ERR_FLATTEN_OBJECT;
372 }
373 if (!data.WriteInt32(netId)) {
374 return ERR_FLATTEN_OBJECT;
375 }
376 if (!data.WriteString(routeInfo.ifName)) {
377 return ERR_FLATTEN_OBJECT;
378 }
379 if (!data.WriteString(routeInfo.destination)) {
380 return ERR_FLATTEN_OBJECT;
381 }
382 if (!data.WriteString(routeInfo.nextHop)) {
383 return ERR_FLATTEN_OBJECT;
384 }
385
386 MessageParcel reply;
387 MessageOption option;
388 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE_PARCEL),
389 data, reply, option);
390
391 return reply.ReadInt32();
392 }
393
NetworkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)394 int32_t NetsysNativeServiceProxy::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
395 {
396 NETNATIVE_LOGI("Begin to NetworkRemoveRouteParcel");
397 MessageParcel data;
398 if (!WriteInterfaceToken(data)) {
399 return ERR_FLATTEN_OBJECT;
400 }
401 if (!data.WriteInt32(netId)) {
402 return ERR_FLATTEN_OBJECT;
403 }
404 if (!data.WriteString(routeInfo.ifName)) {
405 return ERR_FLATTEN_OBJECT;
406 }
407 if (!data.WriteString(routeInfo.destination)) {
408 return ERR_FLATTEN_OBJECT;
409 }
410 if (!data.WriteString(routeInfo.nextHop)) {
411 return ERR_FLATTEN_OBJECT;
412 }
413
414 MessageParcel reply;
415 MessageOption option;
416 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE_PARCEL),
417 data, reply, option);
418
419 return reply.ReadInt32();
420 }
421
NetworkSetDefault(int32_t netId)422 int32_t NetsysNativeServiceProxy::NetworkSetDefault(int32_t netId)
423 {
424 NETNATIVE_LOGI("Begin to NetworkSetDefault");
425 MessageParcel data;
426 if (!WriteInterfaceToken(data)) {
427 return ERR_FLATTEN_OBJECT;
428 }
429 if (!data.WriteInt32(netId)) {
430 return ERR_FLATTEN_OBJECT;
431 }
432
433 MessageParcel reply;
434 MessageOption option;
435 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_DEFAULT), data, reply, option);
436
437 return reply.ReadInt32();
438 }
439
NetworkGetDefault()440 int32_t NetsysNativeServiceProxy::NetworkGetDefault()
441 {
442 NETNATIVE_LOGI("Begin to NetworkGetDefault");
443 MessageParcel data;
444 if (!WriteInterfaceToken(data)) {
445 return ERR_FLATTEN_OBJECT;
446 }
447
448 MessageParcel reply;
449 MessageOption option;
450 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_GET_DEFAULT), data, reply, option);
451
452 return reply.ReadInt32();
453 }
454
NetworkClearDefault()455 int32_t NetsysNativeServiceProxy::NetworkClearDefault()
456 {
457 NETNATIVE_LOGI("Begin to NetworkClearDefault");
458 MessageParcel data;
459 if (!WriteInterfaceToken(data)) {
460 return ERR_FLATTEN_OBJECT;
461 }
462
463 MessageParcel reply;
464 MessageOption option;
465 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CLEAR_DEFAULT),
466 data, reply, option);
467
468 return reply.ReadInt32();
469 }
470
GetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)471 int32_t NetsysNativeServiceProxy::GetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
472 const std::string ¶meter, std::string &value)
473 {
474 NETNATIVE_LOGI("Begin to GetSysProcNet");
475 MessageParcel data;
476 if (!WriteInterfaceToken(data)) {
477 return ERR_FLATTEN_OBJECT;
478 }
479
480 if (!data.WriteInt32(family)) {
481 return ERR_FLATTEN_OBJECT;
482 }
483 if (!data.WriteInt32(which)) {
484 return ERR_FLATTEN_OBJECT;
485 }
486 if (data.WriteString(ifname)) {
487 return ERR_FLATTEN_OBJECT;
488 }
489 if (!data.WriteString(parameter)) {
490 return ERR_FLATTEN_OBJECT;
491 }
492 MessageParcel reply;
493 MessageOption option;
494 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_PROC_SYS_NET), data, reply, option);
495 int32_t ret = reply.ReadInt32();
496 if (ret != ERR_NONE) {
497 NETNATIVE_LOGE("Fail to GetProcSysNet ret= %{public}d", ret);
498 return ret;
499 }
500 std::string valueRsl = reply.ReadString();
501 NETNATIVE_LOGE("NETSYS_GET_PROC_SYS_NET value %{public}s", valueRsl.c_str());
502 value = valueRsl;
503 return ret;
504 }
505
SetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)506 int32_t NetsysNativeServiceProxy::SetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
507 const std::string ¶meter, std::string &value)
508 {
509 NETNATIVE_LOGI("Begin to SetSysProcNet");
510 MessageParcel data;
511 if (!WriteInterfaceToken(data)) {
512 return ERR_FLATTEN_OBJECT;
513 }
514 if (!data.WriteInt32(family)) {
515 return ERR_FLATTEN_OBJECT;
516 }
517 if (!data.WriteInt32(which)) {
518 return ERR_FLATTEN_OBJECT;
519 }
520 if (data.WriteString(ifname)) {
521 return ERR_FLATTEN_OBJECT;
522 }
523 if (!data.WriteString(parameter)) {
524 return ERR_FLATTEN_OBJECT;
525 }
526 if (!data.WriteString(value)) {
527 return ERR_FLATTEN_OBJECT;
528 }
529 MessageParcel reply;
530 MessageOption option;
531 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_PROC_SYS_NET), data, reply, option);
532
533 return reply.ReadInt32();
534 }
535
SetInternetPermission(uint32_t uid,uint8_t allow)536 int32_t NetsysNativeServiceProxy::SetInternetPermission(uint32_t uid, uint8_t allow)
537 {
538 MessageParcel data;
539 if (!WriteInterfaceToken(data)) {
540 return ERR_FLATTEN_OBJECT;
541 }
542
543 if (!data.WriteUint32(uid)) {
544 return ERR_FLATTEN_OBJECT;
545 }
546
547 if (!data.WriteUint8(allow)) {
548 return ERR_FLATTEN_OBJECT;
549 }
550
551 MessageParcel reply;
552 MessageOption option;
553 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_INTERNET_PERMISSION),
554 data, reply, option);
555
556 return reply.ReadInt32();
557 }
558
NetworkCreatePhysical(int32_t netId,int32_t permission)559 int32_t NetsysNativeServiceProxy::NetworkCreatePhysical(int32_t netId, int32_t permission)
560 {
561 NETNATIVE_LOGI("Begin to NetworkCreatePhysical");
562 MessageParcel data;
563 if (!WriteInterfaceToken(data)) {
564 return ERR_FLATTEN_OBJECT;
565 }
566 if (!data.WriteInt32(netId)) {
567 return ERR_FLATTEN_OBJECT;
568 }
569 if (!data.WriteInt32(permission)) {
570 return ERR_FLATTEN_OBJECT;
571 }
572
573 MessageParcel reply;
574 MessageOption option;
575 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_PHYSICAL),
576 data, reply, option);
577
578 return reply.ReadInt32();
579 }
580
NetworkCreateVirtual(int32_t netId,bool hasDns)581 int32_t NetsysNativeServiceProxy::NetworkCreateVirtual(int32_t netId, bool hasDns)
582 {
583 MessageParcel data;
584 if (!WriteInterfaceToken(data) || !data.WriteInt32(netId) || !data.WriteBool(hasDns)) {
585 return ERR_FLATTEN_OBJECT;
586 }
587
588 MessageParcel reply;
589 MessageOption option;
590 sptr<IRemoteObject> remote = Remote();
591 if (remote == nullptr) {
592 return IPC_PROXY_NULL_INVOKER_ERR;
593 }
594 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_VIRTUAL),
595 data, reply, option);
596 if (ERR_NONE != ret) {
597 NETNATIVE_LOGE("NetworkCreateVirtual proxy SendRequest failed, error code: [%{public}d]", ret);
598 return IPC_INVOKER_ERR;
599 }
600
601 int32_t result = ERR_INVALID_DATA;
602 if (!reply.ReadInt32(result)) {
603 return IPC_PROXY_TRANSACTION_ERR;
604 }
605 return result;
606 }
607
NetworkAddUids(int32_t netId,const std::vector<UidRange> & uidRanges)608 int32_t NetsysNativeServiceProxy::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
609 {
610 MessageParcel data;
611 if (!WriteInterfaceToken(data) || !data.WriteInt32(netId)) {
612 return ERR_FLATTEN_OBJECT;
613 }
614
615 if (!data.WriteInt32(uidRanges.size())) {
616 return IPC_PROXY_TRANSACTION_ERR;
617 }
618 for (auto iter : uidRanges) {
619 if (!iter.Marshalling(data)) {
620 return IPC_PROXY_TRANSACTION_ERR;
621 }
622 }
623
624 MessageParcel reply;
625 MessageOption option;
626 sptr<IRemoteObject> remote = Remote();
627 if (remote == nullptr) {
628 return IPC_PROXY_NULL_INVOKER_ERR;
629 }
630 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_UIDS),
631 data, reply, option);
632 if (ret != ERR_NONE) {
633 NETNATIVE_LOGE("NetworkAddUids proxy SendRequest failed, error code: [%{public}d]", ret);
634 return IPC_INVOKER_ERR;
635 }
636
637 int32_t result = ERR_INVALID_DATA;
638 if (!reply.ReadInt32(result)) {
639 return IPC_PROXY_TRANSACTION_ERR;
640 }
641 return result;
642 }
643
NetworkDelUids(int32_t netId,const std::vector<UidRange> & uidRanges)644 int32_t NetsysNativeServiceProxy::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
645 {
646 MessageParcel data;
647 if (!WriteInterfaceToken(data) || !data.WriteInt32(netId)) {
648 return ERR_FLATTEN_OBJECT;
649 }
650
651 if (!data.WriteInt32(uidRanges.size())) {
652 return IPC_PROXY_TRANSACTION_ERR;
653 }
654 for (auto iter : uidRanges) {
655 if (!iter.Marshalling(data)) {
656 return IPC_PROXY_TRANSACTION_ERR;
657 }
658 }
659
660 MessageParcel reply;
661 MessageOption option;
662 sptr<IRemoteObject> remote = Remote();
663 if (remote == nullptr) {
664 return IPC_PROXY_NULL_INVOKER_ERR;
665 }
666 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DEL_UIDS),
667 data, reply, option);
668 if (ret != ERR_NONE) {
669 NETNATIVE_LOGE("NetworkDelUids proxy SendRequest failed, error code: [%{public}d]", ret);
670 return ERR_FLATTEN_OBJECT;
671 }
672
673 int32_t result = ERR_INVALID_DATA;
674 if (!reply.ReadInt32(result)) {
675 return IPC_PROXY_TRANSACTION_ERR;
676 }
677 return result;
678 }
679
AddInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)680 int32_t NetsysNativeServiceProxy::AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
681 int32_t prefixLength)
682 {
683 NETNATIVE_LOGI("Begin to AddInterfaceAddress");
684 MessageParcel data;
685 if (!WriteInterfaceToken(data)) {
686 return ERR_FLATTEN_OBJECT;
687 }
688 if (!data.WriteString(interfaceName)) {
689 return ERR_FLATTEN_OBJECT;
690 }
691 if (!data.WriteString(addrString)) {
692 return ERR_FLATTEN_OBJECT;
693 }
694 if (!data.WriteInt32(prefixLength)) {
695 return ERR_FLATTEN_OBJECT;
696 }
697
698 MessageParcel reply;
699 MessageOption option;
700 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_ADD_ADDRESS),
701 data, reply, option);
702
703 return reply.ReadInt32();
704 }
705
DelInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)706 int32_t NetsysNativeServiceProxy::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
707 int32_t prefixLength)
708 {
709 NETNATIVE_LOGI("Begin to DelInterfaceAddress");
710 MessageParcel data;
711 if (!WriteInterfaceToken(data)) {
712 return ERR_FLATTEN_OBJECT;
713 }
714 if (!data.WriteString(interfaceName)) {
715 return ERR_FLATTEN_OBJECT;
716 }
717 if (!data.WriteString(addrString)) {
718 return ERR_FLATTEN_OBJECT;
719 }
720 if (!data.WriteInt32(prefixLength)) {
721 return ERR_FLATTEN_OBJECT;
722 }
723
724 MessageParcel reply;
725 MessageOption option;
726 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_DEL_ADDRESS),
727 data, reply, option);
728
729 return reply.ReadInt32();
730 }
731
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)732 int32_t NetsysNativeServiceProxy::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
733 {
734 NETNATIVE_LOGI("Begin to InterfaceSetIpAddress");
735 MessageParcel data;
736 if (!WriteInterfaceToken(data)) {
737 return ERR_FLATTEN_OBJECT;
738 }
739 if (!data.WriteString(ifaceName) || !data.WriteString(ipAddress)) {
740 return ERR_FLATTEN_OBJECT;
741 }
742 MessageParcel reply;
743 MessageOption option;
744 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IP_ADDRESS),
745 data, reply, option);
746
747 return reply.ReadInt32();
748 }
749
InterfaceSetIffUp(const std::string & ifaceName)750 int32_t NetsysNativeServiceProxy::InterfaceSetIffUp(const std::string &ifaceName)
751 {
752 NETNATIVE_LOGI("Begin to InterfaceSetIffUp");
753 MessageParcel data;
754 if (!WriteInterfaceToken(data)) {
755 return ERR_FLATTEN_OBJECT;
756 }
757 if (!data.WriteString(ifaceName)) {
758 return ERR_FLATTEN_OBJECT;
759 }
760 MessageParcel reply;
761 MessageOption option;
762 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IFF_UP), data, reply, option);
763
764 return reply.ReadInt32();
765 }
766
NetworkAddInterface(int32_t netId,const std::string & iface)767 int32_t NetsysNativeServiceProxy::NetworkAddInterface(int32_t netId, const std::string &iface)
768 {
769 NETNATIVE_LOGI("Begin to NetworkAddInterface");
770 MessageParcel data;
771 if (!WriteInterfaceToken(data)) {
772 return ERR_FLATTEN_OBJECT;
773 }
774 if (!data.WriteInt32(netId)) {
775 return ERR_FLATTEN_OBJECT;
776 }
777 if (!data.WriteString(iface)) {
778 return ERR_FLATTEN_OBJECT;
779 }
780
781 MessageParcel reply;
782 MessageOption option;
783 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_INTERFACE),
784 data, reply, option);
785
786 return reply.ReadInt32();
787 }
788
NetworkRemoveInterface(int32_t netId,const std::string & iface)789 int32_t NetsysNativeServiceProxy::NetworkRemoveInterface(int32_t netId, const std::string &iface)
790 {
791 NETNATIVE_LOGI("Begin to NetworkRemoveInterface");
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_REMOVE_INTERFACE),
806 data, reply, option);
807
808 return reply.ReadInt32();
809 }
810
NetworkDestroy(int32_t netId)811 int32_t NetsysNativeServiceProxy::NetworkDestroy(int32_t netId)
812 {
813 NETNATIVE_LOGI("Begin to NetworkDestroy");
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
822 MessageParcel reply;
823 MessageOption option;
824 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DESTROY), data, reply, option);
825
826 return reply.ReadInt32();
827 }
828
GetFwmarkForNetwork(int32_t netId,MarkMaskParcel & markMaskParcel)829 int32_t NetsysNativeServiceProxy::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
830 {
831 NETNATIVE_LOGI("Begin to GetFwmarkForNetwork");
832 MessageParcel data;
833 if (!WriteInterfaceToken(data)) {
834 return ERR_FLATTEN_OBJECT;
835 }
836 if (!data.WriteInt32(netId)) {
837 return ERR_FLATTEN_OBJECT;
838 }
839 if (!data.WriteInt32(markMaskParcel.mark)) {
840 return ERR_FLATTEN_OBJECT;
841 }
842 if (!data.WriteInt32(markMaskParcel.mask)) {
843 return ERR_FLATTEN_OBJECT;
844 }
845
846 MessageParcel reply;
847 MessageOption option;
848 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_FWMARK_FOR_NETWORK),
849 data, reply, option);
850
851 return reply.ReadInt32();
852 }
853
SetInterfaceConfig(const InterfaceConfigurationParcel & cfg)854 int32_t NetsysNativeServiceProxy::SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)
855 {
856 NETNATIVE_LOGI("Begin to SetInterfaceConfig");
857 MessageParcel data;
858 if (!WriteInterfaceToken(data)) {
859 return ERR_FLATTEN_OBJECT;
860 }
861 if (!data.WriteString(cfg.ifName)) {
862 return ERR_FLATTEN_OBJECT;
863 }
864 if (!data.WriteString(cfg.hwAddr)) {
865 return ERR_FLATTEN_OBJECT;
866 }
867 if (!data.WriteString(cfg.ipv4Addr)) {
868 return ERR_FLATTEN_OBJECT;
869 }
870 if (!data.WriteInt32(cfg.prefixLength)) {
871 return ERR_FLATTEN_OBJECT;
872 }
873 int32_t vsize = static_cast<int32_t>(cfg.flags.size());
874 if (!data.WriteInt32(vsize)) {
875 return ERR_FLATTEN_OBJECT;
876 }
877 std::vector<std::string> vCflags;
878 vCflags.assign(cfg.flags.begin(), cfg.flags.end());
879 NETNATIVE_LOGI("PROXY: SetInterfaceConfig Write flags String_SIZE: %{public}d",
880 static_cast<int32_t>(vCflags.size()));
881 for (std::vector<std::string>::iterator it = vCflags.begin(); it != vCflags.end(); ++it) {
882 data.WriteString(*it);
883 }
884 MessageParcel reply;
885 MessageOption option;
886 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_CONFIG), data, reply, option);
887
888 return reply.ReadInt32();
889 }
890
GetInterfaceConfig(InterfaceConfigurationParcel & cfg)891 int32_t NetsysNativeServiceProxy::GetInterfaceConfig(InterfaceConfigurationParcel &cfg)
892 {
893 NETNATIVE_LOGI("Begin to GetInterfaceConfig");
894 MessageParcel data;
895 int32_t ret;
896 int32_t vSize;
897 if (!WriteInterfaceToken(data)) {
898 return ERR_FLATTEN_OBJECT;
899 }
900 if (!data.WriteString(cfg.ifName)) {
901 return ERR_FLATTEN_OBJECT;
902 }
903
904 MessageParcel reply;
905 MessageOption option;
906 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_CONFIG), data, reply, option);
907 ret = reply.ReadInt32();
908 if (ret != ERR_NONE) {
909 NETNATIVE_LOGE("Fail to GetInterfaceConfig ret= %{public}d", ret);
910 return ret;
911 }
912 reply.ReadString(cfg.ifName);
913 reply.ReadString(cfg.hwAddr);
914 reply.ReadString(cfg.ipv4Addr);
915 reply.ReadInt32(cfg.prefixLength);
916 vSize = reply.ReadInt32();
917 vSize = vSize > MAX_INTERFACE_CONFIG_SIZE ? MAX_INTERFACE_CONFIG_SIZE : vSize;
918 std::vector<std::string> vecString;
919 for (int i = 0; i < vSize; i++) {
920 vecString.push_back(reply.ReadString());
921 }
922 if (vSize > 0) {
923 cfg.flags.assign(vecString.begin(), vecString.end());
924 }
925 NETNATIVE_LOGI("End to GetInterfaceConfig, ret =%{public}d", ret);
926 return ret;
927 }
928
InterfaceGetList(std::vector<std::string> & ifaces)929 int32_t NetsysNativeServiceProxy::InterfaceGetList(std::vector<std::string> &ifaces)
930 {
931 NETNATIVE_LOGI("NetsysNativeServiceProxy Begin to InterfaceGetList");
932 MessageParcel data;
933 int32_t ret;
934 int32_t vSize;
935 if (!WriteInterfaceToken(data)) {
936 return ERR_FLATTEN_OBJECT;
937 }
938 MessageParcel reply;
939 MessageOption option;
940 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_LIST), data, reply, option);
941 ret = reply.ReadInt32();
942 if (ret != ERR_NONE) {
943 NETNATIVE_LOGE("Fail to InterfaceGetList ret= %{public}d", ret);
944 return ret;
945 }
946 vSize = reply.ReadInt32();
947 std::vector<std::string> vecString;
948 for (int i = 0; i < vSize; i++) {
949 vecString.push_back(reply.ReadString());
950 }
951 if (vSize > 0) {
952 ifaces.assign(vecString.begin(), vecString.end());
953 }
954 NETNATIVE_LOGI("NetsysNativeServiceProxy End to InterfaceGetList, ret =%{public}d", ret);
955 return ret;
956 }
957
StartDhcpClient(const std::string & iface,bool bIpv6)958 int32_t NetsysNativeServiceProxy::StartDhcpClient(const std::string &iface, bool bIpv6)
959 {
960 NETNATIVE_LOGI("Begin to StartDhcpClient");
961 MessageParcel data;
962 int32_t ret;
963 if (!WriteInterfaceToken(data)) {
964 return ERR_FLATTEN_OBJECT;
965 }
966 if (!data.WriteString(iface)) {
967 return ERR_FLATTEN_OBJECT;
968 }
969 if (!data.WriteBool(bIpv6)) {
970 return ERR_FLATTEN_OBJECT;
971 }
972
973 MessageParcel reply;
974 MessageOption option;
975 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_CLIENT), data, reply, option);
976
977 ret = reply.ReadInt32();
978 NETNATIVE_LOGI("End to StartDhcpClient, ret =%{public}d", ret);
979 return ret;
980 }
981
StopDhcpClient(const std::string & iface,bool bIpv6)982 int32_t NetsysNativeServiceProxy::StopDhcpClient(const std::string &iface, bool bIpv6)
983 {
984 NETNATIVE_LOGI("Begin to StopDhcpClient");
985 MessageParcel data;
986 int32_t ret;
987 if (!WriteInterfaceToken(data)) {
988 return ERR_FLATTEN_OBJECT;
989 }
990 if (!data.WriteString(iface)) {
991 return ERR_FLATTEN_OBJECT;
992 }
993 if (!data.WriteBool(bIpv6)) {
994 return ERR_FLATTEN_OBJECT;
995 }
996
997 MessageParcel reply;
998 MessageOption option;
999 ret = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_CLIENT),
1000 data, reply, option);
1001 NETNATIVE_LOGI("SendRequest, ret =%{public}d", ret);
1002 ret = reply.ReadInt32();
1003 NETNATIVE_LOGI("End to StopDhcpClient, ret =%{public}d", ret);
1004 return ret;
1005 }
1006
StartDhcpService(const std::string & iface,const std::string & ipv4addr)1007 int32_t NetsysNativeServiceProxy::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
1008 {
1009 NETNATIVE_LOGI("Begin to StartDhcpService");
1010 MessageParcel data;
1011
1012 if (!WriteInterfaceToken(data)) {
1013 return ERR_FLATTEN_OBJECT;
1014 }
1015 if (!data.WriteString(iface)) {
1016 return ERR_FLATTEN_OBJECT;
1017 }
1018 if (!data.WriteString(ipv4addr)) {
1019 return ERR_FLATTEN_OBJECT;
1020 }
1021
1022 MessageParcel reply;
1023 MessageOption option;
1024 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_SERVICE), data, reply, option);
1025 int32_t ret = reply.ReadInt32();
1026 NETNATIVE_LOGI("End to StartDhcpService, ret =%{public}d", ret);
1027 return ret;
1028 }
1029
StopDhcpService(const std::string & iface)1030 int32_t NetsysNativeServiceProxy::StopDhcpService(const std::string &iface)
1031 {
1032 NETNATIVE_LOGI("Begin to StopDhcpService");
1033 MessageParcel data;
1034 if (!WriteInterfaceToken(data)) {
1035 return ERR_FLATTEN_OBJECT;
1036 }
1037 if (!data.WriteString(iface)) {
1038 return ERR_FLATTEN_OBJECT;
1039 }
1040
1041 MessageParcel reply;
1042 MessageOption option;
1043 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_SERVICE), data, reply, option);
1044 int32_t ret = reply.ReadInt32();
1045 NETNATIVE_LOGI("End to StopDhcpService, ret =%{public}d", ret);
1046 return ret;
1047 }
1048
IpEnableForwarding(const std::string & requestor)1049 int32_t NetsysNativeServiceProxy::IpEnableForwarding(const std::string &requestor)
1050 {
1051 MessageParcel data;
1052 if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1053 return ERR_FLATTEN_OBJECT;
1054 }
1055
1056 MessageParcel reply;
1057 MessageOption option;
1058 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPENABLE_FORWARDING), data, reply, option);
1059
1060 int32_t ret = reply.ReadInt32();
1061 NETNATIVE_LOGI("End to IpEnableForwarding, ret =%{public}d", ret);
1062 return ret;
1063 }
1064
IpDisableForwarding(const std::string & requestor)1065 int32_t NetsysNativeServiceProxy::IpDisableForwarding(const std::string &requestor)
1066 {
1067 MessageParcel data;
1068 if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
1069 return ERR_FLATTEN_OBJECT;
1070 }
1071
1072 MessageParcel reply;
1073 MessageOption option;
1074 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPDISABLE_FORWARDING), data, reply, option);
1075
1076 int32_t ret = reply.ReadInt32();
1077 NETNATIVE_LOGI("End to IpDisableForwarding, ret =%{public}d", ret);
1078 return ret;
1079 }
1080
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)1081 int32_t NetsysNativeServiceProxy::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1082 {
1083 MessageParcel data;
1084 if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1085 return ERR_FLATTEN_OBJECT;
1086 }
1087
1088 MessageParcel reply;
1089 MessageOption option;
1090 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_NAT), data, reply, option);
1091
1092 int32_t ret = reply.ReadInt32();
1093 NETNATIVE_LOGI("End to EnableNat, ret =%{public}d", ret);
1094 return ret;
1095 }
1096
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)1097 int32_t NetsysNativeServiceProxy::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
1098 {
1099 MessageParcel data;
1100 if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
1101 return ERR_FLATTEN_OBJECT;
1102 }
1103
1104 MessageParcel reply;
1105 MessageOption option;
1106 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_NAT), data, reply, option);
1107
1108 int32_t ret = reply.ReadInt32();
1109 NETNATIVE_LOGI("End to DisableNat, ret =%{public}d", ret);
1110 return ret;
1111 }
1112
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)1113 int32_t NetsysNativeServiceProxy::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
1114 {
1115 MessageParcel data;
1116 if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1117 return ERR_FLATTEN_OBJECT;
1118 }
1119
1120 MessageParcel reply;
1121 MessageOption option;
1122 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_ADD_INTERFACE_FORWARD),
1123 data, reply, option);
1124
1125 int32_t ret = reply.ReadInt32();
1126 NETNATIVE_LOGI("End to IpfwdAddInterfaceForward, ret =%{public}d", ret);
1127 return ret;
1128 }
1129
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)1130 int32_t NetsysNativeServiceProxy::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
1131 {
1132 MessageParcel data;
1133 if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
1134 return ERR_FLATTEN_OBJECT;
1135 }
1136
1137 MessageParcel reply;
1138 MessageOption option;
1139 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD),
1140 data, reply, option);
1141
1142 int32_t ret = reply.ReadInt32();
1143 NETNATIVE_LOGI("End to IpfwdRemoveInterfaceForward, ret =%{public}d", ret);
1144 return ret;
1145 }
1146
BandwidthEnableDataSaver(bool enable)1147 int32_t NetsysNativeServiceProxy::BandwidthEnableDataSaver(bool enable)
1148 {
1149 MessageParcel data;
1150 if (!WriteInterfaceToken(data)) {
1151 NETNATIVE_LOGE("WriteInterfaceToken failed");
1152 return ERR_FLATTEN_OBJECT;
1153 }
1154 if (!data.WriteBool(enable)) {
1155 NETNATIVE_LOGE("WriteBool failed");
1156 return ERR_FLATTEN_OBJECT;
1157 }
1158
1159 MessageParcel reply;
1160 MessageOption option;
1161 auto remote = Remote();
1162 if (remote == nullptr) {
1163 return IPC_PROXY_NULL_INVOKER_ERR;
1164 }
1165 int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER),
1166 data, reply, option);
1167 if (error != ERR_NONE) {
1168 NETNATIVE_LOGE("proxy SendRequest failed");
1169 return ERR_FLATTEN_OBJECT;
1170 }
1171 int32_t ret = reply.ReadInt32();
1172 return ret;
1173 }
1174
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)1175 int32_t NetsysNativeServiceProxy::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
1176 {
1177 MessageParcel data;
1178 if (!WriteInterfaceToken(data)) {
1179 NETNATIVE_LOGE("WriteInterfaceToken failed");
1180 return ERR_FLATTEN_OBJECT;
1181 }
1182 if (!data.WriteString(ifName)) {
1183 NETNATIVE_LOGE("WriteString failed");
1184 return ERR_FLATTEN_OBJECT;
1185 }
1186 if (!data.WriteInt64(bytes)) {
1187 NETNATIVE_LOGE("WriteInt64 failed");
1188 return ERR_FLATTEN_OBJECT;
1189 }
1190
1191 MessageParcel reply;
1192 MessageOption option;
1193 auto remote = Remote();
1194 if (remote == nullptr) {
1195 return IPC_PROXY_NULL_INVOKER_ERR;
1196 }
1197 int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_SET_IFACE_QUOTA),
1198 data, reply, option);
1199 if (error != ERR_NONE) {
1200 NETNATIVE_LOGE("proxy SendRequest failed");
1201 return ERR_FLATTEN_OBJECT;
1202 }
1203 int32_t ret = reply.ReadInt32();
1204 return ret;
1205 }
1206
BandwidthRemoveIfaceQuota(const std::string & ifName)1207 int32_t NetsysNativeServiceProxy::BandwidthRemoveIfaceQuota(const std::string &ifName)
1208 {
1209 MessageParcel data;
1210 if (!WriteInterfaceToken(data)) {
1211 NETNATIVE_LOGE("WriteInterfaceToken failed");
1212 return ERR_FLATTEN_OBJECT;
1213 }
1214 if (!data.WriteString(ifName)) {
1215 NETNATIVE_LOGE("WriteString failed");
1216 return ERR_FLATTEN_OBJECT;
1217 }
1218
1219 MessageParcel reply;
1220 MessageOption option;
1221 auto remote = Remote();
1222 if (remote == nullptr) {
1223 return IPC_PROXY_NULL_INVOKER_ERR;
1224 }
1225 int32_t error = remote->SendRequest(
1226 static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA), data, reply, option);
1227 if (error != ERR_NONE) {
1228 NETNATIVE_LOGE("proxy SendRequest failed");
1229 return ERR_FLATTEN_OBJECT;
1230 }
1231 int32_t ret = reply.ReadInt32();
1232 return ret;
1233 }
1234
BandwidthAddDeniedList(uint32_t uid)1235 int32_t NetsysNativeServiceProxy::BandwidthAddDeniedList(uint32_t uid)
1236 {
1237 MessageParcel data;
1238 if (!WriteInterfaceToken(data)) {
1239 NETNATIVE_LOGE("WriteInterfaceToken failed");
1240 return ERR_FLATTEN_OBJECT;
1241 }
1242 if (!data.WriteUint32(uid)) {
1243 NETNATIVE_LOGE("WriteUint32 failed");
1244 return ERR_FLATTEN_OBJECT;
1245 }
1246
1247 MessageParcel reply;
1248 MessageOption option;
1249 auto remote = Remote();
1250 if (remote == nullptr) {
1251 return IPC_PROXY_NULL_INVOKER_ERR;
1252 }
1253 int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_DENIED_LIST),
1254 data, reply, option);
1255 if (error != ERR_NONE) {
1256 NETNATIVE_LOGE("proxy SendRequest failed");
1257 return ERR_FLATTEN_OBJECT;
1258 }
1259 int32_t ret = reply.ReadInt32();
1260 return ret;
1261 }
1262
BandwidthRemoveDeniedList(uint32_t uid)1263 int32_t NetsysNativeServiceProxy::BandwidthRemoveDeniedList(uint32_t uid)
1264 {
1265 MessageParcel data;
1266 if (!WriteInterfaceToken(data)) {
1267 NETNATIVE_LOGE("WriteInterfaceToken failed");
1268 return ERR_FLATTEN_OBJECT;
1269 }
1270 if (!data.WriteUint32(uid)) {
1271 NETNATIVE_LOGE("WriteUint32 failed");
1272 return ERR_FLATTEN_OBJECT;
1273 }
1274
1275 MessageParcel reply;
1276 MessageOption option;
1277 auto remote = Remote();
1278 if (remote == nullptr) {
1279 return IPC_PROXY_NULL_INVOKER_ERR;
1280 }
1281 int32_t error = remote->SendRequest(
1282 static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST), data, reply, option);
1283 if (error != ERR_NONE) {
1284 NETNATIVE_LOGE("proxy SendRequest failed");
1285 return ERR_FLATTEN_OBJECT;
1286 }
1287 int32_t ret = reply.ReadInt32();
1288 return ret;
1289 }
1290
BandwidthAddAllowedList(uint32_t uid)1291 int32_t NetsysNativeServiceProxy::BandwidthAddAllowedList(uint32_t uid)
1292 {
1293 MessageParcel data;
1294 if (!WriteInterfaceToken(data)) {
1295 NETNATIVE_LOGE("WriteInterfaceToken failed");
1296 return ERR_FLATTEN_OBJECT;
1297 }
1298 if (!data.WriteUint32(uid)) {
1299 NETNATIVE_LOGE("WriteUint32 failed");
1300 return ERR_FLATTEN_OBJECT;
1301 }
1302
1303 MessageParcel reply;
1304 MessageOption option;
1305 auto remote = Remote();
1306 if (remote == nullptr) {
1307 return IPC_PROXY_NULL_INVOKER_ERR;
1308 }
1309 int32_t error = remote->SendRequest(
1310 static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST), data, reply, option);
1311 if (error != ERR_NONE) {
1312 NETNATIVE_LOGE("proxy SendRequest failed");
1313 return ERR_FLATTEN_OBJECT;
1314 }
1315 int32_t ret = reply.ReadInt32();
1316 return ret;
1317 }
1318
BandwidthRemoveAllowedList(uint32_t uid)1319 int32_t NetsysNativeServiceProxy::BandwidthRemoveAllowedList(uint32_t uid)
1320 {
1321 MessageParcel data;
1322 if (!WriteInterfaceToken(data)) {
1323 NETNATIVE_LOGE("WriteInterfaceToken failed");
1324 return ERR_FLATTEN_OBJECT;
1325 }
1326 if (!data.WriteUint32(uid)) {
1327 NETNATIVE_LOGE("WriteUint32 failed");
1328 return ERR_FLATTEN_OBJECT;
1329 }
1330
1331 MessageParcel reply;
1332 MessageOption option;
1333 auto remote = Remote();
1334 if (remote == nullptr) {
1335 return IPC_PROXY_NULL_INVOKER_ERR;
1336 }
1337 int32_t error = remote->SendRequest(
1338 static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST), data, reply, option);
1339 if (error != ERR_NONE) {
1340 NETNATIVE_LOGE("proxy SendRequest failed");
1341 return ERR_FLATTEN_OBJECT;
1342 }
1343 int32_t ret = reply.ReadInt32();
1344 return ret;
1345 }
1346
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1347 int32_t NetsysNativeServiceProxy::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1348 {
1349 MessageParcel data;
1350 uint32_t uidSize = uids.size();
1351 if (uidSize > UIDS_LIST_MAX_SIZE) {
1352 NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1353 return ERR_INVALID_DATA;
1354 }
1355 if (!WriteInterfaceToken(data)) {
1356 NETNATIVE_LOGE("WriteInterfaceToken failed");
1357 return ERR_FLATTEN_OBJECT;
1358 }
1359 if (!data.WriteUint32(chain)) {
1360 NETNATIVE_LOGE("WriteUint32 failed");
1361 return ERR_FLATTEN_OBJECT;
1362 }
1363 if (!data.WriteUint32(uidSize)) {
1364 NETNATIVE_LOGE("WriteUint32 failed");
1365 return ERR_FLATTEN_OBJECT;
1366 }
1367 std::vector<uint32_t> vUids;
1368 vUids.assign(uids.begin(), uids.end());
1369 std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1370
1371 MessageParcel reply;
1372 MessageOption option;
1373 auto remote = Remote();
1374 if (remote == nullptr) {
1375 return IPC_PROXY_NULL_INVOKER_ERR;
1376 }
1377 int32_t error = remote->SendRequest(
1378 static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN), data, reply, option);
1379 if (error != ERR_NONE) {
1380 NETNATIVE_LOGE("proxy SendRequest failed");
1381 return ERR_FLATTEN_OBJECT;
1382 }
1383 int32_t ret = reply.ReadInt32();
1384 return ret;
1385 }
1386
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1387 int32_t NetsysNativeServiceProxy::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1388 {
1389 MessageParcel data;
1390 uint32_t uidSize = uids.size();
1391 if (uidSize > UIDS_LIST_MAX_SIZE) {
1392 NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1393 return ERR_INVALID_DATA;
1394 }
1395 if (!WriteInterfaceToken(data)) {
1396 NETNATIVE_LOGE("WriteInterfaceToken failed");
1397 return ERR_FLATTEN_OBJECT;
1398 }
1399 if (!data.WriteUint32(chain)) {
1400 NETNATIVE_LOGE("WriteUint32 Error");
1401 return ERR_FLATTEN_OBJECT;
1402 }
1403 if (!data.WriteUint32(uidSize)) {
1404 NETNATIVE_LOGE("WriteUint32 Error");
1405 return ERR_FLATTEN_OBJECT;
1406 }
1407 std::vector<uint32_t> vUids;
1408 vUids.assign(uids.begin(), uids.end());
1409 std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1410
1411 MessageParcel reply;
1412 MessageOption option;
1413 auto remote = Remote();
1414 if (remote == nullptr) {
1415 return IPC_PROXY_NULL_INVOKER_ERR;
1416 }
1417 int32_t error = remote->SendRequest(
1418 static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN), data, reply, option);
1419 if (error != ERR_NONE) {
1420 NETNATIVE_LOGE("proxy SendRequest failed");
1421 return ERR_FLATTEN_OBJECT;
1422 }
1423 int32_t ret = reply.ReadInt32();
1424 return ret;
1425 }
1426
FirewallEnableChain(uint32_t chain,bool enable)1427 int32_t NetsysNativeServiceProxy::FirewallEnableChain(uint32_t chain, bool enable)
1428 {
1429 MessageParcel data;
1430 if (!WriteInterfaceToken(data)) {
1431 NETNATIVE_LOGE("WriteInterfaceToken failed");
1432 return ERR_FLATTEN_OBJECT;
1433 }
1434 if (!data.WriteUint32(chain)) {
1435 NETNATIVE_LOGE("WriteUint32 Error");
1436 return ERR_FLATTEN_OBJECT;
1437 }
1438 if (!data.WriteBool(enable)) {
1439 NETNATIVE_LOGE("WriteBool Error");
1440 return ERR_FLATTEN_OBJECT;
1441 }
1442
1443 MessageParcel reply;
1444 MessageOption option;
1445 auto remote = Remote();
1446 if (remote == nullptr) {
1447 return IPC_PROXY_NULL_INVOKER_ERR;
1448 }
1449 int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_ENABLE_CHAIN),
1450 data, reply, option);
1451 if (error != ERR_NONE) {
1452 NETNATIVE_LOGE("proxy SendRequest failed");
1453 return ERR_FLATTEN_OBJECT;
1454 }
1455 int32_t ret = reply.ReadInt32();
1456 return ret;
1457 }
1458
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)1459 int32_t NetsysNativeServiceProxy::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
1460 uint32_t firewallRule)
1461 {
1462 MessageParcel data;
1463 if (!WriteInterfaceToken(data)) {
1464 NETNATIVE_LOGE("WriteInterfaceToken failed");
1465 return ERR_FLATTEN_OBJECT;
1466 }
1467 if (!data.WriteUint32(chain)) {
1468 NETNATIVE_LOGE("WriteUint32 failed");
1469 return ERR_FLATTEN_OBJECT;
1470 }
1471 if (!data.WriteUInt32Vector(uids)) {
1472 NETNATIVE_LOGE("WriteUint32 failed");
1473 return ERR_FLATTEN_OBJECT;
1474 }
1475 if (!data.WriteUint32(firewallRule)) {
1476 NETNATIVE_LOGE("WriteUint32 failed");
1477 return ERR_FLATTEN_OBJECT;
1478 }
1479
1480 MessageParcel reply;
1481 MessageOption option;
1482 auto remote = Remote();
1483 if (remote == nullptr) {
1484 return IPC_PROXY_NULL_INVOKER_ERR;
1485 }
1486 int32_t error = remote->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_RULE),
1487 data, reply, option);
1488 if (error != ERR_NONE) {
1489 NETNATIVE_LOGE("proxy SendRequest failed");
1490 return ERR_FLATTEN_OBJECT;
1491 }
1492 int32_t ret = reply.ReadInt32();
1493 return ret;
1494 }
1495
ShareDnsSet(uint16_t netId)1496 int32_t NetsysNativeServiceProxy::ShareDnsSet(uint16_t netId)
1497 {
1498 MessageParcel data;
1499 if (!WriteInterfaceToken(data)) {
1500 return ERR_FLATTEN_OBJECT;
1501 }
1502 if (!data.WriteUint16(netId)) {
1503 return ERR_FLATTEN_OBJECT;
1504 }
1505
1506 MessageParcel reply;
1507 MessageOption option;
1508 int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_TETHER_DNS_SET),
1509 data, reply, option);
1510 if (error != ERR_NONE) {
1511 NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1512 return error;
1513 }
1514 return reply.ReadInt32();
1515 }
1516
StartDnsProxyListen()1517 int32_t NetsysNativeServiceProxy::StartDnsProxyListen()
1518 {
1519 MessageParcel data;
1520 if (!WriteInterfaceToken(data)) {
1521 return ERR_FLATTEN_OBJECT;
1522 }
1523
1524 MessageParcel reply;
1525 MessageOption option;
1526 int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DNS_PROXY_LISTEN),
1527 data, reply, option);
1528 if (error != ERR_NONE) {
1529 NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1530 return error;
1531 }
1532 return reply.ReadInt32();
1533 }
1534
StopDnsProxyListen()1535 int32_t NetsysNativeServiceProxy::StopDnsProxyListen()
1536 {
1537 MessageParcel data;
1538 if (!WriteInterfaceToken(data)) {
1539 return ERR_FLATTEN_OBJECT;
1540 }
1541
1542 MessageParcel reply;
1543 MessageOption option;
1544 int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DNS_PROXY_LISTEN),
1545 data, reply, option);
1546 if (error != ERR_NONE) {
1547 NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1548 return error;
1549 }
1550 return reply.ReadInt32();
1551 }
1552
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,NetworkSharingTraffic & traffic)1553 int32_t NetsysNativeServiceProxy::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
1554 NetworkSharingTraffic &traffic)
1555 {
1556 MessageParcel data;
1557 if (!WriteInterfaceToken(data)) {
1558 return ERR_FLATTEN_OBJECT;
1559 }
1560 if (!data.WriteString(downIface)) {
1561 return ERR_FLATTEN_OBJECT;
1562 }
1563 if (!data.WriteString(upIface)) {
1564 return ERR_FLATTEN_OBJECT;
1565 }
1566
1567 MessageParcel reply;
1568 MessageOption option;
1569 Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_SHARING_NETWORK_TRAFFIC),
1570 data, reply, option);
1571
1572 int32_t ret = reply.ReadInt32();
1573 if (ret != ERR_NONE) {
1574 NETNATIVE_LOGE("Fail to GetNetworkSharingTraffic ret= %{public}d", ret);
1575 return ret;
1576 }
1577
1578 traffic.receive = reply.ReadInt64();
1579 traffic.send = reply.ReadInt64();
1580 traffic.all = reply.ReadInt64();
1581 NETNATIVE_LOGI("NetsysNativeServiceProxy GetNetworkSharingTraffic ret=%{public}d", ret);
1582 return ret;
1583 }
1584
GetTotalStats(uint64_t & stats,uint32_t type)1585 int32_t NetsysNativeServiceProxy::GetTotalStats(uint64_t &stats, uint32_t type)
1586 {
1587 MessageParcel data;
1588 if (!WriteInterfaceToken(data)) {
1589 return ERR_FLATTEN_OBJECT;
1590 }
1591 if (!data.WriteUint8(type)) {
1592 return ERR_FLATTEN_OBJECT;
1593 }
1594 MessageParcel reply;
1595 MessageOption option;
1596 if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_TOTAL_STATS),
1597 data, reply, option)) {
1598 NETNATIVE_LOGE("proxy SendRequest failed");
1599 return ERR_FLATTEN_OBJECT;
1600 }
1601
1602 int32_t ret;
1603 if (!reply.ReadInt32(ret)) {
1604 NETNATIVE_LOGE("get ret falil");
1605 return ERR_FLATTEN_OBJECT;
1606 }
1607 if (ret != ERR_NONE) {
1608 NETNATIVE_LOGE("fail to GetTotalStats ret= %{public}d", ret);
1609 return ret;
1610 }
1611 if (!reply.ReadUint64(stats)) {
1612 NETNATIVE_LOGE("get stats falil");
1613 return ERR_FLATTEN_OBJECT;
1614 }
1615
1616 return ERR_NONE;
1617 }
1618
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)1619 int32_t NetsysNativeServiceProxy::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
1620 {
1621 MessageParcel data;
1622 if (!WriteInterfaceToken(data)) {
1623 return ERR_FLATTEN_OBJECT;
1624 }
1625 if (!data.WriteUint32(type)) {
1626 return ERR_FLATTEN_OBJECT;
1627 }
1628 if (!data.WriteUint32(uid)) {
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_UID_STATS),
1634 data, 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 GetUidStats ret= %{public}d", ret);
1646 return ret;
1647 }
1648 if (!reply.ReadUint64(stats)) {
1649 NETNATIVE_LOGE("get stats falil");
1650 return ERR_FLATTEN_OBJECT;
1651 }
1652 return ERR_NONE;
1653 }
1654
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)1655 int32_t NetsysNativeServiceProxy::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
1656 {
1657 MessageParcel data;
1658 if (!WriteInterfaceToken(data)) {
1659 return ERR_FLATTEN_OBJECT;
1660 }
1661 if (!data.WriteUint32(type)) {
1662 return ERR_FLATTEN_OBJECT;
1663 }
1664 if (!data.WriteString(interfaceName)) {
1665 return ERR_FLATTEN_OBJECT;
1666 }
1667 MessageParcel reply;
1668 MessageOption option;
1669 if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_IFACE_STATS),
1670 data, reply, option)) {
1671 NETNATIVE_LOGE("proxy SendRequest failed");
1672 return ERR_FLATTEN_OBJECT;
1673 }
1674
1675 int32_t ret;
1676 if (!reply.ReadInt32(ret)) {
1677 NETNATIVE_LOGE("get ret falil");
1678 return ERR_FLATTEN_OBJECT;
1679 }
1680 if (ret != ERR_NONE) {
1681 NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1682 return ret;
1683 }
1684 if (!reply.ReadUint64(stats)) {
1685 NETNATIVE_LOGE("get stats falil");
1686 return ERR_FLATTEN_OBJECT;
1687 }
1688 return ERR_NONE;
1689 }
1690
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)1691 int32_t NetsysNativeServiceProxy::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
1692 {
1693 MessageParcel data;
1694 if (!WriteInterfaceToken(data)) {
1695 return ERR_FLATTEN_OBJECT;
1696 }
1697 MessageParcel reply;
1698 MessageOption option;
1699 if (ERR_NONE != Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_STATS_INFO),
1700 data, reply, option)) {
1701 NETNATIVE_LOGE("proxy SendRequest failed");
1702 return ERR_FLATTEN_OBJECT;
1703 }
1704
1705 int32_t ret;
1706 if (!reply.ReadInt32(ret)) {
1707 NETNATIVE_LOGE("get ret falil");
1708 return ERR_FLATTEN_OBJECT;
1709 }
1710 if (ret != ERR_NONE) {
1711 NETNATIVE_LOGE("fail to GetIfaceStats ret= %{public}d", ret);
1712 return ret;
1713 }
1714 if (!OHOS::NetManagerStandard::NetStatsInfo::Unmarshalling(reply, stats)) {
1715 NETNATIVE_LOGE("Read stats info failed");
1716 return ERR_FLATTEN_OBJECT;
1717 }
1718
1719 return ERR_NONE;
1720 }
1721
SetIptablesCommandForRes(const std::string & cmd,std::string & respond)1722 int32_t NetsysNativeServiceProxy::SetIptablesCommandForRes(const std::string &cmd, std::string &respond)
1723 {
1724 MessageParcel data;
1725 if (!WriteInterfaceToken(data)) {
1726 return ERR_FLATTEN_OBJECT;
1727 }
1728 if (!data.WriteString(cmd)) {
1729 return ERR_FLATTEN_OBJECT;
1730 }
1731 MessageParcel reply;
1732 MessageOption option;
1733 if (Remote() == nullptr) {
1734 NETNATIVE_LOGE("SetIptablesCommandForRes Remote pointer is null");
1735 return ERR_FLATTEN_OBJECT;
1736 }
1737 int32_t error = Remote()->SendRequest(static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES),
1738 data, reply, option);
1739 if (error != ERR_NONE) {
1740 NETNATIVE_LOGE("SetIptablesCommandForRes proxy SendRequest failed");
1741 return ERR_FLATTEN_OBJECT;
1742 }
1743 int32_t ret;
1744 if (!reply.ReadInt32(ret)) {
1745 NETNATIVE_LOGE("SetIptablesCommandForRes proxy read ret failed");
1746 return ERR_FLATTEN_OBJECT;
1747 }
1748 if (!reply.ReadString(respond)) {
1749 NETNATIVE_LOGE("SetIptablesCommandForRes proxy read respond failed");
1750 return ERR_FLATTEN_OBJECT;
1751 }
1752 return ret;
1753 }
1754 } // namespace NetsysNative
1755 } // namespace OHOS
1756