VirtualBox

Changeset 79616 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jul 9, 2019 12:28:41 AM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131918
Message:

VBoxManage/dhcpserver: Code cleanups, untested. bugref:9288

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageDHCPServer.cpp

    r79615 r79616  
    113113typedef DHCPDCMDCTX *PDHCPDCMDCTX;
    114114
    115 
    116 
    117 typedef enum enMainOpCodes
    118 {
    119     OP_ADD = 1000,
    120     OP_REMOVE,
    121     OP_MODIFY,
    122     OP_RESTART
    123 } OPCODE;
    124 
    125115typedef std::pair<DhcpOpt_T, std::string> DhcpOptSpec;
    126116typedef std::vector<DhcpOptSpec> DhcpOpts;
     
    136126
    137127    VmNameSlotKey(const std::string &aVmName, uint8_t aSlot)
    138       : VmName(aVmName)
    139       , u8Slot(aSlot)
     128        : VmName(aVmName)
     129        , u8Slot(aSlot)
    140130    {}
    141131
    142     bool operator< (const VmNameSlotKey& that) const
     132    bool operator<(const VmNameSlotKey& that) const
    143133    {
    144134        if (VmName == that.VmName)
    145135            return u8Slot < that.u8Slot;
    146         else
    147             return VmName < that.VmName;
     136        return VmName < that.VmName;
    148137    }
    149138};
     
    156145typedef VmSlot2OptionIdsM::iterator VmSlot2OptionIdsIterator;
    157146
    158 typedef std::vector<VmNameSlotKey> VmConfigs;
    159 
    160 
    161 /*********************************************************************************************************************************
    162 *   Global Variables                                                                                                             *
    163 *********************************************************************************************************************************/
    164 
    165 
    166 
    167 static RTEXITCODE dhcpdHandleTooManyCmds(PDHCPDCMDCTX pCtx, int argc, char **argv)
    168 {
    169     // glue - start
    170     HandlerArg *a       = pCtx->pArg;
    171     OPCODE      enmCode = pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD ? OP_ADD
    172                         : pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_MODIFY ? OP_MODIFY
    173                         : pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_REMOVE ? OP_REMOVE : OP_RESTART;
    174     RT_NOREF(argc, argv);
    175     // glue - end
    176 
    177     static const RTGETOPTDEF s_aDHCPIPOptions[] =
     147
     148
     149/**
     150 * Helper that find the DHCP server instance.
     151 *
     152 * @returns The DHCP server instance. NULL if failed (complaining done).
     153 * @param   pCtx                The DHCP server command context.
     154 */
     155static ComPtr<IDHCPServer> dhcpdFindServer(PDHCPDCMDCTX pCtx)
     156{
     157    ComPtr<IDHCPServer> ptrRet;
     158    if (pCtx->pszNetwork || pCtx->pszInterface)
     159    {
     160        Assert(pCtx->pszNetwork == NULL || pCtx->pszInterface == NULL);
     161
     162        /*
     163         * We need a network name to find the DHCP server.  So, if interface is
     164         * given we have to look it up.
     165         */
     166        HRESULT hrc;
     167        Bstr bstrNetName(pCtx->pszNetwork);
     168        if (!pCtx->pszNetwork)
     169        {
     170            ComPtr<IHost> ptrIHost;
     171            CHECK_ERROR2_RET(hrc, pCtx->pArg->virtualBox, COMGETTER(Host)(ptrIHost.asOutParam()), ptrRet);
     172
     173            Bstr bstrInterface(pCtx->pszInterface);
     174            ComPtr<IHostNetworkInterface> ptrIHostIf;
     175            CHECK_ERROR2(hrc, ptrIHost, FindHostNetworkInterfaceByName(bstrInterface.raw(), ptrIHostIf.asOutParam()));
     176            if (FAILED(hrc))
     177            {
     178                errorArgument("Failed to locate host-only interface '%s'", pCtx->pszInterface);
     179                return ptrRet;
     180            }
     181
     182            CHECK_ERROR2_RET(hrc, ptrIHostIf, COMGETTER(NetworkName)(bstrNetName.asOutParam()), ptrRet);
     183        }
     184
     185        /*
     186         * Now, try locate the server
     187         */
     188        hrc = pCtx->pArg->virtualBox->FindDHCPServerByNetworkName(bstrNetName.raw(), ptrRet.asOutParam());
     189        if (SUCCEEDED(hrc))
     190            return ptrRet;
     191        if (pCtx->pszNetwork)
     192            errorArgument("Failed to find DHCP server for network '%s'", pCtx->pszNetwork);
     193        else
     194            errorArgument("Failed to find DHCP server for host-only interface '%s' (network '%ls')",
     195                          pCtx->pszInterface, bstrNetName.raw());
     196    }
     197    else
     198        errorSyntax("You need to specify either --network or --interface to identify the DHCP server");
     199    return ptrRet;
     200}
     201
     202
     203/**
     204 * Handles the 'add' and 'modify' subcommands.
     205 */
     206static RTEXITCODE dhcpdHandleAddAndModify(PDHCPDCMDCTX pCtx, int argc, char **argv)
     207{
     208    /*
     209     * Parse options.
     210     */
     211    static const RTGETOPTDEF s_aOptions[] =
    178212    {
    179213        DHCPD_CMD_COMMON_OPTION_DEFS(),
     
    202236    };
    203237
    204     bool fNeedValueOrRemove = false; /* Only used with --id; remove in 6.1+ */
    205 
    206     const char *pszDhcpdIp = NULL;
    207     const char *pszNetmask = NULL;
    208     const char *pszLowerIp = NULL;
    209     const char *pszUpperIp = NULL;
    210     int         fEnabled   = -1;
     238    const char       *pszDhcpdIp = NULL;
     239    const char       *pszNetmask = NULL;
     240    const char       *pszLowerIp = NULL;
     241    const char       *pszUpperIp = NULL;
     242    int               fEnabled   = -1;
    211243
    212244    DhcpOpts          GlobalDhcpOptions;
     
    214246    VmSlot2OptionsM   VmSlot2Options;
    215247    VmSlot2OptionIdsM VmSlot2Options2Delete;
    216     /// @todo what was  this for: VmConfigs         VmConfigs2Delete;
    217248
    218249    const char       *pszVmName             = NULL;
    219     uint8_t           u8OptId               = 0;
    220250    uint8_t           u8Slot                = 0;
    221251    DhcpOpts         *pScopeOptions         = &GlobalDhcpOptions;
    222252    DhcpOptIds       *pScopeOptions2Delete  = &GlobalDhcpOptions2Delete;
    223253
    224     int c;
     254    bool              fNeedValueOrRemove    = false; /* Only used with --id; remove in 6.1+ */
     255    uint8_t           u8OptId               = 0;     /* Only used too keep --id for following --value/--remove. remove in 6.1+ */
     256
     257    RTGETOPTSTATE GetState;
     258    int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
     259    AssertRCReturn(vrc, RTEXITCODE_FAILURE);
     260
    225261    RTGETOPTUNION ValueUnion;
    226     RTGETOPTSTATE GetState;
    227     RTGetOptInit(&GetState,
    228                  a->argc,
    229                  a->argv,
    230                  s_aDHCPIPOptions,
    231                  enmCode != OP_REMOVE ? RT_ELEMENTS(s_aDHCPIPOptions) : 4, /* we use only --netname and --ifname for remove*/
    232                  1,
    233                  0);
    234     while ((c = RTGetOpt(&GetState, &ValueUnion)))
    235     {
    236         switch (c)
     262    while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
     263    {
     264        switch (vrc)
    237265        {
    238             DHCPD_CMD_COMMON_OPTION_CASES(pCtx, c, &ValueUnion);
     266            DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
    239267            case 'a':   // -ip
    240268                pszDhcpdIp = ValueUnion.psz;
     
    288316            {
    289317                uint8_t const idAddOpt = ValueUnion.u8;
    290                 c = RTGetOptFetchValue(&GetState, &ValueUnion, RTGETOPT_REQ_STRING);
    291                 if (RT_FAILURE(c))
    292                     return errorFetchValue(1, "--add-opt", c, &ValueUnion);
     318                vrc = RTGetOptFetchValue(&GetState, &ValueUnion, RTGETOPT_REQ_STRING);
     319                if (RT_FAILURE(vrc))
     320                    return errorFetchValue(1, "--add-opt", vrc, &ValueUnion);
    293321                pScopeOptions->push_back(DhcpOptSpec((DhcpOpt_T)idAddOpt, std::string(ValueUnion.psz)));
    294322                break;
     
    296324
    297325            case 'D':   // --del-opt num
    298                 if (enmCode == OP_ADD)
     326                if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
    299327                    return errorSyntax("--del-opt does not apply to the 'add' subcommand");
    300328                pScopeOptions2Delete->push_back((DhcpOpt_T)ValueUnion.u8);
     
    323351
    324352            case 'r':   // --remove
    325                 if (enmCode == OP_ADD)
     353                if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
    326354                    return errorSyntax("--remove does not apply to the 'add' subcommand");
    327355                if (!fNeedValueOrRemove)
     
    333361
    334362            default:
    335                 return errorGetOpt(c, &ValueUnion);
     363                return errorGetOpt(vrc, &ValueUnion);
    336364        }
    337365    }
    338366
     367    /*
     368     * Ensure we've got mandatory options and supply defaults
     369     * where needed (modify case)
     370     */
    339371    if (!pCtx->pszNetwork && !pCtx->pszInterface)
    340372        return errorSyntax("You need to specify either --network or --interface to identify the DHCP server");
    341373
    342     if (   enmCode != OP_REMOVE
    343         && enmCode != OP_RESTART
    344         && GlobalDhcpOptions.empty()
    345         && VmSlot2Options.empty()
    346         && GlobalDhcpOptions2Delete.empty()
    347         && VmSlot2Options2Delete.empty())
    348     {
    349         /** @todo For the 'modify' subcmd, missing configuration parameters could be
    350          * retrieved from the current config.  All are only required for 'add'!
    351          * The 'fEnabled' attribute does not come into play here.  */
    352         if (fEnabled < 0 || pszDhcpdIp || pszNetmask || pszLowerIp || pszUpperIp)
    353         {
    354             RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
    355             if (!pszDhcpdIp)
    356                 rcExit = errorSyntax("Missing required option: --ip");
    357             if (!pszNetmask)
    358                 rcExit = errorSyntax("Missing required option: --netmask");
    359             if (!pszLowerIp)
    360                 rcExit = errorSyntax("Missing required option: --lowerip");
    361             if (!pszUpperIp)
    362                 rcExit = errorSyntax("Missing required option: --upperip");
    363             if (rcExit != RTEXITCODE_SUCCESS)
    364                 return rcExit;
    365         }
    366     }
    367 
     374    if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
     375    {
     376        RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
     377        if (!pszDhcpdIp)
     378            rcExit = errorSyntax("Missing required option: --ip");
     379        if (!pszNetmask)
     380            rcExit = errorSyntax("Missing required option: --netmask");
     381        if (!pszLowerIp)
     382            rcExit = errorSyntax("Missing required option: --lowerip");
     383        if (!pszUpperIp)
     384            rcExit = errorSyntax("Missing required option: --upperip");
     385        if (rcExit != RTEXITCODE_SUCCESS)
     386            return rcExit;
     387    }
     388
     389    /*
     390     * Find or create the server.
     391     */
    368392    HRESULT rc;
    369393    Bstr NetName;
     
    371395    {
    372396        ComPtr<IHost> host;
    373         CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
     397        CHECK_ERROR(pCtx->pArg->virtualBox, COMGETTER(Host)(host.asOutParam()));
    374398
    375399        ComPtr<IHostNetworkInterface> hif;
     
    388412
    389413    ComPtr<IDHCPServer> svr;
    390     rc = a->virtualBox->FindDHCPServerByNetworkName(NetName.mutableRaw(), svr.asOutParam());
    391     if(enmCode == OP_ADD)
     414    rc = pCtx->pArg->virtualBox->FindDHCPServerByNetworkName(NetName.mutableRaw(), svr.asOutParam());
     415    if (pCtx->pCmdDef->fSubcommandScope == HELP_SCOPE_DHCPSERVER_ADD)
    392416    {
    393417        if (SUCCEEDED(rc))
    394418            return errorArgument("DHCP server already exists");
    395419
    396         CHECK_ERROR(a->virtualBox, CreateDHCPServer(NetName.mutableRaw(), svr.asOutParam()));
     420        CHECK_ERROR(pCtx->pArg->virtualBox, CreateDHCPServer(NetName.mutableRaw(), svr.asOutParam()));
    397421        if (FAILED(rc))
    398422            return errorArgument("Failed to create the DHCP server");
    399423    }
    400424    else if (FAILED(rc))
    401     {
    402425        return errorArgument("DHCP server does not exist");
    403     }
    404 
    405     if (enmCode == OP_RESTART)
    406     {
    407         CHECK_ERROR(svr, Restart());
    408         if(FAILED(rc))
    409             return errorArgument("Failed to restart server");
    410     }
    411     else if (enmCode == OP_REMOVE)
    412     {
    413         CHECK_ERROR(a->virtualBox, RemoveDHCPServer(svr));
    414         if(FAILED(rc))
    415             return errorArgument("Failed to remove server");
    416     }
    417     else
    418     {
    419         if (pszDhcpdIp || pszNetmask || pszLowerIp || pszUpperIp)
     426
     427    /*
     428     * Apply settings.
     429     */
     430    HRESULT hrc;
     431    RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
     432    if (pszDhcpdIp || pszNetmask || pszLowerIp || pszUpperIp)
     433    {
     434        Bstr bstrDhcpdIp(pszDhcpdIp);
     435        Bstr bstrNetmask(pszNetmask);
     436        Bstr bstrLowerIp(pszLowerIp);
     437        Bstr bstrUpperIp(pszUpperIp);
     438
     439        if (!pszDhcpdIp)
     440            CHECK_ERROR2_RET(hrc, svr, COMGETTER(IPAddress)(bstrDhcpdIp.asOutParam()), RTEXITCODE_FAILURE);
     441        if (!pszNetmask)
     442            CHECK_ERROR2_RET(hrc, svr, COMGETTER(NetworkMask)(bstrNetmask.asOutParam()), RTEXITCODE_FAILURE);
     443        if (!pszLowerIp)
     444            CHECK_ERROR2_RET(hrc, svr, COMGETTER(LowerIP)(bstrNetmask.asOutParam()), RTEXITCODE_FAILURE);
     445        if (!pszUpperIp)
     446            CHECK_ERROR2_RET(hrc, svr, COMGETTER(UpperIP)(bstrNetmask.asOutParam()), RTEXITCODE_FAILURE);
     447
     448        CHECK_ERROR2_STMT(hrc, svr, SetConfiguration(bstrDhcpdIp.raw(), bstrNetmask.raw(), bstrLowerIp.raw(), bstrUpperIp.raw()),
     449                          rcExit = errorArgument("Failed to set configuration"));
     450    }
     451
     452    if (fEnabled >= 0)
     453        CHECK_ERROR2_STMT(hrc, svr, COMSETTER(Enabled)((BOOL)fEnabled), rcExit = RTEXITCODE_FAILURE);
     454
     455    /* Remove options: */
     456    for (DhcpOptIdIterator itOptId = GlobalDhcpOptions2Delete.begin(); itOptId != GlobalDhcpOptions2Delete.end(); ++itOptId)
     457        CHECK_ERROR2_STMT(hrc, svr, RemoveGlobalOption(*itOptId), rcExit = RTEXITCODE_FAILURE);
     458
     459    for (VmSlot2OptionIdsIterator itIdVector = VmSlot2Options2Delete.begin();
     460         itIdVector != VmSlot2Options2Delete.end(); ++itIdVector)
     461        for (DhcpOptIdIterator itOptId = itIdVector->second.begin(); itOptId != itIdVector->second.end(); ++itOptId)
     462            CHECK_ERROR2_STMT(hrc, svr, RemoveVmSlotOption(Bstr(itIdVector->first.VmName.c_str()).raw(),
     463                                                           itIdVector->first.u8Slot, *itOptId),
     464                              rcExit = RTEXITCODE_FAILURE);
     465
     466    /* Global Options */
     467    for (DhcpOptIterator itOpt = GlobalDhcpOptions.begin(); itOpt != GlobalDhcpOptions.end(); ++itOpt)
     468        CHECK_ERROR2_STMT(hrc, svr, AddGlobalOption(itOpt->first, com::Bstr(itOpt->second.c_str()).raw()),
     469                          rcExit = RTEXITCODE_FAILURE);
     470
     471    /* VM slot options. */
     472    for (VmSlot2OptionsIterator it = VmSlot2Options.begin(); it != VmSlot2Options.end(); ++it)
     473        for (DhcpOptIterator itOpt = it->second.begin(); itOpt != it->second.end(); ++itOpt)
     474            CHECK_ERROR2_STMT(hrc, svr, AddVmSlotOption(Bstr(it->first.VmName.c_str()).raw(), it->first.u8Slot, itOpt->first,
     475                                                        com::Bstr(itOpt->second.c_str()).raw()),
     476                              rcExit = RTEXITCODE_FAILURE);
     477
     478    return rcExit;
     479}
     480
     481
     482/**
     483 * Handles the 'remove' subcommand.
     484 */
     485static RTEXITCODE dhcpdHandleRemove(PDHCPDCMDCTX pCtx, int argc, char **argv)
     486{
     487    /*
     488     * Parse the command line.
     489     */
     490    static const RTGETOPTDEF s_aOptions[] =
     491    {
     492        DHCPD_CMD_COMMON_OPTION_DEFS(),
     493    };
     494
     495    RTGETOPTSTATE   GetState;
     496    int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
     497    AssertRCReturn(vrc, RTEXITCODE_FAILURE);
     498
     499    RTGETOPTUNION   ValueUnion;
     500    while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
     501    {
     502        switch (vrc)
    420503        {
    421             CHECK_ERROR(svr, SetConfiguration(Bstr(pszDhcpdIp).mutableRaw(),
    422                                               Bstr(pszNetmask).mutableRaw(),
    423                                               Bstr(pszLowerIp).mutableRaw(),
    424                                               Bstr(pszUpperIp).mutableRaw()));
    425             if (FAILED(rc))
    426                 return errorArgument("Failed to set configuration");
     504            DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
     505            default:
     506                return errorGetOpt(vrc, &ValueUnion);
    427507        }
    428 
    429         if (fEnabled >= 0)
    430             CHECK_ERROR(svr, COMSETTER(Enabled)((BOOL)fEnabled));
    431 
    432         /* Remove options: */
    433         for (DhcpOptIdIterator itOptId = GlobalDhcpOptions2Delete.begin(); itOptId != GlobalDhcpOptions2Delete.end(); ++itOptId)
    434             CHECK_ERROR(svr, RemoveGlobalOption(*itOptId));
    435 
    436         for (VmSlot2OptionIdsIterator itIdVector = VmSlot2Options2Delete.begin();
    437              itIdVector != VmSlot2Options2Delete.end(); ++itIdVector)
    438             for (DhcpOptIdIterator itOptId = itIdVector->second.begin(); itOptId != itIdVector->second.end(); ++itOptId)
    439                 CHECK_ERROR(svr, RemoveVmSlotOption(Bstr(itIdVector->first.VmName.c_str()).raw(),
    440                                                     itIdVector->first.u8Slot, *itOptId));
    441 
    442         /* Global Options */
    443         for (DhcpOptIterator itOpt = GlobalDhcpOptions.begin(); itOpt != GlobalDhcpOptions.end(); ++itOpt)
    444             CHECK_ERROR(svr, AddGlobalOption(itOpt->first, com::Bstr(itOpt->second.c_str()).raw()));
    445 
    446         /* VM slot options. */
    447         for (VmSlot2OptionsIterator it = VmSlot2Options.begin(); it != VmSlot2Options.end(); ++it)
    448             for (DhcpOptIterator itOpt = it->second.begin(); itOpt != it->second.end(); ++itOpt)
    449                 CHECK_ERROR(svr, AddVmSlotOption(Bstr(it->first.VmName.c_str()).raw(), it->first.u8Slot, itOpt->first,
    450                                                  com::Bstr(itOpt->second.c_str()).raw()));
    451     }
    452 
    453     return RTEXITCODE_SUCCESS;
     508    }
     509
     510    /*
     511     * Locate the server and perform the requested operation.
     512     */
     513    ComPtr<IDHCPServer> ptrDHCPServer = dhcpdFindServer(pCtx);
     514    if (ptrDHCPServer.isNotNull())
     515    {
     516        HRESULT hrc;
     517        CHECK_ERROR2(hrc, pCtx->pArg->virtualBox, RemoveDHCPServer(ptrDHCPServer));
     518        if (SUCCEEDED(hrc))
     519            return RTEXITCODE_SUCCESS;
     520        errorArgument("Failed to remove server");
     521    }
     522    return RTEXITCODE_FAILURE;
    454523}
    455524
    456525
    457526/**
    458  * Helper that find the DHCP server instance.
    459  *
    460  * @returns The DHCP server instance. NULL if failed (complaining done).
    461  * @param   pCtx                The DHCP server command context.
    462  */
    463 static ComPtr<IDHCPServer> dhcpdFindServer(PDHCPDCMDCTX pCtx)
    464 {
    465     ComPtr<IDHCPServer> ptrRet;
    466     if (pCtx->pszNetwork || pCtx->pszInterface)
    467     {
    468         Assert(pCtx->pszNetwork == NULL || pCtx->pszInterface == NULL);
    469 
    470         /*
    471          * We need a network name to find the DHCP server.  So, if interface is
    472          * given we have to look it up.
    473          */
     527 * Handles the 'restart' subcommand.
     528 */
     529static RTEXITCODE dhcpdHandleRestart(PDHCPDCMDCTX pCtx, int argc, char **argv)
     530{
     531    /*
     532     * Parse the command line.
     533     */
     534    static const RTGETOPTDEF s_aOptions[] =
     535    {
     536        DHCPD_CMD_COMMON_OPTION_DEFS(),
     537    };
     538
     539    RTGETOPTSTATE   GetState;
     540    int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
     541    AssertRCReturn(vrc, RTEXITCODE_FAILURE);
     542
     543    RTGETOPTUNION   ValueUnion;
     544    while ((vrc = RTGetOpt(&GetState, &ValueUnion)))
     545    {
     546        switch (vrc)
     547        {
     548            DHCPD_CMD_COMMON_OPTION_CASES(pCtx, vrc, &ValueUnion);
     549            default:
     550                return errorGetOpt(vrc, &ValueUnion);
     551        }
     552    }
     553
     554    /*
     555     * Locate the server and perform the requested operation.
     556     */
     557    ComPtr<IDHCPServer> ptrDHCPServer = dhcpdFindServer(pCtx);
     558    if (ptrDHCPServer.isNotNull())
     559    {
    474560        HRESULT hrc;
    475         Bstr bstrNetName(pCtx->pszNetwork);
    476         if (!pCtx->pszNetwork)
    477         {
    478             ComPtr<IHost> ptrIHost;
    479             CHECK_ERROR2_RET(hrc, pCtx->pArg->virtualBox, COMGETTER(Host)(ptrIHost.asOutParam()), ptrRet);
    480 
    481             Bstr bstrInterface(pCtx->pszInterface);
    482             ComPtr<IHostNetworkInterface> ptrIHostIf;
    483             CHECK_ERROR2(hrc, ptrIHost, FindHostNetworkInterfaceByName(bstrInterface.raw(), ptrIHostIf.asOutParam()));
    484             if (FAILED(hrc))
    485             {
    486                 errorArgument("Failed to locate host-only interface '%s'", pCtx->pszInterface);
    487                 return ptrRet;
    488             }
    489 
    490             CHECK_ERROR2_RET(hrc, ptrIHostIf, COMGETTER(NetworkName)(bstrNetName.asOutParam()), ptrRet);
    491         }
    492 
    493         /*
    494          * Now, try locate the server
    495          */
    496         hrc = pCtx->pArg->virtualBox->FindDHCPServerByNetworkName(bstrNetName.raw(), ptrRet.asOutParam());
     561        CHECK_ERROR2(hrc, ptrDHCPServer, Restart());
    497562        if (SUCCEEDED(hrc))
    498             return ptrRet;
    499         if (pCtx->pszNetwork)
    500             errorArgument("Failed to find DHCP server for network '%s'", pCtx->pszNetwork);
    501         else
    502             errorArgument("Failed to find DHCP server for host-only interface '%s' (network '%ls')",
    503                           pCtx->pszInterface, bstrNetName.raw());
    504     }
    505     else
    506         errorSyntax("You need to specify either --network or --interface to identify the DHCP server");
    507     return ptrRet;
     563            return RTEXITCODE_SUCCESS;
     564        errorArgument("Failed to restart server");
     565    }
     566    return RTEXITCODE_FAILURE;
    508567}
    509568
    510569
     570/**
     571 * Handles the 'findlease' subcommand.
     572 */
    511573static RTEXITCODE dhcpdHandleFindLease(PDHCPDCMDCTX pCtx, int argc, char **argv)
    512574{
     
    592654
    593655
     656/**
     657 * Handles the 'dhcpserver' command.
     658 */
    594659RTEXITCODE handleDHCPServer(HandlerArg *pArg)
    595660{
     
    599664    static const DHCPDCMDDEF s_aCmdDefs[] =
    600665    {
    601         { "add",            dhcpdHandleTooManyCmds,     HELP_SCOPE_DHCPSERVER_ADD },
    602         { "modify",         dhcpdHandleTooManyCmds,     HELP_SCOPE_DHCPSERVER_MODIFY },
    603         { "remove",         dhcpdHandleTooManyCmds,     HELP_SCOPE_DHCPSERVER_REMOVE },
    604         { "restart",        dhcpdHandleTooManyCmds,     HELP_SCOPE_DHCPSERVER_RESTART },
     666        { "add",            dhcpdHandleAddAndModify,    HELP_SCOPE_DHCPSERVER_ADD },
     667        { "modify",         dhcpdHandleAddAndModify,    HELP_SCOPE_DHCPSERVER_MODIFY },
     668        { "remove",         dhcpdHandleRemove,          HELP_SCOPE_DHCPSERVER_REMOVE },
     669        { "restart",        dhcpdHandleRestart,         HELP_SCOPE_DHCPSERVER_RESTART },
    605670        { "findlease",      dhcpdHandleFindLease,       HELP_SCOPE_DHCPSERVER_FINDLEASE },
    606671    };
     
    616681
    617682    static const RTGETOPTDEF s_CommonOptions[] = { DHCPD_CMD_COMMON_OPTION_DEFS() };
    618 
    619     int ch;
     683    RTGETOPTSTATE GetState;
     684    int vrc = RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_CommonOptions, RT_ELEMENTS(s_CommonOptions), 0,
     685                           0 /* No sorting! */);
     686    AssertRCReturn(vrc, RTEXITCODE_FAILURE);
     687
    620688    RTGETOPTUNION ValueUnion;
    621     RTGETOPTSTATE GetState;
    622     RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_CommonOptions, RT_ELEMENTS(s_CommonOptions), 0, 0 /* No sorting! */);
    623 
    624     while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
    625     {
    626         switch (ch)
     689    while ((vrc = RTGetOpt(&GetState, &ValueUnion)) != 0)
     690    {
     691        switch (vrc)
    627692        {
    628             DHCPD_CMD_COMMON_OPTION_CASES(&CmdCtx, ch, &ValueUnion);
     693            DHCPD_CMD_COMMON_OPTION_CASES(&CmdCtx, vrc, &ValueUnion);
    629694
    630695            case VINF_GETOPT_NOT_OPTION:
     
    644709
    645710            default:
    646                 return errorGetOpt(ch, &ValueUnion);
     711                return errorGetOpt(vrc, &ValueUnion);
    647712        }
    648713    }
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