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 #ifndef OHOS_ADDRESS_IPV4_H 16 #define OHOS_ADDRESS_IPV4_H 17 18 #include <cstddef> 19 #include <string> 20 #include <netinet/in.h> 21 #include "base_address.h" 22 23 constexpr int MAX_MASK_LENGTH = 32; 24 25 namespace OHOS { 26 namespace Wifi { 27 class Ipv4Address : public BaseAddress { 28 public: 29 /** 30 * @Description Check whether an IPv4 address is a valid IPv4 address. 31 * 32 * @param None 33 * @return true - legal false - illegal 34 */ 35 static bool IsValidIPv4(const std::string &ipv4); 36 37 /** 38 * @Description Factory method for generating an object from Ipv4Address 39 * 40 * @param IPv4 - Character description, for example, 192.168.1.1 [input] 41 * @param prefixLength - Number of bits 1 in the mask. [input] 42 For example, if the mask is 255.255.255.0, the value is 24. 43 * @return Ipv4Address - Create the generated IPv4Address object. 44 */ 45 static Ipv4Address Create(const std::string &ipv4, size_t prefixLength); 46 47 /** 48 * @Description Factory method for generating an object from Ipv4Address 49 * 50 * @param IPv4 - Character description, for example, 192.168.1.1 [input] 51 * @param Mask - IPv4 mask, for example, 255.255.255.0 [input] 52 * @return Ipv4Address - Create the generated IPv4Address object. 53 */ 54 static Ipv4Address Create(const std::string &ipv4, const std::string &mask); 55 56 /** 57 * @Description Factory method for generating an object from Ipv4Address 58 * 59 * @param IPv4 - IPv4 address description of the in_addr type [input] 60 * @param Mask - IPv4 mask description of the in_addr type [input] 61 * @return Ipv4Address - Create the generated IPv4Address object. 62 */ 63 static Ipv4Address Create(const in_addr &ipv4, const in_addr &mask); 64 65 public: 66 static const Ipv4Address invalidInetAddress; /* Invalid IPv4 object constant. */ 67 static const Ipv4Address defaultInetAddress; 68 static const Ipv4Address conflictInetAddress; 69 70 public: 71 /** 72 * @Description Use Default Destruction 73 * 74 * @param None 75 * @return None 76 */ 77 virtual ~Ipv4Address() = default; 78 79 /** 80 * @Description Check whether the current IP address is valid. 81 * 82 * @param None 83 * @return true - legal false - illegal 84 */ 85 bool IsValid() const override; 86 /** 87 * @Description Obtains an IPv4 address of the in_addr type. 88 * 89 * @param None 90 * @return in_addr - IPv4 address of the in_addr type 91 */ 92 in_addr GetAddressWithInet() const; 93 94 /** 95 * @Description Obtains the subnet mask of the IPv4 character description. 96 * 97 * @param None 98 * @return string - Subnet mask described by IPv4 characters 99 */ 100 std::string GetMaskWithString() const; 101 102 /** 103 * @Description Obtains the subnet mask of the IPv4 in_addr type. 104 * 105 * @param None 106 * @return in_addr - Subnet mask described by IPv4 characters 107 */ 108 in_addr GetMaskWithInet() const; 109 110 /** 111 * @Description Obtains the network ID of the current IP address. 112 * 113 * @param None 114 * @return string - Description of the string type of the network code. 115 */ 116 std::string GetNetworkAddressWithString() const; 117 118 /** 119 * @Description Obtains the network ID of the in_addr type of the current IP address. 120 * 121 * @param None 122 * @return in_addr - Description of the in_addr type of the network code 123 */ 124 in_addr GetNetworkAddressWithInet() const; 125 126 /** 127 * @Description Obtains the host ID of the string type of the current IP address. 128 * 129 * @param None 130 * @return string - Description of the string type of the host ID 131 */ 132 std::string GetHostAddressWithString() const; 133 134 /** 135 * @Description Obtains the host ID of the in_addr type of the current IP address. 136 * 137 * @param None 138 * @return in_addr - Description of the in_addr type of the host ID 139 */ 140 in_addr GetHostAddressWithInet() const; 141 142 /** 143 * @Description Obtain the description of the network where the current IP address is located. For 144 example, if the description is 192.168.1.1/24, the description is 192.168.1.0/24. 145 * 146 * @param None 147 * @return string - Description of the in_addr type of the host ID 148 */ 149 std::string GetNetwork() const; 150 151 private: 152 /** 153 * @Description Delete Default Construct 154 * 155 * @param None 156 * @return None 157 */ 158 Ipv4Address() = delete; 159 160 /** 161 * @Description Unique constructor, private. Use the factory mode create. 162 * 163 * @param IPv4 - IPv4 address in character format [input] 164 * @param prefixLength - Number of bits whose mask is 1, that is, the prefix length [input] 165 * @return Ipv4Address object 166 */ 167 Ipv4Address(const std::string &ipv4, size_t prefixLength); 168 }; 169 } // namespace Wifi 170 } // namespace OHOS 171 172 #endif /* OHOS_ADDRESS_IPV4_H */ 173