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