Skip to content

Commit 98c2837

Browse files
committed
Fix sending NACK, use helper function to fill pbuf
As noticed in esp8266#8582 (comment) Plus, handle the case when `pbuf->len` is less than struct size
1 parent 8bfc2e9 commit 98c2837

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

cores/esp8266/LwipDhcpServer.cpp

+6-21
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ DhcpServer::OptionsBuffer DhcpServer::create_msg(struct dhcps_msg* m)
414414
///////////////////////////////////////////////////////////////////////////////////
415415
void DhcpServer::send_offer(struct dhcps_msg* m)
416416
{
417-
struct pbuf *p, *q;
417+
struct pbuf *p;
418418

419419
auto options = create_msg(m);
420420
options.add(DHCP_OPTION_MSG_TYPE, DHCPOFFER);
@@ -438,12 +438,7 @@ void DhcpServer::send_offer(struct dhcps_msg* m)
438438
os_printf("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len);
439439
os_printf("dhcps: send_offer>>p->len = %d\n", p->len);
440440
#endif
441-
q = p;
442-
while (q != nullptr)
443-
{
444-
std::memcpy((u8_t*)q->payload, reinterpret_cast<u8_t*>(&m), q->len);
445-
q = q->next;
446-
}
441+
pbuf_take(p, m, sizeof(struct dhcps_msg));
447442
}
448443
else
449444
{
@@ -475,7 +470,7 @@ void DhcpServer::send_offer(struct dhcps_msg* m)
475470
///////////////////////////////////////////////////////////////////////////////////
476471
void DhcpServer::send_nak(struct dhcps_msg* m)
477472
{
478-
struct pbuf *p, *q;
473+
struct pbuf *p;
479474

480475
auto options = create_msg(m);
481476
options.add(DHCP_OPTION_MSG_TYPE, DHCPNAK);
@@ -492,12 +487,7 @@ void DhcpServer::send_nak(struct dhcps_msg* m)
492487
os_printf("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len);
493488
os_printf("dhcps: send_nak>>p->len = %d\n", p->len);
494489
#endif
495-
q = p;
496-
while (q != nullptr)
497-
{
498-
std::memcpy((u8_t*)q->payload, (u8_t*)m, q->len);
499-
q = q->next;
500-
}
490+
pbuf_take(p, m, sizeof(struct dhcps_msg));
501491
}
502492
else
503493
{
@@ -524,7 +514,7 @@ void DhcpServer::send_nak(struct dhcps_msg* m)
524514
///////////////////////////////////////////////////////////////////////////////////
525515
void DhcpServer::send_ack(struct dhcps_msg* m)
526516
{
527-
struct pbuf *p, *q;
517+
struct pbuf *p;
528518

529519
auto options = create_msg(m);
530520
options.add(DHCP_OPTION_MSG_TYPE, DHCPACK);
@@ -548,12 +538,7 @@ void DhcpServer::send_ack(struct dhcps_msg* m)
548538
os_printf("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len);
549539
os_printf("dhcps: send_ack>>p->len = %d\n", p->len);
550540
#endif
551-
q = p;
552-
while (q != nullptr)
553-
{
554-
std::memcpy((u8_t*)q->payload, (u8_t*)m, q->len);
555-
q = q->next;
556-
}
541+
pbuf_take(p, m, sizeof(struct dhcps_msg));
557542
}
558543
else
559544
{

0 commit comments

Comments
 (0)