LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/common - hccl_ip_address.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 56.8 % 125 71
Test Date: 2026-08-25 19:18:03 Functions: 45.5 % 11 5

            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              : 
      11              : #include <algorithm>
      12              : #include <sstream>
      13              : #include <iomanip>
      14              : #include "hccl_ip_address.h"
      15              : #include <regex>
      16              : #include <log.h>
      17              : namespace hccl {
      18              : constexpr int MIN_IPV6_COLON_COUNT = 2;             // IPv6地址中至少包含的冒号个数
      19              : constexpr uint32_t URMA_EID_HEX_CHARS_PER_BYTE = 2; // EID十六进制字符串中每字节占用的字符数
      20              : constexpr uint32_t EID_DESCRIBE_HEX_WIDTH = 16;     // Eid::Describe中64位段对应的十六进制显示宽度
      21              : 
      22         1855 : HcclResult HcclIpAddress::SetBianryAddress(s32 family, const union HcclInAddr& address)
      23              : {
      24         1855 :     char buf[IP_ADDRESS_BUFFER_LEN] = {0};
      25         1855 :     if (inet_ntop(family, &address, buf, sizeof(buf)) == nullptr) {
      26            2 :         if (family == AF_INET) {
      27            1 :             HCCL_ERROR("ip addr[0x%08x] is invalid IPv4 address.", address.addr.s_addr);
      28              :         } else {
      29            1 :             HCCL_ERROR(
      30              :                 "ip addr[%08x %08x %08x %08x] is invalid IPv6 address.",
      31              :                 address.addr6.s6_addr32[0],  // 打印ipv6地址中的 word 0
      32              :                 address.addr6.s6_addr32[1],  // 打印ipv6地址中的 word 1
      33              :                 address.addr6.s6_addr32[2],  // 打印ipv6地址中的 word 2
      34              :                 address.addr6.s6_addr32[3]); // 打印ipv6地址中的 word 3
      35              :         }
      36            2 :         return HCCL_E_PARA;
      37              :     } else {
      38         1854 :         this->family = family;
      39         1854 :         this->binaryAddr = address;
      40         1854 :         this->readableIP = buf;
      41         1856 :         this->readableAddr = this->readableIP;
      42         1857 :         return HCCL_SUCCESS;
      43              :     }
      44              : }
      45              : 
      46         3538 : HcclResult HcclIpAddress::SetReadableAddress(const std::string& address)
      47              : {
      48         3538 :     CHK_PRT_RET(address.empty(), HCCL_ERROR("ip addr is null."), HCCL_E_PARA);
      49              : 
      50         3537 :     std::size_t found = address.find("%");
      51         3536 :     if ((found == 0) || (found == (address.length() - 1))) {
      52            0 :         HCCL_ERROR("addr[%s] is invalid.", address.c_str());
      53            0 :         return HCCL_E_PARA;
      54              :     }
      55         3537 :     std::string ipStr = address.substr(0, found);
      56         3537 :     int cnt = std::count(ipStr.begin(), ipStr.end(), ':');
      57         3530 :     if (cnt >= MIN_IPV6_COLON_COUNT) { // ipv6地址中至少有2个":"
      58            7 :         if (inet_pton(AF_INET6, ipStr.c_str(), &binaryAddr.addr6) <= 0) {
      59            1 :             HCCL_ERROR("ip addr[%s] is invalid IPv6 address.", ipStr.c_str());
      60            1 :             binaryAddr.addr6.s6_addr32[0] = 0; // 清空ipv6地址中的 word 0
      61            1 :             binaryAddr.addr6.s6_addr32[1] = 0; // 清空ipv6地址中的 word 1
      62            1 :             binaryAddr.addr6.s6_addr32[2] = 0; // 清空ipv6地址中的 word 2
      63            1 :             binaryAddr.addr6.s6_addr32[3] = 0; // 清空ipv6地址中的 word 3
      64            1 :             clear();
      65            1 :             return HCCL_E_PARA;
      66              :         }
      67            6 :         this->family = AF_INET6;
      68              :     } else {
      69         3523 :         if (inet_pton(AF_INET, ipStr.c_str(), &binaryAddr.addr) <= 0) {
      70            3 :             HCCL_ERROR("ip addr[%s] is invalid IPv4 address.", ipStr.c_str());
      71            3 :             clear();
      72            3 :             return HCCL_E_PARA;
      73              :         }
      74         3528 :         this->family = AF_INET;
      75              :     }
      76         3534 :     if (found != std::string::npos) {
      77            0 :         this->ifname = address.substr(found + 1);
      78              :     }
      79         3534 :     this->readableIP = ipStr;
      80         3534 :     this->readableAddr = address;
      81         3533 :     return HCCL_SUCCESS;
      82         3537 : }
      83              : 
      84          189 : HcclResult HcclIpAddress::SetIfName(const std::string& name)
      85              : {
      86          189 :     CHK_PRT_RET(name.empty(), HCCL_ERROR("if name is null."), HCCL_E_PARA);
      87              : 
      88          189 :     std::size_t found = readableAddr.find("%");
      89          189 :     if (found == std::string::npos) {
      90          189 :         ifname = name;
      91          189 :         readableAddr.append("%");
      92          189 :         readableAddr.append(ifname);
      93              :     } else {
      94            0 :         HCCL_ERROR("addr[%s] ifname has existed.", readableAddr.c_str());
      95            0 :         return HCCL_E_PARA;
      96              :     }
      97          189 :     return HCCL_SUCCESS;
      98              : }
      99              : 
     100            0 : std::string HcclIpAddress::Describe() const
     101              : {
     102            0 :     std::ostringstream oss;
     103            0 :     oss << "IpAddress[" << eid.Describe() << ",";
     104              : 
     105            0 :     if (family == AF_INET) {
     106            0 :         oss << "AF=v4,addr=" << GetIpStr() << "]";
     107              :     } else {
     108            0 :         oss << "AF=v6,addr=" << GetIpStr() << ", scopeId=0x" << std::hex << scopeID << "]";
     109              :     }
     110            0 :     return oss.str();
     111            0 : }
     112              : 
     113            0 : HcclIpAddress::HcclIpAddress(const Eid& eidInput)
     114              : {
     115            0 :     for (uint32_t i = 0; i < URMA_EID_LEN; i++) {
     116            0 :         eid.raw[i] = eidInput.raw[i];
     117              :     }
     118              : 
     119            0 :     HCCL_INFO("[IpAddress] %s", eid.Describe().c_str());
     120              :     // IPoURMA适配后,使用EID初始化时转为ipv6建链
     121            0 :     this->family = AF_INET6;
     122            0 :     (void)memcpy_s(binaryAddr.addr6.s6_addr, sizeof(eid.raw), eid.raw, sizeof(eid.raw));
     123            0 :     (void)SetBianryAddress(family, binaryAddr);
     124            0 : }
     125              : 
     126            0 : bool HcclIpAddress::IsEID(const std::string& str)
     127              : {
     128            0 :     if (str.length() == URMA_EID_LEN * URMA_EID_NUM_TWO) {
     129            0 :         std::regex hexCharsRegex("[0-9a-fA-F]+");
     130            0 :         return std::regex_match(str, hexCharsRegex);
     131            0 :     }
     132            0 :     return false;
     133              : }
     134              : 
     135            0 : Eid HcclIpAddress::StrToEID(const std::string& str)
     136              : {
     137            0 :     Eid tmpeEid{};
     138            0 :     const int Base = 16;
     139            0 :     for (size_t i = 0; i < URMA_EID_LEN; ++i) {
     140            0 :         std::string byteString = str.substr(i * URMA_EID_HEX_CHARS_PER_BYTE, URMA_EID_HEX_CHARS_PER_BYTE);
     141            0 :         tmpeEid.raw[i] = static_cast<uint8_t>(std::stoi(byteString, nullptr, Base));
     142            0 :     }
     143            0 :     return tmpeEid;
     144              : }
     145            0 : std::string HcclIpAddress::GetIpStr() const
     146              : {
     147            0 :     const void* src = nullptr;
     148            0 :     if (family == AF_INET) {
     149            0 :         src = &binaryAddr.addr;
     150            0 :     } else if (family == AF_INET6) {
     151            0 :         src = &binaryAddr.addr6;
     152              :     }
     153              :     char dst[INET6_ADDRSTRLEN];
     154            0 :     const char* res = inet_ntop(family, src, dst, INET6_ADDRSTRLEN);
     155            0 :     if (res == nullptr) {
     156              :         // 转换失败处理:返回空字符串或抛异常
     157            0 :         return ""; // 示例
     158              :     }
     159            0 :     return dst;
     160              : }
     161              : 
     162            0 : bool HcclIpAddress::IsIPv6(const std::string& str)
     163              : {
     164              :     std::regex ipv6Pattern(
     165            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}:$)");
     166            0 :     return regex_match(str, ipv6Pattern);
     167            0 : }
     168              : 
     169              : /*All the five types of IPV4 addresses,ABCDE,can be identified.
     170              :     A: 1.0.0.1 - 126.255.255.254
     171              :     B: 128.0.0.1 - 191.255.255.254
     172              :     C: 192.0.0.1 - 223.255.255.254
     173              :     D: 224.0.0.1 - 239.255.255.254
     174              :     E: 240.0.0.1 - 255.255.255.254
     175              :     127.x.x.x is reserved address for loopback test.
     176              :     0.0.0.0 can only be used as the source address.
     177              :     255.255.255.255 is broadcast address.
     178              : */
     179           22 : bool HcclIpAddress::IsIPv4(const std::string& str)
     180              : {
     181              :     // 快速长度检查
     182           22 :     size_t len = str.length();
     183           22 :     if (len < MIN_IPV4_LEN || len > MAX_IPV4_LEN) {
     184            0 :         return false;
     185              :     }
     186              : 
     187           22 :     uint32_t num = 0;
     188           22 :     uint32_t dotCount = 0;
     189           22 :     bool hasDigit = false;
     190              : 
     191          242 :     for (size_t i = 0; i < len; ++i) {
     192          220 :         char c = str[i];
     193              : 
     194          220 :         if (c >= '0' && c <= '9') {
     195              :             // 检查前导零
     196          154 :             if (!hasDigit && c == '0' && i + 1 < len && str[i + 1] != '.') {
     197            0 :                 return false;
     198              :             }
     199              : 
     200          154 :             num = num * BASE + (c - '0');
     201          154 :             hasDigit = true;
     202              : 
     203          154 :             if (num > MAX_IPV4_SEGMENT_VALUE) {
     204            0 :                 return false;
     205              :             }
     206           66 :         } else if (c == '.') {
     207              :             // 检查点号位置和数字有效性
     208           66 :             if (!hasDigit || dotCount >= MAX_DOT_COUNT || i == 0 || i == len - 1) {
     209            0 :                 return false;
     210              :             }
     211              : 
     212           66 :             dotCount++;
     213           66 :             num = 0;
     214           66 :             hasDigit = false;
     215              :         } else {
     216            0 :             return false;
     217              :         }
     218              :     }
     219              : 
     220           22 :     return dotCount == MAX_DOT_COUNT && hasDigit;
     221              : }
     222              : 
     223            1 : std::string Eid::Describe() const
     224              : {
     225            1 :     std::ostringstream oss;
     226            1 :     oss << "eid[" << std::hex << std::setw(EID_DESCRIBE_HEX_WIDTH) << std::setfill('0') << be64toh(in6.subnetPrefix)
     227            1 :         << ":" << std::hex << std::setw(EID_DESCRIBE_HEX_WIDTH) << std::setfill('0') << be64toh(in6.interfaceId) << "]";
     228            2 :     return oss.str();
     229            1 : }
     230              : 
     231              : } // namespace hccl
        

Generated by: LCOV version 2.0-1