1 /* 2 * Copyright (c) 2021 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(); 28 ~NetsysControllerServiceImpl(); 29 void Init() override; 30 31 /** 32 * @brief 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 * @brief 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 * @brief 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 * @brief 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 * @brief 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 * @brief 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 Turn off the device 92 * 93 * @param iface Network port device name 94 * @return Return the result of this action 95 */ 96 int32_t SetInterfaceDown(const std::string &iface) override; 97 98 /** 99 * @brief Turn on the device 100 * 101 * @param iface Network port device name 102 * @return Return the result of this action 103 */ 104 int32_t SetInterfaceUp(const std::string &iface) override; 105 106 /** 107 * @brief Clear the network interface ip address 108 * 109 * @param ifName Network port device name 110 */ 111 void InterfaceClearAddrs(const std::string &ifName) override; 112 113 /** 114 * @brief Obtain mtu from the network interface device 115 * 116 * @param ifName Network port device name 117 * @return Return the return value of the netsys interface call 118 */ 119 int32_t InterfaceGetMtu(const std::string &ifName) override; 120 121 /** 122 * @brief Set mtu to network interface device 123 * 124 * @param ifName Network port device name 125 * @param mtu 126 * @return Return the return value of the netsys interface call 127 */ 128 int32_t InterfaceSetMtu(const std::string &ifName, int32_t mtu) override; 129 130 /** 131 * @brief Add ip address 132 * 133 * @param ifName Network port device name 134 * @param ipAddr ip address 135 * @param prefixLength subnet mask 136 * @return Return the return value of the netsys interface call 137 */ 138 int32_t InterfaceAddAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength) override; 139 140 /** 141 * @brief Delete ip address 142 * 143 * @param ifName Network port device name 144 * @param ipAddr ip address 145 * @param prefixLength subnet mask 146 * @return Return the return value of the netsys interface call 147 */ 148 int32_t InterfaceDelAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength) override; 149 150 /** 151 * @brief Set dns 152 * 153 * @param netId 154 * @param baseTimeoutMsec 155 * @param retryCount 156 * @param servers 157 * @param domains 158 * @return Return the return value of the netsys interface call 159 */ 160 int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, 161 const std::vector<std::string> &servers, const std::vector<std::string> &domains) override; 162 163 /** 164 * @brief Get dns server param info 165 * 166 * @param netId 167 * @param servers 168 * @param domains 169 * @param baseTimeoutMsec 170 * @param retryCount 171 * @return Return the return value of the netsys interface call 172 */ 173 int32_t GetResolverInfo(uint16_t netId, std::vector<std::string> &servers, 174 std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount) override; 175 176 /** 177 * @brief Create dns cache before set dns 178 * 179 * @param netId 180 * @return Return the return value for status of call 181 */ 182 int32_t CreateNetworkCache(uint16_t netId) override; 183 184 /** 185 * @brief Destroy dns cache 186 * 187 * @param netId 188 * @return Return the return value of the netsys interface call 189 */ 190 int32_t DestroyNetworkCache(uint16_t netId) override; 191 192 /** 193 * @brief Flush dns cache 194 * 195 * @param netId 196 * @return Return the return value of the netsys interface call 197 */ 198 int32_t FlushNetworkCache(uint16_t netId) override; 199 200 /** 201 * @brief Domain name resolution Obtains the domain name address 202 * 203 * @param hostName 204 * @param serverName 205 * @param hints 206 * @param res 207 * @return Return the return value of the netsys interface call 208 */ 209 int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, 210 const struct addrinfo &hints, std::unique_ptr<addrinfo> &res, uint16_t netId) override; 211 212 /** 213 * @brief Obtains the bytes received over the cellular network. 214 * 215 * @return The number of received bytes. 216 */ 217 int64_t GetCellularRxBytes() override; 218 219 /** 220 * @brief Obtains the bytes sent over the cellular network. 221 * 222 * @return The number of sent bytes. 223 */ 224 int64_t GetCellularTxBytes() override; 225 226 /** 227 * @brief Obtains the bytes received through all NICs. 228 * 229 * @return The number of received bytes. 230 */ 231 int64_t GetAllRxBytes() override; 232 233 /** 234 * @brief Obtains the bytes sent through all NICs. 235 * 236 * @return The number of sent bytes. 237 */ 238 int64_t GetAllTxBytes() override; 239 240 /** 241 * @brief Obtains the bytes received through a specified UID. 242 * 243 * @param uid app id. 244 * @return The number of received bytes. 245 */ 246 int64_t GetUidRxBytes(uint32_t uid) override; 247 248 /** 249 * @brief Obtains the bytes sent through a specified UID. 250 * 251 * @param uid app id. 252 * @return The number of sent bytes. 253 */ 254 int64_t GetUidTxBytes(uint32_t uid) override; 255 256 /** 257 * @brief Obtains the bytes received through a specified UID on Iface. 258 * 259 * @param uid app id. 260 * @param iface The name of the interface. 261 * @return The number of received bytes. 262 */ 263 int64_t GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName) override; 264 265 /** 266 * @brief Obtains the bytes sent through a specified UID on Iface. 267 * 268 * @param uid app id. 269 * @param iface The name of the interface. 270 * @return The number of sent bytes. 271 */ 272 int64_t GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName) override; 273 274 /** 275 * @brief Obtains the bytes received through a specified NIC. 276 * 277 * @param iface The name of the interface. 278 * @return The number of received bytes. 279 */ 280 int64_t GetIfaceRxBytes(const std::string &interfaceName) override; 281 282 /** 283 * @brief Obtains the bytes sent through a specified NIC. 284 * 285 * @param iface The name of the interface. 286 * @return The number of sent bytes. 287 */ 288 int64_t GetIfaceTxBytes(const std::string &interfaceName) override; 289 290 /** 291 * @brief Obtains the NIC list. 292 * 293 * @return The list of interface. 294 */ 295 std::vector<std::string> InterfaceGetList() override; 296 297 /** 298 * @brief Obtains the uid list. 299 * 300 * @return The list of uid. 301 */ 302 std::vector<std::string> UidGetList() override; 303 304 /** 305 * @brief Obtains the packets received through a specified NIC. 306 * 307 * @param iface The name of the interface. 308 * @return The number of received packets. 309 */ 310 int64_t GetIfaceRxPackets(const std::string &interfaceName) override; 311 312 /** 313 * @brief Obtains the packets sent through a specified NIC. 314 * 315 * @param iface The name of the interface. 316 * @return The number of sent packets. 317 */ 318 int64_t GetIfaceTxPackets(const std::string &interfaceName) override; 319 320 /** 321 * @brief set default network. 322 * 323 * @return Return the return value of the netsys interface call 324 */ 325 int32_t SetDefaultNetWork(int32_t netId) override; 326 327 /** 328 * @brief clear default network netId. 329 * 330 * @return Return the return value of the netsys interface call 331 */ 332 int32_t ClearDefaultNetWorkNetId() override; 333 334 /** 335 * @brief Obtains the NIC list. 336 * 337 * @param socket_fd 338 * @param netId 339 * @return Return the return value of the netsys interface call 340 */ 341 int32_t BindSocket(int32_t socket_fd, uint32_t netId) override; 342 343 /** 344 * @brief Enable ip forwarding. 345 * 346 * @param requester the requester of forwarding 347 * @return Return the return value of the netsys interface call. 348 */ 349 int32_t IpEnableForwarding(const std::string& requester) override; 350 351 /** 352 * @brief Disable ip forwarding. 353 * 354 * @param requester the requester of forwarding 355 * @return Return the return value of the netsys interface call. 356 */ 357 int32_t IpDisableForwarding(const std::string& requester) override; 358 359 /** 360 * @brief Add tether forward. 361 * 362 * @param downstreamIface the name of downstream interface 363 * @param upstreamIface the name of upstream interface 364 * @return Return the return value of the netsys interface call. 365 */ 366 int32_t TetherAddForward(const std::string& downstreamIface, const std::string& upstreamIface) override; 367 368 /** 369 * @brief Remove tether forward. 370 * 371 * @param downstreamIface the name of downstream interface 372 * @param upstreamIface the name of upstream interface 373 * @return Return the return value of the netsys interface call. 374 */ 375 int32_t TetherRemoveForward(const std::string& downstreamIface, const std::string& upstreamIface) override; 376 377 /** 378 * @brief Add interface forward. 379 * 380 * @param fromIface the name of incoming interface 381 * @param toIface the name of outcoming interface 382 * @return Return the return value of the netsys interface call. 383 */ 384 int32_t IpfwdAddInterfaceForward(const std::string& fromIface, const std::string& toIface) override; 385 386 /** 387 * @brief Remove interface forward. 388 * 389 * @param fromIface the name of incoming interface 390 * @param toIface the name of outcoming interface 391 * @return Return the return value of the netsys interface call. 392 */ 393 int32_t IpfwdRemoveInterfaceForward(const std::string& fromIface, const std::string& toIface) override; 394 395 /** 396 * @brief Set tether dns. 397 * 398 * @param netId network id 399 * @param dnsAddr the list of dns address 400 * @return Return the return value of the netsys interface call. 401 */ 402 int32_t TetherDnsSet(uint32_t netId, const std::vector<std::string>& dnsAddrs) override; 403 404 /** 405 * @brief Set net callbackfuction. 406 * 407 * @param callback callbackfuction class 408 * @return Return the return value of the netsys interface call. 409 */ 410 int32_t RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback) override; 411 412 /** 413 * @brief protect tradition network to connect VPN. 414 * 415 * @param socketFd socket file description 416 * @return Return the return value of the netsys interface call. 417 */ 418 int32_t BindNetworkServiceVpn(int32_t socketFd) override; 419 420 /** 421 * @brief enable virtual network iterface card. 422 * 423 * @param socketFd socket file description 424 * @param ifRequest interface request 425 * @return Return the return value of the netsys interface call. 426 */ 427 int32_t EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd) override; 428 429 /** 430 * @brief Set ip address. 431 * 432 * @param socketFd socket file description 433 * @param ipAddress ip address 434 * @param prefixLen the mask of ip address 435 * @param ifRequest interface request 436 * @return Return the return value of the netsys interface call. 437 */ 438 int32_t SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen, 439 struct ifreq &ifRequest) override; 440 441 /** 442 * @brief Set network blocking. 443 * 444 * @param ifaceFd interface file description 445 * @param isBlock network blocking 446 * @return Return the return value of the netsys interface call. 447 */ 448 int32_t SetBlocking(int32_t ifaceFd, bool isBlock) override; 449 /** 450 * @brief Start Dhcp Client. 451 * 452 * @param iface interface file description 453 * @param bIpv6 network blocking 454 * @return. 455 */ 456 int32_t StartDhcpClient(const std::string &iface, bool bIpv6) override; 457 /** 458 * @brief Stop Dhcp Client. 459 * 460 * @param iface interface file description 461 * @param bIpv6 network blocking 462 * @return . 463 */ 464 int32_t StopDhcpClient(const std::string &iface, bool bIpv6) override; 465 /** 466 * @brief Register Notify Callback 467 * 468 * @param callback 469 * @return . 470 */ 471 int32_t RegisterCallback(sptr<NetsysControllerCallback> callback) override; 472 473 /** 474 * @brief start dhcpservice. 475 * 476 * @param iface interface name 477 * @param ipv4addr ipv4 addr 478 * @return Return the return value of the netsys interface call. 479 */ 480 int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr) override; 481 482 /** 483 * @brief stop dhcpservice. 484 * 485 * @param iface interface name 486 * @return Return the return value of the netsys interface call. 487 */ 488 int32_t StopDhcpService(const std::string &iface) override; 489 490 private: 491 MockNetsysNativeClient mockNetsysClient_; 492 NetsysNativeClient netsysClient_; 493 }; 494 } // namespace NetManagerStandard 495 } // namespace OHOS 496 #endif // NETSYS_CONTROLLER_SERVICE_IMPL_H