VBoxNetDHCP.cpp revision 4ce88e224dfcf94a91abfb31626bce1448a5477c
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.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. */
//std::string<char> m_HostName;
/* * 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. */
/** 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;
}
return 0;
}
};
/**
* DHCP lease.
*/
class VBoxNetDhcpLease
{
public:
/** The client MAC address. */
/** The lease expiration time. */
};
/**
* DHCP server instance.
*/
class VBoxNetDhcp
{
public:
VBoxNetDhcp();
virtual ~VBoxNetDhcp();
int tryGoOnline(void);
int run(void);
protected:
protected:
/** @name The server configuration data members.
* @{ */
/** @} */
/** The current configs. */
/** The current leases. */
/** @name The network interface
* @{ */
/** @} */
};
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Pointer to the DHCP server. */
static VBoxNetDhcp *g_pDhcp;
/**
* Construct a DHCP server with a default configuration.
*/
{
m_Name = "VBoxNetDhcp";
m_Network = "VBoxNetDhcp";
m_pSession = NULL;
m_cbSendBuf = 8192;
#if 1 /* while hacking. */
#endif
}
/**
* Destruct a DHCP server.
*/
{
/*
* Close the interface connection.
*/
if (m_hIf != INTNET_HANDLE_INVALID)
{
}
if (m_pSession)
{
SUPTerm(false /* not forced */);
m_pSession = NULL;
}
}
/**
* 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)
delete pCfg;
}
return rc;
}
/**
* 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[] =
{
/// @todo { "--mac-address", 'a', RTGETOPT_REQ_MACADDR },
};
for (;;)
{
if (!rc)
break;
switch (rc)
{
case 'N':
break;
case 'n':
break;
case 'a':
AssertFailed();
// m_MacAddress = Val.Mac;
break;
case 'i':
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)
{
rc = 1;
break;
}
}
switch (rc)
{
case 'g':
break;
case 'l':
break;
case 'u':
break;
case 'm':
break;
case 0: /* ignore */ break;
default:
rc = 1;
break;
}
break;
case 'V':
rc = 0;
break;
case 'h':
RTPrintf("VBoxNetDHCP Version %s\n"
"(C) 2009 Sun Microsystems, Inc.\n"
"All rights reserved\n"
"\n"
"Usage:\n"
" TODO\n",
rc = 1;
break;
default:
break;
}
}
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))
{
m_pSession = NULL;
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 (m_cVerbosity >= 2)
RTStrmPrintf(g_pStdErr, "VBoxNetDHCP: attempting to open/create network \"%s\"...\n", OpenReq.szNetwork);
if (RT_SUCCESS(rc))
{
if (m_cVerbosity >= 1)
/*
* Get the ring-3 address of the shared interface buffer.
*/
if (RT_SUCCESS(rc))
{
if (m_cVerbosity >= 1)
/*
* 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)
{
/** @todo The idea is that run() to use VBoxNetUDP here to mimic sockets and not
* do all the parsing here... */
/*
* The loop.
*/
for (;;)
{
/*
* Wait for a packet to become available.
*/
if (RT_FAILURE(rc))
{
return 1;
}
/*
* Process the receive buffer.
*/
while (INTNETRingGetReadable(pRingBuf) > 0)
{
{
if (m_cVerbosity >= 2)
/* Look for the DHCP messages. */
if ( cbFrame > 64
{
{
// PCRTNETDHCP pDhcpMsg = (PCRTNETDHCP)(pUdpHdr + 1);
}
}
}
/* Advance to the next frame. */
}
}
return 0;
}
/**
* 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;
}
}
#endif /* !VBOX_WITH_HARDENING */