diff --git a/libraries/Network/src/NetworkInterface.cpp b/libraries/Network/src/NetworkInterface.cpp index 01dc53093a9..99896d9060c 100644 --- a/libraries/Network/src/NetworkInterface.cpp +++ b/libraries/Network/src/NetworkInterface.cpp @@ -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){ diff --git a/libraries/WiFi/src/AP.cpp b/libraries/WiFi/src/AP.cpp index 1cf1df18f5a..b245469fb33 100644 --- a/libraries/WiFi/src/AP.cpp +++ b/libraries/WiFi/src/AP.cpp @@ -20,6 +20,7 @@ #include #include #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(); diff --git a/libraries/WiFi/src/WiFiAP.cpp b/libraries/WiFi/src/WiFiAP.cpp index 9b01ee33c79..bb733c09ad2 100644 --- a/libraries/WiFi/src/WiFiAP.cpp +++ b/libraries/WiFi/src/WiFiAP.cpp @@ -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); } /** diff --git a/libraries/WiFi/src/WiFiAP.h b/libraries/WiFi/src/WiFiAP.h index f36db11b0e4..29666c9bc11 100644 --- a/libraries/WiFi/src/WiFiAP.h +++ b/libraries/WiFi/src/WiFiAP.h @@ -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 @@ 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);