Skip to content

Commit 8f19382

Browse files
committed
Fix DHCP ACK parsing
1 parent 2a39848 commit 8f19382

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
@@ -4138,6 +4138,8 @@ START_TEST(test_dhcp_parse_offer_and_ack)
41384138
struct ipconf *primary;
41394139
uint32_t offer_ip = 0x0A000064U;
41404140
uint32_t server_ip = 0x0A000001U;
4141+
uint32_t router_ip = 0x0A000002U;
4142+
uint32_t dns_ip = 0x08080808U;
41414143
uint32_t mask = 0xFFFFFF00U;
41424144

41434145
wolfIP_init(&s);
@@ -4196,17 +4198,34 @@ START_TEST(test_dhcp_parse_offer_and_ack)
41964198
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
41974199
opt->code = DHCP_OPTION_DNS;
41984200
opt->len = 4;
4199-
opt->data[0] = (server_ip >> 24) & 0xFF;
4200-
opt->data[1] = (server_ip >> 16) & 0xFF;
4201-
opt->data[2] = (server_ip >> 8) & 0xFF;
4202-
opt->data[3] = (server_ip >> 0) & 0xFF;
4201+
opt->data[0] = (dns_ip >> 24) & 0xFF;
4202+
opt->data[1] = (dns_ip >> 16) & 0xFF;
4203+
opt->data[2] = (dns_ip >> 8) & 0xFF;
4204+
opt->data[3] = (dns_ip >> 0) & 0xFF;
4205+
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
4206+
opt->code = DHCP_OPTION_ROUTER;
4207+
opt->len = 4;
4208+
opt->data[0] = (router_ip >> 24) & 0xFF;
4209+
opt->data[1] = (router_ip >> 16) & 0xFF;
4210+
opt->data[2] = (router_ip >> 8) & 0xFF;
4211+
opt->data[3] = (router_ip >> 0) & 0xFF;
4212+
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
4213+
opt->code = DHCP_OPTION_OFFER_IP;
4214+
opt->len = 4;
4215+
opt->data[0] = (offer_ip >> 24) & 0xFF;
4216+
opt->data[1] = (offer_ip >> 16) & 0xFF;
4217+
opt->data[2] = (offer_ip >> 8) & 0xFF;
4218+
opt->data[3] = (offer_ip >> 0) & 0xFF;
42034219
opt = (struct dhcp_option *)((uint8_t *)opt + 6);
42044220
opt->code = DHCP_OPTION_END;
42054221
opt->len = 0;
42064222

42074223
ck_assert_int_eq(dhcp_parse_ack(&s, &msg), 0);
42084224
ck_assert_int_eq(s.dhcp_state, DHCP_BOUND);
4209-
ck_assert_uint_eq(s.dns_server, server_ip);
4225+
ck_assert_uint_eq(primary->ip, offer_ip);
4226+
ck_assert_uint_eq(primary->mask, mask);
4227+
ck_assert_uint_eq(primary->gw, router_ip);
4228+
ck_assert_uint_eq(s.dns_server, dns_ip);
42104229
}
42114230
END_TEST
42124231

src/wolfip.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4576,10 +4576,9 @@ static int dhcp_parse_ack(struct wolfIP *s, struct dhcp_msg *msg)
45764576
while (opt->code != 0xFF) {
45774577
if (opt->code == DHCP_OPTION_MSG_TYPE) {
45784578
if (opt->data[0] == DHCP_ACK) {
4579-
uint32_t data;
45804579
opt = (struct dhcp_option *)((uint8_t *)opt + 3);
4581-
data = DHCP_OPT_data_to_u32(opt);
45824580
while (opt->code != 0xFF) {
4581+
uint32_t data = DHCP_OPT_data_to_u32(opt);
45834582
if (opt->code == DHCP_OPTION_SERVER_ID)
45844583
s->dhcp_server_ip = ee32(data);
45854584
if (primary) {

0 commit comments

Comments
 (0)