LCOV - code coverage report
Current view: top level - legacy/ascend910/pub_inc - hccl_ip_address.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.6 % 67 60
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 21 21

            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              : #ifndef HCCL_IP_ADDRESS_H
      12              : #define HCCL_IP_ADDRESS_H
      13              : 
      14              : #include <securec.h>
      15              : #include <arpa/inet.h>
      16              : #include <hccl/base.h>
      17              : #include <string>
      18              : #include <vector>
      19              : 
      20              : namespace hccl {
      21              : constexpr u32 IP_ADDRESS_BUFFER_LEN = 64;
      22              : 
      23              : struct HcclSocketInfo {
      24              :     void* socketHandle; /**< socket handle */
      25              :     void* fdHandle;     /**< fd handle */
      26              : };
      27              : 
      28              : constexpr uint32_t URMA_EID_LEN = 16;
      29              : constexpr uint32_t URMA_EID_NUM_TWO = 2;
      30              : constexpr uint32_t MAX_IPV4_LEN = 15;            // 最大IPv4地址长度
      31              : constexpr uint32_t MIN_IPV4_LEN = 7;             // 最小IPv4地址长度
      32              : constexpr uint32_t BASE = 10;                    // 进制基数
      33              : constexpr uint32_t MAX_DOT_COUNT = 3;            // IPv4地址.分割符的最大个数
      34              : constexpr uint32_t MAX_IPV4_SEGMENT_VALUE = 255; // 每个段的最大值
      35              : constexpr uint32_t URMA_EID_IPV4_PREFIX = 0x0;
      36              : 
      37              : union Eid {
      38              :     uint8_t raw[URMA_EID_LEN]{0};
      39              :     struct {
      40              :         uint64_t reserved;
      41              :         uint32_t prefix;
      42              :         uint32_t addr;
      43              :     } in4;
      44              :     struct {
      45              :         uint64_t subnetPrefix;
      46              :         uint64_t interfaceId;
      47              :     } in6;
      48              : 
      49              :     std::string Describe() const;
      50              : 
      51              :     bool operator==(const Eid& other) const { return memcmp(raw, other.raw, URMA_EID_LEN) == 0; }
      52              : 
      53            4 :     bool operator<(const Eid& other) const { return memcmp(raw, other.raw, URMA_EID_LEN) < 0; }
      54              : };
      55              : 
      56              : union HcclInAddr {
      57              :     struct in_addr addr;
      58              :     struct in6_addr addr6;
      59              : };
      60              : 
      61              : class HcclIpAddress {
      62              : public:
      63        46942 :     explicit HcclIpAddress()
      64        46942 :     {
      65        46945 :         scopeID = 0;
      66        46945 :         family = AF_INET;
      67        46945 :         binaryAddr.addr.s_addr = 0;
      68        46945 :         readableIP = "0.0.0.0";
      69        46953 :         readableAddr = readableIP;
      70        46966 :     }
      71              :     explicit HcclIpAddress(const Eid& eidInput);
      72              : 
      73         1553 :     explicit HcclIpAddress(u32 address)
      74         1553 :     {
      75           60 :         union HcclInAddr ipAddr;
      76         1552 :         ipAddr.addr.s_addr = address;
      77         1552 :         (void)SetBianryAddress(AF_INET, ipAddr);
      78         1555 :     }
      79          289 :     explicit HcclIpAddress(s32 family, const union HcclInAddr& address) { (void)SetBianryAddress(family, address); }
      80           10 :     explicit HcclIpAddress(const struct in_addr& address)
      81           10 :     {
      82              :         union HcclInAddr ipAddr;
      83           10 :         ipAddr.addr = address;
      84           10 :         (void)SetBianryAddress(AF_INET, ipAddr);
      85           10 :     }
      86            4 :     explicit HcclIpAddress(const struct in6_addr& address)
      87            4 :     {
      88              :         union HcclInAddr ipAddr;
      89            4 :         ipAddr.addr6 = address;
      90            4 :         (void)SetBianryAddress(AF_INET6, ipAddr);
      91            4 :     }
      92         2354 :     explicit HcclIpAddress(const std::string& address) { (void)SetReadableAddress(address); }
      93       466922 :     ~HcclIpAddress() {}
      94              : 
      95              :     static bool IsIPv6(const std::string& str);
      96              :     static bool IsIPv4(const std::string& str);
      97              :     static bool IsEID(const std::string& str);
      98              :     static Eid StrToEID(const std::string& str);
      99              :     std::string GetIpStr() const;
     100            3 :     Eid GetEid() const { return eid; }
     101              :     std::string Describe() const;
     102              : 
     103              :     std::string GetIfName() const { return ifname; }
     104          189 :     HcclResult SetScopeID(s32 scopeID)
     105              :     {
     106          189 :         this->scopeID = scopeID;
     107          189 :         return HCCL_SUCCESS;
     108              :     }
     109           22 :     s32 GetScopeID() const { return scopeID; }
     110          621 :     s32 GetFamily() const { return family; }
     111              : 
     112         4407 :     const char* GetReadableIP() const
     113              :     {
     114              :         // return "IP address (string)"
     115         4404 :         return readableIP.c_str();
     116              :     }
     117         8840 :     const char* GetReadableAddress() const
     118              :     {
     119              :         // return "IP address (string) % ifname"
     120         8840 :         return readableAddr.c_str();
     121              :     }
     122         1979 :     union HcclInAddr GetBinaryAddress() const { return binaryAddr; }
     123         1517 :     bool IsIPv6() const { return (family == AF_INET6); }
     124         6088 :     void clear()
     125              :     {
     126         6088 :         family = AF_INET;
     127         6088 :         scopeID = 0;
     128         6088 :         binaryAddr.addr.s_addr = 0;
     129         6088 :         readableAddr.clear();
     130         6088 :         readableIP.clear();
     131         6088 :         ifname.clear();
     132         6088 :     }
     133         6372 :     bool IsInvalid() const { return ((family == AF_INET) && (binaryAddr.addr.s_addr == 0)); }
     134          172 :     bool operator==(const HcclIpAddress& that) const
     135              :     {
     136          172 :         if (this->family != that.family) {
     137            2 :             return false;
     138              :         }
     139          170 :         if (this->family == AF_INET) {
     140          170 :             return (this->binaryAddr.addr.s_addr == that.binaryAddr.addr.s_addr);
     141              :         } else {
     142            0 :             if (memcmp(&this->binaryAddr.addr6, &that.binaryAddr.addr6, sizeof(this->binaryAddr.addr6)) != 0) {
     143            0 :                 return false;
     144              :             } else {
     145            0 :                 if (this->ifname.empty() || that.ifname.empty()) {
     146            0 :                     return true;
     147              :                 } else {
     148            0 :                     return (this->ifname == that.ifname);
     149              :                 }
     150              :             }
     151              :         }
     152              :     }
     153              : 
     154          157 :     bool operator!=(const HcclIpAddress& that) const { return !(*this == that); }
     155         9357 :     bool operator<(const HcclIpAddress& that) const
     156              :     {
     157         9357 :         if (this->family < that.family) {
     158            0 :             return true;
     159              :         }
     160         9357 :         if (this->family > that.family) {
     161            0 :             return false;
     162              :         }
     163         9357 :         return (this->family == AF_INET) ? (this->binaryAddr.addr.s_addr < that.binaryAddr.addr.s_addr) :
     164         9357 :                                            (this->readableAddr < that.readableAddr);
     165              :     }
     166              : 
     167              :     HcclResult SetReadableAddress(const std::string& address);
     168              :     HcclResult SetIfName(const std::string& name);
     169              : 
     170              : private:
     171              :     HcclResult SetBianryAddress(s32 family, const union HcclInAddr& address);
     172              : 
     173              :     union HcclInAddr binaryAddr {}; // 二进制IP地址
     174              :     std::string readableAddr{};     // 字符串IP地址 + % + 网卡名
     175              :     std::string readableIP{};       // 字符串IP地址
     176              :     std::string ifname{};           // 网卡名
     177              :     s32 family{};
     178              :     s32 scopeID{};
     179              :     Eid eid{};
     180              : };
     181              : } // namespace hccl
     182              : #endif // HCCL_IP_ADDRESS_H
        

Generated by: LCOV version 2.0-1