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 OHOS_NETWORK_INTERFACE_H 17 #define OHOS_NETWORK_INTERFACE_H 18 19 #include <ifaddrs.h> 20 #include <string> 21 #include <vector> 22 #include <ifaddrs.h> 23 24 #include "ipv4_address.h" 25 #include "ipv6_address.h" 26 27 namespace OHOS { 28 namespace Wifi { 29 namespace NetworkInterface { 30 /** 31 * @Description Check whether the network interface name is valid. 32 * 33 * @param interfaceName - Network Interface Name 34 * @return true - legal false - illegal 35 */ 36 bool IsValidInterfaceName(const std::string &interfaceName); 37 38 /** 39 * @Description Print all members in an object 40 * 41 * @param interfaceName - Network Interface Name 42 * @return None 43 */ 44 void Dump(const std::string &interfaceName); 45 46 /** 47 * @Description Obtain the current configuration from the network interface name. 48 * 49 * @param interfaceName - Network Interface Name [input] 50 * @param ipv4 - ipv4 address of network interface [output] 51 * @param vecIPv6 - the vector of ipv6 addresses of network [output] 52 * @return true - fetch success false - fetch failed 53 */ 54 bool FetchInterfaceConfig( 55 const std::string &interfaceName, std::vector<Ipv4Address> &vecIPv4, std::vector<Ipv6Address> &vecIPv6); 56 57 /** 58 * @Description Obtains the IPv4 address object of the current network interface. 59 * 60 * @param interfaceName - Network Interface Name [input] 61 * @param vecIPv4 - the vector of address of the network interface interface. If 62 the IPv4 address fails to be obtained, an invalid IPv4 63 ddress is returned. [output] 64 * @return true - success false - failed 65 */ 66 bool GetIpv4Address(const std::string &interfaceName, std::vector<Ipv4Address> &ipv4); 67 68 /** 69 * @Description Obtains all IPv6 addresses of a network interface. 70 * 71 * @param interfaceName - Network Interface Name [input] 72 * @param vecIPv6 - All IPv6 addresses configured for the current 73 network interface [output] 74 * @return true - success false - failed 75 */ 76 bool GetAllIpv6Address(const std::string &interfaceName, std::vector<Ipv6Address> &vecIPv6); 77 78 /** 79 * @Description Whether an address exists in the network interface. 80 * 81 * @param interfaceName - Network Interface Name [input] 82 * @param address - Specifies the IPv4 or IPv6 address to be queried. [input] 83 * @return true - exist false - no exist 84 */ 85 bool IsExistAddressForInterface(const std::string &interfaceName, const BaseAddress &address); 86 87 /** 88 * @Description Set the IPv4 or IPv6 address to the network interface. 89 * 90 * @param interfaceName - Network Interface Name [input] 91 * @param ipAddress - Specifies the IPv4 or IPv6 address to be added. [input] 92 * @return true - success false - failed 93 */ 94 bool AddIpAddress(const std::string &interfaceName, const BaseAddress &ipAddress); 95 96 /** 97 * @Description Deletes information about a specified IPv4 or IPv6 98 address from a network interface. 99 * 100 * @param interfaceName - NetworkInterface Name [input] 101 * @param ipAddress - Specifies the IPv4 or IPv6 address to be deleted. [input] 102 * @return true - success false - failed 103 */ 104 bool DelIpAddress(const std::string &interfaceName, const BaseAddress &ipAddress); 105 106 /** 107 * @Description Clears all IPv4 or IPv6 addresses except local link 108 addresses starting with "fe80:" 109 * 110 * @param interfaceName - NetworkInterface Name [input] 111 * @return true - clear success false - clear failed 112 */ 113 bool ClearAllIpAddress(const std::string &interfaceName); 114 115 /** 116 * @Description obtain IPv4 and IPv6 address from interfaceName 117 * 118 * @param interfaceName - Network Interface Name [input] 119 * @param ipv4 - IPv4 address. [output] 120 * @param vecIPv6 - IPv6 address. [output] 121 * @return true - success false - failed 122 */ 123 bool FetchIpAddress( 124 const std::string &interfaceName, std::vector<Ipv4Address> &vecipv4, std::vector<Ipv6Address> &vecIPv6); 125 126 /** 127 * @Description obtain IPv4 and IPv6 address of Ap and P2p from interfaceName 128 * 129 * @param interfaceName - Network Interface Name [input] 130 * @param ipv4 - IPv4 address. [output] 131 * @param vecIPv6 - IPv6 address. [output] 132 * @return true - success false - failed 133 */ 134 bool FetchApOrP2pIpAddress( 135 const std::string &interfaceName, std::vector<Ipv4Address> &vecipv4, std::vector<Ipv6Address> &vecIPv6); 136 137 /** 138 * @Description Saves the IP address obtained through ifaddrs. 139 * 140 * @param ifa - Source address. [input] 141 * @param vecIPv4 - IPv4 address. [output] 142 * @param vecIPv6 - IPv6 address. [output] 143 * @return true - success false - failed 144 */ 145 bool SaveIpAddress(const struct ifaddrs &ifa, std::vector<Ipv4Address> &vecIPv4, std::vector<Ipv6Address> &vecIPv6); 146 147 /** 148 * @Description obtain IPv4 and IPv6 address from interfaceName 149 * 150 * @param interfaceName - Network Interface Name. [input] 151 * @param ipAddress - IP address. [input] 152 * @param action - true - add ip flase - del app [input] 153 * @param dad - duplicate address detection,default disable [input] 154 * @return true - success false - fail 155 */ 156 bool IpAddressChange(const std::string &interface, const BaseAddress &ipAddress, bool action, bool dad = false); 157 /** 158 * @Description Write data to a file. 159 * @param fileName - file name 160 * @param content - Character data to be written 161 * @return true: success false: fail 162 */ 163 bool WriteDataToFile(const std::string &fileName, const std::string &content); 164 }; /* namespace NetworkInterface */ 165 } // namespace Wifi 166 } // namespace OHOS 167 168 #endif /* OHOS_NETWORK_INTERFACE_H */