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

Generated by: LCOV version 2.0-1