VirtualBox

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


Ignore:
Timestamp:
Mar 4, 2009 3:18:37 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
43691
Message:

IPRT: Added RTGETOPT_REQ_IPV4ADDR to RTGetOpt.

Location:
trunk/src/VBox/Runtime
Files:
2 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:
  • trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp

    r17141 r17319  
    3030
    3131
     32/*******************************************************************************
     33*   Header Files                                                               *
     34*******************************************************************************/
     35#include <iprt/net.h>
    3236#include <iprt/getopt.h>
    3337#include <iprt/stream.h>
     
    8387        { "nodash",             387, RTGETOPT_REQ_NOTHING },
    8488        { "nodashval",          388, RTGETOPT_REQ_STRING },
     89        { "--gateway",          'g', RTGETOPT_REQ_IPV4ADDR },
    8590    };
    8691
     
    118123
    119124        "-vqi999",
     125
     126        "-g192.168.1.1",
    120127        NULL
    121128    };
     
    191198    CHECK(Val.i32 == 999);
    192199
     200    /* IPv4 */
     201    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'g', 1);
     202    CHECK(Val.IPv4Addr.u == RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8(192,168,1,1))));
     203
    193204    /* the end */
    194205    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
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