Skip to content

Commit 8c75c35

Browse files
authored
feat(wifi): Add support for NAPT to WIFI AP (#9478)
Allows another interface's connection to be shared to the AP
1 parent 8ceb4ba commit 8c75c35

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

Diff for: libraries/Network/src/NetworkInterface.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
357357

358358
esp_netif_flags_t flags = esp_netif_get_flags(_esp_netif);
359359
if(flags & ESP_NETIF_DHCP_SERVER){
360+
361+
// Set DNS Server
362+
if(d2.ip.u_addr.ip4.addr != 0){
363+
err = esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d2);
364+
if(err){
365+
log_e("Netif Set DNS Info Failed! 0x%04x: %s", err, esp_err_to_name(err));
366+
return false;
367+
}
368+
}
369+
360370
// Stop DHCPS
361371
err = esp_netif_dhcps_stop(_esp_netif);
362372
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
@@ -371,11 +381,6 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
371381
return false;
372382
}
373383

374-
// Set DNS Server
375-
if(d2.ip.u_addr.ip4.addr != 0){
376-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d2);
377-
}
378-
379384
dhcps_lease_t lease;
380385
lease.enable = true;
381386
uint8_t CIDR = calculateSubnetCIDR(subnet);
@@ -438,6 +443,17 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
438443
log_e("DHCPS Set Lease Failed! 0x%04x: %s", err, esp_err_to_name(err));
439444
return false;
440445
}
446+
447+
// Offer DNS to DHCP clients
448+
if(d2.ip.u_addr.ip4.addr != 0){
449+
dhcps_offer_t dhcps_dns_value = OFFER_DNS;
450+
err = esp_netif_dhcps_option(_esp_netif, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value));
451+
if(err){
452+
log_e("Netif Set DHCP Option Failed! 0x%04x: %s", err, esp_err_to_name(err));
453+
return false;
454+
}
455+
}
456+
441457
// Start DHCPS
442458
err = esp_netif_dhcps_start(_esp_netif);
443459
if(err){

Diff for: libraries/WiFi/src/AP.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <esp_event.h>
2121
#include <lwip/ip_addr.h>
2222
#include "dhcpserver/dhcpserver_options.h"
23+
#include "esp_netif.h"
2324

2425

2526
esp_netif_t* get_esp_interface_netif(esp_interface_t interface);
@@ -279,6 +280,24 @@ bool APClass::bandwidth(wifi_bandwidth_t bandwidth){
279280
return true;
280281
}
281282

283+
bool APClass::enableNAPT(bool enable){
284+
if(!started()) {
285+
log_e("AP must be first started to enable/disable NAPT");
286+
return false;
287+
}
288+
esp_err_t err = ESP_OK;
289+
if(enable){
290+
err = esp_netif_napt_enable(_esp_netif);
291+
} else {
292+
err = esp_netif_napt_disable(_esp_netif);
293+
}
294+
if(err){
295+
log_e("Could not set enable/disable NAPT! 0x%x: %s", err, esp_err_to_name(err));
296+
return false;
297+
}
298+
return true;
299+
}
300+
282301
String APClass::SSID(void) const{
283302
if(!started()){
284303
return String();

Diff for: libraries/WiFi/src/WiFiAP.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ String WiFiAPClass::softAPSSID() const
6767
* @param gateway gateway IP
6868
* @param subnet subnet mask
6969
*/
70-
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start)
70+
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start, IPAddress dns)
7171
{
72-
return AP.config(local_ip, gateway, subnet, dhcp_lease_start);
72+
return AP.begin() && AP.config(local_ip, gateway, subnet, dhcp_lease_start, dns);
7373
}
7474

7575
/**

Diff for: libraries/WiFi/src/WiFiAP.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class APClass: public NetworkInterface {
4848
bool clear();
4949

5050
bool bandwidth(wifi_bandwidth_t bandwidth);
51+
bool enableNAPT(bool enable=true);
5152

5253
String SSID(void) const;
5354
uint8_t stationCount();
@@ -77,7 +78,7 @@ class WiFiAPClass
7778
return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection, ftm_responder, auth_mode, cipher);
7879
}
7980

80-
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0);
81+
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0, IPAddress dns = (uint32_t) 0);
8182
bool softAPdisconnect(bool wifioff = false);
8283

8384
bool softAPbandwidth(wifi_bandwidth_t bandwidth);

0 commit comments

Comments
 (0)