mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-25 20:02:37 +03:00
* works * fixup! works * back to callbacks * names * daisy chain * seconds * less inline * fix dns setter * might as well keep using initlist /to d-a-v it has automatic storage, here it's the same stack based one (just one less line for us) * shift blame * naming * fix impl * revert to ip4 dns * merge fix * restyle * masking done wrong
90 lines
2.5 KiB
C++
90 lines
2.5 KiB
C++
|
|
// NAPT example released to public domain
|
|
|
|
#if LWIP_FEATURES && !LWIP_IPV6
|
|
|
|
#define HAVE_NETDUMP 0
|
|
|
|
#ifndef STASSID
|
|
#define STASSID "mynetwork"
|
|
#define STAPSK "mynetworkpassword"
|
|
#endif
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <lwip/napt.h>
|
|
#include <lwip/dns.h>
|
|
|
|
#define NAPT 1000
|
|
#define NAPT_PORT 10
|
|
|
|
#if HAVE_NETDUMP
|
|
|
|
#include <NetDump.h>
|
|
|
|
void dump(int netif_idx, const char* data, size_t len, int out, int success) {
|
|
(void)success;
|
|
Serial.print(out ? F("out ") : F(" in "));
|
|
Serial.printf("%d ", netif_idx);
|
|
|
|
// optional filter example: if (netDump_is_ARP(data))
|
|
{
|
|
netDump(Serial, data, len);
|
|
// netDumpHex(Serial, data, len);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.printf("\n\nNAPT Range extender\n");
|
|
Serial.printf("Heap on start: %d\n", ESP.getFreeHeap());
|
|
|
|
#if HAVE_NETDUMP
|
|
phy_capture = dump;
|
|
#endif
|
|
|
|
// first, connect to STA so we can get a proper local DNS server
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(STASSID, STAPSK);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print('.');
|
|
delay(500);
|
|
}
|
|
Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());
|
|
|
|
// By default, DNS option will point to the interface IP
|
|
// Instead, point it to the real DNS server.
|
|
// Notice that:
|
|
// - DhcpServer class only supports IPv4
|
|
// - Only a single IP can be set
|
|
auto& server = WiFi.softAPDhcpServer();
|
|
server.setDns(WiFi.dnsIP(0));
|
|
|
|
WiFi.softAPConfig( // enable AP, with android-compatible google domain
|
|
IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));
|
|
WiFi.softAP(STASSID "extender", STAPSK);
|
|
Serial.printf("AP: %s\n", WiFi.softAPIP().toString().c_str());
|
|
|
|
Serial.printf("Heap before: %d\n", ESP.getFreeHeap());
|
|
err_t ret = ip_napt_init(NAPT, NAPT_PORT);
|
|
Serial.printf("ip_napt_init(%d,%d): ret=%d (OK=%d)\n", NAPT, NAPT_PORT, (int)ret, (int)ERR_OK);
|
|
if (ret == ERR_OK) {
|
|
ret = ip_napt_enable_no(SOFTAP_IF, 1);
|
|
Serial.printf("ip_napt_enable_no(SOFTAP_IF): ret=%d (OK=%d)\n", (int)ret, (int)ERR_OK);
|
|
if (ret == ERR_OK) { Serial.printf("WiFi Network '%s' with same password is now NATed behind '%s'\n", STASSID "extender", STASSID); }
|
|
}
|
|
Serial.printf("Heap after napt init: %d\n", ESP.getFreeHeap());
|
|
if (ret != ERR_OK) { Serial.printf("NAPT initialization failed\n"); }
|
|
}
|
|
|
|
#else
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.printf("\n\nNAPT not supported in this configuration\n");
|
|
}
|
|
|
|
#endif
|
|
|
|
void loop() {}
|