Skip to content

Commit

Permalink
feat(wifi): Add support for NAPT to WIFI AP (espressif#9478)
Browse files Browse the repository at this point in the history
Allows another interface's connection to be shared to the AP
  • Loading branch information
me-no-dev authored and P-R-O-C-H-Y committed Apr 16, 2024
1 parent 53f902f commit f9a06ec
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
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

0 comments on commit f9a06ec

Please sign in to comment.