bootp.c revision b895bb5ba480cbdcd7abe96183427d61bd56a3d0
/*
*
* Copyright (c) 2004 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <slirp.h>
/* XXX: only DHCP is supported */
{
int i;
for (i = 0; i < NB_ADDR; i++)
{
if (!bootp_clients[i].allocated)
{
bc = &bootp_clients[i];
return bc;
}
}
return NULL;
}
{
unsigned i;
if (i >= NB_ADDR)
return 0;
bootp_clients[i].allocated = 0;
return 1;
}
{
int i;
for (i = 0; i < NB_ADDR; i++)
{
{
bc = &bootp_clients[i];
return bc;
}
}
return NULL;
}
{
*pmsg_type = 0;
p = buf;
if (size < 5)
return;
return;
p += 4;
while (p < p_end)
{
tag = p[0];
if (tag == RFC1533_PAD)
p++;
else if (tag == RFC1533_END)
break;
else
{
p++;
if (p >= p_end)
break;
len = *p++;
switch(tag)
{
case RFC2132_REQ_ADDR:
if (len >= 4)
break;
case RFC2132_MSG_TYPE:
if (len >= 1)
*pmsg_type = p[0];
break;
default:
break;
}
p += len;
}
}
}
{
struct mbuf *m; /* XXX: @todo vasily - it'd be better to reuse this mbuf here */
int dhcp_msg_type, val;
uint8_t *q;
int send_nak = 0;
do { \
}while(0)
/* appending another value to tag, calculates len of whole block*/
do { \
(q) += (len); \
}while(0)
/* extract exact DHCP msg type */
if (dhcp_msg_type == 0)
if (dhcp_msg_type == DHCPRELEASE)
{
int rc;
LogRel(("NAT: %s %R[IP4]\n",
/* This message is not to be answered in any way. */
return;
}
if ( dhcp_msg_type != DHCPDISCOVER
&& dhcp_msg_type != DHCPREQUEST)
return;
return;
if (dhcp_msg_type == DHCPDISCOVER)
{
/* Do not allocate a new lease for clients that forgot that they had a lease. */
if (!bc)
{
if (!bc)
{
LogRel(("NAT: DHCP no IP address left\n"));
Log(("no address left\n"));
return;
}
}
}
else
{
if (!bc)
{
/* if never assigned, behaves as if it was already
assigned (windows fix because it remembers its address) */
goto new_addr;
}
}
if ( tftp_prefix
&& bootp_filename)
#ifndef VBOX_WITH_NAT_SERVICE
#else
#endif
q += 4;
if (dhcp_msg_type == DHCPDISCOVER)
{
*q++ = RFC2132_MSG_TYPE;
*q++ = 1;
*q++ = DHCPOFFER;
}
else if (dhcp_msg_type == DHCPREQUEST)
{
*q++ = RFC2132_MSG_TYPE;
*q++ = 1;
{
/* network changed */
*q++ = DHCPNAK;
send_nak = 1;
}
else
*q++ = DHCPACK;
}
if (send_nak)
LogRel(("NAT: Client requested IP address %R[IP4] -- sending NAK\n",
&requested_ip));
else
LogRel(("NAT: DHCP offered IP address %R[IP4]\n",
if ( dhcp_msg_type == DHCPDISCOVER
|| dhcp_msg_type == DHCPREQUEST)
{
}
if (!send_nak &&
|| dhcp_msg_type == DHCPREQUEST))
{
int added = 0;
if (pData->use_dns_proxy)
{
goto skip_dns_servers;
}
{
q_dns_header = q;
}
{
continue; /* first value with head we've ingected before */
}
{
/* Microsoft dhcp client doen't like domain-less dhcp and trimmed packets*/
/* dhcpcd client very sad if no domain name is passed */
}
if (pData->fPassDomain)
{
{
continue;
/* never meet valid separator here in RFC1533*/
if (added != 0)
else
added = 1;
}
}
if (*slirp_hostname)
{
}
}
*q++ = RFC1533_END;
- sizeof(struct ip)
- sizeof(struct udphdr);
+ sizeof(struct ip);
/* Reply to the broadcast address, as some clients perform paranoid checks. */
}
{
bootp_reply(pData, m);
}