Pass around the DHCP message type as the literal data, not as a character

pointer to the matching data in the options field of the DHCP packet.
This commit is contained in:
Nicholas J. Kain 2011-07-18 10:47:35 -04:00
parent 27081be84f
commit 7f6721bb82
3 changed files with 19 additions and 17 deletions

View File

@ -480,9 +480,8 @@ void set_listen_none(struct client_state_t *cs)
}
static int validate_dhcp_packet(struct client_state_t *cs, int len,
struct dhcpmsg *packet, uint8_t **msg)
struct dhcpmsg *packet, uint8_t *msgtype)
{
ssize_t optlen;
if (len < sizeof *packet - sizeof packet->options) {
log_line("Packet is too short to contain magic cookie. Ignoring.");
return 0;
@ -496,16 +495,19 @@ static int validate_dhcp_packet(struct client_state_t *cs, int len,
packet->xid, cs->xid);
return 0;
}
if (!(*msg = get_option_data(packet, DHCP_MESSAGE_TYPE, &optlen))) {
ssize_t optlen;
uint8_t *temp = get_option_data(packet, DHCP_MESSAGE_TYPE, &optlen);
if (!temp) {
log_line("Packet does not specify a DHCP message type. Ignoring.");
return 0;
}
*msgtype = *temp;
return 1;
}
void handle_packet(struct client_state_t *cs)
{
uint8_t *message = NULL;
uint8_t msgtype;
int len;
struct dhcpmsg packet;
@ -523,9 +525,9 @@ void handle_packet(struct client_state_t *cs)
change_listen_mode(cs, cs->listenMode);
}
if (!validate_dhcp_packet(cs, len, &packet, &message))
if (!validate_dhcp_packet(cs, len, &packet, &msgtype))
return;
packet_action(cs, &packet, message);
packet_action(cs, &packet, msgtype);
}
static void add_option_vendor(struct dhcpmsg *packet)

View File

@ -13,9 +13,9 @@
#include "random.h"
static void selecting_packet(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message);
uint8_t msgtype);
static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message);
uint8_t msgtype);
static void selecting_timeout(struct client_state_t *cs, long long nowts);
static void requesting_timeout(struct client_state_t *cs, long long nowts);
static void bound_timeout(struct client_state_t *cs, long long nowts);
@ -28,7 +28,7 @@ static void frenew(struct client_state_t *cs);
typedef struct {
void (*packet_fn)(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message);
uint8_t msgtype);
void (*timeout_fn)(struct client_state_t *cs, long long nowts);
void (*force_renew_fn)(struct client_state_t *cs);
void (*force_release_fn)(struct client_state_t *cs);
@ -186,9 +186,9 @@ static int validate_serverid(struct client_state_t *cs, struct dhcpmsg *packet,
// Can transition to DS_BOUND or DS_SELECTING.
static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message)
uint8_t msgtype)
{
if (*message == DHCPACK) {
if (msgtype == DHCPACK) {
if (!validate_serverid(cs, packet, "a DHCP ACK"))
return;
ssize_t optlen;
@ -230,7 +230,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
set_listen_none(cs);
}
} else if (*message == DHCPNAK) {
} else if (msgtype == DHCPNAK) {
if (!validate_serverid(cs, packet, "a DHCP NAK"))
return;
log_line("Our request was rejected. Searching for a new lease...");
@ -239,9 +239,9 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
}
static void selecting_packet(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message)
uint8_t msgtype)
{
if (*message == DHCPOFFER) {
if (msgtype == DHCPOFFER) {
uint8_t *temp = NULL;
ssize_t optlen;
if ((temp = get_option_data(packet, DHCP_SERVER_ID, &optlen))) {
@ -351,10 +351,10 @@ void ifnocarrier_action(struct client_state_t *cs)
}
void packet_action(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message)
uint8_t msgtype)
{
if (dhcp_states[cs->dhcpState].packet_fn)
dhcp_states[cs->dhcpState].packet_fn(cs, packet, message);
dhcp_states[cs->dhcpState].packet_fn(cs, packet, msgtype);
}
void timeout_action(struct client_state_t *cs, long long nowts)

View File

@ -19,7 +19,7 @@ typedef enum {
void reinit_selecting(struct client_state_t *cs, int timeout);
void packet_action(struct client_state_t *cs, struct dhcpmsg *packet,
uint8_t *message);
uint8_t msgtype);
void timeout_action(struct client_state_t *cs, long long nowts);
void force_renew_action(struct client_state_t *cs);
void force_release_action(struct client_state_t *cs);