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: 91.4 % 81 74
Test Date: 2026-08-04 10:52:23 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 {
      52              :         return memcmp(raw, other.raw, URMA_EID_LEN) == 0;
      53              :     }
      54              : 
      55            4 :     bool operator<(const Eid& other) const {
      56            4 :         return memcmp(raw, other.raw, URMA_EID_LEN) < 0;
      57              :     }
      58              : };
      59              : 
      60              : union HcclInAddr {
      61              :     struct in_addr addr;
      62              :     struct in6_addr addr6;
      63              : };
      64              : 
      65              : class HcclIpAddress {
      66              : public:
      67        46729 :     explicit HcclIpAddress()
      68        46729 :     {
      69        46733 :         scopeID = 0;
      70        46733 :         family = AF_INET;
      71        46733 :         binaryAddr.addr.s_addr = 0;
      72        46733 :         readableIP = "0.0.0.0";
      73        46728 :         readableAddr = readableIP;
      74        46747 :     }
      75              :     explicit HcclIpAddress(const Eid &eidInput);
      76              :  
      77         1552 :     explicit HcclIpAddress(u32 address)
      78         1552 :     {
      79           60 :         union HcclInAddr ipAddr;
      80         1552 :         ipAddr.addr.s_addr = address;
      81         1552 :         (void)SetBianryAddress(AF_INET, ipAddr);
      82         1551 :     }
      83          289 :     explicit HcclIpAddress(s32 family, const union HcclInAddr &address)
      84          289 :     {
      85          289 :         (void)SetBianryAddress(family, address);
      86          289 :     }
      87           10 :     explicit HcclIpAddress(const struct in_addr &address)
      88           10 :     {
      89              :         union HcclInAddr ipAddr;
      90           10 :         ipAddr.addr = address;
      91           10 :         (void)SetBianryAddress(AF_INET, ipAddr);
      92           10 :     }
      93            4 :     explicit HcclIpAddress(const struct in6_addr &address)
      94            4 :     {
      95              :         union HcclInAddr ipAddr;
      96            4 :         ipAddr.addr6 = address;
      97            4 :         (void)SetBianryAddress(AF_INET6, ipAddr);
      98            4 :     }
      99         2347 :     explicit HcclIpAddress(const std::string &address)
     100         2347 :     {
     101         2349 :         (void)SetReadableAddress(address);
     102         2348 :     }
     103       465767 :     ~HcclIpAddress() {}
     104              : 
     105              :     static bool IsIPv6(const std::string& str);
     106              :         static bool IsIPv4(const std::string& str);
     107              :     static bool IsEID(const std::string& str);
     108              :     static Eid StrToEID(const std::string& str);
     109              :     std::string GetIpStr() const;
     110            3 :     Eid GetEid() const
     111              :     {
     112            3 :         return eid;
     113              :     }
     114              :     std::string Describe() const;
     115              : 
     116              :     std::string GetIfName() const
     117              :     {
     118              :         return ifname;
     119              :     }
     120          189 :     HcclResult SetScopeID(s32 scopeID)
     121              :     {
     122          189 :         this->scopeID = scopeID;
     123          189 :         return HCCL_SUCCESS;
     124              :     }
     125           22 :     s32 GetScopeID() const
     126              :     {
     127           22 :         return scopeID;
     128              :     }
     129          621 :     s32 GetFamily() const
     130              :     {
     131          621 :         return family;
     132              :     }
     133              : 
     134         4392 :     const char *GetReadableIP() const
     135              :     {
     136              :         // return "IP address (string)"
     137         4389 :         return readableIP.c_str();
     138              :     }
     139         8772 :     const char *GetReadableAddress() const
     140              :     {
     141              :         // return "IP address (string) % ifname"
     142         8772 :         return readableAddr.c_str();
     143              :     }
     144         1974 :     union HcclInAddr GetBinaryAddress() const
     145              :     {
     146         1974 :         return binaryAddr;
     147              :     }
     148         1512 :     bool IsIPv6() const
     149              :     {
     150         1512 :         return (family == AF_INET6);
     151              :     }
     152         5626 :     void clear()
     153              :     {
     154         5626 :         family = AF_INET;
     155         5626 :         scopeID = 0;
     156         5626 :         binaryAddr.addr.s_addr = 0;
     157         5626 :         readableAddr.clear();
     158         5626 :         readableIP.clear();
     159         5626 :         ifname.clear();
     160         5626 :     }
     161         6362 :     bool IsInvalid() const
     162              :     {
     163         6332 :         return ((family == AF_INET) && (binaryAddr.addr.s_addr == 0));
     164              :     }
     165          172 :     bool operator == (const HcclIpAddress &that) const
     166              :     {
     167          172 :         if (this->family != that.family) {
     168            2 :             return false;
     169              :         }
     170          170 :         if (this->family == AF_INET) {
     171          170 :             return (this->binaryAddr.addr.s_addr == that.binaryAddr.addr.s_addr);
     172              :         } else {
     173            0 :             if (memcmp(&this->binaryAddr.addr6, &that.binaryAddr.addr6, sizeof(this->binaryAddr.addr6)) != 0) {
     174            0 :                 return false;
     175              :             } else {
     176            0 :                 if (this->ifname.empty() || that.ifname.empty()) {
     177            0 :                     return true;
     178              :                 } else {
     179            0 :                     return (this->ifname == that.ifname);
     180              :                 }
     181              :             }
     182              :         }
     183              :     }
     184              : 
     185          157 :     bool operator != (const HcclIpAddress &that) const
     186              :     {
     187          157 :         return !(*this == that);
     188              :     }
     189         9350 :     bool operator < (const HcclIpAddress &that) const
     190              :     {
     191         9350 :         if (this->family < that.family) {
     192            0 :             return true;
     193              :         }
     194         9350 :         if (this->family > that.family) {
     195            0 :             return false;
     196              :         }
     197         9350 :         return (this->family == AF_INET) ? (this->binaryAddr.addr.s_addr < that.binaryAddr.addr.s_addr) :
     198         9350 :                                            (this->readableAddr < that.readableAddr);
     199              :     }
     200              : 
     201              :     HcclResult SetReadableAddress(const std::string &address);
     202              :     HcclResult SetIfName(const std::string &name);
     203              : private:
     204              :     HcclResult SetBianryAddress(s32 family, const union HcclInAddr &address);
     205              : 
     206              :     union HcclInAddr binaryAddr{};   // 二进制IP地址
     207              :     std::string readableAddr{};      // 字符串IP地址 + % + 网卡名
     208              :     std::string readableIP{};        // 字符串IP地址
     209              :     std::string ifname{};            // 网卡名
     210              :     s32 family{};
     211              :     s32 scopeID{};
     212              :     Eid eid{};
     213              : };
     214              : }
     215              : #endif // HCCL_IP_ADDRESS_H
        

Generated by: LCOV version 2.0-1