Unverified Commit 8c75c352 authored by Me No Dev's avatar Me No Dev Committed by GitHub

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

Allows another interface's connection to be shared to the AP
parent 8ceb4bac
......@@ -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){
......@@ -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);
......@@ -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){
......
......@@ -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);
......@@ -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();
......
......@@ -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);
}
/**
......
......@@ -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();
......@@ -77,7 +78,7 @@ public:
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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment