Skip to content

Commit 072844a

Browse files
mcsprd-a-v
authored andcommitted
Fix DHCP server pbuf handling and allow to change netif address (esp8266#8602)
* 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 * Make sure to call SDK functions to start and stop DHCP server As noticed in esp8266#8582 (comment) Can't really use `server.begin()` and `server.end()` directly, only default static IP is applied to the interface since DHCP server is deemed 'running' (see `wifi_softap_dhcps_status()` return value) * s Co-authored-by: david gauchard <gauchard@laas.fr>
1 parent a22a098 commit 072844a

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
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
{

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
167167
DEBUG_WIFI("[AP] softap config unchanged\n");
168168
}
169169

170-
auto& server = softAPDhcpServer();
171-
server.end();
170+
wifi_softap_dhcps_stop();
172171

173172
// check IP config
174173
struct ip_info ip;
@@ -179,17 +178,13 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
179178
IPAddress(192, 168, 4, 1),
180179
IPAddress(192, 168, 4, 1),
181180
IPAddress(255, 255, 255, 0));
182-
if(!ret) {
183-
DEBUG_WIFI("[AP] softAPConfig failed!\n");
184-
ret = false;
185-
}
186181
}
187182
} else {
188183
DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n");
189184
ret = false;
190185
}
191186

192-
server.begin();
187+
wifi_softap_dhcps_start();
193188

194189
return ret;
195190
}
@@ -227,9 +222,10 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
227222
info.gw.addr = gateway.v4();
228223
info.netmask.addr = subnet.v4();
229224

230-
auto& server = softAPDhcpServer();
231-
server.end();
232-
225+
// use SDK function for dhcps, not just server.begin()
226+
// setting info with static IPs will fail otherwise
227+
// (TODO: dhcps_flag seems to store 'SDK' DHCPs status)
228+
wifi_softap_dhcps_stop();
233229
if(!wifi_set_ip_info(SOFTAP_IF, &info)) {
234230
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
235231
ret = false;
@@ -246,14 +242,17 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
246242
dhcp_lease.end_ip.addr = ip.v4();
247243
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
248244

245+
auto& server = softAPDhcpServer();
249246
if(!server.set_dhcps_lease(&dhcp_lease))
250247
{
251-
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
248+
DEBUG_WIFI("[APConfig] server set_dhcps_lease failed!\n");
252249
ret = false;
253250
}
254251

255-
server.setRouter(true); // send ROUTER option with netif's gateway IP
256-
server.begin();
252+
// send ROUTER option with netif's gateway IP
253+
server.setRouter(true);
254+
255+
wifi_softap_dhcps_start();
257256

258257
// check config
259258
if(wifi_get_ip_info(SOFTAP_IF, &info)) {

0 commit comments

Comments
 (0)