From 7f6721bb8283d11644794f436cae3ec7bf16de3e Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Mon, 18 Jul 2011 10:47:35 -0400 Subject: [PATCH] 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. --- ndhc/dhcp.c | 14 ++++++++------ ndhc/state.c | 20 ++++++++++---------- ndhc/state.h | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ndhc/dhcp.c b/ndhc/dhcp.c index 2200360..acb190b 100644 --- a/ndhc/dhcp.c +++ b/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, - 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) diff --git a/ndhc/state.c b/ndhc/state.c index 75e14da..098aad2 100644 --- a/ndhc/state.c +++ b/ndhc/state.c @@ -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) diff --git a/ndhc/state.h b/ndhc/state.h index ab86e7f..3ba5f16 100644 --- a/ndhc/state.h +++ b/ndhc/state.h @@ -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);