VirtualBox

Ignore:
Timestamp:
Feb 19, 2021 9:37:34 AM (4 years ago)
Author:
vboxsync
Message:

NAT/Net: Do our own getopt, we only need one option really (the
network name) and we get the rest via the API. Main now starts us
with just the network name. First step towards divorcing from the old
base class. bugref:9929.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp

    r87791 r87798  
    4545#include <iprt/param.h>
    4646#include <iprt/pipe.h>
    47 #include <iprt/getopt.h>
    4847#include <iprt/string.h>
    4948#include <iprt/mem.h>
     
    5453#include <iprt/cpp/utils.h>
    5554#include <VBox/log.h>
     55
     56#include <iprt/buildconfig.h>
     57#include <iprt/getopt.h>
     58#include <iprt/process.h>
    5659
    5760#include <VBox/sup.h>
     
    182185    virtual ~VBoxNetLwipNAT();
    183186
    184     virtual void usage() { /** @todo should be implemented */ };
    185     virtual int parseOpt(int c, const RTGETOPTUNION &Value);
    186 
    187     virtual bool isMainNeeded() const { return true; }
     187    RTEXITCODE parseArgs(int argc, char *argv[]);
    188188
    189189    virtual int init();
     
    191191
    192192private:
     193    virtual int parseOpt(int c, const RTGETOPTUNION &Value);
     194    virtual void usage();
     195
     196    virtual bool isMainNeeded() const { return true; }
     197
    193198    int initCom();
    194199    int initHome();
     
    236241INTNETSEG VBoxNetLwipNAT::aXmitSeg[64];
    237242
    238 /**
    239  * Additional command line options.
    240  *
    241  * Our parseOpt() will be called by the base class if any of these are
    242  * supplied.
    243  */
    244 RTGETOPTDEF VBoxNetLwipNAT::s_aGetOptDef[] =
    245 {
    246     /*
    247      * Currently there are no extra options and since arrays can't be
    248      * empty use a sentinel entry instead, so that the placeholder
    249      * code to process the options can be supplied nonetheless.
    250      */
    251     {}                          /* sentinel */
    252 };
    253 
    254243
    255244
    256245VBoxNetLwipNAT::VBoxNetLwipNAT()
    257   : VBoxNetBaseService("VBoxNetNAT", "nat-network")
     246  : VBoxNetBaseService("VBoxNetNAT", "")
    258247{
    259248    LogFlowFuncEnter();
     
    292281    setMacAddress(mac);
    293282
    294     /* tell the base class about our command line options */
    295     for (PCRTGETOPTDEF pcOpt = &s_aGetOptDef[0]; pcOpt->iShort != 0; ++pcOpt)
    296         addCommandLineOption(pcOpt);
    297 
    298283    m_loOptDescriptor.lomap = NULL;
    299284    m_loOptDescriptor.num_lomap = 0;
     
    325310
    326311/**
    327  * Hook into the option processing.
     312 * Command line options.
    328313 *
    329  * Called by VBoxNetBaseService::parseArgs() for options that are not
    330  * recognized by the base class.  See s_aGetOptDef[].
     314 * @note This class is currently in transition away from being
     315 * inheritted from VBoxNetBaseService, so it no longer calls its
     316 * getopt code and has its own parseArgs() instead.
     317 */
     318RTGETOPTDEF VBoxNetLwipNAT::s_aGetOptDef[] =
     319{
     320    { "--network",              'n',   RTGETOPT_REQ_STRING },
     321    { "--verbose",              'v',   RTGETOPT_REQ_NOTHING },
     322};
     323
     324
     325/** Icky hack to tell the caller it should exit with RTEXITCODE_SUCCESS */
     326#define RTEXITCODE_DONE RTEXITCODE_32BIT_HACK
     327
     328void
     329VBoxNetLwipNAT::usage()
     330{
     331    RTPrintf("%s Version %sr%u\n"
     332             "(C) 2009-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
     333             "All rights reserved.\n"
     334             "\n"
     335             "Usage: %s <options>\n"
     336             "\n"
     337             "Options:\n",
     338             RTProcShortName(), RTBldCfgVersion(), RTBldCfgRevision(),
     339             RTProcShortName());
     340    for (size_t i = 0; i < RT_ELEMENTS(s_aGetOptDef); ++i)
     341        RTPrintf("    -%c, %s\n", s_aGetOptDef[i].iShort, s_aGetOptDef[i].pszLong);
     342}
     343
     344
     345RTEXITCODE
     346VBoxNetLwipNAT::parseArgs(int argc, char *argv[])
     347{
     348    unsigned int uVerbosity = 0;
     349    int rc;
     350
     351    RTGETOPTSTATE State;
     352    rc = RTGetOptInit(&State, argc, argv,
     353                      s_aGetOptDef, RT_ELEMENTS(s_aGetOptDef),
     354                      1, 0);
     355
     356    int ch;
     357    RTGETOPTUNION Val;
     358    while ((ch = RTGetOpt(&State, &Val)) != 0)
     359    {
     360        switch (ch)
     361        {
     362            case 'n':           /* --network */
     363                if (!getNetworkName().empty())
     364                    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "multiple --network options");
     365                setNetworkName(Val.psz);
     366                break;
     367
     368            case 'v':           /* --verbose */
     369                ++uVerbosity;
     370                break;
     371
     372
     373            /*
     374             * Standard options recognized by RTGetOpt()
     375             */
     376
     377            case 'V':           /* --version */
     378                RTPrintf("%sr%u\n", RTBldCfgVersion(), RTBldCfgRevision());
     379                return RTEXITCODE_DONE;
     380
     381            case 'h':           /* --help */
     382                usage();
     383                return RTEXITCODE_DONE;
     384
     385            case VINF_GETOPT_NOT_OPTION:
     386                return RTMsgErrorExit(RTEXITCODE_SYNTAX, "unexpected non-option argument");
     387
     388            default:
     389                return RTGetOptPrintError(ch, &Val);
     390        }
     391    }
     392
     393    if (getNetworkName().empty())
     394        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "missing --network option");
     395
     396    /* tell the base class while we're still joined at the hip */
     397    setVerbosityLevel((int32_t)uVerbosity);
     398
     399    return RTEXITCODE_SUCCESS;
     400}
     401
     402
     403/**
     404 * @note This class is currently in transition away from being
     405 * inheritted from VBoxNetBaseService, so it no longer calls its
     406 * getopt code and has its own parseArgs() instead.
    331407 */
    332408int VBoxNetLwipNAT::parseOpt(int c, const RTGETOPTUNION &Value)
     
    21172193    VBoxNetLwipNAT NAT;
    21182194
    2119     int rcExit = NAT.parseArgs(argc - 1, argv + 1);
     2195    int rcExit = NAT.parseArgs(argc, argv);
    21202196    if (rcExit != RTEXITCODE_SUCCESS)
    2121         return rcExit;          /* messages are already printed */
     2197    {
     2198        /* messages are already printed */
     2199        return rcExit == RTEXITCODE_DONE ? RTEXITCODE_SUCCESS : rcExit;
     2200    }
    21222201
    21232202    rc = NAT.init();
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