Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include <algorithm>
11 : #include <sstream>
12 : #include <iomanip>
13 : #include "hccl_ip_address.h"
14 : #include <regex>
15 : #include <log.h>
16 : namespace hccl {
17 :
18 1854 : HcclResult HcclIpAddress::SetBianryAddress(s32 family, const union HcclInAddr& address)
19 : {
20 1854 : char buf[IP_ADDRESS_BUFFER_LEN] = {0};
21 1854 : if (inet_ntop(family, &address, buf, sizeof(buf)) == nullptr) {
22 2 : if (family == AF_INET) {
23 1 : HCCL_ERROR("ip addr[0x%08x] is invalid IPv4 address.", address.addr.s_addr);
24 : } else {
25 1 : HCCL_ERROR(
26 : "ip addr[%08x %08x %08x %08x] is invalid IPv6 address.",
27 : address.addr6.s6_addr32[0], // 打印ipv6地址中的 word 0
28 : address.addr6.s6_addr32[1], // 打印ipv6地址中的 word 1
29 : address.addr6.s6_addr32[2], // 打印ipv6地址中的 word 2
30 : address.addr6.s6_addr32[3]); // 打印ipv6地址中的 word 3
31 : }
32 2 : return HCCL_E_PARA;
33 : } else {
34 1856 : this->family = family;
35 1856 : this->binaryAddr = address;
36 1856 : this->readableIP = buf;
37 1857 : this->readableAddr = this->readableIP;
38 1857 : return HCCL_SUCCESS;
39 : }
40 : }
41 :
42 3535 : HcclResult HcclIpAddress::SetReadableAddress(const std::string& address)
43 : {
44 3535 : CHK_PRT_RET(address.empty(), HCCL_ERROR("ip addr is null."), HCCL_E_PARA);
45 :
46 3535 : std::size_t found = address.find("%");
47 3534 : if ((found == 0) || (found == (address.length() - 1))) {
48 0 : HCCL_ERROR("addr[%s] is invalid.", address.c_str());
49 0 : return HCCL_E_PARA;
50 : }
51 3535 : std::string ipStr = address.substr(0, found);
52 3535 : int cnt = std::count(ipStr.begin(), ipStr.end(), ':');
53 3533 : if (cnt >= 2) { // ipv6地址中至少有2个":"
54 7 : if (inet_pton(AF_INET6, ipStr.c_str(), &binaryAddr.addr6) <= 0) {
55 1 : HCCL_ERROR("ip addr[%s] is invalid IPv6 address.", ipStr.c_str());
56 1 : binaryAddr.addr6.s6_addr32[0] = 0; // 清空ipv6地址中的 word 0
57 1 : binaryAddr.addr6.s6_addr32[1] = 0; // 清空ipv6地址中的 word 1
58 1 : binaryAddr.addr6.s6_addr32[2] = 0; // 清空ipv6地址中的 word 2
59 1 : binaryAddr.addr6.s6_addr32[3] = 0; // 清空ipv6地址中的 word 3
60 1 : clear();
61 1 : return HCCL_E_PARA;
62 : }
63 6 : this->family = AF_INET6;
64 : } else {
65 3526 : if (inet_pton(AF_INET, ipStr.c_str(), &binaryAddr.addr) <= 0) {
66 3 : HCCL_ERROR("ip addr[%s] is invalid IPv4 address.", ipStr.c_str());
67 3 : clear();
68 3 : return HCCL_E_PARA;
69 : }
70 3526 : this->family = AF_INET;
71 : }
72 3532 : if (found != std::string::npos) {
73 0 : this->ifname = address.substr(found + 1);
74 : }
75 3532 : this->readableIP = ipStr;
76 3532 : this->readableAddr = address;
77 3532 : return HCCL_SUCCESS;
78 3536 : }
79 :
80 189 : HcclResult HcclIpAddress::SetIfName(const std::string& name)
81 : {
82 189 : CHK_PRT_RET(name.empty(), HCCL_ERROR("if name is null."), HCCL_E_PARA);
83 :
84 189 : std::size_t found = readableAddr.find("%");
85 189 : if (found == std::string::npos) {
86 189 : ifname = name;
87 189 : readableAddr.append("%");
88 189 : readableAddr.append(ifname);
89 : } else {
90 0 : HCCL_ERROR("addr[%s] ifname has existed.", readableAddr.c_str());
91 0 : return HCCL_E_PARA;
92 : }
93 189 : return HCCL_SUCCESS;
94 : }
95 :
96 0 : std::string HcclIpAddress::Describe() const
97 : {
98 0 : std::ostringstream oss;
99 0 : oss << "IpAddress[" << eid.Describe() << ",";
100 :
101 0 : if (family == AF_INET) {
102 0 : oss << "AF=v4,addr=" << GetIpStr() << "]";
103 : } else {
104 0 : oss << "AF=v6,addr=" << GetIpStr() << ", scopeId=0x" << std::hex << scopeID << "]";
105 : }
106 0 : return oss.str();
107 0 : }
108 :
109 0 : HcclIpAddress::HcclIpAddress(const Eid& eidInput)
110 : {
111 0 : for (uint32_t i = 0; i < URMA_EID_LEN; i++) {
112 0 : eid.raw[i] = eidInput.raw[i];
113 : }
114 :
115 0 : HCCL_INFO("[IpAddress] %s", eid.Describe().c_str());
116 : // IPoURMA适配后,使用EID初始化时转为ipv6建链
117 0 : this->family = AF_INET6;
118 0 : (void)memcpy_s(binaryAddr.addr6.s6_addr, sizeof(eid.raw), eid.raw, sizeof(eid.raw));
119 0 : (void)SetBianryAddress(family, binaryAddr);
120 0 : }
121 :
122 0 : bool HcclIpAddress::IsEID(const std::string& str)
123 : {
124 0 : if (str.length() == URMA_EID_LEN * URMA_EID_NUM_TWO) {
125 0 : std::regex hexCharsRegex("[0-9a-fA-F]+");
126 0 : return std::regex_match(str, hexCharsRegex);
127 0 : }
128 0 : return false;
129 : }
130 :
131 0 : Eid HcclIpAddress::StrToEID(const std::string& str)
132 : {
133 0 : Eid tmpeEid{};
134 0 : const int Base = 16;
135 0 : for (size_t i = 0; i < URMA_EID_LEN; ++i) {
136 0 : std::string byteString = str.substr(i * 2, 2);
137 0 : tmpeEid.raw[i] = static_cast<uint8_t>(std::stoi(byteString, nullptr, Base));
138 0 : }
139 0 : return tmpeEid;
140 : }
141 0 : std::string HcclIpAddress::GetIpStr() const
142 : {
143 0 : const void* src = nullptr;
144 0 : if (family == AF_INET) {
145 0 : src = &binaryAddr.addr;
146 0 : } else if (family == AF_INET6) {
147 0 : src = &binaryAddr.addr6;
148 : }
149 : char dst[INET6_ADDRSTRLEN];
150 0 : const char* res = inet_ntop(family, src, dst, INET6_ADDRSTRLEN);
151 0 : if (res == nullptr) {
152 : // 转换失败处理:返回空字符串或抛异常
153 0 : return ""; // 示例
154 : }
155 0 : return dst;
156 : }
157 :
158 0 : bool HcclIpAddress::IsIPv6(const std::string& str)
159 : {
160 : std::regex ipv6Pattern(
161 0 : R"(^([\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^::([\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:):([\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){2}:([\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){3}:([\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$)");
162 0 : return regex_match(str, ipv6Pattern);
163 0 : }
164 :
165 : /*All the five types of IPV4 addresses,ABCDE,can be identified.
166 : A: 1.0.0.1 - 126.255.255.254
167 : B: 128.0.0.1 - 191.255.255.254
168 : C: 192.0.0.1 - 223.255.255.254
169 : D: 224.0.0.1 - 239.255.255.254
170 : E: 240.0.0.1 - 255.255.255.254
171 : 127.x.x.x is reserved address for loopback test.
172 : 0.0.0.0 can only be used as the source address.
173 : 255.255.255.255 is broadcast address.
174 : */
175 22 : bool HcclIpAddress::IsIPv4(const std::string& str)
176 : {
177 : // 快速长度检查
178 22 : size_t len = str.length();
179 22 : if (len < MIN_IPV4_LEN || len > MAX_IPV4_LEN) {
180 0 : return false;
181 : }
182 :
183 22 : uint32_t num = 0;
184 22 : uint32_t dotCount = 0;
185 22 : bool hasDigit = false;
186 :
187 242 : for (size_t i = 0; i < len; ++i) {
188 220 : char c = str[i];
189 :
190 220 : if (c >= '0' && c <= '9') {
191 : // 检查前导零
192 154 : if (!hasDigit && c == '0' && i + 1 < len && str[i + 1] != '.') {
193 0 : return false;
194 : }
195 :
196 154 : num = num * BASE + (c - '0');
197 154 : hasDigit = true;
198 :
199 154 : if (num > MAX_IPV4_SEGMENT_VALUE) {
200 0 : return false;
201 : }
202 66 : } else if (c == '.') {
203 : // 检查点号位置和数字有效性
204 66 : if (!hasDigit || dotCount >= MAX_DOT_COUNT || i == 0 || i == len - 1) {
205 0 : return false;
206 : }
207 :
208 66 : dotCount++;
209 66 : num = 0;
210 66 : hasDigit = false;
211 : } else {
212 0 : return false;
213 : }
214 : }
215 :
216 22 : return dotCount == MAX_DOT_COUNT && hasDigit;
217 : }
218 :
219 1 : std::string Eid::Describe() const
220 : {
221 1 : std::ostringstream oss;
222 1 : oss << "eid[" << std::hex << std::setw(16) << std::setfill('0') << be64toh(in6.subnetPrefix) << ":" << std::hex
223 1 : << std::setw(16) << std::setfill('0') << be64toh(in6.interfaceId) << "]";
224 2 : return oss.str();
225 1 : }
226 :
227 : } // namespace hccl
|