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