Skip to content

feat(wifi): Add support for NAPT to WIFI AP #9478

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 1 commit into from
Apr 10, 2024
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
26 changes: 21 additions & 5 deletions libraries/Network/src/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,16 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s

esp_netif_flags_t flags = esp_netif_get_flags(_esp_netif);
if(flags & ESP_NETIF_DHCP_SERVER){

// Set DNS Server
if(d2.ip.u_addr.ip4.addr != 0){
err = esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d2);
if(err){
log_e("Netif Set DNS Info Failed! 0x%04x: %s", err, esp_err_to_name(err));
return false;
}
}

// Stop DHCPS
err = esp_netif_dhcps_stop(_esp_netif);
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
Expand All @@ -371,11 +381,6 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
return false;
}

// Set DNS Server
if(d2.ip.u_addr.ip4.addr != 0){
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d2);
}

dhcps_lease_t lease;
lease.enable = true;
uint8_t CIDR = calculateSubnetCIDR(subnet);
Expand Down Expand Up @@ -438,6 +443,17 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
log_e("DHCPS Set Lease Failed! 0x%04x: %s", err, esp_err_to_name(err));
return false;
}

// Offer DNS to DHCP clients
if(d2.ip.u_addr.ip4.addr != 0){
dhcps_offer_t dhcps_dns_value = OFFER_DNS;
err = esp_netif_dhcps_option(_esp_netif, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value));
if(err){
log_e("Netif Set DHCP Option Failed! 0x%04x: %s", err, esp_err_to_name(err));
return false;
}
}

// Start DHCPS
err = esp_netif_dhcps_start(_esp_netif);
if(err){
Expand Down
19 changes: 19 additions & 0 deletions libraries/WiFi/src/AP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <esp_event.h>
#include <lwip/ip_addr.h>
#include "dhcpserver/dhcpserver_options.h"
#include "esp_netif.h"


esp_netif_t* get_esp_interface_netif(esp_interface_t interface);
Expand Down Expand Up @@ -279,6 +280,24 @@ bool APClass::bandwidth(wifi_bandwidth_t bandwidth){
return true;
}

bool APClass::enableNAPT(bool enable){
if(!started()) {
log_e("AP must be first started to enable/disable NAPT");
return false;
}
esp_err_t err = ESP_OK;
if(enable){
err = esp_netif_napt_enable(_esp_netif);
} else {
err = esp_netif_napt_disable(_esp_netif);
}
if(err){
log_e("Could not set enable/disable NAPT! 0x%x: %s", err, esp_err_to_name(err));
return false;
}
return true;
}

String APClass::SSID(void) const{
if(!started()){
return String();
Expand Down
4 changes: 2 additions & 2 deletions libraries/WiFi/src/WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ String WiFiAPClass::softAPSSID() const
* @param gateway gateway IP
* @param subnet subnet mask
*/
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start)
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start, IPAddress dns)
{
return AP.config(local_ip, gateway, subnet, dhcp_lease_start);
return AP.begin() && AP.config(local_ip, gateway, subnet, dhcp_lease_start, dns);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion libraries/WiFi/src/WiFiAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class APClass: public NetworkInterface {
bool clear();

bool bandwidth(wifi_bandwidth_t bandwidth);
bool enableNAPT(bool enable=true);

String SSID(void) const;
uint8_t stationCount();
Expand Down Expand Up @@ -77,7 +78,7 @@ class WiFiAPClass
return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection, ftm_responder, auth_mode, cipher);
}

bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0);
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0, IPAddress dns = (uint32_t) 0);
bool softAPdisconnect(bool wifioff = false);

bool softAPbandwidth(wifi_bandwidth_t bandwidth);
Expand Down