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