VirtualBox

Ignore:
Timestamp:
Jul 6, 2019 11:42:51 PM (6 years ago)
Author:
vboxsync
Message:

Dhcpd: Went over the DhcpMessage a little as well as revisiting the lease reading code in the database. Logging is LogRel() or similar, no cout, RTPrintf or similar. The debug & release loggers can both output to stderr/out/whatever as needed. bugref:9288

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

Legend:

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

    r79563 r79568  
    247247    if (m_strNetwork.isEmpty())
    248248    {
    249         LogDHCP(("network name is not specified\n"));
     249        LogRel(("network name is not specified\n"));
    250250        return false;
    251251    }
     
    269269        m_MacAddress.au8[5] = Uuid.Gen.au8Node[5];
    270270
    271         LogDHCP(("MAC address is not specified: will use generated MAC %RTmac\n", &m_MacAddress));
     271        LogRel(("MAC address is not specified: will use generated MAC %RTmac\n", &m_MacAddress));
    272272        fMACGenerated = true;
    273273    }
     
    276276    if (m_MacAddress.au8[0] & 0x01)
    277277    {
    278         LogDHCP(("MAC address is not unicast: %RTmac\n", &m_MacAddress));
     278        LogRel(("MAC address is not unicast: %RTmac\n", &m_MacAddress));
    279279        return VERR_GENERAL_FAILURE;
    280280    }
     
    283283    if ((m_IPv4Address.au8[0] & 0xe0) == 0xe0)
    284284    {
    285         LogDHCP(("IP address is not unicast: %RTnaipv4\n", m_IPv4Address.u));
     285        LogRel(("IP address is not unicast: %RTnaipv4\n", m_IPv4Address.u));
    286286        return VERR_GENERAL_FAILURE;
    287287    }
     
    292292    if (RT_FAILURE(rc) || cPrefixBits == 0)
    293293    {
    294         LogDHCP(("IP mask is not valid: %RTnaipv4\n", m_IPv4Netmask.u));
     294        LogRel(("IP mask is not valid: %RTnaipv4\n", m_IPv4Netmask.u));
    295295        return VERR_GENERAL_FAILURE;
    296296    }
     
    299299    if ((m_IPv4PoolFirst.u & m_IPv4Netmask.u) != (m_IPv4Address.u & m_IPv4Netmask.u))
    300300    {
    301         LogDHCP(("first pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n",
     301        LogRel(("first pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n",
    302302                 (m_IPv4Address.u & m_IPv4Netmask.u), cPrefixBits, m_IPv4PoolFirst.u));
    303303        return VERR_GENERAL_FAILURE;
     
    307307    if ((m_IPv4PoolLast.u & m_IPv4Netmask.u) != (m_IPv4Address.u & m_IPv4Netmask.u))
    308308    {
    309         LogDHCP(("last pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n",
     309        LogRel(("last pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n",
    310310                 (m_IPv4Address.u & m_IPv4Netmask.u), cPrefixBits, m_IPv4PoolLast.u));
    311311        return VERR_GENERAL_FAILURE;
     
    315315    if (RT_N2H_U32(m_IPv4PoolLast.u) < RT_N2H_U32(m_IPv4PoolFirst.u))
    316316    {
    317         LogDHCP(("pool range is invalid: %RTnaipv4 - %RTnaipv4\n",
     317        LogRel(("pool range is invalid: %RTnaipv4 - %RTnaipv4\n",
    318318                 m_IPv4PoolFirst.u, m_IPv4PoolLast.u));
    319319        return VERR_GENERAL_FAILURE;
     
    324324        && RT_N2H_U32(m_IPv4Address.u)   <= RT_N2H_U32(m_IPv4PoolLast.u))
    325325    {
    326         LogDHCP(("server address inside the pool range %RTnaipv4 - %RTnaipv4: %RTnaipv4\n",
     326        LogRel(("server address inside the pool range %RTnaipv4 - %RTnaipv4: %RTnaipv4\n",
    327327                 m_IPv4PoolFirst.u, m_IPv4PoolLast.u, m_IPv4Address.u));
    328328        return VERR_GENERAL_FAILURE;
     
    330330
    331331    if (!fMACGenerated)
    332         LogDHCP(("MAC address %RTmac\n", &m_MacAddress));
    333     LogDHCP(("IP address %RTnaipv4/%d\n", m_IPv4Address.u, cPrefixBits));
    334     LogDHCP(("address pool %RTnaipv4 - %RTnaipv4\n", m_IPv4PoolFirst.u, m_IPv4PoolLast.u));
     332        LogRel(("MAC address %RTmac\n", &m_MacAddress));
     333    LogRel(("IP address %RTnaipv4/%d\n", m_IPv4Address.u, cPrefixBits));
     334    LogRel(("address pool %RTnaipv4 - %RTnaipv4\n", m_IPv4PoolFirst.u, m_IPv4PoolLast.u));
    335335
    336336    return VINF_SUCCESS;
     
    770770            i_parseVMConfig(pElmChild);
    771771        else
    772             LogDHCP(("Ignoring unexpected DHCPServer child: %s\n", pElmChild->getName()));
     772            LogRel(("Ignoring unexpected DHCPServer child: %s\n", pElmChild->getName()));
    773773    }
    774774}
  • trunk/src/VBox/NetworkServices/Dhcpd/DHCPD.cpp

    r79563 r79568  
    221221 * @throws  std::bad_alloc
    222222 */
    223 DhcpServerMessage *DHCPD::i_createMessage(int type, DhcpClientMessage &req)
     223DhcpServerMessage *DHCPD::i_createMessage(int type, const DhcpClientMessage &req)
    224224{
    225225    return new DhcpServerMessage(req, type, m_pConfig->getIPv4Address());
     
    251251 * @throws  std::bad_alloc
    252252 */
    253 DhcpServerMessage *DHCPD::i_doDiscover(DhcpClientMessage &req)
     253DhcpServerMessage *DHCPD::i_doDiscover(const DhcpClientMessage &req)
    254254{
    255255    /** @todo
     
    317317 * @throws  std::bad_alloc
    318318 */
    319 DhcpServerMessage *DHCPD::i_doRequest(DhcpClientMessage &req)
     319DhcpServerMessage *DHCPD::i_doRequest(const DhcpClientMessage &req)
    320320{
    321321    OptRequestedAddress reqAddr(req);
     
    367367 * @throws  std::bad_alloc
    368368 */
    369 DhcpServerMessage *DHCPD::i_doInform(DhcpClientMessage &req)
     369DhcpServerMessage *DHCPD::i_doInform(const DhcpClientMessage &req)
    370370{
    371371    if (req.ciaddr().u == 0)
  • trunk/src/VBox/NetworkServices/Dhcpd/DHCPD.h

    r79563 r79568  
    6262    /** @name DHCP message processing methods
    6363     * @{ */
    64     DhcpServerMessage *i_doDiscover(DhcpClientMessage &req);
    65     DhcpServerMessage *i_doRequest(DhcpClientMessage &req);
    66     DhcpServerMessage *i_doInform(DhcpClientMessage &req);
     64    DhcpServerMessage *i_doDiscover(const DhcpClientMessage &req);
     65    DhcpServerMessage *i_doRequest(const DhcpClientMessage &req);
     66    DhcpServerMessage *i_doInform(const DhcpClientMessage &req);
    6767    DhcpServerMessage *i_doDecline(const DhcpClientMessage &req) RT_NOEXCEPT;
    6868    DhcpServerMessage *i_doRelease(const DhcpClientMessage &req) RT_NOEXCEPT;
    6969
    70     DhcpServerMessage *i_createMessage(int type, DhcpClientMessage &req);
     70    DhcpServerMessage *i_createMessage(int type, const DhcpClientMessage &req);
    7171    /** @} */
    7272
  • trunk/src/VBox/NetworkServices/Dhcpd/Db.cpp

    r79563 r79568  
    159159 * Serializes the binding to XML for the lease database.
    160160 *
    161  * @throw  std::bad_alloc
     161 * @throw   std::bad_alloc
     162 * @note    DHCPServerImpl.cpp contains a reader, keep it in sync.
    162163 */
    163164void Binding::toXML(xml::ElementNode *pElmParent) const
     
    204205 * Deserializes the binding from the XML lease database.
    205206 *
    206  * @param   pElmLease   The "Lease" element.
     207 * @param   pElmLease   The "Lease" element to serialize into.
    207208 * @return  Pointer to the resulting binding, NULL on failure.
    208209 * @throw   std::bad_alloc
     210 * @note    DHCPServerImpl.cpp contains a similar reader, keep it in sync.
    209211 */
    210212Binding *Binding::fromXML(const xml::ElementNode *pElmLease)
    211213{
    212     /* Lease/@network seems to always have bogus value, ignore it. */
    213 
    214     /*
    215      * Lease/@mac
    216      */
    217     RTCString strMac;
    218     bool fHasMac = pElmLease->getAttributeValue("mac", &strMac);
    219     if (!fHasMac)
    220         return NULL;
     214    /* Note! Lease/@network seems to always have bogus value, ignore it. */
     215    /* Note! We parse the mandatory attributes and elements first, then
     216             the optional ones.  This means things appear a little jumbled. */
     217
     218    /*
     219     * Lease/@mac - mandatory.
     220     */
     221    const char *pszMacAddress = pElmLease->findAttributeValue("mac");
     222    if (!pszMacAddress)
     223        DHCP_LOG_RET_NULL(("Binding::fromXML: <Lease> element without 'mac' attribute! Skipping lease.\n"));
    221224
    222225    RTMAC mac;
    223     int rc = RTNetStrToMacAddr(strMac.c_str(), &mac);
     226    int rc = RTNetStrToMacAddr(pszMacAddress, &mac);
    224227    if (RT_FAILURE(rc))
    225         return NULL;
    226 
    227     OptClientId id;
    228     RTCString strId;
    229     bool fHasId = pElmLease->getAttributeValue("id", &strId);
    230     if (fHasId)
    231     {
    232         /*
    233          * Decode from "de:ad:be:ef".
    234          */
    235         /** @todo RTStrConvertHexBytes() doesn't grok colons */
    236         size_t   cbBytes = strId.length() / 2;
    237         uint8_t *pbBytes = new uint8_t[cbBytes];
    238         rc = RTStrConvertHexBytes(strId.c_str(), pbBytes, cbBytes, 0);
    239         if (RT_SUCCESS(rc))
    240         {
    241             try
    242             {
    243                 std::vector<uint8_t> rawopt(pbBytes, pbBytes + cbBytes);
    244                 id = OptClientId(rawopt);
    245             }
    246             catch (std::bad_alloc &)
    247             {
    248                 delete[] pbBytes;
    249                 throw;
    250             }
    251         }
    252         delete[] pbBytes;
    253     }
    254 
    255     /*
    256      * Lease/@state - not present in old leases file.  We will try to
    257      * infer from lease time below.
    258      */
    259     RTCString strState;
    260     bool fHasState = pElmLease->getAttributeValue("state", &strState);
    261 
    262     /*
    263      * Lease/Address
    264      */
    265     const xml::ElementNode *ndAddress = pElmLease->findChildElement("Address");
    266     if (ndAddress == NULL)
    267         return NULL;
    268 
    269     /*
    270      * Lease/Address/@value
    271      */
    272     RTCString strAddress;
    273     bool fHasValue = ndAddress->getAttributeValue("value", &strAddress);
    274     if (!fHasValue)
    275         return NULL;
     228        DHCP_LOG_RET_NULL(("Binding::fromXML: Malformed mac address attribute value '%s': %Rrc - Skipping lease.\n",
     229                           pszMacAddress, rc));
     230
     231    /*
     232     * Lease/Address/@value - mandatory.
     233     */
     234    const char *pszAddress = pElmLease->findChildElementAttributeValue("Address", "value");
     235    if (!pszAddress)
     236        DHCP_LOG_RET_NULL(("Binding::fromXML: Could not find <Address> with a 'value' attribute! Skipping lease.\n"));
    276237
    277238    RTNETADDRIPV4 addr;
    278     rc = RTNetStrToIPv4Addr(strAddress.c_str(), &addr);
     239    rc = RTNetStrToIPv4Addr(pszAddress, &addr);
    279240    if (RT_FAILURE(rc))
    280         return NULL;
    281 
    282     /*
    283      * Lease/Time
    284      */
    285     const xml::ElementNode *ndTime = pElmLease->findChildElement("Time");
    286     if (ndTime == NULL)
    287         return NULL;
    288 
    289     /*
    290      * Lease/Time/@issued
    291      */
    292     int64_t issued;
    293     bool fHasIssued = ndTime->getAttributeValue("issued", &issued);
    294     if (!fHasIssued)
    295         return NULL;
    296 
    297     /*
    298      * Lease/Time/@expiration
    299      */
    300     uint32_t duration;
    301     bool fHasExpiration = ndTime->getAttributeValue("expiration", &duration);
    302     if (!fHasExpiration)
    303         return NULL;
     241        DHCP_LOG_RET_NULL(("Binding::fromXML: Malformed IPv4 address value '%s': %Rrc - Skipping lease.\n", pszAddress, rc));
     242
     243    /*
     244     * Lease/Time - mandatory.
     245     */
     246    const xml::ElementNode *pElmTime = pElmLease->findChildElement("Time");
     247    if (pElmTime == NULL)
     248        DHCP_LOG_RET_NULL(("Binding::fromXML: No <Time> element under <Lease mac=%RTmac>! Skipping lease.\n", &mac));
     249
     250    /*
     251     * Lease/Time/@issued - mandatory.
     252     */
     253    int64_t secIssued;
     254    if (!pElmTime->getAttributeValue("issued", &secIssued))
     255        DHCP_LOG_RET_NULL(("Binding::fromXML: <Time> element for %RTmac has no valid 'issued' attribute! Skipping lease.\n", &mac));
     256
     257    /*
     258     * Lease/Time/@expiration - mandatory.
     259     */
     260    uint32_t cSecToLive;
     261    if (!pElmTime->getAttributeValue("expiration", &cSecToLive))
     262        DHCP_LOG_RET_NULL(("Binding::fromXML: <Time> element for %RTmac has no valid 'expiration' attribute! Skipping lease.\n", &mac));
    304263
    305264    std::unique_ptr<Binding> b(new Binding(addr));
    306     b->m_id = ClientId(mac, id);
    307 
    308     if (fHasState)
    309     {
    310         b->m_issued = Timestamp::absSeconds(issued);
    311         b->m_secLease = duration;
    312         b->setState(strState.c_str());
     265
     266    /*
     267     * Lease/@state - mandatory but not present in old leases file, so pretent
     268     *                we're loading an expired one if absent.
     269     */
     270    const char *pszState = pElmLease->findAttributeValue("state");
     271    if (pszState)
     272    {
     273        b->m_issued = Timestamp::absSeconds(secIssued);
     274        b->setState(pszState);
    313275    }
    314276    else
    315277    {   /** @todo XXX: old code wrote timestamps instead of absolute time. */
    316278        /* pretend that lease has just ended */
    317         Timestamp fakeIssued = Timestamp::now();
    318         fakeIssued.subSeconds(duration);
    319         b->m_issued = fakeIssued;
    320         b->m_secLease = duration;
    321         b->m_state = Binding::EXPIRED;
    322     }
     279        LogRel(("Binding::fromXML: No 'state' attribute for <Lease mac=%RTmac> (ts=%RI64 ttl=%RU32)! Assuming EXPIRED.\n",
     280                &mac, secIssued, cSecToLive));
     281        b->m_issued = Timestamp::now().subSeconds(cSecToLive);
     282        b->m_state  = Binding::EXPIRED;
     283    }
     284    b->m_secLease   = cSecToLive;
     285
     286
     287    /*
     288     * Lease/@id - optional, ignore if bad.
     289     * Value format: "deadbeef..." or "de:ad:be:ef...".
     290     */
     291    const char *pszClientId = pElmLease->findAttributeValue("id");
     292    if (pszClientId)
     293    {
     294        uint8_t abBytes[255];
     295        size_t  cbActual;
     296        rc = RTStrConvertHexBytesEx(pszClientId, abBytes, sizeof(abBytes), RTSTRCONVERTHEXBYTES_F_SEP_COLON, NULL, &cbActual);
     297        if (RT_SUCCESS(rc))
     298        {
     299            b->m_id = ClientId(mac, OptClientId(std::vector<uint8_t>(&abBytes[0], &abBytes[cbActual]))); /* throws bad_alloc */
     300            if (rc != VINF_BUFFER_UNDERFLOW && rc != VINF_SUCCESS)
     301                LogRel(("Binding::fromXML: imperfect 'id' attribute: rc=%Rrc, cbActual=%u, '%s'\n", rc, cbActual, pszClientId));
     302        }
     303        else
     304        {
     305            LogRel(("Binding::fromXML: ignoring malformed 'id' attribute: rc=%Rrc, cbActual=%u, '%s'\n",
     306                    rc, cbActual, pszClientId));
     307            b->m_id = ClientId(mac, OptClientId());
     308        }
     309    }
     310    else
     311        b->m_id = ClientId(mac, OptClientId());
    323312
    324313    return b.release();
     
    433422
    434423    if (addr.u != 0)
    435         LogDHCP(("> allocateAddress %RTnaipv4 to client %R[id]\n", addr.u, &id));
     424        LogRel(("> allocateAddress %RTnaipv4 to client %R[id]\n", addr.u, &id));
    436425    else
    437         LogDHCP(("> allocateAddress to client %R[id]\n", &id));
     426        LogRel(("> allocateAddress to client %R[id]\n", &id));
    438427
    439428    /*
     
    456445        if (b->m_id == id)
    457446        {
    458             LogDHCP(("> ... found existing binding %R[binding]\n", b));
     447            LogRel(("> ... found existing binding %R[binding]\n", b));
    459448            return b;
    460449        }
     
    464453            Assert(addrBinding == NULL);
    465454            addrBinding = b;
    466             LogDHCP(("> .... noted existing binding %R[binding]\n", addrBinding));
     455            LogRel(("> .... noted existing binding %R[binding]\n", addrBinding));
    467456        }
    468457
     
    473462            {
    474463                freeBinding = b;
    475                 LogDHCP(("> .... noted free binding %R[binding]\n", freeBinding));
     464                LogRel(("> .... noted free binding %R[binding]\n", freeBinding));
    476465                continue;
    477466            }
     
    485474                {
    486475                    reuseBinding = b;
    487                     LogDHCP(("> .... noted released binding %R[binding]\n", reuseBinding));
     476                    LogRel(("> .... noted released binding %R[binding]\n", reuseBinding));
    488477                }
    489478            }
     
    495484                {
    496485                    reuseBinding = b;
    497                     LogDHCP(("> .... noted expired binding %R[binding]\n", reuseBinding));
     486                    LogRel(("> .... noted expired binding %R[binding]\n", reuseBinding));
    498487                }
    499488            }
     
    510499            addrBinding = i_createBinding(addr, id);
    511500            Assert(addrBinding != NULL);
    512             LogDHCP(("> .... creating new binding for this address %R[binding]\n", addrBinding));
     501            LogRel(("> .... creating new binding for this address %R[binding]\n", addrBinding));
    513502            return addrBinding;
    514503        }
     
    516505        if (addrBinding->m_state <= Binding::EXPIRED) /* not in use */
    517506        {
    518             LogDHCP(("> .... reusing %s binding for this address\n", addrBinding->stateName()));
     507            LogRel(("> .... reusing %s binding for this address\n", addrBinding->stateName()));
    519508            addrBinding->giveTo(id);
    520509            return addrBinding;
    521510        }
    522         LogDHCP(("> .... cannot reuse %s binding for this address\n", addrBinding->stateName()));
     511        LogRel(("> .... cannot reuse %s binding for this address\n", addrBinding->stateName()));
    523512    }
    524513
     
    530519    {
    531520        idBinding = freeBinding;
    532         LogDHCP(("> .... reusing free binding\n"));
     521        LogRel(("> .... reusing free binding\n"));
    533522    }
    534523    else
     
    536525        idBinding = i_createBinding();
    537526        if (idBinding != NULL)
    538             LogDHCP(("> .... creating new binding\n"));
     527            LogRel(("> .... creating new binding\n"));
    539528        else
    540529        {
    541530            idBinding = reuseBinding;
    542531            if (idBinding != NULL)
    543                 LogDHCP(("> .... reusing %s binding %R[binding]\n", reuseBinding->stateName(), reuseBinding));
     532                LogRel(("> .... reusing %s binding %R[binding]\n", reuseBinding->stateName(), reuseBinding));
    544533            else
    545             {
    546                 LogDHCP(("> .... failed to allocate binding\n"));
    547                 return NULL;
    548             }
     534                DHCP_LOG_RET_NULL(("> .... failed to allocate binding\n"));
    549535        }
    550536    }
    551537
    552538    idBinding->giveTo(id);
    553     LogDHCP(("> .... allocated %R[binding]\n", idBinding));
     539    LogRel(("> .... allocated %R[binding]\n", idBinding));
    554540
    555541    return idBinding;
     
    576562        if (req.messageType() == RTNET_DHCP_MT_DISCOVER)
    577563        {
    578             LogDHCP(("DISCOVER: ignoring invalid requested address\n"));
     564            LogRel(("DISCOVER: ignoring invalid requested address\n"));
    579565            reqAddr = OptRequestedAddress();
    580566        }
    581567        else
    582         {
    583             LogDHCP(("rejecting invalid requested address\n"));
    584             return NULL;
    585         }
     568            DHCP_LOG_RET_NULL(("rejecting invalid requested address\n"));
    586569    }
    587570
     
    621604    if (!addressBelongs(pNewBinding->m_addr))
    622605    {
    623         LogDHCP(("Binding for out of range address %RTnaipv4 ignored\n", pNewBinding->m_addr.u));
     606        LogRel(("Binding for out of range address %RTnaipv4 ignored\n", pNewBinding->m_addr.u));
    624607        return VERR_OUT_OF_RANGE;
    625608    }
     
    631614        if (pNewBinding->m_addr.u == b->m_addr.u)
    632615        {
    633             LogDHCP(("> ADD: %R[binding]\n", pNewBinding));
    634             LogDHCP(("> .... duplicate ip: %R[binding]\n", b));
     616            LogRel(("> ADD: %R[binding]\n", pNewBinding));
     617            LogRel(("> .... duplicate ip: %R[binding]\n", b));
    635618            return VERR_DUPLICATE;
    636619        }
     
    638621        if (pNewBinding->m_id == b->m_id)
    639622        {
    640             LogDHCP(("> ADD: %R[binding]\n", pNewBinding));
    641             LogDHCP(("> .... duplicate id: %R[binding]\n", b));
     623            LogRel(("> ADD: %R[binding]\n", pNewBinding));
     624            LogRel(("> .... duplicate id: %R[binding]\n", b));
    642625            return VERR_DUPLICATE;
    643626        }
     
    734717int Db::writeLeases(const RTCString &strFilename) const RT_NOEXCEPT
    735718{
    736     LogDHCP(("writing leases to %s\n", strFilename.c_str()));
     719    LogRel(("writing leases to %s\n", strFilename.c_str()));
    737720
    738721    /** @todo This could easily be written directly to the file w/o going thru
     
    777760    catch (const xml::EIPRTFailure &e)
    778761    {
    779         LogDHCP(("%s\n", e.what()));
     762        LogRel(("%s\n", e.what()));
    780763        return e.rc();
    781764    }
    782765    catch (const RTCError &e)
    783766    {
    784         LogDHCP(("%s\n", e.what()));
     767        LogRel(("%s\n", e.what()));
    785768        return VERR_GENERAL_FAILURE;
    786769    }
    787770    catch (...)
    788771    {
    789         LogDHCP(("Unknown exception while writing '%s'\n", strFilename.c_str()));
     772        LogRel(("Unknown exception while writing '%s'\n", strFilename.c_str()));
    790773        return VERR_UNEXPECTED_EXCEPTION;
    791774    }
     
    806789int Db::loadLeases(const RTCString &strFilename) RT_NOEXCEPT
    807790{
    808     LogDHCP(("loading leases from %s\n", strFilename.c_str()));
     791    LogRel(("loading leases from %s\n", strFilename.c_str()));
    809792
    810793    /*
     
    819802    catch (const xml::EIPRTFailure &e)
    820803    {
    821         LogDHCP(("%s\n", e.what()));
     804        LogRel(("%s\n", e.what()));
    822805        return e.rc();
    823806    }
    824807    catch (const RTCError &e)
    825808    {
    826         LogDHCP(("%s\n", e.what()));
     809        LogRel(("%s\n", e.what()));
    827810        return VERR_GENERAL_FAILURE;
    828811    }
    829812    catch (...)
    830813    {
    831         LogDHCP(("Unknown exception while reading and parsing '%s'\n", strFilename.c_str()));
     814        LogRel(("Unknown exception while reading and parsing '%s'\n", strFilename.c_str()));
    832815        return VERR_UNEXPECTED_EXCEPTION;
    833816    }
     
    839822    if (!pElmRoot)
    840823    {
    841         LogDHCP(("No root element in '%s'\n", strFilename.c_str()));
     824        LogRel(("No root element in '%s'\n", strFilename.c_str()));
    842825        return VERR_NOT_FOUND;
    843826    }
    844827    if (!pElmRoot->nameEquals("Leases"))
    845828    {
    846         LogDHCP(("No root element is not 'Leases' in '%s', but '%s'\n", strFilename.c_str(), pElmRoot->getName()));
     829        LogRel(("No root element is not 'Leases' in '%s', but '%s'\n", strFilename.c_str(), pElmRoot->getName()));
    847830        return VERR_NOT_FOUND;
    848831    }
     
    864847        }
    865848        else
    866             LogDHCP(("Ignoring unexpected element '%s' under 'Leases'...\n", pElmLease->getName()));
     849            LogRel(("Ignoring unexpected element '%s' under 'Leases'...\n", pElmLease->getName()));
    867850    }
    868851
     
    892875        bool fExpired = pBinding->expire();
    893876        if (!fExpired)
    894             LogDHCP(("> LOAD:         lease %R[binding]\n", pBinding));
     877            LogRel(("> LOAD:         lease %R[binding]\n", pBinding));
    895878        else
    896             LogDHCP(("> LOAD: EXPIRED lease %R[binding]\n", pBinding));
     879            LogRel(("> LOAD: EXPIRED lease %R[binding]\n", pBinding));
    897880
    898881        int rc = i_addBinding(pBinding);
     
    901884        return rc;
    902885    }
    903     LogDHCP(("> LOAD: failed to load lease!\n"));
     886    LogRel(("> LOAD: failed to load lease!\n"));
    904887    return VERR_PARSE_ERROR;
    905888}
  • trunk/src/VBox/NetworkServices/Dhcpd/Db.h

    r79563 r79568  
    5151private:
    5252    const RTNETADDRIPV4 m_addr;
    53     State m_state;
    54     ClientId m_id;
    55     Timestamp m_issued;
    56     uint32_t m_secLease;
     53    State               m_state;
     54    ClientId            m_id;
     55    Timestamp           m_issued;
     56    uint32_t            m_secLease;
    5757
    5858public:
  • trunk/src/VBox/NetworkServices/Dhcpd/DhcpMessage.cpp

    r79563 r79568  
    1616 */
    1717
     18
     19/*********************************************************************************************************************************
     20*   Header Files                                                                                                                 *
     21*********************************************************************************************************************************/
    1822#include "DhcpdInternal.h"
    1923#include "DhcpMessage.h"
     
    2125
    2226#include <iprt/string.h>
    23 #include <iprt/stream.h>
    2427
    2528
    2629
    2730DhcpMessage::DhcpMessage()
    28   : m_xid(0), m_flags(0),
    29     m_ciaddr(), m_yiaddr(), m_siaddr(), m_giaddr(),
    30     m_sname(), m_file(),
    31     m_optMessageType()
    32 {
    33 }
    34 
     31    : m_xid(0)
     32    , m_flags(0)
     33    , m_ciaddr()
     34    , m_yiaddr()
     35    , m_siaddr()
     36    , m_giaddr()
     37#if 0 /* not currently unused  */
     38    , m_sname()
     39    , m_file()
     40#endif
     41    , m_optMessageType()
     42{
     43}
     44
     45
     46/*********************************************************************************************************************************
     47*   DhcpClientMessage Implementation                                                                                             *
     48*********************************************************************************************************************************/
    3549
    3650/* static */
    3751DhcpClientMessage *DhcpClientMessage::parse(bool broadcasted, const void *buf, size_t buflen)
    3852{
     53    /*
     54     * Validate the request.
     55     */
    3956    if (buflen < RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts))
    40     {
    41         RTPrintf("%s: %zu bytes datagram is too short\n", __FUNCTION__, buflen);
    42         return NULL;
    43     }
     57        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: %zu bytes datagram is too short\n", buflen));
    4458
    4559    PCRTNETBOOTP bp = (PCRTNETBOOTP)buf;
    4660
    4761    if (bp->bp_op != RTNETBOOTP_OP_REQUEST)
    48     {
    49         RTPrintf("%s: bad opcode: %d\n", __FUNCTION__, bp->bp_op);
    50         return NULL;
    51     }
     62        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: bad opcode: %d\n", bp->bp_op));
    5263
    5364    if (bp->bp_htype != RTNET_ARP_ETHER)
    54     {
    55         RTPrintf("%s: unsupported htype %d\n", __FUNCTION__, bp->bp_htype);
    56         return NULL;
    57     }
     65        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: unsupported htype %d\n", bp->bp_htype));
    5866
    5967    if (bp->bp_hlen != sizeof(RTMAC))
    60     {
    61         RTPrintf("%s: unexpected hlen %d\n", __FUNCTION__, bp->bp_hlen);
    62         return NULL;
    63     }
     68        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: unexpected hlen %d\n", bp->bp_hlen));
    6469
    6570    if (   (bp->bp_chaddr.Mac.au8[0] & 0x01) != 0
    6671        && (bp->bp_flags & RTNET_DHCP_FLAG_BROADCAST) == 0)
    67     {
    68         RTPrintf("%s: multicast chaddr %RTmac without broadcast flag\n",
    69                  __FUNCTION__, &bp->bp_chaddr.Mac);
    70     }
     72        LogRel2(("DhcpClientMessage::parse: multicast chaddr %RTmac without broadcast flag\n", &bp->bp_chaddr.Mac));
    7173
    7274    /* we don't want to deal with forwarding */
    7375    if (bp->bp_giaddr.u != 0)
    74     {
    75         RTPrintf("%s: giaddr %RTnaipv4\n", __FUNCTION__, bp->bp_giaddr.u);
    76         return NULL;
    77     }
     76        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: giaddr %RTnaipv4\n", bp->bp_giaddr.u));
    7877
    7978    if (bp->bp_hops != 0)
    80     {
    81         RTPrintf("%s: non-zero hops %d\n", __FUNCTION__, bp->bp_hops);
    82         return NULL;
    83     }
    84 
     79        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: non-zero hops %d\n", bp->bp_hops));
     80
     81    if (bp->bp_vend.Dhcp.dhcp_cookie != RT_H2N_U32_C(RTNET_DHCP_COOKIE))
     82        DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: bad cookie %#RX32\n", bp->bp_vend.Dhcp.dhcp_cookie));
     83
     84    /*
     85     * Convert it into a DhcpClientMessage instance.
     86     */
    8587    std::unique_ptr<DhcpClientMessage> msg(new DhcpClientMessage());
    8688
    8789    msg->m_broadcasted = broadcasted;
    88 
    89     msg->m_xid = bp->bp_xid;
    90     msg->m_flags = bp->bp_flags;
    91 
    92     msg->m_mac = bp->bp_chaddr.Mac;
    93 
    94     msg->m_ciaddr = bp->bp_ciaddr;
    95     msg->m_yiaddr = bp->bp_yiaddr;
    96     msg->m_siaddr = bp->bp_siaddr;
    97     msg->m_giaddr = bp->bp_giaddr;
    98 
    99     if (bp->bp_vend.Dhcp.dhcp_cookie != RT_H2N_U32_C(RTNET_DHCP_COOKIE))
    100     {
    101         RTPrintf("bad cookie\n");
    102         return NULL;
    103     }
    104 
    105     int fOptOverload = msg->parseOptions(&bp->bp_vend.Dhcp.dhcp_opts, buflen - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts));
     90    msg->m_xid         = bp->bp_xid;
     91    msg->m_flags       = bp->bp_flags;
     92    msg->m_mac         = bp->bp_chaddr.Mac;
     93    msg->m_ciaddr      = bp->bp_ciaddr;
     94    msg->m_yiaddr      = bp->bp_yiaddr;
     95    msg->m_siaddr      = bp->bp_siaddr;
     96    msg->m_giaddr      = bp->bp_giaddr;
     97
     98    int fOptOverload = msg->i_parseOptions(&bp->bp_vend.Dhcp.dhcp_opts[0],
     99                                           buflen - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts));
    106100    if (fOptOverload < 0)
    107101        return NULL;
     
    110104    if (fOptOverload & RTNET_DHCP_OPTION_OVERLOAD_FILE)
    111105    {
    112         int status = msg->parseOptions(bp->bp_file, sizeof(bp->bp_file));
     106        int status = msg->i_parseOptions(bp->bp_file, sizeof(bp->bp_file));
    113107        if (status != 0)
    114108            return NULL;
    115109    }
     110#if 0 /* not currently unused  */
    116111    else if (bp->bp_file[0] != '\0')
    117112    {
     
    122117            msg->m_file.assign(pszFile, len);
    123118    }
     119#endif
    124120
    125121    /* "... followed by the 'sname' field." */
    126122    if (fOptOverload & RTNET_DHCP_OPTION_OVERLOAD_SNAME)
    127123    {
    128         int status = msg->parseOptions(bp->bp_sname, sizeof(bp->bp_sname));
     124        int status = msg->i_parseOptions(bp->bp_sname, sizeof(bp->bp_sname));
    129125        if (status != 0) /* NB: this includes "nested" Option Overload */
    130126            return NULL;
    131127    }
     128#if 0 /* not currently unused  */
    132129    else if (bp->bp_sname[0] != '\0')
    133130    {
     
    138135            msg->m_sname.assign(pszSName, len);
    139136    }
     137#endif
    140138
    141139    msg->m_optMessageType = OptMessageType(*msg);
     
    149147
    150148
    151 int DhcpClientMessage::parseOptions(const void *buf, size_t buflen)
    152 {
    153     int            fOptOverload = 0;
    154     const uint8_t *data        = static_cast<const uint8_t *>(buf);
    155     while (buflen > 0)
    156     {
    157         uint8_t const opt = *data++;
    158         --buflen;
    159 
    160         if (opt == RTNET_DHCP_OPT_PAD)
     149int DhcpClientMessage::i_parseOptions(const uint8_t *pbBuf, size_t cbBuf) RT_NOEXCEPT
     150{
     151    int fOptOverload = 0;
     152    while (cbBuf > 0)
     153    {
     154        uint8_t const bOpt = *pbBuf++;
     155        --cbBuf;
     156
     157        if (bOpt == RTNET_DHCP_OPT_PAD)
    161158            continue;
    162159
    163         if (opt == RTNET_DHCP_OPT_END)
    164             break;
    165 
    166         if (buflen == 0)
    167         {
    168             RTPrintf("option %d has no length field\n", opt);
    169             return -1;
    170         }
    171 
    172         uint8_t optlen = *data++;
    173         --buflen;
    174 
    175         if (optlen > buflen)
    176         {
    177             RTPrintf("option %d truncated (length %d, but only %lu bytes left)\n", opt, optlen, (unsigned long)buflen);
    178             return -1;
    179         }
     160        if (bOpt == RTNET_DHCP_OPT_END)
     161            break;
     162
     163        if (cbBuf == 0)
     164            DHCP_LOG_RET(-1, ("option %d has no length field\n", bOpt));
     165
     166        uint8_t const cbOpt = *pbBuf++;
     167        --cbBuf;
     168
     169        if (cbOpt > cbBuf)
     170            DHCP_LOG_RET(-1, ("option %d truncated (length %d, but only %zu bytes left)\n", bOpt, cbOpt, cbBuf));
    180171
    181172#if 0
    182         rawopts_t::const_iterator it(m_optmap.find(opt));
     173        rawopts_t::const_iterator it(m_optmap.find(bOpt));
    183174        if (it != m_optmap.cend())
    184175            return -1;
    185176#endif
    186         if (opt == RTNET_DHCP_OPT_OPTION_OVERLOAD)
     177        if (bOpt == RTNET_DHCP_OPT_OPTION_OVERLOAD)
    187178        {
    188             if (optlen != 1)
    189             {
    190                 RTPrintf("Overload Option (option %d) has invalid length %d\n", opt, optlen);
    191                 return -1;
    192             }
    193 
    194             fOptOverload = *data;
     179            if (cbOpt != 1)
     180                DHCP_LOG_RET(-1, ("Overload Option (option %d) has invalid length %d\n", bOpt, cbOpt));
     181
     182            fOptOverload = *pbBuf;
    195183
    196184            if ((fOptOverload & ~RTNET_DHCP_OPTION_OVERLOAD_MASK) != 0)
    197             {
    198                 RTPrintf("Overload Option (option %d) has invalid value 0x%x\n", opt, fOptOverload);
    199                 return -1;
    200             }
     185                DHCP_LOG_RET(-1, ("Overload Option (option %d) has invalid value 0x%x\n", bOpt, fOptOverload));
    201186        }
    202187        else
    203             m_rawopts.insert(std::make_pair(opt, octets_t(data, data + optlen)));
    204 
    205         data   += optlen;
    206         buflen -= optlen;
     188            m_rawopts.insert(std::make_pair(bOpt, octets_t(pbBuf, pbBuf + cbOpt)));
     189
     190        pbBuf += cbOpt;
     191        cbBuf -= cbOpt;
    207192    }
    208193
     
    211196
    212197
    213 void DhcpClientMessage::dump() const
     198void DhcpClientMessage::dump() const RT_NOEXCEPT
    214199{
    215200    switch (m_optMessageType.value())
    216201    {
    217202        case RTNET_DHCP_MT_DISCOVER:
    218             LogDHCP(("DISCOVER"));
     203            LogRel(("DISCOVER"));
    219204            break;
    220205
    221206        case RTNET_DHCP_MT_REQUEST:
    222             LogDHCP(("REQUEST"));
     207            LogRel(("REQUEST"));
    223208            break;
    224209
    225210        case RTNET_DHCP_MT_INFORM:
    226             LogDHCP(("INFORM"));
     211            LogRel(("INFORM"));
    227212            break;
    228213
    229214        case RTNET_DHCP_MT_DECLINE:
    230             LogDHCP(("DECLINE"));
     215            LogRel(("DECLINE"));
    231216            break;
    232217
    233218        case RTNET_DHCP_MT_RELEASE:
    234             LogDHCP(("RELEASE"));
     219            LogRel(("RELEASE"));
    235220            break;
    236221
    237222        default:
    238             LogDHCP(("<Unknown Mesage Type %d>", m_optMessageType.value()));
     223            LogRel(("<Unknown Mesage Type %d>", m_optMessageType.value()));
    239224            break;
    240225    }
    241226
    242227    if (OptRapidCommit(*this).present())
    243         LogDHCP((" (rapid commit)"));
    244 
    245 
    246     const OptServerId sid(*this);
    247     if (sid.present())
    248         LogDHCP((" for server %RTnaipv4", sid.value().u));
    249 
    250     LogDHCP((" xid 0x%08x", m_xid));
    251     LogDHCP((" chaddr %RTmac\n", &m_mac));
    252 
    253     const OptClientId cid(*this);
    254     if (cid.present()) {
    255         if (cid.value().size() > 0)
    256             LogDHCP((" client id: %.*Rhxs\n", cid.value().size(), &cid.value().front()));
    257         else
    258             LogDHCP((" client id: <empty>\n"));
    259     }
    260 
    261     LogDHCP((" ciaddr %RTnaipv4", m_ciaddr.u));
    262     if (m_yiaddr.u != 0)
    263         LogDHCP((" yiaddr %RTnaipv4", m_yiaddr.u));
    264     if (m_siaddr.u != 0)
    265         LogDHCP((" siaddr %RTnaipv4", m_siaddr.u));
    266     if (m_giaddr.u != 0)
    267         LogDHCP((" giaddr %RTnaipv4", m_giaddr.u));
    268     LogDHCP(("%s\n", broadcast() ? "broadcast" : ""));
    269 
    270 
    271     const OptRequestedAddress reqAddr(*this);
    272     if (reqAddr.present())
    273         LogDHCP((" requested address %RTnaipv4", reqAddr.value().u));
    274     const OptLeaseTime reqLeaseTime(*this);
    275     if (reqLeaseTime.present())
    276         LogDHCP((" requested lease time %d", reqAddr.value()));
    277     if (reqAddr.present() || reqLeaseTime.present())
    278         LogDHCP(("\n"));
    279 
    280     const OptParameterRequest params(*this);
    281     if (params.present())
    282     {
    283         LogDHCP((" params {"));
    284         typedef OptParameterRequest::value_t::const_iterator it_t;
    285         for (it_t it = params.value().begin(); it != params.value().end(); ++it)
    286             LogDHCP((" %d", *it));
    287         LogDHCP((" }\n"));
     228        LogRel((" (rapid commit)"));
     229
     230    try
     231    {
     232        const OptServerId sid(*this);
     233        if (sid.present())
     234            LogRel((" for server %RTnaipv4", sid.value().u));
     235
     236        LogRel((" xid 0x%08x", m_xid));
     237        LogRel((" chaddr %RTmac\n", &m_mac));
     238
     239        const OptClientId cid(*this);
     240        if (cid.present()) {
     241            if (cid.value().size() > 0)
     242                LogRel((" client id: %.*Rhxs\n", cid.value().size(), &cid.value().front()));
     243            else
     244                LogRel((" client id: <empty>\n"));
     245        }
     246
     247        LogRel((" ciaddr %RTnaipv4", m_ciaddr.u));
     248        if (m_yiaddr.u != 0)
     249            LogRel((" yiaddr %RTnaipv4", m_yiaddr.u));
     250        if (m_siaddr.u != 0)
     251            LogRel((" siaddr %RTnaipv4", m_siaddr.u));
     252        if (m_giaddr.u != 0)
     253            LogRel((" giaddr %RTnaipv4", m_giaddr.u));
     254        LogRel(("%s\n", broadcast() ? "broadcast" : ""));
     255
     256
     257        const OptRequestedAddress reqAddr(*this);
     258        if (reqAddr.present())
     259            LogRel((" requested address %RTnaipv4", reqAddr.value().u));
     260        const OptLeaseTime reqLeaseTime(*this);
     261        if (reqLeaseTime.present())
     262            LogRel((" requested lease time %d", reqAddr.value()));
     263        if (reqAddr.present() || reqLeaseTime.present())
     264            LogRel(("\n"));
     265
     266        const OptParameterRequest params(*this);
     267        if (params.present())
     268        {
     269            LogRel((" params {"));
     270            typedef OptParameterRequest::value_t::const_iterator it_t;
     271            for (it_t it = params.value().begin(); it != params.value().end(); ++it)
     272                LogRel((" %d", *it));
     273            LogRel((" }\n"));
     274        }
     275    }
     276    catch (std::bad_alloc &)
     277    {
     278        LogRel(("bad_alloc during dumping\n"));
    288279    }
    289280
     
    292283    {
    293284        const uint8_t optcode = (*it).first;
    294         switch (optcode) {
     285        switch (optcode)
     286        {
    295287            case OptMessageType::optcode:      /* FALLTHROUGH */
    296288            case OptClientId::optcode:         /* FALLTHROUGH */
     
    304296                if (fHeader)
    305297                {
    306                     LogDHCP((" other options:"));
     298                    LogRel((" other options:"));
    307299                    fHeader = false;
    308300                }
    309                 LogDHCP((" %d", optcode));
     301                LogRel((" %d", optcode));
    310302                break;
    311303        }
    312304    }
    313305    if (!fHeader)
    314         LogDHCP(("\n"));
    315 }
    316 
    317 
    318 DhcpServerMessage::DhcpServerMessage(const DhcpClientMessage &req,
    319                                      uint8_t messageTypeParam, RTNETADDRIPV4 serverAddr)
    320   : DhcpMessage(),
    321     m_optServerId(serverAddr)
     306        LogRel(("\n"));
     307}
     308
     309
     310
     311/*********************************************************************************************************************************
     312*   DhcpServerMessage Implementation                                                                                             *
     313*********************************************************************************************************************************/
     314
     315DhcpServerMessage::DhcpServerMessage(const DhcpClientMessage &req, uint8_t messageTypeParam, RTNETADDRIPV4 serverAddr)
     316    : DhcpMessage()
     317    , m_optServerId(serverAddr)
    322318{
    323319    m_dst.u = 0xffffffff;       /* broadcast */
     
    336332
    337333
    338 void DhcpServerMessage::maybeUnicast(const DhcpClientMessage &req)
     334void DhcpServerMessage::maybeUnicast(const DhcpClientMessage &req) RT_NOEXCEPT
    339335{
    340336    if (!req.broadcast() && req.ciaddr().u != 0)
     
    343339
    344340
     341/**
     342 * @throws  std::bad_alloc
     343 */
    345344void DhcpServerMessage::addOption(DhcpOption *opt)
    346345{
     
    349348
    350349
     350/**
     351 * @throws  std::bad_alloc
     352 */
    351353void DhcpServerMessage::addOptions(const optmap_t &optmap)
    352354{
    353     for (optmap_t::const_iterator it ( optmap.begin() );
    354          it != optmap.end(); ++it)
    355     {
     355    for (optmap_t::const_iterator it( optmap.begin() ); it != optmap.end(); ++it)
    356356        m_optmap << it->second;
    357     }
    358 }
    359 
    360 
     357}
     358
     359
     360/**
     361 * @throws  std::bad_alloc
     362 */
    361363int DhcpServerMessage::encode(octets_t &data)
    362364{
     
    393395         it != m_optmap.end(); ++it)
    394396    {
    395         RTPrintf("encoding option %d\n", it->first);
     397        LogRel3(("encoding option %d\n", it->first));
    396398        DhcpOption &opt = *it->second;
    397399        data << opt;
     
    400402    data << OptEnd();
    401403
    402     if (data.size() < 548)      /* XXX */
    403         data.resize(548);
     404    AssertCompile(RTNET_DHCP_NORMAL_SIZE == 548);
     405    if (data.size() < RTNET_DHCP_NORMAL_SIZE)      /* XXX */
     406        data.resize(RTNET_DHCP_NORMAL_SIZE);
    404407
    405408    return VINF_SUCCESS;
  • trunk/src/VBox/NetworkServices/Dhcpd/DhcpMessage.h

    r79563 r79568  
    2929
    3030
    31 
     31/**
     32 * Base class for internal DHCP client and server message representations.
     33 */
    3234class DhcpMessage
    3335{
    3436protected:
    35     uint32_t m_xid;
    36     uint16_t m_flags;
     37    uint32_t        m_xid;
     38    uint16_t        m_flags;
    3739
    38     RTMAC m_mac;
     40    RTMAC           m_mac;
    3941
    4042    RTNETADDRIPV4   m_ciaddr;
     
    4345    RTNETADDRIPV4   m_giaddr;
    4446
    45     RTCString m_sname;
    46     RTCString m_file;
     47#if 0 /* not currently unused, so avoid wasting time on them for now.  */
     48    RTCString       m_sname;  /**< @note Not necessarily UTF-8 clean. */
     49    RTCString       m_file;   /**< @note Not necessarily UTF-8 clean. */
     50#endif
    4751
    48     OptMessageType m_optMessageType;
     52    OptMessageType  m_optMessageType;
     53
     54protected:
     55    DhcpMessage();
    4956
    5057public:
    51     DhcpMessage();
     58    /** @name Accessors
     59     * @{ */
     60    uint32_t        xid() const RT_NOEXCEPT                     { return m_xid; }
    5261
     62    uint16_t        flags() const RT_NOEXCEPT                   { return m_flags; }
     63    bool            broadcast() const RT_NOEXCEPT               { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; }
    5364
    54     uint32_t xid() const { return m_xid; }
     65    const RTMAC     &mac() const RT_NOEXCEPT                    { return m_mac; }
    5566
    56     uint16_t flags() const { return m_flags; }
    57     bool broadcast() const { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; }
     67    RTNETADDRIPV4   ciaddr() const RT_NOEXCEPT                  { return m_ciaddr; }
     68    RTNETADDRIPV4   yiaddr() const RT_NOEXCEPT                  { return m_yiaddr; }
     69    RTNETADDRIPV4   siaddr() const RT_NOEXCEPT                  { return m_siaddr; }
     70    RTNETADDRIPV4   giaddr() const RT_NOEXCEPT                  { return m_giaddr; }
    5871
    59     const RTMAC &mac() const { return m_mac; }
     72    void            setCiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_ciaddr = addr; }
     73    void            setYiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_yiaddr = addr; }
     74    void            setSiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_siaddr = addr; }
     75    void            setGiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_giaddr = addr; }
    6076
    61     RTNETADDRIPV4 ciaddr() const { return m_ciaddr; }
    62     RTNETADDRIPV4 yiaddr() const { return m_yiaddr; }
    63     RTNETADDRIPV4 siaddr() const { return m_siaddr; }
    64     RTNETADDRIPV4 giaddr() const { return m_giaddr; }
    65 
    66     void setCiaddr(RTNETADDRIPV4 addr) { m_ciaddr = addr; }
    67     void setYiaddr(RTNETADDRIPV4 addr) { m_yiaddr = addr; }
    68     void setSiaddr(RTNETADDRIPV4 addr) { m_siaddr = addr; }
    69     void setGiaddr(RTNETADDRIPV4 addr) { m_giaddr = addr; }
    70 
    71     uint8_t messageType() const RT_NOEXCEPT
     77    uint8_t         messageType() const RT_NOEXCEPT
    7278    {
    7379        Assert(m_optMessageType.present());
    7480        return m_optMessageType.value();
    7581    }
     82    /** @} */
    7683};
    7784
    7885
     86/**
     87 * Decoded DHCP client message.
     88 *
     89 * This is the internal decoded representation of a DHCP message picked up from
     90 * the wire.
     91 */
    7992class DhcpClientMessage
    80   : public DhcpMessage
     93    : public DhcpMessage
    8194{
    8295protected:
    83     rawopts_t m_rawopts;
    84     ClientId m_id;
    85     bool m_broadcasted;
     96    rawopts_t       m_rawopts;
     97    ClientId        m_id;
     98    bool            m_broadcasted;
    8699
    87100public:
    88101    static DhcpClientMessage *parse(bool broadcasted, const void *buf, size_t buflen);
    89102
    90     bool broadcasted() const { return m_broadcasted; }
     103    /** @name Getters
     104     * @{ */
     105    bool            broadcasted() const RT_NOEXCEPT             { return m_broadcasted; }
     106    const rawopts_t &rawopts() const RT_NOEXCEPT                { return m_rawopts; }
     107    const ClientId &clientId() const RT_NOEXCEPT                { return m_id; }
     108    /** @} */
    91109
    92     const rawopts_t &rawopts() const { return m_rawopts; }
    93     const ClientId &clientId() const { return m_id; }
    94 
    95     void dump() const;
     110    void            dump() const RT_NOEXCEPT;
    96111
    97112protected:
    98     int parseOptions(const void *buf, size_t buflen);
     113    int             i_parseOptions(const uint8_t *pbBuf, size_t cbBuf) RT_NOEXCEPT;
    99114};
    100115
    101116
    102117
     118/**
     119 * DHCP server message for encoding.
     120 */
    103121class DhcpServerMessage
    104   : public DhcpMessage
     122    : public DhcpMessage
    105123{
    106124protected:
    107     RTNETADDRIPV4 m_dst;
    108 
    109     OptServerId m_optServerId;
    110 
    111     optmap_t m_optmap;
     125    RTNETADDRIPV4   m_dst;
     126    OptServerId     m_optServerId;
     127    optmap_t        m_optmap;
    112128
    113129public:
    114     DhcpServerMessage(const DhcpClientMessage &req,
    115                       uint8_t messageType, RTNETADDRIPV4 serverAddr);
     130    DhcpServerMessage(const DhcpClientMessage &req, uint8_t messageType, RTNETADDRIPV4 serverAddr);
    116131
    117     RTNETADDRIPV4 dst() const { return m_dst; }
    118     void setDst(RTNETADDRIPV4 aDst) { m_dst = aDst; }
     132    /** @name Accessors
     133     * @{ */
     134    RTNETADDRIPV4   dst() const RT_NOEXCEPT                     { return m_dst; }
     135    void            setDst(RTNETADDRIPV4 aDst) RT_NOEXCEPT      { m_dst = aDst; }
    119136
    120     void maybeUnicast(const DhcpClientMessage &req);
     137    void            maybeUnicast(const DhcpClientMessage &req) RT_NOEXCEPT;
    121138
    122     void addOption(DhcpOption *opt);
    123     void addOption(const DhcpOption &opt)
    124     {
    125         addOption(opt.clone());
    126     }
     139    void            addOption(DhcpOption *opt);
     140    void            addOption(const DhcpOption &opt)            { addOption(opt.clone()); }
    127141
    128     void addOptions(const optmap_t &optmap);
     142    void            addOptions(const optmap_t &optmap);
     143    /** @} */
    129144
    130     int encode(octets_t &data);
     145    int             encode(octets_t &data);
    131146};
    132147
  • trunk/src/VBox/NetworkServices/Dhcpd/DhcpdInternal.h

    r79530 r79568  
    6363
    6464
    65 /** @def LogDHCP
    66  * Wrapper around LogRel.  */
    67 #if 1
    68 # define LogDHCP LogRel
    69 #else
    70 # include <iprt/stream.h>
    71 # define LogDHCP(args) RTPrintf args
    72 #endif
     65/** @name LogXRel + return NULL helpers
     66 * @{ */
     67#define DHCP_LOG_RET_NULL(a_MsgArgs)        do { LogRel(a_MsgArgs);     return NULL; } while (0)
     68#define DHCP_LOG2_RET_NULL(a_MsgArgs)       do { LogRel2(a_MsgArgs);    return NULL; } while (0)
     69#define DHCP_LOG3_RET_NULL(a_MsgArgs)       do { LogRel3(a_MsgArgs);    return NULL; } while (0)
     70/** @} */
     71
     72
     73/** @name LogXRel + return a_rcRet helpers
     74 * @{ */
     75#define DHCP_LOG_RET(a_rcRet, a_MsgArgs)    do { LogRel(a_MsgArgs);     return (a_rcRet); } while (0)
     76#define DHCP_LOG2_RET(a_rcRet, a_MsgArgs)   do { LogRel2(a_MsgArgs);    return (a_rcRet); } while (0)
     77#define DHCP_LOG3_RET(a_rcRet, a_MsgArgs)   do { LogRel3(a_MsgArgs);    return (a_rcRet); } while (0)
     78/** @} */
    7379
    7480#endif /* !VBOX_INCLUDED_SRC_Dhcpd_DhcpdInternal_h */
  • trunk/src/VBox/NetworkServices/Dhcpd/IPv4Pool.cpp

    r79563 r79568  
    7676        if (a_Range.FirstAddr <= prev->LastAddr)
    7777        {
    78             LogDHCP(("%08x-%08x conflicts with %08x-%08x\n",
     78            LogRel(("%08x-%08x conflicts with %08x-%08x\n",
    7979                     a_Range.FirstAddr, a_Range.LastAddr,
    8080                     prev->FirstAddr, prev->LastAddr));
     
    127127            IPv4Range trimmed = *itBeg;
    128128            trimmed.FirstAddr += 1;
     129            Assert(trimmed.FirstAddr <= trimmed.LastAddr);
    129130            m_pool.erase(itBeg);
    130131            try
  • trunk/src/VBox/NetworkServices/Dhcpd/VBoxNetDhcpd.cpp

    r79563 r79568  
    741741                    || ip_addr_cmp(ip_current_dest_addr(), &ip_addr_any);
    742742
    743     DhcpClientMessage *msgIn = DhcpClientMessage::parse(broadcasted, p->payload, p->len);
    744     if (msgIn == NULL)
    745         return;
    746 
    747     std::unique_ptr<DhcpClientMessage> autoFreeMsgIn(msgIn);
    748 
    749     DhcpServerMessage *msgOut = m_server.process(*msgIn);
    750     if (msgOut == NULL)
    751         return;
    752 
    753     std::unique_ptr<DhcpServerMessage> autoFreeMsgOut(msgOut);
    754 
    755     ip_addr_t dst = { msgOut->dst().u };
    756     if (ip_addr_cmp(&dst, &ip_addr_any))
    757         ip_addr_copy(dst, ip_addr_broadcast);
    758 
    759     octets_t data;
    760     int rc = msgOut->encode(data);
    761     if (RT_FAILURE(rc))
    762         return;
    763 
    764     unique_ptr_pbuf q ( pbuf_alloc(PBUF_RAW, (u16_t)data.size(), PBUF_RAM) );
    765     if (!q)
    766         return;
    767 
    768     err_t error = pbuf_take(q.get(), &data.front(), (u16_t)data.size());
    769     if (error != ERR_OK)
    770         return;
    771 
    772     error = udp_sendto(pcb, q.get(), &dst, RTNETIPV4_PORT_BOOTPC);
    773     if (error != ERR_OK)
    774         return;
     743    try
     744    {
     745        DhcpClientMessage *msgIn = DhcpClientMessage::parse(broadcasted, p->payload, p->len);
     746        if (msgIn == NULL)
     747            return;
     748
     749        std::unique_ptr<DhcpClientMessage> autoFreeMsgIn(msgIn);
     750
     751        DhcpServerMessage *msgOut = m_server.process(*msgIn);
     752        if (msgOut == NULL)
     753            return;
     754
     755        std::unique_ptr<DhcpServerMessage> autoFreeMsgOut(msgOut);
     756
     757        ip_addr_t dst = { msgOut->dst().u };
     758        if (ip_addr_cmp(&dst, &ip_addr_any))
     759            ip_addr_copy(dst, ip_addr_broadcast);
     760
     761        octets_t data;
     762        int rc = msgOut->encode(data);
     763        if (RT_FAILURE(rc))
     764            return;
     765
     766        unique_ptr_pbuf q ( pbuf_alloc(PBUF_RAW, (u16_t)data.size(), PBUF_RAM) );
     767        if (!q)
     768            return;
     769
     770        err_t error = pbuf_take(q.get(), &data.front(), (u16_t)data.size());
     771        if (error != ERR_OK)
     772            return;
     773
     774        error = udp_sendto(pcb, q.get(), &dst, RTNETIPV4_PORT_BOOTPC);
     775        if (error != ERR_OK)
     776            return;
     777    }
     778    catch (std::bad_alloc &)
     779    {
     780        LogRel(("VBoxNetDhcpd::dhcp4Recv: Caught std::bad_alloc!\n"));
     781    }
    775782}
    776783
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