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