Skip to content

Commit 50606a1

Browse files
committed
Fix DHCP ACK parsing
1 parent 6076a46 commit 50606a1

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/test/unit/unit.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,6 +3984,8 @@ START_TEST(test_dhcp_parse_offer_and_ack)
39843984
struct ipconf *primary;
39853985
uint32_t offer_ip = 0x0A000064U;
39863986
uint32_t server_ip = 0x0A000001U;
3987+
uint32_t router_ip = 0x0A000002U;
3988+
uint32_t dns_ip = 0x08080808U;
39873989
uint32_t mask = 0xFFFFFF00U;
39883990

39893991
wolfIP_init(&s);
@@ -4042,17 +4044,34 @@ START_TEST(test_dhcp_parse_offer_and_ack)
40424044
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
40434045
opt->code = DHCP_OPTION_DNS;
40444046
opt->len = 4;
4045-
opt->data[0] = (server_ip >> 24) & 0xFF;
4046-
opt->data[1] = (server_ip >> 16) & 0xFF;
4047-
opt->data[2] = (server_ip >> 8) & 0xFF;
4048-
opt->data[3] = (server_ip >> 0) & 0xFF;
4047+
opt->data[0] = (dns_ip >> 24) & 0xFF;
4048+
opt->data[1] = (dns_ip >> 16) & 0xFF;
4049+
opt->data[2] = (dns_ip >> 8) & 0xFF;
4050+
opt->data[3] = (dns_ip >> 0) & 0xFF;
4051+
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
4052+
opt->code = DHCP_OPTION_ROUTER;
4053+
opt->len = 4;
4054+
opt->data[0] = (router_ip >> 24) & 0xFF;
4055+
opt->data[1] = (router_ip >> 16) & 0xFF;
4056+
opt->data[2] = (router_ip >> 8) & 0xFF;
4057+
opt->data[3] = (router_ip >> 0) & 0xFF;
4058+
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
4059+
opt->code = DHCP_OPTION_OFFER_IP;
4060+
opt->len = 4;
4061+
opt->data[0] = (offer_ip >> 24) & 0xFF;
4062+
opt->data[1] = (offer_ip >> 16) & 0xFF;
4063+
opt->data[2] = (offer_ip >> 8) & 0xFF;
4064+
opt->data[3] = (offer_ip >> 0) & 0xFF;
40494065
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
40504066
opt->code = DHCP_OPTION_END;
40514067
opt->len = 0;
40524068

40534069
ck_assert_int_eq(dhcp_parse_ack(&s, &msg), 0);
40544070
ck_assert_int_eq(s.dhcp_state, DHCP_BOUND);
4055-
ck_assert_uint_eq(s.dns_server, server_ip);
4071+
ck_assert_uint_eq(primary->ip, offer_ip);
4072+
ck_assert_uint_eq(primary->mask, mask);
4073+
ck_assert_uint_eq(primary->gw, router_ip);
4074+
ck_assert_uint_eq(s.dns_server, dns_ip);
40564075
}
40574076
END_TEST
40584077

src/wolfip.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4573,10 +4573,9 @@ static int dhcp_parse_ack(struct wolfIP *s, struct dhcp_msg *msg)
45734573
while (opt->code != 0xFF) {
45744574
if (opt->code == DHCP_OPTION_MSG_TYPE) {
45754575
if (opt->data[0] == DHCP_ACK) {
4576-
uint32_t data;
45774576
opt = (struct dhcp_option *)((uint8_t *)opt + 3);
4578-
data = DHCP_OPT_data_to_u32(opt);
45794577
while (opt->code != 0xFF) {
4578+
uint32_t data = DHCP_OPT_data_to_u32(opt);
45804579
if (opt->code == DHCP_OPTION_SERVER_ID)
45814580
s->dhcp_server_ip = ee32(data);
45824581
if (primary) {

0 commit comments

Comments
 (0)