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 INCLUDE_INTERFACE_MANAGER_H 17 #define INCLUDE_INTERFACE_MANAGER_H 18 19 #include "interface_type.h" 20 #include <ostream> 21 #include <string> 22 #include <vector> 23 #include "net/if_arp.h" 24 25 namespace OHOS { 26 namespace nmd { 27 static const uint32_t INTERFACE_ERR_MAX_LEN = 256; 28 29 class InterfaceManager { 30 public: 31 InterfaceManager() = default; 32 ~InterfaceManager() = default; 33 /** 34 * Set network device mtu 35 * 36 * @param interfaceName Network device name 37 * @param mtuValue Value of mtu 38 * @return Returns 0, set network device mtu successfully, otherwise it will fail 39 */ 40 static int SetMtu(const char *interfaceName, const char *mtuValue); 41 42 /** 43 * Get network device mtu 44 * 45 * @param interfaceName Network device name 46 * @return Returns value of mtu 47 */ 48 static int GetMtu(const char *interfaceName); 49 50 /** 51 * Add local IP address to network 52 * 53 * @param interfaceName Network device name 54 * @param addr Network IP address 55 * @param prefixLen Length of the network number of the subnet mask 56 * @return Returns 0, add local IP address to network successfully, otherwise it will fail 57 */ 58 static int AddAddress(const char *interfaceName, const char *addr, int prefixLen); 59 60 /** 61 * Delete local IP address to network 62 * 63 * @param interfaceName Network device name 64 * @param addr Network IP address 65 * @param prefixLen Length of the network number of the subnet mask 66 * @return Returns 0, delete local IP address to network successfully, otherwise it will fail 67 */ 68 static int DelAddress(const char *interfaceName, const char *addr, int prefixLen); 69 70 /** 71 * Get the network interface names 72 * 73 * @return Network interface names 74 */ 75 static std::vector<std::string> GetInterfaceNames(); 76 77 /** 78 * Get the network interface config 79 * 80 * @param ifName Network device name 81 * @return Interface configuration parcel 82 */ 83 static InterfaceConfigurationParcel GetIfaceConfig(const std::string &ifName); 84 85 /** 86 * Set network interface config 87 * 88 * @param ifaceConfig Interface configuration parcel 89 * @return Returns 1, set network interface config successfully, otherwise it will fail 90 */ 91 static int SetIfaceConfig(const nmd::InterfaceConfigurationParcel &ifaceConfig); 92 93 /** 94 * Set network interface ip address 95 * 96 * @param ifaceName Network port device name 97 * @param ipAddress Ip address 98 * @return Returns 0, set IP address to network successfully, otherwise it will fail 99 */ 100 static int SetIpAddress(const std::string &ifaceName, const std::string &ipAddress); 101 102 /** 103 * Set iface up 104 * 105 * @param ifaceName Network port device name 106 * @return Returns 0, set up to network successfully, otherwise it will fail 107 */ 108 static int SetIffUp(const std::string &ifaceName); 109 static int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, 110 const std::string &ifName); 111 static int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, 112 const std::string &ifName); 113 114 private: 115 static int ModifyAddress(uint32_t action, const char *interfaceName, const char *addr, int prefixLen); 116 static int32_t AssembleArp(const std::string &ipAddr, const std::string &macAddr, 117 const std::string &ifName, arpreq &req); 118 static int32_t MacStringToArray(const std::string &macAddr, sockaddr &macSock); 119 }; 120 } // namespace nmd 121 } // namespace OHOS 122 #endif // INCLUDE_INTERFACE_MANAGER_H 123