lwip library with NAT feature for Arduino environment
Install the Arduino environment for the esp8266 as described here: https://github.com/esp8266/Arduino . As you are here you probably did this already...
This extension has been developed for the version 2.5 of the ESP8266 core. Switch to that in the Board Manager, if you havn't done already.
Download this repo to some place. Go to the ".../packages/esp8266/hardware/esp8266/2.5.0/tools/sdk/" dirctory of your installation. Here you rename the directory "lwip" to "lwip.orig". Then you copy the complete directory "lwip" of this repo to this place (in fact you replace "lwip" with my implementation).
Whenever you want to use this library, select LwIP Variant: "v1.4 Compile from source in the "Tools" menu of the Arduino shell.
The new functions are exported in the "lwip/lwip_napt.h" header:
/** * Allocates and initializes the NAPT tables. * * @param max_nat max number of enties in the NAPT table (use IP_NAPT_MAX if in doubt) * @param max_portmap max number of enties in the NAPT table (use IP_PORTMAP_MAX if in doubt) */ void ip_napt_init(uint16_t max_nat, uint8_t max_portmap); /** * Enable/Disable NAPT for a specified interface. * * @param addr ip address of the interface * @param enable non-zero to enable NAPT, or 0 to disable. */ void ip_napt_enable(u32_t addr, int enable); /** * Enable/Disable NAPT for a specified interface. * * @param netif number of the interface: 0 = STA, 1 = AP * @param enable non-zero to enable NAPT, or 0 to disable. */ void ip_napt_enable_no(u8_t number, int enable); /** * Register port mapping on the external interface to internal interface. * When the same port mapping is registered again, the old mapping is overwritten. * In this implementation, only 1 unique port mapping can be defined for each target address/port. * * @param proto target protocol * @param maddr ip address of the external interface * @param mport mapped port on the external interface, in host byte order. * @param daddr destination ip address * @param dport destination port, in host byte order. */ u8_t ip_portmap_add(u8_t proto, u32_t maddr, u16_t mport, u32_t daddr, u16_t dport); /** * Unregister port mapping on the external interface to internal interface. * * @param proto target protocol * @param maddr ip address of the external interface */ u8_t ip_portmap_remove(u8_t proto, u16_t mport); /** * Sets the NAPT timeout for TCP connections. * * @param secs timeout in secs */ void ip_napt_set_tcp_timeout(u32_t secs); /** * Sets the NAPT timeout for UDP 'connections'. * * @param secs timeout in secs */ void ip_napt_set_udp_timeout(u32_t secs); In addition, the following extensions to the DHCP server of the AP interface might help:
void dhcps_set_DNS(struct ip_addr *dns_ip) ICACHE_FLASH_ATTR; This sets the DNS server that is distributed to the stations connected to the AP interface.
For an example look into: "WiFiNATRouter.ino" that sets up a basic NAT router between the AP and the STA interface (works like a basic version of https://github.com/martin-ger/esp_wifi_repeater )