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