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