• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ipv4_address.h"
17 #include <cstdio>
18 #include <string>
19 #include <arpa/inet.h>
20 #include <sys/socket.h>
21 #include "wifi_log.h"
22 
23 #undef LOG_TAG
24 #define LOG_TAG "OHWIFI_UTIL_Ipv4Address"
25 
26 namespace OHOS {
27 namespace Wifi {
28 const int MAX_IPV4_PREFIX_LENGTH = 32;
29 const int MAX_IPV4_STRING_LENGTH = 64;
30 const Ipv4Address Ipv4Address::INVALID_INET_ADDRESS("255.255.255.255", MAX_IPV4_PREFIX_LENGTH);
IsValidIPv4(const std::string & ipv4)31 bool Ipv4Address::IsValidIPv4(const std::string &ipv4)
32 {
33     struct in_addr ipv4Addr = {INADDR_ANY};
34     if (inet_pton(AF_INET, ipv4.c_str(), (void *)&ipv4Addr) != 1 ||
35         ipv4 == INVALID_INET_ADDRESS.GetAddressWithString()) {
36         return false;
37     } else {
38         return true;
39     }
40 }
41 
Create(const std::string & ipv4,size_t prefixLength)42 Ipv4Address Ipv4Address::Create(const std::string &ipv4, size_t prefixLength)
43 {
44     if (!IsValidIPv4(ipv4) || prefixLength > MAX_IPV4_PREFIX_LENGTH - 1) {
45         return INVALID_INET_ADDRESS;
46     }
47     return Ipv4Address(ipv4, prefixLength);
48 }
49 
Create(const std::string & ipv4,const std::string & mask)50 Ipv4Address Ipv4Address::Create(const std::string &ipv4, const std::string &mask)
51 {
52     size_t prefixLength = 0;
53     struct in_addr maskAddr = {INADDR_ANY};
54     if ((inet_aton(mask.c_str(), &maskAddr) != 1) || (!IsValidIPv4(ipv4))) {
55         return INVALID_INET_ADDRESS;
56     }
57     for (int i = 0; i < MAX_MASK_LENGTH; i++) {
58         if (maskAddr.s_addr != 0) {
59             maskAddr.s_addr = maskAddr.s_addr >> 1;
60             ++prefixLength;
61         }
62     }
63     return Ipv4Address(ipv4, prefixLength);
64 }
65 
Create(const in_addr & ipv4,const in_addr & mask)66 Ipv4Address Ipv4Address::Create(const in_addr &ipv4, const in_addr &mask)
67 {
68     size_t prefixLength = 0;
69     struct in_addr maskAddr = mask;
70     for (int i = 0; i < MAX_MASK_LENGTH; i++) {
71         if (maskAddr.s_addr != 0) {
72             maskAddr.s_addr = maskAddr.s_addr >> 1;
73             ++prefixLength;
74         }
75     }
76     char ipStr[MAX_IPV4_STRING_LENGTH] = {0};
77     if (inet_ntop(AF_INET, &ipv4, ipStr, sizeof(ipStr)) == nullptr) {
78         return INVALID_INET_ADDRESS;
79     }
80     return Ipv4Address(ipStr, prefixLength);
81 }
82 
Ipv4Address(const std::string & ipv4,size_t prefixLength)83 Ipv4Address::Ipv4Address(const std::string &ipv4, size_t prefixLength)
84     : BaseAddress(ipv4, prefixLength, FamilyType::FAMILY_INET)
85 {}
86 
IsValid() const87 bool Ipv4Address::IsValid() const
88 {
89     return IsValidIPv4(GetAddressWithString());
90 }
91 
GetAddressWithInet() const92 in_addr Ipv4Address::GetAddressWithInet() const
93 {
94     struct in_addr ipv4Addr = {INADDR_ANY};
95     if (inet_aton(GetAddressWithString().c_str(), &ipv4Addr) == 0) {
96         LOGI("inet_aton error");
97     }
98     return ipv4Addr;
99 }
100 
GetMaskWithString() const101 std::string Ipv4Address::GetMaskWithString() const
102 {
103     char ipStr[MAX_IPV4_STRING_LENGTH] = {0};
104     in_addr ipAddr = GetMaskWithInet();
105     if (inet_ntop(AF_INET, &ipAddr, ipStr, sizeof(ipStr)) == nullptr) {
106         return "";
107     }
108     return std::string(ipStr);
109 }
110 
GetMaskWithInet() const111 in_addr Ipv4Address::GetMaskWithInet() const
112 {
113     struct in_addr mask4Addr = {INADDR_ANY};
114     size_t num = GetAddressPrefixLength();
115     size_t i = 0xffffffff;
116     i = i >> (MAX_MASK_LENGTH - num);
117     mask4Addr.s_addr = i;
118     return mask4Addr;
119 }
120 
GetNetworkAddressWithString() const121 std::string Ipv4Address::GetNetworkAddressWithString() const
122 {
123     char ipStr[MAX_IPV4_STRING_LENGTH] = {0};
124     in_addr ipAddr = GetNetworkAddressWithInet();
125     if (inet_ntop(AF_INET, &ipAddr, ipStr, sizeof(ipStr)) == nullptr) {
126         return "";
127     }
128     return std::string(ipStr);
129 }
130 
GetNetworkAddressWithInet() const131 in_addr Ipv4Address::GetNetworkAddressWithInet() const
132 {
133     struct in_addr networkAddress = {INADDR_ANY};
134     networkAddress.s_addr = GetMaskWithInet().s_addr & GetAddressWithInet().s_addr;
135     return networkAddress;
136 }
137 
GetHostAddressWithString() const138 std::string Ipv4Address::GetHostAddressWithString() const
139 {
140     char ipStr[MAX_IPV4_STRING_LENGTH] = {0};
141     in_addr ipAddr = GetHostAddressWithInet();
142     if (inet_ntop(AF_INET, &ipAddr, ipStr, sizeof(ipStr)) == nullptr) {
143         return "";
144     }
145     return std::string(ipStr);
146 }
147 
GetHostAddressWithInet() const148 in_addr Ipv4Address::GetHostAddressWithInet() const
149 {
150     struct in_addr hostAddress = {INADDR_ANY};
151     hostAddress.s_addr = (~(GetMaskWithInet().s_addr)) & GetAddressWithInet().s_addr;
152     return hostAddress;
153 }
154 
GetNetwork() const155 std::string Ipv4Address::GetNetwork() const
156 {
157     std::string network = GetAddressWithString() + "/" + std::to_string(GetAddressPrefixLength());
158     return network;
159 }
160 }  // namespace Wifi
161 }  // namespace OHOS
162