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