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 #ifndef NETSYS_NATIVE_CLIENT_H
17 #define NETSYS_NATIVE_CLIENT_H
18 
19 #include <linux/if.h>
20 #include <memory>
21 #include <netdb.h>
22 #include <string>
23 #include <vector>
24 
25 #include "i_netsys_service.h"
26 #include "i_net_diag_callback.h"
27 #include "i_net_dns_health_callback.h"
28 #include "net_dns_result_callback_stub.h"
29 #include "netsys_controller_callback.h"
30 #include "netsys_controller_define.h"
31 #include "network_sharing.h"
32 #include "notify_callback_stub.h"
33 #include "netsys_dns_report_callback.h"
34 
35 namespace OHOS {
36 namespace NetManagerStandard {
37 class NetsysNativeClient {
38 private:
39     class NativeNotifyCallback : public OHOS::NetsysNative::NotifyCallbackStub {
40     public:
41         NativeNotifyCallback(NetsysNativeClient &netsysNativeClient);
42         ~NativeNotifyCallback() override = default;
43         int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
44                                           int scope) override;
45         int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
46                                           int scope) override;
47         int32_t OnInterfaceAdded(const std::string &ifName) override;
48         int32_t OnInterfaceRemoved(const std::string &ifName) override;
49         int32_t OnInterfaceChanged(const std::string &ifName, bool up) override;
50         int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
51         int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
52                                const std::string &ifName) override;
53         int32_t OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult) override;
54         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
55 
56     private:
57         NetsysNativeClient &netsysNativeClient_;
58     };
59 
60     class NativeNetDnsResultCallback : public OHOS::NetsysNative::NetDnsResultCallbackStub {
61     public:
62         NativeNetDnsResultCallback(NetsysNativeClient &netsysNativeClient);
63         ~NativeNetDnsResultCallback() override = default;
64         int32_t OnDnsResultReport(uint32_t size, std::list<OHOS::NetsysNative::NetDnsResultReport> res) override;
65 
66     private:
67         NetsysNativeClient &netsysNativeClient_;
68     };
69 
70 public:
71     NetsysNativeClient();
72     ~NetsysNativeClient() = default;
73 
74     /**
75      * Disallow or allow a app to create AF_INET or AF_INET6 socket
76      *
77      * @param uid App's uid which need to be disallowed ot allowed to create AF_INET or AF_INET6 socket
78      * @param allow 0 means disallow, 1 means allow
79      * @return return 0 if OK, return error number if not OK
80      */
81     int32_t SetInternetPermission(uint32_t uid, uint8_t allow);
82 
83     /**
84      * Create a physical network
85      *
86      * @param netId
87      * @param permission Permission to create a physical network
88      * @return Return the return value of the netsys interface call
89      */
90     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission);
91 
92     int32_t NetworkCreateVirtual(int32_t netId, bool hasDns);
93     int32_t NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges);
94     int32_t NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges);
95 
96     /**
97      * Destroy the network
98      *
99      * @param netId
100      * @return Return the return value of the netsys interface call
101      */
102     int32_t NetworkDestroy(int32_t netId);
103 
104     int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set<int32_t> &uids);
105     int32_t DestroyVnic();
106 
107     /**
108      * Add network port device
109      *
110      * @param netId
111      * @param iface Network port device name
112      * @return Return the return value of the netsys interface call
113      */
114     int32_t NetworkAddInterface(int32_t netId, const std::string &iface, NetBearType netBearerType);
115 
116     /**
117      * Delete network port device
118      *
119      * @param netId
120      * @param iface Network port device name
121      * @return Return the return value of the netsys interface call
122      */
123     int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface);
124 
125     /**
126      * Add route
127      *
128      * @param netId
129      * @param ifName Network port device name
130      * @param destination Target host ip
131      * @param nextHop Next hop address
132      * @return Return the return value of the netsys interface call
133      */
134     int32_t NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
135                             const std::string &nextHop);
136 
137     /**
138      * Remove route
139      *
140      * @param netId
141      * @param ifName Network port device name
142      * @param destination Target host ip
143      * @param nextHop Next hop address
144      * @return Return the return value of the netsys interface call
145      */
146     int32_t NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
147                                const std::string &nextHop);
148 
149     /**
150      * @brief Get interface config
151      *
152      * @param iface Network port device name
153      * @return Return the result of this action, ERR_NONE is success.
154      */
155     int32_t GetInterfaceConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg);
156 
157     /**
158      * @brief Set interface config
159      *
160      * @param cfg Network port info
161      * @return Return the result of this action, ERR_NONE is success.
162      */
163     int32_t SetInterfaceConfig(const OHOS::nmd::InterfaceConfigurationParcel &cfg);
164 
165     /**
166      * Turn off the device
167      *
168      * @param iface Network port device name
169      * @return Return the result of this action
170      */
171     int32_t SetInterfaceDown(const std::string &iface);
172 
173     /**
174      * Turn on the device
175      *
176      * @param iface Network port device name
177      * @return Return the result of this action
178      */
179     int32_t SetInterfaceUp(const std::string &iface);
180 
181     /**
182      * Clear the network interface ip address
183      *
184      * @param ifName Network port device name
185      */
186     void ClearInterfaceAddrs(const std::string &ifName);
187 
188     /**
189      * Obtain mtu from the network interface device
190      *
191      * @param ifName Network port device name
192      * @return Return the return value of the netsys interface call
193      */
194     int32_t GetInterfaceMtu(const std::string &ifName);
195 
196     /**
197      * Set mtu to network interface device
198      *
199      * @param ifName Network port device name
200      * @param mtu
201      * @return Return the return value of the netsys interface call
202      */
203     int32_t SetInterfaceMtu(const std::string &ifName, int32_t mtu);
204 
205     /**
206      * Set tcp buffer sizes
207      *
208      * @param tcpBufferSizes tcpBufferSizes
209      * @return Return the return value of the netsys interface call
210      */
211     int32_t SetTcpBufferSizes(const std::string &tcpBufferSizes);
212 
213     /**
214      * Add ip address
215      *
216      * @param ifName Network port device name
217      * @param ipAddr    ip address
218      * @param prefixLength  subnet mask
219      * @return Return the return value of the netsys interface call
220      */
221     int32_t AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength);
222 
223     /**
224      * Delete ip address
225      *
226      * @param ifName Network port device name
227      * @param ipAddr ip address
228      * @param prefixLength subnet mask
229      * @return Return the return value of the netsys interface call
230      */
231     int32_t DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength);
232 
233     /**
234      * Delete ip address
235      *
236      * @param ifName Network port device name
237      * @param ipAddr ip address
238      * @param prefixLength subnet mask
239      * @param netCapabilities Net capabilities in string format
240      * @return Return the return value of the netsys interface call
241      */
242     int32_t DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength,
243                                 const std::string &netCapabilities);
244 
245     /**
246      * Set iface ip address
247      *
248      * @param ifaceName Network port device name
249      * @param ipAddress Ip address
250      * @return Return the return value of the netsys interface call
251      */
252     int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress);
253 
254     /**
255      * Set iface up
256      *
257      * @param ifaceName Network port device name
258      * @return Return the return value of the netsys interface call
259      */
260     int32_t InterfaceSetIffUp(const std::string &ifaceName);
261 
262     /**
263      * Set dns
264      *
265      * @param netId
266      * @param baseTimeoutMsec
267      * @param retryCount
268      * @param servers
269      * @param domains
270      * @return Return the return value of the netsys interface call
271      */
272     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
273                               const std::vector<std::string> &servers, const std::vector<std::string> &domains);
274 
275     /**
276      * Get dns server param info
277      *
278      * @param netId
279      * @param servers
280      * @param domains
281      * @param baseTimeoutMsec
282      * @param retryCount
283      * @return Return the return value of the netsys interface call
284      */
285     int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
286                               uint16_t &baseTimeoutMsec, uint8_t &retryCount);
287 
288     /**
289      * Create dns cache before set dns
290      *
291      * @param netId
292      * @return Return the return value for status of call
293      */
294     int32_t CreateNetworkCache(uint16_t netId);
295 
296     /**
297      * Destroy dns cache
298      *
299      * @param netId
300      * @return Return the return value of the netsys interface call
301      */
302     int32_t DestroyNetworkCache(uint16_t netId);
303 
304     /**
305      * Domain name resolution Obtains the domain name address
306      *
307      * @param hostName Domain name to be resolved
308      * @param serverName Server name used for query
309      * @param hints Limit parameters when querying
310      * @param netId Network id
311      * @param res return addrinfo
312      * @return Return the return value of the netsys interface call
313      */
314     int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
315                         uint16_t netId, std::vector<AddrInfo> &res);
316 
317     /**
318      * Obtains the bytes of the sharing network.
319      *
320      * @return Success return 0.
321      */
322     int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
323                                      nmd::NetworkSharingTraffic &traffic);
324 
325     /**
326      * Obtains the bytes received over the cellular network.
327      *
328      * @return The number of received bytes.
329      */
330     int64_t GetCellularRxBytes();
331 
332     /**
333      * Obtains the bytes sent over the cellular network.
334      *
335      * @return The number of sent bytes.
336      */
337     int64_t GetCellularTxBytes();
338 
339     /**
340      * Obtains the bytes received through all NICs.
341      *
342      * @return The number of received bytes.
343      */
344     int64_t GetAllRxBytes();
345 
346     /**
347      * Obtains the bytes sent through all NICs.
348      *
349      * @return The number of sent bytes.
350      */
351     int64_t GetAllTxBytes();
352 
353     /**
354      * Obtains the bytes received through a specified UID.
355      *
356      * @param uid app id.
357      * @return The number of received bytes.
358      */
359     int64_t GetUidRxBytes(uint32_t uid);
360 
361     /**
362      * Obtains the bytes sent through a specified UID.
363      *
364      * @param uid app id.
365      * @return The number of sent bytes.
366      */
367     int64_t GetUidTxBytes(uint32_t uid);
368 
369     /**
370      * Obtains the bytes received through a specified UID on Iface.
371      *
372      * @param uid app id.
373      * @param iface The name of the interface.
374      * @return The number of received bytes.
375      */
376     int64_t GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName);
377 
378     /**
379      * Obtains the bytes sent through a specified UID on Iface.
380      *
381      * @param uid app id.
382      * @param iface The name of the interface.
383      * @return The number of sent bytes.
384      */
385     int64_t GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName);
386 
387     /**
388      * Obtains the bytes received through a specified NIC.
389      *
390      * @param iface The name of the interface.
391      * @return The number of received bytes.
392      */
393     int64_t GetIfaceRxBytes(const std::string &interfaceName);
394 
395     /**
396      * Obtains the bytes sent through a specified NIC.
397      *
398      * @param iface The name of the interface.
399      * @return The number of sent bytes.
400      */
401     int64_t GetIfaceTxBytes(const std::string &interfaceName);
402 
403     /**
404      * Obtains the NIC list.
405      *
406      * @return The list of interface.
407      */
408     std::vector<std::string> InterfaceGetList();
409 
410     /**
411      * Obtains the uid list.
412      *
413      * @return The list of uid.
414      */
415     std::vector<std::string> UidGetList();
416 
417     /**
418      * Obtains the packets received through a specified NIC.
419      *
420      * @param iface The name of the interface.
421      * @return The number of received packets.
422      */
423     int64_t GetIfaceRxPackets(const std::string &interfaceName);
424 
425     /**
426      * Obtains the packets sent through a specified NIC.
427      *
428      * @param iface The name of the interface.
429      * @return The number of sent packets.
430      */
431     int64_t GetIfaceTxPackets(const std::string &interfaceName);
432 
433     /**
434      *  set default network.
435      *
436      * @return Return the return value of the netsys interface call
437      */
438     int32_t SetDefaultNetWork(int32_t netId);
439 
440     /**
441      * clear default network netId.
442      *
443      * @return Return the return value of the netsys interface call
444      */
445     int32_t ClearDefaultNetWorkNetId();
446 
447     /**
448      * Obtains the NIC list.
449      *
450      * @param socketFd
451      * @param netId
452      * @return Return the return value of the netsys interface call
453      */
454     int32_t BindSocket(int32_t socketFd, uint32_t netId);
455 
456     /**
457      * Enable ip forwarding.
458      *
459      * @param requestor the requestor of forwarding
460      * @return Return the return value of the netsys interface call.
461      */
462     int32_t IpEnableForwarding(const std::string &requestor);
463 
464     /**
465      * Disable ip forwarding.
466      *
467      * @param requestor the requestor of forwarding
468      * @return Return the return value of the netsys interface call.
469      */
470     int32_t IpDisableForwarding(const std::string &requestor);
471 
472     /**
473      * Enable Nat.
474      *
475      * @param downstreamIface the name of downstream interface
476      * @param upstreamIface the name of upstream interface
477      * @return Return the return value of the netsys interface call.
478      */
479     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface);
480     /**
481      * Disable Nat.
482      *
483      * @param downstreamIface the name of downstream interface
484      * @param upstreamIface the name of upstream interface
485      * @return Return the return value of the netsys interface call.
486      */
487     int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface);
488 
489     /**
490      * Add interface forward.
491      *
492      * @param fromIface the name of incoming interface
493      * @param toIface the name of outcoming interface
494      * @return Return the return value of the netsys interface call.
495      */
496     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface);
497 
498     /**
499      * Remove interface forward.
500      *
501      * @param fromIface the name of incoming interface
502      * @param toIface the name of outcoming interface
503      * @return Return the return value of the netsys interface call.
504      */
505     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface);
506 
507     /**
508      * Set tether dns.
509      *
510      * @param netId network id
511      * @param dnsAddr the list of dns address
512      * @return Return the return value of the netsys interface call.
513      */
514     int32_t ShareDnsSet(uint16_t netId);
515 
516     /**
517      * tart dns proxy listen
518      *
519      * @return Return the return value of the netsys interface call.
520      */
521     virtual int32_t StartDnsProxyListen();
522 
523     /**
524      * stop dns proxy listen
525      *
526      * @return Return the return value of the netsys interface call.
527      */
528     virtual int32_t StopDnsProxyListen();
529 
530     /**
531      * Set net callback function.
532      *
533      * @param callback callback function class
534      * @return Return the return value of the netsys interface call.
535      */
536     int32_t RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback);
537 
538     /**
539      * protect tradition network to connect VPN.
540      *
541      * @param socketFd socket file description
542      * @return Return the return value of the netsys interface call.
543      */
544     int32_t BindNetworkServiceVpn(int32_t socketFd);
545 
546     /**
547      * enable virtual network interface card.
548      *
549      * @param socketFd socket file description
550      * @param ifRequest interface request
551      * @return Return the return value of the netsys interface call.
552      */
553     int32_t EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd);
554 
555     /**
556      * Set ip address.
557      *
558      * @param socketFd socket file description
559      * @param ipAddress ip address
560      * @param prefixLen the mask of ip address
561      * @param ifRequest interface request
562      * @return Return the return value of the netsys interface call.
563      */
564     int32_t SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen, struct ifreq &ifRequest);
565 
566     /**
567      * Set network blocking.
568      *
569      * @param ifaceFd interface file description
570      * @param isBlock network blocking
571      * @return Return the return value of the netsys interface call.
572      */
573     int32_t SetBlocking(int32_t ifaceFd, bool isBlock);
574 
575     /**
576      * Start Dhcp Client.
577      *
578      * @param iface interface file description
579      * @param bIpv6 network blocking
580      * @return Return the return value of the netsys interface call.
581      */
582     int32_t StartDhcpClient(const std::string &iface, bool bIpv6);
583 
584     /**
585      * Stop Dhcp Client.
586      *
587      * @param iface interface file description
588      * @param bIpv6 network blocking
589      * @return Return the return value of the netsys interface call.
590      */
591     int32_t StopDhcpClient(const std::string &iface, bool bIpv6);
592 
593     /**
594      * Register Notify Callback
595      *
596      * @param callback
597      * @return Return the return value of the netsys interface call.
598      */
599     int32_t RegisterCallback(const sptr<NetsysControllerCallback> &callback);
600 
601     /**
602      * start dhcpservice.
603      *
604      * @param iface interface name
605      * @param ipv4addr ipv4 addr
606      * @return Return the return value of the netsys interface call.
607      */
608     int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr);
609 
610     /**
611      * stop dhcpservice.
612      *
613      * @param iface interface name
614      * @return Return the return value of the netsys interface call.
615      */
616     int32_t StopDhcpService(const std::string &iface);
617 
618     /**
619      * Turn on data saving mode.
620      *
621      * @param enable enable or disable
622      * @return value the return value of the netsys interface call.
623      */
624     int32_t BandwidthEnableDataSaver(bool enable);
625 
626     /**
627      * Set quota.
628      *
629      * @param iface interface name
630      * @param bytes
631      * @return Return the return value of the netsys interface call.
632      */
633     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes);
634 
635     /**
636      * delete quota.
637      *
638      * @param iface interface name
639      * @return Return the return value of the netsys interface call.
640      */
641     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName);
642 
643     /**
644      * Add DeniedList.
645      *
646      * @param uid
647      * @return Return the return value of the netsys interface call.
648      */
649     int32_t BandwidthAddDeniedList(uint32_t uid);
650 
651     /**
652      * Remove DeniedList.
653      *
654      * @param uid
655      * @return Return the return value of the netsys interface call.
656      */
657     int32_t BandwidthRemoveDeniedList(uint32_t uid);
658 
659     /**
660      * Add DeniedList.
661      *
662      * @param uid
663      * @return Return the return value of the netsys interface call.
664      */
665     int32_t BandwidthAddAllowedList(uint32_t uid);
666 
667     /**
668      * Remove DeniedList.
669      *
670      * @param uid
671      * @return Return the return value of the netsys interface call.
672      */
673     int32_t BandwidthRemoveAllowedList(uint32_t uid);
674 
675     /**
676      * Set firewall rules.
677      *
678      * @param chain chain type
679      * @param isAllowedList is or not AllowedList
680      * @param uids
681      * @return value the return value of the netsys interface call.
682      */
683     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
684 
685     /**
686      * Set firewall rules.
687      *
688      * @param chain chain type
689      * @param isAllowedList is or not AllowedList
690      * @param uids
691      * @return value the return value of the netsys interface call.
692      */
693     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
694 
695     /**
696      * Enable or disable the specified firewall chain.
697      *
698      * @param chain chain type
699      * @param enable enable or disable
700      * @return Return the return value of the netsys interface call.
701      */
702     int32_t FirewallEnableChain(uint32_t chain, bool enable);
703 
704     /**
705      * Firewall set uid rule.
706      *
707      * @param chain chain type
708      * @param uid uid
709      * @param firewallRule firewall rule
710      * @return Return the return value of the netsys interface call.
711      */
712     int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule);
713 
714     /**
715      * Get total traffic
716      *
717      * @param stats stats
718      * @param type type
719      * @return returns the total traffic of the specified type
720      */
721     int32_t GetTotalStats(uint64_t &stats, uint32_t type);
722 
723     /**
724      * Get uid traffic
725      *
726      * @param stats stats
727      * @param type type
728      * @param uid uid
729      * @return returns the traffic of the uid
730      */
731     int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid);
732 
733     /**
734      * Get Iface traffic
735      *
736      * @param stats stats
737      * @param type type
738      * @param interfaceName interfaceName
739      * @return returns the traffic of the Iface
740      */
741     int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName);
742 
743     /**
744      * Get all Sim stats info
745      * @param stats stats
746      * @return returns the all info of the stats
747      */
748     int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats);
749 
750     /**
751      * Delete the Sim Iface Stats with uid
752      *
753      * @param uid the uid of application
754      * @return returns 0 for success other as failed.
755      */
756     int32_t DeleteSimStatsInfo(uint32_t uid);
757 
758     /**
759      * Get all stats info
760      *
761      * @param stats stats
762      * @return returns the all info of the stats
763      */
764     int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats);
765 
766     /**
767      * Delete the Iface Stats with uid
768      *
769      * @param uid the uid of application
770      * @return returns 0 for success other as failed.
771      */
772     int32_t DeleteStatsInfo(uint32_t uid);
773 
774     /**
775      * Set iptables for result
776      *
777      * @param cmd Iptables command
778      * @param respond The respond of execute iptables command
779      * @param ipType The type of iptables command.
780      * @return Value the return value of the netsys interface call
781      */
782     int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond, NetsysNative::IptablesType ipType);
783 
784     /**
785      * Check network connectivity by sending packets to a host and reporting its response.
786      *
787      * @param pingOption Ping option
788      * @param callback The respond of execute ping cmd.
789      * @return Value the return value of the netsys interface call
790      */
791     int32_t NetDiagPingHost(const OHOS::NetsysNative::NetDiagPingOption &pingOption,
792                             const sptr<OHOS::NetsysNative::INetDiagCallback> &callback);
793 
794     /**
795      * Get networking route table
796      *
797      * @param routeTables Network route table list.
798      * @return Value the return value of the netsys interface call
799      */
800     int32_t NetDiagGetRouteTable(std::list<OHOS::NetsysNative::NetDiagRouteTable> &routeTables);
801 
802     /**
803      * Get networking sockets info.
804      *
805      * @param socketType Network protocol.
806      * @param socketsInfo The result of network sockets info.
807      * @return Value the return value of the netsys interface call
808      */
809     int32_t NetDiagGetSocketsInfo(OHOS::NetsysNative::NetDiagProtocolType socketType,
810                                   OHOS::NetsysNative::NetDiagSocketsInfo &socketsInfo);
811 
812     /**
813      * Get network interface configuration.
814      *
815      * @param configs The result of network interface configuration.
816      * @param ifaceName Get interface configuration information for the specified interface name.
817      *                  If the interface name is empty, default to getting all interface configuration information.
818      * @return Value the return value of the netsys interface call
819      */
820     int32_t NetDiagGetInterfaceConfig(std::list<OHOS::NetsysNative::NetDiagIfaceConfig> &configs,
821                                       const std::string &ifaceName);
822 
823     /**
824      * Update network interface configuration.
825      *
826      * @param configs Network interface configuration.
827      * @param ifaceName Interface name.
828      * @param add Add or delete.
829      * @return Value the return value of the netsys interface call
830      */
831     int32_t NetDiagUpdateInterfaceConfig(const OHOS::NetsysNative::NetDiagIfaceConfig &config,
832                                          const std::string &ifaceName, bool add);
833 
834     /**
835      * Set network interface up/down state.
836      *
837      * @param ifaceName Interface name.
838      * @param up Up or down.
839      * @return Value the return value of the netsys interface call
840      */
841     int32_t NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up);
842     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
843                          const std::string &ifName);
844     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
845                          const std::string &ifName);
846 
847         /**
848      * Register Dns Result Callback Listener.
849      *
850      * @param callback Callback function
851      * @param timestep Time gap between two callbacks
852      * @return Value the return value of the netsys interface call
853      */
854     int32_t RegisterDnsResultCallback(const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> &callback,
855         uint32_t timeStep);
856 
857     /**
858      * Unregister Dns Result Callback Listener.
859      *
860      * @param callback Callback function
861      * @return Value the return value of the netsys interface call
862      */
863     int32_t UnregisterDnsResultCallback(const sptr<OHOS::NetManagerStandard::NetsysDnsReportCallback> &callback);
864 
865     /**
866      * Register Dns Health Callback Listener.
867      *
868      * @param callback Callback function
869      * @return Value the return value of the netsys interface call
870      */
871     int32_t RegisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback);
872 
873     /**
874      * Unregister Dns Health Callback Listener.
875      *
876      * @param callback Callback function
877      * @return Value the return value of the netsys interface call
878      */
879     int32_t UnregisterDnsHealthCallback(const sptr<OHOS::NetsysNative::INetDnsHealthCallback> &callback);
880 
881     /**
882      * Get Cookie Stats.
883      *
884      * @param stats stats
885      * @param type type
886      * @param cookie cookie
887      * @return Value the return value of the netsys interface call
888      */
889     int32_t GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie);
890 
891     int32_t GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn);
892 
893     int32_t UpdateNetworkSharingType(uint32_t type, bool isOpen);
894 
895 #ifdef FEATURE_NET_FIREWALL_ENABLE
896     /**
897      * Set firewall rules to native
898      *
899      * @param type ip, dns, domain
900      * @param ruleList list of NetFirewallIpRule
901      * @param isFinish transmit finish or not
902      * @return 0 if success or -1 if an error occurred
903      */
904     int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
905                              bool isFinish);
906 
907     /**
908      * Set firewall default action
909      *
910      * @param inDefault  Default action of NetFirewallRuleDirection:RULE_IN
911      * @param outDefault Default action of NetFirewallRuleDirection:RULE_OUT
912      * @return 0 if success or -1 if an error occurred
913      */
914     int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault);
915 
916     /**
917      * Set firewall current user id
918      *
919      * @param userId current user id
920      * @return 0 if success or -1 if an error occurred
921      */
922     int32_t SetFirewallCurrentUserId(int32_t userId);
923 
924     /**
925      * Clear firewall rules by type
926      *
927      * @param type type
928      * @return 0 if success or -1 if an error occurred
929      */
930     int32_t ClearFirewallRules(NetFirewallRuleType type);
931 
932     /**
933      * Register callback for recevie intercept event
934      *
935      * @param callback implement of INetFirewallCallback
936      * @return 0 if success or -1 if an error occurred
937      */
938     int32_t RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback);
939 
940     /**
941      * Unregister callback for recevie intercept event
942      *
943      * @param callback register callback for recevie intercept event
944      * @return 0 if success or -1 if an error occurred
945      */
946     int32_t UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback);
947 #endif
948 
949     int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on);
950 
951     int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on);
952 
953     /**
954      * Set the policy to access the network of the specified application.
955      *
956      * @param uid - The specified UID of application.
957      * @param policy - the network access policy of application. For details, see {@link NetworkAccessPolicy}.
958      * @param reconfirmFlag true means a reconfirm diaglog trigger while policy deny network access.
959      * @param isBroker true means the broker application.
960      * @return return 0 if OK, return error number if not OK
961      */
962     int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag, bool isBroker);
963 
964     int32_t NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes);
965     int32_t DeleteNetworkAccessPolicy(uint32_t uid);
966 
967     int32_t StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr);
968     int32_t StopClat(const std::string &interfaceName);
969     int32_t ClearFirewallAllRules();
970 
971     /**
972      * Set NIC Traffic allowed or disallowed
973      *
974      * @param ifaceNames ifaceNames
975      * @param status true for allowed, false for disallowed
976      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
977      */
978     int32_t SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status);
979 private:
980     void ProcessDhcpResult(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult);
981     void ProcessBandwidthReachedLimit(const std::string &limitName, const std::string &iface);
982     sptr<OHOS::NetsysNative::INetsysService> GetProxy();
983     void OnRemoteDied(const wptr<IRemoteObject> &remote);
984 
985     void RegisterNotifyCallback();
986 
987 private:
988     sptr<OHOS::NetsysNative::INotifyCallback> nativeNotifyCallback_ = nullptr;
989     sptr<OHOS::NetsysNative::INetDnsResultCallback> nativeDnsReportCallback_ = nullptr;
990     uint32_t dnsReportTimeStep = 500;
991     sptr<OHOS::NetsysNative::INetsysService> netsysNativeService_ = nullptr;
992     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
993     std::list<sptr<NetsysControllerCallback>> cbObjects_;
994     std::list<sptr<NetsysDnsReportCallback>> cbDnsReportObjects_;
995     std::mutex mutex_;
996     std::mutex cbObjMutex_;
997     std::mutex cbDnsReportObjMutex_;
998 
999 private:
1000     class NetNativeConnDeathRecipient : public IRemoteObject::DeathRecipient {
1001     public:
NetNativeConnDeathRecipient(NetsysNativeClient & client)1002         explicit NetNativeConnDeathRecipient(NetsysNativeClient &client) : client_(client) {}
1003         ~NetNativeConnDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)1004         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
1005         {
1006             client_.OnRemoteDied(remote);
1007         }
1008 
1009     private:
1010         NetsysNativeClient &client_;
1011     };
1012 };
1013 } // namespace NetManagerStandard
1014 } // namespace OHOS
1015 #endif // NETSYS_NATIVE_CLIENT_H
1016