VirtualBox

Changeset 76061 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Dec 7, 2018 3:13:22 PM (6 years ago)
Author:
vboxsync
Message:

IPRT: Add RTNetStrToIPv4Cidr() that is intended as a better API
replacement for RTCidrStrToIPv4(). Existing call sites for the latter
are not converted to the new function yet to avoid risky churn right
before the release.

File:
1 edited

Legend:

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

    r71435 r76061  
    196196
    197197
     198RTDECL(int) RTNetStrToIPv4Cidr(const char *pcszAddr, PRTNETADDRIPV4 pAddr, int *piPrefix)
     199{
     200    RTNETADDRIPV4 Addr;
     201    uint8_t u8Prefix;
     202    char *pszNext;
     203    int rc;
     204
     205    AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
     206    AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
     207    AssertPtrReturn(piPrefix, VERR_INVALID_PARAMETER);
     208
     209    pcszAddr = RTStrStripL(pcszAddr);
     210    rc = rtNetStrToIPv4AddrEx(pcszAddr, &Addr, &pszNext);
     211    if (RT_FAILURE(rc))
     212        return rc;
     213
     214    /* if prefix is missing, treat is as exact (/32) address specification */
     215    if (*pszNext == '\0' || rc == VWRN_TRAILING_SPACES)
     216    {
     217        *pAddr = Addr;
     218        *piPrefix = 32;
     219        return VINF_SUCCESS;
     220    }
     221
     222    if (*pszNext != '/')
     223        return VERR_INVALID_PARAMETER;
     224
     225    ++pszNext;
     226    rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &u8Prefix);
     227    if (RT_FAILURE(rc) || rc == VWRN_TRAILING_CHARS)
     228        return VERR_INVALID_PARAMETER;
     229
     230    if (u8Prefix == 0 || u8Prefix > 32)
     231        return VERR_INVALID_PARAMETER;
     232
     233    *pAddr = Addr;
     234    *piPrefix = u8Prefix;
     235    return VINF_SUCCESS;
     236}
     237RT_EXPORT_SYMBOL(RTNetStrToIPv4Cidr);
     238
     239
    198240static int rtNetStrToHexGroup(const char *pcszValue, char **ppszNext,
    199241                              uint16_t *pu16)
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