bootp.c revision a68b0110cd3338c318b355c0ac5ac72f92e510b7
/*
*
* 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 */
{
#ifdef LOG_ENABLED
#endif
}
{
int i;
for(i = 0; i < NB_ADDR; i++) {
if (!bootp_clients[i].allocated)
goto found;
}
return NULL;
bc = &bootp_clients[i];
return bc;
}
{
int i;
if (i >= NB_ADDR)
return;
bootp_clients[i].allocated = 0;
}
{
int i;
for(i = 0; i < NB_ADDR; i++) {
goto found;
}
return NULL;
bc = &bootp_clients[i];
return bc;
}
int *pmsg_type)
{
*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_MSG_TYPE:
if (len >= 1)
*pmsg_type = p[0];
break;
default:
break;
}
p += len;
}
}
}
{
struct mbuf *m;
struct in_addr dns_addr_dhcp;
int dhcp_msg_type, val;
uint8_t *q;
/* extract exact DHCP msg type */
if (dhcp_msg_type == 0)
if (dhcp_msg_type == DHCPRELEASE) {
LogRel(("NAT: DHCP released IP address %u.%u.%u.%u\n",
/* This message is not to be answered in any way. */
return;
}
if (dhcp_msg_type != DHCPDISCOVER &&
return;
/* XXX: this is a hack to get the client mac address */
return;
m->m_data += if_maxlinkhdr;
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"));
dprintf("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 (bootp_filename)
{
LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
}
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;
*q++ = DHCPACK;
}
if (dhcp_msg_type == DHCPDISCOVER ||
dhcp_msg_type == DHCPREQUEST) {
*q++ = RFC2132_SRV_ID;
*q++ = 4;
q += 4;
*q++ = RFC1533_NETMASK;
*q++ = 4;
*q++ = 0xff;
*q++ = 0xff;
*q++ = 0xff;
*q++ = 0x00;
*q++ = RFC1533_GATEWAY;
*q++ = 4;
q += 4;
*q++ = RFC1533_DNS;
*q++ = 4;
q += 4;
*q++ = RFC2132_LEASE_TIME;
*q++ = 4;
q += 4;
if (*slirp_hostname) {
*q++ = RFC1533_HOSTNAME;
*q++ = val;
q += val;
}
}
*q++ = RFC1533_END;
/* Reply to the broadcast address, as some clients perform paranoid checks. */
}
{
}
}