esp32_nat_router icon indicating copy to clipboard operation
esp32_nat_router copied to clipboard

Arduino compatibility

Open rw-django-fan-2020 opened this issue 5 years ago • 12 comments

Is there a way to integrate the NAT feature into a Arduino project?

rw-django-fan-2020 avatar Apr 15 '20 16:04 rw-django-fan-2020

Since release 2.6.0 NAPT/NAT is part of the standard distribution. See also this example: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino

Okay, that's ESP8266, not ESP32... (don't know about ESP32)

martin-ger avatar Apr 15 '20 16:04 martin-ger

oh god how good it would be

zekageri avatar Sep 28 '20 15:09 zekageri

Guess you can do that using Arduino as a component of the ESP-IDF: https://github.com/espressif/arduino-esp32/blob/master/docs/esp-idf_component.md In the IDF menuconfig use these setting also: https://github.com/martin-ger/esp32_nat_router#building-the-binaries

martin-ger avatar Sep 28 '20 15:09 martin-ger

I'm gonna check on this! Thanks.

zekageri avatar Sep 29 '20 06:09 zekageri

Do you have some hints on how to integrate that like in the esp8266 release 2.6.0? I want to add this functionality to some other project that it is using platformio, but I am not sure how to compile your code and make it accessible to platformio exactly.

Thank you!

paclema avatar Apr 16 '21 20:04 paclema

The NAT feature is actually included in ESP8266 Arduino, have a look here: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/RangeExtender-NAPT/RangeExtender-NAPT.ino

martin-ger avatar Apr 17 '21 04:04 martin-ger

Thank you @martin-ger ! This is exactly what I want to approach but for the ESP32, not for the ESP8266, but for the ESP32 the lwip libraries are not available like in this example and I am not sure how to integrate them.

Do you know how could I make your awesome libraries accessible to Arduino core for the ESP32?

paclema avatar Apr 17 '21 10:04 paclema

Okay, I understand. The later ESP32 Arduino core includes the lwip_napt.h from the esp-idf ( https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/esp32/include/lwip/lwip/src/include/lwip/lwip_napt.h ).

So I guess it is included in liblwip and you can just include and call it. Haven't tried this so far.

If not, the libs are built without this option on. Then I would try to build the lib with esp-idf an copy it over.

martin-ger avatar Apr 17 '21 11:04 martin-ger

Thank you @martin-ger for your tips! As you mention, I had to re-build esp-idf libs after enabling IP forwarding and NAT so I can access to lwip_napt.h added in the latest ESP32 Arduino core.

I have created this example esp32_lwip_nat_example using my fork for arduino-esp32 framework where I have enabled the LWIP lib. It compiles successfully, however, it is still missing the last part to provide DNS to dhcp server. It looks that the core also contains the dhcpserver however if I try to call the dhcps_set_option_info() and dhcps_dns_setserver() functions, the compiler fails to link the libraries giving the errors:

undefined 
reference to `dhcps_set_option_info(unsigned char, void*, unsigned int)'
undefined 
reference to `dhcps_dns_setserver(ip_addr const*)'

With this example, I can connect the esp32 as a station and I can also connect devices to it, but they don't get internet connection.

Do you know how could I also configure the AP dns to fit with the station dns?

paclema avatar Apr 27 '21 16:04 paclema

Does it change anything if you include "dhcpserver/dhcpserver.h" and "dhcpserver/dhcpserver_options.h" as "extern "C" { ... }?

martin-ger avatar Apr 27 '21 19:04 martin-ger

I think that the libraries are included correctly directly here (no errors while compiling) but I tried adding:

extern "C" {
  #include "dhcpserver/dhcpserver.h"
  #include "dhcpserver/dhcpserver_options.h"
}

And it also includes the libraries correctly but if I uncomment the functions dhcps_set_option_info and dhcps_dns_setserver I still get the linking errors.

I am not sure if this lwip/apps/dhcpserver/dhcpserver.c is perhaps not added correctly into the sdk libraries?

paclema avatar Apr 27 '21 19:04 paclema

I have found the solution to accomplish the last part to provide DNS to DHCP server. The solution is described in this arduino-esp32 issue #5117.

You can also check the working example at this repo: esp32_lwip_nat_example

paclema avatar May 06 '21 09:05 paclema