VirtualBox

Ignore:
Timestamp:
Feb 11, 2014 6:50:58 PM (11 years ago)
Author:
vboxsync
Message:

Add RTNetStrToIPv4AddrEx - a variant of IPv4 dotted-decimal parsing
function with ppszNext argument. Express RTNetStrToIPv4Addr and
RTNetIsIPv4AddrStr in its terms.

Note that RTNetIsIPv4AddrStr does not accept leading/trailing
whitespace while RTNetStrToIPv4Addr does - this is existing behaviour
that these changes keep.

File:
1 edited

Legend:

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

    r48935 r50418  
    3636#include "internal/string.h"
    3737
    38 RTDECL(int) RTNetStrToIPv4Addr(const char *pszAddr, PRTNETADDRIPV4 pAddr)
     38
     39DECLHIDDEN(int) rtNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr,
     40                                     char **ppszNext)
    3941{
    4042    char *pszNext;
    41     AssertPtrReturn(pszAddr, VERR_INVALID_PARAMETER);
     43    int rc;
     44
     45    AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
    4246    AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
    4347
    44     int rc = RTStrToUInt8Ex(RTStrStripL(pszAddr), &pszNext, 10, &pAddr->au8[0]);
     48    rc = RTStrToUInt8Ex(pcszAddr, &pszNext, 10, &pAddr->au8[0]);
    4549    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
    4650        return VERR_INVALID_PARAMETER;
     
    6165
    6266    rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
    63     if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
     67    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES && rc != VWRN_TRAILING_CHARS)
    6468        return VERR_INVALID_PARAMETER;
     69
     70    if (ppszNext != NULL)
     71        *ppszNext = pszNext;
     72    return VINF_SUCCESS;
     73}
     74
     75
     76RTDECL(int) RTNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr,
     77                                 char **ppszNext)
     78{
     79    return rtNetStrToIPv4AddrEx(pcszAddr, pAddr, ppszNext);
     80}
     81RT_EXPORT_SYMBOL(RTNetStrToIPv4AddrEx);
     82
     83
     84RTDECL(int) RTNetStrToIPv4Addr(const char *pcszAddr, PRTNETADDRIPV4 pAddr)
     85{
     86    char *pszNext;
     87    int rc;
     88
     89    AssertPtrReturn(pcszAddr, VERR_INVALID_PARAMETER);
     90    AssertPtrReturn(pAddr, VERR_INVALID_PARAMETER);
     91
     92    pcszAddr = RTStrStripL(pcszAddr);
     93    rc = rtNetStrToIPv4AddrEx(pcszAddr, pAddr, &pszNext);
     94    if (rc != VINF_SUCCESS)
     95        return VERR_INVALID_PARAMETER;
     96
    6597    pszNext = RTStrStripL(pszNext);
    66     if (*pszNext)
     98    if (*pszNext != '\0')
    6799        return VERR_INVALID_PARAMETER;
    68100
     
    71103RT_EXPORT_SYMBOL(RTNetStrToIPv4Addr);
    72104
     105
     106RTDECL(bool) RTNetIsIPv4AddrStr(const char *pcszAddr)
     107{
     108    RTNETADDRIPV4 addrIPv4;
     109    char *pszNext;
     110    int rc;
     111
     112    if (pcszAddr == NULL)
     113        return false;
     114
     115    rc = rtNetStrToIPv4AddrEx(pcszAddr, &addrIPv4, &pszNext);
     116    if (rc != VINF_SUCCESS)
     117        return false;
     118
     119    if (*pszNext != '\0')
     120        return false;
     121
     122    return true;
     123}
     124RT_EXPORT_SYMBOL(RTNetIsIPv4AddrStr);
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