jonask1337 / esp-idf-nat-example

ESP-IDF example project with NAT setup.
82 stars 15 forks source link

What's the dns and dhcp calls for? #16

Closed will-emmerson closed 3 years ago

will-emmerson commented 4 years ago

Sorry to ask a stupid networking question which is probably not really specific to this project, but why are the following lines needed?

    ip_addr_t dnsserver;
    dhcps_offer_t dhcps_dns_value = OFFER_DNS;
    dhcps_set_option_info(DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value));

    // Set custom dns server address for dhcp server
    dnsserver.u_addr.ip4.addr = esp_ip4addr_aton("8.8.8.8");
    dnsserver.type = IPADDR_TYPE_V4;
    dhcps_dns_setserver(&dnsserver);

These aren't necessary in the Espressif Wifi AP example, but obviously they are here otherwise it doesn't work. Is it because DHCP is only enabled for the default connection (in this case wifi STA) normally?

jonask1337 commented 4 years ago

Hi, by default the DHCP server on the ESP32 offers the IP address of the AP Interface (192.168.4.1) as DNS-Server address. If a device wants to reach for example the url www.example.com it sends a request to the DNS-Server, which in this case should be at 192.168.4.1. The problem is that there is no DNS-Server running on the ESP32. In this example this problem is solved by setting the DNS-Server address that the ESP32 DHCP-Server offers to the IP address of the Public-DNS-Server from Google (8.8.8.8).

If you don't need a custom DNS-Server to resolve domain names the code above is not needed. In most examples you only communicate in the ESP32-AP-Network and probably by using the IP address of the device you want to reach so that this is not necessary.

I hope this answers your questions, feel free to ask if you have any further questions. :)

jonask1337 commented 3 years ago

I am closing this issue due to inactivity. Please reopen the issue if it still exists.