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