VBoxNetDHCP.cpp revision b052d1ad9cc3caa1660571fb7322bc4a4db977eb
/* $Id$ */
/** @file
* VBoxNetDHCP - DHCP Service for connecting to IntNet.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/** @page pg_net_dhcp VBoxNetDHCP
*
* Write a few words...
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
#include "../NetLib/VBoxNetLib.h"
#include <vector>
#include <string>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* DHCP configuration item.
*
* This is all public data because I'm too lazy to do it propertly right now.
*/
class VBoxNetDhcpCfg
{
public:
/** The etheret addresses this matches config applies to.
* An empty vector means 'ANY'. */
/** The upper address in the range. */
/** The lower address in the range. */
/** Option 1: The net mask. */
/* * Option 2: The time offset. */
/** Option 3: Routers for the subnet. */
/* * Option 4: Time server. */
/* * Option 5: Name server. */
/** Option 6: Domain Name Server (DNS) */
/* * Option 7: Log server. */
/* * Option 8: Cookie server. */
/* * Option 9: LPR server. */
/* * Option 10: Impress server. */
/* * Option 11: Resource location server. */
/* * Option 12: Host name. */
/* * Option 13: Boot file size option. */
/* * Option 14: Merit dump file. */
/** Option 15: Domain name. */
/* * Option 16: Swap server. */
/* * Option 17: Root path. */
/* * Option 18: Extension path. */
/* * Option 21: Policy filter. */
/* * Option 22: Maximum datagram reassembly size (MRS). */
/* * Option 23: Default IP time-to-live. */
/* * Option 24: Path MTU aging timeout. */
/* * Option 25: Path MTU plateau table. */
/* * Option 26: Interface MTU. */
/* * Option 27: All subnets are local. */
/* * Option 28: Broadcast address. */
/* * Option 29: Perform maximum discovery. */
/* * Option 30: Mask supplier. */
/* * Option 31: Perform route discovery. */
/* * Option 32: Router solicitation address. */
/* * Option 33: Static route. */
/* * Option 34: Trailer encapsulation. */
/* * Option 35: ARP cache timeout. */
/* * Option 36: Ethernet encapsulation. */
/* * Option 37: TCP Default TTL. */
/* * Option 38: TCP Keepalive Interval. */
/* * Option 39: TCP Keepalive Garbage. */
/* * Option 40: Network Information Service (NIS) Domain. */
/* * Option 41: Network Information Servers. */
/* * Option 42: Network Time Protocol Servers. */
/* * Option 43: Vendor Specific Information. */
/* * Option 48: X Window System Font Server. */
/* * Option 49: X Window System Display Manager. */
/** Option 51: IP Address Lease Time. */
/* * Option 64: Network Information Service+ Domain. */
/* * Option 65: Network Information Service+ Servers. */
/** Option 66: TFTP server name. */
/** Address for the bp_siaddr field corresponding to m_TftpServer. */
/** Option 67: Bootfile name. */
/* * Option 68: Mobile IP Home Agent. */
/* * Option 69: Simple Mail Transport Protocol (SMPT) Server. */
/* * Option 70: Post Office Protocol (POP3) Server. */
/* * Option 71: Network News Transport Protocol (NNTP) Server. */
/* * Option 72: Default World Wide Web (WWW) Server. */
/* * Option 73: Default Finger Server. */
/* * Option 74: Default Internet Relay Chat (IRC) Server. */
/* * Option 75: StreetTalk Server. */
/* * Option 119: Domain Search. */
{
m_UpperAddr.u = UINT32_MAX;
m_LowerAddr.u = UINT32_MAX;
m_SubnetMask.u = UINT32_MAX;
}
/** Validates the configuration.
* @returns 0 on success, exit code + error message to stderr on failure. */
int validate(void)
{
if ( m_UpperAddr.u == UINT32_MAX
|| m_LowerAddr.u == UINT32_MAX
|| m_SubnetMask.u == UINT32_MAX)
{
if (m_UpperAddr.u == UINT32_MAX)
if (m_LowerAddr.u == UINT32_MAX)
if (m_SubnetMask.u == UINT32_MAX)
return 2;
}
{
" %d.%d.%d.%d < %d.%d.%d.%d\n",
return 3;
}
/* the code goes insane if we have too many atm. lazy bird */
if (cIPs > 1024)
{
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: Too many IPs between --upper-ip and --lower-ip! %d (max 1024)\n"
" %d.%d.%d.%d < %d.%d.%d.%d\n",
cIPs,
return 3;
}
return 0;
}
/**
* Is this config for one specific client?
*
* @return true / false.
*/
bool isOneSpecificClient(void) const
{
return m_LowerAddr.u == m_UpperAddr.u
&& m_MacAddresses.size() > 0;
}
/**
* Checks if this config matches the specified MAC address.
*
* @returns true / false.
*
* @param pMac The MAC address to match.
*/
{
if (RT_LIKELY(i < 1))
return true; /* no entries == ALL wildcard match */
while (i--)
{
return true;
}
return false;
}
};
/**
* DHCP lease.
*/
class VBoxNetDhcpLease
{
public:
typedef enum State
{
/** Invalid. */
kState_Invalid = 0,
/** The lease is free / released. */
/** An offer has been made.
* Expire time indicates when the offer expires. */
/** The lease is active.
* Expire time indicates when the lease expires. */
} State;
/** The client MAC address. */
/** The IPv4 address. */
/** The current lease state. */
/** The lease expiration time. */
/** Transaction ID. */
/** The configuration for this lease. */
public:
/** Constructor taking an IPv4 address and a Config. */
{
m_xid = UINT32_MAX;
}
/** Destructor. */
{
m_IPv4Address.u = UINT32_MAX;
m_xid = UINT32_MAX;
}
void activate(void);
void release(void);
bool hasExpired(void) const;
/**
* Checks if the lease is in use or not.
*
* @returns true if active, false if free or expired.
*
* @param pNow The current time to use. Optional.
*/
{
if ( m_enmState == kState_Offer
|| m_enmState == kState_Active)
{
if (!pNow)
}
return false;
}
/**
* Is this lease for one specific client?
*
*/
bool isOneSpecificClient(void) const
{
return m_pCfg
&& m_pCfg->isOneSpecificClient();
}
/**
* Is this lease currently being offered to a client.
*
* @returns true / false.
*/
bool isBeingOffered(void) const
{
return m_enmState == kState_Offer
&& isInUse();
}
/**
* Is the lease in the current config or not.
*
* When updating the config we might leave active leases behind which aren't
* included in the new config. These will have m_pCfg set to NULL and should be
* freed up when they expired.
*
* @returns true / false.
*/
bool isInCurrentConfig(void) const
{
}
};
/**
* DHCP server instance.
*/
class VBoxNetDhcp
{
public:
VBoxNetDhcp();
virtual ~VBoxNetDhcp();
int tryGoOnline(void);
int run(void);
protected:
void explodeConfig(void);
VBoxNetDhcpLease *findLeaseByIpv4AndMacAddresses(RTNETADDRIPV4 IPv4Addr, PCRTMAC pMacAddress, bool fAnyState);
static uint8_t const *findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbMaxOpt);
static bool findOptionIPv4Addr(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, PRTNETADDRIPV4 pIPv4Addr);
protected:
/** @name The server configuration data members.
* @{ */
/** @} */
/** The current configs. */
/** The current leases. */
/** @name The network interface
* @{ */
/** @} */
/** @name Debug stuff
* @{ */
/** @} */
};
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Pointer to the DHCP server. */
static VBoxNetDhcp *g_pDhcp;
/**
* Offer this lease to a client.
*
* @param xid The transaction ID.
*/
{
}
/**
* Activate this lease (i.e. a client is now using it).
*/
void VBoxNetDhcpLease::activate(void)
{
RTTimeSpecAddSeconds(&m_ExpireTime, m_pCfg ? m_pCfg->m_cSecLease : 60); /* m_pCfg can be NULL right now... */
}
/**
* Activate this lease with a new transaction ID.
*
* @param xid The transaction ID.
* @todo check if this is really necessary.
*/
{
activate();
}
/**
* Release a lease either upon client request or because it didn't quite match a
* DHCP_REQUEST.
*/
void VBoxNetDhcpLease::release(void)
{
}
/**
* Checks if the lease has expired or not.
*
* This just checks the expiration time not the state. This is so that this
* method will work for reusing RELEASEd leases when the client comes back after
* a reboot or ipconfig /renew. Callers not interested in info on released
* leases should check the state first.
*
* @returns true if expired, false if not.
*/
bool VBoxNetDhcpLease::hasExpired() const
{
}
/**
* Construct a DHCP server with a default configuration.
*/
{
m_Name = "VBoxNetDhcp";
m_Network = "VBoxNetDhcp";
m_TrunkName = "";
m_cbSendBuf = 8192;
m_cVerbosity = 0;
m_cbCurMsg = 0;
#if 0 /* enable to hack the code without a mile long argument list. */
#if 0
#else
#endif
#endif
}
/**
* Destruct a DHCP server.
*/
{
/*
* Close the interface connection.
*/
if (m_hIf != INTNET_HANDLE_INVALID)
{
}
if (m_pSession)
{
SUPTerm(false /* not forced */);
}
}
/**
* Adds a config to the tail.
*
* @returns See VBoxNetDHCP::validate().
* @param pCfg The config too add.
* This object will be consumed by this call!
*/
{
int rc = 0;
if (pCfg)
{
if (!rc)
else
delete pCfg;
}
return rc;
}
/**
* Explodes the config into leases.
*
* @remarks This code is brute force and not very fast nor memory efficient.
* We will have to revisit this later.
*
* @remarks If an IP has been reconfigured for a fixed mac address and it's
* already leased to a client, we it won't be available until the
* client releases its lease or it expires.
*/
void VBoxNetDhcp::explodeConfig(void)
{
/*
* Remove all non-active leases from the vector and zapping the
* config pointers of the once left behind.
*/
{
else
{
Itr++;
}
}
/*
* Loop thru the configurations in reverse order, giving the last
* configs priority of the newer ones.
*/
while (iCfg-- > 0)
{
/* Expand the IP lease range. */
{
IPv4Addr.u = RT_H2N_U32(i);
/* Check if it exists and is configured. */
{
break;
}
if (pLease)
{
}
else
{
/* add it. */
}
}
}
}
/**
* Parse the arguments.
*
* @returns 0 on success, fully bitched exit code on failure.
*
* @param argc Argument count.
* @param argv Argument vector.
*/
{
static const RTGETOPTDEF s_aOptionDefs[] =
{
};
for (;;)
{
if (!rc)
break;
switch (rc)
{
case 'N':
break;
case 'n':
break;
case 't':
break;
case 'T':
else
{
return 1;
}
break;
case 'a':
break;
case 'i':
break;
case 'd':
break;
case 'v':
m_cVerbosity++;
break;
/* Begin config. */
case 'b':
if (rc)
break;
/* fall thru */
/* config specific ones. */
case 'g':
case 'l':
case 'u':
case 'm':
if (!pCurCfg)
{
pCurCfg = new VBoxNetDhcpCfg();
if (!pCurCfg)
{
return 1;
}
}
switch (rc)
{
case 'g':
break;
case 'l':
break;
case 'u':
break;
case 'm':
break;
case 0: /* ignore */ break;
default:
return 1;
}
break;
case 'V':
return 0;
case 'h':
RTPrintf("VBoxNetDHCP Version %s\n"
"(C) 2009 Sun Microsystems, Inc.\n"
"All rights reserved\n"
"\n"
"Usage: VBoxNetDHCP <options>\n"
"\n"
"Options:\n",
return 1;
case VINF_GETOPT_NOT_OPTION:
return 1;
default:
break;
}
}
/*
* Do the reconfig. (move this later)
*/
if (!rc)
return rc;
}
/**
* Tries to connect to the internal network.
*
* @returns 0 on success, exit code + error message to stderr on failure.
*/
int VBoxNetDhcp::tryGoOnline(void)
{
/*
* Open the session, load ring-0 and issue the request.
*/
if (RT_FAILURE(rc))
{
return 1;
}
char szPath[RTPATH_MAX];
if (RT_FAILURE(rc))
{
return 1;
}
if (RT_FAILURE(rc))
{
return 1;
}
/*
* Create the open request.
*/
/*
* Issue the request.
*/
if (RT_SUCCESS(rc))
{
/*
* Get the ring-3 address of the shared interface buffer.
*/
if (RT_SUCCESS(rc))
{
/*
* Activate the interface.
*/
if (RT_SUCCESS(rc))
return 0;
/* bail out */
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
}
else
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_IF_GET_RING3_BUFFER,) failed, rc=%Rrc\n", rc);
}
else
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_OPEN,) failed, rc=%Rrc\n", rc);
}
/**
* Runs the DHCP server.
*
* @returns exit code + error message to stderr on failure, won't return on
* success (you must kill this process).
*/
int VBoxNetDhcp::run(void)
{
/*
* The loop.
*/
for (;;)
{
/*
* Wait for a packet to become available.
*/
WaitReq.cMillies = 2000; /* 2 secs - the sleep is for some reason uninterruptible... */ /** @todo fix interruptability in SrvIntNet! */
if (RT_FAILURE(rc))
{
if (rc == VERR_TIMEOUT)
continue;
return 1;
}
/*
* Process the receive buffer.
*/
while (INTNETRingGetReadable(pRingBuf) > 0)
{
{
m_cbCurMsg = cb;
{
}
else
debugPrint(1, true, "VBoxNetDHCP: Skipping invalid DHCP packet.\n"); /** @todo handle pure bootp clients too? */
m_cbCurMsg = 0;
}
{
/* nothing */
}
/* Advance to the next frame. */
}
}
return 0;
}
/**
* Handles a DHCP message.
*
* @returns true if handled, false if not.
* @param uMsgType The message type.
* @param pDhcpMsg The DHCP message.
* @param cb The size of the DHCP message.
*/
{
{
switch (uMsgType)
{
case RTNET_DHCP_MT_DISCOVER:
case RTNET_DHCP_MT_REQUEST:
case RTNET_DHCP_MT_DECLINE:
case RTNET_DHCP_MT_RELEASE:
case RTNET_DHCP_MT_INFORM:
debugPrint(0, true, "Should we handle this?");
break;
default:
debugPrint(0, true, "Unexpected.");
break;
}
}
return false;
}
/**
* The client is requesting an offer.
*
* @returns true.
*
* @param pDhcpMsg The message.
* @param cb The message size.
*/
{
/*
* The newLease() method contains logic for finding current leases
* and reusing them in case the client is forgetful.
*/
if (!pLease)
return false;
return true;
}
/**
* The client is requesting an offer.
*
* @returns true.
*
* @param pDhcpMsg The message.
* @param cb The message size.
*/
{
/** @todo Probably need to match the server IP here to work correctly with
* other servers. */
/** @todo This code isn't entirely correct and quite a bit of a hack, but it
* will have to do for now as the right thing (tm) is very complex.
* Part of the fun is verifying that the request is something we can
* and should handle. */
/*
* Try find the lease by the requested address + client MAC address.
*/
if (fReqAddr)
{
fReqAddr = true;
}
/*
* Try find the lease by the client IP address + client MAC address.
*/
if ( !pLease
pLease = findLeaseByIpv4AndMacAddresses(pDhcpMsg->bp_ciaddr, &pDhcpMsg->bp_chaddr.Mac, true /* fAnyState */);
#if 0 /** @todo client id stuff - it doesn't make sense here imho, we need IP + MAC. What would make sense
though is to compare the client id with what we've got in the lease and use it to root out
bad requests. */
/*
* Try find the lease by using the client id.
*/
if (!pLease)
{
size_t cbClientID = 0;
if ( pbClientID
&& pbClientID[0] == RTNET_ARP_ETHER
&&
)
{
pLease = findLeaseByIpv4AndMacAddresses(pDhcpMsg->bp_ciaddr, &pDhcpMsg->bp_chaddr.Mac, true /* fAnyState */);
}
}
#endif
/*
* Validate the lease that's requested.
* We've already check the MAC and IP addresses.
*/
bool fAckIt = false;
if (pLease)
{
if (pLease->isBeingOffered())
{
fAckIt = true;
else
}
else if (!pLease->isInCurrentConfig())
{
/** @todo this ain't safe. */
fAckIt = true;
}
else
}
/*
* NAK if if no lease was found.
*/
if (fAckIt)
{
}
else
{
}
return true;
}
/**
* The client is declining an offer we've made.
*
* @returns true.
*
* @param pDhcpMsg The message.
* @param cb The message size.
*/
{
/** @todo Probably need to match the server IP here to work correctly with
* other servers. */
/*
* The client is supposed to pass us option 50, requested address,
* from the offer. We also match the lease state. Apparently the
* MAC address is not supposed to be checked here.
*/
/** @todo this is not required in the initial implementation, do it later. */
return true;
}
/**
* The client is releasing its lease - good boy.
*
* @returns true.
*
* @param pDhcpMsg The message.
* @param cb The message size.
*/
{
/** @todo Probably need to match the server IP here to work correctly with
* other servers. */
/*
* The client may pass us option 61, client identifier, which we should
* use to find the lease by.
*
* We're matching MAC address and lease state as well.
*/
/*
* If no client identifier or if we couldn't find a lease by using it,
* we will try look it up by the client IP address.
*/
/*
* If found, release it.
*/
/** @todo this is not required in the initial implementation, do it later. */
return true;
}
/**
* Helper class for stuffing DHCP options into a reply packet.
*/
class VBoxNetDhcpWriteCursor
{
private:
bool m_fOverflowed; /**< Set if we've overflowed, otherwise false. */
public:
/** Instantiate an option cursor for the specified DHCP message. */
m_fUsed(0),
m_fOverflowed(false)
{
}
/** Destructor. */
{
}
/**
* Try use the bp_file field.
* @returns true if not overloaded, false otherwise.
*/
bool useBpFile(void)
{
if ( m_pfOverload
&& (*m_pfOverload & 1))
return false;
return true;
}
/**
* Try overload more BOOTP fields
*/
bool overloadMore(void)
{
/* switch option area. */
if (!(m_fUsed & 1))
{
fField = 1;
}
else if (!(m_fUsed & 2))
{
fField = 2;
}
else
return false;
if (!m_pfOverload)
{
/* Add an overload option. */
}
else
*m_pfOverload |= fField;
/* pad current option field */
/* switch */
return true;
}
/**
* Begin an option.
*
* @returns true on succes, false if we're out of space.
*
* @param uOption The option number.
* @param cb The amount of data.
*/
{
/* Check that the data of the previous option has all been written. */
/* Check if we need to overload more stuff. */
{
if (!overloadMore())
{
m_fOverflowed = true;
}
{
m_fOverflowed = true;
}
}
/* Emit the option header. */
m_pbCur += 2;
return true;
}
/**
* Puts option data.
*
* @param pvData The data.
* @param cb The amount to put.
*/
{
{
}
}
/**
* Puts an IPv4 Address.
*
* @param IPv4Addr The address.
*/
{
}
/**
* Adds an IPv4 address option.
*
*
* @param uOption The option number.
* @param IPv4Addr The address.
*/
{
return false;
return true;
}
/**
* Adds an option taking 1 or more IPv4 address.
*
* If the vector contains no addresses, the option will not be added.
*
*
* @param uOption The option number.
* @param rIPv4Addrs Reference to the address vector.
*/
{
if (!c)
return true;
return false;
for (size_t i = 0; i < c; i++)
putIPv4Addr(rIPv4Addrs[i]);
return true;
}
/**
* Puts an 8-bit integer.
*
* @param u8 The integer.
*/
{
}
/**
* Adds an 8-bit integer option.
*
*
* @param uOption The option number.
* @param u8 The integer
*/
{
return false;
return true;
}
/**
* Puts an 32-bit integer (network endian).
*
* @param u32Network The integer.
*/
{
}
/**
* Adds an 32-bit integer (network endian) option.
*
*
* @param uOption The option number.
* @param u32Network The integer.
*/
{
return false;
return true;
}
/**
* Puts a std::string.
*
* @param rStr Reference to the string.
*/
{
}
/**
* Adds an std::string option if the string isn't empty.
*
*
* @param uOption The option number.
* @param rStr Reference to the string.
*/
{
if (!cch)
return true;
return false;
return true;
}
/**
* Whether we've overflowed.
*
* @returns true on overflow, false otherwise.
*/
bool hasOverflowed(void) const
{
return m_fOverflowed;
}
/**
* Adds the terminating END option.
*
* The END will always be added as we're reserving room for it, however, we
* might've dropped previous options due to overflows and that is what the
* return status indicates.
*
* @returns true on success, false on a (previous) overflow.
*/
bool optEnd(void)
{
*m_pbCur++ = RTNET_DHCP_OPT_END;
return !hasOverflowed();
}
};
/**
* Constructs and sends a reply to a client.
*
* @returns
* @param uMsgType The DHCP message type.
* @param pLease The lease. This can be NULL for some replies.
* @param pDhcpMsg The client message. We will dig out the MAC address,
* transaction ID, and requested options from this.
* @param cb The size of the client message.
*/
void VBoxNetDhcp::makeDhcpReply(uint8_t uMsgType, VBoxNetDhcpLease *pLease, PCRTNETBOOTP pDhcpMsg, size_t cb)
{
size_t cbReply = RTNET_DHCP_NORMAL_SIZE; /** @todo respect the RTNET_DHCP_OPT_MAX_DHCP_MSG_SIZE option */
/*
* The fixed bits stuff.
*/
pReply->bp_siaddr.u = pLease && pLease->m_pCfg ? pLease->m_pCfg->m_TftpServerAddr.u : 0; /* (next server == TFTP)*/
/*
* The options - use a cursor class for dealing with the ugly stuff.
*/
/* The basics */
if (uMsgType != RTNET_DHCP_MT_NAC)
{
/* The IP config. */
/* The PXE config. */
{
RTStrPrintf((char *)&pReply->bp_file[0], sizeof(pReply->bp_file), "%s", pCfg->m_BootfileName.c_str());
else
}
}
/* Terminate the options. */
debugPrint(0, true, "option overflow\n");
/*
* Send it.
*/
int rc;
#if 0
if (!(pDhcpMsg->bp_flags & RTNET_DHCP_FLAGS_NO_BROADCAST)) /** @todo need to see someone set this flag to check that it's correct. */
{
}
else
#endif
RTNETIPV4_PORT_BOOTPC, /* receiver port */
if (RT_FAILURE(rc))
}
/**
* Look up a lease by MAC address.
*
* @returns Pointer to the lease if found, NULL if not found.
* @param pMacAddress The mac address.
* @param fAnyState Any state.
*/
{
while (iLease-- > 0)
{
if ( pLease
&& ( fAnyState
return pLease;
}
return NULL;
}
/**
* Look up a lease by IPv4 and MAC addresses.
*
* @returns Pointer to the lease if found, NULL if not found.
* @param IPv4Addr The IPv4 address.
* @param pMacAddress The mac address.
* @param fAnyState Any state.
*/
VBoxNetDhcpLease *VBoxNetDhcp::findLeaseByIpv4AndMacAddresses(RTNETADDRIPV4 IPv4Addr, PCRTMAC pMacAddress, bool fAnyState)
{
while (iLease-- > 0)
{
if ( pLease
&& ( fAnyState
return pLease;
}
return NULL;
}
/**
* Creates a new lease for the client specified in the DHCP message.
*
* The caller has already made sure it doesn't already have a lease.
*
* @returns Pointer to the lease if found, NULL+log if not found.
* @param IPv4Addr The IPv4 address.
* @param pMacAddress The MAC address.
*/
{
/*
* Search the possible leases.
*
* We'll try do all the searches in one pass, that is to say, perfect
*/
{
/* Skip it if no configuration, that means its not in the current config. */
continue;
/* best */
if ( pCur->isOneSpecificClient()
{
if ( !pBest
}
/* old lease */
{
if ( !pOld
}
/* expired lease */
{
if ( !pFree
}
}
if (!pNew)
if (!pNew)
if (!pNew)
{
debugPrint(0, true, "No more leases.");
return NULL;
}
/*
* Init the lease.
*/
/** @todo extract the client id. */
return pNew;
}
/**
* Finds an option.
*
* @returns On success, a pointer to the first byte in the option data (no none
* then it'll be the byte following the 0 size field) and *pcbOpt set
* to the option length.
* On failure, NULL is returned and *pcbOpt unchanged.
*
* @param uOption The option to search for.
* @param pDhcpMsg The DHCP message.
* @param cb The size of the message.
* @param pcbOpt Where to store the option size size. Optional. Note
* that this is adjusted if the option length is larger
* than the message buffer.
*/
/* static */ const uint8_t *
{
/*
* Validate the DHCP bits and figure the max size of the options in the vendor field.
*/
return NULL;
return NULL;
if (cbLeft > RTNET_DHCP_OPT_SIZE)
/*
* Search the vendor field.
*/
bool fExtended = false;
{
if (uCur == RTNET_DHCP_OPT_PAD)
{
cbLeft--;
pb++;
}
else if (cbLeft <= 1)
break;
else
{
{
if (pcbOpt)
return pb+2;
}
}
}
/** @todo search extended dhcp option field(s) when present */
return NULL;
}
/**
* Locates an option with an IPv4 address in the DHCP message.
*
* @returns true and *pIpv4Addr if found, false if not.
*
* @param uOption The option to find.
* @param pDhcpMsg The DHCP message.
* @param cb The size of the message.
* @param pIPv4Addr Where to put the address.
*/
/* static */ bool
VBoxNetDhcp::findOptionIPv4Addr(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, PRTNETADDRIPV4 pIPv4Addr)
{
if (pbOpt)
{
if (cbOpt >= sizeof(RTNETADDRIPV4))
{
return true;
}
}
return false;
}
/**
* Print debug message depending on the m_cVerbosity level.
*
* @param iMinLevel The minimum m_cVerbosity level for this message.
* @param fMsg Whether to dump parts for the current DHCP message.
* @param pszFmt The message format string.
* @param ... Optional arguments.
*/
{
if (iMinLevel <= m_cVerbosity)
{
}
}
/**
* Print debug message depending on the m_cVerbosity level.
*
* @param iMinLevel The minimum m_cVerbosity level for this message.
* @param fMsg Whether to dump parts for the current DHCP message.
* @param pszFmt The message format string.
* @param va Optional arguments.
*/
{
if (iMinLevel <= m_cVerbosity)
{
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: %s: %N\n", iMinLevel >= 2 ? "debug" : "info", pszFmt, &vaCopy);
if ( fMsg
&& m_cVerbosity >= 2
&& m_pCurMsg)
{
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: debug: %8s chaddr=%.6Rhxs ciaddr=%d.%d.%d.%d yiaddr=%d.%d.%d.%d siaddr=%d.%d.%d.%d xid=%#x\n",
m_pCurMsg->bp_ciaddr.au8[0], m_pCurMsg->bp_ciaddr.au8[1], m_pCurMsg->bp_ciaddr.au8[2], m_pCurMsg->bp_ciaddr.au8[3],
m_pCurMsg->bp_yiaddr.au8[0], m_pCurMsg->bp_yiaddr.au8[1], m_pCurMsg->bp_yiaddr.au8[2], m_pCurMsg->bp_yiaddr.au8[3],
m_pCurMsg->bp_siaddr.au8[0], m_pCurMsg->bp_siaddr.au8[1], m_pCurMsg->bp_siaddr.au8[2], m_pCurMsg->bp_siaddr.au8[3],
}
}
}
/**
* Gets the name of given DHCP message type.
*
* @returns Readonly name.
* @param uMsgType The message number.
*/
{
switch (uMsgType)
{
case 0: return "MT_00";
case RTNET_DHCP_MT_DISCOVER: return "DISCOVER";
case RTNET_DHCP_MT_OFFER: return "OFFER";
case RTNET_DHCP_MT_REQUEST: return "REQUEST";
case RTNET_DHCP_MT_DECLINE: return "DECLINE";
case RTNET_DHCP_MT_ACK: return "ACK";
case RTNET_DHCP_MT_NAC: return "NAC";
case RTNET_DHCP_MT_RELEASE: return "RELEASE";
case RTNET_DHCP_MT_INFORM: return "INFORM";
case 9: return "MT_09";
case 10: return "MT_0a";
case 11: return "MT_0b";
case 12: return "MT_0c";
case 13: return "MT_0d";
case 14: return "MT_0e";
case 15: return "MT_0f";
case 16: return "MT_10";
case 17: return "MT_11";
case 18: return "MT_12";
case 19: return "MT_13";
case UINT8_MAX: return "MT_ff";
default: return "UNKNOWN";
}
}
/**
* Entry point.
*/
{
/*
* Instantiate the DHCP server and hand it the options.
*/
if (!pDhcp)
{
return 1;
}
if (rc)
return rc;
/*
* Try connect the server to the network.
*/
if (rc)
{
delete pDhcp;
return rc;
}
/*
* Process requests.
*/
delete pDhcp;
return rc;
}
#ifndef VBOX_WITH_HARDENING
{
int rc = RTR3InitAndSUPLib();
if (RT_FAILURE(rc))
{
return 1;
}
}
# ifdef RT_OS_WINDOWS
#include <windows.h>
int nCmdShow
)
{
int rc = RTR3InitAndSUPLib();
if (RT_FAILURE(rc))
{
return 1;
}
size++; /* for null terminator */
int argc;
int ret = 1;
if(pwArgs)
{
if(argv)
{
int i;
for(i = 0; i < argc; i++)
{
int num = WideCharToMultiByte(
CP_ACP, /*UINT CodePage*/
0, /*DWORD dwFlags*/
pwArgs[i],
-1, /*int cchWideChar */
argv[i], /*LPSTR lpMultiByteStr*/
size, /*int cbMultiByte*/
NULL, /*LPCSTR lpDefaultChar*/
FALSE/*LPBOOL lpUsedDefaultChar*/
);
if(num <= 0)
break;
}
if(i == argc)
{
}
}
}
return ret;
}
# endif
#endif /* !VBOX_WITH_HARDENING */