Skip to content

Commit 4108c8d

Browse files
committed
Adds NAPT and port mapping functionality to esp8266 lwip
esp8266 can handle 2 network interfaces (Wifi client and softAP) simultaneously in the WIFI_AP_STA mode. Currently IP_FORWARD is implemented in lwip to forward packets between 2 interfaces, but static routing must be setup manually. This patch adds IP_NAPT config option to enable esp8266 to use NAPT technique on forwarding packets from softAP clients to the parent WiFi AP. To enable NAPT on the interface, ip_napt_enable() must be called with esp8266's softAP interface address. For example: extern "C" void ip_napt_enable(unsigned long addr, int enable); ip_napt_enable(IPAddress(192,168,4,1), 1); This patch also provides port mappings to forward packets from the parent WiFi network to softAP network. To register and unregister port mappings, following functions can be used: void ip_portmap_add(byte proto, unsigned long maddr, unsigned short mport, unsigned long daddr, unsigned short dport); bool ip_portmap_remove(byte proto, unsigned short mport); For exmaple: #define IP_PROTO_TCP 6 #define IP_PROTO_UDP 17 ... ip_portmap_add(IP_PROTO_TCP, WiFi.localIP(), 8080, IPAddress(192,168,4,3), 80); ip_portmap_add(IP_PROTO_UDP, WiFi.localIP(), 12345, IPAddress(192,168,4,2), 12345); These map TCP port 8080 on WiFi client address to 192.168.4.3:80, and UDP port 12345 to 192.168.4.2:12345. To remove the first mapping: ip_portmap_remove(IP_PROTO_TCP, 8080); To apply this change, SDK needs to be recompiled with this patch: $ cd tools/sdk/lwip/src $ make && make release #=> intalled to ../../lib/liblwip_gcc.a Note that this patch is experimental and is provided as is.
1 parent 4897e00 commit 4108c8d

File tree

6 files changed

+662
-2
lines changed

6 files changed

+662
-2
lines changed

tools/sdk/lwip/include/lwip/netif.h

+3
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ struct netif {
228228
u16_t loop_cnt_current;
229229
#endif /* LWIP_LOOPBACK_MAX_PBUFS */
230230
#endif /* ENABLE_LOOPBACK */
231+
#if IP_NAPT
232+
u8_t napt;
233+
#endif
231234
};
232235

233236
#if LWIP_SNMP

tools/sdk/lwip/include/lwip/opt.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,11 @@
501501
* interface, define this to 0.
502502
*/
503503
#ifndef IP_FORWARD
504-
#define IP_FORWARD 0
504+
#define IP_FORWARD 1
505+
#endif
506+
507+
#ifndef IP_NAPT
508+
#define IP_NAPT 1
505509
#endif
506510

507511
/**

tools/sdk/lwip/include/lwipopts.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,11 @@
505505
* interface, define this to 0.
506506
*/
507507
#ifndef IP_FORWARD
508-
#define IP_FORWARD 0
508+
#define IP_FORWARD 1
509+
#endif
510+
511+
#ifndef IP_NAPT
512+
#define IP_NAPT 1
509513
#endif
510514

511515
/**

tools/sdk/lwip/src/core/init.c

+4
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,8 @@ lwip_init(void)
322322
#if LWIP_TIMERS
323323
sys_timeouts_init();
324324
#endif /* LWIP_TIMERS */
325+
326+
#if IP_NAPT
327+
ip_napt_init();
328+
#endif /* IP_NAPT */
325329
}

0 commit comments

Comments
 (0)