1 /*
2 * Copyright (c) 2021-2022 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: InterfaceSetConfig 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(INetsysService::NETSYS_SET_RESOLVER_CONFIG, data, reply, option);
102
103 return reply.ReadInt32();
104 }
105
GetResolverConfig(const uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)106 int32_t NetsysNativeServiceProxy::GetResolverConfig(const 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(INetsysService::NETSYS_GET_RESOLVER_CONFIG, data, reply, option);
120 int result = reply.ReadInt32();
121 reply.ReadUint16(baseTimeoutMsec);
122 reply.ReadUint8(retryCount);
123 int32_t vServerSize = reply.ReadInt32();
124 vServerSize = vServerSize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vServerSize;
125 std::vector<std::string> vecString;
126 for (int i = 0; i < vServerSize; i++) {
127 vecString.push_back(reply.ReadString());
128 }
129 if (vServerSize > 0) {
130 servers.assign(vecString.begin(), vecString.end());
131 }
132 int32_t vDomainSize = reply.ReadInt32();
133 vDomainSize = vDomainSize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vDomainSize;
134 std::vector<std::string> vecDomain;
135 for (int i = 0; i < vDomainSize; i++) {
136 vecDomain.push_back(reply.ReadString());
137 }
138 if (vDomainSize > 0) {
139 domains.assign(vecDomain.begin(), vecDomain.end());
140 }
141 NETNATIVE_LOGI("Begin to GetResolverConfig %{public}d", result);
142 return result;
143 }
144
CreateNetworkCache(const uint16_t netId)145 int32_t NetsysNativeServiceProxy::CreateNetworkCache(const uint16_t netId)
146 {
147 NETNATIVE_LOGI("Begin to CreateNetworkCache");
148 MessageParcel data;
149 if (!WriteInterfaceToken(data)) {
150 return ERR_FLATTEN_OBJECT;
151 }
152 if (!data.WriteUint16(netId)) {
153 return ERR_FLATTEN_OBJECT;
154 }
155
156 MessageParcel reply;
157 MessageOption option;
158 Remote()->SendRequest(INetsysService::NETSYS_CREATE_NETWORK_CACHE, data, reply, option);
159
160 return reply.ReadInt32();
161 }
162
DestroyNetworkCache(const uint16_t netId)163 int32_t NetsysNativeServiceProxy::DestroyNetworkCache(const uint16_t netId)
164 {
165 NETNATIVE_LOGI("Begin to DestroyNetworkCache");
166 MessageParcel data;
167 if (!WriteInterfaceToken(data)) {
168 return ERR_FLATTEN_OBJECT;
169 }
170 if (!data.WriteUint16(netId)) {
171 return ERR_FLATTEN_OBJECT;
172 }
173
174 MessageParcel reply;
175 MessageOption option;
176 Remote()->SendRequest(INetsysService::NETSYS_DESTROY_NETWORK_CACHE, data, reply, option);
177
178 return reply.ReadInt32();
179 }
180
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)181 int32_t NetsysNativeServiceProxy::GetAddrInfo(const std::string &hostName, const std::string &serverName,
182 const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
183 {
184 MessageParcel data;
185 if (!WriteInterfaceToken(data) || !data.WriteString(hostName) || !data.WriteString(serverName) ||
186 !data.WriteRawData(&hints, sizeof(AddrInfo)) || !data.WriteUint16(netId)) {
187 return ERR_FLATTEN_OBJECT;
188 }
189
190 MessageParcel reply;
191 MessageOption option;
192 Remote()->SendRequest(INetsysService::NETSYS_GET_ADDR_INFO, data, reply, option);
193
194 int32_t ret;
195 uint32_t addrSize;
196 if (!reply.ReadInt32(ret) || ret != ERR_NONE || !reply.ReadUint32(addrSize) || addrSize > MAX_RESULTS) {
197 return ERR_INVALID_DATA;
198 }
199
200 std::vector<AddrInfo> infos;
201 for (uint32_t i = 0; i < addrSize; ++i) {
202 auto p = reply.ReadRawData(sizeof(AddrInfo));
203 if (p == nullptr) {
204 return ERR_INVALID_DATA;
205 }
206
207 AddrInfo info = {};
208 if (memcpy_s(&info, sizeof(AddrInfo), p, sizeof(AddrInfo)) != EOK) {
209 return ERR_INVALID_DATA;
210 }
211
212 infos.emplace_back(info);
213 }
214
215 res = infos;
216 return ret;
217 }
218
InterfaceSetMtu(const std::string & interfaceName,int32_t mtu)219 int32_t NetsysNativeServiceProxy::InterfaceSetMtu(const std::string &interfaceName, int32_t mtu)
220 {
221 NETNATIVE_LOGI("Begin to InterfaceSetMtu");
222 MessageParcel data;
223 if (!WriteInterfaceToken(data)) {
224 return ERR_FLATTEN_OBJECT;
225 }
226 if (!data.WriteString(interfaceName)) {
227 return ERR_FLATTEN_OBJECT;
228 }
229 if (!data.WriteInt32(mtu)) {
230 return ERR_FLATTEN_OBJECT;
231 }
232
233 MessageParcel reply;
234 MessageOption option;
235 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_SET_MTU, data, reply, option);
236
237 return reply.ReadInt32();
238 }
239
InterfaceGetMtu(const std::string & interfaceName)240 int32_t NetsysNativeServiceProxy::InterfaceGetMtu(const std::string &interfaceName)
241 {
242 NETNATIVE_LOGI("Begin to InterfaceGetMtu");
243 MessageParcel data;
244 if (!WriteInterfaceToken(data)) {
245 return ERR_FLATTEN_OBJECT;
246 }
247 if (!data.WriteString(interfaceName)) {
248 return ERR_FLATTEN_OBJECT;
249 }
250
251 MessageParcel reply;
252 MessageOption option;
253 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_GET_MTU, data, reply, option);
254
255 return reply.ReadInt32();
256 }
257
RegisterNotifyCallback(sptr<INotifyCallback> & callback)258 int32_t NetsysNativeServiceProxy::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
259 {
260 NETNATIVE_LOGI("Begin to RegisterNotifyCallback");
261 MessageParcel data;
262 if (callback == nullptr) {
263 NETNATIVE_LOGE("The parameter of callback is nullptr");
264 return ERR_NULL_OBJECT;
265 }
266
267 if (!WriteInterfaceToken(data)) {
268 return ERR_FLATTEN_OBJECT;
269 }
270 data.WriteRemoteObject(callback->AsObject().GetRefPtr());
271
272 MessageParcel reply;
273 MessageOption option;
274 Remote()->SendRequest(INetsysService::NETSYS_REGISTER_NOTIFY_CALLBACK, data, reply, option);
275
276 return reply.ReadInt32();
277 }
278
UnRegisterNotifyCallback(sptr<INotifyCallback> & callback)279 int32_t NetsysNativeServiceProxy::UnRegisterNotifyCallback(sptr<INotifyCallback> &callback)
280 {
281 NETNATIVE_LOGI("Begin to UnRegisterNotifyCallback");
282 MessageParcel data;
283 if (callback == nullptr) {
284 NETNATIVE_LOGE("The parameter of callback is nullptr");
285 return ERR_NULL_OBJECT;
286 }
287
288 if (!WriteInterfaceToken(data)) {
289 NETNATIVE_LOGE("WriteInterfaceToken fail");
290 return ERR_FLATTEN_OBJECT;
291 }
292
293 data.WriteRemoteObject(callback->AsObject().GetRefPtr());
294
295 MessageParcel reply;
296 MessageOption option;
297 Remote()->SendRequest(INetsysService::NETSYS_UNREGISTER_NOTIFY_CALLBACK, data, reply, option);
298
299 return reply.ReadInt32();
300 }
301
NetworkAddRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)302 int32_t NetsysNativeServiceProxy::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
303 const std::string &destination, const std::string &nextHop)
304 {
305 NETNATIVE_LOGI("Begin to NetworkAddRoute");
306 MessageParcel data;
307 if (!WriteInterfaceToken(data)) {
308 return ERR_FLATTEN_OBJECT;
309 }
310 if (!data.WriteInt32(netId)) {
311 return ERR_FLATTEN_OBJECT;
312 }
313 if (!data.WriteString(interfaceName)) {
314 return ERR_FLATTEN_OBJECT;
315 }
316 if (!data.WriteString(destination)) {
317 return ERR_FLATTEN_OBJECT;
318 }
319 if (!data.WriteString(nextHop)) {
320 return ERR_FLATTEN_OBJECT;
321 }
322
323 MessageParcel reply;
324 MessageOption option;
325 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_ADD_ROUTE, data, reply, option);
326
327 return reply.ReadInt32();
328 }
329
NetworkRemoveRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)330 int32_t NetsysNativeServiceProxy::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
331 const std::string &destination, const std::string &nextHop)
332 {
333 NETNATIVE_LOGI("Begin to NetworkRemoveRoute");
334 MessageParcel data;
335 if (!WriteInterfaceToken(data)) {
336 return ERR_FLATTEN_OBJECT;
337 }
338 if (!data.WriteInt32(netId)) {
339 return ERR_FLATTEN_OBJECT;
340 }
341 if (!data.WriteString(interfaceName)) {
342 return ERR_FLATTEN_OBJECT;
343 }
344 if (!data.WriteString(destination)) {
345 return ERR_FLATTEN_OBJECT;
346 }
347 if (!data.WriteString(nextHop)) {
348 return ERR_FLATTEN_OBJECT;
349 }
350
351 MessageParcel reply;
352 MessageOption option;
353 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_REMOVE_ROUTE, data, reply, option);
354
355 return reply.ReadInt32();
356 }
357
NetworkAddRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)358 int32_t NetsysNativeServiceProxy::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
359 {
360 NETNATIVE_LOGI("Begin to NetworkAddRouteParcel");
361 MessageParcel data;
362 if (!WriteInterfaceToken(data)) {
363 return ERR_FLATTEN_OBJECT;
364 }
365 if (!data.WriteInt32(netId)) {
366 return ERR_FLATTEN_OBJECT;
367 }
368 if (!data.WriteString(routeInfo.ifName)) {
369 return ERR_FLATTEN_OBJECT;
370 }
371 if (!data.WriteString(routeInfo.destination)) {
372 return ERR_FLATTEN_OBJECT;
373 }
374 if (!data.WriteString(routeInfo.nextHop)) {
375 return ERR_FLATTEN_OBJECT;
376 }
377
378 MessageParcel reply;
379 MessageOption option;
380 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_ADD_ROUTE_PARCEL, data, reply, option);
381
382 return reply.ReadInt32();
383 }
384
NetworkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)385 int32_t NetsysNativeServiceProxy::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
386 {
387 NETNATIVE_LOGI("Begin to NetworkRemoveRouteParcel");
388 MessageParcel data;
389 if (!WriteInterfaceToken(data)) {
390 return ERR_FLATTEN_OBJECT;
391 }
392 if (!data.WriteInt32(netId)) {
393 return ERR_FLATTEN_OBJECT;
394 }
395 if (!data.WriteString(routeInfo.ifName)) {
396 return ERR_FLATTEN_OBJECT;
397 }
398 if (!data.WriteString(routeInfo.destination)) {
399 return ERR_FLATTEN_OBJECT;
400 }
401 if (!data.WriteString(routeInfo.nextHop)) {
402 return ERR_FLATTEN_OBJECT;
403 }
404
405 MessageParcel reply;
406 MessageOption option;
407 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_REMOVE_ROUTE_PARCEL, data, reply, option);
408
409 return reply.ReadInt32();
410 }
411
NetworkSetDefault(int32_t netId)412 int32_t NetsysNativeServiceProxy::NetworkSetDefault(int32_t netId)
413 {
414 NETNATIVE_LOGI("Begin to NetworkSetDefault");
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
423 MessageParcel reply;
424 MessageOption option;
425 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_SET_DEFAULT, data, reply, option);
426
427 return reply.ReadInt32();
428 }
429
NetworkGetDefault()430 int32_t NetsysNativeServiceProxy::NetworkGetDefault()
431 {
432 NETNATIVE_LOGI("Begin to NetworkGetDefault");
433 MessageParcel data;
434 if (!WriteInterfaceToken(data)) {
435 return ERR_FLATTEN_OBJECT;
436 }
437
438 MessageParcel reply;
439 MessageOption option;
440 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_GET_DEFAULT, data, reply, option);
441
442 return reply.ReadInt32();
443 }
444
NetworkClearDefault()445 int32_t NetsysNativeServiceProxy::NetworkClearDefault()
446 {
447 NETNATIVE_LOGI("Begin to NetworkClearDefault");
448 MessageParcel data;
449 if (!WriteInterfaceToken(data)) {
450 return ERR_FLATTEN_OBJECT;
451 }
452
453 MessageParcel reply;
454 MessageOption option;
455 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_CLEAR_DEFAULT, data, reply, option);
456
457 return reply.ReadInt32();
458 }
459
GetProcSysNet(int32_t ipversion,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)460 int32_t NetsysNativeServiceProxy::GetProcSysNet(int32_t ipversion, int32_t which, const std::string &ifname,
461 const std::string ¶meter, std::string &value)
462 {
463 NETNATIVE_LOGI("Begin to GetSysProcNet");
464 MessageParcel data;
465 if (!WriteInterfaceToken(data)) {
466 return ERR_FLATTEN_OBJECT;
467 }
468
469 if (!data.WriteInt32(ipversion)) {
470 return ERR_FLATTEN_OBJECT;
471 }
472 if (!data.WriteInt32(which)) {
473 return ERR_FLATTEN_OBJECT;
474 }
475 if (data.WriteString(ifname)) {
476 return ERR_FLATTEN_OBJECT;
477 }
478 if (!data.WriteString(parameter)) {
479 return ERR_FLATTEN_OBJECT;
480 }
481 MessageParcel reply;
482 MessageOption option;
483 Remote()->SendRequest(INetsysService::NETSYS_GET_PROC_SYS_NET, data, reply, option);
484 int32_t ret = reply.ReadInt32();
485 std::string valueRsl = reply.ReadString();
486 NETNATIVE_LOGE("NETSYS_GET_PROC_SYS_NET value %{public}s", valueRsl.c_str());
487 value = valueRsl;
488 return ret;
489 }
490
SetProcSysNet(int32_t ipversion,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)491 int32_t NetsysNativeServiceProxy::SetProcSysNet(int32_t ipversion, int32_t which, const std::string &ifname,
492 const std::string ¶meter, std::string &value)
493 {
494 NETNATIVE_LOGI("Begin to SetSysProcNet");
495 MessageParcel data;
496 if (!WriteInterfaceToken(data)) {
497 return ERR_FLATTEN_OBJECT;
498 }
499 if (!data.WriteInt32(ipversion)) {
500 return ERR_FLATTEN_OBJECT;
501 }
502 if (!data.WriteInt32(which)) {
503 return ERR_FLATTEN_OBJECT;
504 }
505 if (data.WriteString(ifname)) {
506 return ERR_FLATTEN_OBJECT;
507 }
508 if (!data.WriteString(parameter)) {
509 return ERR_FLATTEN_OBJECT;
510 }
511 if (!data.WriteString(value)) {
512 return ERR_FLATTEN_OBJECT;
513 }
514 MessageParcel reply;
515 MessageOption option;
516 Remote()->SendRequest(INetsysService::NETSYS_SET_PROC_SYS_NET, data, reply, option);
517
518 return reply.ReadInt32();
519 }
520
NetworkCreatePhysical(int32_t netId,int32_t permission)521 int32_t NetsysNativeServiceProxy::NetworkCreatePhysical(int32_t netId, int32_t permission)
522 {
523 NETNATIVE_LOGI("Begin to NetworkCreatePhysical");
524 MessageParcel data;
525 if (!WriteInterfaceToken(data)) {
526 return ERR_FLATTEN_OBJECT;
527 }
528 if (!data.WriteInt32(netId)) {
529 return ERR_FLATTEN_OBJECT;
530 }
531 if (!data.WriteInt32(permission)) {
532 return ERR_FLATTEN_OBJECT;
533 }
534
535 MessageParcel reply;
536 MessageOption option;
537 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_CREATE_PHYSICAL, data, reply, option);
538
539 return reply.ReadInt32();
540 }
541
InterfaceAddAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)542 int32_t NetsysNativeServiceProxy::InterfaceAddAddress(const std::string &interfaceName, const std::string &addrString,
543 int32_t prefixLength)
544 {
545 NETNATIVE_LOGI("Begin to InterfaceAddAddress");
546 MessageParcel data;
547 if (!WriteInterfaceToken(data)) {
548 return ERR_FLATTEN_OBJECT;
549 }
550 if (!data.WriteString(interfaceName)) {
551 return ERR_FLATTEN_OBJECT;
552 }
553 if (!data.WriteString(addrString)) {
554 return ERR_FLATTEN_OBJECT;
555 }
556 if (!data.WriteInt32(prefixLength)) {
557 return ERR_FLATTEN_OBJECT;
558 }
559
560 MessageParcel reply;
561 MessageOption option;
562 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_ADD_ADDRESS, data, reply, option);
563
564 return reply.ReadInt32();
565 }
566
InterfaceDelAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)567 int32_t NetsysNativeServiceProxy::InterfaceDelAddress(const std::string &interfaceName, const std::string &addrString,
568 int32_t prefixLength)
569 {
570 NETNATIVE_LOGI("Begin to InterfaceDelAddress");
571 MessageParcel data;
572 if (!WriteInterfaceToken(data)) {
573 return ERR_FLATTEN_OBJECT;
574 }
575 if (!data.WriteString(interfaceName)) {
576 return ERR_FLATTEN_OBJECT;
577 }
578 if (!data.WriteString(addrString)) {
579 return ERR_FLATTEN_OBJECT;
580 }
581 if (!data.WriteInt32(prefixLength)) {
582 return ERR_FLATTEN_OBJECT;
583 }
584
585 MessageParcel reply;
586 MessageOption option;
587 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_DEL_ADDRESS, data, reply, option);
588
589 return reply.ReadInt32();
590 }
591
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)592 int32_t NetsysNativeServiceProxy::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
593 {
594 NETNATIVE_LOGI("Begin to InterfaceSetIpAddress");
595 MessageParcel data;
596 if (!WriteInterfaceToken(data)) {
597 return ERR_FLATTEN_OBJECT;
598 }
599 if (!data.WriteString(ifaceName) || !data.WriteString(ipAddress)) {
600 return ERR_FLATTEN_OBJECT;
601 }
602 MessageParcel reply;
603 MessageOption option;
604 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_SET_IP_ADDRESS, data, reply, option);
605
606 return reply.ReadInt32();
607 }
608
InterfaceSetIffUp(const std::string & ifaceName)609 int32_t NetsysNativeServiceProxy::InterfaceSetIffUp(const std::string &ifaceName)
610 {
611 NETNATIVE_LOGI("Begin to InterfaceSetIffUp");
612 MessageParcel data;
613 if (!WriteInterfaceToken(data)) {
614 return ERR_FLATTEN_OBJECT;
615 }
616 if (!data.WriteString(ifaceName)) {
617 return ERR_FLATTEN_OBJECT;
618 }
619 MessageParcel reply;
620 MessageOption option;
621 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_SET_IFF_UP, data, reply, option);
622
623 return reply.ReadInt32();
624 }
625
NetworkAddInterface(int32_t netId,const std::string & iface)626 int32_t NetsysNativeServiceProxy::NetworkAddInterface(int32_t netId, const std::string &iface)
627 {
628 NETNATIVE_LOGI("Begin to NetworkAddInterface");
629 MessageParcel data;
630 if (!WriteInterfaceToken(data)) {
631 return ERR_FLATTEN_OBJECT;
632 }
633 if (!data.WriteInt32(netId)) {
634 return ERR_FLATTEN_OBJECT;
635 }
636 if (!data.WriteString(iface)) {
637 return ERR_FLATTEN_OBJECT;
638 }
639
640 MessageParcel reply;
641 MessageOption option;
642 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_ADD_INTERFACE, data, reply, option);
643
644 return reply.ReadInt32();
645 }
646
NetworkRemoveInterface(int32_t netId,const std::string & iface)647 int32_t NetsysNativeServiceProxy::NetworkRemoveInterface(int32_t netId, const std::string &iface)
648 {
649 NETNATIVE_LOGI("Begin to NetworkRemoveInterface");
650 MessageParcel data;
651 if (!WriteInterfaceToken(data)) {
652 return ERR_FLATTEN_OBJECT;
653 }
654 if (!data.WriteInt32(netId)) {
655 return ERR_FLATTEN_OBJECT;
656 }
657 if (!data.WriteString(iface)) {
658 return ERR_FLATTEN_OBJECT;
659 }
660
661 MessageParcel reply;
662 MessageOption option;
663 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_REMOVE_INTERFACE, data, reply, option);
664
665 return reply.ReadInt32();
666 }
667
NetworkDestroy(int32_t netId)668 int32_t NetsysNativeServiceProxy::NetworkDestroy(int32_t netId)
669 {
670 NETNATIVE_LOGI("Begin to NetworkDestroy");
671 MessageParcel data;
672 if (!WriteInterfaceToken(data)) {
673 return ERR_FLATTEN_OBJECT;
674 }
675 if (!data.WriteInt32(netId)) {
676 return ERR_FLATTEN_OBJECT;
677 }
678
679 MessageParcel reply;
680 MessageOption option;
681 Remote()->SendRequest(INetsysService::NETSYS_NETWORK_DESTROY, data, reply, option);
682
683 return reply.ReadInt32();
684 }
685
GetFwmarkForNetwork(int32_t netId,MarkMaskParcel & markMaskParcel)686 int32_t NetsysNativeServiceProxy::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
687 {
688 NETNATIVE_LOGI("Begin to GetFwmarkForNetwork");
689 MessageParcel data;
690 if (!WriteInterfaceToken(data)) {
691 return ERR_FLATTEN_OBJECT;
692 }
693 if (!data.WriteInt32(netId)) {
694 return ERR_FLATTEN_OBJECT;
695 }
696 if (!data.WriteInt32(markMaskParcel.mark)) {
697 return ERR_FLATTEN_OBJECT;
698 }
699 if (!data.WriteInt32(markMaskParcel.mask)) {
700 return ERR_FLATTEN_OBJECT;
701 }
702
703 MessageParcel reply;
704 MessageOption option;
705 Remote()->SendRequest(INetsysService::NETSYS_GET_FWMARK_FOR_NETWORK, data, reply, option);
706
707 return reply.ReadInt32();
708 }
709
InterfaceSetConfig(const InterfaceConfigurationParcel & cfg)710 int32_t NetsysNativeServiceProxy::InterfaceSetConfig(const InterfaceConfigurationParcel &cfg)
711 {
712 NETNATIVE_LOGI("Begin to InterfaceSetConfig");
713 MessageParcel data;
714 if (!WriteInterfaceToken(data)) {
715 return ERR_FLATTEN_OBJECT;
716 }
717 if (!data.WriteString(cfg.ifName)) {
718 return ERR_FLATTEN_OBJECT;
719 }
720 if (!data.WriteString(cfg.hwAddr)) {
721 return ERR_FLATTEN_OBJECT;
722 }
723 if (!data.WriteString(cfg.ipv4Addr)) {
724 return ERR_FLATTEN_OBJECT;
725 }
726 if (!data.WriteInt32(cfg.prefixLength)) {
727 return ERR_FLATTEN_OBJECT;
728 }
729 int32_t vsize = static_cast<int32_t>(cfg.flags.size());
730 if (!data.WriteInt32(vsize)) {
731 return ERR_FLATTEN_OBJECT;
732 }
733 std::vector<std::string> vCflags;
734 vCflags.assign(cfg.flags.begin(), cfg.flags.end());
735 NETNATIVE_LOGI("PROXY: InterfaceSetConfig Write flags String_SIZE: %{public}d",
736 static_cast<int32_t>(vCflags.size()));
737 for (std::vector<std::string>::iterator it = vCflags.begin(); it != vCflags.end(); ++it) {
738 data.WriteString(*it);
739 }
740 MessageParcel reply;
741 MessageOption option;
742 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_SET_CONFIG, data, reply, option);
743
744 return reply.ReadInt32();
745 }
746
InterfaceGetConfig(InterfaceConfigurationParcel & cfg)747 int32_t NetsysNativeServiceProxy::InterfaceGetConfig(InterfaceConfigurationParcel &cfg)
748 {
749 NETNATIVE_LOGI("Begin to InterfaceGetConfig");
750 MessageParcel data;
751 int32_t ret;
752 int32_t vSize;
753 if (!WriteInterfaceToken(data)) {
754 return ERR_FLATTEN_OBJECT;
755 }
756 if (!data.WriteString(cfg.ifName)) {
757 return ERR_FLATTEN_OBJECT;
758 }
759
760 MessageParcel reply;
761 MessageOption option;
762 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_GET_CONFIG, data, reply, option);
763 ret = reply.ReadInt32();
764 reply.ReadString(cfg.ifName);
765 reply.ReadString(cfg.hwAddr);
766 reply.ReadString(cfg.ipv4Addr);
767 reply.ReadInt32(cfg.prefixLength);
768 vSize = reply.ReadInt32();
769 vSize = vSize > MAX_INTERFACE_CONFIG_SIZE ? MAX_INTERFACE_CONFIG_SIZE : vSize;
770 std::vector<std::string> vecString;
771 for (int i = 0; i < vSize; i++) {
772 vecString.push_back(reply.ReadString());
773 }
774 if (vSize > 0) {
775 cfg.flags.assign(vecString.begin(), vecString.end());
776 }
777 NETNATIVE_LOGI("End to InterfaceGetConfig, ret =%{public}d", ret);
778 return ret;
779 }
780
InterfaceGetList(std::vector<std::string> & ifaces)781 int32_t NetsysNativeServiceProxy::InterfaceGetList(std::vector<std::string> &ifaces)
782 {
783 NETNATIVE_LOGI("NetsysNativeServiceProxy Begin to InterfaceGetList");
784 MessageParcel data;
785 int32_t ret;
786 int32_t vSize;
787 if (!WriteInterfaceToken(data)) {
788 return ERR_FLATTEN_OBJECT;
789 }
790 MessageParcel reply;
791 MessageOption option;
792 Remote()->SendRequest(INetsysService::NETSYS_INTERFACE_GET_LIST, data, reply, option);
793 ret = reply.ReadInt32();
794 vSize = reply.ReadInt32();
795 std::vector<std::string> vecString;
796 for (int i = 0; i < vSize; i++) {
797 vecString.push_back(reply.ReadString());
798 }
799 if (vSize > 0) {
800 ifaces.assign(vecString.begin(), vecString.end());
801 }
802 NETNATIVE_LOGI("NetsysNativeServiceProxy End to InterfaceGetList, ret =%{public}d", ret);
803 return ret;
804 }
805
StartDhcpClient(const std::string & iface,bool bIpv6)806 int32_t NetsysNativeServiceProxy::StartDhcpClient(const std::string &iface, bool bIpv6)
807 {
808 NETNATIVE_LOGI("Begin to StartDhcpClient");
809 MessageParcel data;
810 int32_t ret;
811 if (!WriteInterfaceToken(data)) {
812 return ERR_FLATTEN_OBJECT;
813 }
814 if (!data.WriteString(iface)) {
815 return ERR_FLATTEN_OBJECT;
816 }
817 if (!data.WriteBool(bIpv6)) {
818 return ERR_FLATTEN_OBJECT;
819 }
820
821 MessageParcel reply;
822 MessageOption option;
823 Remote()->SendRequest(INetsysService::NETSYS_START_DHCP_CLIENT, data, reply, option);
824
825 ret = reply.ReadInt32();
826 NETNATIVE_LOGI("End to StartDhcpClient, ret =%{public}d", ret);
827 return ret;
828 }
829
StopDhcpClient(const std::string & iface,bool bIpv6)830 int32_t NetsysNativeServiceProxy::StopDhcpClient(const std::string &iface, bool bIpv6)
831 {
832 NETNATIVE_LOGI("Begin to StopDhcpClient");
833 MessageParcel data;
834 int32_t ret;
835 if (!WriteInterfaceToken(data)) {
836 return ERR_FLATTEN_OBJECT;
837 }
838 if (!data.WriteString(iface)) {
839 return ERR_FLATTEN_OBJECT;
840 }
841 if (!data.WriteBool(bIpv6)) {
842 return ERR_FLATTEN_OBJECT;
843 }
844
845 MessageParcel reply;
846 MessageOption option;
847 ret = Remote()->SendRequest(INetsysService::NETSYS_STOP_DHCP_CLIENT, data, reply, option);
848 NETNATIVE_LOGI("SendRequest, ret =%{public}d", ret);
849 ret = reply.ReadInt32();
850 NETNATIVE_LOGI("End to StopDhcpClient, ret =%{public}d", ret);
851 return ret;
852 }
853
StartDhcpService(const std::string & iface,const std::string & ipv4addr)854 int32_t NetsysNativeServiceProxy::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
855 {
856 NETNATIVE_LOGI("Begin to StartDhcpService");
857 MessageParcel data;
858
859 if (!WriteInterfaceToken(data)) {
860 return ERR_FLATTEN_OBJECT;
861 }
862 if (!data.WriteString(iface)) {
863 return ERR_FLATTEN_OBJECT;
864 }
865 if (!data.WriteString(ipv4addr)) {
866 return ERR_FLATTEN_OBJECT;
867 }
868
869 MessageParcel reply;
870 MessageOption option;
871 Remote()->SendRequest(INetsysService::NETSYS_START_DHCP_SERVICE, data, reply, option);
872 int32_t ret = reply.ReadInt32();
873 NETNATIVE_LOGI("End to StartDhcpService, ret =%{public}d", ret);
874 return ret;
875 }
876
StopDhcpService(const std::string & iface)877 int32_t NetsysNativeServiceProxy::StopDhcpService(const std::string &iface)
878 {
879 NETNATIVE_LOGI("Begin to StopDhcpService");
880 MessageParcel data;
881 if (!WriteInterfaceToken(data)) {
882 return ERR_FLATTEN_OBJECT;
883 }
884 if (!data.WriteString(iface)) {
885 return ERR_FLATTEN_OBJECT;
886 }
887
888 MessageParcel reply;
889 MessageOption option;
890 Remote()->SendRequest(INetsysService::NETSYS_STOP_DHCP_SERVICE, data, reply, option);
891 int32_t ret = reply.ReadInt32();
892 NETNATIVE_LOGI("End to StopDhcpService, ret =%{public}d", ret);
893 return ret;
894 }
895
IpEnableForwarding(const std::string & requestor)896 int32_t NetsysNativeServiceProxy::IpEnableForwarding(const std::string &requestor)
897 {
898 MessageParcel data;
899 if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
900 return ERR_FLATTEN_OBJECT;
901 }
902
903 MessageParcel reply;
904 MessageOption option;
905 Remote()->SendRequest(INetsysService::NETSYS_IPENABLE_FORWARDING, data, reply, option);
906
907 int32_t ret = reply.ReadInt32();
908 NETNATIVE_LOGI("End to IpEnableForwarding, ret =%{public}d", ret);
909 return ret;
910 }
911
IpDisableForwarding(const std::string & requestor)912 int32_t NetsysNativeServiceProxy::IpDisableForwarding(const std::string &requestor)
913 {
914 MessageParcel data;
915 if (!WriteInterfaceToken(data) || !data.WriteString(requestor)) {
916 return ERR_FLATTEN_OBJECT;
917 }
918
919 MessageParcel reply;
920 MessageOption option;
921 Remote()->SendRequest(INetsysService::NETSYS_IPDISABLE_FORWARDING, data, reply, option);
922
923 int32_t ret = reply.ReadInt32();
924 NETNATIVE_LOGI("End to IpDisableForwarding, ret =%{public}d", ret);
925 return ret;
926 }
927
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)928 int32_t NetsysNativeServiceProxy::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
929 {
930 MessageParcel data;
931 if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
932 return ERR_FLATTEN_OBJECT;
933 }
934
935 MessageParcel reply;
936 MessageOption option;
937 Remote()->SendRequest(INetsysService::NETSYS_ENABLE_NAT, data, reply, option);
938
939 int32_t ret = reply.ReadInt32();
940 NETNATIVE_LOGI("End to EnableNat, ret =%{public}d", ret);
941 return ret;
942 }
943
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)944 int32_t NetsysNativeServiceProxy::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
945 {
946 MessageParcel data;
947 if (!WriteNatDataToMessage(data, downstreamIface, upstreamIface)) {
948 return ERR_FLATTEN_OBJECT;
949 }
950
951 MessageParcel reply;
952 MessageOption option;
953 Remote()->SendRequest(INetsysService::NETSYS_DISABLE_NAT, data, reply, option);
954
955 int32_t ret = reply.ReadInt32();
956 NETNATIVE_LOGI("End to DisableNat, ret =%{public}d", ret);
957 return ret;
958 }
959
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)960 int32_t NetsysNativeServiceProxy::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
961 {
962 MessageParcel data;
963 if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
964 return ERR_FLATTEN_OBJECT;
965 }
966
967 MessageParcel reply;
968 MessageOption option;
969 Remote()->SendRequest(INetsysService::NETSYS_IPFWD_ADD_INTERFACE_FORWARD, data, reply, option);
970
971 int32_t ret = reply.ReadInt32();
972 NETNATIVE_LOGI("End to IpfwdAddInterfaceForward, ret =%{public}d", ret);
973 return ret;
974 }
975
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)976 int32_t NetsysNativeServiceProxy::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
977 {
978 MessageParcel data;
979 if (!WriteInterfaceToken(data) || !data.WriteString(fromIface) || !data.WriteString(toIface)) {
980 return ERR_FLATTEN_OBJECT;
981 }
982
983 MessageParcel reply;
984 MessageOption option;
985 Remote()->SendRequest(INetsysService::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD, data, reply, option);
986
987 int32_t ret = reply.ReadInt32();
988 NETNATIVE_LOGI("End to IpfwdRemoveInterfaceForward, ret =%{public}d", ret);
989 return ret;
990 }
991
BandwidthEnableDataSaver(bool enable)992 int32_t NetsysNativeServiceProxy::BandwidthEnableDataSaver(bool enable)
993 {
994 MessageParcel data;
995 if (!WriteInterfaceToken(data)) {
996 NETNATIVE_LOGE("WriteInterfaceToken failed");
997 return ERR_FLATTEN_OBJECT;
998 }
999 if (!data.WriteBool(enable)) {
1000 NETNATIVE_LOGE("WriteBool failed");
1001 return ERR_FLATTEN_OBJECT;
1002 }
1003
1004 MessageParcel reply;
1005 MessageOption option;
1006 auto remote = Remote();
1007 if (remote) {
1008 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER, data, reply, option)) {
1009 NETNATIVE_LOGE("proxy SendRequest failed");
1010 return ERR_FLATTEN_OBJECT;
1011 }
1012 }
1013 int32_t ret = reply.ReadInt32();
1014 return ret;
1015 }
1016
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)1017 int32_t NetsysNativeServiceProxy::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
1018 {
1019 MessageParcel data;
1020 if (!WriteInterfaceToken(data)) {
1021 NETNATIVE_LOGE("WriteInterfaceToken failed");
1022 return ERR_FLATTEN_OBJECT;
1023 }
1024 if (!data.WriteString(ifName)) {
1025 NETNATIVE_LOGE("WriteString failed");
1026 return ERR_FLATTEN_OBJECT;
1027 }
1028 if (!data.WriteInt64(bytes)) {
1029 NETNATIVE_LOGE("WriteInt64 failed");
1030 return ERR_FLATTEN_OBJECT;
1031 }
1032
1033 MessageParcel reply;
1034 MessageOption option;
1035 auto remote = Remote();
1036 if (remote) {
1037 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_SET_IFACE_QUOTA, data, reply, option)) {
1038 NETNATIVE_LOGE("proxy SendRequest failed");
1039 return ERR_FLATTEN_OBJECT;
1040 }
1041 }
1042 int32_t ret = reply.ReadInt32();
1043 return ret;
1044 }
1045
BandwidthRemoveIfaceQuota(const std::string & ifName)1046 int32_t NetsysNativeServiceProxy::BandwidthRemoveIfaceQuota(const std::string &ifName)
1047 {
1048 MessageParcel data;
1049 if (!WriteInterfaceToken(data)) {
1050 NETNATIVE_LOGE("WriteInterfaceToken failed");
1051 return ERR_FLATTEN_OBJECT;
1052 }
1053 if (!data.WriteString(ifName)) {
1054 NETNATIVE_LOGE("WriteString failed");
1055 return ERR_FLATTEN_OBJECT;
1056 }
1057
1058 MessageParcel reply;
1059 MessageOption option;
1060 auto remote = Remote();
1061 if (remote) {
1062 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA, data, reply, option)) {
1063 NETNATIVE_LOGE("proxy SendRequest failed");
1064 return ERR_FLATTEN_OBJECT;
1065 }
1066 }
1067 int32_t ret = reply.ReadInt32();
1068 return ret;
1069 }
1070
BandwidthAddDeniedList(uint32_t uid)1071 int32_t NetsysNativeServiceProxy::BandwidthAddDeniedList(uint32_t uid)
1072 {
1073 MessageParcel data;
1074 if (!WriteInterfaceToken(data)) {
1075 NETNATIVE_LOGE("WriteInterfaceToken failed");
1076 return ERR_FLATTEN_OBJECT;
1077 }
1078 if (!data.WriteUint32(uid)) {
1079 NETNATIVE_LOGE("WriteUint32 failed");
1080 return ERR_FLATTEN_OBJECT;
1081 }
1082
1083 MessageParcel reply;
1084 MessageOption option;
1085 auto remote = Remote();
1086 if (remote) {
1087 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_ADD_DENIED_LIST, data, reply, option)) {
1088 NETNATIVE_LOGE("proxy SendRequest failed");
1089 return ERR_FLATTEN_OBJECT;
1090 }
1091 }
1092 int32_t ret = reply.ReadInt32();
1093 return ret;
1094 }
1095
BandwidthRemoveDeniedList(uint32_t uid)1096 int32_t NetsysNativeServiceProxy::BandwidthRemoveDeniedList(uint32_t uid)
1097 {
1098 MessageParcel data;
1099 if (!WriteInterfaceToken(data)) {
1100 NETNATIVE_LOGE("WriteInterfaceToken failed");
1101 return ERR_FLATTEN_OBJECT;
1102 }
1103 if (!data.WriteUint32(uid)) {
1104 NETNATIVE_LOGE("WriteUint32 failed");
1105 return ERR_FLATTEN_OBJECT;
1106 }
1107
1108 MessageParcel reply;
1109 MessageOption option;
1110 auto remote = Remote();
1111 if (remote) {
1112 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST, data, reply, option)) {
1113 NETNATIVE_LOGE("proxy SendRequest failed");
1114 return ERR_FLATTEN_OBJECT;
1115 }
1116 }
1117 int32_t ret = reply.ReadInt32();
1118 return ret;
1119 }
1120
BandwidthAddAllowedList(uint32_t uid)1121 int32_t NetsysNativeServiceProxy::BandwidthAddAllowedList(uint32_t uid)
1122 {
1123 MessageParcel data;
1124 if (!WriteInterfaceToken(data)) {
1125 NETNATIVE_LOGE("WriteInterfaceToken failed");
1126 return ERR_FLATTEN_OBJECT;
1127 }
1128 if (!data.WriteUint32(uid)) {
1129 NETNATIVE_LOGE("WriteUint32 failed");
1130 return ERR_FLATTEN_OBJECT;
1131 }
1132
1133 MessageParcel reply;
1134 MessageOption option;
1135 auto remote = Remote();
1136 if (remote) {
1137 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST, data, reply, option)) {
1138 NETNATIVE_LOGE("proxy SendRequest failed");
1139 return ERR_FLATTEN_OBJECT;
1140 }
1141 }
1142 int32_t ret = reply.ReadInt32();
1143 return ret;
1144 }
1145
BandwidthRemoveAllowedList(uint32_t uid)1146 int32_t NetsysNativeServiceProxy::BandwidthRemoveAllowedList(uint32_t uid)
1147 {
1148 MessageParcel data;
1149 if (!WriteInterfaceToken(data)) {
1150 NETNATIVE_LOGE("WriteInterfaceToken failed");
1151 return ERR_FLATTEN_OBJECT;
1152 }
1153 if (!data.WriteUint32(uid)) {
1154 NETNATIVE_LOGE("WriteUint32 failed");
1155 return ERR_FLATTEN_OBJECT;
1156 }
1157
1158 MessageParcel reply;
1159 MessageOption option;
1160 auto remote = Remote();
1161 if (remote) {
1162 if (ERR_NONE !=
1163 remote->SendRequest(INetsysService::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST, data, reply, option)) {
1164 NETNATIVE_LOGE("proxy SendRequest failed");
1165 return ERR_FLATTEN_OBJECT;
1166 }
1167 }
1168 int32_t ret = reply.ReadInt32();
1169 return ret;
1170 }
1171
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1172 int32_t NetsysNativeServiceProxy::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1173 {
1174 MessageParcel data;
1175 uint32_t uidSize = uids.size();
1176 if (uidSize > UIDS_LIST_MAX_SIZE) {
1177 NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1178 return ERR_INVALID_DATA;
1179 }
1180 if (!WriteInterfaceToken(data)) {
1181 NETNATIVE_LOGE("WriteInterfaceToken failed");
1182 return ERR_FLATTEN_OBJECT;
1183 }
1184 if (!data.WriteUint32(chain)) {
1185 NETNATIVE_LOGE("WriteUint32 failed");
1186 return ERR_FLATTEN_OBJECT;
1187 }
1188 if (!data.WriteUint32(uidSize)) {
1189 NETNATIVE_LOGE("WriteUint32 failed");
1190 return ERR_FLATTEN_OBJECT;
1191 }
1192 std::vector<uint32_t> vUids;
1193 vUids.assign(uids.begin(), uids.end());
1194 std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1195
1196 MessageParcel reply;
1197 MessageOption option;
1198 auto remote = Remote();
1199 if (remote) {
1200 if (ERR_NONE !=
1201 remote->SendRequest(INetsysService::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN, data, reply, option)) {
1202 NETNATIVE_LOGE("proxy SendRequest failed");
1203 return ERR_FLATTEN_OBJECT;
1204 }
1205 }
1206 int32_t ret = reply.ReadInt32();
1207 return ret;
1208 }
1209
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)1210 int32_t NetsysNativeServiceProxy::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
1211 {
1212 MessageParcel data;
1213 uint32_t uidSize = uids.size();
1214 if (uidSize > UIDS_LIST_MAX_SIZE) {
1215 NETNATIVE_LOGE("Uids size err [%{public}d]", uidSize);
1216 return ERR_INVALID_DATA;
1217 }
1218 if (!WriteInterfaceToken(data)) {
1219 NETNATIVE_LOGE("WriteInterfaceToken failed");
1220 return ERR_FLATTEN_OBJECT;
1221 }
1222 if (!data.WriteUint32(chain)) {
1223 NETNATIVE_LOGE("WriteUint32 Error");
1224 return ERR_FLATTEN_OBJECT;
1225 }
1226 if (!data.WriteUint32(uidSize)) {
1227 NETNATIVE_LOGE("WriteUint32 Error");
1228 return ERR_FLATTEN_OBJECT;
1229 }
1230 std::vector<uint32_t> vUids;
1231 vUids.assign(uids.begin(), uids.end());
1232 std::for_each(vUids.begin(), vUids.end(), [&data](uint32_t uid) { data.WriteUint32(uid); });
1233
1234 MessageParcel reply;
1235 MessageOption option;
1236 auto remote = Remote();
1237 if (remote) {
1238 if (ERR_NONE !=
1239 remote->SendRequest(INetsysService::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN, data, reply, option)) {
1240 NETNATIVE_LOGE("proxy SendRequest failed");
1241 return ERR_FLATTEN_OBJECT;
1242 }
1243 }
1244 int32_t ret = reply.ReadInt32();
1245 return ret;
1246 }
1247
FirewallEnableChain(uint32_t chain,bool enable)1248 int32_t NetsysNativeServiceProxy::FirewallEnableChain(uint32_t chain, bool enable)
1249 {
1250 MessageParcel data;
1251 if (!WriteInterfaceToken(data)) {
1252 NETNATIVE_LOGE("WriteInterfaceToken failed");
1253 return ERR_FLATTEN_OBJECT;
1254 }
1255 if (!data.WriteUint32(chain)) {
1256 NETNATIVE_LOGE("WriteUint32 Error");
1257 return ERR_FLATTEN_OBJECT;
1258 }
1259 if (!data.WriteBool(enable)) {
1260 NETNATIVE_LOGE("WriteBool Error");
1261 return ERR_FLATTEN_OBJECT;
1262 }
1263
1264 MessageParcel reply;
1265 MessageOption option;
1266 auto remote = Remote();
1267 if (remote) {
1268 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_FIREWALL_ENABLE_CHAIN, data, reply, option)) {
1269 NETNATIVE_LOGE("proxy SendRequest failed");
1270 return ERR_FLATTEN_OBJECT;
1271 }
1272 }
1273 int32_t ret = reply.ReadInt32();
1274 return ret;
1275 }
1276
FirewallSetUidRule(uint32_t chain,uint32_t uid,uint32_t firewallRule)1277 int32_t NetsysNativeServiceProxy::FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule)
1278 {
1279 MessageParcel data;
1280 if (!WriteInterfaceToken(data)) {
1281 NETNATIVE_LOGE("WriteInterfaceToken failed");
1282 return ERR_FLATTEN_OBJECT;
1283 }
1284 if (!data.WriteUint32(chain)) {
1285 NETNATIVE_LOGE("WriteUint32 failed");
1286 return ERR_FLATTEN_OBJECT;
1287 }
1288 if (!data.WriteUint32(uid)) {
1289 NETNATIVE_LOGE("WriteUint32 failed");
1290 return ERR_FLATTEN_OBJECT;
1291 }
1292 if (!data.WriteUint32(firewallRule)) {
1293 NETNATIVE_LOGE("WriteUint32 failed");
1294 return ERR_FLATTEN_OBJECT;
1295 }
1296
1297 MessageParcel reply;
1298 MessageOption option;
1299 auto remote = Remote();
1300 if (remote) {
1301 if (ERR_NONE != remote->SendRequest(INetsysService::NETSYS_FIREWALL_SET_UID_RULE, data, reply, option)) {
1302 NETNATIVE_LOGE("proxy SendRequest failed");
1303 return ERR_FLATTEN_OBJECT;
1304 }
1305 }
1306 int32_t ret = reply.ReadInt32();
1307 return ret;
1308 }
1309
ShareDnsSet(uint16_t netId)1310 int32_t NetsysNativeServiceProxy::ShareDnsSet(uint16_t netId)
1311 {
1312 MessageParcel data;
1313 if (!WriteInterfaceToken(data)) {
1314 return ERR_FLATTEN_OBJECT;
1315 }
1316 if (!data.WriteUint16(netId)) {
1317 return ERR_FLATTEN_OBJECT;
1318 }
1319
1320 MessageParcel reply;
1321 MessageOption option;
1322 int32_t error = Remote()->SendRequest(INetsysService::NETSYS_TETHER_DNS_SET, data, reply, option);
1323 if (error != ERR_NONE) {
1324 NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1325 return error;
1326 }
1327 return reply.ReadInt32();
1328 }
1329
StartDnsProxyListen()1330 int32_t NetsysNativeServiceProxy::StartDnsProxyListen()
1331 {
1332 MessageParcel data;
1333 if (!WriteInterfaceToken(data)) {
1334 return ERR_FLATTEN_OBJECT;
1335 }
1336
1337 MessageParcel reply;
1338 MessageOption option;
1339 int32_t error = Remote()->SendRequest(INetsysService::NETSYS_START_DNS_PROXY_LISTEN, data, reply, option);
1340 if (error != ERR_NONE) {
1341 NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1342 return error;
1343 }
1344 return reply.ReadInt32();
1345 }
1346
StopDnsProxyListen()1347 int32_t NetsysNativeServiceProxy::StopDnsProxyListen()
1348 {
1349 MessageParcel data;
1350 if (!WriteInterfaceToken(data)) {
1351 return ERR_FLATTEN_OBJECT;
1352 }
1353
1354 MessageParcel reply;
1355 MessageOption option;
1356 int32_t error = Remote()->SendRequest(INetsysService::NETSYS_STOP_DNS_PROXY_LISTEN, data, reply, option);
1357 if (error != ERR_NONE) {
1358 NETNATIVE_LOGE("proxy SendRequest failed, error code: [%{public}d]", error);
1359 return error;
1360 }
1361 return reply.ReadInt32();
1362 }
1363
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,NetworkSharingTraffic & traffic)1364 int32_t NetsysNativeServiceProxy::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
1365 NetworkSharingTraffic &traffic)
1366 {
1367 MessageParcel data;
1368 if (!WriteInterfaceToken(data)) {
1369 return ERR_FLATTEN_OBJECT;
1370 }
1371 if (!data.WriteString(downIface)) {
1372 return ERR_FLATTEN_OBJECT;
1373 }
1374 if (!data.WriteString(upIface)) {
1375 return ERR_FLATTEN_OBJECT;
1376 }
1377
1378 MessageParcel reply;
1379 MessageOption option;
1380 Remote()->SendRequest(INetsysService::NETSYS_GET_SHARING_NETWORK_TRAFFIC, data, reply, option);
1381
1382 int32_t ret = reply.ReadInt32();
1383 traffic.receive = reply.ReadInt64();
1384 traffic.send = reply.ReadInt64();
1385 traffic.all = reply.ReadInt64();
1386 NETNATIVE_LOGI("NetsysNativeServiceProxy GetNetworkSharingTraffic ret=%{public}d", ret);
1387 return ret;
1388 }
1389 } // namespace NetsysNative
1390 } // namespace OHOS
1391