From 98c283720939f2a31c8c6992f637663db07d0366 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 14 Jun 2022 23:15:49 +0300 Subject: [PATCH 1/3] Fix sending NACK, use helper function to fill pbuf As noticed in https://github.com/esp8266/Arduino/pull/8582#issuecomment-1153331408 Plus, handle the case when `pbuf->len` is less than struct size --- cores/esp8266/LwipDhcpServer.cpp | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp index ddae5dd4fe..5a3d1fff52 100644 --- a/cores/esp8266/LwipDhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -414,7 +414,7 @@ DhcpServer::OptionsBuffer DhcpServer::create_msg(struct dhcps_msg* m) /////////////////////////////////////////////////////////////////////////////////// void DhcpServer::send_offer(struct dhcps_msg* m) { - struct pbuf *p, *q; + struct pbuf *p; auto options = create_msg(m); options.add(DHCP_OPTION_MSG_TYPE, DHCPOFFER); @@ -438,12 +438,7 @@ void DhcpServer::send_offer(struct dhcps_msg* m) os_printf("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len); os_printf("dhcps: send_offer>>p->len = %d\n", p->len); #endif - q = p; - while (q != nullptr) - { - std::memcpy((u8_t*)q->payload, reinterpret_cast(&m), q->len); - q = q->next; - } + pbuf_take(p, m, sizeof(struct dhcps_msg)); } else { @@ -475,7 +470,7 @@ void DhcpServer::send_offer(struct dhcps_msg* m) /////////////////////////////////////////////////////////////////////////////////// void DhcpServer::send_nak(struct dhcps_msg* m) { - struct pbuf *p, *q; + struct pbuf *p; auto options = create_msg(m); options.add(DHCP_OPTION_MSG_TYPE, DHCPNAK); @@ -492,12 +487,7 @@ void DhcpServer::send_nak(struct dhcps_msg* m) os_printf("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len); os_printf("dhcps: send_nak>>p->len = %d\n", p->len); #endif - q = p; - while (q != nullptr) - { - std::memcpy((u8_t*)q->payload, (u8_t*)m, q->len); - q = q->next; - } + pbuf_take(p, m, sizeof(struct dhcps_msg)); } else { @@ -524,7 +514,7 @@ void DhcpServer::send_nak(struct dhcps_msg* m) /////////////////////////////////////////////////////////////////////////////////// void DhcpServer::send_ack(struct dhcps_msg* m) { - struct pbuf *p, *q; + struct pbuf *p; auto options = create_msg(m); options.add(DHCP_OPTION_MSG_TYPE, DHCPACK); @@ -548,12 +538,7 @@ void DhcpServer::send_ack(struct dhcps_msg* m) os_printf("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len); os_printf("dhcps: send_ack>>p->len = %d\n", p->len); #endif - q = p; - while (q != nullptr) - { - std::memcpy((u8_t*)q->payload, (u8_t*)m, q->len); - q = q->next; - } + pbuf_take(p, m, sizeof(struct dhcps_msg)); } else { From 13b8ff0e889d9b7cede0c6719cbcbf62f5889900 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 14 Jun 2022 23:20:56 +0300 Subject: [PATCH 2/3] Make sure to call SDK functions to start and stop DHCP server As noticed in https://github.com/esp8266/Arduino/pull/8582#issuecomment-1153331408 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) --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 25 ++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 2b7575d2f7..6d5880ad38 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -167,8 +167,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, DEBUG_WIFI("[AP] softap config unchanged\n"); } - auto& server = softAPDhcpServer(); - server.end(); + wifi_softap_dhcps_stop(); // check IP config struct ip_info ip; @@ -179,17 +178,13 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0)); - if(!ret) { - DEBUG_WIFI("[AP] softAPConfig failed!\n"); - ret = false; - } } } else { DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n"); ret = false; } - server.begin(); + wifi_softap_dhcps_start(); return ret; } @@ -227,9 +222,10 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA info.gw.addr = gateway.v4(); info.netmask.addr = subnet.v4(); - auto& server = softAPDhcpServer(); - server.end(); - + // use SDK function for dhcps, not just server.begin() + // setting info with static IPs will fail otherwise + // (TODO: dhcps_flag seems to store 'SDK' DHCPs status) + wifi_softap_dhcps_stop(); if(!wifi_set_ip_info(SOFTAP_IF, &info)) { DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); ret = false; @@ -246,14 +242,17 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA dhcp_lease.end_ip.addr = ip.v4(); DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); + auto& server = softAPDhcpServer(); if(!server.set_dhcps_lease(&dhcp_lease)) { - DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); + DEBUG_WIFI("[APConfig] server set_dhcps_lease failed!\n"); ret = false; } - server.setRouter(true); // send ROUTER option with netif's gateway IP - server.begin(); + // send ROUTER option with netif's gateway IP + server.setRouter(true); + + wifi_softap_dhcps_start(); // check config if(wifi_get_ip_info(SOFTAP_IF, &info)) { From fe8e5572d05713d50aa482ae34a786cd065b836b Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 15 Jun 2022 00:32:22 +0300 Subject: [PATCH 3/3] s --- cores/esp8266/LwipDhcpServer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp index 5a3d1fff52..0f5d2bcf32 100644 --- a/cores/esp8266/LwipDhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -414,7 +414,7 @@ DhcpServer::OptionsBuffer DhcpServer::create_msg(struct dhcps_msg* m) /////////////////////////////////////////////////////////////////////////////////// void DhcpServer::send_offer(struct dhcps_msg* m) { - struct pbuf *p; + struct pbuf* p; auto options = create_msg(m); options.add(DHCP_OPTION_MSG_TYPE, DHCPOFFER); @@ -470,7 +470,7 @@ void DhcpServer::send_offer(struct dhcps_msg* m) /////////////////////////////////////////////////////////////////////////////////// void DhcpServer::send_nak(struct dhcps_msg* m) { - struct pbuf *p; + struct pbuf* p; auto options = create_msg(m); options.add(DHCP_OPTION_MSG_TYPE, DHCPNAK); @@ -514,7 +514,7 @@ void DhcpServer::send_nak(struct dhcps_msg* m) /////////////////////////////////////////////////////////////////////////////////// void DhcpServer::send_ack(struct dhcps_msg* m) { - struct pbuf *p; + struct pbuf* p; auto options = create_msg(m); options.add(DHCP_OPTION_MSG_TYPE, DHCPACK);