VirtualBox

Changeset 79819 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jul 16, 2019 7:57:16 PM (5 years ago)
Author:
vboxsync
Message:

Dhcpd: Configurable lease time. bugref:9288

Location:
trunk/src/VBox/NetworkServices/Dhcpd
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/Dhcpd/Config.cpp

    r79818 r79819  
    830830        m_secMaxLeaseTime = 0;
    831831
     832    /* Swap min and max if max is smaller: */
     833    if (m_secMaxLeaseTime < m_secMinLeaseTime && m_secMinLeaseTime && m_secMaxLeaseTime)
     834    {
     835        LogRel(("Swapping min/max lease times: %u <-> %u\n", m_secMinLeaseTime, m_secMaxLeaseTime));
     836        uint32_t uTmp = m_secMaxLeaseTime;
     837        m_secMaxLeaseTime = m_secMinLeaseTime;
     838        m_secMinLeaseTime = uTmp;
     839    }
     840
    832841    /*
    833842     * Parse children.
     
    852861{
    853862    ConfigLevelBase::initFromXml(pElmOptions, fStrict, pConfig);
     863
     864    /*
     865     * Resolve defaults here in the global config so we don't have to do this
     866     * in Db::allocate() for every lease request.
     867     */
     868    if (m_secMaxLeaseTime == 0 && m_secDefaultLeaseTime == 0 && m_secMinLeaseTime == 0)
     869    {
     870        m_secMinLeaseTime     = 300;                /*  5 min */
     871        m_secDefaultLeaseTime = 600;                /* 10 min */
     872        m_secMaxLeaseTime     = 12 * RT_SEC_1HOUR;  /* 12 hours */
     873    }
     874    else
     875    {
     876        if (m_secDefaultLeaseTime == 0)
     877        {
     878            if (m_secMaxLeaseTime != 0)
     879                m_secDefaultLeaseTime = RT_MIN(RT_MAX(m_secMinLeaseTime, 600), m_secMaxLeaseTime);
     880            else
     881            {
     882                m_secDefaultLeaseTime = RT_MAX(m_secMinLeaseTime, 600);
     883                m_secMaxLeaseTime = RT_MAX(m_secDefaultLeaseTime, 12 * RT_SEC_1HOUR);
     884            }
     885        }
     886        if (m_secMaxLeaseTime == 0)
     887            m_secMaxLeaseTime = RT_MAX(RT_MAX(m_secMinLeaseTime, m_secDefaultLeaseTime), 12 * RT_SEC_1HOUR);
     888        if (m_secMinLeaseTime == 0)
     889            m_secMinLeaseTime = RT_MIN(300, m_secDefaultLeaseTime);
     890    }
     891
    854892}
    855893
  • trunk/src/VBox/NetworkServices/Dhcpd/Config.h

    r79818 r79819  
    4646    /** DHCP options. */
    4747    optmap_t        m_Options;
     48protected:
    4849    /** Minimum lease time, zero means try next level up. */
    4950    uint32_t        m_secMinLeaseTime;
     
    7778        return a_rItRet != m_Options.end();
    7879    }
     80
     81    /** @name Accessors
     82     * @{ */
     83    uint32_t        getMinLeaseTime()     const RT_NOEXCEPT { return m_secMinLeaseTime; }
     84    uint32_t        getDefaultLeaseTime() const RT_NOEXCEPT { return m_secDefaultLeaseTime; }
     85    uint32_t        getMaxLeaseTime()     const RT_NOEXCEPT { return m_secMaxLeaseTime; }
     86    /** @} */
    7987
    8088protected:
  • trunk/src/VBox/NetworkServices/Dhcpd/DHCPD.cpp

    r79818 r79819  
    245245    m_pConfig->getConfigsForClient(vecConfigs, req.clientId(), OptVendorClassId(req), OptUserClassId(req));
    246246
    247     Binding *b = m_db.allocateBinding(req);
     247    Binding *b = m_db.allocateBinding(req, vecConfigs);
    248248    if (b == NULL)
    249249        return NULL;
     
    313313    m_pConfig->getConfigsForClient(vecConfigs, req.clientId(), OptVendorClassId(req), OptUserClassId(req));
    314314
    315     Binding *b = m_db.allocateBinding(req);
     315    Binding *b = m_db.allocateBinding(req, vecConfigs);
    316316    if (b == NULL)
    317317    {
  • trunk/src/VBox/NetworkServices/Dhcpd/Db.cpp

    r79818 r79819  
    649649 * @returns Pointer to the binding, NULL on failure.
    650650 * @param   req                 The DHCP request being served.
    651  */
    652 Binding *Db::allocateBinding(const DhcpClientMessage &req)
     651 * @param   rConfigVec          The configurations that applies to the client.
     652 *                              Used for lease time calculation.
     653 */
     654Binding *Db::allocateBinding(const DhcpClientMessage &req, Config::ConfigVec const &rConfigVec)
    653655{
    654656    const ClientId &id(req.clientId());
     
    694696        Assert(b->id() == id);
    695697
    696         /** @todo
    697          * XXX: handle requests for specific lease time!
    698          * XXX: old lease might not have expired yet?
    699          * Make lease time configurable.
     698        /*
     699         * Figure out the lease time.
    700700         */
    701         // OptLeaseTime reqLeaseTime(req);
    702         b->setLeaseTime(1200);
     701        uint32_t secMin = 0;
     702        uint32_t secDfl = 0;
     703        uint32_t secMax = 0;
     704        for (Config::ConfigVec::const_iterator it = rConfigVec.begin(); it != rConfigVec.end(); ++it)
     705        {
     706            ConfigLevelBase const *pConfig = *it;
     707            if (secMin == 0)
     708                secMin = pConfig->getMinLeaseTime();
     709            if (secDfl == 0)
     710                secDfl = pConfig->getDefaultLeaseTime();
     711            if (secMax == 0)
     712                secMax = pConfig->getMaxLeaseTime();
     713        }
     714        Assert(secMin); Assert(secMax); Assert(secDfl); /* global config always have non-defaults set */
     715        if (secMin > secMax)
     716            secMin = secMax;
     717
     718        OptLeaseTime reqLeaseTime(req);
     719        if (!reqLeaseTime.present())
     720        {
     721            b->setLeaseTime(secDfl);
     722            LogRel2(("Lease time %u secs (default)\n", b->leaseTime()));
     723        }
     724        else if (reqLeaseTime.value() < secMin)
     725        {
     726            b->setLeaseTime(secMin);
     727            LogRel2(("Lease time %u secs (min)\n", b->leaseTime()));
     728        }
     729        else if (reqLeaseTime.value() > secMax)
     730        {
     731            b->setLeaseTime(secMax);
     732            LogRel2(("Lease time %u secs (max)\n", b->leaseTime()));
     733        }
     734        else
     735        {
     736            b->setLeaseTime(reqLeaseTime.value());
     737            LogRel2(("Lease time %u secs (requested)\n", b->leaseTime()));
     738        }
    703739    }
    704740    return b;
  • trunk/src/VBox/NetworkServices/Dhcpd/Db.h

    r79818 r79819  
    182182    bool     addressBelongs(RTNETADDRIPV4 addr) const RT_NOEXCEPT { return m_pool.contains(addr); }
    183183
    184     Binding *allocateBinding(const DhcpClientMessage &req);
     184    Binding *allocateBinding(const DhcpClientMessage &req, Config::ConfigVec const &rConfigVec);
    185185    bool     releaseBinding(const DhcpClientMessage &req) RT_NOEXCEPT;
    186186
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