-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapNat.cpp
More file actions
40 lines (32 loc) · 1010 Bytes
/
Copy pathapNat.cpp
File metadata and controls
40 lines (32 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "apNat.h"
#include <WiFi.h>
esp_netif_t *apNetif()
{
return esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
}
void apOfferUpstreamDns()
{
esp_netif_t *ap = apNetif();
if (!ap) return;
IPAddress up = WiFi.dnsIP();
if ((uint32_t)up == 0) up = IPAddress(8, 8, 8, 8); // fallback if none learned
esp_netif_dns_info_t dns = {};
dns.ip.type = ESP_IPADDR_TYPE_V4;
dns.ip.u_addr.ip4.addr = (uint32_t)up;
esp_netif_dhcps_stop(ap);
esp_netif_set_dns_info(ap, ESP_NETIF_DNS_MAIN, &dns);
uint8_t offerDns = 0x02; // OFFER_DNS: include the DNS option in DHCP leases
esp_netif_dhcps_option(ap, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER,
&offerDns, sizeof(offerDns));
esp_netif_dhcps_start(ap);
}
esp_err_t apNaptEnable()
{
esp_netif_t *ap = apNetif();
return ap ? esp_netif_napt_enable(ap) : ESP_ERR_INVALID_STATE;
}
void apNaptDisable()
{
esp_netif_t *ap = apNetif();
if (ap) esp_netif_napt_disable(ap);
}