Skip to content

Fix DHCP server pbuf handling and allow to change netif address #8602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions cores/esp8266/LwipDhcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<u8_t*>(&m), q->len);
q = q->next;
}
pbuf_take(p, m, sizeof(struct dhcps_msg));
}
else
{
Expand Down Expand Up @@ -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);
Expand All @@ -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
{
Expand All @@ -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);
Expand All @@ -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
{
Expand Down
25 changes: 12 additions & 13 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down