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:
parent
27081be84f
commit
7f6721bb82
14
ndhc/dhcp.c
14
ndhc/dhcp.c
@ -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,
|
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) {
|
if (len < sizeof *packet - sizeof packet->options) {
|
||||||
log_line("Packet is too short to contain magic cookie. Ignoring.");
|
log_line("Packet is too short to contain magic cookie. Ignoring.");
|
||||||
return 0;
|
return 0;
|
||||||
@ -496,16 +495,19 @@ static int validate_dhcp_packet(struct client_state_t *cs, int len,
|
|||||||
packet->xid, cs->xid);
|
packet->xid, cs->xid);
|
||||||
return 0;
|
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.");
|
log_line("Packet does not specify a DHCP message type. Ignoring.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
*msgtype = *temp;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_packet(struct client_state_t *cs)
|
void handle_packet(struct client_state_t *cs)
|
||||||
{
|
{
|
||||||
uint8_t *message = NULL;
|
uint8_t msgtype;
|
||||||
int len;
|
int len;
|
||||||
struct dhcpmsg packet;
|
struct dhcpmsg packet;
|
||||||
|
|
||||||
@ -523,9 +525,9 @@ void handle_packet(struct client_state_t *cs)
|
|||||||
change_listen_mode(cs, cs->listenMode);
|
change_listen_mode(cs, cs->listenMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validate_dhcp_packet(cs, len, &packet, &message))
|
if (!validate_dhcp_packet(cs, len, &packet, &msgtype))
|
||||||
return;
|
return;
|
||||||
packet_action(cs, &packet, message);
|
packet_action(cs, &packet, msgtype);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_option_vendor(struct dhcpmsg *packet)
|
static void add_option_vendor(struct dhcpmsg *packet)
|
||||||
|
20
ndhc/state.c
20
ndhc/state.c
@ -13,9 +13,9 @@
|
|||||||
#include "random.h"
|
#include "random.h"
|
||||||
|
|
||||||
static void selecting_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);
|
||||||
static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
|
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 selecting_timeout(struct client_state_t *cs, long long nowts);
|
||||||
static void requesting_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);
|
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 {
|
typedef struct {
|
||||||
void (*packet_fn)(struct client_state_t *cs, struct dhcpmsg *packet,
|
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 (*timeout_fn)(struct client_state_t *cs, long long nowts);
|
||||||
void (*force_renew_fn)(struct client_state_t *cs);
|
void (*force_renew_fn)(struct client_state_t *cs);
|
||||||
void (*force_release_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.
|
// Can transition to DS_BOUND or DS_SELECTING.
|
||||||
static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
|
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"))
|
if (!validate_serverid(cs, packet, "a DHCP ACK"))
|
||||||
return;
|
return;
|
||||||
ssize_t optlen;
|
ssize_t optlen;
|
||||||
@ -230,7 +230,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
|
|||||||
set_listen_none(cs);
|
set_listen_none(cs);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (*message == DHCPNAK) {
|
} else if (msgtype == DHCPNAK) {
|
||||||
if (!validate_serverid(cs, packet, "a DHCP NAK"))
|
if (!validate_serverid(cs, packet, "a DHCP NAK"))
|
||||||
return;
|
return;
|
||||||
log_line("Our request was rejected. Searching for a new lease...");
|
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,
|
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;
|
uint8_t *temp = NULL;
|
||||||
ssize_t optlen;
|
ssize_t optlen;
|
||||||
if ((temp = get_option_data(packet, DHCP_SERVER_ID, &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,
|
void packet_action(struct client_state_t *cs, struct dhcpmsg *packet,
|
||||||
uint8_t *message)
|
uint8_t msgtype)
|
||||||
{
|
{
|
||||||
if (dhcp_states[cs->dhcpState].packet_fn)
|
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)
|
void timeout_action(struct client_state_t *cs, long long nowts)
|
||||||
|
@ -19,7 +19,7 @@ typedef enum {
|
|||||||
void reinit_selecting(struct client_state_t *cs, int timeout);
|
void reinit_selecting(struct client_state_t *cs, int timeout);
|
||||||
|
|
||||||
void packet_action(struct client_state_t *cs, struct dhcpmsg *packet,
|
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 timeout_action(struct client_state_t *cs, long long nowts);
|
||||||
void force_renew_action(struct client_state_t *cs);
|
void force_renew_action(struct client_state_t *cs);
|
||||||
void force_release_action(struct client_state_t *cs);
|
void force_release_action(struct client_state_t *cs);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user