VirtualBox

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


Ignore:
Timestamp:
Mar 4, 2009 3:18:37 AM (16 years ago)
Author:
vboxsync
Message:

IPRT: Added RTGETOPT_REQ_IPV4ADDR to RTGetOpt.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/getopt.cpp

    r17143 r17319  
    3232*   Header Files                                                               *
    3333*******************************************************************************/
     34#include <iprt/net.h>
    3435#include <iprt/getopt.h>
    3536#include <iprt/err.h>
     
    6465    /** @todo Add an flag for sorting the arguments so that all the options comes
    6566     *        first. */
     67    return VINF_SUCCESS;
     68}
     69
     70
     71/**
     72 * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
     73 *
     74 * This should be move to some generic part of the runtime.
     75 *
     76 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
     77 *          failure.
     78 *
     79 * @param   pszValue        The value to convert.
     80 * @param   pAddr           Where to store the result.
     81 */
     82static int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
     83{
     84    char *pszNext;
     85    int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 10, &pAddr->au8[0]);
     86    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
     87        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     88    if (*pszNext++ != '.')
     89        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     90
     91    rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
     92    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
     93        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     94    if (*pszNext++ != '.')
     95        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     96
     97    rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
     98    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
     99        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     100    if (*pszNext++ != '.')
     101        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     102
     103    rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
     104    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
     105        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     106    pszNext = RTStrStripL(pszNext);
     107    if (*pszNext)
     108        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     109
    66110    return VINF_SUCCESS;
    67111}
     
    315359                MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u,   RTStrToUInt32Full, 8)
    316360                MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u,   RTStrToUInt64Full, 8)
     361
    317362#undef MY_INT_CASE
    318363#undef MY_BASE_INT_CASE
     364
     365                case RTGETOPT_REQ_IPV4ADDR:
     366                {
     367                    RTNETADDRIPV4 Addr;
     368                    if (rtgetoptConvertIPv4Addr(pszValue, &Addr) != VINF_SUCCESS)
     369                        return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
     370                    pValueUnion->IPv4Addr = Addr;
     371                    break;
     372                }
    319373
    320374                default:
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