VirtualBox

Ignore:
Timestamp:
Feb 2, 2021 2:17:11 AM (4 years ago)
Author:
vboxsync
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).

File:
1 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);
Note: See TracChangeset for help on using the changeset viewer.

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