VirtualBox

Changeset 87524 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Feb 2, 2021 2:17:11 AM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
142549
Message:

IPRT: RTNetStrToIPv6Cidr() - to parse f00::/42 notation for IPv6
prefixes.

"CIDR" in the name is a misnomer as IPv6 doesn't have network classes,
but it is parallel to the IPv4 name (and naming things is hard).

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/net/netaddrstr2.cpp

    r87362 r87524  
    679679}
    680680RT_EXPORT_SYMBOL(RTNetPrefixToMaskIPv6);
     681
     682
     683RTDECL(int) RTNetStrToIPv6Cidr(const char *pcszAddr, PRTNETADDRIPV6 pAddr, int *piPrefix)
     684{
     685    RTNETADDRIPV6 Addr;
     686    uint8_t u8Prefix;
     687    char *pszZone, *pszNext;
     688    int rc;
     689
     690    AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
     691    AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
     692    AssertPtrReturn(piPrefix, VERR_INVALID_PARAMETER);
     693
     694    pcszAddr = RTStrStripL(pcszAddr);
     695    rc = rtNetStrToIPv6AddrEx(pcszAddr, &Addr, &pszZone, &pszNext);
     696    if (RT_FAILURE(rc))
     697        return rc;
     698
     699    RT_NOREF(pszZone);
     700
     701    /*
     702     * If the prefix is missing, treat is as exact (/128) address
     703     * specification.
     704     */
     705    if (*pszNext == '\0' || rc == VWRN_TRAILING_SPACES)
     706    {
     707        *pAddr = Addr;
     708        *piPrefix = 128;
     709        return VINF_SUCCESS;
     710    }
     711
     712    if (*pszNext != '/')
     713        return VERR_INVALID_PARAMETER;
     714
     715    ++pszNext;
     716    rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &u8Prefix);
     717    if (RT_FAILURE(rc) || rc == VWRN_TRAILING_CHARS)
     718        return VERR_INVALID_PARAMETER;
     719
     720    if (u8Prefix == 0 || u8Prefix > 128)
     721        return VERR_INVALID_PARAMETER;
     722
     723    *pAddr = Addr;
     724    *piPrefix = u8Prefix;
     725    return VINF_SUCCESS;
     726}
     727RT_EXPORT_SYMBOL(RTNetStrToIPv6Cidr);
  • trunk/src/VBox/Runtime/testcase/tstRTNetIPv6.cpp

    r82968 r87524  
    7373
    7474
     75#define CHECKCIDR(String, rcExpected, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix)    \
     76    do {                                                                \
     77        RTNETADDRIPV6 Addr;                                             \
     78        uint32_t ExpectedAddr[4] = {                                    \
     79            RT_H2N_U32_C(u32_0), RT_H2N_U32_C(u32_1),                   \
     80            RT_H2N_U32_C(u32_2), RT_H2N_U32_C(u32_3)                    \
     81        };                                                              \
     82        int iPrefix;                                                    \
     83                                                                        \
     84        int rc2 = RTNetStrToIPv6Cidr(String, &Addr, &iPrefix);          \
     85        if ((rcExpected) && !rc2)                                       \
     86        {                                                               \
     87            RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
     88                          __LINE__, String, (rcExpected), rc2);         \
     89        }                                                               \
     90        else if (   (rcExpected) != rc2                                 \
     91                 || (   rc2 == VINF_SUCCESS                             \
     92                     && (   memcmp(ExpectedAddr, &Addr, sizeof(Addr)) != 0 \
     93                         || iExpectedPrefix != iPrefix)))               \
     94        {                                                               \
     95            RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc,"   \
     96                          " expected address %RTnaipv6/%d got %RTnaipv6/%d\n",\
     97                          __LINE__, String, rcExpected, rc2,            \
     98                          ExpectedAddr, iExpectedPrefix,                \
     99                          &Addr, iPrefix);                              \
     100        }                                                               \
     101    } while (0)
     102
     103#define GOODCIDR(String, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix) \
     104    CHECKCIDR(String, VINF_SUCCESS, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix)
     105
     106#define BADCIDR(String) \
     107    CHECKCIDR(String, VERR_INVALID_PARAMETER, 0, 0, 0, 0, 0)
     108
     109
    75110#define CHECKANY(String, fExpected)                                     \
    76111    do {                                                                \
     
    204239    GOODADDR(" ff01::1%net1.1\t", 0xff010000, 0, 0, 1);
    205240
     241    /* just some light testing */
     242    GOODCIDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008, 128);
     243    GOODCIDR("1:2:3:4::/64",    0x00010002, 0x00030004,          0,          0,  64);
     244    GOODCIDR(" 1:2:3:4::/64 ",  0x00010002, 0x00030004,          0,          0,  64);
     245
     246    /* we currently ignore the zone */
     247    GOODCIDR("1:2:3:4::%if/64", 0x00010002, 0x00030004,          0,          0,  64);
     248
     249    BADCIDR("1:2:3:4:: 64");
     250    BADCIDR("1:2:3:4::/64x");
     251    BADCIDR("1:2:3:4::/0");
     252    BADCIDR("1:2:3:4::/-1");
     253    BADCIDR("1:2:3:4::/129");
     254    BADCIDR("1:2:3:4::/256");
    206255
    207256    IS_ANY("::");
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette