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