• 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 "netsys_controller_callback.h"
27 #include "netsys_controller_define.h"
28 #include "network_sharing.h"
29 #include "notify_callback_stub.h"
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
33 class NetsysNativeClient {
34 private:
35     class NativeNotifyCallback : public OHOS::NetsysNative::NotifyCallbackStub {
36     public:
37         NativeNotifyCallback(NetsysNativeClient &netsysNativeClient);
38         ~NativeNotifyCallback() override = default;
39         int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
40                                           int scope) override;
41         int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
42                                           int scope) override;
43         int32_t OnInterfaceAdded(const std::string &ifName) override;
44         int32_t OnInterfaceRemoved(const std::string &ifName) override;
45         int32_t OnInterfaceChanged(const std::string &ifName, bool up) override;
46         int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
47         int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
48                                const std::string &ifName) override;
49         int32_t OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult) override;
50         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
51 
52     private:
53         NetsysNativeClient &netsysNativeClient_;
54     };
55 
56 public:
57     NetsysNativeClient();
58     ~NetsysNativeClient() = default;
59 
60     /**
61      * Disallow or allow a app to create AF_INET or AF_INET6 socket
62      *
63      * @param uid App's uid which need to be disallowed ot allowed to create AF_INET or AF_INET6 socket
64      * @param allow 0 means disallow, 1 means allow
65      * @return return 0 if OK, return error number if not OK
66      */
67     int32_t SetInternetPermission(uint32_t uid, uint8_t allow);
68 
69     /**
70      * Create a physical network
71      *
72      * @param netId
73      * @param permission Permission to create a physical network
74      * @return Return the return value of the netsys interface call
75      */
76     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission);
77 
78     int32_t NetworkCreateVirtual(int32_t netId, bool hasDns);
79     int32_t NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges);
80     int32_t NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges);
81 
82     /**
83      * Destroy the network
84      *
85      * @param netId
86      * @return Return the return value of the netsys interface call
87      */
88     int32_t NetworkDestroy(int32_t netId);
89 
90     /**
91      * Add network port device
92      *
93      * @param netId
94      * @param iface Network port device name
95      * @return Return the return value of the netsys interface call
96      */
97     int32_t NetworkAddInterface(int32_t netId, const std::string &iface);
98 
99     /**
100      * Delete network port device
101      *
102      * @param netId
103      * @param iface Network port device name
104      * @return Return the return value of the netsys interface call
105      */
106     int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface);
107 
108     /**
109      * Add route
110      *
111      * @param netId
112      * @param ifName Network port device name
113      * @param destination Target host ip
114      * @param nextHop Next hop address
115      * @return Return the return value of the netsys interface call
116      */
117     int32_t NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination,
118                             const std::string &nextHop);
119 
120     /**
121      * Remove route
122      *
123      * @param netId
124      * @param ifName Network port device name
125      * @param destination Target host ip
126      * @param nextHop Next hop address
127      * @return Return the return value of the netsys interface call
128      */
129     int32_t NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination,
130                                const std::string &nextHop);
131 
132     /**
133      * @brief Get interface config
134      *
135      * @param iface Network port device name
136      * @return Return the result of this action, ERR_NONE is success.
137      */
138     int32_t GetInterfaceConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg);
139 
140     /**
141      * @brief Set interface config
142      *
143      * @param cfg Network port info
144      * @return Return the result of this action, ERR_NONE is success.
145      */
146     int32_t SetInterfaceConfig(const OHOS::nmd::InterfaceConfigurationParcel &cfg);
147 
148     /**
149      * Turn off the device
150      *
151      * @param iface Network port device name
152      * @return Return the result of this action
153      */
154     int32_t SetInterfaceDown(const std::string &iface);
155 
156     /**
157      * Turn on the device
158      *
159      * @param iface Network port device name
160      * @return Return the result of this action
161      */
162     int32_t SetInterfaceUp(const std::string &iface);
163 
164     /**
165      * Clear the network interface ip address
166      *
167      * @param ifName Network port device name
168      */
169     void ClearInterfaceAddrs(const std::string &ifName);
170 
171     /**
172      * Obtain mtu from the network interface device
173      *
174      * @param ifName Network port device name
175      * @return Return the return value of the netsys interface call
176      */
177     int32_t GetInterfaceMtu(const std::string &ifName);
178 
179     /**
180      * Set mtu to network interface device
181      *
182      * @param ifName Network port device name
183      * @param mtu
184      * @return Return the return value of the netsys interface call
185      */
186     int32_t SetInterfaceMtu(const std::string &ifName, int32_t mtu);
187 
188     /**
189      * Add ip address
190      *
191      * @param ifName Network port device name
192      * @param ipAddr    ip address
193      * @param prefixLength  subnet mask
194      * @return Return the return value of the netsys interface call
195      */
196     int32_t AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength);
197 
198     /**
199      * Delete ip address
200      *
201      * @param ifName Network port device name
202      * @param ipAddr ip address
203      * @param prefixLength subnet mask
204      * @return Return the return value of the netsys interface call
205      */
206     int32_t DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength);
207 
208     /**
209      * Set iface ip address
210      *
211      * @param ifaceName Network port device name
212      * @param ipAddress Ip address
213      * @return Return the return value of the netsys interface call
214      */
215     int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress);
216 
217     /**
218      * Set iface up
219      *
220      * @param ifaceName Network port device name
221      * @return Return the return value of the netsys interface call
222      */
223     int32_t InterfaceSetIffUp(const std::string &ifaceName);
224 
225     /**
226      * Set dns
227      *
228      * @param netId
229      * @param baseTimeoutMsec
230      * @param retryCount
231      * @param servers
232      * @param domains
233      * @return Return the return value of the netsys interface call
234      */
235     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
236                               const std::vector<std::string> &servers, const std::vector<std::string> &domains);
237 
238     /**
239      * Get dns server param info
240      *
241      * @param netId
242      * @param servers
243      * @param domains
244      * @param baseTimeoutMsec
245      * @param retryCount
246      * @return Return the return value of the netsys interface call
247      */
248     int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
249                               uint16_t &baseTimeoutMsec, uint8_t &retryCount);
250 
251     /**
252      * Create dns cache before set dns
253      *
254      * @param netId
255      * @return Return the return value for status of call
256      */
257     int32_t CreateNetworkCache(uint16_t netId);
258 
259     /**
260      * Destroy dns cache
261      *
262      * @param netId
263      * @return Return the return value of the netsys interface call
264      */
265     int32_t DestroyNetworkCache(uint16_t netId);
266 
267     /**
268      * Domain name resolution Obtains the domain name address
269      *
270      * @param hostName Domain name to be resolved
271      * @param serverName Server name used for query
272      * @param hints Limit parameters when querying
273      * @param netId Network id
274      * @param res return addrinfo
275      * @return Return the return value of the netsys interface call
276      */
277     int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
278                         uint16_t netId, std::vector<AddrInfo> &res);
279 
280     /**
281      * Obtains the bytes of the sharing network.
282      *
283      * @return Success return 0.
284      */
285     int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
286                                      nmd::NetworkSharingTraffic &traffic);
287 
288     /**
289      * Obtains the bytes received over the cellular network.
290      *
291      * @return The number of received bytes.
292      */
293     int64_t GetCellularRxBytes();
294 
295     /**
296      * Obtains the bytes sent over the cellular network.
297      *
298      * @return The number of sent bytes.
299      */
300     int64_t GetCellularTxBytes();
301 
302     /**
303      * Obtains the bytes received through all NICs.
304      *
305      * @return The number of received bytes.
306      */
307     int64_t GetAllRxBytes();
308 
309     /**
310      * Obtains the bytes sent through all NICs.
311      *
312      * @return The number of sent bytes.
313      */
314     int64_t GetAllTxBytes();
315 
316     /**
317      * Obtains the bytes received through a specified UID.
318      *
319      * @param uid app id.
320      * @return The number of received bytes.
321      */
322     int64_t GetUidRxBytes(uint32_t uid);
323 
324     /**
325      * Obtains the bytes sent through a specified UID.
326      *
327      * @param uid app id.
328      * @return The number of sent bytes.
329      */
330     int64_t GetUidTxBytes(uint32_t uid);
331 
332     /**
333      * Obtains the bytes received through a specified UID on Iface.
334      *
335      * @param uid app id.
336      * @param iface The name of the interface.
337      * @return The number of received bytes.
338      */
339     int64_t GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName);
340 
341     /**
342      * Obtains the bytes sent through a specified UID on Iface.
343      *
344      * @param uid app id.
345      * @param iface The name of the interface.
346      * @return The number of sent bytes.
347      */
348     int64_t GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName);
349 
350     /**
351      * Obtains the bytes received through a specified NIC.
352      *
353      * @param iface The name of the interface.
354      * @return The number of received bytes.
355      */
356     int64_t GetIfaceRxBytes(const std::string &interfaceName);
357 
358     /**
359      * Obtains the bytes sent through a specified NIC.
360      *
361      * @param iface The name of the interface.
362      * @return The number of sent bytes.
363      */
364     int64_t GetIfaceTxBytes(const std::string &interfaceName);
365 
366     /**
367      * Obtains the NIC list.
368      *
369      * @return The list of interface.
370      */
371     std::vector<std::string> InterfaceGetList();
372 
373     /**
374      * Obtains the uid list.
375      *
376      * @return The list of uid.
377      */
378     std::vector<std::string> UidGetList();
379 
380     /**
381      * Obtains the packets received through a specified NIC.
382      *
383      * @param iface The name of the interface.
384      * @return The number of received packets.
385      */
386     int64_t GetIfaceRxPackets(const std::string &interfaceName);
387 
388     /**
389      * Obtains the packets sent through a specified NIC.
390      *
391      * @param iface The name of the interface.
392      * @return The number of sent packets.
393      */
394     int64_t GetIfaceTxPackets(const std::string &interfaceName);
395 
396     /**
397      *  set default network.
398      *
399      * @return Return the return value of the netsys interface call
400      */
401     int32_t SetDefaultNetWork(int32_t netId);
402 
403     /**
404      * clear default network netId.
405      *
406      * @return Return the return value of the netsys interface call
407      */
408     int32_t ClearDefaultNetWorkNetId();
409 
410     /**
411      * Obtains the NIC list.
412      *
413      * @param socket_fd
414      * @param netId
415      * @return Return the return value of the netsys interface call
416      */
417     int32_t BindSocket(int32_t socket_fd, uint32_t netId);
418 
419     /**
420      * Enable ip forwarding.
421      *
422      * @param requestor the requestor of forwarding
423      * @return Return the return value of the netsys interface call.
424      */
425     int32_t IpEnableForwarding(const std::string &requestor);
426 
427     /**
428      * Disable ip forwarding.
429      *
430      * @param requestor the requestor of forwarding
431      * @return Return the return value of the netsys interface call.
432      */
433     int32_t IpDisableForwarding(const std::string &requestor);
434 
435     /**
436      * Enable Nat.
437      *
438      * @param downstreamIface the name of downstream interface
439      * @param upstreamIface the name of upstream interface
440      * @return Return the return value of the netsys interface call.
441      */
442     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface);
443     /**
444      * Disable Nat.
445      *
446      * @param downstreamIface the name of downstream interface
447      * @param upstreamIface the name of upstream interface
448      * @return Return the return value of the netsys interface call.
449      */
450     int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface);
451 
452     /**
453      * Add interface forward.
454      *
455      * @param fromIface the name of incoming interface
456      * @param toIface the name of outcoming interface
457      * @return Return the return value of the netsys interface call.
458      */
459     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface);
460 
461     /**
462      * Remove interface forward.
463      *
464      * @param fromIface the name of incoming interface
465      * @param toIface the name of outcoming interface
466      * @return Return the return value of the netsys interface call.
467      */
468     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface);
469 
470     /**
471      * Set tether dns.
472      *
473      * @param netId network id
474      * @param dnsAddr the list of dns address
475      * @return Return the return value of the netsys interface call.
476      */
477     int32_t ShareDnsSet(uint16_t netId);
478 
479     /**
480      * tart dns proxy listen
481      *
482      * @return Return the return value of the netsys interface call.
483      */
484     virtual int32_t StartDnsProxyListen();
485 
486     /**
487      * stop dns proxy listen
488      *
489      * @return Return the return value of the netsys interface call.
490      */
491     virtual int32_t StopDnsProxyListen();
492 
493     /**
494      * Set net callback function.
495      *
496      * @param callback callback function class
497      * @return Return the return value of the netsys interface call.
498      */
499     int32_t RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback);
500 
501     /**
502      * protect tradition network to connect VPN.
503      *
504      * @param socketFd socket file description
505      * @return Return the return value of the netsys interface call.
506      */
507     int32_t BindNetworkServiceVpn(int32_t socketFd);
508 
509     /**
510      * enable virtual network interface card.
511      *
512      * @param socketFd socket file description
513      * @param ifRequest interface request
514      * @return Return the return value of the netsys interface call.
515      */
516     int32_t EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd);
517 
518     /**
519      * Set ip address.
520      *
521      * @param socketFd socket file description
522      * @param ipAddress ip address
523      * @param prefixLen the mask of ip address
524      * @param ifRequest interface request
525      * @return Return the return value of the netsys interface call.
526      */
527     int32_t SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen, struct ifreq &ifRequest);
528 
529     /**
530      * Set network blocking.
531      *
532      * @param ifaceFd interface file description
533      * @param isBlock network blocking
534      * @return Return the return value of the netsys interface call.
535      */
536     int32_t SetBlocking(int32_t ifaceFd, bool isBlock);
537 
538     /**
539      * Start Dhcp Client.
540      *
541      * @param iface interface file description
542      * @param bIpv6 network blocking
543      * @return Return the return value of the netsys interface call.
544      */
545     int32_t StartDhcpClient(const std::string &iface, bool bIpv6);
546 
547     /**
548      * Stop Dhcp Client.
549      *
550      * @param iface interface file description
551      * @param bIpv6 network blocking
552      * @return Return the return value of the netsys interface call.
553      */
554     int32_t StopDhcpClient(const std::string &iface, bool bIpv6);
555 
556     /**
557      * Register Notify Callback
558      *
559      * @param callback
560      * @return Return the return value of the netsys interface call.
561      */
562     int32_t RegisterCallback(const sptr<NetsysControllerCallback> &callback);
563 
564     /**
565      * start dhcpservice.
566      *
567      * @param iface interface name
568      * @param ipv4addr ipv4 addr
569      * @return Return the return value of the netsys interface call.
570      */
571     int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr);
572 
573     /**
574      * stop dhcpservice.
575      *
576      * @param iface interface name
577      * @return Return the return value of the netsys interface call.
578      */
579     int32_t StopDhcpService(const std::string &iface);
580 
581     /**
582      * Turn on data saving mode.
583      *
584      * @param enable enable or disable
585      * @return value the return value of the netsys interface call.
586      */
587     int32_t BandwidthEnableDataSaver(bool enable);
588 
589     /**
590      * Set quota.
591      *
592      * @param iface interface name
593      * @param bytes
594      * @return Return the return value of the netsys interface call.
595      */
596     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes);
597 
598     /**
599      * delete quota.
600      *
601      * @param iface interface name
602      * @return Return the return value of the netsys interface call.
603      */
604     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName);
605 
606     /**
607      * Add DeniedList.
608      *
609      * @param uid
610      * @return Return the return value of the netsys interface call.
611      */
612     int32_t BandwidthAddDeniedList(uint32_t uid);
613 
614     /**
615      * Remove DeniedList.
616      *
617      * @param uid
618      * @return Return the return value of the netsys interface call.
619      */
620     int32_t BandwidthRemoveDeniedList(uint32_t uid);
621 
622     /**
623      * Add DeniedList.
624      *
625      * @param uid
626      * @return Return the return value of the netsys interface call.
627      */
628     int32_t BandwidthAddAllowedList(uint32_t uid);
629 
630     /**
631      * Remove DeniedList.
632      *
633      * @param uid
634      * @return Return the return value of the netsys interface call.
635      */
636     int32_t BandwidthRemoveAllowedList(uint32_t uid);
637 
638     /**
639      * Set firewall rules.
640      *
641      * @param chain chain type
642      * @param isAllowedList is or not AllowedList
643      * @param uids
644      * @return value the return value of the netsys interface call.
645      */
646     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
647 
648     /**
649      * Set firewall rules.
650      *
651      * @param chain chain type
652      * @param isAllowedList is or not AllowedList
653      * @param uids
654      * @return value the return value of the netsys interface call.
655      */
656     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
657 
658     /**
659      * Enable or disable the specified firewall chain.
660      *
661      * @param chain chain type
662      * @param enable enable or disable
663      * @return Return the return value of the netsys interface call.
664      */
665     int32_t FirewallEnableChain(uint32_t chain, bool enable);
666 
667     /**
668      * Firewall set uid rule.
669      *
670      * @param chain chain type
671      * @param uid uid
672      * @param firewallRule firewall rule
673      * @return Return the return value of the netsys interface call.
674      */
675     int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule);
676 
677     /**
678      * Get total traffic
679      *
680      * @param stats stats
681      * @param type type
682      * @return returns the total traffic of the specified type
683      */
684     int32_t GetTotalStats(uint64_t &stats, uint32_t type);
685 
686     /**
687      * Get uid traffic
688      *
689      * @param stats stats
690      * @param type type
691      * @param uid uid
692      * @return returns the traffic of the uid
693      */
694     int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid);
695 
696     /**
697      * Get Iface traffic
698      *
699      * @param stats stats
700      * @param type type
701      * @param interfaceName interfaceName
702      * @return returns the traffic of the Iface
703      */
704     int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName);
705 
706     /**
707      * Get all stats info
708      *
709      * @param stats stats
710      * @return returns the all info of the stats
711      */
712     int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats);
713 
714     /**
715      * Set iptables for result
716      *
717      * @param cmd Iptables command
718      * @param respond The respond of execute iptables command
719      * @return Value the return value of the netsys interface call
720      */
721     int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond);
722 
723 private:
724     void ProcessDhcpResult(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult);
725     void ProcessBandwidthReachedLimit(const std::string &limitName, const std::string &iface);
726     sptr<OHOS::NetsysNative::INetsysService> GetProxy();
727     void OnRemoteDied(const wptr<IRemoteObject> &remote);
728 
729 private:
730     sptr<OHOS::NetsysNative::INotifyCallback> nativeNotifyCallback_ = nullptr;
731     sptr<OHOS::NetsysNative::INetsysService> netsysNativeService_ = nullptr;
732     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
733     std::vector<sptr<NetsysControllerCallback>> cbObjects_;
734     std::mutex mutex_;
735     std::mutex cbObjMutex_;
736 
737 private:
738     class NetNativeConnDeathRecipient : public IRemoteObject::DeathRecipient {
739     public:
NetNativeConnDeathRecipient(NetsysNativeClient & client)740         explicit NetNativeConnDeathRecipient(NetsysNativeClient &client) : client_(client) {}
741         ~NetNativeConnDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)742         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
743         {
744             client_.OnRemoteDied(remote);
745         }
746 
747     private:
748         NetsysNativeClient &client_;
749     };
750 };
751 } // namespace NetManagerStandard
752 } // namespace OHOS
753 #endif // NETSYS_NATIVE_CLIENT_H
754