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