VirtualBox

Changeset 75819 in vbox


Ignore:
Timestamp:
Nov 29, 2018 4:16:40 PM (6 years ago)
Author:
vboxsync
Message:

Main/DHCPServer: (bugref:9288) Allow removal of DHCP options, show options when listing DHCP servers.

Location:
trunk/src/VBox
Files:
6 edited

Legend:

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

    r75648 r75819  
    6060typedef DhcpOpts::iterator DhcpOptIterator;
    6161
     62typedef std::vector<DhcpOpt_T> DhcpOptIds;
     63typedef DhcpOptIds::iterator DhcpOptIdIterator;
     64
    6265struct VmNameSlotKey
    6366{
     
    8285typedef VmSlot2OptionsM::iterator VmSlot2OptionsIterator;
    8386typedef VmSlot2OptionsM::value_type VmSlot2OptionsPair;
     87
     88typedef std::map<VmNameSlotKey, DhcpOptIds> VmSlot2OptionIdsM;
     89typedef VmSlot2OptionIdsM::iterator VmSlot2OptionIdsIterator;
    8490
    8591typedef std::vector<VmNameSlotKey> VmConfigs;
     
    112118    { "--slot",             's', RTGETOPT_REQ_UINT8}, /* only with -o and -n */
    113119    { "--id",               'i', RTGETOPT_REQ_UINT8}, /* only with -o */
    114     { "--value",            'p', RTGETOPT_REQ_STRING} /* only with -i */
     120    { "--value",            'p', RTGETOPT_REQ_STRING}, /* only with -i */
     121    { "--remove",           'r', RTGETOPT_REQ_NOTHING} /* only with -i */
    115122};
    116123
     
    138145    int enable = -1;
    139146
    140     DhcpOpts        GlobalDhcpOptions;
    141     VmSlot2OptionsM VmSlot2Options;
    142     VmConfigs       VmConfigs2Delete;
     147    DhcpOpts          GlobalDhcpOptions;
     148    DhcpOptIds        GlobalDhcpOptions2Delete;
     149    VmSlot2OptionsM   VmSlot2Options;
     150    VmSlot2OptionIdsM VmSlot2Options2Delete;
     151    VmConfigs         VmConfigs2Delete;
    143152
    144153    int c;
     
    299308                break; // --end of value
    300309
     310            case 'r': /* --remove */
     311                {
     312                    if (!fOptionsRead)
     313                        return errorSyntax(USAGE_DHCPSERVER,
     314                                           "-o wasn't found");
     315
     316                    if (u8OptId == (uint8_t)~0)
     317                        return errorSyntax(USAGE_DHCPSERVER,
     318                                           "--id wasn't found");
     319                    if (   fVmOptionRead
     320                        && u8Slot == (uint8_t)~0)
     321                        return errorSyntax(USAGE_DHCPSERVER,
     322                                           "--slot wasn't found");
     323
     324                    DhcpOptIds &optIds = fVmOptionRead ? VmSlot2Options2Delete[VmNameSlotKey(pszVmName, u8Slot)]
     325                                                       : GlobalDhcpOptions2Delete;
     326                    optIds.push_back((DhcpOpt_T)u8OptId);
     327                }
     328                break; /* --end of remove */
     329
    301330            default:
    302331                if (c > 0)
     
    322351       && enmCode != OP_RESTART
    323352       && GlobalDhcpOptions.empty()
    324        && VmSlot2Options.empty())
     353       && VmSlot2Options.empty()
     354       && GlobalDhcpOptions2Delete.empty()
     355       && VmSlot2Options2Delete.empty())
    325356    {
    326357        if(enable < 0 || pIp || pNetmask || pLowerIp || pUpperIp)
     
    404435        {
    405436            CHECK_ERROR(svr, COMSETTER(Enabled) ((BOOL)enable));
     437        }
     438
     439        /* remove specified options */
     440        DhcpOptIdIterator itOptId;
     441        for (itOptId = GlobalDhcpOptions2Delete.begin();
     442             itOptId != GlobalDhcpOptions2Delete.end();
     443             ++itOptId)
     444        {
     445            CHECK_ERROR(svr, RemoveGlobalOption(*itOptId));
     446        }
     447        VmSlot2OptionIdsIterator itIdVector;
     448        for (itIdVector = VmSlot2Options2Delete.begin();
     449             itIdVector != VmSlot2Options2Delete.end();
     450             ++itIdVector)
     451        {
     452            for(itOptId = itIdVector->second.begin();
     453                itOptId != itIdVector->second.end();
     454                ++itOptId)
     455            {
     456                CHECK_ERROR(svr,
     457                            RemoveVmSlotOption(Bstr(itIdVector->first.VmName.c_str()).raw(),
     458                                               itIdVector->first.u8Slot,
     459                                               *itOptId));
     460            }
    406461        }
    407462
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp

    r75365 r75819  
    12101210                     "                            --lowerip <lower_ip>\n"
    12111211                     "                            --upperip <upper_ip>]\n"
    1212                      "                            [--enable | --disable]\n\n"
     1212                     "                            [--enable | --disable]\n"
     1213                     "                            [--options [--vm <name> --slot <number>]\n"
     1214                     "                             --id <number> [--value <string> | --remove]]\n"
     1215                     "                             (multiple options allowed after --options)\n\n"
    12131216                           "%s dhcpserver %s      remove --netname <network_name> |\n"
    12141217#if defined(VBOX_WITH_NETFLT)
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageList.cpp

    r74432 r75819  
    778778
    779779/**
     780 * Helper function for querying and displaying DHCP option for an adapter.
     781 *
     782 * @returns See produceList.
     783 * @param   pSrv                Smart pointer to IDHCPServer.
     784 * @param   vmSlot              String identifying the adapter, like '[vmname]:slot'
     785 */
     786static HRESULT listVmSlotDhcpOptions(const ComPtr<IDHCPServer> pSrv, const Utf8Str& vmSlot)
     787{
     788    RTCList<RTCString> lstParts = vmSlot.split(":");
     789    if (lstParts.size() < 2)
     790        return E_INVALIDARG;
     791    if (lstParts[0].length() < 2 || !lstParts[0].startsWith("[") || !lstParts[0].endsWith("]"))
     792        return E_INVALIDARG;
     793    Bstr vmName(lstParts[0].substr(1, lstParts[0].length()-2));
     794    ULONG uSlot = lstParts[1].toUInt32();
     795    com::SafeArray<BSTR> options;
     796    CHECK_ERROR2I_RET(pSrv, GetVmSlotOptions(vmName.raw(), uSlot, ComSafeArrayAsOutParam(options)), hrcCheck);
     797    if (options.size())
     798        RTPrintf("Options for slot #%d of '%ls':\n", uSlot, vmName.raw());
     799    for (size_t i = 0; i < options.size(); ++i)
     800    {
     801        RTPrintf("   %ls\n", options[i]);
     802    }
     803    return S_OK;
     804}
     805
     806
     807/**
     808 * List DHCP servers.
     809 *
     810 * @returns See produceList.
     811 * @param   pVirtualBox         Reference to the IVirtualBox smart pointer.
     812 */
     813static HRESULT listDhcpServers(const ComPtr<IVirtualBox> &pVirtualBox)
     814{
     815    HRESULT rc = S_OK;
     816    com::SafeIfaceArray<IDHCPServer> svrs;
     817    CHECK_ERROR_RET(pVirtualBox, COMGETTER(DHCPServers)(ComSafeArrayAsOutParam(svrs)), rc);
     818    for (size_t i = 0; i < svrs.size(); ++i)
     819    {
     820        ComPtr<IDHCPServer> svr = svrs[i];
     821        Bstr netName;
     822        svr->COMGETTER(NetworkName)(netName.asOutParam());
     823        RTPrintf("NetworkName:    %ls\n", netName.raw());
     824        Bstr ip;
     825        svr->COMGETTER(IPAddress)(ip.asOutParam());
     826        RTPrintf("IP:             %ls\n", ip.raw());
     827        Bstr netmask;
     828        svr->COMGETTER(NetworkMask)(netmask.asOutParam());
     829        RTPrintf("NetworkMask:    %ls\n", netmask.raw());
     830        Bstr lowerIp;
     831        svr->COMGETTER(LowerIP)(lowerIp.asOutParam());
     832        RTPrintf("lowerIPAddress: %ls\n", lowerIp.raw());
     833        Bstr upperIp;
     834        svr->COMGETTER(UpperIP)(upperIp.asOutParam());
     835        RTPrintf("upperIPAddress: %ls\n", upperIp.raw());
     836        BOOL fEnabled;
     837        svr->COMGETTER(Enabled)(&fEnabled);
     838        RTPrintf("Enabled:        %s\n", fEnabled ? "Yes" : "No");
     839        com::SafeArray<BSTR> globalOptions;
     840        CHECK_ERROR_BREAK(svr, COMGETTER(GlobalOptions)(ComSafeArrayAsOutParam(globalOptions)));
     841        if (globalOptions.size())
     842        {
     843            RTPrintf("Global options:\n");
     844            for (size_t j = 0; j < globalOptions.size(); ++j)
     845                RTPrintf("   %ls\n", globalOptions[j]);
     846        }
     847        com::SafeArray<BSTR> vmConfigs;
     848        CHECK_ERROR_BREAK(svr, COMGETTER(VmConfigs)(ComSafeArrayAsOutParam(vmConfigs)));
     849        for (size_t j = 0; j < vmConfigs.size(); ++j)
     850        {
     851            rc = listVmSlotDhcpOptions(svr, vmConfigs[j]);
     852            if (FAILED(rc))
     853                break;
     854        }
     855        RTPrintf("\n");
     856    }
     857
     858    return rc;
     859}
     860
     861/**
    780862 * List extension packs.
    781863 *
     
    13021384
    13031385        case kListDhcpServers:
    1304         {
    1305             com::SafeIfaceArray<IDHCPServer> svrs;
    1306             CHECK_ERROR(pVirtualBox, COMGETTER(DHCPServers)(ComSafeArrayAsOutParam(svrs)));
    1307             for (size_t i = 0; i < svrs.size(); ++i)
    1308             {
    1309                 ComPtr<IDHCPServer> svr = svrs[i];
    1310                 Bstr netName;
    1311                 svr->COMGETTER(NetworkName)(netName.asOutParam());
    1312                 RTPrintf("NetworkName:    %ls\n", netName.raw());
    1313                 Bstr ip;
    1314                 svr->COMGETTER(IPAddress)(ip.asOutParam());
    1315                 RTPrintf("IP:             %ls\n", ip.raw());
    1316                 Bstr netmask;
    1317                 svr->COMGETTER(NetworkMask)(netmask.asOutParam());
    1318                 RTPrintf("NetworkMask:    %ls\n", netmask.raw());
    1319                 Bstr lowerIp;
    1320                 svr->COMGETTER(LowerIP)(lowerIp.asOutParam());
    1321                 RTPrintf("lowerIPAddress: %ls\n", lowerIp.raw());
    1322                 Bstr upperIp;
    1323                 svr->COMGETTER(UpperIP)(upperIp.asOutParam());
    1324                 RTPrintf("upperIPAddress: %ls\n", upperIp.raw());
    1325                 BOOL fEnabled;
    1326                 svr->COMGETTER(Enabled)(&fEnabled);
    1327                 RTPrintf("Enabled:        %s\n", fEnabled ? "Yes" : "No");
    1328                 RTPrintf("\n");
    1329             }
    1330             break;
    1331         }
     1386            rc = listDhcpServers(pVirtualBox);
     1387            break;
    13321388
    13331389        case kListExtPacks:
  • trunk/src/VBox/Main/idl/VirtualBox.xidl

    r75817 r75819  
    17911791  <interface
    17921792    name="IDHCPServer" extends="$unknown"
    1793     uuid="197717d2-a742-4966-a529-d1467e903feb"
     1793    uuid="ea2d467f-b6c2-4b9a-8eb5-6e2f275dd72e"
    17941794    wsmap="managed"
    17951795    reservedMethods="2" reservedAttributes="6"
     
    18421842      <param name="option" type="DhcpOpt" dir="in"/>
    18431843      <param name="value" type="wstring" dir="in" />
     1844    </method>
     1845
     1846    <method name="removeGlobalOption">
     1847      <desc>
     1848        removes the specified option
     1849        <result name="E_INVALIDARG">
     1850          invalid option id supplied
     1851        </result>
     1852      </desc>
     1853      <param name="option" type="DhcpOpt" dir="in"/>
     1854    </method>
     1855
     1856    <method name="removeGlobalOptions">
     1857      <desc>
     1858        removes all global options
     1859        <result name="E_FAIL">
     1860          failed to remove global options
     1861        </result>
     1862      </desc>
    18441863    </method>
    18451864
     
    18771896    </method>
    18781897
     1898    <method name="removeVmSlotOption">
     1899      <desc>
     1900        removes the specified option
     1901        <result name="E_INVALIDARG">
     1902          invalid VM, slot, or option id supplied
     1903        </result>
     1904      </desc>
     1905      <param name="vmname" type="wstring" dir="in"/>
     1906      <param name="slot" type="long" dir="in"/>
     1907      <param name="option" type="DhcpOpt" dir="in"/>
     1908    </method>
     1909
    18791910    <method name="removeVmSlotOptions">
     1911      <desc>
     1912        removes all option for the specified adapter
     1913        <result name="E_INVALIDARG">
     1914          invalid VM or slot supplied
     1915        </result>
     1916      </desc>
    18801917      <param name="vmname" type="wstring" dir="in"/>
    18811918      <param name="slot" type="long" dir="in"/>
  • trunk/src/VBox/Main/include/DHCPServerImpl.h

    r75648 r75819  
    116116    HRESULT addGlobalOption(DhcpOpt_T aOption,
    117117                            const com::Utf8Str &aValue);
     118    HRESULT removeGlobalOption(DhcpOpt_T aOption);
     119    HRESULT removeGlobalOptions();
    118120    HRESULT addVmSlotOption(const com::Utf8Str &aVmName,
    119121                            LONG aSlot,
    120122                            DhcpOpt_T aOption,
    121123                            const com::Utf8Str &aValue);
     124    HRESULT removeVmSlotOption(const com::Utf8Str &aVmName,
     125                               LONG aSlot,
     126                               DhcpOpt_T aOption);
    122127    HRESULT removeVmSlotOptions(const com::Utf8Str &aVmName,
    123128                                LONG aSlot);
  • trunk/src/VBox/Main/src-server/DHCPServerImpl.cpp

    r75648 r75819  
    478478
    479479
     480HRESULT DHCPServer::removeGlobalOption(DhcpOpt_T aOption)
     481{
     482    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
     483
     484    settings::DhcpOptionMap::size_type cErased = m->GlobalDhcpOptions.erase(aOption);
     485    if (!cErased)
     486        return E_INVALIDARG;
     487
     488    alock.release();
     489
     490    AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
     491    return mVirtualBox->i_saveSettings();
     492}
     493
     494
     495HRESULT DHCPServer::removeGlobalOptions()
     496{
     497    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
     498    m->GlobalDhcpOptions.clear();
     499
     500    alock.release();
     501
     502    AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
     503    return mVirtualBox->i_saveSettings();
     504}
     505
     506
    480507HRESULT DHCPServer::getGlobalOptions(std::vector<com::Utf8Str> &aValues)
    481508{
     
    520547    int rc = addOption(map, aOption, aValue);
    521548    if (!RT_SUCCESS(rc))
     549        return E_INVALIDARG;
     550
     551    alock.release();
     552
     553    AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
     554    return mVirtualBox->i_saveSettings();
     555}
     556
     557
     558HRESULT DHCPServer::removeVmSlotOption(const com::Utf8Str &aVmName, LONG aSlot, DhcpOpt_T aOption)
     559{
     560    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
     561    settings::DhcpOptionMap &map = i_findOptMapByVmNameSlot(aVmName, aSlot);
     562    settings::DhcpOptionMap::size_type cErased = map.erase(aOption);
     563    if (!cErased)
    522564        return E_INVALIDARG;
    523565
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