VirtualBox

Changeset 17691 in vbox


Ignore:
Timestamp:
Mar 11, 2009 12:56:27 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
44193
Message:

VBoxNetDHCP: Some more code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp

    r17678 r17691  
    5252/** The requested address. */
    5353#define RTNET_DHCP_OPT_REQUESTED_ADDRESS    50
     54
     55/** The normal size of RTNETBOOTP::bp_vend::Dhcp::dhcp_opts.  */
     56#define RTNET_DHCP_OPT_SIZE         (312 - 4)
    5457
    5558
     
    245248    void                updateLeaseConfig(VBoxNetDhcpLease *pLease);
    246249
    247     static uint8_t     *findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbMaxOpt);
     250    static uint8_t const *findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbMaxOpt);
    248251    static bool         findOptionIPv4Addr(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, PRTNETADDRIPV4 pIPv4Addr);
    249252
     
    292295/** Pointer to the DHCP server. */
    293296static VBoxNetDhcp *g_pDhcp;
     297
     298
     299/**
     300 * Offer this lease to a client.
     301 *
     302 * @param   xid             The transaction ID.
     303 */
     304void VBoxNetDhcpLease::offer(uint32_t xid)
     305{
     306    m_enmState = kState_Offer;
     307    RTTimeNow(&m_ExpireTime);
     308    RTTimeSpecAddSeconds(&m_ExpireTime, 60);
     309}
     310
     311
     312/**
     313 * Activate this lease (i.e. a client is now using it).
     314 */
     315void VBoxNetDhcpLease::activate(void)
     316{
     317    m_enmState = kState_Active;
     318    RTTimeNow(&m_ExpireTime);
     319    RTTimeSpecAddSeconds(&m_ExpireTime, m_pCfg ? m_pCfg->m_cSecLease : 60); /* m_pCfg can be NULL right now... */
     320}
     321
     322
     323/**
     324 * Release a lease either upon client request or because it didn't quite match a
     325 * DHCP_REQUEST.
     326 */
     327void VBoxNetDhcpLease::release(void)
     328{
     329    m_enmState = kState_Free;
     330    RTTimeNow(&m_ExpireTime);
     331    RTTimeSpecAddSeconds(&m_ExpireTime, 5);
     332}
    294333
    295334
     
    912951
    913952
     953/**
     954 * Look up a lease by MAC address.
     955 *
     956 * @returns Pointer to the lease if found, NULL if not found.
     957 * @param   pMacAddress             The mac address.
     958 * @param   fEnsureUpToDateConfig   Whether to update the configuration.
     959 */
    914960VBoxNetDhcpLease *VBoxNetDhcp::findLeaseByMacAddress(PCRTMAC pMacAddress, bool fEnsureUpToDateConfig)
    915961{
     962    size_t iLease = m_Leases.size();
     963    while (iLease-- > 0)
     964    {
     965        VBoxNetDhcpLease *pLease = &m_Leases[iLease];
     966        if (    pLease
     967            &&  pLease->m_MacAddress.au16[0] == pMacAddress->au16[0]
     968            &&  pLease->m_MacAddress.au16[1] == pMacAddress->au16[1]
     969            &&  pLease->m_MacAddress.au16[2] == pMacAddress->au16[2])
     970        {
     971            if (fEnsureUpToDateConfig)
     972                updateLeaseConfig(pLease);
     973            return pLease;
     974        }
     975    }
     976
    916977    return NULL;
    917978}
    918979
    919980
     981/**
     982 * Look up a lease by IPv4 and MAC addresses.
     983 *
     984 * @returns Pointer to the lease if found, NULL if not found.
     985 * @param   IPv4Addr                The IPv4 address.
     986 * @param   pMacAddress             The mac address.
     987 */
    920988VBoxNetDhcpLease *VBoxNetDhcp::findLeaseByIpv4AndMacAddresses(RTNETADDRIPV4 IPv4Addr, PCRTMAC pMacAddress)
    921989{
     990    size_t iLease = m_Leases.size();
     991    while (iLease-- > 0)
     992    {
     993        VBoxNetDhcpLease *pLease = &m_Leases[iLease];
     994        if (    pLease
     995            &&  pLease->m_IPv4Address.u      == IPv4Addr.u
     996            &&  pLease->m_MacAddress.au16[0] == pMacAddress->au16[0]
     997            &&  pLease->m_MacAddress.au16[1] == pMacAddress->au16[1]
     998            &&  pLease->m_MacAddress.au16[2] == pMacAddress->au16[2])
     999            return pLease;
     1000    }
     1001
    9221002    return NULL;
    923 
    924 }
    925 
     1003}
     1004
     1005
     1006/**
     1007 * Creates a new lease for the client specified in the DHCP message.
     1008 *
     1009 * The caller has already made sure it doesn't already have a lease.
     1010 *
     1011 * @returns Pointer to the lease if found, NULL+log if not found.
     1012 * @param   IPv4Addr                The IPv4 address.
     1013 * @param   pMacAddress             The MAC address.
     1014 */
    9261015VBoxNetDhcpLease *VBoxNetDhcp::newLease(PCRTNETBOOTP pDhcpMsg, size_t cb)
    9271016{
     
    9341023 * Finds an option.
    9351024 *
    936  * @returns On success, a pointer to the option number within the DHCP message
    937  *          and *pcbMaxOpt set to the maximum number of bytes the option may
    938  *          contain.
    939  *          If not found NULL is returned and *pcbMaxOpt is not changed.
     1025 * @returns On success, a pointer to the first byte in the option data (no none
     1026 *          then it'll be the byte following the 0 size field) and *pcbOpt set
     1027 *          to the option length.
     1028 *          On failure, NULL is returned and *pcbOpt unchanged.
    9401029 *
    9411030 * @param   uOption         The option to search for.
    9421031 * @param   pDhcpMsg        The DHCP message.
    9431032 * @param   cb              The size of the message.
    944  * @param   pcbMaxOpt       Where to store the max option size. Optional.
    945  */
    946 /* static */ uint8_t *
    947 VBoxNetDhcp::findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbMaxOpt)
    948 {
     1033 * @param   pcbOpt          Where to store the option size size. Optional. Note
     1034 *                          that this is adjusted if the option length is larger
     1035 *                          than the message buffer.
     1036 */
     1037/* static */ const uint8_t *
     1038VBoxNetDhcp::findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbOpt)
     1039{
     1040    Assert(uOption != RTNET_DHCP_OPT_PAD);
     1041
     1042    /*
     1043     * Validate the DHCP bits and figure the max size of the options in the vendor field.
     1044     */
     1045    if (cb <= RT_UOFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts))
     1046        return NULL;
     1047    if (pDhcpMsg->bp_vend.Dhcp.dhcp_cookie != RTNET_DHCP_COOKIE)
     1048        return NULL;
     1049    size_t cbLeft = cb - RT_UOFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts);
     1050    if (cbLeft > RTNET_DHCP_OPT_SIZE)
     1051        cbLeft = RTNET_DHCP_OPT_SIZE;
     1052
     1053    /*
     1054     * Search the vendor field.
     1055     */
     1056    bool            fExtended = false;
     1057    uint8_t const  *pb = &pDhcpMsg->bp_vend.Dhcp.dhcp_opts[0];
     1058    while (pb && cbLeft > 0)
     1059    {
     1060        uint8_t uCur  = *pb;
     1061        if (uCur == RTNET_DHCP_OPT_PAD)
     1062        {
     1063            cbLeft--;
     1064            pb++;
     1065        }
     1066        else if (cbLeft <= 1)
     1067            break;
     1068        else
     1069        {
     1070            size_t  cbCur = pb[1];
     1071            if (cbCur > cbLeft - 2)
     1072                cbCur = cbLeft - 2;
     1073            if (uCur == uOption)
     1074            {
     1075                if (pcbOpt)
     1076                    *pcbOpt = cbCur;
     1077                return pb+2;
     1078            }
     1079            pb     += cbCur + 2;
     1080            cbLeft -= cbCur - 2;
     1081        }
     1082    }
     1083
     1084    /** @todo search extended dhcp option field(s) when present */
     1085
    9491086    return NULL;
    9501087}
     
    9641101VBoxNetDhcp::findOptionIPv4Addr(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, PRTNETADDRIPV4 pIPv4Addr)
    9651102{
    966     size_t   cbMaxOpt;
    967     uint8_t *pbOpt = findOption(uOption, pDhcpMsg, cb, &cbMaxOpt);
     1103    size_t          cbOpt;
     1104    uint8_t const  *pbOpt = findOption(uOption, pDhcpMsg, cb, &cbOpt);
    9681105    if (pbOpt)
    9691106    {
    970 
     1107        if (cbOpt >= sizeof(RTNETADDRIPV4))
     1108        {
     1109            *pIPv4Addr = *(PCRTNETADDRIPV4)pbOpt;
     1110            return true;
     1111        }
    9711112    }
    9721113    return false;
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