diff --git a/src/core/ipv4/ip4_napt.c b/src/core/ipv4/ip4_napt.c index 68b07958..28d844fd 100644 --- a/src/core/ipv4/ip4_napt.c +++ b/src/core/ipv4/ip4_napt.c @@ -205,6 +205,37 @@ ip_napt_enable_no(u8_t number, int enable) } } +int +ip_napt_enable_netif(struct netif *netif, int enable) +{ + struct netif *each_netif; + + if (!netif_is_up(netif)){ + return 0; + } + + if (!netif->napt && enable) { + /* Enable napt */ + netif->napt = 1; + ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX); + + } else if (netif->napt && !enable) { + /* Disable napt */ + netif->napt = 0; + + NETIF_FOREACH(each_netif) { + if (each_netif->napt) { + /* napt used on another interface, no need for cleanup */ + return 1; + } + } + ip_napt_deinit(); + } + /* Enabling/disabling napt a second time is a no operation */ + + return 1; +} + /* adjusts checksum in a packet - chksum points to the chksum in the packet - optr points to the old data in the packet (before) diff --git a/src/include/lwip/lwip_napt.h b/src/include/lwip/lwip_napt.h index 605b8eb7..70a587bc 100644 --- a/src/include/lwip/lwip_napt.h +++ b/src/include/lwip/lwip_napt.h @@ -67,6 +67,7 @@ extern "C" { #define IP_NAPT_PORT_RANGE_START 49152 #define IP_NAPT_PORT_RANGE_END 61439 + /** * Enable/Disable NAPT for a specified interface. * @@ -87,6 +88,20 @@ void ip_napt_enable_no(u8_t number, int enable); +/** + * Enable/Disable NAPT for a specified interface. + * + * @param netif interface handle + * @param enable non-zero to enable NAPT, or 0 to disable. + * + * @return + * - 0: Failure + * - 1: Success + */ +int +ip_napt_enable_netif(struct netif *netif, 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.