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

Generated by: LCOV version 2.0-1