Consolidate DHCP option code numbers.

This commit is contained in:
Nicholas J. Kain 2011-07-25 03:11:47 -04:00
parent 77af1d81fe
commit 2cb0b9227d
7 changed files with 91 additions and 111 deletions

View File

@ -76,7 +76,7 @@ enum states {
STATE_IPTTL,
STATE_MTU,
STATE_BROADCAST,
STATE_NTPSRV,
STATE_NTPSVR,
STATE_WINS
};
@ -419,8 +419,8 @@ static void execute_list(int i)
state[i] = STATE_MTU;
if (strncmp(p, CMD_BROADCAST, sizeof(CMD_BROADCAST)) == 0)
state[i] = STATE_BROADCAST;
if (strncmp(p, CMD_NTPSRV, sizeof(CMD_NTPSRV)) == 0)
state[i] = STATE_NTPSRV;
if (strncmp(p, CMD_NTPSVR, sizeof(CMD_NTPSVR)) == 0)
state[i] = STATE_NTPSVR;
if (strncmp(p, CMD_WINS, sizeof(CMD_WINS)) == 0)
state[i] = STATE_WINS;
free_stritem(&(curl[i]));
@ -504,7 +504,7 @@ static void execute_list(int i)
state[i] = STATE_NOTHING;
break;
case STATE_NTPSRV:
case STATE_NTPSVR:
perform_ntpsrv(i, p);
free_stritem(&(curl[i]));
state[i] = STATE_NOTHING;

View File

@ -487,7 +487,7 @@ void arp_success(struct client_state_t *cs)
arp_switch_state(cs, AS_DEFENSE);
} else {
ssize_t ol;
uint8_t *od = get_option_data(&arp_dhcp_packet, DHCP_ROUTER, &ol);
uint8_t *od = get_option_data(&arp_dhcp_packet, DCODE_ROUTER, &ol);
if (ol == 4) {
memcpy(&cs->routerAddr, od, 4);
} else

View File

@ -146,7 +146,7 @@ static int send_dhcp_cooked(struct client_state_t *cs, struct dhcpmsg *payload)
ssize_t endloc = get_end_option_idx(payload);
if (endloc < 0) {
log_error("send_dhcp_cooked: Attempt to send packet with no DHCP_END.");
log_error("send_dhcp_cooked: Attempt to send packet with no DCODE_END.");
goto out_fd;
}
size_t payload_len =
@ -407,7 +407,7 @@ static int send_dhcp_raw(struct dhcpmsg *payload)
// and drop packets that are longer than 562 bytes.
ssize_t endloc = get_end_option_idx(payload);
if (endloc < 0) {
log_error("send_dhcp_raw: Attempt to send packet with no DHCP_END.");
log_error("send_dhcp_raw: Attempt to send packet with no DCODE_END.");
close(fd);
return ret;
}
@ -504,7 +504,7 @@ static int validate_dhcp_packet(struct client_state_t *cs, int len,
return 0;
}
ssize_t optlen;
uint8_t *temp = get_option_data(packet, DHCP_MESSAGE_TYPE, &optlen);
uint8_t *temp = get_option_data(packet, DCODE_MESSAGE_TYPE, &optlen);
if (!temp) {
log_line("Packet does not specify a DHCP message type. Ignoring.");
return 0;
@ -542,9 +542,9 @@ static void add_option_vendor(struct dhcpmsg *packet)
{
size_t len = strlen(client_config.vendor);
if (len)
add_option_string(packet, DHCP_VENDOR, client_config.vendor, len);
add_option_string(packet, DCODE_VENDOR, client_config.vendor, len);
else
add_option_string(packet, DHCP_VENDOR, "ndhc", sizeof "ndhc" - 1);
add_option_string(packet, DCODE_VENDOR, "ndhc", sizeof "ndhc" - 1);
}
static void add_option_clientid(struct dhcpmsg *packet)
@ -563,14 +563,14 @@ static void add_option_clientid(struct dhcpmsg *packet)
}
} else
memcpy(buf+1, client_config.clientid, len);
add_option_string(packet, DHCP_CLIENT_ID, buf, len+1);
add_option_string(packet, DCODE_CLIENT_ID, buf, len+1);
}
static void add_option_hostname(struct dhcpmsg *packet)
{
size_t len = strlen(client_config.hostname);
if (len)
add_option_string(packet, DHCP_HOST_NAME, client_config.hostname, len);
add_option_string(packet, DCODE_HOSTNAME, client_config.hostname, len);
}
// Initialize a DHCP client packet that will be sent to a server
@ -581,10 +581,10 @@ static struct dhcpmsg init_packet(char type, uint32_t xid)
.htype = 1, // ETH_10MB
.hlen = 6, // ETH_10MB_LEN
.cookie = htonl(DHCP_MAGIC),
.options[0] = DHCP_END,
.options[0] = DCODE_END,
.xid = xid,
};
add_u8_option(&packet, DHCP_MESSAGE_TYPE, type);
add_u8_option(&packet, DCODE_MESSAGE_TYPE, type);
memcpy(packet.chaddr, client_config.arp, 6);
add_option_clientid(&packet);
return packet;
@ -594,8 +594,8 @@ int send_discover(struct client_state_t *cs)
{
struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid);
if (cs->clientAddr)
add_u32_option(&packet, DHCP_REQUESTED_IP, cs->clientAddr);
add_u16_option(&packet, DHCP_MAX_SIZE,
add_u32_option(&packet, DCODE_REQUESTED_IP, cs->clientAddr);
add_u16_option(&packet, DCODE_MAX_SIZE,
htons(sizeof(struct ip_udp_dhcp_packet)));
add_option_request_list(&packet);
add_option_vendor(&packet);
@ -608,9 +608,9 @@ int send_selecting(struct client_state_t *cs)
{
char clibuf[INET_ADDRSTRLEN];
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
add_u32_option(&packet, DHCP_REQUESTED_IP, cs->clientAddr);
add_u32_option(&packet, DHCP_SERVER_ID, cs->serverAddr);
add_u16_option(&packet, DHCP_MAX_SIZE,
add_u32_option(&packet, DCODE_REQUESTED_IP, cs->clientAddr);
add_u32_option(&packet, DCODE_SERVER_ID, cs->serverAddr);
add_u16_option(&packet, DCODE_MAX_SIZE,
htons(sizeof(struct ip_udp_dhcp_packet)));
add_option_request_list(&packet);
add_option_vendor(&packet);
@ -625,7 +625,7 @@ int send_renew(struct client_state_t *cs)
{
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
packet.ciaddr = cs->clientAddr;
add_u16_option(&packet, DHCP_MAX_SIZE,
add_u16_option(&packet, DCODE_MAX_SIZE,
htons(sizeof(struct ip_udp_dhcp_packet)));
add_option_request_list(&packet);
add_option_vendor(&packet);
@ -638,8 +638,8 @@ int send_rebind(struct client_state_t *cs)
{
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
packet.ciaddr = cs->clientAddr;
add_u32_option(&packet, DHCP_REQUESTED_IP, cs->clientAddr);
add_u16_option(&packet, DHCP_MAX_SIZE,
add_u32_option(&packet, DCODE_REQUESTED_IP, cs->clientAddr);
add_u16_option(&packet, DCODE_MAX_SIZE,
htons(sizeof(struct ip_udp_dhcp_packet)));
add_option_request_list(&packet);
add_option_vendor(&packet);
@ -651,8 +651,8 @@ int send_rebind(struct client_state_t *cs)
int send_decline(struct client_state_t *cs, uint32_t server)
{
struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid);
add_u32_option(&packet, DHCP_REQUESTED_IP, cs->clientAddr);
add_u32_option(&packet, DHCP_SERVER_ID, server);
add_u32_option(&packet, DCODE_REQUESTED_IP, cs->clientAddr);
add_u32_option(&packet, DCODE_SERVER_ID, server);
log_line("Sending a decline message...");
return send_dhcp_raw(&packet);
}
@ -661,8 +661,8 @@ int send_release(struct client_state_t *cs)
{
struct dhcpmsg packet = init_packet(DHCPRELEASE, libc_random_u32());
packet.ciaddr = cs->clientAddr;
add_u32_option(&packet, DHCP_REQUESTED_IP, cs->clientAddr);
add_u32_option(&packet, DHCP_SERVER_ID, cs->serverAddr);
add_u32_option(&packet, DCODE_REQUESTED_IP, cs->clientAddr);
add_u32_option(&packet, DCODE_SERVER_ID, cs->serverAddr);
log_line("Sending a release message...");
return send_dhcp_cooked(cs, &packet);
}

View File

@ -234,14 +234,14 @@ void ifchange_bind(struct dhcpmsg *packet)
snprintf(buf, sizeof buf, CMD_INTERFACE ":%s:", client_config.interface);
tbs |= send_client_ip(buf, sizeof buf, packet);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_SUBNET);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_ROUTER);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_DNS_SERVER);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_HOST_NAME);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_DOMAIN_NAME);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_MTU);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_BROADCAST);
tbs |= send_cmd(buf, sizeof buf, packet, DHCP_WINS_SERVER);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_SUBNET);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_ROUTER);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_DNS);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_HOSTNAME);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_DOMAIN);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_MTU);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_BROADCAST);
tbs |= send_cmd(buf, sizeof buf, packet, DCODE_WINS);
if (tbs) {
sockfd = open_ifch();
sockwrite(sockfd, buf, strlen(buf));

View File

@ -40,6 +40,8 @@ struct dhcp_option {
uint8_t code;
};
#define DCODE_PADDING 0x00
// Marks an option that will be sent on the parameter request list to the
// remote DHCP server.
#define OPTION_REQ 16
@ -48,19 +50,19 @@ struct dhcp_option {
static const struct dhcp_option options[] = {
// name[10] type code
{CMD_SUBNET , OPTION_IP | OPTION_LIST | OPTION_REQ, 0x01},
{CMD_TIMEZONE , OPTION_S32, 0x02},
{CMD_ROUTER , OPTION_IP | OPTION_REQ, 0x03},
{CMD_TIMESVR , OPTION_IP | OPTION_LIST, 0x04},
{CMD_DNS , OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
{CMD_LPRSVR , OPTION_IP | OPTION_LIST, 0x09},
{CMD_HOSTNAME , OPTION_STRING | OPTION_REQ, 0x0c},
{CMD_DOMAIN , OPTION_STRING | OPTION_REQ, 0x0f},
{CMD_IPTTL , OPTION_U8, 0x17},
{CMD_MTU , OPTION_U16, 0x1a},
{CMD_BROADCAST, OPTION_IP | OPTION_REQ, 0x1c},
{CMD_NTPSRV , OPTION_IP | OPTION_LIST, 0x2a},
{CMD_WINS , OPTION_IP | OPTION_LIST, 0x2c},
{CMD_SUBNET , OPTION_IP | OPTION_LIST | OPTION_REQ, DCODE_SUBNET},
{CMD_TIMEZONE , OPTION_S32, DCODE_TIMEZONE},
{CMD_ROUTER , OPTION_IP | OPTION_REQ, DCODE_ROUTER},
{CMD_TIMESVR , OPTION_IP | OPTION_LIST, DCODE_TIMESVR},
{CMD_DNS , OPTION_IP | OPTION_LIST | OPTION_REQ, DCODE_DNS},
{CMD_LPRSVR , OPTION_IP | OPTION_LIST, DCODE_LPRSVR},
{CMD_HOSTNAME , OPTION_STRING | OPTION_REQ, DCODE_HOSTNAME},
{CMD_DOMAIN , OPTION_STRING | OPTION_REQ, DCODE_DOMAIN},
{CMD_IPTTL , OPTION_U8, DCODE_IPTTL},
{CMD_MTU , OPTION_U16, DCODE_MTU},
{CMD_BROADCAST, OPTION_IP | OPTION_REQ, DCODE_BROADCAST},
{CMD_NTPSVR , OPTION_IP | OPTION_LIST, DCODE_NTPSVR},
{CMD_WINS , OPTION_IP | OPTION_LIST, DCODE_WINS},
// Past this point, these options are not useful for client configuration
// and contain DHCP protocol metadata. Perhaps they can be removed.
{"requestip", OPTION_IP, 0x32},
@ -69,7 +71,7 @@ static const struct dhcp_option options[] = {
{"serverid" , OPTION_IP, 0x36},
{"message" , OPTION_STRING, 0x38},
{"maxsize" , OPTION_U16, 0x39},
{"NONE" , OPTION_NONE, 0x00}
{"0XX0" , OPTION_NONE, 0x00}
};
enum option_type option_type(uint8_t code)
@ -121,7 +123,7 @@ int option_valid_list(uint8_t code)
static size_t sizeof_option(uint8_t code, size_t datalen)
{
if (code == DHCP_PADDING || code == DHCP_END)
if (code == DCODE_PADDING || code == DCODE_END)
return 1;
return 2 + datalen;
}
@ -135,14 +137,14 @@ static uint8_t *do_get_option_data(uint8_t *buf, ssize_t buflen, int code,
*overload = 0;
while (buflen > 0) {
// Advance over padding.
if (buf[0] == DHCP_PADDING) {
if (buf[0] == DCODE_PADDING) {
buflen--;
buf++;
continue;
}
// We hit the end.
if (buf[0] == DHCP_END) {
if (buf[0] == DCODE_END) {
*optlen = 0;
return NULL;
}
@ -159,7 +161,7 @@ static uint8_t *do_get_option_data(uint8_t *buf, ssize_t buflen, int code,
return buf + 2;
}
if (buf[0] == DHCP_OPTION_OVERLOAD) {
if (buf[0] == DCODE_OVERLOAD) {
if (buf[1] == 1)
*overload |= buf[2];
// fall through
@ -210,14 +212,14 @@ uint8_t *get_option_data(struct dhcpmsg *packet, int code, ssize_t *optlen)
ssize_t get_end_option_idx(struct dhcpmsg *packet)
{
for (size_t i = 0; i < sizeof packet->options; ++i) {
if (packet->options[i] == DHCP_END)
if (packet->options[i] == DCODE_END)
return i;
if (packet->options[i] == DHCP_PADDING)
if (packet->options[i] == DCODE_PADDING)
continue;
if (packet->options[i] != DHCP_PADDING)
if (packet->options[i] != DCODE_PADDING)
i += packet->options[i+1] + 1;
}
log_warning("get_end_option_idx: Did not find DHCP_END marker.");
log_warning("get_end_option_idx: Did not find DCODE_END marker.");
return -1;
}
@ -234,7 +236,7 @@ size_t add_option_string(struct dhcpmsg *packet, uint8_t code, char *str,
ssize_t end = get_end_option_idx(packet);
if (end == -1) {
log_warning("add_option_string: Buffer has no DHCP_END marker.");
log_warning("add_option_string: Buffer has no DCODE_END marker.");
return 0;
}
if (end + len >= sizeof packet->options) {
@ -244,7 +246,7 @@ size_t add_option_string(struct dhcpmsg *packet, uint8_t code, char *str,
packet->options[end] = code;
packet->options[end+1] = slen;
memcpy(packet->options + end + 2, str, slen);
packet->options[end+len] = DHCP_END;
packet->options[end+len] = DCODE_END;
return len;
}
@ -259,7 +261,7 @@ static ssize_t add_option_check(struct dhcpmsg *packet, uint8_t code,
}
ssize_t end = get_end_option_idx(packet);
if (end == -1) {
log_warning("add_u%01u_option: Buffer has no DHCP_END marker.", rlen*8);
log_warning("add_u%01u_option: Buffer has no DCODE_END marker.", rlen*8);
return -1;
}
if (end + 2 + rlen >= sizeof packet->options) {
@ -278,7 +280,7 @@ size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data)
packet->options[end] = code;
packet->options[end+1] = 1;
packet->options[end+2] = data;
packet->options[end+3] = DHCP_END;
packet->options[end+3] = DCODE_END;
return 3;
}
@ -293,7 +295,7 @@ size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data)
packet->options[end+1] = 2;
packet->options[end+2] = dp[0];
packet->options[end+3] = dp[1];
packet->options[end+4] = DHCP_END;
packet->options[end+4] = DCODE_END;
return 4;
}
@ -310,7 +312,7 @@ size_t add_u32_option(struct dhcpmsg *packet, uint8_t code, uint32_t data)
packet->options[end+3] = dp[1];
packet->options[end+4] = dp[2];
packet->options[end+5] = dp[3];
packet->options[end+6] = DHCP_END;
packet->options[end+6] = DCODE_END;
return 6;
}
@ -323,6 +325,6 @@ size_t add_option_request_list(struct dhcpmsg *packet)
if (options[i].type & OPTION_REQ)
reqdata[j++] = options[i].code;
}
return add_option_string(packet, DHCP_PARAM_REQ, (char *)reqdata, j);
return add_option_string(packet, DCODE_PARAM_REQ, (char *)reqdata, j);
}

View File

@ -30,51 +30,29 @@
#include "dhcp.h"
enum dhcp_codes {
DHCP_PADDING = 0x00,
DHCP_SUBNET = 0x01,
DHCP_TIME_OFFSET = 0x02,
DHCP_ROUTER = 0x03,
DHCP_TIME_SERVER = 0x04,
DHCP_NAME_SERVER = 0x05,
DHCP_DNS_SERVER = 0x06,
DHCP_LOG_SERVER = 0x07,
DHCP_COOKIE_SERVER = 0x08,
DHCP_LPR_SERVER = 0x09,
DHCP_HOST_NAME = 0x0c,
DHCP_BOOT_SIZE = 0x0d,
DHCP_DOMAIN_NAME = 0x0f,
DHCP_SWAP_SERVER = 0x10,
DHCP_ROOT_PATH = 0x11,
DHCP_IP_TTL = 0x17,
DHCP_MTU = 0x1a,
DHCP_BROADCAST = 0x1c,
DHCP_NIS_DOMAIN = 0x28,
DHCP_NIS_SERVER = 0x29,
DHCP_NTP_SERVER = 0x2a,
DHCP_WINS_SERVER = 0x2c,
DHCP_REQUESTED_IP = 0x32,
DHCP_LEASE_TIME = 0x33,
DHCP_OPTION_OVERLOAD = 0x34,
DHCP_MESSAGE_TYPE = 0x35,
DHCP_SERVER_ID = 0x36,
DHCP_PARAM_REQ = 0x37,
DHCP_MESSAGE = 0x38,
DHCP_MAX_SIZE = 0x39,
DHCP_T1 = 0x3a,
DHCP_T2 = 0x3b,
DHCP_VENDOR = 0x3c,
DHCP_CLIENT_ID = 0x3d,
DHCP_TFTP_SERVER_NAME = 0x42,
DHCP_BOOT_FILE = 0x43,
DHCP_USER_CLASS = 0x4d,
DHCP_FQDN = 0x51,
DHCP_DOMAIN_SEARCH = 0x77,
DHCP_SIP_SERVERS = 0x78,
DHCP_STATIC_ROUTES = 0x79,
DHCP_WPAD = 0xfc,
DHCP_END = 0xff,
};
#define DCODE_SUBNET 0x01
#define DCODE_TIMEZONE 0x02
#define DCODE_ROUTER 0x03
#define DCODE_TIMESVR 0x04
#define DCODE_DNS 0x06
#define DCODE_LPRSVR 0x09
#define DCODE_HOSTNAME 0x0c
#define DCODE_DOMAIN 0x0f
#define DCODE_IPTTL 0x17
#define DCODE_MTU 0x1a
#define DCODE_BROADCAST 0x1c
#define DCODE_NTPSVR 0x2a
#define DCODE_WINS 0x2c
#define DCODE_REQUESTED_IP 0x32
#define DCODE_LEASE_TIME 0x33
#define DCODE_OVERLOAD 0x34
#define DCODE_MESSAGE_TYPE 0x35
#define DCODE_SERVER_ID 0x36
#define DCODE_PARAM_REQ 0x37
#define DCODE_MAX_SIZE 0x39
#define DCODE_VENDOR 0x3c
#define DCODE_CLIENT_ID 0x3d
#define DCODE_END 0xff
enum option_type {
OPTION_NONE = 0,

View File

@ -194,7 +194,7 @@ static int validate_serverid(struct client_state_t *cs, struct dhcpmsg *packet,
{
uint8_t *temp = NULL;
ssize_t optlen;
if (!(temp = get_option_data(packet, DHCP_SERVER_ID, &optlen))) {
if (!(temp = get_option_data(packet, DCODE_SERVER_ID, &optlen))) {
log_line("Received %s with no server id. Ignoring it.");
return 0;
}
@ -219,7 +219,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
if (!validate_serverid(cs, packet, "a DHCP ACK"))
return;
ssize_t optlen;
uint8_t *temp = get_option_data(packet, DHCP_LEASE_TIME, &optlen);
uint8_t *temp = get_option_data(packet, DCODE_LEASE_TIME, &optlen);
cs->leaseStartTime = curms();
if (!temp) {
log_line("No lease time received, assuming 1h.");
@ -271,7 +271,7 @@ static void selecting_packet(struct client_state_t *cs, struct dhcpmsg *packet,
if (msgtype == DHCPOFFER) {
uint8_t *temp = NULL;
ssize_t optlen;
if ((temp = get_option_data(packet, DHCP_SERVER_ID, &optlen))) {
if ((temp = get_option_data(packet, DCODE_SERVER_ID, &optlen))) {
char clibuf[INET_ADDRSTRLEN];
char svrbuf[INET_ADDRSTRLEN];
memcpy(&cs->serverAddr, temp, 4);